CMS 3D CMS Logo

HistoPdf.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_HistoPdf_h
2 #define PhysicsTools_Utilities_HistoPdf_h
3 
5 
6 #include <iostream>
7 #include "TH1.h"
8 
9 namespace funct {
10  class HistoPdf {
11  public:
12  template <typename Iterator>
13  HistoPdf(double xMin, double xMax, const Iterator& begin, const Iterator& end)
14  : xMin_(xMin), xMax_(xMax), delta_(xMax - xMin), binSize_(delta_ / (end - begin)), y_(end - begin) {
15  double s = 0;
16  unsigned int i = 0;
17  for (Iterator it = begin; it != end; ++it)
18  s += (y_[i++] = *it);
19  for (std::vector<double>::iterator i = y_.begin(); i != y_.end(); ++i)
20  *i /= s;
21  }
22  HistoPdf() {}
23  template <typename Iterator>
24  void init(double xMin, double xMax, const Iterator& begin, const Iterator& end) {
25  xMin_ = xMin;
26  xMax_ = xMax;
27  delta_ = xMax - xMin;
28  unsigned int n = end - begin;
29  binSize_ = delta_ / n;
30  y_.resize(n);
31  double s = 0;
32  unsigned int i = 0;
33  for (Iterator it = begin; it != end; ++it)
34  s += (y_[i++] = *it);
35  for (std::vector<double>::iterator i = y_.begin(); i != y_.end(); ++i)
36  *i /= s;
37  }
38  double operator()(double x) const {
39  if (x < xMin_ || x > xMax_)
40  return 0;
41  double pdf = y_[static_cast<unsigned int>(((x - xMin_) / delta_) * y_.size())] / binSize_;
42  return pdf;
43  }
44  void rebin(unsigned int r) {
45  if (y_.size() % r != 0)
47  << "HistoPdf: can't rebin histogram of " << y_.size() << " entries by " << r << "\n";
48  unsigned int n = y_.size() / r;
49  std::vector<double> y(n, 0);
50  for (unsigned int i = 0, j = 0; i < n; ++i)
51  for (unsigned int k = 0; k < r; ++k)
52  y[i] += y_[j++];
53  y_ = y;
54  binSize_ *= r;
55  }
56  void dump() {
57  std::cout << ">>> range: [" << xMin_ << ", " << xMax_ << "], bin size: " << delta_ << "/" << y_.size() << " = "
58  << binSize_ << std::endl;
59  double s = 0;
60  for (unsigned int i = 0; i != y_.size(); ++i) {
61  double x = xMin_ + (0.5 + i) * binSize_;
62  double y = operator()(x);
63  std::cout << ">>> pdf(" << x << ") = " << y << std::endl;
64  s += y * binSize_;
65  }
66  std::cout << ">>>: PDF normalization is " << s << std::endl;
67  }
68 
69  private:
71  std::vector<double> y_;
72  };
73 
74  class RootHistoPdf : public HistoPdf {
75  public:
76  explicit RootHistoPdf(const TH1& histo, double fMin, double fMax) {
77  unsigned int nBins = histo.GetNbinsX();
78  std::vector<double> y;
79  y.reserve(nBins);
80  double xMin = histo.GetXaxis()->GetXmin();
81  double xMax = histo.GetXaxis()->GetXmax();
82  double deltaX = (xMax - xMin) / nBins;
83  for (unsigned int i = 0; i != nBins; ++i) {
84  double x = xMin + (i + .5) * deltaX;
85  if (x > fMin && x < fMax) {
86  y.push_back(histo.GetBinContent(i + 1));
87  }
88  }
89  init(fMin, fMax, y.begin(), y.end());
90  }
91  };
92 
93 } // namespace funct
94 
95 #endif
HistoPdf(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:13
double operator()(double x) const
Definition: HistoPdf.h:38
Definition: Abs.h:5
RootHistoPdf(const TH1 &histo, double fMin, double fMax)
Definition: HistoPdf.h:76
void dump()
Definition: HistoPdf.h:56
void init(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:24
void rebin(unsigned int r)
Definition: HistoPdf.h:44
double binSize_
Definition: HistoPdf.h:70
double xMin_
Definition: HistoPdf.h:70
double xMax_
Definition: HistoPdf.h:70
std::vector< double > y_
Definition: HistoPdf.h:71
float x
double delta_
Definition: HistoPdf.h:70