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 {
26  public:
27  // Constructor and destructor
29  ~UnbinnedLikelihoodFit() override;
30 
31  // Set the data for the fit: a set of measurements
32  void setData(uint32_t n, double* x);
33 
34  // Set the fit function
35  void setFunction(TF1* f);
36 
37  // Set the fit options
38  void setTolerance( double tol) { tolerance_ = tol; }
39  void setMaxIterations( uint32_t n ) { maxIterations_ = n; }
40 
41  // Fit
42  int32_t fit(int32_t verbosity = -1);
43  int32_t fit(int32_t n, double* x, int32_t verbosity = -1) { setData(n,x); return fit(verbosity); }
44 
45  // Results a retrieved via the TF1
46  TF1* getFunction() const { return function_; }
47  double getParameterValue(uint32_t i) { return function_ ? function_->GetParameter(i) : 0; }
48  double getParameterError(uint32_t i) { return function_ ? function_->GetParError(i) : 0; }
49  double* getParameterValues() { return function_ ? function_->GetParameters() : nullptr; }
50  const double* getParameterErrors() { return function_ ? function_->GetParErrors() : nullptr; }
51 
52  private:
53  // input data
54  uint32_t datasize_;
55  double* x_;
56  // the function
57  TF1* function_;
58  uint32_t nparameters_;
59  // arguments for Minuit methods
60  double arglist_[10];
61  uint32_t maxIterations_;
62  double tolerance_;
63  // the minimizer (minuit)
64  TVirtualFitter* min;
65 
66  private:
67  // LL function
68  double logL(const double* x) const;
69  // The function to minimize
70  friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag);
71 
72 };
73 
74 #endif
75 
int32_t fit(int32_t n, double *x, int32_t verbosity=-1)
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)
void setTolerance(double tol)
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)