88 if (
this == &rhs)
return *
this;
114 MATH_WARN_MSG(
"Fitter::SetFunction",
"Requested function does not provide gradient - use it as non-gradient function ");
140 MATH_WARN_MSG(
"Fitter::SetFunction",
"Requested function does not provide gradient - use it as non-gradient function ");
185 unsigned int npar = fcn.
NDim();
187 MATH_ERROR_MSG(
"Fitter::SetFCN",
"FCN function has zero parameters ");
215 if (!
SetFCN(fcn, params, dataSize, chi2fit) )
return false;
218 return (
fFunc !=
nullptr);
233 unsigned int dataSize,
bool chi2fit)
236 if (!
SetFCN(fcn, params, dataSize, chi2fit) )
return false;
238 return (
fFunc !=
nullptr);
269 if (!
SetFCN(fcn, params, dataSize, chi2fit))
278 if (!
SetFCN(fcn, params, dataSize, chi2fit))
299bool Fitter::SetFCN(MinuitFCN_t fcn,
int npar,
const double *params,
unsigned int dataSize,
bool chi2fit)
307 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Fit Parameter settings have not been created ");
313 return SetFCN(newFcn, params, dataSize, chi2fit);
316bool Fitter::FitFCN(MinuitFCN_t fcn,
int npar,
const double *params,
unsigned int dataSize,
bool chi2fit)
320 if (!
SetFCN(fcn, npar, params, dataSize, chi2fit))
331 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
352 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
358 double fcnval = (*fObjFunction)(
fResult->GetParams());
369 std::shared_ptr<BinData> data = std::dynamic_pointer_cast<BinData>(
fData);
374 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"model function is not set");
379 std::cout <<
"Fitter ParamSettings " <<
Config().
ParamsSettings()[3].IsBound() <<
" lower limit "
401 MATH_INFO_MSG(
"Fitter::DoLeastSquareFit",
"use gradient from model function");
404 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
411 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
418 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"wrong type of function - it does not provide gradient");
429 std::shared_ptr<BinData> data = std::dynamic_pointer_cast<BinData>(
fData);
436 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"model function is not set");
446 MATH_INFO_MSG(
"Fitter::DoBinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
486 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
488 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
505 MATH_INFO_MSG(
"Fitter::DoLikelihoodFit",
"use gradient from model function");
507 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
509 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
516 "Not-extended binned fit with gradient not yet supported - do an extended fit");
536 std::shared_ptr<UnBinData> data = std::dynamic_pointer_cast<UnBinData>(
fData);
542 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"model function is not set");
547 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
591 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"use gradient from model function");
592 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
596 "Extended unbinned fit with gradient not yet supported - do a not-extended fit");
609 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
613 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"use gradient from model function");
614 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
618 "Extended unbinned fit with gradient not yet supported - do a not-extended fit");
631 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
640 std::shared_ptr<BinData> data = std::dynamic_pointer_cast<BinData>(
fData);
659 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Objective function has not been set");
666 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Re-computation of Hesse errors not implemented for weighted likelihood fits");
667 MATH_INFO_MSG(
"Fitter::CalculateHessErrors",
"Do the Fit using configure option FitConfig::SetParabErrors()");
688 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"FitResult has not been created");
694 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
700 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Need to do a fit before calculating the errors");
707 if (!ret)
MATH_WARN_MSG(
"Fitter::CalculateHessErrors",
"Error when calculating Hessian");
739 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minimizer does not exist - cannot calculate Minos errors");
744 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Invalid Fit Result - cannot calculate Minos errors");
749 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Computation of MINOS errors not implemented for weighted likelihood fits");
755 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
765 unsigned int n = (ipars.size() > 0) ? ipars.size() :
fResult->Parameters().size();
774 MATH_INFO_MSG(
"Fitter::CalculateMinosErrors",
"Run again Minos for some parameters because a new Minimum has been found");
776 for (
int i = 0; i < iparMax; ++i) {
778 unsigned int index = (ipars.size() > 0) ? ipars[i] : i;
779 bool ret =
fMinimizer->GetMinosError(index, elow, eup);
785 fResult->SetMinosError(index, elow, eup);
789 iparMax = iparNewMin;
792 while( iparNewMin > 0 && iter < 10);
794 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minos error calculation failed for all the selected parameters");
810 static unsigned int NCalls(
const Func & ) {
return 0; }
811 static int Type(
const Func & ) {
return -1; }
812 static bool IsGrad() {
return false; }
818 static bool IsGrad() {
return false; }
824 static bool IsGrad() {
return true; }
835 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong function dimension or wrong size for FitConfig");
843 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"Minimizer cannot be created");
851 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong type of function - it does not provide gradient");
878 if (canDifferentMinim) {
879 std::string msg =
"Using now " + newMinimType;
880 MATH_INFO_MSG(
"Fitter::DoUpdateMinimizerOptions: ", msg.c_str());
885 std::string msg =
"Cannot change minimizer. Continue using " +
fResult->MinimizerType();
886 MATH_WARN_MSG(
"Fitter::DoUpdateMinimizerOptions",msg.c_str());
932 std::cout <<
"ROOT::Fit::Fitter::DoMinimization : ncalls = " <<
fResult->fNCalls <<
" type of objfunc " << fFitFitResType <<
" typeid: " <<
typeid(*fObjFunction).name() <<
" use gradient " <<
fUseGradient << std::endl;
949 fObjFunction = std::unique_ptr<ROOT::Math::IMultiGenFunction> ( objFunc.
Clone() );
958 for (
unsigned int i = 0; i <
fConfig.
NPar(); ++i) {
971 if (fcn) ncalls = fcn->
NCalls();
975 if (fcn) ncalls = fcn->
NCalls();
990 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Must perform first a fit before applying the correction");
994 unsigned int n = loglw2.
NDim();
996 std::vector<double> cov(
n*
n);
997 bool ret =
fMinimizer->GetCovMatrix(&cov[0] );
999 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Previous fit has no valid Covariance matrix");
1003 fObjFunction = std::unique_ptr<ROOT::Math::IMultiGenFunction> ( loglw2.
Clone() );
1016 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error running Hesse on weight2 likelihood - cannot compute errors");
1021 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not accurate, the errors may be not reliable");
1023 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood was forced to be defined positive");
1026 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not valid !");
1040 std::vector<double> hes(
n*
n);
1041 ret =
fMinimizer->GetHessianMatrix(&hes[0] );
1043 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error retrieving Hesse on weight2 likelihood - cannot compute errors");
1059 std::vector<double> tmp(
n*
n);
1060 for (
unsigned int i = 0; i <
n; ++i) {
1061 for (
unsigned int j = 0; j <
n; ++j) {
1062 for (
unsigned int k = 0; k <
n; ++k)
1063 tmp[i*
n+j] += hes[i*
n + k] * cov[k*
n + j];
1067 std::vector<double> newCov(
n*
n);
1068 for (
unsigned int i = 0; i <
n; ++i) {
1069 for (
unsigned int j = 0; j <
n; ++j) {
1070 for (
unsigned int k = 0; k <
n; ++k)
1071 newCov[i*
n+j] += cov[i*
n + k] * tmp[k*
n + j];
1076 for (
unsigned int i = 0; i <
n; ++i) {
1078 for (
unsigned int j = 0; j <= i; ++j)
1079 fResult->fCovMatrix[k++] = newCov[i *
n + j];
#define MATH_INFO_MSG(loc, str)
Pre-processor macro to report messages which can be configured to use ROOT error or simply an std::io...
#define MATH_ERROR_MSG(loc, str)
#define MATH_WARN_MSG(loc, str)
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Chi2FCN class for binnned fits using the least square methods.
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
const std::vector< unsigned int > & MinosParams() const
return vector of parameter indeces for which the Minos Error will be computed
void SetParamsSettings(unsigned int npar, const double *params, const double *vstep=0)
set the parameter settings from number of parameters and a vector of values and optionally step value...
bool UpdateAfterFit() const
Update configuration after a fit using the FitResult.
void SetMinosErrors(bool on=true)
set Minos erros computation to be performed after fitting
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
bool NormalizeErrors() const
flag to check if resulting errors are be normalized according to chi2/ndf
bool ParabErrors() const
do analysis for parabolic errors
unsigned int NPar() const
number of parameters settings
std::string MinimizerName() const
return Minimizer full name (type / algorithm)
bool UseWeightCorrection() const
Apply Weight correction for error matrix computation.
const std::vector< ROOT::Fit::ParameterSettings > & ParamsSettings() const
get the vector of parameter settings (const method)
ROOT::Math::Minimizer * CreateMinimizer()
create a new minimizer according to chosen configuration
void CreateParamsSettings(const ROOT::Math::IParamMultiFunctionTempl< T > &func)
set the parameter settings from a model function.
const std::string & MinimizerType() const
return type of minimizer package
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
bool MinosErrors() const
do minos errros analysis on the parameters
class containg the result of the fit and all the related information (fitted parameter values,...
Fitter class, entry point for performing all type of fits.
bool EvalFCN()
Perform a simple FCN evaluation.
bool FitFCN()
Perform a fit with the previously set FCN function.
std::shared_ptr< ROOT::Math::Minimizer > fMinimizer
pointer to the object containing the result of the fit
bool DoBinnedLikelihoodFit(bool extended=true, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
binned likelihood fit
std::shared_ptr< ROOT::Fit::FitData > fData
pointer to used minimizer
bool SetFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Set a generic FCN function as a C++ callable object implementing double () (const double *) Note that...
bool DoMinimization(const BaseFunc &f, const ROOT::Math::IMultiGenFunction *chifunc=0)
do minimization
std::shared_ptr< ROOT::Math::IMultiGenFunction > fObjFunction
pointer to the fit data (binned or unbinned data)
bool ApplyWeightCorrection(const ROOT::Math::IMultiGenFunction &loglw2, bool minimizeW2L=false)
apply correction in the error matrix for the weights for likelihood fits This method can be called on...
void ExamineFCN()
look at the user provided FCN and get data and model function is they derive from ROOT::Fit FCN class...
const FitConfig & Config() const
access to the fit configuration (const method)
std::shared_ptr< IModelFunction_v > fFunc_v
bool DoUnbinnedLikelihoodFit(bool extended=false, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
un-binned likelihood fit
Fitter & operator=(const Fitter &rhs)
Assignment operator (disabled, class is not copyable)
std::shared_ptr< ROOT::Fit::FitResult > fResult
copy of the fitted function containing on output the fit result
bool GetDataFromFCN()
internal functions to get data set and model function from FCN useful for fits done with customized F...
bool CalculateMinosErrors()
perform an error analysis on the result using MINOS To be called only after fitting and when a minimi...
bool DoUpdateMinimizerOptions(bool canDifferentMinim=true)
void SetFunction(const IModelFunction &func, bool useGradient=false)
Set the fitted function (model function) from a parametric function interface.
bool CalculateHessErrors()
perform an error analysis on the result using the Hessian Errors are obtaied from the inverse of the ...
Fitter()
Default constructor.
std::shared_ptr< IModelFunction > fFunc
copy of the fitted function containing on output the fit result
bool DoLinearFit()
linear least square fit
bool DoLeastSquareFit(const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
least square fit
LogLikelihoodFCN class for likelihood fits.
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
void UseSumOfWeightSquare(bool on=true)
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
void SetValue(double val)
set the value
void SetStepSize(double err)
set the step size
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
void UseSumOfWeightSquare(bool on=true)
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
virtual Type_t Type() const
return the type of method, override if needed
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
virtual unsigned int NCalls() const
return the total number of function calls (overrided if needed)
Documentation for the abstract class IBaseFunctionMultiDim.
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
double ErrorDef() const
error definition
int PrintLevel() const
non-static methods for retrieving options
void SetErrorDef(double err)
set error def
static double DefaultErrorDef()
MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in a multi dimension...
MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in a mu...
Type
enumeration specifying the integration types.
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction
BasicFitMethodFunction< ROOT::Math::IMultiGradFunction > FitMethodGradFunction
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...