File indexing completed on 2025-01-18 10:10:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef ROOT_Fit_Chi2FCN
0014 #define ROOT_Fit_Chi2FCN
0015
0016 #include "ROOT/EExecutionPolicy.hxx"
0017 #include "Fit/BasicFCN.h"
0018 #include "Fit/BinData.h"
0019 #include "Fit/FitUtil.h"
0020 #include "Math/IFunction.h"
0021 #include "Math/IFunctionfwd.h"
0022 #include "Math/IParamFunction.h"
0023
0024 #include <memory>
0025 #include <vector>
0026
0027
0028
0029
0030
0031
0032
0033
0034 namespace ROOT {
0035
0036
0037 namespace Fit {
0038
0039
0040
0041
0042
0043
0044
0045 template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
0046 class Chi2FCN : public BasicFCN<DerivFunType, ModelFunType, BinData> {
0047
0048 public:
0049
0050 typedef typename ModelFunType::BackendType T;
0051 typedef BasicFCN<DerivFunType, ModelFunType, BinData> BaseFCN;
0052
0053 typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
0054 typedef typename BaseObjFunction::BaseFunction BaseFunction;
0055
0056
0057 typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
0058 typedef typename BaseObjFunction::Type_t Type_t;
0059
0060
0061
0062
0063 Chi2FCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
0064 BaseFCN( data, func),
0065 fNEffPoints(0),
0066 fGrad ( std::vector<double> ( func->NPar() ) ),
0067 fExecutionPolicy(executionPolicy)
0068 { }
0069
0070
0071
0072
0073 Chi2FCN ( const BinData & data, const IModelFunction & func, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
0074 BaseFCN(std::make_shared<BinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
0075 fNEffPoints(0),
0076 fGrad ( std::vector<double> ( func.NPar() ) ),
0077 fExecutionPolicy(executionPolicy)
0078 { }
0079
0080
0081
0082
0083 virtual ~Chi2FCN () {}
0084
0085
0086
0087 Chi2FCN(const Chi2FCN & f) :
0088 BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
0089 fNEffPoints( f.fNEffPoints ),
0090 fGrad( f.fGrad),
0091 fExecutionPolicy(f.fExecutionPolicy)
0092 { }
0093
0094
0095
0096
0097 Chi2FCN & operator = (const Chi2FCN & rhs) {
0098 SetData(rhs.DataPtr() );
0099 SetModelFunction(rhs.ModelFunctionPtr() );
0100 fNEffPoints = rhs.fNEffPoints;
0101 fGrad = rhs.fGrad;
0102 }
0103
0104
0105
0106
0107 virtual BaseFunction * Clone() const {
0108 return new Chi2FCN(*this);
0109 }
0110
0111
0112
0113 using BaseObjFunction::operator();
0114
0115
0116
0117 virtual double DataElement(const double *x, unsigned int i, double *g, double * h = nullptr, bool fullHessian = false) const {
0118 if (i==0) this->UpdateNCalls();
0119 return FitUtil::Evaluate<T>::EvalChi2Residual(BaseFCN::ModelFunction(), BaseFCN::Data(), x, i, g, h, BaseFCN::IsAGradFCN(), fullHessian);
0120 }
0121
0122
0123 virtual void Gradient(const double *x, double *g) const {
0124
0125 FitUtil::Evaluate<T>::EvalChi2Gradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g, fNEffPoints,
0126 fExecutionPolicy);
0127 }
0128
0129
0130
0131 virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
0132
0133
0134 protected:
0135
0136
0137 virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
0138
0139 private:
0140
0141
0142
0143
0144 virtual double DoEval (const double * x) const {
0145 this->UpdateNCalls();
0146 if (BaseFCN::Data().HaveCoordErrors() || BaseFCN::Data().HaveAsymErrors())
0147 return FitUtil::Evaluate<T>::EvalChi2Effective(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fNEffPoints);
0148 else
0149 return FitUtil::Evaluate<T>::EvalChi2(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fNEffPoints, fExecutionPolicy);
0150 }
0151
0152
0153 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
0154 Gradient(x, fGrad.data());
0155 return fGrad[icoord];
0156 }
0157
0158
0159 mutable unsigned int fNEffPoints;
0160
0161 mutable std::vector<double> fGrad;
0162 ::ROOT::EExecutionPolicy fExecutionPolicy;
0163
0164 };
0165
0166
0167 typedef Chi2FCN<ROOT::Math::IMultiGenFunction,ROOT::Math::IParamMultiFunction> Chi2Function;
0168 typedef Chi2FCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> Chi2GradFunction;
0169
0170
0171 }
0172
0173 }
0174
0175
0176 #endif