yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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/Binned.h"
11 #include "YODA/Fillable.h"
12 #include "YODA/HistoBin1D.h"
13 #include "YODA/Dbn1D.h"
14 #include "YODA/Scatter2D.h"
15 #include "YODA/Axis1D.h"
16 #include "YODA/Exceptions.h"
17 #include <vector>
18 #include <string>
19 #include <map>
20 
21 namespace YODA {
22 
23 
26 
28  class Histo1D : public AnalysisObject, public Binned, public Fillable {
29  public:
30 
32  typedef Histo1DAxis Axis;
33  typedef Axis::Bins Bins;
34  typedef HistoBin1D Bin;
35 
36  typedef double FillType;
37  typedef FillType BinType;
38  typedef std::shared_ptr<Histo1D> Ptr;
39 
42 
44  Histo1D(const std::string& path="", const std::string& title="")
45  : AnalysisObject("Histo1D", path, title),
46  _axis()
47  { }
48 
49 
51  Histo1D(size_t nbins, double lower, double upper,
52  const std::string& path="", const std::string& title="")
53  : AnalysisObject("Histo1D", path, title),
54  _axis(nbins, lower, upper)
55  { }
56 
57 
62  Histo1D(const std::vector<double>& binedges,
63  const std::string& path="", const std::string& title="")
64  : AnalysisObject("Histo1D", path, title),
65  _axis(binedges)
66  { }
67 
68 
70  Histo1D(const std::vector<Bin>& bins,
71  const std::string& path="", const std::string& title="")
72  : AnalysisObject("Histo1D", path, title),
73  _axis(bins)
74  { }
75 
76 
79  Histo1D(const Histo1D& h, const std::string& path="");
80 
81 
84  Histo1D(const Scatter2D& s, const std::string& path="");
85 
86 
89  Histo1D(const Profile1D& p, const std::string& path="");
90 
91 
95  Histo1D(const std::vector<HistoBin1D>& bins,
96  const Dbn1D& dbn_tot, const Dbn1D& dbn_uflow, const Dbn1D& dbn_oflow,
97  const std::string& path="", const std::string& title="")
98  : AnalysisObject("Histo1D", path, title),
99  _axis(bins, dbn_tot, dbn_uflow, dbn_oflow)
100  { }
101 
102 
105  AnalysisObject::operator = (h1); //< AO treatment of paths etc.
106  _axis = h1._axis;
107  return *this;
108  }
109 
111  Histo1D clone() const {
112  return Histo1D(*this);
113  }
114 
116  Histo1D* newclone() const {
117  return new Histo1D(*this);
118  }
119 
121 
122 
126  size_t dim() const { return 1; }
127 
129  size_t fillDim() const { return 1; }
130 
131 
134 
138  virtual void reset() {
139  _axis.reset();
140  }
141 
143  virtual void fill(double x, double weight=1.0, double fraction=1.0);
144 
146  virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0);
147 
148 
150  void scaleW(double scalefactor) {
151  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
152  _axis.scaleW(scalefactor);
153  }
154 
155 
161  void normalize(double normto=1.0, bool includeoverflows=true) {
162  const double oldintegral = integral(includeoverflows);
163  if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
165  scaleW(normto / oldintegral);
166  }
167 
168 
170  void mergeBins(size_t from, size_t to) {
171  _axis.mergeBins(from, to);
172  }
173 
174 
176  void rebinBy(unsigned int n, size_t begin=0, size_t end=UINT_MAX) {
177  _axis.rebinBy(n, begin, end);
178  }
180  void rebin(unsigned int n, size_t begin=0, size_t end=UINT_MAX) {
181  rebinBy(n, begin, end);
182  }
183 
185  void rebinTo(const std::vector<double>& newedges) {
186  _axis.rebinTo(newedges);
187  }
189  void rebin(const std::vector<double>& newedges) {
190  rebinTo(newedges);
191  }
192 
194 
195 
198 
200  size_t numBins() const { return bins().size(); }
201 
203  size_t numBinsX() const { return numBins(); }
204 
206  double xMin() const { return _axis.xMin(); }
207 
209  double xMax() const { return _axis.xMax(); }
210 
212  bool sameBinning(const Histo1D& h1) {
213  return _axis == h1._axis;
214  }
215 
220  const std::vector<double> xEdges() const { return _axis.xEdges(); }
221 
222 
224  std::vector<YODA::HistoBin1D>& bins() { return _axis.bins(); }
226  const std::vector<YODA::HistoBin1D>& bins() const { return _axis.bins(); }
227 
228 
230  HistoBin1D& bin(size_t index) { return _axis.bins()[index]; }
232  const HistoBin1D& bin(size_t index) const { return _axis.bins()[index]; }
233 
234 
237  int binIndexAt(double x) {
238  return _axis.binIndexAt(x);
239  }
240 
242  const HistoBin1D& binAt(double x) const { return _axis.binAt(x); }
243 
244 
246  Dbn1D& totalDbn() { return _axis.totalDbn(); }
248  const Dbn1D& totalDbn() const { return _axis.totalDbn(); }
250  void setTotalDbn(const Dbn1D& dbn) { _axis.setTotalDbn(dbn); }
251 
252 
254  Dbn1D& underflow() { return _axis.underflow(); }
256  const Dbn1D& underflow() const { return _axis.underflow(); }
258  void setUnderflow(const Dbn1D& dbn) { _axis.setUnderflow(dbn); }
259 
260 
262  Dbn1D& overflow() { return _axis.overflow(); }
264  const Dbn1D& overflow() const { return _axis.overflow(); }
266  void setOverflow(const Dbn1D& dbn) { _axis.setOverflow(dbn); }
267 
269 
270 
273 
275  void addBin(double from, double to) { _axis.addBin(from, to); }
276 
278  void addBins(std::vector<double> edges) { _axis.addBins(edges); }
279 
280  // /// Add new bins specifying a beginning and end of each of them
281  // void addBins(std::vector<std::pair<double,double> > edges) {
282  // _axis.addBins(edges);
283  // }
284 
286  void addBin(const HistoBin1D& b) { _axis.addBin(b); }
287 
291  void addBins(const Bins& bins) {
292  _axis.addBins(bins);
293  }
294 
296  void rmBin(size_t index) { _axis.eraseBin(index); }
297 
299 
300 
303 
305  double integral(bool includeoverflows=true) const { return sumW(includeoverflows); }
306 
314  double integralRange(size_t binindex1, size_t binindex2) const {
315  assert(binindex2 >= binindex1);
316  if (binindex1 >= numBins()) throw RangeError("binindex1 is out of range");
317  if (binindex2 >= numBins()) throw RangeError("binindex2 is out of range");
318  double rtn = 0;
319  for (size_t i = binindex1; i <= binindex2; ++i) {
320  rtn += bin(i).sumW();
321  }
322  return rtn;
323  }
324 
331  double integralTo(size_t binindex, bool includeunderflow=true) const {
332  double rtn = includeunderflow ? underflow().sumW() : 0;
333  rtn += integralRange(0, binindex);
334  return rtn;
335  }
336 
338  double numEntries(bool includeoverflows=true) const;
339 
341  double effNumEntries(bool includeoverflows=true) const;
342 
344  double sumW(bool includeoverflows=true) const;
345 
347  double sumW2(bool includeoverflows=true) const;
348 
350  double xMean(bool includeoverflows=true) const;
351 
353  double xVariance(bool includeoverflows=true) const;
354 
356  double xStdDev(bool includeoverflows=true) const {
357  if (includeoverflows) return _axis.totalDbn().xStdDev();
358  return std::sqrt(xVariance(includeoverflows));
359  }
360 
362  double xStdErr(bool includeoverflows=true) const;
363 
365  double xRMS(bool includeoverflows=true) const;
366 
368 
369 
372 
376  Histo1D& operator += (const Histo1D& toAdd) {
377  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
378  _axis += toAdd._axis;
379  return *this;
380 
381  // if (!hasAnnotation("ScaledBy") && !toAdd.hasAnnotation("ScaledBy")) {
382  // _axis += toAdd._axis;
383  // } else {
384  // // Undo scaling of both histograms
385  // double scaledBy = annotation<double>("ScaledBy", 1.0);
386  // _axis.scaleW(1.0/scaledBy);
387 
388  // double toAddScaledBy = toAdd.annotation<double>("ScaledBy", 1.0);
389  // Axis1D<HistoBin1D, Dbn1D> toAddAxis = toAdd._axis;
390  // toAddAxis.scaleW(1.0/toAddScaledBy);
391 
392  // _axis += toAddAxis;
393 
394  // // Re-apply combined scaling
395  // double newScaledBy = scaledBy*toAddScaledBy/(scaledBy+toAddScaledBy);
396  // _axis.scaleW(newScaledBy);
397  // setAnnotation("ScaledBy", newScaledBy);
398  // }
400  // return *this;
401  }
402 
406  Histo1D& operator -= (const Histo1D& toSubtract) {
407  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
408  _axis -= toSubtract._axis;
409  return *this;
410  }
411 
413 
414 
415  protected:
416 
418  HistoBin1D& _binAt(double x) { return _axis.binAt(x); }
419 
420 
421  private:
422 
425 
428 
430 
431  };
432 
433 
435  typedef Histo1D H1D;
436 
437 
440 
442  inline Histo1D add(const Histo1D& first, const Histo1D& second) {
443  Histo1D tmp = first;
444  if (first.path() != second.path()) tmp.setPath("");
445  tmp += second;
446  return tmp;
447  }
448 
449 
451  inline Histo1D operator + (const Histo1D& first, const Histo1D& second) {
452  return add(first, second);
453  }
454 
455 
457  inline Histo1D subtract(const Histo1D& first, const Histo1D& second) {
458  Histo1D tmp = first;
459  if (first.path() != second.path()) tmp.setPath("");
460  tmp -= second;
461  return tmp;
462  }
463 
464 
466  inline Histo1D operator - (const Histo1D& first, const Histo1D& second) {
467  return subtract(first, second);
468  }
469 
470 
472 
473 
479  Scatter2D divide(const Histo1D& numer, const Histo1D& denom);
480 
481 
485  inline Scatter2D operator / (const Histo1D& numer, const Histo1D& denom) {
486  return divide(numer, denom);
487  }
488 
489 
491  Scatter2D add(const Histo1D& histo, const Scatter2D& scatt);
492 
493  inline Scatter2D add(const Scatter2D& scatt, const Histo1D& histo) {
494  return add(histo, scatt);
495  }
496 
498  Scatter2D subtract(const Histo1D& histo, const Scatter2D& scatt);
499 
501  Scatter2D subtract(const Scatter2D& scatt, const Histo1D& histo);
502 
504  Scatter2D multiply(const Histo1D& histo, const Scatter2D& scatt);
505 
507  inline Scatter2D multiply(const Scatter2D& scatt, const Histo1D& histo) {
508  return multiply(histo, scatt);
509  }
510 
512  Scatter2D divide(const Histo1D& numer, const Scatter2D& denom);
513 
515  Scatter2D divide(const Scatter2D& numer, const Histo1D& denom);
516 
518 
519 
520 
527  Scatter2D efficiency(const Histo1D& accepted, const Histo1D& total);
528 
529 
533  inline Scatter2D asymm(const Histo1D& a, const Histo1D& b) {
534  return (a-b) / (a+b);
535  }
536 
537 
548  Scatter2D toIntegralHisto(const Histo1D& h, bool includeunderflow=true);
549 
550 
564  Scatter2D toIntegralEfficiencyHisto(const Histo1D& h, bool includeunderflow=true, bool includeoverflow=true);
565 
567 
568 
569 }
570 
571 #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:314
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:256
FillType BinType
Definition: Histo1D.h:37
Histo1D & operator+=(const Histo1D &toAdd)
Add another histogram to this one.
Definition: Histo1D.h:376
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
size_t fillDim() const
Fill dimension of this data object.
Definition: Histo1D.h:129
double xStdDev(bool includeoverflows=true) const
Get the standard deviation in x.
Definition: Histo1D.h:356
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:170
size_t numBinsX() const
Number of bins on the x (only) axis (not counting under/overflow)
Definition: Histo1D.h:203
A very generic data type which is just a collection of 2D data points with errors.
Definition: Scatter2D.h:25
double integral(bool includeoverflows=true) const
Get the total area (sumW) of the histogram.
Definition: Histo1D.h:305
void setTotalDbn(const Dbn1D &dbn)
Set summary distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:250
Axis::Bins Bins
Definition: Histo1D.h:33
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:264
void setPath(const std::string &path)
A base class for all fillable objects.
Definition: Fillable.h:16
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
A base class for all binned objects.
Definition: Binned.h:16
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
Dbn1D & underflow()
Access underflow (non-const version)
Definition: Histo1D.h:254
double xMin() const
Low edge of this histo&#39;s axis.
Definition: Histo1D.h:206
Dbn1D & overflow()
Access overflow (non-const version)
Definition: Histo1D.h:262
std::vector< YODA::HistoBin1D > & bins()
Access the bin vector.
Definition: Histo1D.h:224
A one-dimensional histogram.
Definition: Histo1D.h:28
void rmBin(size_t index)
Remove a bin.
Definition: Histo1D.h:296
int binIndexAt(double x)
Definition: Histo1D.h:237
const std::vector< YODA::HistoBin1D > & bins() const
Access the bin vector (const version)
Definition: Histo1D.h:226
Histo1D(const std::vector< Bin > &bins, const std::string &path="", const std::string &title="")
Constructor accepting an explicit collection of bins.
Definition: Histo1D.h:70
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:247
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:51
void rebin(const std::vector< double > &newedges)
Overloaded alias for rebinTo.
Definition: Histo1D.h:189
Axis1D< HistoBin1D, Dbn1D > Histo1DAxis
Convenience typedef.
Definition: Histo1D.h:25
double xMean(bool includeoverflows=true) const
Get the mean in x.
Definition: Histo1D.cc:82
Histo1D & operator=(const Histo1D &h1)
Assignment operator.
Definition: Histo1D.h:104
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:38
Histo1D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Histo1D.h:44
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:176
double integralTo(size_t binindex, bool includeunderflow=true) const
Get the integrated area of the histogram up to bin binindex.
Definition: Histo1D.h:331
Histo1D H1D
Convenience typedef.
Definition: Histo1D.h:435
void setUnderflow(const Dbn1D &dbn)
Set underflow distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:258
virtual void reset()
Reset the histogram.
Definition: Histo1D.h:138
void rebin(unsigned int n, size_t begin=0, size_t end=UINT_MAX)
Overloaded alias for rebinBy.
Definition: Histo1D.h:180
bool sameBinning(const Histo1D &h1)
check if binning is the same as different Histo1D
Definition: Histo1D.h:212
Histo1DAxis Axis
Convenience typedefs.
Definition: Histo1D.h:32
void setOverflow(const Dbn1D &dbn)
Set overflow distribution, mainly for persistency: CAREFUL!
Definition: Histo1D.h:266
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:286
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:278
void normalize(double normto=1.0, bool includeoverflows=true)
Definition: Histo1D.h:161
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Histo1D.h:150
HistoBin1D Bin
Definition: Histo1D.h:34
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:260
void addBins(const Bins &bins)
Bins addition operator.
Definition: Histo1D.h:291
const std::string title() const
Get the AO title.
size_t dim() const
Fill dimension of this data object.
Definition: Histo1D.h:126
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:230
AnalysisObject is the base class for histograms and scatters.
size_t numBins() const
Number of bins (not counting under/overflow)
Definition: Histo1D.h:200
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:111
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:246
void rebinTo(const std::vector< double > &newedges)
Rebin to the given list of bin edges.
Definition: Histo1D.h:185
double sumW() const
The sum of weights.
Definition: Bin1D.h:226
Scatter2D asymm(const Histo1D &a, const Histo1D &b)
Calculate the asymmetry (a-b)/(a+b) of two histograms.
Definition: Histo1D.h:533
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:95
const HistoBin1D & binAt(double x) const
Access a bin by coordinate (const version)
Definition: Histo1D.h:242
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:278
const Dbn1D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Histo1D.h:248
Histo1D & operator-=(const Histo1D &toSubtract)
Subtract another histogram from this one.
Definition: Histo1D.h:406
Histo1D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Histo1D.h:116
A one-dimensional profile histogram.
Definition: Profile1D.h:35
void addBin(double from, double to)
Add a new bin specifying its lower and upper bound.
Definition: Histo1D.h:275
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:220
double xMax() const
High edge of this histo&#39;s axis.
Definition: Histo1D.h:209
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:232
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:62
double xRMS(bool includeoverflows=true) const
Get the RMS in x.
Definition: Histo1D.cc:106
double FillType
Definition: Histo1D.h:36
1D bin container
Definition: Axis1D.h:20