ROOT  6.06/09
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 
25 #ifndef ROOT_Fit_BinData
26 #include "Fit/BinData.h"
27 #endif
28 
29 
30 #ifndef ROOT_Fit_FitUtil
31 #include "Fit/FitUtil.h"
32 #endif
33 
34 //#define ROOT_FIT_PARALLEL
35 
36 #ifdef ROOT_FIT_PARALLEL
37 #ifndef ROOT_Fit_FitUtilParallel
38 #include "Fit/FitUtilParallel.h"
39 #endif
40 #endif
41 
42 #include <memory>
43 
44 /**
45 @defgroup FitMethodFunc Fit Method Classes
46 
47 Classes describing Fit Method functions
48 @ingroup Fit
49 */
50 
51 
52 namespace ROOT {
53 
54 
55  namespace Fit {
56 
57 
58 
59 //___________________________________________________________________________________
60 /**
61  Chi2FCN class for binnned fits using the least square methods
62 
63  @ingroup FitMethodFunc
64 */
65 template<class FunType>
66 class Chi2FCN : public BasicFCN<FunType,BinData> {
67 
68 public:
69 
71 
72  typedef ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
74 
75  //typedef typename ::ROOT::Math::ParamFunctionTrait<FunType>::PFType IModelFunction;
77  typedef typename BaseObjFunction::Type_t Type_t;
78 
79  /**
80  Constructor from data set (binned ) and model function
81  */
82  Chi2FCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func) :
83  BaseFCN( data, func),
84  fNEffPoints(0),
85  fGrad ( std::vector<double> ( func->NPar() ) )
86  { }
87 
88  /**
89  Same Constructor from data set (binned ) and model function but now managed by the user
90  we clone the function but not the data
91  */
92  Chi2FCN ( const BinData & data, const IModelFunction & func) :
93  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
94  fNEffPoints(0),
95  fGrad ( std::vector<double> ( func.NPar() ) )
96  { }
97 
98  /**
99  Destructor (no operations)
100  */
101  virtual ~Chi2FCN () {}
102  /**
103  Copy constructor
104  */
105  Chi2FCN(const Chi2FCN & f) :
106  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
108  fGrad( f.fGrad)
109  { }
110 
111  /**
112  Assignment operator
113  */
114  Chi2FCN & operator = (const Chi2FCN & rhs) {
115  SetData(rhs.DataPtr() );
117  fNEffPoints = rhs.fNEffPoints;
118  fGrad = rhs.fGrad;
119  }
120 
121  /*
122  clone the function
123  */
124  virtual BaseFunction * Clone() const {
125  return new Chi2FCN(*this);
126  }
127 
128 
129 
130  using BaseObjFunction::operator();
131 
132 
133  /// i-th chi-square residual
134  virtual double DataElement(const double * x, unsigned int i, double * g) const {
135  if (i==0) this->UpdateNCalls();
137  }
138 
139  // need to be virtual to be instantiated
140  virtual void Gradient(const double *x, double *g) const {
141  // evaluate the chi2 gradient
143  }
144 
145  /// get type of fit method function
146  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
147 
148 
149 
150 protected:
151 
152  /// set number of fit points (need to be called in const methods, make it const)
153  virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
154 
155 private:
156 
157  /**
158  Evaluation of the function (required by interface)
159  */
160  virtual double DoEval (const double * x) const {
161  this->UpdateNCalls();
162 #ifdef ROOT_FIT_PARALLEL
164 #else
165  if (!BaseFCN::Data().HaveCoordErrors() )
167  else
169 #endif
170  }
171 
172  // for derivatives
173  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
174  Gradient(x, fGrad.data());
175  return fGrad[icoord];
176  }
177 
178 
179  mutable unsigned int fNEffPoints; // number of effective points used in the fit
180 
181  mutable std::vector<double> fGrad; // for derivatives
182 
183 
184 };
185 
186  // define useful typedef's
189 
190 
191  } // end namespace Fit
192 
193 } // end namespace ROOT
194 
195 
196 #endif /* ROOT_Fit_Chi2FCN */
BaseObjFunction::Type_t Type_t
Definition: Chi2FCN.h:77
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:698
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:79
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:93
BasicFCN< FunType, BinData > BaseFCN
Definition: Chi2FCN.h:70
virtual ~Chi2FCN()
Destructor (no operations)
Definition: Chi2FCN.h:101
Chi2FCN & operator=(const Chi2FCN &rhs)
Assignment operator.
Definition: Chi2FCN.h:114
virtual const BinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:73
STL namespace.
::ROOT::Math::IParamMultiFunction IModelFunction
Definition: Chi2FCN.h:76
Double_t x[n]
Definition: legend1.C:17
BaseObjFunction::BaseFunction BaseFunction
Definition: Chi2FCN.h:73
Chi2FCN(const Chi2FCN &f)
Copy constructor.
Definition: Chi2FCN.h:105
::ROOT::Math::BasicFitMethodFunction< FunType > BaseObjFunction
Definition: Chi2FCN.h:72
unsigned int fNEffPoints
Definition: Chi2FCN.h:179
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th chi-square residual
Definition: Chi2FCN.h:134
Chi2FCN class for binnned fits using the least square methods.
Definition: Chi2FCN.h:66
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Definition: Chi2FCN.h:160
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:90
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:472
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:43
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:593
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:153
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
virtual double DoDerivative(const double *x, unsigned int icoord) const
Definition: Chi2FCN.h:173
double EvaluateChi2(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
Chi2 Functions.
Definition: FitUtil.cxx:336
Chi2FCN< ROOT::Math::IMultiGenFunction > Chi2Function
Definition: Chi2FCN.h:187
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:132
Chi2FCN< ROOT::Math::IMultiGradFunction > Chi2GradFunction
Definition: Chi2FCN.h:188
virtual void Gradient(const double *x, double *g) const
Definition: Chi2FCN.h:140
double func(double *x, double *p)
Definition: stressTF1.cxx:213
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:82
virtual BaseFunction * Clone() const
Definition: Chi2FCN.h:124
IParametricFunctionMultiDim IParamMultiFunction
std::vector< double > fGrad
Definition: Chi2FCN.h:181
virtual void UpdateNCalls() const
update number of calls
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:92
const int NPar
Type_t
enumeration specyfing the possible fit method types
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Definition: Chi2FCN.h:146
const Int_t n
Definition: legend1.C:16
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:76
Chi2FCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func)
Constructor from data set (binned ) and model function.
Definition: Chi2FCN.h:82