yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
Histo2D.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_Histo2D_h
7 #define YODA_Histo2D_h
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Fillable.h"
11 #include "YODA/Binned.h"
12 #include "YODA/HistoBin2D.h"
13 #include "YODA/Dbn2D.h"
14 #include "YODA/Axis2D.h"
15 #include "YODA/Scatter3D.h"
16 #include "YODA/Exceptions.h"
17 #include <vector>
18 #include <tuple>
19 
20 namespace YODA {
21 
22 
23  // Forward declaration
24  class Profile2D;
25  class Scatter3D;
26 
29 
30 
32  class Histo2D : public AnalysisObject, public Fillable, public Binned {
33  public:
34 
36  typedef Histo2DAxis Axis;
37  typedef Axis::Bins Bins;
38  typedef HistoBin2D Bin;
40 
41  typedef std::tuple<double, double> FillType;
42  typedef FillType BinType;
43  typedef std::shared_ptr<Histo2D> Ptr;
44 
45 
48 
50  Histo2D(const std::string& path="", const std::string& title="")
51  : AnalysisObject("Histo2D", path, title),
52  _axis()
53  { }
54 
55 
57  Histo2D(size_t nbinsX, double lowerX, double upperX,
58  size_t nbinsY, double lowerY, double upperY,
59  const std::string& path="", const std::string& title="")
60  : AnalysisObject("Histo2D", path, title),
61  _axis(nbinsX, std::make_pair(lowerX, upperX), nbinsY, std::make_pair(lowerY, upperY))
62  { }
63 
64 
66  Histo2D(const std::vector<double>& xedges, const std::vector<double>& yedges,
67  const std::string& path="", const std::string& title="")
68  : AnalysisObject("Histo2D", path, title),
69  _axis(xedges, yedges)
70  { }
71 
72 
74  Histo2D(const std::vector<Bin>& bins,
75  const std::string& path="", const std::string& title="")
76  : AnalysisObject("Histo2D", path, title),
77  _axis(bins)
78  { }
79 
80 
83  Histo2D(const Histo2D& h, const std::string& path="");
84 
87  Histo2D(const Scatter3D& s, const std::string& path="");
88 
91  Histo2D(const Profile2D& h, const std::string& path="");
92 
96  Histo2D(const std::vector<HistoBin2D>& bins,
97  const Dbn2D& totalDbn,
98  const Outflows& outflows,
99  const std::string& path="", const std::string& title="")
100  : AnalysisObject("Histo2D", path, title),
101  _axis(bins, totalDbn, outflows)
102  { }
103 
104 
107  AnalysisObject::operator = (h2); //< AO treatment of paths etc.
108  _axis = h2._axis;
109  return *this;
110  }
111 
112 
114  Histo2D clone() const {
115  return Histo2D(*this);
116  }
117 
119  Histo2D* newclone() const {
120  return new Histo2D(*this);
121  }
122 
124 
125 
129  size_t dim() const { return 2; }
130 
132  size_t fillDim() const { return 2; }
133 
134 
137 
139  virtual void fill(double x, double y, double weight=1.0, double fraction=1.0);
140 
142  virtual void fill(const FillType & xs, double weight=1.0, double fraction=1.0) {
143  fill(std::get<0>(xs), std::get<1>(xs), weight, fraction);
144  }
145 
146 
148  virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0);
149 
150 
154  void reset() {
155  _axis.reset();
156  }
157 
159  void scaleW(double scalefactor) {
160  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
161  _axis.scaleW(scalefactor);
162  }
163 
164 
170  void normalize(double normto=1.0, bool includeoverflows=true) {
171  const double oldintegral = integral(includeoverflows);
172  if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
173  scaleW(normto / oldintegral);
174  }
175 
176 
178  void scaleXY(double scaleX = 1.0, double scaleY = 1.0) {
179  _axis.scaleXY(scaleX, scaleY);
180  }
181 
183 
184 
187 
191  void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange) {
192  _axis.addBin(xrange, yrange);
193  }
194 
198  void addBin(const Bin& bin) {
199  _axis.addBin(bin);
200  }
201 
202 
206  void addBins(const Axis::Edges& xcuts, const Axis::Edges& ycuts) {
207  _axis.addBins(xcuts, ycuts);
208  }
209 
213  void addBins(const Bins& bins) {
214  _axis.addBins(bins);
215  }
216 
217 
218  // /// Adding bins
220  // void addBin(const std::vector<std::pair<std::pair<double,double>, std::pair<double,double> > > coords) {
221  // _axis.addBin(coords);
222  // }
223 
224  // /// Adding bins which is not so eloquent
226  // void addBin(double lowX, double lowY, double highX, double highY) {
227  // _axis.addBin(lowX, lowY, highX, highY);
228  // }
229 
230  // /// Merge the bins
232  // void mergeBins(size_t from, size_t to) {
233  // _axis.mergeBins(from, to);
234  // }
235 
239  // void rebin(size_t factorX, size_t factorY){
240  // _axis.rebin(factorX, factorY);
241  // }
242 
243 
244  void rmBin(size_t index) {
245  _axis.eraseBin(index);
246  }
247 
249 
250 
253 
258  const std::vector<double> xEdges() const { return _axis.xEdges(); }
259 
264  const std::vector<double> yEdges() const { return _axis.yEdges(); }
265 
267 
269  double xMin() const { return _axis.xMin(); }
270 
272  double xMax() const { return _axis.xMax(); }
273 
274 
276  double yMin() const { return _axis.yMin(); }
277 
279  double yMax() const { return _axis.yMax(); }
280 
281 
283  bool sameBinning(const Histo2D& h2) {
284  return _axis == h2._axis;
285  }
286 
288  std::vector<YODA::HistoBin2D>& bins() { return _axis.bins(); }
290  const std::vector<YODA::HistoBin2D>& bins() const { return _axis.bins(); }
291 
292 
294  HistoBin2D& bin(size_t index) { return _axis.bin(index); }
296  const HistoBin2D& bin(size_t index) const { return _axis.bin(index); }
297 
298 
300  int binIndexAt(double x, double y) { return _axis.binIndexAt(x, y); }
301 
302  int binIndexAt(const BinType& t) { return _axis.binIndexAt(std::get<0>(t), std::get<1>(t)); }
303 
305  const HistoBin2D& binAt(double x, double y) const { return _axis.binAt(x, y); }
306 
307  const HistoBin2D& binAt(const BinType& t) { return _axis.binAt(std::get<0>(t), std::get<1>(t)); }
308 
309 
311  size_t numBins() const { return _axis.numBins(); }
312 
314  size_t numBinsX() const { return _axis.numBinsX(); }
315 
317  size_t numBinsY() const { return _axis.numBinsY(); }
318 
319 
321  Dbn2D& totalDbn() { return _axis.totalDbn(); }
323  const Dbn2D& totalDbn() const { return _axis.totalDbn(); }
325  void setTotalDbn(const Dbn2D& dbn) { _axis.setTotalDbn(dbn); }
326 
327 
328  // /// @brief Access an outflow (non-const)
329  // ///
330  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
331  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
332  // Dbn2D& outflow(int ix, int iy) {
333  // std::cout << "Histo2D::outflow\n";
334  // return _axis.outflow(ix, iy);
335  // }
336 
337  // /// @brief Access an outflow (const)
338  // ///
339  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
340  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
341  // const Dbn2D& outflow(int ix, int iy) const {
342  // return _axis.outflow(ix, iy);
343  // }
344 
346 
347 
350 
352  double integral(bool includeoverflows=true) const { return sumW(includeoverflows); }
353 
355  double numEntries(bool includeoverflows=true) const;
356 
358  double effNumEntries(bool includeoverflows=true) const;
359 
361  double sumW(bool includeoverflows=true) const;
362 
364  double sumW2(bool includeoverflows=true) const;
365 
367  double xMean(bool includeoverflows=true) const;
368 
370  double yMean(bool includeoverflows=true) const;
371 
373  double xVariance(bool includeoverflows=true) const;
374 
376  double yVariance(bool includeoverflows=true) const;
377 
379  double xStdDev(bool includeoverflows=true) const {
380  return std::sqrt(xVariance(includeoverflows));
381  }
382 
384  double yStdDev(bool includeoverflows=true) const {
385  return std::sqrt(yVariance(includeoverflows));
386  }
387 
389  double xStdErr(bool includeoverflows=true) const;
390 
392  double yStdErr(bool includeoverflows=true) const;
393 
395  double xRMS(bool includeoverflows=true) const;
396 
398  double yRMS(bool includeoverflows=true) const;
399 
401 
402 
405 
409  Histo2D& operator += (const Histo2D& toAdd) {
410  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
411  _axis += toAdd._axis;
412  return *this;
413  }
414 
418  Histo2D& operator -= (const Histo2D& toSubtract) {
419  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
420  _axis -= toSubtract._axis;
421  return *this;
422  }
423 
424  bool operator == (const Histo2D& other) const {
425  return _axis == other._axis;
426  }
427 
428  bool operator != (const Histo2D& other) const {
429  return ! operator == (other);
430  }
431 
433 
434 
435  // /// @name Slicing operators
436  // /// @{
437 
438  // /// @brief Create a Histo2D for the bin slice parallel to the x axis at the specified y coordinate
439  // ///
440  // /// Note that the created histogram will not have correctly filled underflow and overflow bins.
441  // /// @todo It's not really *at* the specified y coord: it's for the corresponding bin row.
442  // /// @todo Change the name!
443  // Histo2D cutterX(double atY, const std::string& path="", const std::string& title="");
444 
445 
446  // /// @brief Create a Histo2D for the bin slice parallel to the y axis at the specified x coordinate
447  // ///
448  // /// Note that the created histogram will not have correctly filled underflow and overflow bins.
449  // /// @todo It's not really *at* the specified x coord: it's for the corresponding bin row.
450  // /// @todo Change the name!
451  // Histo2D cutterY(double atX, const std::string& path="", const std::string& title="");
452 
453 
454  // /// X-wise Profile1D creator from Histo2D
455  // Profile1D mkProfileX();
456 
457  // /// Y-wise Profile1D creator from Histo2D
458  // Profile1D mkProfileY();
459  // /// @}
460 
461 
462  protected:
463 
465  HistoBin2D& _binAt(double x, double y) { return _axis.binAt(x, y); }
466 
467 
468  private:
469 
472 
475 
477 
478  };
479 
480 
482  typedef Histo2D H2D;
483 
484 
487 
489  inline Histo2D add(const Histo2D& first, const Histo2D& second) {
490  Histo2D tmp = first;
491  if (first.path() != second.path()) tmp.setPath("");
492  tmp += second;
493  return tmp;
494  }
495 
496 
498  inline Histo2D operator + (const Histo2D& first, const Histo2D& second) {
499  return add(first, second);
500  }
501 
502 
504  inline Histo2D subtract(const Histo2D& first, const Histo2D& second) {
505  Histo2D tmp = first;
506  if (first.path() != second.path()) tmp.setPath("");
507  tmp -= second;
508  return tmp;
509  }
510 
511 
513  inline Histo2D operator - (const Histo2D& first, const Histo2D& second) {
514  return subtract(first, second);
515  }
516 
517 
519 
520 
526  Scatter3D divide(const Histo2D& numer, const Histo2D& denom);
527 
528 
532  inline Scatter3D operator / (const Histo2D& numer, const Histo2D& denom) {
533  return divide(numer, denom);
534  }
535 
536 
543  Scatter3D efficiency(const Histo2D& accepted, const Histo2D& total);
544 
545 
549  inline Scatter3D asymm(const Histo2D& a, const Histo2D& b) {
550  return (a-b) / (a+b);
551  }
552 
554 
555 
556 }
557 
558 #endif
size_t dim() const
Fill dimension of this data object.
Definition: Histo2D.h:129
std::pair< double, double > EdgePair1D
Definition: Axis2D.h:35
void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange)
Bin addition operator.
Definition: Histo2D.h:191
void rmAnnotation(const std::string &name)
Delete an annotation by name.
double integral(bool includeoverflows=true) const
Get the total volume of the histogram.
Definition: Histo2D.h:352
double yStdDev(bool includeoverflows=true) const
Get the standard deviation in y.
Definition: Histo2D.h:384
const std::vector< YODA::HistoBin2D > & bins() const
Access the bin vector (const version)
Definition: Histo2D.h:290
const std::vector< double > xEdges() const
Definition: Histo2D.h:258
double effNumEntries(bool includeoverflows=true) const
Get the effective number of fills.
Definition: Histo2D.cc:95
bool operator!=(const Histo2D &other) const
Definition: Histo2D.h:428
Histo2D(size_t nbinsX, double lowerX, double upperX, size_t nbinsY, double lowerY, double upperY, const std::string &path="", const std::string &title="")
Constructor giving range and number of bins.
Definition: Histo2D.h:57
std::vector< YODA::HistoBin2D > & bins()
Access the bin vector (non-const version)
Definition: Histo2D.h:288
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
void addBin(const Bin &bin)
Bin addition operator.
Definition: Histo2D.h:198
int binIndexAt(const BinType &t)
Definition: Histo2D.h:302
bool sameBinning(const Histo2D &h2)
check if binning is the same as different Histo2D
Definition: Histo2D.h:283
const std::string path() const
Get the AO path.
const HistoBin2D & binAt(const BinType &t)
Definition: Histo2D.h:307
const std::vector< double > yEdges() const
Definition: Histo2D.h:264
void rmBin(size_t index)
Definition: Histo2D.h:244
double xVariance(bool includeoverflows=true) const
Get the variance in x.
Definition: Histo2D.cc:138
void setPath(const std::string &path)
Histo2D H2D
Convenience typedef.
Definition: Histo2D.h:482
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
A base class for all binned objects.
Definition: Binned.h:16
Histo2D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Histo2D.h:119
STL namespace.
virtual void fill(const FillType &xs, double weight=1.0, double fraction=1.0)
Definition: Histo2D.h:142
std::vector< Bin > Bins
A vector containing 2D bins. Not used for searching.
Definition: Axis2D.h:31
std::tuple< double, double > FillType
Definition: Histo2D.h:41
virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0)
Fill histo x-y bin i with the given weight.
Definition: Histo2D.cc:78
void addBins(const Axis::Edges &xcuts, const Axis::Edges &ycuts)
Bins addition operator.
Definition: Histo2D.h:206
void setTotalDbn(const Dbn2D &dbn)
Set summary distribution, including gaps and overflows.
Definition: Histo2D.h:325
double yStdErr(bool includeoverflows=true) const
Get the standard error in y.
Definition: Histo2D.cc:162
const HistoBin2D & binAt(double x, double y) const
Access a bin by coordinate (const version)
Definition: Histo2D.h:305
Histo2D & operator-=(const Histo2D &toSubtract)
Subtract another histogram from this one.
Definition: Histo2D.h:418
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:247
2D bin container
Definition: Axis2D.h:21
Axis::Bins Bins
Definition: Histo2D.h:37
size_t fillDim() const
Fill dimension of this data object.
Definition: Histo2D.h:132
A 2D distribution.
Definition: Dbn2D.h:16
bool operator==(const Histo2D &other) const
Definition: Histo2D.h:424
A two-dimensional profile histogram.
Definition: Profile2D.h:32
double xMin() const
Low x edge of this histo&#39;s axis.
Definition: Histo2D.h:269
void addBins(const Bins &bins)
Bins addition operator.
Definition: Histo2D.h:213
Histo2DAxis Axis
Convenience typedefs.
Definition: Histo2D.h:36
Histo2D & operator+=(const Histo2D &toAdd)
Add another histogram to this one.
Definition: Histo2D.h:409
int binIndexAt(double x, double y)
Access a bin index by coordinate.
Definition: Histo2D.h:300
double yVariance(bool includeoverflows=true) const
Get the variance in y.
Definition: Histo2D.cc:146
double xMax() const
High x edge of this histo&#39;s axis.
Definition: Histo2D.h:272
const HistoBin2D & bin(size_t index) const
Access a bin by index (const version)
Definition: Histo2D.h:296
Axis2D< HistoBin2D, Dbn2D > Histo2DAxis
Convenience typedef.
Definition: Histo2D.h:25
Errors relating to event/bin weights.
Definition: Exceptions.h:65
Histo2D(const std::vector< Bin > &bins, const std::string &path="", const std::string &title="")
Constructor accepting an explicit collection of bins.
Definition: Histo2D.h:74
Axis1D< BIN1D, DBN > operator+(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Add the statistics on two axes.
Definition: Axis1D.h:589
void normalize(double normto=1.0, bool includeoverflows=true)
Definition: Histo2D.h:170
virtual void fill(double x, double y, double weight=1.0, double fraction=1.0)
Fill histo with weight at (x,y)
Definition: Histo2D.cc:50
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Histo2D.h:159
double numEntries(bool includeoverflows=true) const
Get the number of fills (fractional fills are possible)
Definition: Histo2D.cc:87
double yRMS(bool includeoverflows=true) const
Get the RMS in y.
Definition: Histo2D.cc:178
Histo2D clone() const
Make a copy on the stack.
Definition: Histo2D.h:114
const Dbn2D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Histo2D.h:323
double xStdDev(bool includeoverflows=true) const
Get the standard deviation in x.
Definition: Histo2D.h:379
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:260
size_t numBinsX() const
Number of bins along the x axis.
Definition: Histo2D.h:314
double yMean(bool includeoverflows=true) const
Get the mean y.
Definition: Histo2D.cc:130
double xStdErr(bool includeoverflows=true) const
Get the standard error in x.
Definition: Histo2D.cc:154
double xMean(bool includeoverflows=true) const
Get the mean x.
Definition: Histo2D.cc:122
const std::string title() const
Get the AO title.
double yMin() const
Low y edge of this histo&#39;s axis.
Definition: Histo2D.h:276
HistoBin2D Bin
Definition: Histo2D.h:38
Dbn2D & totalDbn()
Access summary distribution, including gaps and overflows (non-const version)
Definition: Histo2D.h:321
Axis::Outflows Outflows
Definition: Histo2D.h:39
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:25
A Bin2D specialised for handling histogram-type information.
Definition: HistoBin2D.h:21
double sumW2(bool includeoverflows=true) const
Get the sum of squared weights in histo.
Definition: Histo2D.cc:111
std::vector< Outflow > Outflows
Definition: Axis2D.h:41
AnalysisObject is the base class for histograms and scatters.
double yMax() const
High y edge of this histo&#39;s axis.
Definition: Histo2D.h:279
Histo2D & operator=(const Histo2D &h2)
Assignment operator.
Definition: Histo2D.h:106
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
void scaleXY(double scaleX=1.0, double scaleY=1.0)
Scale the dimensions.
Definition: Histo2D.h:178
HistoBin2D & bin(size_t index)
Access a bin by index (non-const version)
Definition: Histo2D.h:294
std::shared_ptr< Histo2D > Ptr
Definition: Histo2D.h:43
Scatter2D asymm(const Histo1D &a, const Histo1D &b)
Calculate the asymmetry (a-b)/(a+b) of two histograms.
Definition: Histo1D.h:533
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:278
FillType BinType
Definition: Histo2D.h:42
Histo2D(const std::vector< HistoBin2D > &bins, const Dbn2D &totalDbn, const Outflows &outflows, const std::string &path="", const std::string &title="")
State-setting constructor.
Definition: Histo2D.h:96
A two-dimensional histogram.
Definition: Histo2D.h:32
void reset()
Reset the histogram.
Definition: Histo2D.h:154
Histo2D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Histo2D.h:50
double xRMS(bool includeoverflows=true) const
Get the RMS in x.
Definition: Histo2D.cc:170
double sumW(bool includeoverflows=true) const
Get the sum of weights in histo.
Definition: Histo2D.cc:103
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
size_t numBins() const
Number of bins.
Definition: Histo2D.h:311
Scatter1D efficiency(const Counter &accepted, const Counter &total)
Calculate an efficiency ratio of two counters.
Definition: Counter.cc:40
size_t numBinsY() const
Number of bins along the y axis.
Definition: Histo2D.h:317
std::vector< double > Edges
Definition: Axis2D.h:34
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
Histo2D(const std::vector< double > &xedges, const std::vector< double > &yedges, const std::string &path="", const std::string &title="")
Constructor accepting the bin edges on X and Y axis.
Definition: Histo2D.h:66