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)
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
mps_fire.i
i
Definition: mps_fire.py:355
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
funct::HistoPdf::dump
void dump()
Definition: HistoPdf.h:56
funct::HistoPdf::operator()
double operator()(double x) const
Definition: HistoPdf.h:38
funct::RootHistoPdf::RootHistoPdf
RootHistoPdf(const TH1 &histo, double fMin, double fMax)
Definition: HistoPdf.h:76
gather_cfg.cout
cout
Definition: gather_cfg.py:144
timingPdfMaker.histo
histo
Definition: timingPdfMaker.py:279
funct::HistoPdf::init
void init(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:24
funct::HistoPdf::HistoPdf
HistoPdf(double xMin, double xMax, const Iterator &begin, const Iterator &end)
Definition: HistoPdf.h:13
Iterator
Definition: DQMStoreStats.h:94
end
#define end
Definition: vmac.h:39
edm::Exception
Definition: EDMException.h:77
photonAnalyzer_cfi.xMin
xMin
Definition: photonAnalyzer_cfi.py:82
EDMException.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
funct::HistoPdf::binSize_
double binSize_
Definition: HistoPdf.h:70
funct::HistoPdf::xMin_
double xMin_
Definition: HistoPdf.h:70
seedmultiplicitymonitor_newtracking_cfi.nBins
nBins
Definition: seedmultiplicitymonitor_newtracking_cfi.py:8
funct::HistoPdf::rebin
void rebin(unsigned int r)
Definition: HistoPdf.h:44
dqmdumpme.k
k
Definition: dqmdumpme.py:60
funct::HistoPdf::HistoPdf
HistoPdf()
Definition: HistoPdf.h:22
funct::HistoPdf::delta_
double delta_
Definition: HistoPdf.h:70
funct::HistoPdf::xMax_
double xMax_
Definition: HistoPdf.h:70
funct::HistoPdf::y_
std::vector< double > y_
Definition: HistoPdf.h:71
alignCSCRings.r
r
Definition: alignCSCRings.py:93
funct::HistoPdf
Definition: HistoPdf.h:10
multiplicitycorr_cfi.xMax
xMax
Definition: multiplicitycorr_cfi.py:5
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::errors::Configuration
Definition: EDMException.h:36
begin
#define begin
Definition: vmac.h:32
funct
Definition: Abs.h:5
funct::RootHistoPdf
Definition: HistoPdf.h:74