Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions math/mathcore/inc/Fit/FitResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
#include <cmath>
#include <memory>

namespace ROOT {
namespace ROOT::Math {
class Minimizer;
}

namespace Math {
class Minimizer;
}


namespace Fit {
namespace ROOT::Fit {

class FitConfig;
class FitData;
class BinData;
class FitConfig;
class FitData;
class BinData;

//___________________________________________________________________________________
/**
Expand All @@ -53,7 +50,7 @@ class FitResult {
/**
Default constructor for an empty (non valid) fit result
*/
FitResult ();
FitResult() = default;

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

// default copy constructor and assignment can be used

/**
Destructor
*/
virtual ~FitResult () {}


Expand Down Expand Up @@ -340,22 +334,22 @@ class FitResult {
friend class Fitter;


bool fValid; ///< flag for indicating valid fit
bool fNormalized; ///< flag for indicating is errors are normalized
unsigned int fNFree; ///< number of fit free parameters (total parameters are in size of parameter vector)
unsigned int fNdf; ///< number of degree of freedom
unsigned int fNCalls; ///< number of function calls
int fStatus; ///< minimizer status code
int fCovStatus; ///< covariance matrix status code
double fVal; ///< minimum function value
double fEdm; ///< expected distance from minimum
double fChi2; ///< fit chi2 value (different than fval in case of chi2 fits)
bool fValid = false; ///< flag for indicating valid fit
bool fNormalized = false; ///< flag for indicating is errors are normalized
unsigned int fNFree = 0; ///< number of fit free parameters (total parameters are in size of parameter vector)
unsigned int fNdf = 0; ///< number of degree of freedom
unsigned int fNCalls = 0; ///< number of function calls
int fStatus = -1; ///< minimizer status code
int fCovStatus = 0; ///< covariance matrix status code
double fVal = 0; ///< minimum function value
double fEdm = -1; ///< expected distance from minimum
double fChi2 = -1; ///< fit chi2 value (different than fval in case of chi2 fits)
std::shared_ptr<ROOT::Math::Minimizer> fMinimizer; ///<! minimizer object used for fitting
std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunc; ///<! objective function used for fitting
std::shared_ptr<IModelFunction> fFitFunc; ///<! model function resulting from the fit.
std::shared_ptr<FitData> fFitData; ///<! data set used in the fit
std::map<unsigned int, bool> fFixedParams; ///< list of fixed parameters
std::map<unsigned int, unsigned int> fBoundParams; ///< list of limited parameters
std::vector<bool> fFixedParams; ///< if parameters are fixed
std::vector<unsigned int> fBoundParams; ///< if parameters are limited
std::vector<std::pair<double,double> > fParamBounds; ///< parameter bounds
std::vector<double> fParams; ///< parameter values. Size is total number of parameters
std::vector<double> fErrors; ///< errors
Expand All @@ -367,13 +361,6 @@ class FitResult {

};


} // end namespace Fit

} // end namespace ROOT




} // namespace ROOT::Fit

#endif /* ROOT_Fit_FitResult */
132 changes: 45 additions & 87 deletions math/mathcore/inc/Fit/Fitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* *
**********************************************************************/

// Header file for class Fitter

#ifndef ROOT_Fit_Fitter
#define ROOT_Fit_Fitter

Expand All @@ -25,34 +23,35 @@ Classes used for fitting (regression analysis) and estimation of parameter value
*/

#include "Fit/BinData.h"
#include "Fit/UnBinData.h"
#include "Fit/FitConfig.h"
#include "ROOT/EExecutionPolicy.hxx"
#include "Fit/FitResult.h"
#include "Fit/UnBinData.h"
#include "Math/IParamFunction.h"
#include <memory>
#include "Math/WrappedFunction.h"
#include "ROOT/EExecutionPolicy.hxx"

namespace ROOT {
#include <memory>

namespace ROOT::Math {

namespace Math {
class Minimizer;
class Minimizer;

// should maybe put this in a FitMethodFunctionfwd file
template<class FunctionType> class BasicFitMethodFunction;
// should maybe put this in a FitMethodFunctionfwd file
template <class FunctionType>
class BasicFitMethodFunction;

// define the normal and gradient function
typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction> FitMethodFunction;
typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;
// define the normal and gradient function
typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction> FitMethodFunction;
typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;

}
} // namespace ROOT::Math

/**
Namespace for the fitting classes
@ingroup Fit
*/

namespace Fit {
namespace ROOT::Fit {

/**
@defgroup FitMain User Fitting classes
Expand Down Expand Up @@ -265,7 +264,9 @@ class Fitter {
For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
*/
template <class Function>
bool FitFCN(unsigned int npar, Function & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0);
bool FitFCN(unsigned int npar, Function & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0) {
return DoSetFCN(false, ROOT::Math::WrappedMultiFunction<Function &>{fcn, npar}, params, dataSize, fitType) ? FitFCN() : false;
}

/**
Set a generic FCN function as a C++ callable object implementing
Expand All @@ -274,7 +275,9 @@ class Fitter {
For the options see documentation for following methods FitFCN(IMultiGenFunction & fcn,..)
*/
template <class Function>
bool SetFCN(unsigned int npar, Function & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0);
bool SetFCN(unsigned int npar, Function & fcn, const double * params = nullptr, unsigned int dataSize = 0, int fitType = 0) {
return DoSetFCN(false, ROOT::Math::WrappedMultiFunction<Function &>{fcn, npar}, params, dataSize, fitType);
}

/**
Fit using the given FCN function represented by a multi-dimensional function interface
Expand Down Expand Up @@ -527,70 +530,49 @@ class Fitter {
/// Set the input data for the fit (Copying the given data object)
template <class Data>
void SetData(const Data & data) {
auto dataClone = std::make_shared<Data>(data);
SetData(dataClone);
SetData(std::make_shared<Data>(data));
}

/// internal functions to get data set and model function from FCN
/// useful for fits done with customized FCN classes
template <class ObjFuncType>
bool GetDataFromFCN();
bool GetDataFromFCN() {
if (const ObjFuncType *objfunc = dynamic_cast<const ObjFuncType *>(ObjFunction())) {
fFunc = objfunc->ModelFunctionPtr();
fData = objfunc->DataPtr();
return true;
}
return false;
}

/// Return pointer to the used objective function for fitting.
/// If using an external function (e.g. given in SetFCN), return the cached pointer,
/// otherwise use the one stored as shared ptr and managed by the Fitter class
const ROOT::Math::IBaseFunctionMultiDimTempl<double> * ObjFunction() const {
// need to specify here full return type since when using the typedef (IMultiGenFunction)
// there is an error when using the class in Python (see issue #12391)
return (fExtObjFunction) ? fExtObjFunction : fObjFunction.get();
return fExtObjFunction ? fExtObjFunction : fObjFunction.get();
}

private:

bool fUseGradient = false; ///< flag to indicate if using gradient or not

bool fBinFit = false; ///< flag to indicate if fit is binned
///< in case of false the fit is unbinned or undefined)
///< flag it is used to compute chi2 for binned likelihood fit

int fFitType = 0; ///< type of fit (0 undefined, 1 least square, 2 likelihood, 3 binned likelihood)

int fDataSize = 0; ///< size of data sets (need for Fumili or LM fitters)

FitConfig fConfig; ///< fitter configuration (options and parameter settings)

std::shared_ptr<IModelFunction_v> fFunc_v; ///<! copy of the fitted function containing on output the fit result

std::shared_ptr<IModelFunction> fFunc; ///<! copy of the fitted function containing on output the fit result

std::shared_ptr<ROOT::Fit::FitResult> fResult; ///<! pointer to the object containing the result of the fit

std::shared_ptr<ROOT::Math::Minimizer> fMinimizer; ///<! pointer to used minimizer

std::shared_ptr<ROOT::Fit::FitData> fData; ///<! pointer to the fit data (binned or unbinned data)

std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunction; ///<! pointer to used objective function

const ROOT::Math::IMultiGenFunction * fExtObjFunction = nullptr; ///<! pointer to an external FCN

bool fUseGradient = false; ///< flag to indicate if using gradient or not
bool fBinFit = false; ///< flag to indicate if fit is binned
///< in case of false the fit is unbinned or undefined)
///< flag it is used to compute chi2 for binned likelihood fit
int fFitType = 0; ///< type of fit (0 undefined, 1 least square, 2 likelihood, 3 binned likelihood)
int fDataSize = 0; ///< size of data sets (need for Fumili or LM fitters)
FitConfig fConfig; ///< fitter configuration (options and parameter settings)
std::shared_ptr<IModelFunction_v> fFunc_v; ///<! copy of the fitted function containing on output the fit result
std::shared_ptr<IModelFunction> fFunc; ///<! copy of the fitted function containing on output the fit result
std::shared_ptr<ROOT::Fit::FitResult> fResult; ///<! pointer to the object containing the result of the fit
std::shared_ptr<ROOT::Math::Minimizer> fMinimizer; ///<! pointer to used minimizer
std::shared_ptr<ROOT::Fit::FitData> fData; ///<! pointer to the fit data (binned or unbinned data)
std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunction; ///<! pointer to used objective function
const ROOT::Math::IMultiGenFunction *fExtObjFunction = nullptr; ///<! pointer to an external FCN
};


// internal functions to get data set and model function from FCN
// useful for fits done with customized FCN classes
template <class ObjFuncType>
bool Fitter::GetDataFromFCN() {
const ObjFuncType * objfunc = dynamic_cast<const ObjFuncType*>(ObjFunction());
if (objfunc) {
fFunc = objfunc->ModelFunctionPtr();
fData = objfunc->DataPtr();
return true;
}
else {
return false;
}
}

#ifdef R__HAS_VECCORE
template <class NotCompileIfScalarBackend>
void Fitter::SetFunction(const IModelFunction_v &func, bool useGradient)
Expand Down Expand Up @@ -633,30 +615,6 @@ void Fitter::SetFunction(const IGradModelFunction_v &func, bool useGradient)
}
#endif

} // end namespace Fit

} // end namespace ROOT

// implementation of inline methods



#include "Math/WrappedFunction.h"

template<class Function>
bool ROOT::Fit::Fitter::FitFCN(unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
ROOT::Math::WrappedMultiFunction<Function &> wf(f,npar);
if (!DoSetFCN(false, wf, par, datasize, fitType))
return false;
return FitFCN();
}
template<class Function>
bool ROOT::Fit::Fitter::SetFCN(unsigned int npar, Function & f, const double * par, unsigned int datasize,int fitType) {
ROOT::Math::WrappedMultiFunction<Function &> wf(f,npar);
return DoSetFCN(false, wf, par, datasize, fitType);
}



} // namespace ROOT::Fit

#endif /* ROOT_Fit_Fitter */
13 changes: 2 additions & 11 deletions math/mathcore/inc/Fit/ParameterSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
* *
**********************************************************************/

// Header file for class ParameterSettings

#ifndef ROOT_Fit_ParameterSettings
#define ROOT_Fit_ParameterSettings

#include <string>

namespace ROOT {

namespace Fit {

namespace ROOT::Fit {

//___________________________________________________________________________________
/**
Expand Down Expand Up @@ -155,12 +150,8 @@ class ParameterSettings {
bool fHasUpperLimit = false; ///< flag to control upper parameter limit

std::string fName; ///< parameter name

};

} // end namespace Fit

} // end namespace ROOT

} // namespace ROOT::Fit

#endif /* ROOT_Fit_ParameterSettings */
Loading
Loading