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
int32_t fit(int32_t n, double *x, int32_t verbosity=-1)
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)
void setTolerance(double tol)
double logL(const double *x) const
double f[11][100]
double getParameterError(uint32_t i)
double getParameterValue(uint32_t i)
const double * getParameterErrors()
int32_t fit(int32_t verbosity=-1)
void setMaxIterations(uint32_t n)