yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
Counter.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_Counter_h
7#define YODA_Counter_h
8
10#include "YODA/Fillable.h"
11#include "YODA/Dbn.h"
12#include "YODA/Point.h"
13#include "YODA/Estimate0D.h"
14#include "YODA/Scatter.h"
15#include "YODA/Exceptions.h"
16#include <vector>
17#include <string>
18#include <map>
19#include <tuple>
20#include <memory>
21
22namespace YODA {
23
24
26 class Counter : public AnalysisObject, public Fillable {
27 public:
28
29
30 using FillType = std::tuple<>;
31 using Ptr = std::shared_ptr<Counter>;
32 using AnalysisObject::operator =;
33
36
38 Counter(const std::string& path="", const std::string& title="")
39 : AnalysisObject("Counter", path, title) { }
40
41
46 const std::string& path="", const std::string& title="")
47 : AnalysisObject("Counter", path, title), _dbn(dbn) { }
48
49
54 const std::string& path="", const std::string& title="")
55 : AnalysisObject("Counter", path, title), _dbn(std::move(dbn)) { }
56
57
61 Counter(double w,
62 const std::string& path="", const std::string& title="")
63 : AnalysisObject("Counter", path, title) { _dbn.fill(w); }
64
65
68 Counter(const Counter& c, const std::string& path="")
69 : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title()),
70 _dbn(c._dbn) { }
71
73 Counter(Counter&& c, const std::string& path="")
74 : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title()),
75 _dbn(std::move(c._dbn)) { }
76
77
79 Counter& operator = (const Counter& c) noexcept {
80 if (this != &c) {
82 _dbn = c._dbn;
83 }
84 return *this;
85 }
86
88 Counter& operator = (Counter&& c) noexcept {
89 if (this != &c) {
91 _dbn = std::move(c._dbn);
92 }
93 return *this;
94 }
95
97 Counter clone() const {
98 return Counter(*this);
99 }
100
102 Counter* newclone() const {
103 return new Counter(*this);
104 }
105
107
108
111
112 // @brief Render information about this AO
113 void _renderYODA(std::ostream& os, const int width = 13) const noexcept {
114 os << std::setw(width) << std::left << "# sumW" << "\t"
115 << std::setw(width) << std::left << "sumW2" << "\t"
116 << "numEntries\n";
117 os << std::setw(width) << std::left << sumW() << "\t";
118 os << std::setw(width) << std::left << sumW2() << "\t";
119 os << std::setw(width) << std::left << numEntries() << "\n";
120 }
121
122 // @brief Render scatter-like information about this AO
123 void _renderFLAT(std::ostream& os, const int width) const noexcept {
124 Scatter1D tmp = this->mkScatter();
125 tmp._renderYODA(os, width);
126 }
127
129
130
133
135 size_t dim() const noexcept { return 1; }
136
138 size_t fillDim() const noexcept { return 0; }
139
141
142
145
147 virtual int fill(double weight=1.0, double fraction=1.0) {
148 _dbn.fill(weight, fraction);
149 return 0;
150 }
151
152 virtual int fill(FillType&&, double weight=1.0, double fraction=1.0) {
153 return fill(weight, fraction);
154 }
155
159 virtual void reset() {
160 _dbn.reset();
161 }
162
163
165 void scaleW(double scalefactor) {
166 setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
167 _dbn.scaleW(scalefactor);
168 }
169
171
172
175
177 double numEntries(bool=false) const { return _dbn.numEntries(); }
178
180 double effNumEntries(bool=false) const { return _dbn.effNumEntries(); }
181
183 double sumW(bool=false) const { return _dbn.sumW(); }
184
186 double sumW2(bool=false) const { return _dbn.sumW2(); }
187
189 double val(bool=false) const { return sumW(); }
190
193 double err() const { return sqrt(sumW2()); }
194
197 // double err() const { return _dbn.err(); }
198 double relErr() const {
200 return sumW2() != 0 ? err()/sumW() : 0;
201 }
202
204
205
208
210 const Dbn0D& dbn() const {
211 return _dbn;
212 }
213
215 void setDbn(const Dbn0D& dbn) {
216 _dbn = dbn;
217 }
218
220 void setDbn(Dbn0D&& dbn) {
221 _dbn = std::move(dbn);
222 }
223
224 // /// Set the whole object state
225 // void setState(const Dbn0D& dbn, const AnalysisObject::Annotations& anns=AnalysisObject::Annotations()) {
226 // setDbn(dbn);
227 // setAnnotations(anns);
228 // }
229
231
232
235
237 Counter& operator += (const Counter& toAdd) {
238 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
239 _dbn += toAdd._dbn;
240 return *this;
241 }
242 //
244 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
245 _dbn += std::move(toAdd._dbn);
246 return *this;
247 }
248
250 Counter& operator -= (const Counter& toSubtract) {
251 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
252 _dbn -= toSubtract._dbn;
253 return *this;
254 }
255 //
256 Counter& operator -= (Counter&& toSubtract) {
257 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
258 _dbn -= std::move(toSubtract._dbn);
259 return *this;
260 }
261
265 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
266 *this += 1;
267 return *this;
268 }
269
273 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
274 *this -= 1;
275 return *this;
276 }
277
280 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
281 scaleW(s);
282 return *this;
283 }
284
287 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
288 scaleW(1/s);
289 return *this;
290 }
291
293
296
297 inline Estimate0D mkEstimate(const std::string& path = "", const std::string& source = "") const {
298 Estimate0D rtn;
299 for (const std::string& a : annotations()) {
300 if (a != "Type") rtn.setAnnotation(a, annotation(a));
301 }
302 rtn.setAnnotation("Path", path);
303
304 if (numEntries()) {
305 rtn.set(val(), err(), source);
306 }
307 return rtn;
308 }
309
310 inline Scatter1D mkScatter(const std::string& path = "") const {
311 Scatter1D rtn;
312 for (const std::string& a : annotations()) {
313 if (a != "Type") rtn.setAnnotation(a, annotation(a));
314 }
315 rtn.setAnnotation("Path", path);
316
317 rtn.addPoint(Point1D(val(), err()));
318 return rtn;
319 }
320
322 AnalysisObject* mkInert(const std::string& path = "",
323 const std::string& source = "") const noexcept {
324 return mkEstimate(path, source).newclone();
325 }
326
328
330
331
332 size_t lengthContent(bool = false) const noexcept {
333 return _dbn._lengthContent();
334 }
335
336 std::vector<double> serializeContent(bool = false) const noexcept {
337 return _dbn._serializeContent();
338 }
339
340 void deserializeContent(const std::vector<double>& data) {
341 _dbn._deserializeContent(data);
342 }
343
344 // @}
345
346 private:
347
350
352 Dbn0D _dbn;
353
355
356 };
357
358
361
363 inline Counter operator + (Counter first, const Counter& second) {
364 first += second;
365 return first;
366 }
367
369 inline Counter operator + (Counter first, Counter&& second) {
370 first += std::move(second);
371 return first;
372 }
373
375 inline Counter operator - (Counter first, const Counter& second) {
376 first -= second;
377 return first;
378 }
379
381 inline Counter operator - (Counter first, Counter&& second) {
382 first -= std::move(second);
383 return first;
384 }
385
387 Estimate0D divide(const Counter& numer, const Counter& denom);
388
390 inline Estimate0D operator / (const Counter& numer, const Counter& denom) {
391 return divide(numer, denom);
392 }
393
395 inline Estimate0D operator / (Counter&& numer, const Counter& denom) {
396 return divide(std::move(numer), denom);
397 }
398
400 inline Estimate0D operator / (const Counter& numer, Counter&& denom) {
401 return divide(numer, std::move(denom));
402 }
403
405 inline Estimate0D operator / (Counter&& numer, Counter&& denom) {
406 return divide(std::move(numer), std::move(denom));
407 }
408
410
415 Estimate0D efficiency(const Counter& accepted, const Counter& total);
416
418
419
420}
421
422#endif
AnalysisObject is the base class for histograms and scatters.
virtual AnalysisObject & operator=(const AnalysisObject &ao) noexcept
Default copy assignment operator.
void setAnnotation(const std::string &name, const T &value)
Add or set an annotation by name (templated for remaining types)
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.
A weighted counter.
Definition Counter.h:26
std::tuple<> FillType
Definition Counter.h:30
Counter & operator/=(double s)
Inverse-scale by a double (syntactic sugar for scaleW(1/s))
Definition Counter.h:286
double sumW2(bool=false) const
Get the sum of squared weights.
Definition Counter.h:186
Counter(double w, const std::string &path="", const std::string &title="")
Constructor accepting a double (treated as the weight of a single fill).
Definition Counter.h:61
double effNumEntries(bool=false) const
Get the effective number of fills.
Definition Counter.h:180
Scatter1D mkScatter(const std::string &path="") const
Definition Counter.h:310
Counter & operator=(const Counter &c) noexcept
Assignment operator.
Definition Counter.h:79
double err() const
Definition Counter.h:193
const Dbn0D & dbn() const
Get the internal distribution object.
Definition Counter.h:210
Counter(Dbn0D &&dbn, const std::string &path="", const std::string &title="")
Constructor accepting an explicit rvalue Dbn0D.
Definition Counter.h:53
Counter & operator++()
Definition Counter.h:264
double numEntries(bool=false) const
Get the number of fills.
Definition Counter.h:177
size_t dim() const noexcept
Total dimension of this data object.
Definition Counter.h:135
Counter & operator--()
Definition Counter.h:272
double relErr() const
Definition Counter.h:198
std::vector< double > serializeContent(bool=false) const noexcept
Content serialisation for MPI reduce operations.
Definition Counter.h:336
Counter clone() const
Make a copy on the stack.
Definition Counter.h:97
Estimate0D mkEstimate(const std::string &path="", const std::string &source="") const
Definition Counter.h:297
virtual void reset()
Reset the histogram.
Definition Counter.h:159
size_t fillDim() const noexcept
Fill dimension of this data object.
Definition Counter.h:138
Counter & operator*=(double s)
Scale by a double (syntactic sugar for scaleW(s))
Definition Counter.h:279
double val(bool=false) const
Get the value.
Definition Counter.h:189
Counter * newclone() const
Make a copy on the heap, via 'new'.
Definition Counter.h:102
AnalysisObject * mkInert(const std::string &path="", const std::string &source="") const noexcept
Return an inert version of the analysis object (e.g. scatter, estimate)
Definition Counter.h:322
Counter(Counter &&c, const std::string &path="")
Move constructor with optional new path.
Definition Counter.h:73
Counter & operator+=(const Counter &toAdd)
Add another counter to this.
Definition Counter.h:237
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition Counter.h:38
virtual int fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition Counter.h:147
void deserializeContent(const std::vector< double > &data)
Content deserialisation for MPI reduce operations.
Definition Counter.h:340
Counter(const Counter &c, const std::string &path="")
Definition Counter.h:68
virtual int fill(FillType &&, double weight=1.0, double fraction=1.0)
Definition Counter.h:152
Counter(const Dbn0D &dbn, const std::string &path="", const std::string &title="")
Constructor accepting an explicit Dbn0D.
Definition Counter.h:45
double sumW(bool=false) const
Get the sum of weights.
Definition Counter.h:183
Counter & operator-=(const Counter &toSubtract)
Subtract another counter from this.
Definition Counter.h:250
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition Counter.h:165
void setDbn(const Dbn0D &dbn)
Set the internal distribution object: CAREFUL!
Definition Counter.h:215
size_t lengthContent(bool=false) const noexcept
Length of serialized content vector for MPI reduce operations.
Definition Counter.h:332
std::shared_ptr< Counter > Ptr
Definition Counter.h:31
void setDbn(Dbn0D &&dbn)
Set the internal distribution object: CAREFUL!
Definition Counter.h:220
Partial template specialisation for Dbn0D.
Definition Dbn.h:647
An estimate in 0D.
Definition Estimate0D.h:24
Estimate0D * newclone() const noexcept
Make a copy on the heap.
Definition Estimate0D.h:83
void set(const double val, const std::pair< double, double > &err, const std::string source="")
Set both central value and uncertainty component.
Definition Estimate.h:181
A base class for all fillable objects.
Definition Fillable.h:13
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
Anonymous namespace to limit visibility.
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