yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.1.0
Estimate0D.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2025 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_Estimate0D_h
7#define YODA_Estimate0D_h
8
10#include "YODA/Exceptions.h"
11#include "YODA/Estimate.h"
12#include "YODA/Scatter.h"
13
14#ifdef HAVE_HDF5
15#include "YODA/Utils/H5Utils.h"
16#endif
17
18#include <string>
19
20namespace YODA {
21
27 class Estimate0D : public Estimate,
28 public AnalysisObject {
29 public:
30
31 using Ptr = std::shared_ptr<Estimate0D>;
32 using BaseT = Estimate;
33 using BaseT::operator =;
34 using BaseT::operator +=;
35 using BaseT::operator -=;
36 using AnalysisObject::operator =;
37
39
40
45 Estimate0D(const std::string& path = "", const std::string& title = "")
46 : BaseT(), AnalysisObject("Estimate0D", path, title) { }
47
48
52 Estimate0D(double v,
53 std::map<std::string,std::pair<double,double>>& errors,
54 const std::string& path = "", const std::string& title = "")
55 : BaseT(v, errors), AnalysisObject("Estimate0D", path, title) { }
56
57
59 Estimate0D(const double v, const std::pair<double,double>& e, const std::string& source = "",
60 const std::string& path = "", const std::string& title = "")
61 : BaseT(v, e, source), AnalysisObject("Estimate0D", path, title) { }
62
66 Estimate0D(const Estimate0D& other) : Estimate(other),
67 AnalysisObject(other.type(), other.path(), other, other.title()) { }
68
70 Estimate0D(Estimate0D&& other) : BaseT(std::move(other)),
71 AnalysisObject(other.type(), other.path(), other, other.title()) { }
72
74 Estimate0D(const BaseT& other, const std::string& path = "", const std::string& title = "")
75 : BaseT(other), AnalysisObject("Estimate0D", path, title) { }
76
78 Estimate0D(BaseT&& other, const std::string& path = "", const std::string& title = "")
79 : BaseT(std::move(other)), AnalysisObject("Estimate0D", path, title) { }
80
82 Estimate0D clone() const noexcept {
83 return Estimate0D(*this);
84 }
85
87 Estimate0D* newclone() const noexcept {
88 return new Estimate0D(*this);
89 }
90
92
93
95
96
100 Estimate0D& operator=(const Estimate0D& toCopy) noexcept {
101 if (this != &toCopy) {
103 BaseT::operator = (toCopy);
104 }
105 return *this;
106 }
107
109 Estimate0D& operator = (Estimate0D&& toMove) noexcept {
110 if (this != &toMove) {
112 BaseT::operator = (std::move(toMove));
113 }
114 return *this;
115 }
116
118 Estimate0D& add(const Estimate0D& toAdd, const std::string& pat_uncorr="^stat|^uncor" ) {
119 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
120 BaseT::add(toAdd, pat_uncorr);
121 return *this;
122 }
123 //
125 return add(toAdd);
126 }
127
129 Estimate0D& add(Estimate0D&& toAdd, const std::string& pat_uncorr="^stat|^uncor" ) {
130 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
131 BaseT::add(std::move(toAdd), pat_uncorr);
132 return *this;
133 }
134 //
136 return add(std::move(toAdd));
137 }
138
140 Estimate0D& subtract(const Estimate0D& toSubtract, const std::string& pat_uncorr="^stat|^uncor" ) {
141 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
142 BaseT::subtract(toSubtract, pat_uncorr);
143 return *this;
144 }
145 //
146 Estimate0D& operator-=(const Estimate0D& toSubtract) {
147 return subtract(toSubtract);
148 }
149
151 Estimate0D& subtract(Estimate0D&& toSubtract, const std::string& pat_uncorr="^stat|^uncor" ) {
152 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
153 BaseT::subtract(std::move(toSubtract), pat_uncorr);
154 return *this;
155 }
157 return subtract(std::move(toSubtract));
158 }
159
161
164
166 size_t dim() const noexcept { return 1; }
167
169
171
172
174 void reset() noexcept {
175 BaseT::reset();
176 }
177
179
180 // @brief Render information about this AO
181 void _renderYODA(std::ostream& os, const int width = 13) const noexcept {
182
183 // Render error sources
184 const std::vector<std::string> labels = this->sources();
185 if (labels.size()) {
186 os << "ErrorLabels: [";
187 for (size_t i = 0; i < labels.size(); ++i) {
188 const std::string& src = labels[i];
189 if (i) os << ", ";
190 os << std::quoted(src);
191 }
192 os << "]\n";
193 }
194
195 // column header: content types
196 os << std::setw(width) << std::left << "# value" << "\t";
197 const int errwidth = std::max(int(std::to_string(labels.size()).size()+7), width); // "errDn(" + src + ")"
198 for (size_t i = 0; i < labels.size(); ++i) {
199 const std::string& src = labels[i];
200 if (src.empty()) {
201 os << std::setw(errwidth) << std::left << "totalDn" << "\t";
202 os << std::setw(errwidth) << std::left << "totalUp" << "\t";
203 }
204 else {
205 os << std::setw(errwidth) << std::left << ("errDn(" + std::to_string(i+1) + ")") << "\t";
206 os << std::setw(errwidth) << std::left << ("errUp(" + std::to_string(i+1) + ")") << "\t";
207 }
208 }
209 os << "\n";
210
211 os << std::setw(width) << std::left << val() << "\t"; // render value
212 // render systs if available
213 for (const std::string& src : labels) {
214 if (!hasSource(src)) {
215 os << std::setw(errwidth) << std::left << "---" << "\t"
216 << std::setw(errwidth) << std::left << "---" << "\t";
217 continue;
218 }
219 const auto& errs = err(src);
220 os << std::setw(errwidth) << std::left << errs.first << "\t"
221 << std::setw(errwidth) << std::left << errs.second << "\t";
222 }
223 os << "\n";
224 }
225
226 // @brief Render scatter-like information about this AO
227 void _renderFLAT(std::ostream& os, const int width = 13) const noexcept {
228 const Scatter1D tmp = mkScatter();
229 tmp._renderYODA(os, width);
230 }
231
232 #ifdef HAVE_HDF5
233
234 // @brief Extract error labels of this AO
235 void _extractLabels(std::vector<std::string>& labels,
236 std::vector<size_t>& labelsizes) const noexcept {
237 // append new set of keys
238 std::vector<std::string> keys = sources();
239 labelsizes.emplace_back(keys.size()+1); //< +1 for length info itself
240 labels.insert(std::end(labels),
241 std::make_move_iterator(std::begin(keys)),
242 std::make_move_iterator(std::end(keys)));
243 // sort and remove duplicates
244 std::sort(labels.begin(), labels.end());
245 labels.erase( std::unique(labels.begin(), labels.end()), labels.end() );
246 }
247
248 // @brief Extract error breakdown of this AO into the map of @a edges
249 void _extractEdges(std::map<std::string, EdgeHandlerBasePtr>& edges,
250 const std::vector<std::string>& labels) const noexcept {
251
252 using lenT = EdgeHandler<size_t>;
253 using lenPtr = EdgeHandlerPtr<size_t>;
254 const std::string lengthID("sizeinfo");
255 lenPtr nedges = std::static_pointer_cast<lenT>(edges.find(lengthID)->second);
256
257 std::vector<std::string> keys = sources();
258 nedges->extend({ keys.size() });
259 const auto& itr = labels.cbegin();
260 const auto& itrEnd = labels.cend();
261 for (const string& k : keys) {
262 size_t dist = std::find(itr, itrEnd, k) - itr;
263 nedges->extend({ dist });
264 }
265
266 }
267
268 #endif
269
271
274
275 size_t lengthContent(bool fixed_length = false) const noexcept {
276 return BaseT::_lengthContent(fixed_length);
277 }
278
279 std::vector<double> serializeContent(bool fixed_length = false) const noexcept {
280 return BaseT::_serializeContent(fixed_length);
281 }
282
283 void deserializeContent(const std::vector<double>& data) {
284 BaseT::_deserializeContent(data, data.size() == 4);
285 }
286
288
290
291
292 inline Scatter1D mkScatter(const std::string& path = "",
293 const std::string& pat_match = "") const noexcept {
294 Scatter1D rtn;
295 for (const std::string& a : annotations()) {
296 if (a != "Type") rtn.setAnnotation(a, annotation(a));
297 }
298 rtn.setAnnotation("Path", path);
299
300 // Add the PointND
301 const double tot = fabs(totalErrPos(pat_match)); // use positive error component
302 rtn.addPoint( Point1D(val(), {tot, tot}) );
303
304 return rtn;
305 }
306
308 AnalysisObject* mkInert(const std::string& path = "",
309 const std::string& source = "") const noexcept {
310 Estimate0D* rtn = newclone();
311 rtn->setPath(path);
312 if (rtn->numErrs() == 1) {
313 try {
314 rtn->renameSource("", source);
315 }
316 catch (YODA::UserError& e) { }
317 }
318 return rtn;
319 }
320
322
323 };
324
327
328 inline void transform(Estimate0D& est, const Trf<1>& fn) {
329 est.transform(fn);
330 }
331
332 template <typename FN>
333 inline void transform(Estimate0D& est, const FN& fn) {
334 transform(est, Trf<1>(fn));
335 }
336
338
339
341
342
344 inline Estimate0D operator + (Estimate0D lhs, const Estimate0D& rhs) {
345 lhs += rhs;
346 return lhs;
347 }
348
351 lhs += std::move(rhs);
352 return lhs;
353 }
354
356 inline Estimate0D operator - (Estimate0D lhs, const Estimate0D& rhs) {
357 lhs -= rhs;
358 return lhs;
359 }
360
363 lhs -= std::move(rhs);
364 return lhs;
365 }
366
368 inline Estimate0D divide(const Estimate0D& numer, const Estimate0D& denom,
369 const std::string& pat_uncorr="^stat|^uncor" ) {
370 Estimate0D rtn = divide(static_cast<const Estimate&>(numer),
371 static_cast<const Estimate&>(denom), pat_uncorr);
372 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
373 if (numer.path() == denom.path()) rtn.setPath(numer.path());
374 return rtn;
375 }
376
378 inline Estimate0D operator / (const Estimate0D& numer, const Estimate0D& denom) {
379 return divide(numer, denom);
380 }
381
383 inline Estimate0D operator / (Estimate0D&& numer, const Estimate0D& denom) {
384 return divide(std::move(numer), denom);
385 }
386
388 inline Estimate0D operator / (const Estimate0D& numer, Estimate0D&& denom) {
389 return divide(numer, std::move(denom));
390 }
391
393 inline Estimate0D operator / (Estimate0D&& numer, Estimate0D&& denom) {
394 return divide(std::move(numer), std::move(denom));
395 }
396
398 inline Estimate0D efficiency(const Estimate0D& accepted, const Estimate0D& total,
399 const std::string& pat_uncorr="^stat|^uncor" ) {
400 Estimate0D rtn = efficiency(static_cast<const Estimate&>(accepted),
401 static_cast<const Estimate&>(total), pat_uncorr);
402 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
403 if (accepted.path() == total.path()) rtn.setPath(total.path());
404 return rtn;
405 }
406
408
409}
410
411#endif
AnalysisObject is the base class for histograms and scatters.
virtual AnalysisObject & operator=(const AnalysisObject &ao) noexcept
Default copy assignment operator.
virtual std::string type() const
Get name of the analysis object type.
void setAnnotation(const std::string &name, const T &value)
Add or set an annotation by name (templated for remaining types)
void setPath(const std::string &path)
const std::string title() const
Get the AO title.
std::vector< std::string > annotations() const
const std::string path() const
Get the AO path.
void rmAnnotation(const std::string &name)
Delete an annotation by name.
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
An estimate in 0D.
Definition Estimate0D.h:28
Estimate0D(const std::string &path="", const std::string &title="")
Nullary constructor for unique pointers etc.
Definition Estimate0D.h:45
Estimate0D(double v, std::map< std::string, std::pair< double, double > > &errors, const std::string &path="", const std::string &title="")
Constructor to set an Estimate0D with a pre-filled state.
Definition Estimate0D.h:52
Estimate0D & subtract(const Estimate0D &toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract two Estimate0Ds.
Definition Estimate0D.h:140
Estimate0D & operator-=(Estimate0D &&toSubtract)
Definition Estimate0D.h:156
Estimate0D & subtract(Estimate0D &&toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract two (rvalue) Estimate0Ds.
Definition Estimate0D.h:151
Estimate0D(Estimate0D &&other)
Move constructor.
Definition Estimate0D.h:70
Scatter1D mkScatter(const std::string &path="", const std::string &pat_match="") const noexcept
Definition Estimate0D.h:292
std::shared_ptr< Estimate0D > Ptr
Definition Estimate0D.h:31
Estimate0D & operator+=(const Estimate0D &toAdd)
Definition Estimate0D.h:124
void reset() noexcept
Reset the internal values.
Definition Estimate0D.h:174
Estimate0D(BaseT &&other, const std::string &path="", const std::string &title="")
Move constructor using base class.
Definition Estimate0D.h:78
Estimate0D * newclone() const noexcept
Make a copy on the heap.
Definition Estimate0D.h:87
Estimate0D(const BaseT &other, const std::string &path="", const std::string &title="")
Copy constructor using base class.
Definition Estimate0D.h:74
AnalysisObject * mkInert(const std::string &path="", const std::string &source="") const noexcept
Method returns clone of the estimate with streamlined error source.
Definition Estimate0D.h:308
size_t lengthContent(bool fixed_length=false) const noexcept
Length of serialized content vector for MPI reduce operations.
Definition Estimate0D.h:275
Estimate0D & add(const Estimate0D &toAdd, const std::string &pat_uncorr="^stat|^uncor")
Add two Estimate0Ds.
Definition Estimate0D.h:118
size_t dim() const noexcept
Total dimension of this data object.
Definition Estimate0D.h:166
Estimate0D(const double v, const std::pair< double, double > &e, const std::string &source="", const std::string &path="", const std::string &title="")
Alternative constructor to set an Estimate0D with value and uncertainty.
Definition Estimate0D.h:59
void deserializeContent(const std::vector< double > &data)
Content deserialisation for MPI reduce operations.
Definition Estimate0D.h:283
Estimate0D & operator=(const Estimate0D &toCopy) noexcept
Definition Estimate0D.h:100
Estimate0D clone() const noexcept
Make a copy on the stack.
Definition Estimate0D.h:82
std::vector< double > serializeContent(bool fixed_length=false) const noexcept
Content serialisation for MPI reduce operations.
Definition Estimate0D.h:279
Estimate0D(const Estimate0D &other)
Copy constructor (needed for clone functions).
Definition Estimate0D.h:66
Estimate0D & operator-=(const Estimate0D &toSubtract)
Definition Estimate0D.h:146
Estimate0D & operator+=(Estimate0D &&toAdd)
Definition Estimate0D.h:135
Estimate0D & add(Estimate0D &&toAdd, const std::string &pat_uncorr="^stat|^uncor")
Add two (rvalue) Estimate0Ds.
Definition Estimate0D.h:129
A point estimate (base class for the Estimate)
Definition Estimate.h:29
Estimate & add(const Estimate &toAdd, const std::string &pat_uncorr="^stat|^uncor")
Add two Estimates.
Definition Estimate.h:95
size_t numErrs() const noexcept
The number of error sources in the error map.
Definition Estimate.h:499
double totalErrPos(const std::string &pat_match="") const noexcept
The positive total uncertainty.
Definition Estimate.h:423
std::vector< std::string > sources() const noexcept
The list of error source names.
Definition Estimate.h:487
void transform(const Trf< 1 > &trf)
Generalised transformations with functors.
Definition Estimate.h:214
Estimate & operator=(const Estimate &toCopy) noexcept
Definition Estimate.h:69
double val() const noexcept
The central value.
Definition Estimate.h:249
Estimate()
Default constructor of a new distribution.
Definition Estimate.h:36
void renameSource(const std::string &old_label, const std::string &new_label)
Replace a source label in the error breakdown.
Definition Estimate.h:219
std::pair< double, double > err(const std::string &source="") const
Convenience alias for errorDownUp(source)
Definition Estimate.h:263
Estimate & subtract(const Estimate &toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract one Estimate from another.
Definition Estimate.h:125
bool hasSource(const std::string &key) const noexcept
Returns true/false if the error map contains key.
Definition Estimate.h:494
void reset() noexcept
Reset the internal values.
Definition Estimate.h:194
A generic data type which is just a collection of n-dim data points with errors.
Definition Scatter.h:153
ScatterND< N > & addPoint(const PointND< N > &pt)
Insert a new point.
Definition Scatter.h:378
Error for problems introduced outside YODA, to put it nicely.
Definition Exceptions.h:86
Anonymous namespace to limit visibility.
void transform(BinnedEstimate< AxisT... > &est, const Trf< 1 > &fn)
PointND< 1 > Point1D
User-familiar alias.
Definition Point.h:714
BinnedDbn< DbnN, AxisT... > operator+(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Add two BinnedDbn objects.
Definition BinnedDbn.h:1142
BinnedEstimate< AxisT... > operator/(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Definition BinnedDbn.h:1215
BinnedEstimate< AxisT... > efficiency(const BinnedDbn< DbnN, AxisT... > &accepted, const BinnedDbn< DbnN, AxisT... > &total)
Calculate a binned efficiency ratio of two BinnedDbn objects.
Definition BinnedDbn.h:1244
BinnedDbn< DbnN, AxisT... > operator-(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Subtract one BinnedDbn object from another.
Definition BinnedDbn.h:1158
BinnedEstimate< AxisT... > divide(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Divide two BinnedDbn objects.
Definition BinnedDbn.h:1174
ScatterND< 1 > Scatter1D
Definition Scatter.h:901