yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Histo1D.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_Histo1D_h
7 #define YODA_Histo1D_h
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/HistoBin1D.h"
11 #include "YODA/Dbn1D.h"
12 #include "YODA/Scatter2D.h"
13 #include "YODA/Axis1D.h"
14 #include "YODA/Exceptions.h"
15 #include <vector>
16 #include <string>
17 #include <map>
18 
19 namespace YODA {
20 
21 
24 
26  class Histo1D : public AnalysisObject {
27  public:
28 
30  typedef Histo1DAxis Axis;
31  typedef Axis::Bins Bins;
32  typedef HistoBin1D Bin;
33 
34  typedef double FillType;
35  typedef FillType BinType;
36  typedef std::shared_ptr<Histo1D> Ptr;
37 
39 
40 
42  Histo1D(const std::string& path="", const std::string& title="")
43  : AnalysisObject("Histo1D", path, title),
44  _axis()
45  { }
46 
47 
49  Histo1D(size_t nbins, double lower, double upper,
50  const std::string& path="", const std::string& title="")
51  : AnalysisObject("Histo1D", path, title),
52  _axis(nbins, lower, upper)
53  { }
54 
55 
60  Histo1D(const std::vector<double>& binedges,
61  const std::string& path="", const std::string& title="")
62  : AnalysisObject("Histo1D", path, title),
63  _axis(binedges)
64  { }
65 
66 
68  Histo1D(const std::vector<Bin>& bins,
69  const std::string& path="", const std::string& title="")
70  : AnalysisObject("Histo1D", path, title),
71  _axis(bins)
72  { }
73 
74 
77  Histo1D(const Histo1D& h, const std::string& path="");
78 
79 
82  Histo1D(const Scatter2D& s, const std::string& path="");
83 
84 
87  Histo1D(const Profile1D& p, const std::string& path="");
88 
89 
93  Histo1D(const std::vector<HistoBin1D>& bins,
94  const Dbn1D& dbn_tot, const Dbn1D& dbn_uflow, const Dbn1D& dbn_oflow,
95  const std::string& path="", const std::string& title="")
96  : AnalysisObject("Histo1D", path, title),
97  _axis(bins, dbn_tot, dbn_uflow, dbn_oflow)
98  { }
99 
100 
103  AnalysisObject::operator = (h1); //< AO treatment of paths etc.
104  _axis = h1._axis;
105  return *this;
106  }
107 
109  Histo1D clone() const {
110  return Histo1D(*this);
111  }
112 
114  Histo1D* newclone() const {
115  return new Histo1D(*this);
116  }
117 
119 
120 
122  size_t dim() const { return 1; }
123 
124 
126 
127 
131  virtual void reset() {
132  _axis.reset();
133  }
134 
136  virtual void fill(double x, double weight=1.0, double fraction=1.0);
137 
139  virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0);
140 
141 
143  void scaleW(double scalefactor) {
144  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
145  _axis.scaleW(scalefactor);
146  }
147 
148 
154  void normalize(double normto=1.0, bool includeoverflows=true) {
155  const double oldintegral = integral(includeoverflows);
156  if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
158  scaleW(normto / oldintegral);
159  }
160 
161 
163  void mergeBins(size_t from, size_t to) {
164  _axis.mergeBins(from, to);
165  }
166 
167 
169  void rebinBy(unsigned int n, size_t begin=0, size_t end=UINT_MAX) {
170  _axis.rebinBy(n, begin, end);
171  }
173  void rebin(unsigned int n, size_t begin=0, size_t end=UINT_MAX) {
174  rebinBy(n, begin, end);
175  }
176 
178  void rebinTo(const std::vector<double>& newedges) {
179  _axis.rebinTo(newedges);
180  }
182  void rebin(const std::vector<double>& newedges) {
183  rebinTo(newedges);
184  }
185 
187 
188 
189  public:
190 
192 
193 
195  size_t numBins() const { return bins().size(); }
196 
198  double xMin() const { return _axis.xMin(); }
199 
201  double xMax() const { return _axis.xMax(); }
202 
207  const std::vector<double> xEdges() const { return _axis.xEdges(); }
208 
209 
211  std::vector<YODA::HistoBin1D>& bins() { return _axis.bins(); }
213  const std::vector<YODA::HistoBin1D>& bins() const { return _axis.bins(); }
214 
215 
217  HistoBin1D& bin(size_t index) { return _axis.bins()[index]; }
219  const HistoBin1D& bin(size_t index) const { return _axis.bins()[index]; }
220 
221 
224  int binIndexAt(double x) {
225  return _axis.binIndexAt(x);
226  }
227 
229  const HistoBin1D& binAt(double x) const { return _axis.binAt(x); }
230 
231 
233  Dbn1D& totalDbn() { return _axis.totalDbn(); }
235  const Dbn1D& totalDbn() const { return _axis.totalDbn(); }
237  void setTotalDbn(const Dbn1D& dbn) { _axis.setTotalDbn(dbn); }
238 
239 
241  Dbn1D& underflow() { return _axis.underflow(); }
243  const Dbn1D& underflow() const { return _axis.underflow(); }
245  void setUnderflow(const Dbn1D& dbn) { _axis.setUnderflow(dbn); }
246 
247 
249  Dbn1D& overflow() { return _axis.overflow(); }
251  const Dbn1D& overflow() const { return _axis.overflow(); }
253  void setOverflow(const Dbn1D& dbn) { _axis.setOverflow(dbn); }
254 
255 
257  void addBin(double from, double to) { _axis.addBin(from, to); }
258 
260  void addBins(std::vector<double> edges) { _axis.addBins(edges); }
261 
262  // /// Add new bins specifying a beginning and end of each of them
263  // void addBins(std::vector<std::pair<double,double> > edges) {
264  // _axis.addBins(edges);
265  // }
266 
268  void addBin(const HistoBin1D& b) { _axis.addBin(b); }
269 
273  void addBins(const Bins& bins) {
274  _axis.addBins(bins);
275  }
276 
278  void eraseBin(size_t index) { _axis.eraseBin(index); }
279 
281 
282 
284 
285 
287  double integral(bool includeoverflows=true) const { return sumW(includeoverflows); }
288 
296  double integralRange(size_t binindex1, size_t binindex2) const {
297  assert(binindex2 >= binindex1);
298  if (binindex1 >= numBins()) throw RangeError("binindex1 is out of range");
299  if (binindex2 >= numBins()) throw RangeError("binindex2 is out of range");
300  double rtn = 0;
301  for (size_t i = binindex1; i < binindex2; ++i) {
302  rtn += bin(i).sumW();
303  }
304  return rtn;
305  }
306 
313  double integralTo(size_t binindex, bool includeunderflow=true) const {
314  double rtn = includeunderflow ? underflow().sumW() : 0;
315  rtn += integralRange(0, binindex);
316  return rtn;
317  }
318 
320  double numEntries(bool includeoverflows=true) const;
321 
323  double effNumEntries(bool includeoverflows=true) const;
324 
326  double sumW(bool includeoverflows=true) const;
327 
329  double sumW2(bool includeoverflows=true) const;
330 
332  double xMean(bool includeoverflows=true) const;
333 
335  double xVariance(bool includeoverflows=true) const;
336 
338  double xStdDev(bool includeoverflows=true) const {
339  if (includeoverflows) return _axis.totalDbn().xStdDev();
340  return std::sqrt(xVariance(includeoverflows));
341  }
342 
344  double xStdErr(bool includeoverflows=true) const;
345 
347  double xRMS(bool includeoverflows=true) const;
348 
350 
351 
353 
354 
358  Histo1D& operator += (const Histo1D& toAdd) {
359  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
360  _axis += toAdd._axis;
361  return *this;
362 
363  // if (!hasAnnotation("ScaledBy") && !toAdd.hasAnnotation("ScaledBy")) {
364  // _axis += toAdd._axis;
365  // } else {
366  // // Undo scaling of both histograms
367  // double scaledBy = annotation<double>("ScaledBy", 1.0);
368  // _axis.scaleW(1.0/scaledBy);
369 
370  // double toAddScaledBy = toAdd.annotation<double>("ScaledBy", 1.0);
371  // Axis1D<HistoBin1D, Dbn1D> toAddAxis = toAdd._axis;
372  // toAddAxis.scaleW(1.0/toAddScaledBy);
373 
374  // _axis += toAddAxis;
375 
376  // // Re-apply combined scaling
377  // double newScaledBy = scaledBy*toAddScaledBy/(scaledBy+toAddScaledBy);
378  // _axis.scaleW(newScaledBy);
379  // setAnnotation("ScaledBy", newScaledBy);
380  // }
382  // return *this;
383  }
384 
388  Histo1D& operator -= (const Histo1D& toSubtract) {
389  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
390  _axis -= toSubtract._axis;
391  return *this;
392  }
393 
395 
396 
397  protected:
398 
400  HistoBin1D& _binAt(double x) { return _axis.binAt(x); }
401 
402 
403  private:
404 
406 
407 
410 
412 
413  };
414 
415 
417  typedef Histo1D H1D;
418 
419 
421 
422 
424  inline Histo1D add(const Histo1D& first, const Histo1D& second) {
425  Histo1D tmp = first;
426  if (first.path() != second.path()) tmp.setPath("");
427  tmp += second;
428  return tmp;
429  }
430 
431 
433  inline Histo1D operator + (const Histo1D& first, const Histo1D& second) {
434  return add(first, second);
435  }
436 
437 
439  inline Histo1D subtract(const Histo1D& first, const Histo1D& second) {
440  Histo1D tmp = first;
441  if (first.path() != second.path()) tmp.setPath("");
442  tmp -= second;
443  return tmp;
444  }
445 
446 
448  inline Histo1D operator - (const Histo1D& first, const Histo1D& second) {
449  return subtract(first, second);
450  }
451 
452 
454 
455 
461  Scatter2D divide(const Histo1D& numer, const Histo1D& denom);
462 
463 
467  inline Scatter2D operator / (const Histo1D& numer, const Histo1D& denom) {
468  return divide(numer, denom);
469  }
470 
471 
473  Scatter2D add(const Histo1D& histo, const Scatter2D& scatt);
474 
475  inline Scatter2D add(const Scatter2D& scatt, const Histo1D& histo) {
476  return add(histo, scatt);
477  }
478 
480  Scatter2D subtract(const Histo1D& histo, const Scatter2D& scatt);
481 
483  Scatter2D subtract(const Scatter2D& scatt, const Histo1D& histo);
484 
486  Scatter2D multiply(const Histo1D& histo, const Scatter2D& scatt);
487 
489  inline Scatter2D multiply(const Scatter2D& scatt, const Histo1D& histo) {
490  return multiply(histo, scatt);
491  }
492 
494  Scatter2D divide(const Histo1D& numer, const Scatter2D& denom);
495 
497  Scatter2D divide(const Scatter2D& numer, const Histo1D& denom);
498 
500 
501 
502 
509  Scatter2D efficiency(const Histo1D& accepted, const Histo1D& total);
510 
511 
515  inline Scatter2D asymm(const Histo1D& a, const Histo1D& b) {
516  return (a-b) / (a+b);
517  }
518 
519 
530  Scatter2D toIntegralHisto(const Histo1D& h, bool includeunderflow=true);
531 
532 
546  Scatter2D toIntegralEfficiencyHisto(const Histo1D& h, bool includeunderflow=true, bool includeoverflow=true);
547 
549 
550 
551 }
552 
553 #endif
double integralRange(size_t binindex1, size_t binindex2) const
Get the integrated area of the histogram between bins binindex1 and binindex2.
Definition: Histo1D.h:296
Scatter2D toIntegralHisto(const Histo1D &h, bool includeunderflow=true)
Convert a Histo1D to a Scatter2D representing the integral of the histogram.
Definition: Histo1D.cc:539
void rmAnnotation(const std::string &name)
Delete an annotation by name.
double xStdErr(bool includeoverflows=true) const
Get the standard error in x.
Definition: Histo1D.cc:98
const Dbn1D & underflow() const
Access underflow (const version)
Definition: Histo1D.h:243
FillType BinType
Definition: Histo1D.h:35
Histo1D & operator+=(const Histo1D &toAdd)
Add another histogram to this one.
Definition: Histo1D.h:358
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
double xStdDev(bool includeoverflows=true) const
Get the standard deviation in x.
Definition: Histo1D.h:338
double sumW(bool includeoverflows=true) const
Get sum of weights in histo.
Definition: Histo1D.cc:64
const std::string path() const
Get the AO path.
double sumW2(bool includeoverflows=true) const
Get sum of squared weights in histo.
Definition: Histo1D.cc:72
void mergeBins(size_t from, size_t to)
Merge together the bin range with indices from from to to, inclusive.
Definition: Histo1D.h:163
A very generic data type which is just a collection of 2D data points with errors.
Definition: Scatter2D.h:24
double integral(bool includeoverflows=true) const
Get the total area (sumW) of the histogram.
Definition: Histo1D.h:287
void setTotalDbn(const Dbn1D &dbn)
Set summary distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:237
Axis::Bins Bins
Definition: Histo1D.h:31
virtual void fill(double x, double weight=1.0, double fraction=1.0)
Fill histo by value and weight, optionally as a fractional fill.
Definition: Histo1D.cc:16
const Dbn1D & overflow() const
Access overflow (const version)
Definition: Histo1D.h:251
void setPath(const std::string &path)
double sumW() const
The sum of weights.
Definition: Dbn1D.h:152
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
Dbn1D & underflow()
Access underflow (non-const version)
Definition: Histo1D.h:241
double xMin() const
Low edge of this histo&#39;s axis.
Definition: Histo1D.h:198
Dbn1D & overflow()
Access overflow (non-const version)
Definition: Histo1D.h:249
std::vector< YODA::HistoBin1D > & bins()
Access the bin vector.
Definition: Histo1D.h:211
A one-dimensional histogram.
Definition: Histo1D.h:26
int binIndexAt(double x)
Definition: Histo1D.h:224
const std::vector< YODA::HistoBin1D > & bins() const
Access the bin vector (const version)
Definition: Histo1D.h:213
Histo1D(const std::vector< Bin > &bins, const std::string &path="", const std::string &title="")
Constructor accepting an explicit collection of bins.
Definition: Histo1D.h:68
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:236
A Bin1D specialised for handling histogram-type information.
Definition: HistoBin1D.h:21
Histo1D(size_t nbins, double lower, double upper, const std::string &path="", const std::string &title="")
Constructor giving range and number of bins.
Definition: Histo1D.h:49
void rebin(const std::vector< double > &newedges)
Overloaded alias for rebinTo.
Definition: Histo1D.h:182
Axis1D< HistoBin1D, Dbn1D > Histo1DAxis
Convenience typedef.
Definition: Histo1D.h:23
double xMean(bool includeoverflows=true) const
Get the mean in x.
Definition: Histo1D.cc:82
void eraseBin(size_t index)
Remove a bin.
Definition: Histo1D.h:278
Histo1D & operator=(const Histo1D &h1)
Assignment operator.
Definition: Histo1D.h:102
Scatter2D toIntegralEfficiencyHisto(const Histo1D &h, bool includeunderflow=true, bool includeoverflow=true)
Convert a Histo1D to a Scatter2D where each bin is a fraction of the total.
Definition: Histo1D.cc:553
std::shared_ptr< Histo1D > Ptr
Definition: Histo1D.h:36
Histo1D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Histo1D.h:42
void rebinBy(unsigned int n, size_t begin=0, size_t end=UINT_MAX)
Merge every group of n bins, starting from the LHS.
Definition: Histo1D.h:169
double integralTo(size_t binindex, bool includeunderflow=true) const
Get the integrated area of the histogram up to bin binindex.
Definition: Histo1D.h:313
Histo1D H1D
Convenience typedef.
Definition: Histo1D.h:417
void setUnderflow(const Dbn1D &dbn)
Set underflow distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:245
virtual void reset()
Reset the histogram.
Definition: Histo1D.h:131
void rebin(unsigned int n, size_t begin=0, size_t end=UINT_MAX)
Overloaded alias for rebinBy.
Definition: Histo1D.h:173
Histo1DAxis Axis
Convenience typedefs.
Definition: Histo1D.h:30
void setOverflow(const Dbn1D &dbn)
Set overflow distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:253
double numEntries(bool includeoverflows=true) const
Get the number of fills.
Definition: Histo1D.cc:48
Errors relating to event/bin weights.
Definition: Exceptions.h:65
void addBin(const HistoBin1D &b)
Add a new bin, perhaps already populated: CAREFUL!
Definition: Histo1D.h:268
Axis1D< BIN1D, DBN > operator+(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Add the statistics on two axes.
Definition: Axis1D.h:589
double xVariance(bool includeoverflows=true) const
Get the variance in x.
Definition: Histo1D.cc:90
void addBins(std::vector< double > edges)
Add new bins by specifying a vector of edges.
Definition: Histo1D.h:260
void normalize(double normto=1.0, bool includeoverflows=true)
Definition: Histo1D.h:154
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Histo1D.h:143
HistoBin1D Bin
Definition: Histo1D.h:32
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:249
void addBins(const Bins &bins)
Bins addition operator.
Definition: Histo1D.h:273
const std::string title() const
Get the AO title.
size_t dim() const
Fill dimension of this data object.
Definition: Histo1D.h:122
std::vector< Bin > Bins
A vector containing 1D bins. Not used for searching.
Definition: Axis1D.h:30
HistoBin1D & bin(size_t index)
Access a bin by index (non-const version)
Definition: Histo1D.h:217
AnalysisObject is the base class for histograms and scatters.
size_t numBins() const
Number of bins on this axis (not counting under/overflow)
Definition: Histo1D.h:195
Scatter2D multiply(const Histo1D &histo, const Scatter2D &scatt)
Multiply histogram with scatter.
Definition: Histo1D.cc:344
Histo1D clone() const
Make a copy on the stack.
Definition: Histo1D.h:109
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
Dbn1D & totalDbn()
Access summary distribution, including gaps and overflows (non-const version)
Definition: Histo1D.h:233
void rebinTo(const std::vector< double > &newedges)
Rebin to the given list of bin edges.
Definition: Histo1D.h:178
double sumW() const
The sum of weights.
Definition: Bin1D.h:218
Scatter2D asymm(const Histo1D &a, const Histo1D &b)
Calculate the asymmetry (a-b)/(a+b) of two histograms.
Definition: Histo1D.h:515
Histo1D(const std::vector< HistoBin1D > &bins, const Dbn1D &dbn_tot, const Dbn1D &dbn_uflow, const Dbn1D &dbn_oflow, const std::string &path="", const std::string &title="")
State-setting constructor.
Definition: Histo1D.h:93
const HistoBin1D & binAt(double x) const
Access a bin by coordinate (const version)
Definition: Histo1D.h:229
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:267
const Dbn1D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Histo1D.h:235
Histo1D & operator-=(const Histo1D &toSubtract)
Subtract another histogram from this one.
Definition: Histo1D.h:388
Histo1D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Histo1D.h:114
A one-dimensional profile histogram.
Definition: Profile1D.h:33
void addBin(double from, double to)
Add a new bin specifying its lower and upper bound.
Definition: Histo1D.h:257
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
const std::vector< double > xEdges() const
Definition: Histo1D.h:207
double xMax() const
High edge of this histo&#39;s axis.
Definition: Histo1D.h:201
virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0)
Fill histo bin i with the given weight, optionally as a fractional fill.
Definition: Histo1D.cc:40
const HistoBin1D & bin(size_t index) const
Access a bin by index (const version)
Definition: Histo1D.h:219
A 1D distribution.
Definition: Dbn1D.h:28
double effNumEntries(bool includeoverflows=true) const
Get the effective number of fills.
Definition: Histo1D.cc:56
Scatter1D efficiency(const Counter &accepted, const Counter &total)
Calculate an efficiency ratio of two counters.
Definition: Counter.cc:40
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
Histo1D(const std::vector< double > &binedges, const std::string &path="", const std::string &title="")
Constructor giving explicit bin edges.
Definition: Histo1D.h:60
double xRMS(bool includeoverflows=true) const
Get the RMS in x.
Definition: Histo1D.cc:106
double FillType
Definition: Histo1D.h:34
1D bin container
Definition: Axis1D.h:20