yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.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-2021 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_Counter_h
7 #define YODA_Counter_h
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Fillable.h"
11 #include "YODA/Dbn0D.h"
12 #include "YODA/Scatter1D.h"
13 #include "YODA/Exceptions.h"
14 #include <vector>
15 #include <string>
16 #include <map>
17 #include <tuple>
18 #include <memory>
19 
20 namespace YODA {
21 
22 
24  class Counter : public AnalysisObject, public Fillable {
25  public:
26 
27 
28  typedef std::tuple<> FillType;
29  typedef std::shared_ptr<Counter> Ptr;
30 
33 
35  Counter(const std::string& path="", const std::string& title="")
36  : AnalysisObject("Counter", path, title)
37  { }
38 
39 
43  Counter(const Dbn0D& dbn,
44  const std::string& path="", const std::string& title="")
45  : AnalysisObject("Counter", path, title),
46  _dbn(dbn)
47  { }
48 
49 
53  Counter(double w,
54  const std::string& path="", const std::string& title="")
55  : AnalysisObject("Counter", path, title)
56  {
57  _dbn.fill(w);
58  }
59 
60 
63  Counter(const Counter& c, const std::string& path="");
64 
65 
68  setPath(c.path());
69  setTitle(c.title());
70  _dbn = c._dbn;
71  return *this;
72  }
73 
75  Counter clone() const {
76  return Counter(*this);
77  }
78 
80  Counter* newclone() const {
81  return new Counter(*this);
82  }
83 
85 
86 
89 
93  size_t dim() const { return 0; }
94 
96  size_t fillDim() const { return 0; }
97 
99 
100 
103 
105  virtual void fill(double weight=1.0, double fraction=1.0) {
106  _dbn.fill(weight, fraction);
107  }
108 
109  virtual void fill(FillType, double weight=1.0, double fraction=1.0) {
110  fill(weight, fraction);
111  }
112 
116  virtual void reset() {
117  _dbn.reset();
118  }
119 
120 
122  void scaleW(double scalefactor) {
123  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
124  _dbn.scaleW(scalefactor);
125  }
126 
128 
129 
132 
134  double numEntries(bool=false) const { return _dbn.numEntries(); }
135 
137  double effNumEntries(bool=false) const { return _dbn.effNumEntries(); }
138 
140  double sumW(bool=false) const { return _dbn.sumW(); }
141 
143  double sumW2(bool=false) const { return _dbn.sumW2(); }
144 
146  double val(bool=false) const { return sumW(); }
147 
150  // double err() const { return _dbn.err(); }
151  double err() const { return sqrt(sumW2()); }
152 
155  // double err() const { return _dbn.err(); }
156  double relErr() const {
158  return sumW2() != 0 ? err()/sumW() : 0;
159  }
160 
162 
163 
166 
168  const Dbn0D& dbn() const {
169  return _dbn;
170  }
171 
173  void setDbn(const Dbn0D& dbn) {
174  _dbn = dbn;
175  }
176 
177  // /// Set the whole object state
178  // void setState(const Dbn0D& dbn, const AnalysisObject::Annotations& anns=AnalysisObject::Annotations()) {
179  // setDbn(dbn);
180  // setAnnotations(anns);
181  // }
182 
184 
185 
188 
190  Counter& operator += (const Counter& toAdd) {
191  _dbn += toAdd._dbn;
192  return *this;
193  }
194 
196  Counter& operator -= (const Counter& toSubtract) {
197  _dbn -= toSubtract._dbn;
198  return *this;
199  }
200 
204  *this += 1;
205  return *this;
206  }
207 
211  *this -= 1;
212  return *this;
213  }
214 
216  Counter& operator *= (double s) {
217  scaleW(s);
218  return *this;
219  }
220 
222  Counter& operator /= (double s) {
223  scaleW(1/s);
224  return *this;
225  }
226 
228 
229 
230  private:
231 
234 
236  Dbn0D _dbn;
237 
239 
240  };
241 
242 
245 
247  inline Counter add(const Counter& first, const Counter& second) {
248  Counter tmp = first;
249  tmp += second;
250  return tmp;
251  }
252 
254  inline Counter operator + (const Counter& first, const Counter& second) {
255  return add(first, second);
256  }
257 
258 
260  inline Counter subtract(const Counter& first, const Counter& second) {
261  Counter tmp = first;
262  tmp -= second;
263  return tmp;
264  }
265 
267  inline Counter operator - (const Counter& first, const Counter& second) {
268  return subtract(first, second);
269  }
270 
271 
274  Scatter1D divide(const Counter& numer, const Counter& denom);
275 
278  inline Scatter1D operator / (const Counter& numer, const Counter& denom) {
279  return divide(numer, denom);
280  }
281 
283 
284 
285 
292  Scatter1D efficiency(const Counter& accepted, const Counter& total);
293 
295 
296 
297 }
298 
299 #endif
double effNumEntries(bool=false) const
Get the effective number of fills.
Definition: Counter.h:137
Counter(const Dbn0D &dbn, const std::string &path="", const std::string &title="")
Constructor accepting an explicit Dbn0D.
Definition: Counter.h:43
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:122
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
Counter & operator/=(double s)
Inverse-scale by a double (syntactic sugar for scaleW(1/s))
Definition: Counter.h:222
const std::string path() const
Get the AO path.
void setDbn(const Dbn0D &dbn)
Set the internal distribution object: CAREFUL!
Definition: Counter.h:173
void setPath(const std::string &path)
A base class for all fillable objects.
Definition: Fillable.h:16
Axis1D< BIN1D, DBN > operator-(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Subtract the statistics on two axis.
Definition: Axis1D.h:597
double sumW2(bool=false) const
Get the sum of squared weights.
Definition: Counter.h:143
A weighted counter.
Definition: Counter.h:24
size_t fillDim() const
Fill dimension of this data object.
Definition: Counter.h:96
Counter & operator--()
Definition: Counter.h:210
double numEntries(bool=false) const
Get the number of fills.
Definition: Counter.h:134
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:247
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Counter.h:35
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:53
A 0D distribution.
Definition: Dbn0D.h:28
double val(bool=false) const
Get the value.
Definition: Counter.h:146
Counter clone() const
Make a copy on the stack.
Definition: Counter.h:75
Counter & operator+=(const Counter &toAdd)
Add another counter to this.
Definition: Counter.h:190
size_t dim() const
Fill dimension of this data object.
Definition: Counter.h:93
Axis1D< BIN1D, DBN > operator+(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Add the statistics on two axes.
Definition: Axis1D.h:589
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:260
virtual void fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition: Counter.h:105
const std::string title() const
Get the AO title.
Counter & operator*=(double s)
Scale by a double (syntactic sugar for scaleW(s))
Definition: Counter.h:216
double err() const
Definition: Counter.h:151
AnalysisObject is the base class for histograms and scatters.
Counter * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Counter.h:80
double relErr() const
Definition: Counter.h:156
void setTitle(const std::string &title)
Set the AO title.
Counter & operator=(const Counter &c)
Assignment operator.
Definition: Counter.h:67
Counter & operator++()
Definition: Counter.h:203
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:278
std::tuple FillType
Definition: Counter.h:28
const Dbn0D & dbn() const
Get the internal distribution object.
Definition: Counter.h:168
std::shared_ptr< Counter > Ptr
Definition: Counter.h:29
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
virtual void reset()
Reset the histogram.
Definition: Counter.h:116
Counter & operator-=(const Counter &toSubtract)
Subtract another counter from this.
Definition: Counter.h:196
virtual void fill(FillType, double weight=1.0, double fraction=1.0)
Definition: Counter.h:109
Scatter1D efficiency(const Counter &accepted, const Counter &total)
Calculate an efficiency ratio of two counters.
Definition: Counter.cc:40
double sumW(bool=false) const
Get the sum of weights.
Definition: Counter.h:140