yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Bin1D.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_Bin1D_h
7 #define YODA_Bin1D_h
8 
9 #include "YODA/Utils/MathUtils.h"
10 #include "YODA/Bin.h"
11 #include <utility>
12 
13 namespace YODA {
14 
15 
26  template <class DBN>
27  class Bin1D : public Bin {
28  public:
29 
31 
32 
33  // /// Make a new, empty bin with a pair of edges.
34  // Bin1D(double lowedge, double highedge)
35  // : _edges( std::make_pair(lowedge, highedge) )
36  // {
37  // if (_edges.second < _edges.first) {
38  // throw RangeError("The bin edges are wrongly defined!");
39  // }
40  // }
41 
42 
44  Bin1D(const std::pair<double,double>& edges)
45  : _edges(edges)
46  {
47  if (_edges.second < _edges.first) {
48  throw RangeError("The bin edges are wrongly defined!");
49  }
50  }
51 
52 
56  Bin1D(const std::pair<double,double>& edges, const DBN& dbn)
57  : _edges(edges), _dbn(dbn)
58  {
59  if (_edges.second < _edges.first) {
60  throw RangeError("The bin edges are wrongly defined!");
61  }
62  }
63 
64 
66  Bin1D(const Bin1D<DBN>& b)
67  : _edges(b._edges),
68  _dbn(b._dbn)
69  { }
70 
71 
74  _edges = b._edges;
75  _dbn = b._dbn;
76  return *this;
77  }
78 
80 
81 
82 
84  size_t dim() { return 1; }
85 
86 
87 
89 
90 
92  virtual void reset() {
93  _dbn.reset();
94  }
95 
99  void scaleW(double scalefactor) {
100  _dbn.scaleW(scalefactor);
101  }
102 
106  void scaleX(double factor) {
107  _edges.first *= factor;
108  _edges.second *= factor;
109  _dbn.scaleX(factor);
110  }
111 
113 
114 
115  public:
116 
118 
119 
121  std::pair<double,double> xEdges() const {
122  return _edges;
123  }
124 
126  double xMin() const {
127  return _edges.first;
128  }
129 
131  double xMax() const {
132  return _edges.second;
133  }
134 
136  double xMid() const {
137  return ( _edges.second + _edges.first ) / 2;
138  }
141  double midpoint() const { return xMid(); }
142 
144  double xWidth() const {
145  return _edges.second - _edges.first;
146  }
149  double width() const { return xWidth(); }
150 
151 
153  double xFocus() const {
154  return (!isZero(sumW())) ? xMean() : xMid();
155  }
156 
158 
159 
161 
162 
164  double xMean() const {
165  return _dbn.xMean();
166  }
167 
169  double xVariance() const {
170  return _dbn.xVariance();
171  }
172 
174  double xStdDev() const {
175  return _dbn.xStdDev();
176  }
177 
179  double xStdErr() const {
180  return _dbn.xStdErr();
181  }
182 
184  double xRMS() const {
185  return _dbn.xRMS();
186  }
187 
189 
190 
191  public:
192 
194 
195 
197  DBN& dbn() {
198  return _dbn;
199  }
200 
202  const DBN& dbn() const {
203  return _dbn;
204  }
205 
206 
208  double numEntries() const {
209  return _dbn.numEntries();
210  }
211 
213  double effNumEntries() const {
214  return _dbn.effNumEntries();
215  }
216 
218  double sumW() const {
219  return _dbn.sumW();
220  }
221 
223  double sumW2() const {
224  return _dbn.sumW2();
225  }
226 
228  double sumWX() const {
229  return _dbn.sumWX();
230  }
231 
233  double sumWX2() const {
234  return _dbn.sumWX2();
235  }
236 
238 
239 
240  public:
241 
243 
244 
247  return add(b);
248  }
249 
252  return subtract(b);
253  }
254 
256 
257 
259 
260 
263  if (fuzzyEquals(_edges.second, b._edges.first)) {
264  _edges.second = b._edges.second;
265  } else if (fuzzyEquals(_edges.second, b._edges.first)) {
266  _edges.first = b._edges.first;
267  } else {
268  throw LogicError("Attempted to merge two non-adjacent bins");
269  }
270  // std::cout << "a " << _dbn.sumW() << std::endl;
271  _dbn += b._dbn;
272  // std::cout << "b " << _dbn.sumW() << std::endl;
273  return *this;
274  }
275 
276 
281  Bin1D<DBN>& add(const Bin1D<DBN>& b) {
282  if (!fuzzyEquals(_edges.first, b._edges.first) ||
283  !fuzzyEquals(_edges.second, b._edges.second)) {
284  throw LogicError("Attempted to add two bins with different edges");
285  }
286  _dbn += b._dbn;
287  return *this;
288  }
289 
290 
296  if (!fuzzyEquals(_edges.first, b._edges.first) ||
297  !fuzzyEquals(_edges.second, b._edges.second)) {
298  throw LogicError("Attempted to subtract two bins with different edges");
299  }
300  _dbn -= b._dbn;
301  return *this;
302  }
303 
305 
306 
307  protected:
308 
310  std::pair<double,double> _edges;
311 
312  // Distribution of weighted x (and perhaps y) values
313  DBN _dbn;
314 
316 
317  };
318 
319 
320 
325  template <class DBN>
326  inline Bin1D<DBN> operator + (const Bin1D<DBN>& a, const Bin1D<DBN>& b) {
327  Bin1D<DBN> rtn = a;
328  rtn += b;
329  return rtn;
330  }
331 
332 
337  template <class DBN>
338  inline Bin1D<DBN> operator - (const Bin1D<DBN>& a, const Bin1D<DBN>& b) {
339  Bin1D<DBN> rtn = a;
340  rtn -= b;
341  return rtn;
342  }
343 
344 
346  template <class DBN>
347  inline bool operator<(const Bin1D<DBN>& a, const Bin1D<DBN>& b) {
348  return b.xEdges().first > a.xEdges().first;
349  }
350 
351 
352 }
353 
354 
355 
356 #endif
double sumWX2() const
The sum of x^2 * weight.
Definition: Bin1D.h:233
void scaleW(double scalefactor)
Definition: Bin1D.h:99
Base class for bins in 1D and 2D histograms.
Definition: Bin.h:20
bool isZero(double val, double tolerance=1E-8)
Definition: MathUtils.h:55
double sumWX() const
The sum of x*weight.
Definition: Bin1D.h:228
Bin1D & operator=(const Bin1D< DBN > &b)
Copy assignment.
Definition: Bin1D.h:73
Bin1D(const std::pair< double, double > &edges)
Make a new, empty bin with a pair of edges.
Definition: Bin1D.h:44
Axis1D< BIN1D, DBN > operator-(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Subtract the statistics on two axis.
Definition: Axis1D.h:597
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.h:72
double xWidth() const
Separation of low and high edges, i.e. high-low.
Definition: Bin1D.h:144
A generic 1D bin type.
Definition: Bin1D.h:27
double sumW2() const
The sum of weights squared.
Definition: Bin1D.h:223
size_t dim()
Dimension of the fill space.
Definition: Bin1D.h:84
double xMid() const
Geometric centre of the bin, i.e. high+low/2.0.
Definition: Bin1D.h:136
double width() const
Definition: Bin1D.h:149
double xStdErr() const
The standard error on the bin focus.
Definition: Bin1D.h:179
void scaleX(double factor)
Definition: Bin1D.h:106
double xStdDev() const
The standard deviation (spread) of x-values in the bin.
Definition: Bin1D.h:174
Error for places where it should not have been possible to get to!
Definition: Exceptions.h:55
double midpoint() const
Definition: Bin1D.h:141
Axis1D< BIN1D, DBN > operator+(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Add the statistics on two axes.
Definition: Axis1D.h:589
Bin1D< DBN > & add(const Bin1D< DBN > &b)
Definition: Bin1D.h:281
Bin1D< DBN > & subtract(const Bin1D< DBN > &b)
Definition: Bin1D.h:295
Bin1D< DBN > & operator-=(const Bin1D< DBN > &b)
Subtract one bin from another.
Definition: Bin1D.h:251
Bin1D< DBN > & merge(const Bin1D< DBN > &b)
Merge two adjacent bins.
Definition: Bin1D.h:262
std::pair< double, double > xEdges() const
Get the {low,high} edges as an STL pair.
Definition: Bin1D.h:121
double xMin() const
Lower limit of the bin (inclusive).
Definition: Bin1D.h:126
virtual void reset()
Reset this bin.
Definition: Bin1D.h:92
double sumW() const
The sum of weights.
Definition: Bin1D.h:218
Bin1D(const std::pair< double, double > &edges, const DBN &dbn)
Make a bin with all the components of a fill history.
Definition: Bin1D.h:56
double effNumEntries() const
The effective number of entries.
Definition: Bin1D.h:213
Bin1D(const Bin1D< DBN > &b)
Copy constructor.
Definition: Bin1D.h:66
double xMean() const
Mean value of x-values in the bin.
Definition: Bin1D.h:164
const DBN & dbn() const
Statistical distribution in this bin (const)
Definition: Bin1D.h:202
double xMax() const
Upper limit of the bin (exclusive).
Definition: Bin1D.h:131
double xFocus() const
The mean position in the bin, or the midpoint if that is not available.
Definition: Bin1D.h:153
DBN & dbn()
Statistical distribution in this bin (non-const)
Definition: Bin1D.h:197
double xRMS() const
The x RMS in the bin.
Definition: Bin1D.h:184
Bin1D< DBN > & operator+=(const Bin1D< DBN > &b)
Add two bins.
Definition: Bin1D.h:246
double numEntries() const
The number of entries.
Definition: Bin1D.h:208
double xVariance() const
The variance of x-values in the bin.
Definition: Bin1D.h:169