88 * *
99 **********************************************************************/
1010
11- // Header file for class Fitter
12-
1311#ifndef ROOT_Fit_Fitter
1412#define ROOT_Fit_Fitter
1513
@@ -25,34 +23,35 @@ Classes used for fitting (regression analysis) and estimation of parameter value
2523*/
2624
2725#include " Fit/BinData.h"
28- #include " Fit/UnBinData.h"
2926#include " Fit/FitConfig.h"
30- #include " ROOT/EExecutionPolicy.hxx"
3127#include " Fit/FitResult.h"
28+ #include " Fit/UnBinData.h"
3229#include " Math/IParamFunction.h"
33- #include < memory>
30+ #include " Math/WrappedFunction.h"
31+ #include " ROOT/EExecutionPolicy.hxx"
3432
35- namespace ROOT {
33+ # include < memory >
3634
35+ namespace ROOT ::Math {
3736
38- namespace Math {
39- class Minimizer ;
37+ class Minimizer ;
4038
41- // should maybe put this in a FitMethodFunctionfwd file
42- template <class FunctionType > class BasicFitMethodFunction ;
39+ // should maybe put this in a FitMethodFunctionfwd file
40+ template <class FunctionType >
41+ class BasicFitMethodFunction ;
4342
44- // define the normal and gradient function
45- typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction> FitMethodFunction;
46- typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;
43+ // define the normal and gradient function
44+ typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction> FitMethodFunction;
45+ typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;
4746
48- }
47+ } // namespace ROOT::Math
4948
5049 /* *
5150 Namespace for the fitting classes
5251 @ingroup Fit
5352 */
5453
55- namespace Fit {
54+ namespace ROOT :: Fit {
5655
5756/* *
5857 @defgroup FitMain User Fitting classes
@@ -265,7 +264,9 @@ class Fitter {
265264 For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
266265 */
267266 template <class Function >
268- bool FitFCN (unsigned int npar, Function & fcn, const double * params = nullptr , unsigned int dataSize = 0 , int fitType = 0 );
267+ bool FitFCN (unsigned int npar, Function & fcn, const double * params = nullptr , unsigned int dataSize = 0 , int fitType = 0 ) {
268+ return DoSetFCN (false , ROOT::Math::WrappedMultiFunction<Function &>{fcn, npar}, params, dataSize, fitType) ? FitFCN () : false ;
269+ }
269270
270271 /* *
271272 Set a generic FCN function as a C++ callable object implementing
@@ -274,7 +275,9 @@ class Fitter {
274275 For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
275276 */
276277 template <class Function >
277- bool SetFCN (unsigned int npar, Function & fcn, const double * params = nullptr , unsigned int dataSize = 0 , int fitType = 0 );
278+ bool SetFCN (unsigned int npar, Function & fcn, const double * params = nullptr , unsigned int dataSize = 0 , int fitType = 0 ) {
279+ return DoSetFCN (false , ROOT::Math::WrappedMultiFunction<Function &>{fcn, npar}, params, dataSize, fitType);
280+ }
278281
279282 /* *
280283 Fit using the given FCN function represented by a multi-dimensional function interface
@@ -527,70 +530,49 @@ class Fitter {
527530 // / Set the input data for the fit (Copying the given data object)
528531 template <class Data >
529532 void SetData (const Data & data) {
530- auto dataClone = std::make_shared<Data>(data);
531- SetData (dataClone);
533+ SetData (std::make_shared<Data>(data));
532534 }
533535
534536 // / internal functions to get data set and model function from FCN
535537 // / useful for fits done with customized FCN classes
536538 template <class ObjFuncType >
537- bool GetDataFromFCN ();
539+ bool GetDataFromFCN () {
540+ if (const ObjFuncType *objfunc = dynamic_cast <const ObjFuncType *>(ObjFunction ())) {
541+ fFunc = objfunc->ModelFunctionPtr ();
542+ fData = objfunc->DataPtr ();
543+ return true ;
544+ }
545+ return false ;
546+ }
538547
539548 // / Return pointer to the used objective function for fitting.
540549 // / If using an external function (e.g. given in SetFCN), return the cached pointer,
541550 // / otherwise use the one stored as shared ptr and managed by the Fitter class
542551 const ROOT::Math::IBaseFunctionMultiDimTempl<double > * ObjFunction () const {
543552 // need to specify here full return type since when using the typedef (IMultiGenFunction)
544553 // there is an error when using the class in Python (see issue #12391)
545- return ( fExtObjFunction ) ? fExtObjFunction : fObjFunction .get ();
554+ return fExtObjFunction ? fExtObjFunction : fObjFunction .get ();
546555 }
547556
548557private:
549558
550- bool fUseGradient = false ; // /< flag to indicate if using gradient or not
551-
552- bool fBinFit = false ; // /< flag to indicate if fit is binned
553- // /< in case of false the fit is unbinned or undefined)
554- // /< flag it is used to compute chi2 for binned likelihood fit
555-
556- int fFitType = 0 ; // /< type of fit (0 undefined, 1 least square, 2 likelihood, 3 binned likelihood)
557-
558- int fDataSize = 0 ; // /< size of data sets (need for Fumili or LM fitters)
559-
560- FitConfig fConfig ; // /< fitter configuration (options and parameter settings)
561-
562- std::shared_ptr<IModelFunction_v> fFunc_v ; // /<! copy of the fitted function containing on output the fit result
563-
564- std::shared_ptr<IModelFunction> fFunc ; // /<! copy of the fitted function containing on output the fit result
565-
566- std::shared_ptr<ROOT::Fit::FitResult> fResult ; // /<! pointer to the object containing the result of the fit
567-
568- std::shared_ptr<ROOT::Math::Minimizer> fMinimizer ; // /<! pointer to used minimizer
569-
570- std::shared_ptr<ROOT::Fit::FitData> fData ; // /<! pointer to the fit data (binned or unbinned data)
571-
572- std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunction ; // /<! pointer to used objective function
573-
574- const ROOT::Math::IMultiGenFunction * fExtObjFunction = nullptr ; // /<! pointer to an external FCN
575-
559+ bool fUseGradient = false ; // /< flag to indicate if using gradient or not
560+ bool fBinFit = false ; // /< flag to indicate if fit is binned
561+ // /< in case of false the fit is unbinned or undefined)
562+ // /< flag it is used to compute chi2 for binned likelihood fit
563+ int fFitType = 0 ; // /< type of fit (0 undefined, 1 least square, 2 likelihood, 3 binned likelihood)
564+ int fDataSize = 0 ; // /< size of data sets (need for Fumili or LM fitters)
565+ FitConfig fConfig ; // /< fitter configuration (options and parameter settings)
566+ std::shared_ptr<IModelFunction_v> fFunc_v ; // /<! copy of the fitted function containing on output the fit result
567+ std::shared_ptr<IModelFunction> fFunc ; // /<! copy of the fitted function containing on output the fit result
568+ std::shared_ptr<ROOT::Fit::FitResult> fResult ; // /<! pointer to the object containing the result of the fit
569+ std::shared_ptr<ROOT::Math::Minimizer> fMinimizer ; // /<! pointer to used minimizer
570+ std::shared_ptr<ROOT::Fit::FitData> fData ; // /<! pointer to the fit data (binned or unbinned data)
571+ std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunction ; // /<! pointer to used objective function
572+ const ROOT::Math::IMultiGenFunction *fExtObjFunction = nullptr ; // /<! pointer to an external FCN
576573};
577574
578575
579- // internal functions to get data set and model function from FCN
580- // useful for fits done with customized FCN classes
581- template <class ObjFuncType >
582- bool Fitter::GetDataFromFCN () {
583- const ObjFuncType * objfunc = dynamic_cast <const ObjFuncType*>(ObjFunction ());
584- if (objfunc) {
585- fFunc = objfunc->ModelFunctionPtr ();
586- fData = objfunc->DataPtr ();
587- return true ;
588- }
589- else {
590- return false ;
591- }
592- }
593-
594576#ifdef R__HAS_VECCORE
595577template <class NotCompileIfScalarBackend >
596578void Fitter::SetFunction (const IModelFunction_v &func, bool useGradient)
@@ -633,30 +615,6 @@ void Fitter::SetFunction(const IGradModelFunction_v &func, bool useGradient)
633615}
634616#endif
635617
636- } // end namespace Fit
637-
638- } // end namespace ROOT
639-
640- // implementation of inline methods
641-
642-
643-
644- #include " Math/WrappedFunction.h"
645-
646- template <class Function >
647- bool ROOT::Fit::Fitter::FitFCN (unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
648- ROOT::Math::WrappedMultiFunction<Function &> wf (f,npar);
649- if (!DoSetFCN (false , wf, par, datasize, fitType))
650- return false ;
651- return FitFCN ();
652- }
653- template <class Function >
654- bool ROOT::Fit::Fitter::SetFCN (unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
655- ROOT::Math::WrappedMultiFunction<Function &> wf (f,npar);
656- return DoSetFCN (false , wf, par, datasize, fitType);
657- }
658-
659-
660-
618+ } // namespace ROOT::Fit
661619
662620#endif /* ROOT_Fit_Fitter */
0 commit comments