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 ");
147 if (params !=
nullptr ||
fConfig.ParamsSettings().size() == 0)
148 fConfig.SetParamsSettings(npar, params);
150 if (
fConfig.ParamsSettings().size() != npar) {
181 return DoSetFCN(
true, fcn, params, dataSize, fitType);
185 if (!
SetFCN(fcn, params, dataSize, fitType) )
return false;
199 int fitType =
static_cast<int>(fcn.
Type());
210 int fitType =
static_cast<int>(fcn.
Type());
221 if (!
SetFCN(fcn, params, dataSize, fitType))
248 npar =
fConfig.ParamsSettings().size();
250 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Fit Parameter settings have not been created ");
256 return DoSetFCN(
false,newFcn, params, dataSize, fitType);
263 if (!
SetFCN(fcn, npar, params, dataSize, fitType))
275 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
296 MATH_ERROR_MSG(
"Fitter::FitFCN",
"Objective function has not been set");
313 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
318 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"model function is not set");
323 std::cout <<
"Fitter ParamSettings " <<
Config().
ParamsSettings()[3].IsBound() <<
" lower limit "
340 if (
fConfig.MinimizerOptions().PrintLevel() > 0)
341 MATH_INFO_MSG(
"Fitter::DoLeastSquareFit",
"use gradient from model function");
344 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
349 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
354 MATH_ERROR_MSG(
"Fitter::DoLeastSquareFit",
"wrong type of function - it does not provide gradient");
365 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
368 bool useWeight =
fConfig.UseWeightCorrection();
372 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"model function is not set");
378 fConfig.MinimizerOptions().SetErrorDef(0.5);
381 if (useWeight &&
fConfig.MinosErrors()) {
382 MATH_INFO_MSG(
"Fitter::DoBinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
394 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseFunc, IModelFunction_v>>(
data,
fFunc_v, useWeight, extended, executionPolicy);
399 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseFunc>>(
data,
fFunc, useWeight, extended, executionPolicy);
403 if (
fConfig.MinimizerOptions().PrintLevel() > 0)
404 MATH_INFO_MSG(
"Fitter::DoLikelihoodFit",
"use gradient from model function");
408 "Not-extended binned fit with gradient not yet supported - do an extended fit");
414 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
416 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
419 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseGradFunc, IModelFunction_v>>(
data, gradFun, useWeight, extended, executionPolicy);
426 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
428 MATH_ERROR_MSG(
"Fitter::DoBinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
432 auto logl = std::make_unique<PoissonLikelihoodFCN<BaseGradFunc>>(
data, gradFun, useWeight, extended, executionPolicy);
443 std::shared_ptr<UnBinData>
data = std::dynamic_pointer_cast<UnBinData>(
fData);
446 bool useWeight =
fConfig.UseWeightCorrection();
449 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"model function is not set");
453 if (useWeight &&
fConfig.MinosErrors() ) {
454 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"MINOS errors cannot be computed in weighted likelihood fits");
469 fConfig.MinimizerOptions().SetErrorDef(0.5);
475 auto logl = std::make_unique<LogLikelihoodFCN<BaseFunc, IModelFunction_v>>(
data,
fFunc_v, useWeight, extended, executionPolicy);
479 auto logl = std::make_unique<LogLikelihoodFCN<BaseFunc>>(
data,
fFunc, useWeight, extended, executionPolicy);
484 if (
fConfig.MinimizerOptions().PrintLevel() > 0)
485 MATH_INFO_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"use gradient from model function");
488 "Extended unbinned fit with gradient not yet supported - do a not-extended fit");
492 std::shared_ptr<IGradModelFunction_v> gradFun = std::dynamic_pointer_cast<IGradModelFunction_v>(
fFunc_v);
494 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
497 auto logl = std::make_unique<LogLikelihoodFCN<BaseGradFunc, IModelFunction_v>>(
data, gradFun, useWeight, extended, executionPolicy);
500 std::shared_ptr<IGradModelFunction> gradFun = std::dynamic_pointer_cast<IGradModelFunction>(
fFunc);
502 MATH_ERROR_MSG(
"Fitter::DoUnbinnedLikelihoodFit",
"wrong type of function - it does not provide gradient");
505 auto logl = std::make_unique<LogLikelihoodFCN<BaseGradFunc>>(
data, gradFun, useWeight, extended, executionPolicy);
515 std::shared_ptr<BinData>
data = std::dynamic_pointer_cast<BinData>(
fData);
519 std::string prevminimizer =
fConfig.MinimizerType();
520 fConfig.SetMinimizer(
"Linear");
525 fConfig.SetMinimizer(prevminimizer.c_str());
534 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Objective function has not been set");
541 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Re-computation of Hesse errors not implemented for weighted likelihood fits");
542 MATH_INFO_MSG(
"Fitter::CalculateHessErrors",
"Do the Fit using configure option FitConfig::SetParabErrors()");
548 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"FitResult has not been created");
554 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
560 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Need to do a fit before calculating the errors");
567 if (!ret)
MATH_WARN_MSG(
"Fitter::CalculateHessErrors",
"Error when calculating Hessian");
601 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minimizer does not exist - cannot calculate Minos errors");
606 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Invalid Fit Result - cannot calculate Minos errors");
611 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Computation of MINOS errors not implemented for weighted likelihood fits");
617 MATH_ERROR_MSG(
"Fitter::CalculateHessErrors",
"Error re-initializing the minimizer");
626 const std::vector<unsigned int> & ipars =
fConfig.MinosParams();
627 unsigned int n = (!ipars.empty()) ? ipars.size() :
fResult->Parameters().size();
636 MATH_INFO_MSG(
"Fitter::CalculateMinosErrors",
"Run again Minos for some parameters because a new Minimum has been found");
638 for (
int i = 0;
i < iparMax; ++
i) {
640 unsigned int index = (!ipars.empty()) ? ipars[
i] :
i;
651 iparMax = iparNewMin;
654 while( iparNewMin > 0 && iter < 10);
656 MATH_ERROR_MSG(
"Fitter::CalculateMinosErrors",
"Minos error calculation failed for all the selected parameters");
675 static unsigned int NCalls(
const Func & ) {
return 0; }
676 static int Type(
const Func & ) {
return -1; }
698 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"Objective function has not been set");
703 if (
fConfig.ParamsSettings().size() != objFunction->NDim() ) {
704 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong function dimension or wrong size for FitConfig");
710 fMinimizer = std::shared_ptr<ROOT::Math::Minimizer> (
fConfig.CreateMinimizer() );
712 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"Minimizer cannot be created");
720 MATH_ERROR_MSG(
"Fitter::DoInitMinimizer",
"wrong type of function - it does not provide gradient");
725 if (
Config().MinimizerType() ==
"Minuit2") {
729 auto hessFcn = [=](
const std::vector<double> &
x,
double *hess) {
730 unsigned int ndim =
x.size();
731 unsigned int nh = ndim * (ndim + 1) / 2;
732 std::vector<double>
h(nh);
733 bool ret = fitGradFcn->
Hessian(
x.data(),
h.data());
734 if (!ret)
return false;
735 for (
unsigned int i = 0;
i < ndim;
i++) {
736 for (
unsigned int j = 0; j <=
i; j++) {
737 unsigned int index = j +
i * (
i + 1) / 2;
769 std::string newMinimType =
fConfig.MinimizerName();
772 if (canDifferentMinim) {
773 std::string msg =
"Using now " + newMinimType;
774 MATH_INFO_MSG(
"Fitter::DoUpdateMinimizerOptions: ", msg.c_str());
779 std::string msg =
"Cannot change minimizer. Continue using " +
fResult->MinimizerType();
780 MATH_WARN_MSG(
"Fitter::DoUpdateMinimizerOptions",msg.c_str());
807 if (isValid &&
fConfig.MinosErrors()) {
824 std::cout <<
"ROOT::Fit::Fitter::DoMinimization : ncalls = " <<
fResult->fNCalls <<
" type of objfunc " << fFitFitResType <<
" typeid: " <<
typeid(*fObjFunction).name() <<
" use gradient " <<
fUseGradient << std::endl;
835template<
class ObjFunc_t>
844template<
class ObjFunc_t>
851 std::shared_ptr<ObjFunc_t> sObjFunc{ std::move(objFunc)};
855 sObjFunc->UseSumOfWeightSquare();
863 for (
unsigned int i = 0;
i <
fConfig.NPar(); ++
i) {
876 if (fcn) ncalls = fcn->
NCalls();
880 if (fcn) ncalls = fcn->
NCalls();
895 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Must perform first a fit before applying the correction");
899 unsigned int n = loglw2.
NDim();
901 std::vector<double> cov(
n*
n);
902 bool ret =
fMinimizer->GetCovMatrix(&cov[0] );
904 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Previous fit has no valid Covariance matrix");
908 std::shared_ptr<ROOT::Math::IMultiGenFunction> objFunc(loglw2.
Clone());
923 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error running Hesse on weight2 likelihood - cannot compute errors");
928 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not accurate, the errors may be not reliable");
930 MATH_WARN_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood was forced to be defined positive");
933 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Covariance matrix for weighted likelihood is not valid !");
937 std::vector<double> hes(
n*
n);
940 MATH_ERROR_MSG(
"Fitter::ApplyWeightCorrection",
"Error retrieving Hesse on weight2 likelihood - cannot compute errors");
948 std::vector<double> tmp(
n*
n);
949 for (
unsigned int i = 0;
i <
n; ++
i) {
950 for (
unsigned int j = 0; j <
n; ++j) {
951 for (
unsigned int k = 0; k <
n; ++k)
952 tmp[
i*
n+j] += hes[
i*
n + k] * cov[k*
n + j];
956 std::vector<double> newCov(
n*
n);
957 for (
unsigned int i = 0;
i <
n; ++
i) {
958 for (
unsigned int j = 0; j <
n; ++j) {
959 for (
unsigned int k = 0; k <
n; ++k)
960 newCov[
i*
n+j] += cov[
i*
n + k] * tmp[k*
n + j];
965 for (
unsigned int i = 0;
i <
n; ++
i) {
966 fResult->fErrors[
i] = std::sqrt( newCov[
i*(
n+1)] );
967 for (
unsigned int j = 0; j <=
i; ++j)
968 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)
if(isa< VarDecl >(D)||isa< FieldDecl >(D)||isa< EnumConstantDecl >(D))
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< ROOT::Fit::ParameterSettings > & ParamsSettings() const
get the vector of parameter settings (const method)
class containing the result of the fit and all the related information (fitted parameter values,...
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
bool DoSetFCN(bool useExtFCN, const ROOT::Math::IMultiGenFunction &fcn, const double *params, unsigned int dataSize, int fitType)
Set Objective function.
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, 3 binned likelihood)
ROOT::Math::IParamGradFunction IGradModel1DFunction
std::shared_ptr< ROOT::Fit::FitData > fData
! pointer to the fit data (binned or unbinned data)
ROOT::Math::IMultiGenFunction BaseFunc
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 ...
void(* MinuitFCN_t)(int &npar, double *gin, double &f, double *u, int flag)
fit using user provided FCN with Minuit-like interface If npar = 0 it is assumed that the parameters ...
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
ROOT::Math::IParamMultiFunction IModelFunction
ROOT::Math::IParamFunction IModel1DFunction
bool SetFCN(unsigned int npar, Function &fcn, const double *params=nullptr, unsigned int dataSize=0, int fitType=0)
Set a generic FCN function as a C++ callable object implementing double () (const double *) Note that...
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...
ROOT::Math::IParamMultiGradFunction IGradModelFunction
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
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)
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
virtual bool HasGradient() const
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
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...
Namespace for the fitting classes.
IGradientFunctionMultiDim IMultiGradFunction
IMultiGenFunctionTempl< double > IMultiGenFunction
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...
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 &)