Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
17#include "Fit/BasicFCN.h"
18#include "Fit/BinData.h"
19#include "Fit/FitUtil.h"
20#include "Math/IFunction.h"
21#include "Math/IFunctionfwd.h"
22#include "Math/IParamFunction.h"
23
24#include <memory>
25#include <vector>
26
27/**
28@defgroup FitMethodFunc Fit Method Classes
29
30Classes describing Fit Method functions
31@ingroup Fit
32*/
33
34namespace ROOT {
35
36
37 namespace Fit {
38
39//___________________________________________________________________________________
40/**
41 Chi2FCN class for binned fits using the least square methods
42
43 @ingroup FitMethodFunc
44*/
45template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
46class Chi2FCN : public BasicFCN<DerivFunType, ModelFunType, BinData> {
47
48public:
49
50 typedef typename ModelFunType::BackendType T;
52
53 typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
55
56 //typedef typename ::ROOT::Math::ParamFunctionTrait<FunType>::PFType IModelFunction;
57 typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
59
60 /**
61 Constructor from data set (binned ) and model function
62 */
63 Chi2FCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
64 BaseFCN( data, func),
65 fNEffPoints(0),
66 fGrad ( std::vector<double> ( func->NPar() ) ),
67 fExecutionPolicy(executionPolicy)
68 { }
69
70 /**
71 Same Constructor from data set (binned ) and model function cloning the function and the data
72 */
73 Chi2FCN ( const BinData & data, const IModelFunction & func, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
74 BaseFCN(std::make_shared<BinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
75 fNEffPoints(0),
76 fGrad ( std::vector<double> ( func.NPar() ) ),
77 fExecutionPolicy(executionPolicy)
78 { }
79
80 /**
81 Destructor (no operations)
82 */
83 virtual ~Chi2FCN () {}
84 /**
85 Copy constructor
86 */
87 Chi2FCN(const Chi2FCN & f) :
90 fGrad( f.fGrad),
92 { }
93
94 /**
95 Assignment operator
96 */
97 Chi2FCN & operator = (const Chi2FCN & rhs) {
98 SetData(rhs.DataPtr() );
101 fGrad = rhs.fGrad;
102 }
103
104 /*
105 clone the function
106 */
107 virtual BaseFunction * Clone() const {
108 return new Chi2FCN(*this);
109 }
110
111
112
113 using BaseObjFunction::operator();
114
115
116 /// i-th chi-square residual
117 virtual double DataElement(const double *x, unsigned int i, double *g, double * h = nullptr, bool fullHessian = false) const {
118 if (i==0) this->UpdateNCalls();
120 }
121
122 // need to be virtual to be instantiated
123 virtual void Gradient(const double *x, double *g) const {
124 // evaluate the chi2 gradient
127 }
128
129
130 /// get type of fit method function
131 virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
132
133
134protected:
135
136 /// set number of fit points (need to be called in const methods, make it const)
137 virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
138
139private:
140
141 /**
142 Evaluation of the function (required by interface)
143 */
144 virtual double DoEval (const double * x) const {
145 this->UpdateNCalls();
146 if (BaseFCN::Data().HaveCoordErrors() || BaseFCN::Data().HaveAsymErrors())
148 else
150 }
151
152 // for derivatives
153 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
154 Gradient(x, fGrad.data());
155 return fGrad[icoord];
156 }
157
158
159 mutable unsigned int fNEffPoints; ///< number of effective points used in the fit
160
161 mutable std::vector<double> fGrad; ///< for derivatives
163
164};
165
166 // define useful typedef's
169
170
171 } // end namespace Fit
172
173} // end namespace ROOT
174
175
176#endif /* ROOT_Fit_Chi2FCN */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition BasicFCN.h:40
void SetData(const std::shared_ptr< DataType > &data)
Set the data pointer.
Definition BasicFCN.h:98
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition BasicFCN.h:81
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition BasicFCN.h:101
virtual const DataType & Data() const
access to const reference to the data
Definition BasicFCN.h:72
std::shared_ptr< DataType > DataPtr() const
access to data pointer
Definition BasicFCN.h:75
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition BasicFCN.h:78
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
Chi2FCN class for binned fits using the least square methods.
Definition Chi2FCN.h:46
BaseObjFunction::BaseFunction BaseFunction
Definition Chi2FCN.h:54
unsigned int fNEffPoints
number of effective points used in the fit
Definition Chi2FCN.h:159
virtual double DoDerivative(const double *x, unsigned int icoord) const
Definition Chi2FCN.h:153
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Definition Chi2FCN.h:131
::ROOT::EExecutionPolicy fExecutionPolicy
Definition Chi2FCN.h:162
virtual void Gradient(const double *x, double *g) const
Definition Chi2FCN.h:123
std::vector< double > fGrad
for derivatives
Definition Chi2FCN.h:161
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
Definition Chi2FCN.h:57
Chi2FCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Constructor from data set (binned ) and model function.
Definition Chi2FCN.h:63
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
Definition Chi2FCN.h:51
ModelFunType::BackendType T
Definition Chi2FCN.h:50
virtual BaseFunction * Clone() const
Definition Chi2FCN.h:107
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:137
Chi2FCN(const Chi2FCN &f)
Copy constructor.
Definition Chi2FCN.h:87
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
Definition Chi2FCN.h:53
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Definition Chi2FCN.h:144
BaseObjFunction::Type_t Type_t
Definition Chi2FCN.h:58
virtual double DataElement(const double *x, unsigned int i, double *g, double *h=nullptr, bool fullHessian=false) const
i-th chi-square residual
Definition Chi2FCN.h:117
Chi2FCN & operator=(const Chi2FCN &rhs)
Assignment operator.
Definition Chi2FCN.h:97
virtual ~Chi2FCN()
Destructor (no operations)
Definition Chi2FCN.h:83
Chi2FCN(const BinData &data, const IModelFunction &func, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Same Constructor from data set (binned ) and model function cloning the function and the data.
Definition Chi2FCN.h:73
Type_t
enumeration specifying the possible fit method types
static bool IsAGradFCN()
Static function to indicate if a function is supporting gradient.
virtual void UpdateNCalls() const
update number of calls
IParamFunction interface (abstract class) describing multi-dimensional parametric functions It is a d...
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
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
Chi2FCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > Chi2Function
Definition Chi2FCN.h:167
Chi2FCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > Chi2GradFunction
Definition Chi2FCN.h:168
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
static double EvalChi2Effective(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int &nPoints)
Definition FitUtil.h:1427
static void EvalChi2Gradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
Definition FitUtil.h:1431
static double EvalChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition FitUtil.h:1399
static double EvalChi2Residual(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)
Definition FitUtil.h:1439