Logo ROOT  
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 #include "Fit/FitUtil.h"
26 
27 #include <memory>
28 #include <vector>
29 
30 /**
31 @defgroup FitMethodFunc Fit Method Classes
32 
33 Classes describing Fit Method functions
34 @ingroup Fit
35 */
36 
37 namespace ROOT {
38 
39 
40  namespace Fit {
41 
42 //___________________________________________________________________________________
43 /**
44  Chi2FCN class for binnned fits using the least square methods
45 
46  @ingroup FitMethodFunc
47 */
48 template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
49 class Chi2FCN : public BasicFCN<DerivFunType, ModelFunType, BinData> {
50 
51 public:
52 
53  typedef typename ModelFunType::BackendType T;
55 
56  typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
58 
59  //typedef typename ::ROOT::Math::ParamFunctionTrait<FunType>::PFType IModelFunction;
60  typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
61  typedef typename BaseObjFunction::Type_t Type_t;
62 
63  /**
64  Constructor from data set (binned ) and model function
65  */
66  Chi2FCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, const ::ROOT::Fit::ExecutionPolicy &executionPolicy = ::ROOT::Fit::ExecutionPolicy::kSerial) :
67  BaseFCN( data, func),
68  fNEffPoints(0),
69  fGrad ( std::vector<double> ( func->NPar() ) ),
70  fExecutionPolicy(executionPolicy)
71  { }
72 
73  /**
74  Same Constructor from data set (binned ) and model function but now managed by the user
75  we clone the function but not the data
76  */
78  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
79  fNEffPoints(0),
80  fGrad ( std::vector<double> ( func.NPar() ) ),
81  fExecutionPolicy(executionPolicy)
82  { }
83 
84  /**
85  Destructor (no operations)
86  */
87  virtual ~Chi2FCN () {}
88  /**
89  Copy constructor
90  */
91  Chi2FCN(const Chi2FCN & f) :
94  fGrad( f.fGrad),
96  { }
97 
98  /**
99  Assignment operator
100  */
101  Chi2FCN & operator = (const Chi2FCN & rhs) {
102  SetData(rhs.DataPtr() );
104  fNEffPoints = rhs.fNEffPoints;
105  fGrad = rhs.fGrad;
106  }
107 
108  /*
109  clone the function
110  */
111  virtual BaseFunction * Clone() const {
112  return new Chi2FCN(*this);
113  }
114 
115 
116 
117  using BaseObjFunction::operator();
118 
119 
120  /// i-th chi-square residual
121  virtual double DataElement(const double *x, unsigned int i, double *g) const {
122  if (i==0) this->UpdateNCalls();
124  }
125 
126  // need to be virtual to be instantiated
127  virtual void Gradient(const double *x, double *g) const {
128  // evaluate the chi2 gradient
131  }
132 
133  /// get type of fit method function
134  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
135 
136 
137 protected:
138 
139  /// set number of fit points (need to be called in const methods, make it const)
140  virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
141 
142 private:
143 
144  /**
145  Evaluation of the function (required by interface)
146  */
147  virtual double DoEval (const double * x) const {
148  this->UpdateNCalls();
149  if (BaseFCN::Data().HaveCoordErrors() || BaseFCN::Data().HaveAsymErrors())
151  else
153  }
154 
155  // for derivatives
156  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
157  Gradient(x, fGrad.data());
158  return fGrad[icoord];
159  }
160 
161 
162  mutable unsigned int fNEffPoints; // number of effective points used in the fit
163 
164  mutable std::vector<double> fGrad; // for derivatives
166 
167 };
168 
169  // define useful typedef's
172 
173 
174  } // end namespace Fit
175 
176 } // end namespace ROOT
177 
178 
179 #endif /* ROOT_Fit_Chi2FCN */
n
const Int_t n
Definition: legend1.C:16
ROOT::Fit::BasicFCN::ModelFunction
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:92
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::SetData
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:103
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::ModelFunctionPtr
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:95
ROOT::Math::BasicFitMethodFunction< DerivFunType >::kLeastSquare
@ kLeastSquare
Definition: FitMethodFunction.h:59
HFit::Fit
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:133
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::DataPtr
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:89
ROOT::Fit::Chi2FCN
Chi2FCN class for binnned fits using the least square methods.
Definition: Chi2FCN.h:49
IFunction.h
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Fit::FitUtil::Evaluate::EvalChi2Gradient
static void EvalChi2Gradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::Fit::ExecutionPolicy executionPolicy=::ROOT::Fit::ExecutionPolicy::kSerial, unsigned nChunks=0)
Definition: FitUtil.h:1425
ROOT::Fit::BinData
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:62
ROOT::Fit::Chi2FCN::SetNFitPoints
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:140
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::SetModelFunction
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:106
ROOT::Fit::Chi2Function
Chi2FCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > Chi2Function
Definition: Chi2FCN.h:170
ROOT::Fit::Chi2FCN::fGrad
std::vector< double > fGrad
Definition: Chi2FCN.h:164
ROOT::Fit::FitUtil::Evaluate::EvalChi2
static double EvalChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints, ::ROOT::Fit::ExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition: FitUtil.h:1393
ROOT::Fit::Chi2FCN::~Chi2FCN
virtual ~Chi2FCN()
Destructor (no operations)
Definition: Chi2FCN.h:87
BasicFCN.h
FitUtil.h
IFunctionfwd.h
ROOT::Fit::BasicFCN
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:50
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::BasicFitMethodFunction< DerivFunType >::UpdateNCalls
virtual void UpdateNCalls() const
update number of calls
Definition: FitMethodFunction.h:105
ROOT::Fit::Chi2FCN::DoDerivative
virtual double DoDerivative(const double *x, unsigned int icoord) const
Definition: Chi2FCN.h:156
ROOT::Fit::Chi2FCN::Clone
virtual BaseFunction * Clone() const
Definition: Chi2FCN.h:111
ROOT::Fit::ExecutionPolicy
ExecutionPolicy
Definition: FitExecutionPolicy.h:5
ROOT::Fit::BasicFCN::Data
virtual const DataType & Data() const
access to const reference to the data
Definition: BasicFCN.h:86
ROOT::Fit::Chi2FCN::Type
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Definition: Chi2FCN.h:134
ROOT::Fit::Chi2GradFunction
Chi2FCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > Chi2GradFunction
Definition: Chi2FCN.h:171
ROOT::Math::IParametricFunctionMultiDimTempl
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
Definition: IParamFunction.h:104
ROOT::Fit::Chi2FCN::BaseFunction
BaseObjFunction::BaseFunction BaseFunction
Definition: Chi2FCN.h:57
ROOT::Fit::DummyDeleter
Definition: FitData.h:40
ROOT::Fit::Chi2FCN::Chi2FCN
Chi2FCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func, const ::ROOT::Fit::ExecutionPolicy &executionPolicy=::ROOT::Fit::ExecutionPolicy::kSerial)
Constructor from data set (binned ) and model function.
Definition: Chi2FCN.h:66
ROOT::Fit::Chi2FCN::DoEval
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Definition: Chi2FCN.h:147
ROOT::Math::BasicFitMethodFunction< DerivFunType >::Type_t
Type_t
enumeration specyfing the possible fit method types
Definition: FitMethodFunction.h:59
double
double
Definition: Converters.cxx:921
ROOT::Fit::Chi2FCN::fExecutionPolicy
::ROOT::Fit::ExecutionPolicy fExecutionPolicy
Definition: Chi2FCN.h:165
IParamFunction.h
ROOT::Fit::Chi2FCN::BaseObjFunction
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
Definition: Chi2FCN.h:56
ROOT::Fit::Chi2FCN::Chi2FCN
Chi2FCN(const Chi2FCN &f)
Copy constructor.
Definition: Chi2FCN.h:91
BinData.h
ROOT::Fit::Chi2FCN::Gradient
virtual void Gradient(const double *x, double *g) const
Definition: Chi2FCN.h:127
ROOT::Fit::Chi2FCN::T
ModelFunType::BackendType T
Definition: Chi2FCN.h:53
ROOT::Fit::Chi2FCN::IModelFunction
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
Definition: Chi2FCN.h:60
ROOT::Math::BasicFitMethodFunction< DerivFunType >::BaseFunction
DerivFunType ::BaseFunc BaseFunction
Definition: FitMethodFunction.h:56
ROOT::Fit::Chi2FCN::Type_t
BaseObjFunction::Type_t Type_t
Definition: Chi2FCN.h:61
ROOT::Fit::FitUtil::Evaluate::EvalChi2Residual
static double EvalChi2Residual(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g=0)
Definition: FitUtil.h:1432
ROOT::Fit::ExecutionPolicy::kSerial
@ kSerial
ROOT::Fit::FitUtil::Evaluate::EvalChi2Effective
static double EvalChi2Effective(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int &nPoints)
Definition: FitUtil.h:1421
ROOT::Fit::Chi2FCN::fNEffPoints
unsigned int fNEffPoints
Definition: Chi2FCN.h:162
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Fit::Chi2FCN::operator=
Chi2FCN & operator=(const Chi2FCN &rhs)
Assignment operator.
Definition: Chi2FCN.h:101
ROOT::Fit::Chi2FCN::BaseFCN
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
Definition: Chi2FCN.h:54
ROOT::Fit::Chi2FCN::Chi2FCN
Chi2FCN(const BinData &data, const IModelFunction &func, const ::ROOT::Fit::ExecutionPolicy &executionPolicy=::ROOT::Fit::ExecutionPolicy::kSerial)
Same Constructor from data set (binned ) and model function but now managed by the user we clone the ...
Definition: Chi2FCN.h:77
ROOT::Fit::Chi2FCN::DataElement
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th chi-square residual
Definition: Chi2FCN.h:121
g
#define g(i)
Definition: RSha256.hxx:123