00001 // @(#)root/mathcore:$Id: Fitter.h 36764 2010-11-19 10:02:00Z moneta $ 00002 // Author: L. Moneta Wed Aug 30 11:05:19 2006 00003 00004 /********************************************************************** 00005 * * 00006 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT * 00007 * * 00008 * * 00009 **********************************************************************/ 00010 00011 // Header file for class Fitter 00012 00013 #ifndef ROOT_Fit_Fitter 00014 #define ROOT_Fit_Fitter 00015 00016 /** 00017 @defgroup Fit Fitting and Parameter Estimation 00018 00019 Classes used for fitting (regression analysis) and estimation of parameter values given a data sample. 00020 */ 00021 00022 #ifndef ROOT_Fit_DataVectorfwd 00023 #include "Fit/DataVectorfwd.h" 00024 #endif 00025 00026 #ifndef ROOT_Fit_FitConfig 00027 #include "Fit/FitConfig.h" 00028 #endif 00029 00030 #ifndef ROOT_Fit_FitResult 00031 #include "Fit/FitResult.h" 00032 #endif 00033 00034 #ifndef ROOT_Math_IParamFunctionfwd 00035 #include "Math/IParamFunctionfwd.h" 00036 #endif 00037 00038 #include <memory> 00039 00040 00041 namespace ROOT { 00042 00043 00044 namespace Math { 00045 class Minimizer; 00046 } 00047 00048 /** 00049 Namespace for the fitting classes 00050 @ingroup Fit 00051 */ 00052 00053 namespace Fit { 00054 00055 /** 00056 @defgroup FitMain User Fitting classes 00057 00058 Main Classes used for fitting a given data set 00059 @ingroup Fit 00060 */ 00061 00062 //___________________________________________________________________________________ 00063 /** 00064 Fitter class, entry point for performing all type of fits. 00065 Fits are performed using the generic ROOT::Fit::Fitter::Fit method. 00066 The inputs are the data points and a model function (using a ROOT::Math::IParamFunction) 00067 The result of the fit is returned and kept internally in the ROOT::Fit::FitResult class. 00068 The configuration of the fit (parameters, options, etc...) are specified in the 00069 ROOT::Math::FitConfig class. 00070 00071 @ingroup FitMain 00072 */ 00073 class Fitter { 00074 00075 public: 00076 00077 typedef ROOT::Math::IParamMultiFunction IModelFunction; 00078 typedef ROOT::Math::IParamMultiGradFunction IGradModelFunction; 00079 typedef ROOT::Math::IParamFunction IModel1DFunction; 00080 typedef ROOT::Math::IParamGradFunction IGradModel1DFunction; 00081 00082 typedef ROOT::Math::IMultiGenFunction BaseFunc; 00083 typedef ROOT::Math::IMultiGradFunction BaseGradFunc; 00084 00085 00086 /** 00087 Default constructor 00088 */ 00089 Fitter (); 00090 00091 /** 00092 Destructor 00093 */ 00094 ~Fitter (); 00095 00096 private: 00097 00098 /** 00099 Copy constructor (disabled, class is not copyable) 00100 */ 00101 Fitter(const Fitter &); 00102 00103 /** 00104 Assignment operator (disabled, class is not copyable) 00105 */ 00106 Fitter & operator = (const Fitter & rhs); 00107 00108 00109 public: 00110 00111 /** 00112 fit a data set using any generic model function 00113 Pre-requisite on the function: 00114 */ 00115 template < class Data , class Function> 00116 bool Fit( const Data & data, const Function & func) { 00117 SetFunction(func); 00118 return Fit(data); 00119 } 00120 00121 /** 00122 fit a binned data set (default method: use chi2) 00123 To be implemented option to do likelihood bin fit 00124 */ 00125 bool Fit(const BinData & data) { 00126 return DoLeastSquareFit(data); 00127 } 00128 /** 00129 fit an binned data set using loglikelihood method 00130 */ 00131 bool Fit(const UnBinData & data) { 00132 return DoLikelihoodFit(data); 00133 } 00134 00135 /** 00136 Likelihood fit 00137 */ 00138 template <class Data> 00139 bool LikelihoodFit(const Data & data) { 00140 return DoLikelihoodFit(data); 00141 } 00142 00143 /** 00144 fit a data set using any generic model function 00145 Pre-requisite on the function: 00146 */ 00147 template < class Data , class Function> 00148 bool LikelihoodFit( const Data & data, const Function & func) { 00149 SetFunction(func); 00150 return DoLikelihoodFit(data); 00151 } 00152 00153 /** 00154 Fit using the a generic FCN function as a C++ callable object implementing 00155 double () (const double *) 00156 Note that the function dimension (i.e. the number of parameter) is needed in this case 00157 For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..) 00158 */ 00159 template <class Function> 00160 bool FitFCN(unsigned int npar, Function & fcn, const double * params = 0, unsigned int dataSize = 0, bool chi2fit = false); 00161 00162 /** 00163 Fit using the given FCN function represented by a multi-dimensional function interface 00164 (ROOT::Math::IMultiGenFunction). 00165 Give optionally the initial arameter values, data size to have the fit Ndf correctly 00166 set in the FitResult and flag specifying if it is a chi2 fit. 00167 Note that if the parameters values are not given (params=0) the 00168 current parameter settings are used. The parameter settings can be created before 00169 by using the FitConfig::SetParamsSetting. If they have not been created they are created 00170 automatically when the params pointer is not zero. 00171 Note that passing a params != 0 will set the parameter settings to the new value AND also the 00172 step sizes to some pre-defined value (stepsize = 0.3 * abs(parameter_value) ) 00173 */ 00174 bool FitFCN(const ROOT::Math::IMultiGenFunction & fcn, const double * params = 0, unsigned int dataSize = 0, bool chi2fit = false); 00175 00176 /** 00177 Fit using the given FCN function representing a multi-dimensional gradient function 00178 interface (ROOT::Math::IMultiGradFunction). In this case the minimizer will use the 00179 gradient information provided by the function. 00180 For the options same consideration as in the previous method 00181 */ 00182 bool FitFCN(const ROOT::Math::IMultiGradFunction & fcn, const double * params = 0, unsigned int dataSize = 0, bool chi2fit = false); 00183 00184 00185 /** 00186 fit using user provided FCN with Minuit-like interface 00187 If npar = 0 it is assumed that the parameters are specified in the parameter settings created before 00188 For the options same consideration as in the previous method 00189 */ 00190 typedef void (* MinuitFCN_t )(int &npar, double *gin, double &f, double *u, int flag); 00191 bool FitFCN( MinuitFCN_t fcn, int npar = 0, const double * params = 0, unsigned int dataSize = 0, bool chi2fit = false); 00192 00193 /** 00194 do a linear fit on a set of bin-data 00195 */ 00196 bool LinearFit(const BinData & data) { return DoLinearFit(data); } 00197 00198 /** 00199 Set the fitted function (model function) from a parametric function interface 00200 */ 00201 void SetFunction(const IModelFunction & func); 00202 /** 00203 Set the fitted function from a parametric 1D function interface 00204 */ 00205 void SetFunction(const IModel1DFunction & func); 00206 00207 /** 00208 Set the fitted function (model function) from a parametric gradient function interface 00209 */ 00210 void SetFunction(const IGradModelFunction & func); 00211 /** 00212 Set the fitted function from 1D gradient parametric function interface 00213 */ 00214 void SetFunction(const IGradModel1DFunction & func); 00215 00216 00217 /** 00218 get fit result 00219 */ 00220 const FitResult & Result() const { 00221 assert( fResult.get() ); 00222 return *fResult; 00223 } 00224 00225 /** 00226 perform an error analysis on the result using the Hessian 00227 Errors are obtaied from the inverse of the Hessian matrix 00228 To be called only after fitting and when a minimizer supporting the Hessian calculations is used 00229 otherwise an error (false) is returned. 00230 A new FitResult with the Hessian result will be produced 00231 */ 00232 bool CalculateHessErrors(); 00233 00234 /** 00235 perform an error analysis on the result using MINOS 00236 To be called only after fitting and when a minimizer supporting MINOS is used 00237 otherwise an error (false) is returned. 00238 The result will be appended in the fit result class 00239 Optionally a vector of parameter indeces can be passed for selecting 00240 the parameters to analyse using FitConfig::SetMinosErrors 00241 */ 00242 bool CalculateMinosErrors(); 00243 00244 /** 00245 access to the fit configuration (const method) 00246 */ 00247 const FitConfig & Config() const { return fConfig; } 00248 00249 /** 00250 access to the configuration (non const method) 00251 */ 00252 FitConfig & Config() { return fConfig; } 00253 00254 /** 00255 query if fit is binned. In cse of false teh fit can be unbinned 00256 or is not defined (like in case of fitting through a ::FitFCN) 00257 */ 00258 bool IsBinFit() const { return fBinFit; } 00259 00260 /** 00261 return pointer to last used minimizer 00262 (is NULL in case fit is not yet done) 00263 This pointer will be valid as far as the data, the objective function 00264 and the fitter class have not been deleted. 00265 To be used only after fitting. 00266 The pointer should not be stored and will be invalided after performing a new fitting. 00267 In this case a new instance of ROOT::Math::Minimizer will be re-created and can be 00268 obtained calling again GetMinimizer() 00269 */ 00270 ROOT::Math::Minimizer * GetMinimizer() const { return fMinimizer.get(); } 00271 00272 /** 00273 return pointer to last used objective function 00274 (is NULL in case fit is not yet done) 00275 This pointer will be valid as far as the data and the fitter class 00276 have not been deleted. To be used after the fitting. 00277 The pointer should not be stored and will be invalided after performing a new fitting. 00278 In this case a new instance of the function pointer will be re-created and can be 00279 obtained calling again GetFCN() 00280 */ 00281 ROOT::Math::IMultiGenFunction * GetFCN() const { return fObjFunction.get(); } 00282 00283 00284 protected: 00285 00286 /// least square fit 00287 bool DoLeastSquareFit(const BinData & data); 00288 /// binned likelihood fit 00289 bool DoLikelihoodFit(const BinData & data); 00290 /// un-binned likelihood fit 00291 bool DoLikelihoodFit(const UnBinData & data); 00292 /// linear least square fit 00293 bool DoLinearFit(const BinData & data); 00294 00295 /// do minimization 00296 template<class ObjFunc> 00297 bool DoMinimization(const ObjFunc & f, unsigned int dataSize, const ROOT::Math::IMultiGenFunction * chifunc = 0); 00298 00299 private: 00300 00301 bool fUseGradient; // flag to indicate if using gradient or not 00302 00303 bool fBinFit; // flag to indicate if fit is binned (in case of false the fit is unbinned or undefined) 00304 00305 IModelFunction * fFunc; // copy of the fitted function containing on output the fit result (managed by FitResult) 00306 00307 FitConfig fConfig; // fitter configuration (options and parameter settings) 00308 00309 std::auto_ptr<ROOT::Fit::FitResult> fResult; //! pointer to the object containing the result of the fit 00310 00311 std::auto_ptr<ROOT::Math::Minimizer> fMinimizer; //! pointer to used minimizer 00312 00313 std::auto_ptr<ROOT::Math::IMultiGenFunction> fObjFunction; //! pointer to used objective function 00314 00315 }; 00316 00317 } // end namespace Fit 00318 00319 } // end namespace ROOT 00320 00321 // implementation of inline methods 00322 00323 00324 #ifndef __CINT__ 00325 00326 00327 #ifndef ROOT_Math_WrappedFunction 00328 #include "Math/WrappedFunction.h" 00329 #endif 00330 00331 template<class Function> 00332 bool ROOT::Fit::Fitter::FitFCN(unsigned int npar, Function & f, const double * par, unsigned int datasize,bool chi2fit) { 00333 ROOT::Math::WrappedMultiFunction<Function &> wf(f,npar); 00334 return FitFCN(wf,par,datasize,chi2fit); 00335 } 00336 00337 #endif // endif __CINT__ 00338 00339 #endif /* ROOT_Fit_Fitter */