CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/Utilities/interface/HistoPoissonLikelihoodRatio.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_Utilities_HistoPoissonLikelihoodRatio_h
00002 #define PhysicsTools_Utilities_HistoPoissonLikelihoodRatio_h
00003 #include "PhysicsTools/Utilities/interface/RootMinuitResultPrinter.h"
00004 #include <vector>
00005 #include <cmath>
00006 #include "TH1.h"
00007 #include "TMath.h"
00008 
00009 namespace fit {
00010   template<typename T>
00011   class HistoPoissonLikelihoodRatio {
00012    public:
00013     HistoPoissonLikelihoodRatio() { }
00014     HistoPoissonLikelihoodRatio(T & t, TH1 *histo, double rangeMin, double rangeMax): 
00015       t_(&t), rangeMin_(rangeMin), rangeMax_(rangeMax) { 
00016       nBins_ = histo->GetNbinsX();
00017       xMin_ = histo->GetXaxis()->GetXmin();
00018       xMax_ = histo->GetXaxis()->GetXmax();
00019       deltaX_ =(xMax_ - xMin_) / nBins_;
00020       for(size_t i = 0; i < nBins_; ++i) { 
00021         cont_.push_back(histo->GetBinContent(i+1));
00022       }
00023     }
00024     double operator()() const { 
00025       double chi2lambda = 0;
00026       for(size_t i = 0; i < nBins_; ++i) { 
00027         double x = xMin_ + ( i +.5 ) * deltaX_;
00028         if((x > rangeMin_)&&(x < rangeMax_)) {
00029           double nu = (*t_)(x);
00030           if(nu > 0 && cont_[i]>0) chi2lambda += nu - cont_[i] + cont_[i]*log(cont_[i]/nu);
00031         }
00032       }
00033       chi2lambda *= 2;
00034       return chi2lambda;
00035     }
00036     void setHistos(TH1 *histo) { 
00037       nBins_ = histo->GetNbinsX();
00038       xMin_ = histo->GetXaxis()->GetXmin();
00039       xMax_ = histo->GetXaxis()->GetXmax();
00040       deltaX_ =(xMax_ - xMin_) / nBins_;
00041     }
00042     size_t numberOfBins() const {
00043       size_t fullBins = 0;
00044       for(size_t i = 0; i < nBins_; ++i) { 
00045         double x = xMin_ + ( i +.5 ) * deltaX_;
00046         if((x > rangeMin_)&&(x < rangeMax_))  
00047           fullBins++;
00048       }
00049       return fullBins; 
00050     }
00051     T & function() { return * t_; }
00052     const T & function() const { return * t_; }
00053   private:
00054     T * t_;
00055     double rangeMin_, rangeMax_;
00056     size_t nBins_;
00057     double xMin_, xMax_, deltaX_;
00058     std::vector<double> cont_;
00059   };
00060 
00061   template<typename T>
00062   struct RootMinuitResultPrinter<HistoPoissonLikelihoodRatio<T> > {
00063     static void print(double amin, unsigned int numberOfFreeParameters, const HistoPoissonLikelihoodRatio<T> & f) {
00064       unsigned int ndof = f.numberOfBins() - numberOfFreeParameters;
00065       std::cout << "chi-squared/n.d.o.f. = " << amin << "/" << ndof << " = " << amin/ndof 
00066                 << "; prob: " << TMath::Prob(amin, ndof)
00067                 << std::endl;
00068     }
00069   };
00070 }
00071 
00072 #endif