00001 #ifndef __UnbinnedLikelihoodFit_h_ 00002 #define __UnbinnedLikelihoodFit_h_ 00003 #include <TObject.h> 00004 #include <TF1.h> 00005 #include <TVirtualFitter.h> 00006 #include <stdint.h> 00007 00008 // a class to perform a likelihood fit 00009 // Author: Christophe Delaere 00010 00011 /* Example of a Landau fit: 00012 * ------------------------ 00013 * UnbinnedLikelihoodFit myfit; 00014 * double x[4] = {89,110,70,80}; 00015 * myfit.setData(4,x); 00016 * TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255); 00017 * myfunction->SetParameters(100,10); 00018 * myfit.setFunction(myfunction); 00019 * myfit.fit(); 00020 * myfit.getFunction()->Print(); 00021 * double MPV = myfit.getFunction()->GetParameter(0); 00022 * double MPVerror = myfit.getFunction()->GetParError(0); 00023 */ 00024 class UnbinnedLikelihoodFit : public TObject 00025 { 00026 public: 00027 // Constructor and destructor 00028 UnbinnedLikelihoodFit(); 00029 ~UnbinnedLikelihoodFit(); 00030 00031 // Set the data for the fit: a set of measurements 00032 void setData(uint32_t n, double* x); 00033 00034 // Set the fit function 00035 void setFunction(TF1* f); 00036 00037 // Set the fit options 00038 void setTolerance( double tol) { tolerance_ = tol; } 00039 void setMaxIterations( uint32_t n ) { maxIterations_ = n; } 00040 00041 // Fit 00042 int32_t fit(int32_t verbosity = -1); 00043 int32_t fit(int32_t n, double* x, int32_t verbosity = -1) { setData(n,x); return fit(verbosity); } 00044 00045 // Results a retrieved via the TF1 00046 TF1* getFunction() const { return function_; } 00047 double getParameterValue(uint32_t i) { return function_ ? function_->GetParameter(i) : 0; } 00048 double getParameterError(uint32_t i) { return function_ ? function_->GetParError(i) : 0; } 00049 double* getParameterValues() { return function_ ? function_->GetParameters() : NULL; } 00050 double* getParameterErrors() { return function_ ? function_->GetParErrors() : NULL; } 00051 00052 private: 00053 // input data 00054 uint32_t datasize_; 00055 double* x_; 00056 // the function 00057 TF1* function_; 00058 uint32_t nparameters_; 00059 // arguments for Minuit methods 00060 double arglist_[10]; 00061 uint32_t maxIterations_; 00062 double tolerance_; 00063 // the minimizer (minuit) 00064 TVirtualFitter* min; 00065 00066 private: 00067 // LL function 00068 double logL(const double* x) const; 00069 // The function to minimize 00070 friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag); 00071 00072 }; 00073 00074 #endif 00075