Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:08

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Tue Sep  5 09:13:32 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class Chi2FCN
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 @defgroup FitMethodFunc Fit Method Classes
0029 
0030 Classes describing Fit Method functions
0031 @ingroup Fit
0032 */
0033 
0034 namespace ROOT {
0035 
0036 
0037    namespace Fit {
0038 
0039 //___________________________________________________________________________________
0040 /**
0041    Chi2FCN class for binned fits using the least square methods
0042 
0043    @ingroup  FitMethodFunc
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    //typedef  typename ::ROOT::Math::ParamFunctionTrait<FunType>::PFType IModelFunction;
0057    typedef  ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
0058    typedef typename BaseObjFunction::Type_t Type_t;
0059 
0060    /**
0061       Constructor from data set (binned ) and model function
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       Same Constructor from data set (binned ) and model function cloning the function and the data
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       Destructor (no operations)
0082    */
0083    virtual ~Chi2FCN ()  {}
0084    /**
0085       Copy constructor
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       Assignment operator
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       clone the function
0106     */
0107    virtual BaseFunction * Clone() const {
0108       return new Chi2FCN(*this);
0109    }
0110 
0111 
0112 
0113    using BaseObjFunction::operator();
0114 
0115 
0116    /// i-th chi-square residual
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    // need to be virtual to be instantiated
0123    virtual void Gradient(const double *x, double *g) const {
0124       // evaluate the chi2 gradient
0125       FitUtil::Evaluate<T>::EvalChi2Gradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g, fNEffPoints,
0126                                              fExecutionPolicy);
0127    }
0128 
0129 
0130    /// get type of fit method function
0131    virtual  typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
0132 
0133 
0134 protected:
0135 
0136    /// set number of fit points (need to be called in const methods, make it const)
0137    virtual void SetNFitPoints(unsigned int n) const { fNEffPoints = n; }
0138 
0139 private:
0140 
0141    /**
0142       Evaluation of the  function (required by interface)
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    // for derivatives
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;  ///< number of effective points used in the fit
0160 
0161    mutable std::vector<double> fGrad; ///< for derivatives
0162    ::ROOT::EExecutionPolicy fExecutionPolicy;
0163 
0164 };
0165 
0166       // define useful typedef's
0167       typedef Chi2FCN<ROOT::Math::IMultiGenFunction,ROOT::Math::IParamMultiFunction> Chi2Function;
0168       typedef Chi2FCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> Chi2GradFunction;
0169 
0170 
0171    } // end namespace Fit
0172 
0173 } // end namespace ROOT
0174 
0175 
0176 #endif /* ROOT_Fit_Chi2FCN */