TFumiliFCN.h

Go to the documentation of this file.
00001 // @(#)root/minuit2:$Id: TFumiliFCN.h 20880 2007-11-19 11:23:41Z rdm $
00002 // Author: L. Moneta    10/2005  
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2005 ROOT Foundation,  CERN/PH-SFT                   *
00007  *                                                                    *
00008  **********************************************************************/
00009 
00010 #ifndef ROOT_TFumiliChi2FCN_H_
00011 #define ROOT_TFumiliChi2FCN_H_
00012 
00013 #include "Minuit2/FumiliFCNBase.h"
00014 
00015 // temporary - should use interface
00016 class TF1;
00017 class TVirtualFitter;
00018 
00019 class TChi2FitData;
00020 
00021 /** 
00022     Base Class for implementing Fumili interface
00023 */
00024 
00025 
00026 class TFumiliFCN : public ROOT::Minuit2::FumiliFCNBase {
00027 
00028 public: 
00029 
00030 //   // use a param function instead of TF1
00031   typedef TF1 ModelFunction;
00032   typedef TChi2FitData FumiliFitData; 
00033   
00034 
00035 
00036   /**
00037      construct passing fitter which has ROOT data object and  model function. 
00038      use a strategy for calculating derivatives 
00039      strategy = 1   default 2 point formula . Fast but not very precise
00040      strategy = 2   5 point formula
00041    */
00042   TFumiliFCN( const TVirtualFitter & fitter, double up = 1., int strategy = 1, bool sipEmptyBins = true);  
00043 
00044 
00045   /**
00046      this class manages the fit data class. Delete it at the end
00047    */
00048   virtual ~TFumiliFCN();
00049 
00050  
00051   /**
00052      evaluate objective function 
00053   */
00054   virtual double operator()(const std::vector<double>&) const = 0; 
00055 
00056 
00057   /**
00058      evaluate gradient and function elements needed by fumili 
00059    */
00060   void EvaluateAll( const std::vector<double> & );  
00061 
00062   /**
00063      return error definition for chi2 = 1
00064   */
00065   double Up() const { return fUp; }
00066 
00067   void SetErrorDef (double up) { fUp = up; }
00068 
00069 
00070   /**
00071      initialize method to set right number of parameters. 
00072      It is known only when starting  the fit
00073    */
00074 
00075   void Initialize(unsigned int npar);
00076 
00077 
00078 protected: 
00079   
00080   
00081   void Calculate_gradient_and_hessian(const std::vector<double> & p);
00082 
00083   void Calculate_numerical_gradient( const std::vector<double> & x, double f0);
00084 
00085  void Calculate_numerical_gradient_of_integral( const std::vector<double> & x1,  const std::vector<double> & x2, double f0);
00086 
00087   // calculate i-th element contribution to objective function
00088   // abstract - need to be re-implemented by the derived classes§
00089   virtual void Calculate_element(int i, const FumiliFitData & points, double fval, double & chi2, std::vector<double> & grad,   std::vector<double> & hess ) = 0;
00090 
00091 protected: 
00092 
00093   double fUp; 
00094   FumiliFitData * fData; 
00095   ModelFunction * fFunc; 
00096   
00097 //   std::vector<double> fGradient;
00098 //   std::vector<double> fHessian;
00099 
00100   //prameter cache
00101   std::vector<double>  fParamCache; 
00102   std::vector<double>  fFunctionGradient; 
00103 
00104   int fStrategy;
00105 };
00106 
00107 
00108 class TFumiliChi2FCN : public TFumiliFCN {
00109 
00110   public: 
00111   /**
00112      construct passing fitter which has ROOT data object and  model function. 
00113      use a strategy for calculating derivatives 
00114      strategy = 1   default 2 point formula . Fast but not very precise
00115      strategy = 2   5 point formula
00116    */
00117   TFumiliChi2FCN( const TVirtualFitter & fitter, int strategy = 1) : 
00118     TFumiliFCN(fitter, 1.0, strategy, true) {}
00119 
00120   virtual ~TFumiliChi2FCN() {}
00121 
00122 
00123   /**
00124      evaluate objective function 
00125   */
00126   double operator()(const std::vector<double>&) const; 
00127 
00128 protected: 
00129 
00130   virtual void Calculate_element(int i, const TChi2FitData & points, double fval, double & chi2, std::vector<double> & grad, std::vector<double> & hess );
00131   
00132 };
00133 
00134 
00135 /** 
00136     Fumili interface for binned (Poisson) likelihood functions
00137 */ 
00138 
00139 class TFumiliBinLikelihoodFCN : public TFumiliFCN {
00140 
00141   public: 
00142   /**
00143      construct passing fitter which has ROOT data object and  model function. 
00144      use a strategy for calculating derivatives 
00145      strategy = 1   default 2 point formula . Fast but not very precise
00146      strategy = 2   5 point formula
00147    */
00148   TFumiliBinLikelihoodFCN( const TVirtualFitter & fitter, int strategy = 1)  : 
00149     TFumiliFCN(fitter, 1.0, strategy, false) {}
00150 
00151 
00152   virtual ~TFumiliBinLikelihoodFCN() {}
00153 
00154   /**
00155      evaluate objective function 
00156   */
00157   double operator()(const std::vector<double>&) const; 
00158 
00159   /**
00160      evaluate chi2 equivalent on the data set
00161   */
00162   double Chi2 ( const std::vector<double>&) const; 
00163 
00164 protected: 
00165 
00166   virtual void Calculate_element(int i, const TChi2FitData & points, double fval, double & chi2, std::vector<double> & grad,   std::vector<double> & hess );
00167 
00168 };
00169 
00170 
00171 /** 
00172     Fumili interface for Unbinned  likelihood functions
00173 */ 
00174 class TFumiliUnbinLikelihoodFCN : public TFumiliFCN {
00175 
00176   public: 
00177   /**
00178      construct passing fitter which has ROOT data object and  model function. 
00179      use a strategy for calculating derivatives 
00180      strategy = 1   default 2 point formula . Fast but not very precise
00181      strategy = 2   5 point formula
00182    */
00183   TFumiliUnbinLikelihoodFCN( const TVirtualFitter & fitter, int strategy = 1) : 
00184     TFumiliFCN(fitter, 0.5, strategy, false) {}
00185 
00186   virtual ~TFumiliUnbinLikelihoodFCN() {}
00187 
00188   /**
00189      evaluate objective function 
00190   */
00191   double operator()(const std::vector<double>&) const; 
00192 
00193 protected: 
00194 
00195   virtual void Calculate_element(int i, const TChi2FitData & points, double fval, double & chi2, std::vector<double> & grad,   std::vector<double> & hess );
00196 
00197 };
00198 
00199 #endif

Generated on Tue Jul 5 14:26:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1