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