CMS 3D CMS Logo

HistoPoissonLikelihoodRatio.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_HistoPoissonLikelihoodRatio_h
2 #define PhysicsTools_Utilities_HistoPoissonLikelihoodRatio_h
4 #include <vector>
5 #include <cmath>
6 #include "TH1.h"
7 #include "TMath.h"
8 
9 namespace fit {
10  template<typename T>
12  public:
14  HistoPoissonLikelihoodRatio(T & t, TH1 *histo, double rangeMin, double rangeMax):
15  t_(&t), rangeMin_(rangeMin), rangeMax_(rangeMax) {
16  nBins_ = histo->GetNbinsX();
17  xMin_ = histo->GetXaxis()->GetXmin();
18  xMax_ = histo->GetXaxis()->GetXmax();
19  deltaX_ =(xMax_ - xMin_) / nBins_;
20  for(size_t i = 0; i < nBins_; ++i) {
21  cont_.push_back(histo->GetBinContent(i+1));
22  }
23  }
24  double operator()() const {
25  double chi2lambda = 0;
26  for(size_t i = 0; i < nBins_; ++i) {
27  double x = xMin_ + ( i +.5 ) * deltaX_;
28  if((x > rangeMin_)&&(x < rangeMax_)) {
29  double nu = (*t_)(x);
30  if(nu > 0 && cont_[i]>0) chi2lambda += nu - cont_[i] + cont_[i]*log(cont_[i]/nu);
31  }
32  }
33  chi2lambda *= 2;
34  return chi2lambda;
35  }
36  void setHistos(TH1 *histo) {
37  nBins_ = histo->GetNbinsX();
38  xMin_ = histo->GetXaxis()->GetXmin();
39  xMax_ = histo->GetXaxis()->GetXmax();
40  deltaX_ =(xMax_ - xMin_) / nBins_;
41  }
42  size_t numberOfBins() const {
43  size_t fullBins = 0;
44  for(size_t i = 0; i < nBins_; ++i) {
45  double x = xMin_ + ( i +.5 ) * deltaX_;
46  if((x > rangeMin_)&&(x < rangeMax_))
47  fullBins++;
48  }
49  return fullBins;
50  }
51  T & function() { return * t_; }
52  const T & function() const { return * t_; }
53  private:
54  T * t_;
56  size_t nBins_;
57  double xMin_, xMax_, deltaX_;
58  std::vector<double> cont_;
59  };
60 
61  template<typename T>
63  static void print(double amin, unsigned int numberOfFreeParameters, const HistoPoissonLikelihoodRatio<T> & f) {
64  unsigned int ndof = f.numberOfBins() - numberOfFreeParameters;
65  std::cout << "chi-squared/n.d.o.f. = " << amin << "/" << ndof << " = " << amin/ndof
66  << "; prob: " << TMath::Prob(amin, ndof)
67  << std::endl;
68  }
69  };
70 }
71 
72 #endif
double f[11][100]
HistoPoissonLikelihoodRatio(T &t, TH1 *histo, double rangeMin, double rangeMax)
long double T
static void print(double amin, unsigned int numberOfFreeParameters, const HistoPoissonLikelihoodRatio< T > &f)