70 MATH_WARN_MSG(
"Fitter::SetFunction",
"Requested function does not provide gradient - use it as non-gradient function ");
96 MATH_WARN_MSG(
"Fitter::SetFunction",
"Requested function does not provide gradient - use it as non-gradient function ");
142 unsigned int npar = fcn.
NDim();
144 MATH_ERROR_MSG(
"Fitter::SetFCN",
"FCN function has zero parameters ");
180 return DoSetFCN(
true, fcn, params, dataSize, chi2fit);
184 if (!
SetFCN(fcn, params, dataSize, chi2fit) )
return false;
187 return (
fFunc !=
nullptr);
202 unsigned int dataSize,
bool chi2fit)
205 if (!
SetFCN(fcn, params, dataSize, chi2fit) )
return false;
207 return (
fFunc !=
nullptr);
238 if (!
SetFCN(fcn, params, dataSize, chi2fit))
247 if (!
SetFCN(fcn, params, dataSize, chi2fit))
268bool Fitter::SetFCN(MinuitFCN_t fcn,
int npar,
const double *params,
unsigned int dataSize,
bool chi2fit)
276 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Fit Parameter settings have not been created ");
282 return DoSetFCN(
false,newFcn, params, dataSize, chi2fit);
285bool Fitter::FitFCN(MinuitFCN_t fcn,
int npar,
const double *params,
unsigned int dataSize,
bool chi2fit)
289 if (!
SetFCN(fcn, npar, params, dataSize, chi2fit))
301 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
322 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
339 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
344 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"model function is not set");
349 std::cout <<
"Fitter ParamSettings " <<
Config().
ParamsSettings()[3].IsBound() <<
" lower limit "
367 MATH_INFO_MSG(
"Fitter::DoLeastSquareFit",
"use gradient from model function");
370 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
375 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
380 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"wrong type of function - it does not provide gradient");
391 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
398 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"model function is not set");
408 MATH_INFO_MSG(
"Fitter::DoBinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
420 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseFunc, IModelFunction_v>>(
data,
fFunc_v, useWeight, extended, executionPolicy);
425 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseFunc>>(
data,
fFunc, useWeight, extended, executionPolicy);
430 MATH_INFO_MSG(
"Fitter::DoLikelihoodFit",
"use gradient from model function");
434 "Not-extended binned fit with gradient not yet supported - do an extended fit");
440 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
442 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
445 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseGradFunc, IModelFunction_v>>(
data, gradFun, useWeight, extended, executionPolicy);
452 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
454 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
458 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseGradFunc>>(
data, gradFun, useWeight, extended, executionPolicy);
469 std::shared_ptr<UnBinData>
data = std::dynamic_pointer_cast<UnBinData>(
fData);
475 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"model function is not set");
480 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
501 auto logl = std::make_unique<LogLikelihoodFCN<BaseFunc, IModelFunction_v>>(
data,
fFunc_v, useWeight, extended, executionPolicy);
505 auto logl = std::make_unique<LogLikelihoodFCN<BaseFunc>>(
data,
fFunc, useWeight, extended, executionPolicy);
511 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"use gradient from model function");
514 "Extended unbinned fit with gradient not yet supported - do a not-extended fit");
518 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
520 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
523 auto logl = std::make_unique<LogLikelihoodFCN<BaseGradFunc, IModelFunction_v>>(
data, gradFun, useWeight, extended, executionPolicy);
526 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
528 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
531 auto logl = std::make_unique<LogLikelihoodFCN<BaseGradFunc>>(
data, gradFun, useWeight, extended, executionPolicy);
541 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
560 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Objective function has not been set");
567 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Re-computation of Hesse errors not implemented for weighted likelihood fits");
568 MATH_INFO_MSG(
"Fitter::CalculateHessErrors",
"Do the Fit using configure option FitConfig::SetParabErrors()");
574 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"FitResult has not been created");
580 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
586 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Need to do a fit before calculating the errors");
593 if (!ret)
MATH_WARN_MSG(
"Fitter::CalculateHessErrors",
"Error when calculating Hessian");
627 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minimizer does not exist - cannot calculate Minos errors");
632 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Invalid Fit Result - cannot calculate Minos errors");
637 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Computation of MINOS errors not implemented for weighted likelihood fits");
643 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
653 unsigned int n = (ipars.size() > 0) ? ipars.size() :
fResult->Parameters().size();
662 MATH_INFO_MSG(
"Fitter::CalculateMinosErrors",
"Run again Minos for some parameters because a new Minimum has been found");
664 for (
int i = 0; i < iparMax; ++i) {
666 unsigned int index = (ipars.size() > 0) ? ipars[i] : i;
677 iparMax = iparNewMin;
680 while( iparNewMin > 0 && iter < 10);
682 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minos error calculation failed for all the selected parameters");
701 static unsigned int NCalls(
const Func & ) {
return 0; }
702 static int Type(
const Func & ) {
return -1; }
724 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"Objective function has not been set");
730 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong function dimension or wrong size for FitConfig");
738 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"Minimizer cannot be created");
746 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong type of function - it does not provide gradient");
751 if (
Config().MinimizerType() ==
"Minuit2") {
755 auto hessFcn = [=](
const std::vector<double> &
x,
double *hess) {
756 unsigned int ndim =
x.size();
757 unsigned int nh = ndim * (ndim + 1) / 2;
758 std::vector<double>
h(nh);
759 bool ret = fitGradFcn->
Hessian(
x.data(),
h.data());
760 if (!ret)
return false;
761 for (
unsigned int i = 0; i < ndim; i++) {
762 for (
unsigned int j = 0; j <= i; j++) {
763 unsigned int index = j + i * (i + 1) / 2;
764 hess[ndim * i + j] =
h[
index];
766 hess[ndim * j + i] =
h[
index];
798 if (canDifferentMinim) {
799 std::string msg =
"Using now " + newMinimType;
800 MATH_INFO_MSG(
"Fitter::DoUpdateMinimizerOptions: ", msg.c_str());
805 std::string msg =
"Cannot change minimizer. Continue using " +
fResult->MinimizerType();
806 MATH_WARN_MSG(
"Fitter::DoUpdateMinimizerOptions",msg.c_str());
850 std::cout <<
"ROOT::Fit::Fitter::DoMinimization : ncalls = " <<
fResult->fNCalls <<
" type of objfunc " << fFitFitResType <<
" typeid: " <<
typeid(*fObjFunction).name() <<
" use gradient " <<
fUseGradient << std::endl;
861template<
class ObjFunc_t>
870template<
class ObjFunc_t>
877 std::shared_ptr<ObjFunc_t> sObjFunc{ std::move(objFunc)};
881 sObjFunc->UseSumOfWeightSquare();
889 for (
unsigned int i = 0; i <
fConfig.
NPar(); ++i) {
902 if (fcn) ncalls = fcn->
NCalls();
906 if (fcn) ncalls = fcn->
NCalls();
921 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Must perform first a fit before applying the correction");
925 unsigned int n = loglw2.
NDim();
927 std::vector<double> cov(
n*
n);
928 bool ret =
fMinimizer->GetCovMatrix(&cov[0] );
930 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Previous fit has no valid Covariance matrix");
934 std::shared_ptr<ROOT::Math::IMultiGenFunction> objFunc(loglw2.
Clone());
949 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error running Hesse on weight2 likelihood - cannot compute errors");
954 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not accurate, the errors may be not reliable");
956 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood was forced to be defined positive");
959 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not valid !");
963 std::vector<double> hes(
n*
n);
966 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error retrieving Hesse on weight2 likelihood - cannot compute errors");
974 std::vector<double> tmp(
n*
n);
975 for (
unsigned int i = 0; i <
n; ++i) {
976 for (
unsigned int j = 0; j <
n; ++j) {
977 for (
unsigned int k = 0; k <
n; ++k)
978 tmp[i*
n+j] += hes[i*
n + k] * cov[k*
n + j];
982 std::vector<double> newCov(
n*
n);
983 for (
unsigned int i = 0; i <
n; ++i) {
984 for (
unsigned int j = 0; j <
n; ++j) {
985 for (
unsigned int k = 0; k <
n; ++k)
986 newCov[i*
n+j] += cov[i*
n + k] * tmp[k*
n + j];
991 for (
unsigned int i = 0; i <
n; ++i) {
992 fResult->fErrors[i] = std::sqrt( newCov[i*(
n+1)] );
993 for (
unsigned int j = 0; j <= i; ++j)
994 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)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Chi2FCN class for binned fits using the least square methods.
const std::vector< unsigned int > & MinosParams() const
return vector of parameter indices for which the Minos Error will be computed
bool UpdateAfterFit() const
Update configuration after a fit using the FitResult.
void SetMinimizer(const char *type, const char *algo=nullptr)
set minimizer type
void SetMinosErrors(bool on=true)
set Minos erros computation to be performed after fitting
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
void SetParamsSettings(unsigned int npar, const double *params, const double *vstep=nullptr)
set the parameter settings from number of parameters and a vector of values and optionally step value...
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 errors analysis on the parameters
class containing the result of the fit and all the related information (fitted parameter values,...
bool DoSetFCN(bool useExtFCN, const ROOT::Math::IMultiGenFunction &fcn, const double *params, unsigned int dataSize, bool chi2fit)
Set Objective function.
bool EvalFCN()
Perform a simple FCN evaluation.
const ROOT::Math::IMultiGenFunction * fExtObjFunction
! pointer to an external FCN
bool FitFCN()
Perform a fit with the previously set FCN function.
bool DoMinimization(std::unique_ptr< ObjFunc_t > f, const ROOT::Math::IMultiGenFunction *chifunc=nullptr)
do minimization
int fDataSize
size of data sets (need for Fumili or LM fitters)
bool DoUnbinnedLikelihoodFit(bool extended=false, const ROOT::EExecutionPolicy &executionPolicy=ROOT::EExecutionPolicy::kSequential)
un-binned likelihood fit
const ROOT::Math::IBaseFunctionMultiDimTempl< double > * ObjFunction() const
Return pointer to the used objective function for fitting.
std::shared_ptr< ROOT::Math::Minimizer > fMinimizer
! pointer to used minimizer
bool DoWeightMinimization(std::unique_ptr< ObjFunc_t > f, const ROOT::Math::IMultiGenFunction *chifunc=nullptr)
bool DoBinnedLikelihoodFit(bool extended=true, const ROOT::EExecutionPolicy &executionPolicy=ROOT::EExecutionPolicy::kSequential)
binned likelihood fit
int fFitType
type of fit (0 undefined, 1 least square, 2 likelihood)
std::shared_ptr< ROOT::Fit::FitData > fData
! pointer to the fit data (binned or unbinned data)
bool fUseGradient
flag to indicate if using gradient or not
bool fBinFit
flag to indicate if fit is binned in case of false the fit is unbinned or undefined) flag it is used ...
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...
std::shared_ptr< ROOT::Math::IMultiGenFunction > fObjFunction
! pointer to used objective function
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)
bool DoLeastSquareFit(const ROOT::EExecutionPolicy &executionPolicy=ROOT::EExecutionPolicy::kSequential)
least square fit
std::shared_ptr< IModelFunction_v > fFunc_v
! 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
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 obtained from the inverse of the...
FitConfig fConfig
fitter configuration (options and parameter settings)
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
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
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 bool Hessian(const double *x, double *hess) const
Computes the full Hessian.
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
virtual bool HasHessian() const
virtual unsigned int NCalls() const
return the total number of function calls (override 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...
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static int Type(const ROOT::Math::FitMethodFunction &f)
static unsigned int NCalls(const ROOT::Math::FitMethodFunction &f)
static int Type(const ROOT::Math::FitMethodGradFunction &f)
static unsigned int NCalls(const ROOT::Math::FitMethodGradFunction &f)
static unsigned int NCalls(const Func &)
static int Type(const Func &)