yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
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-2024 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#include <string>
15
16namespace YODA {
17
23 class Estimate0D : public Estimate,
24 public AnalysisObject {
25 public:
26
27 using Ptr = std::shared_ptr<Estimate0D>;
28 using BaseT = Estimate;
29 using BaseT::operator =;
30 using BaseT::operator +=;
31 using BaseT::operator -=;
32 using AnalysisObject::operator =;
33
35
36
41 Estimate0D(const std::string& path = "", const std::string& title = "")
42 : BaseT(), AnalysisObject("Estimate0D", path, title) { }
43
44
48 Estimate0D(double v,
49 std::map<std::string,std::pair<double,double>>& errors,
50 const std::string& path = "", const std::string& title = "")
51 : BaseT(v, errors), AnalysisObject("Estimate0D", path, title) { }
52
53
55 Estimate0D(const double v, const std::pair<double,double>& e, const std::string& source = "",
56 const std::string& path = "", const std::string& title = "")
57 : BaseT(v, e, source), AnalysisObject("Estimate0D", path, title) { }
58
62 Estimate0D(const Estimate0D& other) : Estimate(other),
63 AnalysisObject(other.type(), other.path(), other, other.title()) { }
64
66 Estimate0D(Estimate0D&& other) : BaseT(std::move(other)),
67 AnalysisObject(other.type(), other.path(), other, other.title()) { }
68
70 Estimate0D(const BaseT& other, const std::string& path = "", const std::string& title = "")
71 : BaseT(other), AnalysisObject("Estimate0D", path, title) { }
72
74 Estimate0D(BaseT&& other, const std::string& path = "", const std::string& title = "")
75 : BaseT(std::move(other)), AnalysisObject("Estimate0D", path, title) { }
76
78 Estimate0D clone() const noexcept {
79 return Estimate0D(*this);
80 }
81
83 Estimate0D* newclone() const noexcept {
84 return new Estimate0D(*this);
85 }
86
88
89
91
92
96 Estimate0D& operator=(const Estimate0D& toCopy) noexcept {
97 if (this != &toCopy) {
99 BaseT::operator = (toCopy);
100 }
101 return *this;
102 }
103
105 Estimate0D& operator = (Estimate0D&& toMove) noexcept {
106 if (this != &toMove) {
108 BaseT::operator = (std::move(toMove));
109 }
110 return *this;
111 }
112
114 Estimate0D& add(const Estimate0D& toAdd, const std::string& pat_uncorr="^stat|^uncor" ) {
115 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
116 BaseT::add(toAdd, pat_uncorr);
117 return *this;
118 }
119 //
121 return add(toAdd);
122 }
123
125 Estimate0D& add(Estimate0D&& toAdd, const std::string& pat_uncorr="^stat|^uncor" ) {
126 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
127 BaseT::add(std::move(toAdd), pat_uncorr);
128 return *this;
129 }
130 //
132 return add(std::move(toAdd));
133 }
134
136 Estimate0D& subtract(const Estimate0D& toSubtract, const std::string& pat_uncorr="^stat|^uncor" ) {
137 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
138 BaseT::subtract(toSubtract, pat_uncorr);
139 return *this;
140 }
141 //
142 Estimate0D& operator-=(const Estimate0D& toSubtract) {
143 return subtract(toSubtract);
144 }
145
147 Estimate0D& subtract(Estimate0D&& toSubtract, const std::string& pat_uncorr="^stat|^uncor" ) {
148 if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
149 BaseT::subtract(std::move(toSubtract), pat_uncorr);
150 return *this;
151 }
153 return subtract(std::move(toSubtract));
154 }
155
157
160
162 size_t dim() const noexcept { return 1; }
163
165
167
168
170 void reset() noexcept {
171 BaseT::reset();
172 }
173
175
176 // @brief Render information about this AO
177 void _renderYODA(std::ostream& os, const int width = 13) const noexcept {
178
179 // Render error sources
180 const std::vector<std::string> labels = this->sources();
181 if (labels.size()) {
182 os << "ErrorLabels: [";
183 for (size_t i = 0; i < labels.size(); ++i) {
184 const std::string& src = labels[i];
185 if (i) os << ", ";
186 os << std::quoted(src);
187 }
188 os << "]\n";
189 }
190
191 // column header: content types
192 os << std::setw(width) << std::left << "# value" << "\t";
193 const int errwidth = std::max(int(std::to_string(labels.size()).size()+7), width); // "errDn(" + src + ")"
194 for (size_t i = 0; i < labels.size(); ++i) {
195 const std::string& src = labels[i];
196 if (src.empty()) {
197 os << std::setw(errwidth) << std::left << "totalDn" << "\t";
198 os << std::setw(errwidth) << std::left << "totalUp" << "\t";
199 }
200 else {
201 os << std::setw(errwidth) << std::left << ("errDn(" + std::to_string(i+1) + ")") << "\t";
202 os << std::setw(errwidth) << std::left << ("errUp(" + std::to_string(i+1) + ")") << "\t";
203 }
204 }
205 os << "\n";
206
207 os << std::setw(width) << std::left << val() << "\t"; // render value
208 // render systs if available
209 for (const std::string& src : labels) {
210 if (!hasSource(src)) {
211 os << std::setw(errwidth) << std::left << "---" << "\t"
212 << std::setw(errwidth) << std::left << "---" << "\t";
213 continue;
214 }
215 const auto& errs = err(src);
216 os << std::setw(errwidth) << std::left << errs.first << "\t"
217 << std::setw(errwidth) << std::left << errs.second << "\t";
218 }
219 os << "\n";
220 }
221
222 // @brief Render scatter-like information about this AO
223 void _renderFLAT(std::ostream& os, const int width = 13) const noexcept {
224 const Scatter1D tmp = mkScatter();
225 tmp._renderYODA(os, width);
226 }
227
229
232
233 size_t lengthContent(bool fixed_length = false) const noexcept {
234 return BaseT::_lengthContent(fixed_length);
235 }
236
237 std::vector<double> serializeContent(bool fixed_length = false) const noexcept {
238 return BaseT::_serializeContent(fixed_length);
239 }
240
241 void deserializeContent(const std::vector<double>& data) {
242 BaseT::_deserializeContent(data, data.size() == 4);
243 }
244
246
248
249
250 inline Scatter1D mkScatter(const std::string& path = "",
251 const std::string& pat_match = "") const noexcept {
252 Scatter1D rtn;
253 for (const std::string& a : annotations()) {
254 if (a != "Type") rtn.setAnnotation(a, annotation(a));
255 }
256 rtn.setAnnotation("Path", path);
257
258 // Add the PointND
259 const double tot = fabs(totalErrPos(pat_match)); // use positive error component
260 rtn.addPoint( Point1D(val(), {tot, tot}) );
261
262 return rtn;
263 }
264
266 AnalysisObject* mkInert(const std::string& path = "",
267 const std::string& source = "") const noexcept {
268 Estimate0D* rtn = newclone();
269 rtn->setPath(path);
270 if (rtn->numErrs() == 1) {
271 try {
272 rtn->renameSource("", source);
273 }
274 catch (YODA::UserError& e) { }
275 }
276 return rtn;
277 }
278
280
281 };
282
285
286 inline void transform(Estimate0D& est, const Trf<1>& fn) {
287 est.transform(fn);
288 }
289
290 template <typename FN>
291 inline void transform(Estimate0D& est, const FN& fn) {
292 transform(est, Trf<1>(fn));
293 }
294
296
297
299
300
302 inline Estimate0D operator + (Estimate0D lhs, const Estimate0D& rhs) {
303 lhs += rhs;
304 return lhs;
305 }
306
309 lhs += std::move(rhs);
310 return lhs;
311 }
312
314 inline Estimate0D operator - (Estimate0D lhs, const Estimate0D& rhs) {
315 lhs -= rhs;
316 return lhs;
317 }
318
321 lhs -= std::move(rhs);
322 return lhs;
323 }
324
326 inline Estimate0D divide(const Estimate0D& numer, const Estimate0D& denom,
327 const std::string& pat_uncorr="^stat|^uncor" ) {
328 Estimate0D rtn = divide(static_cast<const Estimate&>(numer),
329 static_cast<const Estimate&>(denom), pat_uncorr);
330 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
331 if (numer.path() == denom.path()) rtn.setPath(numer.path());
332 return rtn;
333 }
334
336 inline Estimate0D operator / (const Estimate0D& numer, const Estimate0D& denom) {
337 return divide(numer, denom);
338 }
339
341 inline Estimate0D operator / (Estimate0D&& numer, const Estimate0D& denom) {
342 return divide(std::move(numer), denom);
343 }
344
346 inline Estimate0D operator / (const Estimate0D& numer, Estimate0D&& denom) {
347 return divide(numer, std::move(denom));
348 }
349
351 inline Estimate0D operator / (Estimate0D&& numer, Estimate0D&& denom) {
352 return divide(std::move(numer), std::move(denom));
353 }
354
356 inline Estimate0D efficiency(const Estimate0D& accepted, const Estimate0D& total,
357 const std::string& pat_uncorr="^stat|^uncor" ) {
358 Estimate0D rtn = efficiency(static_cast<const Estimate&>(accepted),
359 static_cast<const Estimate&>(total), pat_uncorr);
360 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
361 if (accepted.path() == total.path()) rtn.setPath(total.path());
362 return rtn;
363 }
364
366
367}
368
369#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:24
Estimate0D(const std::string &path="", const std::string &title="")
Nullary constructor for unique pointers etc.
Definition Estimate0D.h:41
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:48
Estimate0D & subtract(const Estimate0D &toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract two Estimate0Ds.
Definition Estimate0D.h:136
Estimate0D & operator-=(Estimate0D &&toSubtract)
Definition Estimate0D.h:152
Estimate0D & subtract(Estimate0D &&toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract two (rvalue) Estimate0Ds.
Definition Estimate0D.h:147
Estimate0D(Estimate0D &&other)
Move constructor.
Definition Estimate0D.h:66
Scatter1D mkScatter(const std::string &path="", const std::string &pat_match="") const noexcept
Definition Estimate0D.h:250
std::shared_ptr< Estimate0D > Ptr
Definition Estimate0D.h:27
Estimate0D & operator+=(const Estimate0D &toAdd)
Definition Estimate0D.h:120
void reset() noexcept
Reset the internal values.
Definition Estimate0D.h:170
Estimate0D(BaseT &&other, const std::string &path="", const std::string &title="")
Move constructor using base class.
Definition Estimate0D.h:74
Estimate0D * newclone() const noexcept
Make a copy on the heap.
Definition Estimate0D.h:83
Estimate0D(const BaseT &other, const std::string &path="", const std::string &title="")
Copy constructor using base class.
Definition Estimate0D.h:70
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:266
size_t lengthContent(bool fixed_length=false) const noexcept
Length of serialized content vector for MPI reduce operations.
Definition Estimate0D.h:233
Estimate0D & add(const Estimate0D &toAdd, const std::string &pat_uncorr="^stat|^uncor")
Add two Estimate0Ds.
Definition Estimate0D.h:114
size_t dim() const noexcept
Total dimension of this data object.
Definition Estimate0D.h:162
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:55
void deserializeContent(const std::vector< double > &data)
Content deserialisation for MPI reduce operations.
Definition Estimate0D.h:241
Estimate0D & operator=(const Estimate0D &toCopy) noexcept
Definition Estimate0D.h:96
Estimate0D clone() const noexcept
Make a copy on the stack.
Definition Estimate0D.h:78
std::vector< double > serializeContent(bool fixed_length=false) const noexcept
Content serialisation for MPI reduce operations.
Definition Estimate0D.h:237
Estimate0D(const Estimate0D &other)
Copy constructor (needed for clone functions).
Definition Estimate0D.h:62
Estimate0D & operator-=(const Estimate0D &toSubtract)
Definition Estimate0D.h:142
Estimate0D & operator+=(Estimate0D &&toAdd)
Definition Estimate0D.h:131
Estimate0D & add(Estimate0D &&toAdd, const std::string &pat_uncorr="^stat|^uncor")
Add two (rvalue) Estimate0Ds.
Definition Estimate0D.h:125
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:154
ScatterND< N > & addPoint(const PointND< N > &pt)
Insert a new point.
Definition Scatter.h:379
Error for problems introduced outside YODA, to put it nicely.
Definition Exceptions.h:100
Anonymous namespace to limit visibility.
void transform(BinnedEstimate< AxisT... > &est, const Trf< 1 > &fn)
PointND< 1 > Point1D
User-familiar alias.
Definition Point.h:715
BinnedDbn< DbnN, AxisT... > operator+(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Add two BinnedDbn objects.
Definition BinnedDbn.h:1060
BinnedEstimate< AxisT... > operator/(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Definition BinnedDbn.h:1133
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:1162
BinnedDbn< DbnN, AxisT... > operator-(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Subtract one BinnedDbn object from another.
Definition BinnedDbn.h:1076
BinnedEstimate< AxisT... > divide(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Divide two BinnedDbn objects.
Definition BinnedDbn.h:1092
ScatterND< 1 > Scatter1D
Definition Scatter.h:902