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 Wed Aug 30 11:05:19 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class Fitter
0012 
0013 #ifndef ROOT_Fit_Fitter
0014 #define ROOT_Fit_Fitter
0015 
0016 /**
0017 @defgroup Fit Fitting and Parameter Estimation
0018 
0019 Classes used for fitting (regression analysis) and estimation of parameter values given a data sample.
0020 
0021 @ingroup MathCore
0022 
0023 */
0024 
0025 #include "Fit/BinData.h"
0026 #include "Fit/UnBinData.h"
0027 #include "Fit/FitConfig.h"
0028 #include "ROOT/EExecutionPolicy.hxx"
0029 #include "Fit/FitResult.h"
0030 #include "Math/IParamFunction.h"
0031 #include <memory>
0032 
0033 namespace ROOT {
0034 
0035 
0036    namespace Math {
0037       class Minimizer;
0038 
0039       // should maybe put this in a FitMethodFunctionfwd file
0040       template<class FunctionType> class BasicFitMethodFunction;
0041 
0042       // define the normal and gradient function
0043       typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction>  FitMethodFunction;
0044       typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;
0045 
0046    }
0047 
0048    /**
0049       Namespace for the fitting classes
0050       @ingroup Fit
0051     */
0052 
0053    namespace Fit {
0054 
0055 /**
0056    @defgroup FitMain User Fitting classes
0057 
0058    Main Classes used for fitting a given data set
0059    @ingroup Fit
0060 */
0061 
0062 
0063 //___________________________________________________________________________________
0064 /**
0065    Fitter class, entry point for performing all type of fits.
0066    Fits are performed using the generic ROOT::Fit::Fitter::Fit method.
0067    The inputs are the data points and a model function (using a ROOT::Math::IParamFunction)
0068    The result of the fit is returned and kept internally in the  ROOT::Fit::FitResult class.
0069    The configuration of the fit (parameters, options, etc...) are specified in the
0070    ROOT::Math::FitConfig class.
0071    After fitting the config of the fit will be modified to have the new values the resulting
0072    parameter of the fit with step sizes equal to the errors. FitConfig can be preserved with
0073    initial parameters by calling FitConfig.SetUpdateAfterFit(false);
0074 
0075    @ingroup FitMain
0076 */
0077 class Fitter {
0078 
0079 public:
0080 
0081    typedef ROOT::Math::IParamMultiFunction                 IModelFunction;
0082    template <class T>
0083    using IModelFunctionTempl =                             ROOT::Math::IParamMultiFunctionTempl<T>;
0084 #ifdef R__HAS_VECCORE
0085    typedef ROOT::Math::IParametricFunctionMultiDimTempl<ROOT::Double_v>  IModelFunction_v;
0086    typedef ROOT::Math::IParamMultiGradFunctionTempl<ROOT::Double_v> IGradModelFunction_v;
0087 #else
0088    typedef ROOT::Math::IParamMultiFunction                 IModelFunction_v;
0089    typedef ROOT::Math::IParamMultiGradFunction IGradModelFunction_v;
0090 #endif
0091    typedef ROOT::Math::IParamMultiGradFunction             IGradModelFunction;
0092    typedef ROOT::Math::IParamFunction                      IModel1DFunction;
0093    typedef ROOT::Math::IParamGradFunction                  IGradModel1DFunction;
0094 
0095    typedef ROOT::Math::IMultiGenFunction BaseFunc;
0096    typedef ROOT::Math::IMultiGradFunction BaseGradFunc;
0097 
0098 
0099    /**
0100       Default constructor
0101    */
0102    Fitter () {}
0103 
0104    /**
0105       Constructor from a result
0106    */
0107    Fitter (const std::shared_ptr<FitResult> & result);
0108 
0109 
0110    /**
0111       Destructor.
0112       Make it virtual in case users derive from Fitter class to extend it by adding new methods.
0113       This is needed to avoid a warning seen when doing from Python
0114       (see ROOT issue [#12391](https://github.com/root-project/root/issues/12391) ).
0115       Note that the Fitter class does not provide virtual functions to be re-implemented by derived classes.
0116    */
0117    virtual ~Fitter () {}
0118 
0119    /**
0120       Copy constructor (disabled, class is not copyable)
0121    */
0122    Fitter(const Fitter &) = delete;
0123 
0124    /**
0125       Assignment operator (disabled, class is not copyable)
0126    */
0127    Fitter & operator = (const Fitter &) = delete;
0128 
0129 
0130 public:
0131 
0132    /**
0133        fit a data set using any  generic model  function
0134        If data set is binned a least square fit is performed
0135        If data set is unbinned a maximum likelihood fit (not extended) is done
0136        Pre-requisite on the function:
0137        it must implement the 1D or multidimensional parametric function interface.
0138        Note that both the input data and the function object are copied by the Fitter.
0139    */
0140    template <class Data, class Function,
0141              class cond = typename std::enable_if<!(std::is_same<Function, ROOT::EExecutionPolicy>::value ||
0142                                                     std::is_same<Function, int>::value),
0143                                                   Function>::type>
0144    bool Fit(const Data &data, const Function &func,
0145             const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential)
0146    {
0147       SetFunction(func);
0148       return Fit(data, executionPolicy);
0149    }
0150 
0151    /**
0152       Fit a binned data set using a least square fit.
0153       Note that the provided input data are copied in the Fitter class.
0154       Use the next function (passing a `shared_ptr` to the BinData class if you want to avoid
0155       copying.
0156    */
0157    bool Fit(const BinData & data, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0158       return LeastSquareFit(data, executionPolicy);
0159    }
0160 
0161    /**
0162       Fit a binned data set using a least square fit.
0163       Pass the input data using a `shared_ptr` for NOT copying the input data.
0164    */
0165    bool Fit(const std::shared_ptr<BinData> & data, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0166       return LeastSquareFit(data, executionPolicy);
0167    }
0168 
0169    /**
0170        Fit a binned data set using a least square fit copying the input data.
0171    */
0172    bool LeastSquareFit(const BinData & data, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0173       SetData(data);
0174       return DoLeastSquareFit(executionPolicy);
0175    }
0176    /**
0177        Fit a binned data set using a least square fit NOT copying the input data.
0178    */
0179    bool LeastSquareFit(const std::shared_ptr<BinData> & data, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0180       SetData(data);
0181       return DoLeastSquareFit(executionPolicy);
0182    }
0183 
0184    /**
0185        Fit an un-binned data set using the negative log-likelihood method.
0186        This function copies the input data.
0187    */
0188    bool Fit(const UnBinData & data, bool extended = false, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0189       return LikelihoodFit(data, extended, executionPolicy);
0190    }
0191    /**
0192        Fit an un-binned data set using the negative log-likelihood method.
0193        This function uses a `shared_ptr` to avoid copying the input data.
0194    */
0195    bool Fit(const std::shared_ptr<UnBinData> & data, bool extended = false, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0196       return LikelihoodFit(data, extended, executionPolicy);
0197    }
0198 
0199    /**
0200       Binned Likelihood fit copying the input data.
0201       Default is extended.
0202     */
0203    bool LikelihoodFit(const BinData &data, bool extended = true,
0204                       const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0205       SetData(data);
0206       return DoBinnedLikelihoodFit(extended, executionPolicy);
0207    }
0208    /**
0209       Binned Likelihood fit using a `shared_ptr` for NOT copying the input data.
0210       Default is extended.
0211     */
0212    bool LikelihoodFit(const std::shared_ptr<BinData> &data, bool extended = true,
0213                       const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0214       SetData(data);
0215       return DoBinnedLikelihoodFit(extended, executionPolicy);
0216    }
0217    /**
0218       Un-binned Likelihood fit copying the input data
0219       Default is NOT extended
0220     */
0221    bool LikelihoodFit(const UnBinData & data, bool extended = false, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0222       SetData(data);
0223       return DoUnbinnedLikelihoodFit(extended, executionPolicy);
0224    }
0225    /**
0226       Un-binned Likelihood fit using a `shared_ptr` for NOT copying the input data.
0227       Default is NOT extended
0228     */
0229    bool LikelihoodFit(const std::shared_ptr<UnBinData> & data, bool extended = false, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential) {
0230       SetData(data);
0231       return DoUnbinnedLikelihoodFit(extended, executionPolicy);
0232    }
0233 
0234    /**
0235       Likelihood fit given a data set (Binned or Un-binned) using any  generic model  function.
0236       This interface copies the input data and the model function object
0237    */
0238    template < class Data , class Function>
0239    bool LikelihoodFit( const Data & data, const Function & func, bool extended) {
0240       SetFunction(func);
0241       return LikelihoodFit(data, extended);
0242    }
0243 
0244    /**
0245       Do a linear fit copying the input data
0246     */
0247    bool LinearFit(const BinData & data) {
0248       SetData(data);
0249       return DoLinearFit();
0250    }
0251    /**
0252       Do a linear fit using a `shared_ptr` for NOT copying the input data
0253     */
0254    bool LinearFit(const std::shared_ptr<BinData> & data) {
0255       SetData(data);
0256       return DoLinearFit();
0257    }
0258 
0259    /**
0260       Fit using the a generic FCN function as a C++ callable object implementing
0261       double () (const double *)
0262       Note that the function dimension (i.e. the number of parameter) is needed in this case
0263       For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
0264     */
0265    template <class Function>
0266    bool FitFCN(unsigned int npar, Function  & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0267 
0268    /**
0269       Set a generic FCN function as a C++ callable object implementing
0270       double () (const double *)
0271       Note that the function dimension (i.e. the number of parameter) is needed in this case
0272       For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
0273     */
0274    template <class Function>
0275    bool SetFCN(unsigned int npar, Function  & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0276 
0277    /**
0278       Fit using the given FCN function represented by a multi-dimensional function interface
0279       (ROOT::Math::IMultiGenFunction).
0280       Give optionally the initial parameter values, data size to have the fit Ndf correctly
0281       set in the FitResult and flag specifying the type of fit. The fitType can be:
0282       0 undefined, 1 least square fit, 2 unbinned likelihood fit, 3 binned likelihood fit
0283       Note that if the parameters values are not given (params=0) the
0284       current parameter settings are used. The parameter settings can be created before
0285       by using the FitConfig::SetParamsSetting. If they have not been created they are created
0286       automatically when the params pointer is not zero.
0287       Note that passing a params != 0 will set the parameter settings to the new value AND also the
0288       step sizes to some pre-defined value (stepsize = 0.3 * abs(parameter_value) )
0289     */
0290    bool FitFCN(const ROOT::Math::IMultiGenFunction &fcn, const double *params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0291 
0292    /**
0293        Fit using a FitMethodFunction interface. Same as method above, but now extra information
0294        can be taken from the function class
0295    */
0296    bool FitFCN(const ROOT::Math::FitMethodFunction & fcn, const double *params = nullptr);
0297 
0298    /**
0299       Set the FCN function represented by a multi-dimensional function interface
0300       (ROOT::Math::IMultiGenFunction) and optionally the initial parameters
0301       See also note above for the initial parameters for FitFCN
0302     */
0303    bool SetFCN(const ROOT::Math::IMultiGenFunction &fcn, const double *params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0304 
0305    /**
0306       Set the FCN function represented by a multi-dimensional function interface
0307      (ROOT::Math::IMultiGenFunction) and optionally the initial parameters
0308       See also note above for the initial parameters for FitFCN
0309       With this interface we pass in addition a ModelFunction that will be attached to the FitResult and
0310       used to compute confidence interval of the fit
0311    */
0312    bool SetFCN(const ROOT::Math::IMultiGenFunction &fcn, const IModelFunction & func, const double *params = nullptr,
0313                unsigned int dataSize = 0, int fitType = 0);
0314 
0315    /**
0316        Set the objective function (FCN)  using a FitMethodFunction interface.
0317        Same as method above, but now extra information can be taken from the function class
0318    */
0319    bool SetFCN(const ROOT::Math::FitMethodFunction & fcn, const double *params = nullptr);
0320 
0321    /**
0322        Fit using a FitMethodGradFunction interface. Same as method above, but now extra information
0323        can be taken from the function class
0324    */
0325    bool FitFCN(const ROOT::Math::FitMethodGradFunction & fcn, const double *params = nullptr);
0326 
0327    /**
0328        Set the objective function (FCN)  using a FitMethodGradFunction interface.
0329        Same as method above, but now extra information can be taken from the function class
0330    */
0331    bool SetFCN(const ROOT::Math::FitMethodGradFunction & fcn, const double *params = nullptr);
0332 
0333 
0334    /**
0335       fit using user provided FCN with Minuit-like interface
0336       If npar = 0 it is assumed that the parameters are specified in the parameter settings created before
0337       For the options same consideration as in the previous method
0338     */
0339    typedef  void (* MinuitFCN_t )(int &npar, double *gin, double &f, double *u, int flag);
0340    bool FitFCN( MinuitFCN_t fcn, int npar = 0, const double *params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0341 
0342    /**
0343       set objective function using user provided FCN with Minuit-like interface
0344       If npar = 0 it is assumed that the parameters are specified in the parameter settings created before
0345       For the options same consideration as in the previous method
0346     */
0347    bool SetFCN( MinuitFCN_t fcn, int npar = 0, const double *params = nullptr, unsigned int dataSize = 0, int fitType = 0);
0348 
0349    /**
0350       Perform a fit with the previously set FCN function. Require SetFCN before
0351     */
0352    bool FitFCN();
0353 
0354    /**
0355       Perform a simple FCN evaluation. FitResult will be modified and contain  the value of the FCN
0356     */
0357    bool EvalFCN();
0358 
0359 
0360 
0361    /**
0362        Set the fitted function (model function) from a parametric function interface
0363    */
0364    void  SetFunction(const IModelFunction & func, bool useGradient = false);
0365 
0366    /**
0367        Set the fitted function (model function) from a vectorized parametric function interface
0368    */
0369 #ifdef R__HAS_VECCORE
0370    template <class NotCompileIfScalarBackend = std::enable_if<!(std::is_same<double, ROOT::Double_v>::value)>>
0371    void SetFunction(const IModelFunction_v &func, bool useGradient = false);
0372 
0373    template <class NotCompileIfScalarBackend = std::enable_if<!(std::is_same<double, ROOT::Double_v>::value)>>
0374    void SetFunction(const IGradModelFunction_v &func, bool useGradient = true);
0375 #endif
0376    /**
0377       Set the fitted function from a parametric 1D function interface
0378     */
0379    void  SetFunction(const IModel1DFunction & func, bool useGradient = false);
0380 
0381    /**
0382        Set the fitted function (model function) from a parametric gradient function interface
0383    */
0384    void  SetFunction(const IGradModelFunction & func, bool useGradient = true);
0385    /**
0386       Set the fitted function from 1D gradient parametric function interface
0387     */
0388    void  SetFunction(const IGradModel1DFunction & func, bool useGradient = true);
0389 
0390 
0391    /**
0392       get fit result
0393    */
0394    const FitResult & Result() const {
0395       assert( fResult.get() );
0396       return *fResult;
0397    }
0398 
0399 
0400    /**
0401       perform an error analysis on the result using the Hessian
0402       Errors are obtained from the inverse of the Hessian matrix
0403       To be called only after fitting and when a minimizer supporting the Hessian calculations is used
0404       otherwise an error (false) is returned.
0405       A new  FitResult with the Hessian result will be produced
0406     */
0407    bool CalculateHessErrors();
0408 
0409    /**
0410       perform an error analysis on the result using MINOS
0411       To be called only after fitting and when a minimizer supporting MINOS is used
0412       otherwise an error (false) is returned.
0413       The result will be appended in the fit result class
0414       Optionally a vector of parameter indices can be passed for selecting
0415       the parameters to analyse using FitConfig::SetMinosErrors
0416     */
0417    bool CalculateMinosErrors();
0418 
0419    /**
0420       access to the fit configuration (const method)
0421    */
0422    const FitConfig & Config() const { return fConfig; }
0423 
0424    /**
0425       access to the configuration (non const method)
0426    */
0427    FitConfig & Config() { return fConfig; }
0428 
0429    /**
0430       query if fit is binned. In cse of false the fit can be unbinned
0431       or is not defined (like in case of fitting through a ROOT::Fit::Fitter::FitFCN)
0432     */
0433    bool IsBinFit() const { return fBinFit; }
0434 
0435    /**
0436       return pointer to last used minimizer
0437       (is NULL in case fit is not yet done)
0438       This pointer is guaranteed to be valid as far as the fitter class is valid and a new fit is not redone.
0439       To be used only after fitting.
0440       The pointer should not be stored and will be invalided after performing a new fitting.
0441       In this case a new instance of ROOT::Math::Minimizer will be re-created and can be
0442       obtained calling again GetMinimizer()
0443     */
0444    ROOT::Math::Minimizer * GetMinimizer() const { return fMinimizer.get(); }
0445 
0446    /**
0447       return pointer to last used objective function
0448       (is NULL in case fit is not yet done)
0449       This pointer will be valid as far as the fitter class
0450       has not been deleted. To be used after the fitting.
0451       The pointer should not be stored and will be invalided after performing a new fitting.
0452       In this case a new instance of the function pointer will be re-created and can be
0453       obtained calling again GetFCN()
0454     */
0455    ROOT::Math::IMultiGenFunction * GetFCN() const {
0456       return fObjFunction.get();
0457     }
0458 
0459 
0460    /**
0461       apply correction in the error matrix for the weights for likelihood fits
0462       This method can be called only after a fit. The
0463       passed function (loglw2) is a log-likelihood function implemented using the
0464       sum of weight squared
0465       When using FitConfig.SetWeightCorrection() this correction is applied
0466       automatically when doing a likelihood fit (binned or unbinned)
0467    */
0468    bool ApplyWeightCorrection(const ROOT::Math::IMultiGenFunction & loglw2, bool minimizeW2L=false);
0469 
0470    /// Set number of fit points when using an external FCN function
0471    /// This function can be called after Fit to set the correct number of Ndf in FitResult
0472    void SetNumberOfFitPoints(unsigned int npoints) {
0473       if (fExtObjFunction) fDataSize = npoints;
0474       if (!fResult->IsEmpty()) fResult->SetChi2AndNdf(-1,npoints);
0475    }
0476 
0477    /// Set the type of fit when using an external FCN
0478    /// possible types are : 1 (least-square), 2 (unbinned-likelihood), 3 (binned-likelihood)
0479    /// Note that in case of binned likelihood fit the chi2 will be computed as 2 * MinFCN()
0480    /// Note this function should be called before fitting to have effect on th FitResult
0481    void SetFitType(int type) {
0482       if (fExtObjFunction) fFitType = type;
0483    }
0484 
0485 
0486 protected:
0487 
0488 
0489    /// least square fit
0490    bool DoLeastSquareFit(const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential);
0491    /// binned likelihood fit
0492    bool DoBinnedLikelihoodFit(bool extended = true, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential);
0493    /// un-binned likelihood fit
0494    bool DoUnbinnedLikelihoodFit( bool extended = false, const ROOT::EExecutionPolicy &executionPolicy = ROOT::EExecutionPolicy::kSequential);
0495    /// linear least square fit
0496    bool DoLinearFit();
0497    /// Set Objective function
0498    bool DoSetFCN(bool useExtFCN, const ROOT::Math::IMultiGenFunction &fcn, const double *params, unsigned int dataSize,
0499                  int fitType);
0500 
0501    // initialize the minimizer
0502    bool DoInitMinimizer();
0503    /// do minimization
0504    template<class ObjFunc_t>
0505    bool DoMinimization(std::unique_ptr<ObjFunc_t> f, const ROOT::Math::IMultiGenFunction * chifunc = nullptr);
0506    // do minimization for weighted likelihood fits
0507    template<class ObjFunc_t>
0508    bool DoWeightMinimization(std::unique_ptr<ObjFunc_t> f, const ROOT::Math::IMultiGenFunction * chifunc = nullptr);
0509    // do minimization after having set the objective function
0510    bool DoMinimization(const ROOT::Math::IMultiGenFunction * chifunc = nullptr);
0511    // update config after fit
0512    void DoUpdateFitConfig();
0513    // update minimizer options for re-fitting
0514    bool DoUpdateMinimizerOptions(bool canDifferentMinim = true);
0515    // get function calls from the FCN
0516    int GetNCallsFromFCN();
0517 
0518    /// Set the input data for the fit using a shared ptr (No Copying)
0519    template <class Data>
0520    void SetData(const std::shared_ptr<Data> & data) {
0521       fData = std::static_pointer_cast<Data>(data);
0522    }
0523 
0524    /// Set the input data for the fit (Copying the given data object)
0525    template <class Data>
0526    void SetData(const Data & data) {
0527       auto dataClone = std::make_shared<Data>(data);
0528       SetData(dataClone);
0529    }
0530 
0531    /// look at the user provided FCN and get data and model function is
0532    /// they derive from ROOT::Fit FCN classes
0533    void ExamineFCN();
0534 
0535 
0536    /// internal functions to get data set and model function from FCN
0537    /// useful for fits done with customized FCN classes
0538    template <class ObjFuncType>
0539    bool GetDataFromFCN();
0540 
0541    /// Return pointer to the used objective function for fitting.
0542    /// If using an external function (e.g. given in SetFCN), return the cached pointer,
0543    /// otherwise use the one stored as shared ptr and managed by the Fitter class
0544    const ROOT::Math::IBaseFunctionMultiDimTempl<double> * ObjFunction() const {
0545       // need to specify here full return type since when using the typedef (IMultiGenFunction)
0546       // there is an error when using the class in Python (see issue #12391)
0547       return (fExtObjFunction) ? fExtObjFunction : fObjFunction.get();
0548    }
0549 
0550 private:
0551 
0552    bool fUseGradient = false;  ///< flag to indicate if using gradient or not
0553 
0554    bool fBinFit = false;    ///< flag to indicate if fit is binned
0555                             ///< in case of false the fit is unbinned or undefined)
0556                             ///< flag it is used to compute chi2 for binned likelihood fit
0557 
0558    int fFitType = 0;   ///< type of fit   (0 undefined, 1 least square, 2 likelihood, 3 binned likelihood)
0559 
0560    int fDataSize = 0;  ///< size of data sets (need for Fumili or LM fitters)
0561 
0562    FitConfig fConfig;       ///< fitter configuration (options and parameter settings)
0563 
0564    std::shared_ptr<IModelFunction_v> fFunc_v;  ///<! copy of the fitted  function containing on output the fit result
0565 
0566    std::shared_ptr<IModelFunction> fFunc;  ///<! copy of the fitted  function containing on output the fit result
0567 
0568    std::shared_ptr<ROOT::Fit::FitResult>  fResult;  ///<! pointer to the object containing the result of the fit
0569 
0570    std::shared_ptr<ROOT::Math::Minimizer>  fMinimizer;  ///<! pointer to used minimizer
0571 
0572    std::shared_ptr<ROOT::Fit::FitData>  fData;  ///<! pointer to the fit data (binned or unbinned data)
0573 
0574    std::shared_ptr<ROOT::Math::IMultiGenFunction>  fObjFunction;  ///<! pointer to used objective function
0575 
0576    const ROOT::Math::IMultiGenFunction * fExtObjFunction = nullptr;     ///<! pointer to an external FCN
0577 
0578 };
0579 
0580 
0581 // internal functions to get data set and model function from FCN
0582 // useful for fits done with customized FCN classes
0583 template <class ObjFuncType>
0584 bool Fitter::GetDataFromFCN()  {
0585    const ObjFuncType * objfunc = dynamic_cast<const ObjFuncType*>(ObjFunction());
0586    if (objfunc) {
0587       fFunc = objfunc->ModelFunctionPtr();
0588       fData = objfunc->DataPtr();
0589       return true;
0590    }
0591    else {
0592       return false;
0593    }
0594 }
0595 
0596 #ifdef R__HAS_VECCORE
0597 template <class NotCompileIfScalarBackend>
0598 void Fitter::SetFunction(const IModelFunction_v &func, bool useGradient)
0599 {
0600    fUseGradient = useGradient;
0601    if (fUseGradient) {
0602       const IGradModelFunction_v *gradFunc = dynamic_cast<const IGradModelFunction_v *>(&func);
0603       if (gradFunc) {
0604          SetFunction(*gradFunc, true);
0605          return;
0606       } else {
0607          MATH_WARN_MSG("Fitter::SetFunction",
0608                        "Requested function does not provide gradient - use it as non-gradient function ");
0609       }
0610    }
0611 
0612    //  set the fit model function (clone the given one and keep a copy )
0613    //  std::cout << "set a non-grad function" << std::endl;
0614    fUseGradient = false;
0615    fFunc_v = std::shared_ptr<IModelFunction_v>(dynamic_cast<IModelFunction_v *>(func.Clone()));
0616    assert(fFunc_v);
0617 
0618    // creates the parameter  settings
0619    fConfig.CreateParamsSettings(*fFunc_v);
0620    fFunc.reset();
0621 }
0622 
0623 template <class NotCompileIfScalarBackend>
0624 void Fitter::SetFunction(const IGradModelFunction_v &func, bool useGradient)
0625 {
0626    fUseGradient = useGradient;
0627 
0628    //  set the fit model function (clone the given one and keep a copy )
0629    fFunc_v = std::shared_ptr<IModelFunction_v>(dynamic_cast<IGradModelFunction_v *>(func.Clone()));
0630    assert(fFunc_v);
0631 
0632    // creates the parameter  settings
0633    fConfig.CreateParamsSettings(*fFunc_v);
0634    fFunc.reset();
0635 }
0636 #endif
0637 
0638    } // end namespace Fit
0639 
0640 } // end namespace ROOT
0641 
0642 // implementation of inline methods
0643 
0644 
0645 
0646 #include "Math/WrappedFunction.h"
0647 
0648 template<class Function>
0649 bool ROOT::Fit::Fitter::FitFCN(unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
0650    ROOT::Math::WrappedMultiFunction<Function &> wf(f,npar);
0651    if (!DoSetFCN(false, wf, par, datasize, fitType))
0652       return false;
0653    return FitFCN();
0654 }
0655 template<class Function>
0656 bool ROOT::Fit::Fitter::SetFCN(unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
0657    ROOT::Math::WrappedMultiFunction<Function &> wf(f,npar);
0658    return DoSetFCN(false, wf, par, datasize, fitType);
0659 }
0660 
0661 
0662 
0663 
0664 #endif /* ROOT_Fit_Fitter */