yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.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-2017 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/Dbn0D.h"
11 #include "YODA/Scatter1D.h"
12 #include "YODA/Exceptions.h"
13 #include <vector>
14 #include <string>
15 #include <map>
16 #include <tuple>
17 #include <memory>
18 
19 namespace YODA {
20 
21 
23  class Counter : public AnalysisObject {
24  public:
25 
26 
27  typedef std::tuple<> FillType;
28  typedef std::shared_ptr<Counter> Ptr;
29 
31 
32 
34  Counter(const std::string& path="", const std::string& title="")
35  : AnalysisObject("Counter", path, title)
36  { }
37 
38 
42  Counter(const Dbn0D& dbn,
43  const std::string& path="", const std::string& title="")
44  : AnalysisObject("Counter", path, title),
45  _dbn(dbn)
46  { }
47 
48 
52  Counter(double w,
53  const std::string& path="", const std::string& title="")
54  : AnalysisObject("Counter", path, title)
55  {
56  _dbn.fill(w);
57  }
58 
59 
62  Counter(const Counter& c, const std::string& path="");
63 
64 
67  setPath(c.path());
68  setTitle(c.title());
69  _dbn = c._dbn;
70  return *this;
71  }
72 
74  Counter clone() const {
75  return Counter(*this);
76  }
77 
79  Counter* newclone() const {
80  return new Counter(*this);
81  }
82 
84 
85 
87  size_t dim() const { return 0; }
88 
89 
91 
92 
94  virtual void fill(double weight=1.0, double fraction=1.0) {
95  _dbn.fill(weight, fraction);
96  }
97 
98  virtual void fill(FillType, double weight=1.0, double fraction=1.0) {
99  fill(weight, fraction);
100  }
101 
105  virtual void reset() {
106  _dbn.reset();
107  }
108 
109 
111  void scaleW(double scalefactor) {
112  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
113  _dbn.scaleW(scalefactor);
114  }
115 
117 
118 
120 
121 
123  double numEntries() const { return _dbn.numEntries(); }
124 
126  double effNumEntries() const { return _dbn.effNumEntries(); }
127 
129  double sumW() const { return _dbn.sumW(); }
130 
132  double sumW2() const { return _dbn.sumW2(); }
133 
135  double val() const { return sumW(); }
136 
139  // double err() const { return _dbn.err(); }
140  double err() const { return sqrt(sumW2()); }
141 
144  // double err() const { return _dbn.err(); }
145  double relErr() const {
147  return sumW2() != 0 ? err()/sumW() : 0;
148  }
149 
151 
152 
154 
155 
157  const Dbn0D& dbn() const {
158  return _dbn;
159  }
160 
162  void setDbn(const Dbn0D& dbn) {
163  _dbn = dbn;
164  }
165 
166  // /// Set the whole object state
167  // void setState(const Dbn0D& dbn, const AnalysisObject::Annotations& anns=AnalysisObject::Annotations()) {
168  // setDbn(dbn);
169  // setAnnotations(anns);
170  // }
171 
173 
174 
176 
177 
179  Counter& operator += (const Counter& toAdd) {
180  _dbn += toAdd._dbn;
181  return *this;
182  }
183 
185  Counter& operator -= (const Counter& toSubtract) {
186  _dbn -= toSubtract._dbn;
187  return *this;
188  }
189 
193  *this += 1;
194  return *this;
195  }
196 
200  *this -= 1;
201  return *this;
202  }
203 
205  Counter& operator *= (double s) {
206  scaleW(s);
207  return *this;
208  }
209 
211  Counter& operator /= (double s) {
212  scaleW(1/s);
213  return *this;
214  }
215 
217 
218 
219  private:
220 
222 
223 
225  Dbn0D _dbn;
226 
228 
229  };
230 
231 
233 
234 
236  inline Counter add(const Counter& first, const Counter& second) {
237  Counter tmp = first;
238  tmp += second;
239  return tmp;
240  }
241 
243  inline Counter operator + (const Counter& first, const Counter& second) {
244  return add(first, second);
245  }
246 
247 
249  inline Counter subtract(const Counter& first, const Counter& second) {
250  Counter tmp = first;
251  tmp -= second;
252  return tmp;
253  }
254 
256  inline Counter operator - (const Counter& first, const Counter& second) {
257  return subtract(first, second);
258  }
259 
260 
263  Scatter1D divide(const Counter& numer, const Counter& denom);
264 
267  inline Scatter1D operator / (const Counter& numer, const Counter& denom) {
268  return divide(numer, denom);
269  }
270 
272 
273 
274 
281  Scatter1D efficiency(const Counter& accepted, const Counter& total);
282 
284 
285 
286 }
287 
288 #endif
Counter(const Dbn0D &dbn, const std::string &path="", const std::string &title="")
Constructor accepting an explicit Dbn0D.
Definition: Counter.h:42
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:111
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:211
const std::string path() const
Get the AO path.
void setDbn(const Dbn0D &dbn)
Set the internal distribution object: CAREFUL!
Definition: Counter.h:162
double sumW2() const
Get the sum of squared weights.
Definition: Counter.h:132
void setPath(const std::string &path)
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 effNumEntries() const
Get the effective number of fills.
Definition: Counter.h:126
A weighted counter.
Definition: Counter.h:23
Counter & operator--()
Definition: Counter.h:199
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:236
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Counter.h:34
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:52
A 0D distribution.
Definition: Dbn0D.h:28
Counter clone() const
Make a copy on the stack.
Definition: Counter.h:74
Counter & operator+=(const Counter &toAdd)
Add another counter to this.
Definition: Counter.h:179
size_t dim() const
Fill dimension of this data object.
Definition: Counter.h:87
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:249
virtual void fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition: Counter.h:94
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:205
double err() const
Definition: Counter.h:140
double sumW() const
Get the sum of weights.
Definition: Counter.h:129
double val() const
Get the value.
Definition: Counter.h:135
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:79
double relErr() const
Definition: Counter.h:145
void setTitle(const std::string &title)
Set the AO title.
Counter & operator=(const Counter &c)
Assignment operator.
Definition: Counter.h:66
Counter & operator++()
Definition: Counter.h:192
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:23
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:267
std::tuple FillType
Definition: Counter.h:27
const Dbn0D & dbn() const
Get the internal distribution object.
Definition: Counter.h:157
std::shared_ptr< Counter > Ptr
Definition: Counter.h:28
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:105
Counter & operator-=(const Counter &toSubtract)
Subtract another counter from this.
Definition: Counter.h:185
virtual void fill(FillType, double weight=1.0, double fraction=1.0)
Definition: Counter.h:98
Scatter1D efficiency(const Counter &accepted, const Counter &total)
Calculate an efficiency ratio of two counters.
Definition: Counter.cc:40
double numEntries() const
Get the number of fills.
Definition: Counter.h:123