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 }
UnbinnedLikelihoodFit::datasize_
uint32_t datasize_
Definition: UnbinnedLikelihoodFit.h:56
UnbinnedLikelihoodFit::UnbinnedLikelihoodFit
UnbinnedLikelihoodFit()
Definition: UnbinnedLikelihoodFit.cc:30
HIPAlignmentAlgorithm_cfi.verbosity
verbosity
Definition: HIPAlignmentAlgorithm_cfi.py:7
UnbinnedLL
void UnbinnedLL(Int_t &, Double_t *, Double_t &val, Double_t *par, Int_t)
Definition: UnbinnedLikelihoodFit.cc:22
mps_fire.i
i
Definition: mps_fire.py:355
UnbinnedLikelihoodFit::~UnbinnedLikelihoodFit
~UnbinnedLikelihoodFit() override
Definition: UnbinnedLikelihoodFit.cc:40
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
UnbinnedLikelihoodFit.h
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
mps_update.status
status
Definition: mps_update.py:69
HLTSiStripMonitoring_cff.Fitter
Fitter
Definition: HLTSiStripMonitoring_cff.py:207
UnbinnedLikelihoodFit::logL
double logL(const double *x) const
Definition: UnbinnedLikelihoodFit.cc:90
UnbinnedLikelihoodFit::arglist_
double arglist_[10]
Definition: UnbinnedLikelihoodFit.h:62
DDAxes::x
tools.TF1
TF1
Definition: tools.py:23
parameters
parameters
Definition: BeamSpot_PayloadInspector.cc:14
UnbinnedLikelihoodFit::nparameters_
uint32_t nparameters_
Definition: UnbinnedLikelihoodFit.h:60
vertices_cff.x
x
Definition: vertices_cff.py:29
UnbinnedLikelihoodFit::min
TVirtualFitter * min
Definition: UnbinnedLikelihoodFit.h:66
UnbinnedLikelihoodFit::fit
int32_t fit(int32_t verbosity=-1)
Definition: UnbinnedLikelihoodFit.cc:56
UnbinnedLikelihoodFit::x_
double * x_
Definition: UnbinnedLikelihoodFit.h:57
heppy_batch.val
val
Definition: heppy_batch.py:351
UnbinnedLikelihoodFit::setData
void setData(uint32_t n, double *x)
Definition: UnbinnedLikelihoodFit.cc:44
UnbinnedLikelihoodFit::UnbinnedLL
friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag)
Definition: UnbinnedLikelihoodFit.cc:22
UnbinnedLikelihoodFit::setFunction
void setFunction(TF1 *f)
Definition: UnbinnedLikelihoodFit.cc:50
UnbinnedLikelihoodFit::maxIterations_
uint32_t maxIterations_
Definition: UnbinnedLikelihoodFit.h:63
UnbinnedLikelihoodFit::function_
TF1 * function_
Definition: UnbinnedLikelihoodFit.h:59
UnbinnedLikelihoodFit::tolerance_
double tolerance_
Definition: UnbinnedLikelihoodFit.h:64