yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.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-2023 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 = "") const noexcept {
251 Scatter1D rtn;
252 for (const std::string& a : annotations()) {
253 if (a != "Type") rtn.setAnnotation(a, annotation(a));
254 }
255 rtn.setAnnotation("Path", path);
256
257 // Add the PointND
258 const double tot = quadSum().second; // use positive error component
259 rtn.addPoint( Point1D(val(), {tot, tot}) );
260
261 return rtn;
262 }
263
265 AnalysisObject* mkInert(const std::string& path = "",
266 const std::string& source = "") const noexcept {
267 Estimate0D* rtn = newclone();
268 rtn->setPath(path);
269 if (rtn->numErrs() == 1) {
270 try {
271 rtn->renameSource("", source);
272 }
273 catch (YODA::UserError& e) { }
274 }
275 return rtn;
276 }
277
279
280 };
281
284
285 inline void transform(Estimate0D& est, const Trf<1>& fn) {
286 est.transform(fn);
287 }
288
289 template <typename FN>
290 inline void transform(Estimate0D& est, const FN& fn) {
291 transform(est, Trf<1>(fn));
292 }
293
295
296
298
299
301 inline Estimate0D operator + (Estimate0D lhs, const Estimate0D& rhs) {
302 lhs += rhs;
303 return lhs;
304 }
305
308 lhs += std::move(rhs);
309 return lhs;
310 }
311
313 inline Estimate0D operator - (Estimate0D lhs, const Estimate0D& rhs) {
314 lhs -= rhs;
315 return lhs;
316 }
317
320 lhs -= std::move(rhs);
321 return lhs;
322 }
323
325 inline Estimate0D divide(const Estimate0D& numer, const Estimate0D& denom,
326 const std::string& pat_uncorr="^stat|^uncor" ) {
327 Estimate0D rtn = divide(static_cast<const Estimate&>(numer),
328 static_cast<const Estimate&>(denom), pat_uncorr);
329 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
330 if (numer.path() == denom.path()) rtn.setPath(numer.path());
331 return rtn;
332 }
333
335 inline Estimate0D operator / (const Estimate0D& numer, const Estimate0D& denom) {
336 return divide(numer, denom);
337 }
338
340 inline Estimate0D operator / (Estimate0D&& numer, const Estimate0D& denom) {
341 return divide(std::move(numer), denom);
342 }
343
345 inline Estimate0D operator / (const Estimate0D& numer, Estimate0D&& denom) {
346 return divide(numer, std::move(denom));
347 }
348
350 inline Estimate0D operator / (Estimate0D&& numer, Estimate0D&& denom) {
351 return divide(std::move(numer), std::move(denom));
352 }
353
355 inline Estimate0D efficiency(const Estimate0D& accepted, const Estimate0D& total,
356 const std::string& pat_uncorr="^stat|^uncor" ) {
357 Estimate0D rtn = efficiency(static_cast<const Estimate&>(accepted),
358 static_cast<const Estimate&>(total), pat_uncorr);
359 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
360 if (accepted.path() == total.path()) rtn.setPath(total.path());
361 return rtn;
362 }
363
365
366}
367
368#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
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:265
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
Scatter1D mkScatter(const std::string &path="") const noexcept
Definition Estimate0D.h:250
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
Definition Estimate.h:418
std::vector< std::string > sources() const noexcept
Definition Estimate.h:408
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:238
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:252
Estimate & subtract(const Estimate &toSubtract, const std::string &pat_uncorr="^stat|^uncor")
Subtract one Estimate from another.
Definition Estimate.h:125
std::pair< double, double > quadSum() const noexcept
Get the quadrature sum of uncertainty components.
Definition Estimate.h:330
bool hasSource(const std::string &key) const noexcept
Definition Estimate.h:414
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:369
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:704
BinnedDbn< DbnN, AxisT... > operator+(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Add two BinnedDbn objects.
Definition BinnedDbn.h:1058
BinnedEstimate< AxisT... > operator/(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Definition BinnedDbn.h:1131
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:1160
BinnedDbn< DbnN, AxisT... > operator-(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Subtract one BinnedDbn object from another.
Definition BinnedDbn.h:1074
BinnedEstimate< AxisT... > divide(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Divide two BinnedDbn objects.
Definition BinnedDbn.h:1090
ScatterND< 1 > Scatter1D
Definition Scatter.h:881