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,
14  const Iterator & begin, const Iterator & end) :
15  xMin_(xMin), xMax_(xMax), delta_(xMax - xMin), binSize_(delta_ / (end - begin)), y_(end - begin) {
16  double s = 0;
17  unsigned int i = 0;
18  for(Iterator it = begin; it != end; ++it)
19  s += (y_[i++] = *it);
20  for(std::vector<double>::iterator i = y_.begin(); i != y_.end(); ++i)
21  *i /= s;
22  }
23  HistoPdf() { }
24  template<typename Iterator>
25  void init(double xMin, double xMax,
26  const Iterator & begin, const Iterator & end) {
27  xMin_ = xMin;
28  xMax_ = xMax;
29  delta_ = xMax - xMin;
30  unsigned int n = end - begin;
31  binSize_ = delta_ / n;
32  y_.resize(n);
33  double s = 0;
34  unsigned int i = 0;
35  for(Iterator it = begin; it != end; ++it)
36  s += (y_[i++] = *it);
37  for(std::vector<double>::iterator i = y_.begin(); i != y_.end(); ++i)
38  *i /= s;
39  }
40  double operator()(double x) const {
41  if (x < xMin_ || x > xMax_) return 0;
42  double pdf = y_[static_cast<unsigned int>(((x -xMin_)/delta_)*y_.size())] / binSize_;
43  return pdf;
44  }
45  void rebin(unsigned int r) {
46  if(y_.size() % r != 0)
48  "HistoPdf: can't rebin histogram of " << y_.size() << " entries by " << r << "\n";
49  unsigned int n = y_.size() / r;
50  std::vector<double> y(n, 0);
51  for(unsigned int i = 0, j = 0; i < n; ++i)
52  for(unsigned int k = 0; k < r; ++k)
53  y[i] += y_[j++];
54  y_ = y;
55  binSize_ *= r;
56  }
57  void dump() {
58  std::cout << ">>> range: [" << xMin_ << ", " << xMax_ << "], bin size: "
59  << delta_ << "/" << y_.size() << " = " << binSize_ << std::endl;
60  double s = 0;
61  for(unsigned int i = 0; i != y_.size(); ++i) {
62  double x = xMin_ + (0.5 + i)*binSize_;
63  double y = operator()(x);
64  std::cout << ">>> pdf(" << x << ") = " << y << std::endl;
65  s+= y*binSize_;
66  }
67  std::cout << ">>>: PDF normalization is " << s << std::endl;
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 }
94 
95 
96 #endif
HistoPdf(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:13
Definition: Abs.h:5
RootHistoPdf(const TH1 &histo, double fMin, double fMax)
Definition: HistoPdf.h:76
void dump()
Definition: HistoPdf.h:57
double operator()(double x) const
Definition: HistoPdf.h:40
void init(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:25
void rebin(unsigned int r)
Definition: HistoPdf.h:45
double binSize_
Definition: HistoPdf.h:70
double xMin_
Definition: HistoPdf.h:70
double xMax_
Definition: HistoPdf.h:70
#define end
Definition: vmac.h:39
int k[5][pyjets_maxn]
std::vector< double > y_
Definition: HistoPdf.h:71
#define begin
Definition: vmac.h:32
double delta_
Definition: HistoPdf.h:70