CMS 3D CMS Logo

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_ = nullptr;
34  min = nullptr;
35  tolerance_ = 0.01;
36  maxIterations_ = 1000;
37 }
38 
39 // the destructor
41 
42 // sets the data
43 // the class is not owner of the data... it only keeps a pointer to it.
44 void UnbinnedLikelihoodFit::setData(uint32_t n, double* x) {
45  datasize_ = n;
46  x_ = x;
47 }
48 
49 // sets the function for the fit
51  function_ = f;
52  nparameters_ = function_ ? function_->GetNpar() : 0;
53 }
54 
55 // The fit itself
57  // creates a fitter
59  min->SetFCN(UnbinnedLL);
60 
61  // set print level: no output
62  arglist_[0] = 0;
63  min->ExecuteCommand("SET NOWarnings", arglist_, 1);
64  arglist_[0] = verbosity;
65  min->ExecuteCommand("SET PRINT", arglist_, 1);
66 
67  // initial values, error, range
68  double parmin, parmax;
69  for (uint32_t i = 0; i < nparameters_; ++i) {
70  function_->GetParLimits(i, parmin, parmax);
71  min->SetParameter(i, function_->GetParName(i), function_->GetParameter(i), tolerance_, parmin, parmax);
72  }
73 
74  // run MIGRAD
75  arglist_[0] = maxIterations_; // number of function calls
76  arglist_[1] = tolerance_; // tolerance
77  int32_t status = min->ExecuteCommand("MIGRAD", arglist_, 2);
78 
79  // get fit parameters and errors
80  for (uint32_t i = 0; i < nparameters_; ++i) {
81  function_->SetParameter(i, min->GetParameter(i));
82  function_->SetParError(i, min->GetParError(i));
83  }
84 
85  // returns the status
86  return status;
87 }
88 
89 // the log-likelihood function
90 double UnbinnedLikelihoodFit::logL(const double* parameters) const {
91  double val = 0;
92  if (!function_)
93  return val;
94  for (uint32_t i = 0; i < datasize_; ++i) {
95  val += TMath::Log(function_->EvalPar(&(x_[i]), parameters));
96  }
97  return val;
98 }
void UnbinnedLL(Int_t &, Double_t *, Double_t &val, Double_t *par, Int_t)
void setData(uint32_t n, double *x)
friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag)
double logL(const double *x) const
double f[11][100]
int32_t fit(int32_t verbosity=-1)
float x