Skip to content

Commit 498e815

Browse files
committed
[Math] Some code modernization of the Fitter classes
This is to keep the net number of added lines of code below zero, when combining with the previous commit.
1 parent a3f7734 commit 498e815

File tree

6 files changed

+105
-240
lines changed

6 files changed

+105
-240
lines changed

math/mathcore/inc/Fit/FitResult.h

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
#include <cmath>
2323
#include <memory>
2424

25-
namespace ROOT {
25+
namespace ROOT::Math {
26+
class Minimizer;
27+
}
2628

27-
namespace Math {
28-
class Minimizer;
29-
}
30-
31-
32-
namespace Fit {
29+
namespace ROOT::Fit {
3330

34-
class FitConfig;
35-
class FitData;
36-
class BinData;
31+
class FitConfig;
32+
class FitData;
33+
class BinData;
3734

3835
//___________________________________________________________________________________
3936
/**
@@ -53,7 +50,7 @@ class FitResult {
5350
/**
5451
Default constructor for an empty (non valid) fit result
5552
*/
56-
FitResult ();
53+
FitResult() = default;
5754

5855
/**
5956
Constructor from a fit-config for a dummy fit
@@ -63,9 +60,6 @@ class FitResult {
6360

6461
// default copy constructor and assignment can be used
6562

66-
/**
67-
Destructor
68-
*/
6963
virtual ~FitResult () {}
7064

7165

@@ -340,16 +334,16 @@ class FitResult {
340334
friend class Fitter;
341335

342336

343-
bool fValid; ///< flag for indicating valid fit
344-
bool fNormalized; ///< flag for indicating is errors are normalized
345-
unsigned int fNFree; ///< number of fit free parameters (total parameters are in size of parameter vector)
346-
unsigned int fNdf; ///< number of degree of freedom
347-
unsigned int fNCalls; ///< number of function calls
348-
int fStatus; ///< minimizer status code
349-
int fCovStatus; ///< covariance matrix status code
350-
double fVal; ///< minimum function value
351-
double fEdm; ///< expected distance from minimum
352-
double fChi2; ///< fit chi2 value (different than fval in case of chi2 fits)
337+
bool fValid = false; ///< flag for indicating valid fit
338+
bool fNormalized = false; ///< flag for indicating is errors are normalized
339+
unsigned int fNFree = 0; ///< number of fit free parameters (total parameters are in size of parameter vector)
340+
unsigned int fNdf = 0; ///< number of degree of freedom
341+
unsigned int fNCalls = 0; ///< number of function calls
342+
int fStatus = -1; ///< minimizer status code
343+
int fCovStatus = 0; ///< covariance matrix status code
344+
double fVal = 0; ///< minimum function value
345+
double fEdm = -1; ///< expected distance from minimum
346+
double fChi2 = -1; ///< fit chi2 value (different than fval in case of chi2 fits)
353347
std::shared_ptr<ROOT::Math::Minimizer> fMinimizer; ///<! minimizer object used for fitting
354348
std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunc; ///<! objective function used for fitting
355349
std::shared_ptr<IModelFunction> fFitFunc; ///<! model function resulting from the fit.
@@ -367,13 +361,6 @@ class FitResult {
367361

368362
};
369363

370-
371-
} // end namespace Fit
372-
373-
} // end namespace ROOT
374-
375-
376-
377-
364+
} // namespace ROOT::Fit
378365

379366
#endif /* ROOT_Fit_FitResult */

math/mathcore/inc/Fit/Fitter.h

Lines changed: 45 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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

548557
private:
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
595577
template <class NotCompileIfScalarBackend>
596578
void 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 */

math/mathcore/inc/Fit/ParameterSettings.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
* *
99
**********************************************************************/
1010

11-
// Header file for class ParameterSettings
12-
1311
#ifndef ROOT_Fit_ParameterSettings
1412
#define ROOT_Fit_ParameterSettings
1513

1614
#include <string>
1715

18-
namespace ROOT {
19-
20-
namespace Fit {
21-
16+
namespace ROOT::Fit {
2217

2318
//___________________________________________________________________________________
2419
/**
@@ -155,12 +150,8 @@ class ParameterSettings {
155150
bool fHasUpperLimit = false; ///< flag to control upper parameter limit
156151

157152
std::string fName; ///< parameter name
158-
159153
};
160154

161-
} // end namespace Fit
162-
163-
} // end namespace ROOT
164-
155+
} // namespace ROOT::Fit
165156

166157
#endif /* ROOT_Fit_ParameterSettings */

0 commit comments

Comments
 (0)