yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
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-2024 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
225 void set(const double numEntries, const double sumW, const double sumW2) {
226 _dbn.set(numEntries, sumW, sumW2);
227 }
228
229 // /// Set the whole object state
230 // void setState(const Dbn0D& dbn, const AnalysisObject::Annotations& anns=AnalysisObject::Annotations()) {
231 // setDbn(dbn);
232 // setAnnotations(anns);
233 // }
234
236
237
240
242 Counter& operator += (const Counter& toAdd) {
243 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
244 _dbn += toAdd._dbn;
245 return *this;
246 }
247 //
249 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
250 _dbn += std::move(toAdd._dbn);
251 return *this;
252 }
253
255 Counter& operator -= (const Counter& toSubtract) {
256 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
257 _dbn -= toSubtract._dbn;
258 return *this;
259 }
260 //
261 Counter& operator -= (Counter&& toSubtract) {
262 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
263 _dbn -= std::move(toSubtract._dbn);
264 return *this;
265 }
266
270 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
271 *this += 1;
272 return *this;
273 }
274
278 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
279 *this -= 1;
280 return *this;
281 }
282
285 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
286 scaleW(s);
287 return *this;
288 }
289
292 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
293 scaleW(1/s);
294 return *this;
295 }
296
298
301
302 inline Estimate0D mkEstimate(const std::string& path = "", const std::string& source = "") const {
303 Estimate0D rtn;
304 for (const std::string& a : annotations()) {
305 if (a != "Type") rtn.setAnnotation(a, annotation(a));
306 }
307 rtn.setAnnotation("Path", path);
308
309 rtn.setVal(val());
310 if (numEntries()) { // only set uncertainty for filled Dbns
311 rtn.setErr(err(), source);
312 }
313 return rtn;
314 }
315
316 inline Scatter1D mkScatter(const std::string& path = "") const {
317 Scatter1D rtn;
318 for (const std::string& a : annotations()) {
319 if (a != "Type") rtn.setAnnotation(a, annotation(a));
320 }
321 rtn.setAnnotation("Path", path);
322
323 rtn.addPoint(Point1D(val(), err()));
324 return rtn;
325 }
326
328 AnalysisObject* mkInert(const std::string& path = "",
329 const std::string& source = "") const noexcept {
330 return mkEstimate(path, source).newclone();
331 }
332
334
336
337
338 size_t lengthContent(bool = false) const noexcept {
339 return _dbn._lengthContent();
340 }
341
342 std::vector<double> serializeContent(bool = false) const noexcept {
343 return _dbn._serializeContent();
344 }
345
346 void deserializeContent(const std::vector<double>& data) {
347 _dbn._deserializeContent(data);
348 }
349
350 // @}
351
352 private:
353
356
358 Dbn0D _dbn;
359
361
362 };
363
364
367
369 inline Counter operator + (Counter first, const Counter& second) {
370 first += second;
371 return first;
372 }
373
375 inline Counter operator + (Counter first, Counter&& second) {
376 first += std::move(second);
377 return first;
378 }
379
381 inline Counter operator - (Counter first, const Counter& second) {
382 first -= second;
383 return first;
384 }
385
387 inline Counter operator - (Counter first, Counter&& second) {
388 first -= std::move(second);
389 return first;
390 }
391
393 Estimate0D divide(const Counter& numer, const Counter& denom);
394
396 inline Estimate0D operator / (const Counter& numer, const Counter& denom) {
397 return divide(numer, denom);
398 }
399
401 inline Estimate0D operator / (Counter&& numer, const Counter& denom) {
402 return divide(std::move(numer), denom);
403 }
404
406 inline Estimate0D operator / (const Counter& numer, Counter&& denom) {
407 return divide(numer, std::move(denom));
408 }
409
411 inline Estimate0D operator / (Counter&& numer, Counter&& denom) {
412 return divide(std::move(numer), std::move(denom));
413 }
414
416
421 Estimate0D efficiency(const Counter& accepted, const Counter& total);
422
424
425
426}
427
428#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:291
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:316
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:269
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:277
double relErr() const
Definition Counter.h:198
std::vector< double > serializeContent(bool=false) const noexcept
Content serialisation for MPI reduce operations.
Definition Counter.h:342
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:302
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:284
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:328
void set(const double numEntries, const double sumW, const double sumW2)
Set the internal distribution object: CAREFUL!
Definition Counter.h:225
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:242
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:346
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:255
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:338
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 setErr(const std::pair< double, double > &err, const std::string &source="")
Set a signed uncertainty component.
Definition Estimate.h:165
void setVal(const double val) noexcept
Alias for setValue.
Definition Estimate.h:162
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:379
Anonymous namespace to limit visibility.
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