CMS 3D CMS Logo

UnbinnedLikelihoodFit.h
Go to the documentation of this file.
1 #ifndef __UnbinnedLikelihoodFit_h_
2 #define __UnbinnedLikelihoodFit_h_
3 #include <TObject.h>
4 #include <TF1.h>
5 #include <TVirtualFitter.h>
6 #include <cstdint>
7 
8 // a class to perform a likelihood fit
9 // Author: Christophe Delaere
10 
11 /* Example of a Landau fit:
12  * ------------------------
13  * UnbinnedLikelihoodFit myfit;
14  * double x[4] = {89,110,70,80};
15  * myfit.setData(4,x);
16  * TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255);
17  * myfunction->SetParameters(100,10);
18  * myfit.setFunction(myfunction);
19  * myfit.fit();
20  * myfit.getFunction()->Print();
21  * double MPV = myfit.getFunction()->GetParameter(0);
22  * double MPVerror = myfit.getFunction()->GetParError(0);
23  */
24 class UnbinnedLikelihoodFit : public TObject {
25 public:
26  // Constructor and destructor
28  ~UnbinnedLikelihoodFit() override;
29 
30  // Set the data for the fit: a set of measurements
31  void setData(uint32_t n, double* x);
32 
33  // Set the fit function
34  void setFunction(TF1* f);
35 
36  // Set the fit options
37  void setTolerance(double tol) { tolerance_ = tol; }
38  void setMaxIterations(uint32_t n) { maxIterations_ = n; }
39 
40  // Fit
41  int32_t fit(int32_t verbosity = -1);
42  int32_t fit(int32_t n, double* x, int32_t verbosity = -1) {
43  setData(n, x);
44  return fit(verbosity);
45  }
46 
47  // Results a retrieved via the TF1
48  TF1* getFunction() const { return function_; }
49  double getParameterValue(uint32_t i) { return function_ ? function_->GetParameter(i) : 0; }
50  double getParameterError(uint32_t i) { return function_ ? function_->GetParError(i) : 0; }
51  double* getParameterValues() { return function_ ? function_->GetParameters() : nullptr; }
52  const double* getParameterErrors() { return function_ ? function_->GetParErrors() : nullptr; }
53 
54 private:
55  // input data
56  uint32_t datasize_;
57  double* x_;
58  // the function
59  TF1* function_;
60  uint32_t nparameters_;
61  // arguments for Minuit methods
62  double arglist_[10];
63  uint32_t maxIterations_;
64  double tolerance_;
65  // the minimizer (minuit)
66  TVirtualFitter* min;
67 
68 private:
69  // LL function
70  double logL(const double* x) const;
71  // The function to minimize
72  friend void UnbinnedLL(Int_t& npar, Double_t* gin, Double_t& val, Double_t* par, Int_t iflag);
73 };
74 
75 #endif
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
mps_fire.i
i
Definition: mps_fire.py:428
UnbinnedLikelihoodFit::~UnbinnedLikelihoodFit
~UnbinnedLikelihoodFit() override
Definition: UnbinnedLikelihoodFit.cc:40
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
UnbinnedLikelihoodFit::getParameterErrors
const double * getParameterErrors()
Definition: UnbinnedLikelihoodFit.h:52
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
UnbinnedLikelihoodFit::logL
double logL(const double *x) const
Definition: UnbinnedLikelihoodFit.cc:90
UnbinnedLikelihoodFit::arglist_
double arglist_[10]
Definition: UnbinnedLikelihoodFit.h:62
DDAxes::x
UnbinnedLikelihoodFit::getParameterValues
double * getParameterValues()
Definition: UnbinnedLikelihoodFit.h:51
UnbinnedLikelihoodFit::nparameters_
uint32_t nparameters_
Definition: UnbinnedLikelihoodFit.h:60
UnbinnedLikelihoodFit::min
TVirtualFitter * min
Definition: UnbinnedLikelihoodFit.h:66
UnbinnedLikelihoodFit::fit
int32_t fit(int32_t verbosity=-1)
Definition: UnbinnedLikelihoodFit.cc:56
UnbinnedLikelihoodFit::fit
int32_t fit(int32_t n, double *x, int32_t verbosity=-1)
Definition: UnbinnedLikelihoodFit.h:42
UnbinnedLikelihoodFit::x_
double * x_
Definition: UnbinnedLikelihoodFit.h:57
UnbinnedLikelihoodFit::setTolerance
void setTolerance(double tol)
Definition: UnbinnedLikelihoodFit.h:37
heppy_batch.val
val
Definition: heppy_batch.py:351
UnbinnedLikelihoodFit::setMaxIterations
void setMaxIterations(uint32_t n)
Definition: UnbinnedLikelihoodFit.h:38
UnbinnedLikelihoodFit
Definition: UnbinnedLikelihoodFit.h:24
UnbinnedLikelihoodFit::getParameterValue
double getParameterValue(uint32_t i)
Definition: UnbinnedLikelihoodFit.h:49
UnbinnedLikelihoodFit::getFunction
TF1 * getFunction() const
Definition: UnbinnedLikelihoodFit.h:48
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::getParameterError
double getParameterError(uint32_t i)
Definition: UnbinnedLikelihoodFit.h:50
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