Logo ROOT   6.08/07
Reference Guide
Chi2FCN.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Tue Sep 5 09:13:32 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class Chi2FCN
12 
13 #ifndef ROOT_Fit_Chi2FCN
14 #define ROOT_Fit_Chi2FCN
15 
16 #ifndef ROOT_Fit_BasicFCN
17 #include "Fit/BasicFCN.h"
18 #endif
19 
20 #ifndef ROOT_Math_IParamFunction
21 #include "Math/IParamFunction.h"
22 #endif
23 
24 #include "Math/IFunction.h"
25 #include "Math/IFunctionfwd.h"
26 
27 #ifndef ROOT_Fit_BinData
28 #include "Fit/BinData.h"
29 #endif
30 
31 
32 #ifndef ROOT_Fit_FitUtil
33 #include "Fit/FitUtil.h"
34 #endif
35 
36 //#define ROOT_FIT_PARALLEL
37 
38 #ifdef ROOT_FIT_PARALLEL
39 #ifndef ROOT_Fit_FitUtilParallel
40 #include "Fit/FitUtilParallel.h"
41 #endif
42 #endif
43 
44 #include <memory>
45 
46 /**
47 @defgroup FitMethodFunc Fit Method Classes
48 
49 Classes describing Fit Method functions
50 @ingroup Fit
51 */
52 
53 
54 namespace ROOT {
55 
56 
57  namespace Fit {
58 
59 
60 
61 //___________________________________________________________________________________
62 /**
63  Chi2FCN class for binnned fits using the least square methods
64 
65  @ingroup FitMethodFunc
66 */
67 template<class FunType>
68 class Chi2FCN : public BasicFCN<FunType,BinData> {
69 
70 public:
71 
73 
74  typedef ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
76 
77  //typedef typename ::ROOT::Math::ParamFunctionTrait<FunType>::PFType IModelFunction;
79  typedef typename BaseObjFunction::Type_t Type_t;
80 
81  /**
82  Constructor from data set (binned ) and model function
83  */
84  Chi2FCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func) :
85  BaseFCN( data, func),
86  fNEffPoints(0),
87  fGrad ( std::vector<double> ( func->NPar() ) )
88  { }
89 
90  /**
91  Same Constructor from data set (binned ) and model function but now managed by the user
92  we clone the function but not the data
93  */
94  Chi2FCN ( const BinData & data, const IModelFunction & func) :
95  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
96  fNEffPoints(0),
97  fGrad ( std::vector<double> ( func.NPar() ) )
98  { }
99 
100  /**
101  Destructor (no operations)
102  */
103  virtual ~Chi2FCN () {}
104  /**
105  Copy constructor
106  */
107  Chi2FCN(const Chi2FCN & f) :
108  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
110  fGrad( f.fGrad)
111  { }
112 
113  /**
114  Assignment operator
115  */
116  Chi2FCN & operator = (const Chi2FCN & rhs) {
117  SetData(rhs.DataPtr() );
119  fNEffPoints = rhs.fNEffPoints;
120  fGrad = rhs.fGrad;
121  }
122 
123  /*
124  clone the function
125  */
126  virtual BaseFunction * Clone() const {
127  return new Chi2FCN(*this);
128  }
129 
130 
131 
132  using BaseObjFunction::operator();
133 
134 
135  /// i-th chi-square residual
136  virtual double DataElement(const double * x, unsigned int i, double * g) const {
137  if (i==0) this->UpdateNCalls();
139  }
140 
141  // need to be virtual to be instantiated
142  virtual void Gradient(const double *x, double *g) const {
143  // evaluate the chi2 gradient
145  }
146 
147  /// get type of fit method function
148  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
149 
150 
151 
152 protected:
153 
154  /// set number of fit points (need to be called in const methods, make it const)
155  virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
156 
157 private:
158 
159  /**
160  Evaluation of the function (required by interface)
161  */
162  virtual double DoEval (const double * x) const {
163  this->UpdateNCalls();
164 #ifdef ROOT_FIT_PARALLEL
166 #else
167  if (!BaseFCN::Data().HaveCoordErrors() )
169  else
171 #endif
172  }
173 
174  // for derivatives
175  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
176  Gradient(x, fGrad.data());
177  return fGrad[icoord];
178  }
179 
180 
181  mutable unsigned int fNEffPoints; // number of effective points used in the fit
182 
183  mutable std::vector<double> fGrad; // for derivatives
184 
185 
186 };
187 
188  // define useful typedef's
191 
192 
193  } // end namespace Fit
194 
195 } // end namespace ROOT
196 
197 
198 #endif /* ROOT_Fit_Chi2FCN */
virtual void UpdateNCalls() const
update number of calls
BaseObjFunction::Type_t Type_t
Definition: Chi2FCN.h:79
Type_t
enumeration specyfing the possible fit method types
virtual void SetNFitPoints(unsigned int n) const
set number of fit points (need to be called in const methods, make it const)
Definition: Chi2FCN.h:155
void EvaluateChi2Gradient(const IModelFunction &func, const BinData &data, const double *x, double *grad, unsigned int &nPoints)
evaluate the Chi2 gradient given a model function and the data at the point x.
Definition: FitUtil.cxx:700
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
virtual BaseFunction * Clone() const
Definition: Chi2FCN.h:126
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:80
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:94
BasicFCN< FunType, BinData > BaseFCN
Definition: Chi2FCN.h:72
virtual void Gradient(const double *x, double *g) const
Definition: Chi2FCN.h:142
virtual ~Chi2FCN()
Destructor (no operations)
Definition: Chi2FCN.h:103
Chi2FCN & operator=(const Chi2FCN &rhs)
Assignment operator.
Definition: Chi2FCN.h:116
STL namespace.
::ROOT::Math::IParamMultiFunction IModelFunction
Definition: Chi2FCN.h:78
Double_t x[n]
Definition: legend1.C:17
BaseObjFunction::BaseFunction BaseFunction
Definition: Chi2FCN.h:75
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Definition: Chi2FCN.h:162
Chi2FCN(const Chi2FCN &f)
Copy constructor.
Definition: Chi2FCN.h:107
virtual const BinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:74
::ROOT::Math::BasicFitMethodFunction< FunType > BaseObjFunction
Definition: Chi2FCN.h:74
unsigned int fNEffPoints
Definition: Chi2FCN.h:181
Chi2FCN class for binnned fits using the least square methods.
Definition: Chi2FCN.h:68
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:91
double EvaluateChi2Effective(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
evaluate the effective Chi2 given a model function and the data at the point x.
Definition: FitUtil.cxx:474
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Definition: Chi2FCN.h:148
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:44
double EvaluateChi2Residual(const IModelFunction &func, const BinData &data, const double *x, unsigned int ipoint, double *g=0)
Parallel evaluate the Chi2 given a model function and the data at the point x.
Definition: FitUtil.cxx:595
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:83
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
double EvaluateChi2(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
Chi2 Functions.
Definition: FitUtil.cxx:338
Chi2FCN< ROOT::Math::IMultiGenFunction > Chi2Function
Definition: Chi2FCN.h:189
double f(double x)
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
Chi2FCN< ROOT::Math::IMultiGradFunction > Chi2GradFunction
Definition: Chi2FCN.h:190
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual double DoDerivative(const double *x, unsigned int icoord) const
Definition: Chi2FCN.h:175
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th chi-square residual
Definition: Chi2FCN.h:136
IParametricFunctionMultiDim IParamMultiFunction
std::vector< double > fGrad
Definition: Chi2FCN.h:183
Chi2FCN(const BinData &data, const IModelFunction &func)
Same Constructor from data set (binned ) and model function but now managed by the user we clone the ...
Definition: Chi2FCN.h:94
const int NPar
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:77
const Int_t n
Definition: legend1.C:16
Chi2FCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func)
Constructor from data set (binned ) and model function.
Definition: Chi2FCN.h:84