CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UnbinnedLikelihoodFit.cc
Go to the documentation of this file.
2 #include <TMath.h>
3 
4 // a class to perform a likelihood fit
5 // Author: Christophe Delaere
6 
7 /* Example of a Landau fit:
8  * ------------------------
9  * UnbinnedLikelihoodFit myfit;
10  * double x[4] = {89,110,70,80};
11  * myfit.setData(4,x);
12  * TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255);
13  * myfunction->SetParameters(100,10);
14  * myfit.setFunction(myfunction);
15  * myfit.fit();
16  * myfit.getFunction()->Print();
17  * double MPV = myfit.getFunction()->GetParameter(0);
18  * double MPVerror = myfit.getFunction()->GetParError(0);
19  */
20 
21 // the function passed to minuit
22 void UnbinnedLL(Int_t&, Double_t*, Double_t &val, Double_t *par, Int_t) {
23  // retrieve the data object (it's also the fitter)
24  // - sign to have a minimum
25  // factor 2 to have the right errors (see for example the pdg)
26  val = -2*((dynamic_cast<const UnbinnedLikelihoodFit*>((TVirtualFitter::GetFitter())->GetObjectFit()))->logL(par));
27 }
28 
29 // the constructor
31  nparameters_ = 0;
32  datasize_ = 0;
33  x_ = NULL;
34  min = NULL;
35  tolerance_ = 0.01;
36  maxIterations_ = 1000;
37 }
38 
39 // the destructor
41 }
42 
43 // sets the data
44 // the class is not owner of the data... it only keeps a pointer to it.
45 void UnbinnedLikelihoodFit::setData(uint32_t n, double* x) {
46  datasize_ = n;
47  x_ = x;
48 }
49 
50 // sets the function for the fit
52  function_ = f;
53  nparameters_ = function_ ? function_->GetNpar() : 0;
54 }
55 
56 // The fit itself
58  // creates a fitter
60  min->SetFCN(UnbinnedLL);
61 
62  // set print level: no output
63  arglist_[0] = 0;
64  min->ExecuteCommand("SET NOWarnings",arglist_,1);
65  arglist_[0] = verbosity;
66  min->ExecuteCommand("SET PRINT",arglist_,1);
67 
68  // initial values, error, range
69  double parmin,parmax;
70  for(uint32_t i=0;i<nparameters_;++i) {
71  function_->GetParLimits(i, parmin, parmax);
72  min->SetParameter(i,
73  function_->GetParName(i),
74  function_->GetParameter(i),
75  tolerance_,
76  parmin, parmax);
77  }
78 
79  // run MIGRAD
80  arglist_[0] = maxIterations_; // number of function calls
81  arglist_[1] = tolerance_; // tolerance
82  int32_t status = min->ExecuteCommand("MIGRAD",arglist_,2);
83 
84  // get fit parameters and errors
85  for(uint32_t i=0;i<nparameters_;++i) {
86  function_->SetParameter(i, min->GetParameter(i));
87  function_->SetParError( i, min->GetParError(i) );
88  }
89 
90  // returns the status
91  return status;
92 }
93 
94 // the log-likelihood function
95 double UnbinnedLikelihoodFit::logL(const double* parameters) const {
96  double val=0;
97  if(!function_) return val;
98  for (uint32_t i=0;i<datasize_;++i){
99  val += TMath::Log(function_->EvalPar(&(x_[i]),parameters));
100  }
101  return val;
102 }
103 
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
void UnbinnedLL(Int_t &, Double_t *, Double_t &val, Double_t *par, Int_t)
void setData(uint32_t n, double *x)
double logL(const double *x) const
friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag)
#define NULL
Definition: scimark2.h:8
double f[11][100]
int32_t fit(int32_t verbosity=-1)
Definition: DDAxes.h:10
tuple status
Definition: ntuplemaker.py:245