CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/RecoTracker/DeDx/src/UnbinnedLikelihoodFit.cc

Go to the documentation of this file.
00001 #include "RecoTracker/DeDx/interface/UnbinnedLikelihoodFit.h"
00002 #include <TMath.h>
00003 
00004 // a class to perform a likelihood fit
00005 // Author: Christophe Delaere
00006 
00007 /* Example of a Landau fit:
00008  * ------------------------
00009  * UnbinnedLikelihoodFit myfit;
00010  * double x[4] = {89,110,70,80};
00011  * myfit.setData(4,x);
00012  * TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255);
00013  * myfunction->SetParameters(100,10);
00014  * myfit.setFunction(myfunction);
00015  * myfit.fit();
00016  * myfit.getFunction()->Print();
00017  * double MPV = myfit.getFunction()->GetParameter(0);
00018  * double MPVerror = myfit.getFunction()->GetParError(0);
00019  */
00020 
00021 // the function passed to minuit
00022 void UnbinnedLL(Int_t&, Double_t*, Double_t &val, Double_t *par, Int_t) {
00023   // retrieve the data object (it's also the fitter)
00024   // - sign to have a minimum
00025   // factor 2 to have the right errors (see for example the pdg)
00026   val = -2*((dynamic_cast<const UnbinnedLikelihoodFit*>((TVirtualFitter::GetFitter())->GetObjectFit()))->logL(par));
00027 }
00028 
00029 // the constructor
00030 UnbinnedLikelihoodFit::UnbinnedLikelihoodFit() {
00031   nparameters_ = 0;
00032   datasize_ = 0;
00033   x_ = NULL;
00034   min = NULL;
00035   tolerance_ = 0.01;
00036   maxIterations_ = 1000;
00037 }
00038 
00039 // the destructor
00040 UnbinnedLikelihoodFit::~UnbinnedLikelihoodFit() {
00041 }
00042 
00043 // sets the data
00044 // the class is not owner of the data... it only keeps a pointer to it.
00045 void UnbinnedLikelihoodFit::setData(uint32_t n, double* x) {
00046   datasize_ = n;
00047   x_ = x;
00048 }
00049 
00050 // sets the function for the fit
00051 void UnbinnedLikelihoodFit::setFunction(TF1* f) {
00052   function_ = f;
00053   nparameters_ = function_ ? function_->GetNpar() : 0;
00054 }
00055 
00056 // The fit itself
00057 int32_t UnbinnedLikelihoodFit::fit(int32_t verbosity) {
00058   // creates a fitter 
00059   min = TVirtualFitter::Fitter(this,nparameters_);
00060   min->SetFCN(UnbinnedLL);
00061   
00062   // set print level: no output
00063   arglist_[0] = 0;
00064   min->ExecuteCommand("SET NOWarnings",arglist_,1);
00065   arglist_[0] = verbosity;
00066   min->ExecuteCommand("SET PRINT",arglist_,1);
00067   
00068   // initial values, error, range
00069   double parmin,parmax;
00070   for(uint32_t i=0;i<nparameters_;++i) {
00071     function_->GetParLimits(i, parmin, parmax);
00072     min->SetParameter(i,
00073                       function_->GetParName(i),
00074                       function_->GetParameter(i),
00075                       tolerance_,
00076                       parmin, parmax);
00077   }
00078 
00079   // run MIGRAD
00080   arglist_[0] = maxIterations_; // number of function calls
00081   arglist_[1] = tolerance_;     // tolerance
00082   int32_t status = min->ExecuteCommand("MIGRAD",arglist_,2);
00083 
00084   // get fit parameters and errors
00085   for(uint32_t i=0;i<nparameters_;++i) {
00086     function_->SetParameter(i, min->GetParameter(i));
00087     function_->SetParError( i, min->GetParError(i) );
00088   }
00089 
00090   // returns the status
00091   return status;
00092 }
00093 
00094 // the log-likelihood function
00095 double UnbinnedLikelihoodFit::logL(const double* parameters) const {
00096   double val=0;
00097   if(!function_) return val;
00098   for (uint32_t i=0;i<datasize_;++i){
00099     val += TMath::Log(function_->EvalPar(&(x_[i]),parameters));
00100   }
00101   return val; 
00102 } 
00103