yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
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-2017 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/HistoBin2D.h"
11 #include "YODA/Dbn2D.h"
12 #include "YODA/Axis2D.h"
13 #include "YODA/Scatter3D.h"
14 #include "YODA/Exceptions.h"
15 
16 #include <vector>
17 #include <tuple>
18 
19 namespace YODA {
20 
21 
22  // Forward declaration
23  class Profile2D;
24  class Scatter3D;
25 
28 
29 
31  class Histo2D : public AnalysisObject {
32  public:
33 
35  typedef Histo2DAxis Axis;
36  typedef Axis::Bins Bins;
37  typedef HistoBin2D Bin;
39 
40  typedef std::tuple<double, double> FillType;
41  typedef FillType BinType;
42  typedef std::shared_ptr<Histo2D> Ptr;
43 
44 
46 
47 
49  Histo2D(const std::string& path="", const std::string& title="")
50  : AnalysisObject("Histo2D", path, title),
51  _axis()
52  { }
53 
54 
56  Histo2D(size_t nbinsX, double lowerX, double upperX,
57  size_t nbinsY, double lowerY, double upperY,
58  const std::string& path="", const std::string& title="")
59  : AnalysisObject("Histo2D", path, title),
60  _axis(nbinsX, std::make_pair(lowerX, upperX), nbinsY, std::make_pair(lowerY, upperY))
61  { }
62 
63 
65  Histo2D(const std::vector<double>& xedges, const std::vector<double>& yedges,
66  const std::string& path="", const std::string& title="")
67  : AnalysisObject("Histo2D", path, title),
68  _axis(xedges, yedges)
69  { }
70 
71 
73  Histo2D(const std::vector<Bin>& bins,
74  const std::string& path="", const std::string& title="")
75  : AnalysisObject("Histo2D", path, title),
76  _axis(bins)
77  { }
78 
79 
82  Histo2D(const Histo2D& h, const std::string& path="");
83 
86  Histo2D(const Scatter3D& s, const std::string& path="");
87 
90  Histo2D(const Profile2D& h, const std::string& path="");
91 
95  Histo2D(const std::vector<HistoBin2D>& bins,
96  const Dbn2D& totalDbn,
97  const Outflows& outflows,
98  const std::string& path="", const std::string& title="")
99  : AnalysisObject("Histo2D", path, title),
100  _axis(bins, totalDbn, outflows)
101  { }
102 
103 
106  AnalysisObject::operator = (h2); //< AO treatment of paths etc.
107  _axis = h2._axis;
108  return *this;
109  }
110 
111 
113  Histo2D clone() const {
114  return Histo2D(*this);
115  }
116 
118  Histo2D* newclone() const {
119  return new Histo2D(*this);
120  }
121 
123 
124 
126  size_t dim() const { return 2; }
127 
128 
130 
131 
133  virtual void fill(double x, double y, double weight=1.0, double fraction=1.0);
134 
136  virtual void fill(const FillType & xs, double weight=1.0, double fraction=1.0) {
137  fill(std::get<0>(xs), std::get<1>(xs), weight, fraction);
138  }
139 
140 
142  virtual void fillBin(size_t i, double weight=1.0, double fraction=1.0);
143 
144 
148  void reset() {
149  _axis.reset();
150  }
151 
153  void scaleW(double scalefactor) {
154  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
155  _axis.scaleW(scalefactor);
156  }
157 
158 
164  void normalize(double normto=1.0, bool includeoverflows=true) {
165  const double oldintegral = integral(includeoverflows);
166  if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
167  scaleW(normto / oldintegral);
168  }
169 
170 
172  void scaleXY(double scaleX = 1.0, double scaleY = 1.0) {
173  _axis.scaleXY(scaleX, scaleY);
174  }
175 
176 
180  void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange) {
181  _axis.addBin(xrange, yrange);
182  }
183 
187  void addBin(const Bin& bin) {
188  _axis.addBin(bin);
189  }
190 
191 
195  void addBins(const Axis::Edges& xcuts, const Axis::Edges& ycuts) {
196  _axis.addBins(xcuts, ycuts);
197  }
198 
202  void addBins(const Bins& bins) {
203  _axis.addBins(bins);
204  }
205 
206 
207  // /// Adding bins
209  // void addBin(const std::vector<std::pair<std::pair<double,double>, std::pair<double,double> > > coords) {
210  // _axis.addBin(coords);
211  // }
212 
213  // /// Adding bins which is not so eloquent
215  // void addBin(double lowX, double lowY, double highX, double highY) {
216  // _axis.addBin(lowX, lowY, highX, highY);
217  // }
218 
219  // /// Merge the bins
221  // void mergeBins(size_t from, size_t to) {
222  // _axis.mergeBins(from, to);
223  // }
224 
228  // void rebin(size_t factorX, size_t factorY){
229  // _axis.rebin(factorX, factorY);
230  // }
231 
232 
233  void eraseBin(size_t index) {
234  _axis.eraseBin(index);
235  }
236 
238 
239 
240  public:
241 
243 
244 
246  double xMin() const { return _axis.xMin(); }
247 
249  double xMax() const { return _axis.xMax(); }
250 
251 
253  double yMin() const { return _axis.yMin(); }
254 
256  double yMax() const { return _axis.yMax(); }
257 
258 
260  std::vector<YODA::HistoBin2D>& bins() { return _axis.bins(); }
262  const std::vector<YODA::HistoBin2D>& bins() const { return _axis.bins(); }
263 
264 
266  HistoBin2D& bin(size_t index) { return _axis.bin(index); }
268  const HistoBin2D& bin(size_t index) const { return _axis.bin(index); }
269 
270 
272  int binIndexAt(double x, double y) { return _axis.binIndexAt(x, y); }
273 
274  int binIndexAt(const BinType& t) { return _axis.binIndexAt(std::get<0>(t), std::get<1>(t)); }
275 
277  const HistoBin2D& binAt(double x, double y) const { return _axis.binAt(x, y); }
278 
279  const HistoBin2D& binAt(const BinType& t) { return _axis.binAt(std::get<0>(t), std::get<1>(t)); }
280 
281 
283  size_t numBins() const { return _axis.numBins(); }
284 
286  size_t numBinsX() const { return _axis.numBinsX(); }
287 
289  size_t numBinsY() const { return _axis.numBinsY(); }
290 
291 
293  Dbn2D& totalDbn() { return _axis.totalDbn(); }
295  const Dbn2D& totalDbn() const { return _axis.totalDbn(); }
297  void setTotalDbn(const Dbn2D& dbn) { _axis.setTotalDbn(dbn); }
298 
299 
300  // /// @brief Access an outflow (non-const)
301  // ///
302  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
303  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
304  // Dbn2D& outflow(int ix, int iy) {
305  // std::cout << "Histo2D::outflow\n";
306  // return _axis.outflow(ix, iy);
307  // }
308 
309  // /// @brief Access an outflow (const)
310  // ///
311  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
312  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
313  // const Dbn2D& outflow(int ix, int iy) const {
314  // return _axis.outflow(ix, iy);
315  // }
316 
318 
319 
321 
322 
324  double integral(bool includeoverflows=true) const { return sumW(includeoverflows); }
325 
327  double numEntries(bool includeoverflows=true) const;
328 
330  double effNumEntries(bool includeoverflows=true) const;
331 
333  double sumW(bool includeoverflows=true) const;
334 
336  double sumW2(bool includeoverflows=true) const;
337 
339  double xMean(bool includeoverflows=true) const;
340 
342  double yMean(bool includeoverflows=true) const;
343 
345  double xVariance(bool includeoverflows=true) const;
346 
348  double yVariance(bool includeoverflows=true) const;
349 
351  double xStdDev(bool includeoverflows=true) const {
352  return std::sqrt(xVariance(includeoverflows));
353  }
354 
356  double yStdDev(bool includeoverflows=true) const {
357  return std::sqrt(yVariance(includeoverflows));
358  }
359 
361  double xStdErr(bool includeoverflows=true) const;
362 
364  double yStdErr(bool includeoverflows=true) const;
365 
367  double xRMS(bool includeoverflows=true) const;
368 
370  double yRMS(bool includeoverflows=true) const;
371 
373 
374 
376 
377 
381  Histo2D& operator += (const Histo2D& toAdd) {
382  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
383  _axis += toAdd._axis;
384  return *this;
385  }
386 
390  Histo2D& operator -= (const Histo2D& toSubtract) {
391  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
392  _axis -= toSubtract._axis;
393  return *this;
394  }
395 
396  bool operator == (const Histo2D& other) const {
397  return _axis == other._axis;
398  }
399 
400  bool operator != (const Histo2D& other) const {
401  return ! operator == (other);
402  }
403 
405 
406 
407  // /// @name Slicing operators
408  // //@{
409 
410  // /// @brief Create a Histo2D for the bin slice parallel to the x axis at the specified y coordinate
411  // ///
412  // /// Note that the created histogram will not have correctly filled underflow and overflow bins.
413  // /// @todo It's not really *at* the specified y coord: it's for the corresponding bin row.
414  // /// @todo Change the name!
415  // Histo2D cutterX(double atY, const std::string& path="", const std::string& title="");
416 
417 
418  // /// @brief Create a Histo2D for the bin slice parallel to the y axis at the specified x coordinate
419  // ///
420  // /// Note that the created histogram will not have correctly filled underflow and overflow bins.
421  // /// @todo It's not really *at* the specified x coord: it's for the corresponding bin row.
422  // /// @todo Change the name!
423  // Histo2D cutterY(double atX, const std::string& path="", const std::string& title="");
424 
425 
426  // /// X-wise Profile1D creator from Histo2D
427  // Profile1D mkProfileX();
428 
429  // /// Y-wise Profile1D creator from Histo2D
430  // Profile1D mkProfileY();
431  // //@}
432 
433 
434  protected:
435 
437  HistoBin2D& _binAt(double x, double y) { return _axis.binAt(x, y); }
438 
439 
440  private:
441 
443 
444 
447 
449 
450  };
451 
452 
454  typedef Histo2D H2D;
455 
456 
458 
459 
461  inline Histo2D add(const Histo2D& first, const Histo2D& second) {
462  Histo2D tmp = first;
463  if (first.path() != second.path()) tmp.setPath("");
464  tmp += second;
465  return tmp;
466  }
467 
468 
470  inline Histo2D operator + (const Histo2D& first, const Histo2D& second) {
471  return add(first, second);
472  }
473 
474 
476  inline Histo2D subtract(const Histo2D& first, const Histo2D& second) {
477  Histo2D tmp = first;
478  if (first.path() != second.path()) tmp.setPath("");
479  tmp -= second;
480  return tmp;
481  }
482 
483 
485  inline Histo2D operator - (const Histo2D& first, const Histo2D& second) {
486  return subtract(first, second);
487  }
488 
489 
491 
492 
498  Scatter3D divide(const Histo2D& numer, const Histo2D& denom);
499 
500 
504  inline Scatter3D operator / (const Histo2D& numer, const Histo2D& denom) {
505  return divide(numer, denom);
506  }
507 
508 
515  Scatter3D efficiency(const Histo2D& accepted, const Histo2D& total);
516 
517 
521  inline Scatter3D asymm(const Histo2D& a, const Histo2D& b) {
522  return (a-b) / (a+b);
523  }
524 
526 
527 
528 }
529 
530 #endif
size_t dim() const
Fill dimension of this data object.
Definition: Histo2D.h:126
std::pair< double, double > EdgePair1D
Definition: Axis2D.h:35
void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange)
Bin addition operator.
Definition: Histo2D.h:180
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:324
double yStdDev(bool includeoverflows=true) const
Get the standard deviation in y.
Definition: Histo2D.h:356
const std::vector< YODA::HistoBin2D > & bins() const
Access the bin vector (const version)
Definition: Histo2D.h:262
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:400
void eraseBin(size_t index)
Definition: Histo2D.h:233
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:56
std::vector< YODA::HistoBin2D > & bins()
Access the bin vector (non-const version)
Definition: Histo2D.h:260
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
void addBin(const Bin &bin)
Bin addition operator.
Definition: Histo2D.h:187
int binIndexAt(const BinType &t)
Definition: Histo2D.h:274
const std::string path() const
Get the AO path.
const HistoBin2D & binAt(const BinType &t)
Definition: Histo2D.h:279
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:454
Axis1D< BIN1D, DBN > operator-(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Subtract the statistics on two axis.
Definition: Axis1D.h:597
Histo2D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Histo2D.h:118
STL namespace.
virtual void fill(const FillType &xs, double weight=1.0, double fraction=1.0)
Definition: Histo2D.h:136
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:40
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:195
void setTotalDbn(const Dbn2D &dbn)
Set summary distribution, including gaps and overflows.
Definition: Histo2D.h:297
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:277
Histo2D & operator-=(const Histo2D &toSubtract)
Subtract another histogram from this one.
Definition: Histo2D.h:390
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:236
2D bin container
Definition: Axis2D.h:21
Axis::Bins Bins
Definition: Histo2D.h:36
A 2D distribution.
Definition: Dbn2D.h:16
bool operator==(const Histo2D &other) const
Definition: Histo2D.h:396
A two-dimensional profile histogram.
Definition: Profile2D.h:31
double xMin() const
Low x edge of this histo&#39;s axis.
Definition: Histo2D.h:246
void addBins(const Bins &bins)
Bins addition operator.
Definition: Histo2D.h:202
Histo2DAxis Axis
Convenience typedefs.
Definition: Histo2D.h:35
Histo2D & operator+=(const Histo2D &toAdd)
Add another histogram to this one.
Definition: Histo2D.h:381
int binIndexAt(double x, double y)
Access a bin index by coordinate.
Definition: Histo2D.h:272
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:249
const HistoBin2D & bin(size_t index) const
Access a bin by index (const version)
Definition: Histo2D.h:268
Axis2D< HistoBin2D, Dbn2D > Histo2DAxis
Convenience typedef.
Definition: Histo2D.h:24
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:73
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:164
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:153
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:113
const Dbn2D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Histo2D.h:295
double xStdDev(bool includeoverflows=true) const
Get the standard deviation in x.
Definition: Histo2D.h:351
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:249
size_t numBinsX() const
Number of bins along the x axis.
Definition: Histo2D.h:286
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:253
HistoBin2D Bin
Definition: Histo2D.h:37
Dbn2D & totalDbn()
Access summary distribution, including gaps and overflows (non-const version)
Definition: Histo2D.h:293
Axis::Outflows Outflows
Definition: Histo2D.h:38
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:24
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:256
Histo2D & operator=(const Histo2D &h2)
Assignment operator.
Definition: Histo2D.h:105
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:172
HistoBin2D & bin(size_t index)
Access a bin by index (non-const version)
Definition: Histo2D.h:266
std::shared_ptr< Histo2D > Ptr
Definition: Histo2D.h:42
Scatter2D asymm(const Histo1D &a, const Histo1D &b)
Calculate the asymmetry (a-b)/(a+b) of two histograms.
Definition: Histo1D.h:515
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:267
FillType BinType
Definition: Histo2D.h:41
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:95
A two-dimensional histogram.
Definition: Histo2D.h:31
void reset()
Reset the histogram.
Definition: Histo2D.h:148
Histo2D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Histo2D.h:49
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:283
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:289
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:65