CMS 3D CMS Logo

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