Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:27:27

0001 #ifndef __UnbinnedLikelihoodFit_h_
0002 #define __UnbinnedLikelihoodFit_h_
0003 #include <TObject.h>
0004 #include <TF1.h>
0005 #include <TVirtualFitter.h>
0006 #include <cstdint>
0007 
0008 // a class to perform a likelihood fit
0009 // Author: Christophe Delaere
0010 
0011 /* Example of a Landau fit:
0012  * ------------------------
0013  * UnbinnedLikelihoodFit myfit;
0014  * double x[4] = {89,110,70,80};
0015  * myfit.setData(4,x);
0016  * TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255);
0017  * myfunction->SetParameters(100,10);
0018  * myfit.setFunction(myfunction);
0019  * myfit.fit();
0020  * myfit.getFunction()->Print();
0021  * double MPV = myfit.getFunction()->GetParameter(0);
0022  * double MPVerror = myfit.getFunction()->GetParError(0);
0023  */
0024 class UnbinnedLikelihoodFit : public TObject {
0025 public:
0026   // Constructor and destructor
0027   UnbinnedLikelihoodFit();
0028   ~UnbinnedLikelihoodFit() override;
0029 
0030   // Set the data for the fit: a set of measurements
0031   void setData(uint32_t n, double* x);
0032 
0033   // Set the fit function
0034   void setFunction(TF1* f);
0035 
0036   // Set the fit options
0037   void setTolerance(double tol) { tolerance_ = tol; }
0038   void setMaxIterations(uint32_t n) { maxIterations_ = n; }
0039 
0040   // Fit
0041   int32_t fit(int32_t verbosity = -1);
0042   int32_t fit(int32_t n, double* x, int32_t verbosity = -1) {
0043     setData(n, x);
0044     return fit(verbosity);
0045   }
0046 
0047   // Results a retrieved via the TF1
0048   TF1* getFunction() const { return function_; }
0049   double getParameterValue(uint32_t i) { return function_ ? function_->GetParameter(i) : 0; }
0050   double getParameterError(uint32_t i) { return function_ ? function_->GetParError(i) : 0; }
0051   double* getParameterValues() { return function_ ? function_->GetParameters() : nullptr; }
0052   const double* getParameterErrors() { return function_ ? function_->GetParErrors() : nullptr; }
0053 
0054 private:
0055   // input data
0056   uint32_t datasize_;
0057   double* x_;
0058   // the function
0059   TF1* function_;
0060   uint32_t nparameters_;
0061   // arguments for Minuit methods
0062   double arglist_[10];
0063   uint32_t maxIterations_;
0064   double tolerance_;
0065   // the minimizer (minuit)
0066   TVirtualFitter* min;
0067 
0068 private:
0069   // LL function
0070   double logL(const double* x) const;
0071   // The function to minimize
0072   friend void UnbinnedLL(Int_t& npar, Double_t* gin, Double_t& val, Double_t* par, Int_t iflag);
0073 };
0074 
0075 #endif