112 const std::vector<double> & minpar =
fFitter->Result().Parameters();
113 assert (npar == (
int) minpar.size() );
116 for (
int i =0; i < npar; ++i) {
117 diff += std::abs( params[i] - minpar[i] );
121 if (diff > s * 1.E-12 )
Warning(
"Chisquare",
"given parameter values are not at minimum - chi2 at minimum is returned");
122 return fFitter->Result().Chi2();
137 std::cout<<
"Execute command= "<<command<<std::endl;
149 Error(
"ExecuteCommand",
"FCN must set before executing this command");
155 return (
ret) ? 0 : -1;
163 Error(
"ExecuteCommand",
"FCN must set before executing this command");
167 return (
ret) ? 0 : -1;
173 Error(
"ExecuteCommand",
"FCN must set before executing this command");
179 return (
ret) ? 0 : -1;
185 Error(
"ExecuteCommand",
"FCN must set before executing this command");
191 return (
ret) ? 0 : -1;
194 else if (scommand.
Contains(
"MINO")) {
196 if (
fFitter->Config().MinosErrors() )
return 0;
199 Error(
"ExecuteCommand",
"FCN must set before executing this command");
203 fFitter->Config().SetMinosErrors(
true);
208 return (
ret) ? 0 : -1;
212 else if (scommand.
Contains(
"HES")) {
214 if (
fFitter->Config().ParabErrors() )
return 0;
217 Error(
"ExecuteCommand",
"FCN must set before executing this command");
222 fFitter->Config().SetParabErrors(
true);
225 return (
ret) ? 0 : -1;
229 else if (scommand.
Contains(
"FIX")) {
230 for(
int i = 0; i < nargs; i++) {
236 else if (scommand.
Contains(
"SET LIM")) {
238 Error(
"ExecuteCommand",
"Invalid parameters given in SET LIMIT");
241 int ipar =
int(args[0]);
243 double low = args[1];
245 fFitter->Config().ParSettings(ipar).SetLimits(low,up);
249 else if (scommand.
Contains(
"SET PRIN")) {
250 if (nargs < 1)
return -1;
251 fFitter->Config().MinimizerOptions().SetPrintLevel(
int(args[0]) );
255 else if (scommand.
Contains(
"SET ERR")) {
256 if (nargs < 1)
return -1;
257 fFitter->Config().MinimizerOptions().SetPrintLevel(
int( args[0]) );
261 else if (scommand.
Contains(
"SET STR")) {
262 if (nargs < 1)
return -1;
263 fFitter->Config().MinimizerOptions().SetStrategy(
int(args[0]) );
267 else if (scommand.
Contains(
"SET GRA")) {
273 else if (scommand.
Contains(
"SET NOW")) {
279 else if (scommand.
Contains(
"CALL FCN")) {
282 if (nargs < 1 ||
fFCN ==
nullptr )
return -1;
285 std::vector<double> params(npar);
286 for (
int i = 0; i < npar; ++i)
290 (*fFCN)(npar,
nullptr, fval, ¶ms[0],
int(args[0]) ) ;
295 Error(
"ExecuteCommand",
"Invalid or not supported command given %s",command);
304 int nps =
fFitter->Config().ParamsSettings().size();
305 if (ipar < 0 || ipar >= nps ) {
307 Error(
"ValidParameterIndex",
"%s",msg.c_str());
318 fFitter->Config().ParSettings(ipar).Fix();
334 if (!
fFitter->Result().IsValid()) {
335 Error(
"GetConfidenceIntervals",
"Cannot compute confidence intervals with an invalid fit result");
339 fFitter->Result().GetConfidenceIntervals(
n,ndim,1,
x,ci,cl,
false);
369 if (!
fFitter->Result().IsValid() ) {
370 Error(
"GetConfidenceIntervals",
"Cannot compute confidence intervals with an invalid fit result");
378 Error(
"GetConfidenceIntervals",
"Cannot compute confidence intervals without a fitting object");
384 TH1 *
h1 =
dynamic_cast<TH1*
>(fitobj);
385 assert(
h1 !=
nullptr);
386 datadim =
h1->GetDimension();
391 Error(
"GetConfidenceIntervals",
"Invalid object passed for storing confidence level data, must be a TGraphErrors or a TH1");
397 Error(
"GetConfidenceIntervals",
"Invalid object passed for storing confidence level data, must be a TGraph2DErrors or a TH2");
403 Error(
"GetConfidenceIntervals",
"Invalid object passed for storing confidence level data, must be a TH3");
410 data.Opt().fUseEmpty =
true;
422 unsigned int n = data.Size();
424 std::vector<double> ci(
n );
426 fFitter->Result().GetConfidenceIntervals(data,&ci[0],cl,
false);
429 assert(func !=
nullptr);
432 for (
unsigned int i = 0; i <
n; ++i) {
433 const double *
x = data.Coords(i);
434 double y = (*func)(
x );
438 assert(
gr !=
nullptr);
439 gr->SetPoint(i, *
x,
y);
440 gr->SetPointError(i, 0, ci[i]);
444 assert(
gr !=
nullptr);
445 gr->SetPoint(i,
x[0],
x[1],
y);
446 gr->SetPointError(i, 0, 0, ci[i]);
449 TH1 *
h1 =
dynamic_cast<TH1 *
> (obj);
450 assert(
h1 !=
nullptr);
452 if (datadim == 1) ibin =
h1->FindBin(*
x);
453 if (datadim == 2) ibin =
h1->FindBin(
x[0],
x[1]);
454 if (datadim == 3) ibin =
h1->FindBin(
x[0],
x[1],
x[2]);
455 h1->SetBinContent(ibin,
y);
456 h1->SetBinError(ibin, ci[i]);
470 if (
fCovar.size() != nfreepar*nfreepar )
471 fCovar.resize(nfreepar*nfreepar);
473 if (!
fFitter->Result().IsValid() ) {
474 Warning(
"GetCovarianceMatrix",
"Invalid fit result");
479 for (
unsigned int i = 0; i < ntotpar; ++i) {
480 if (
fFitter->Config().ParSettings(i).IsFixed() )
continue;
482 for (
unsigned int j = 0; j < ntotpar; ++j) {
483 if (
fFitter->Config().ParSettings(j).IsFixed() )
continue;
484 unsigned int index = nfreepar*
l +
m;
485 assert(index <
fCovar.size() );
498 unsigned int np2 =
fCovar.size();
500 if ( np2 == 0 || np2 != npar *npar ) {
502 if (
c ==
nullptr)
return 0;
504 return fCovar[i*npar + j];
515 Warning(
"GetErrors",
"Invalid fit result");
519 eparab = result.
Error(ipar);
530 return fFitter->Result().NTotalParameters();
534 return fFitter->Result().NFreeParameters();
541 if (
fFitter->Result().IsEmpty() ) {
545 return fFitter->Result().Error(ipar);
552 if (
fFitter->Result().IsEmpty() ) {
556 return fFitter->Result().Value(ipar);
566 const std::string & pname =
fFitter->Config().ParSettings(ipar).Name();
567 const char *
c = pname.c_str();
568 std::copy(
c,
c + pname.size(),
name);
570 if (
fFitter->Result().IsEmpty() ) {
571 value =
fFitter->Config().ParSettings(ipar).Value();
572 verr =
fFitter->Config().ParSettings(ipar).Value();
573 vlow =
fFitter->Config().ParSettings(ipar).LowerLimit();
574 vhigh =
fFitter->Config().ParSettings(ipar).UpperLimit();
578 value =
fFitter->Result().Value(ipar);
579 verr =
fFitter->Result().Error(ipar);
580 vlow =
fFitter->Result().LowerError(ipar);
581 vhigh =
fFitter->Result().UpperError(ipar);
593 return fFitter->Config().ParSettings(ipar).Name().c_str();
603 errdef =
fFitter->Config().MinimizerOptions().ErrorDef();
613 Warning(
"GetSumLog",
"Dummy method - returned 0");
624 return fFitter->Config().ParSettings(ipar).IsFixed();
632 if (
fFitter->GetMinimizer() &&
fFitter->Config().MinimizerType() ==
"Minuit")
633 fFitter->GetMinimizer()->PrintResults();
635 if (level > 0)
fFitter->Result().Print(std::cout);
636 if (level > 1)
fFitter->Result().PrintCovMatrix(std::cout);
646 fFitter->Config().ParSettings(ipar).Release();
654 Info(
"SetFitMethod",
"non supported method");
663 std::vector<ROOT::Fit::ParameterSettings> & parlist =
fFitter->Config().ParamsSettings();
664 if ( ipar >= (
int) parlist.size() ) parlist.resize(ipar+1);
666 if (verr == 0) ps.
Fix();
667 if (vlow < vhigh) ps.
SetLimits(vlow, vhigh);
691 if (
fFitter->Result().FittedFunction() !=
nullptr) {
715 Error(
"SetMinimizerFunction",
"cannot create minimizer %s",
fFitter->Config().MinimizerType().c_str() );
719 Error(
"SetMinimizerFunction",
"Object Function pointer is NULL");
756 assert(fobj !=
nullptr);
757 int ndim =
fFitter->Config().ParamsSettings().size();
780 return fFitter->GetMinimizer();
787 if (!
fFitter.get() )
return nullptr;
799 if (!
gr)
return false;
802 Error(
"Scan",
"Minimizer is not available - cannot scan before fitting");
806 unsigned int npoints =
gr->GetN();
812 if ((
int) npoints < gr->GetN() )
gr->Set(npoints);
852 if (!
gr)
return false;
855 Error(
"Scan",
"Minimizer is not available - cannot scan before fitting");
860 double upScale =
fFitter->Config().MinimizerOptions().ErrorDef();
867 unsigned int npoints =
gr->GetN();
872 bool ret = minimizer->
Contour( ipar, jpar, npoints,
gr->GetX(),
gr->GetY());
873 if ((
int) npoints < gr->GetN() )
gr->Set(npoints);
int Int_t
Signed integer 4 bytes (int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
const char Option_t
Option string (const char).
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Chi2FCN class for binned fits using the least square methods.
void SetDimension(int dim)
class containing the result of the fit and all the related information (fitted parameter values,...
bool IsValid() const
True if fit successful, otherwise false.
double UpperError(unsigned int i) const
upper Minos error. If Minos has not run for parameter i return the parabolic error
double Error(unsigned int i) const
parameter error by index
double LowerError(unsigned int i) const
lower Minos error. If Minos has not run for parameter i return the parabolic error
double MinFcnValue() const
Return value of the objective function (chi2 or likelihood) used in the fit.
double Edm() const
Expected distance from minimum.
unsigned int NTotalParameters() const
get total number of parameters
unsigned int NFreeParameters() const
get total number of free parameters
double GlobalCC(unsigned int i) const
parameter global correlation coefficient
LogLikelihoodFCN class for likelihood fits.
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
void SetLimits(double low, double up)
set a double side limit, if low == up the parameter is fixed if low > up the limits are removed The c...
void Fix()
fix the parameter
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
void SetErrorDef(double up)
Set scale for calculating the errors.
virtual bool Scan(unsigned int ivar, unsigned int &nstep, double *x, double *y, double xmin=0, double xmax=0)
Scan function minimum for variable i.
virtual bool Contour(unsigned int ivar, unsigned int jvar, unsigned int &npoints, double *xi, double *xj)
Find the contour points (xi, xj) of the function for parameter ivar and jvar around the minimum.
const char * GetParName(Int_t ipar) const override
Return name of parameter ipar.
void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95) override
Computes point-by-point confidence intervals for the fitted function.
void ReleaseParameter(Int_t ipar) override
Release a fit parameter.
Double_t GetParError(Int_t ipar) const override
Parameter error.
ROOT::Math::Minimizer * fMinimizer
void DoSetDimension()
Private method to set dimension in objective function.
void SetFCN(void(*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t)) override
Override setFCN to use the Adapter to Minuit2 FCN interface To set the address of the minimization fu...
ROOT::Math::Minimizer * GetMinimizer() const
Return a pointer to the minimizer.
void Clear(Option_t *option="") override
Clear resources for consecutive fits.
void PrintResults(Int_t level, Double_t amin) const override
Print the fit result.
Int_t GetStats(Double_t &amin, Double_t &edm, Double_t &errdef, Int_t &nvpar, Int_t &nparx) const override
Get fit statistical information.
Int_t GetNumberFreeParameters() const override
TBackCompFitter()
Constructor needed by TVirtualFitter interface.
void FixParameter(Int_t ipar) override
Fix the parameter.
void SetFitMethod(const char *name) override
Set fit method (chi2 or likelihood).
Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const override
Get error matrix element (return all zero if matrix is not available).
Bool_t IsFixed(Int_t ipar) const override
Query if parameter ipar is fixed.
std::shared_ptr< ROOT::Fit::FitData > fFitData
! Data of the fit
ROOT::Math::IParamMultiFunction * fModelFunc
Int_t GetNumberTotalParameters() const override
Number of total parameters.
Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs) override
Execute the command (Fortran Minuit compatible interface).
TFitResult * GetTFitResult() const
Get a copy of the Fit result returning directly a new TFitResult.
void ReCreateMinimizer()
Recreate a minimizer instance using the function and data set objective function in minimizers functi...
std::shared_ptr< ROOT::Fit::Fitter > fFitter
! Pointer to fitter object
Int_t GetErrors(Int_t ipar, Double_t &eplus, Double_t &eminus, Double_t &eparab, Double_t &globcc) const override
Get fit errors.
std::vector< double > fCovar
Cached covariance matrix (NxN).
bool ValidParameterIndex(int ipar) const
Check if ipar is a valid parameter index.
bool Contour(unsigned int ipar, unsigned int jpar, TGraph *gr, double confLevel=0.683)
Create a 2D contour around the minimum for the parameter ipar and jpar if a minimum does not exist or...
Double_t GetSumLog(Int_t i) override
Sum of log (un-needed).
~TBackCompFitter() override
Destructor - delete the managed objects.
Double_t Chisquare(Int_t npar, Double_t *params) const override
Do chisquare calculations in case of likelihood fits Do evaluation a the minimum only.
ROOT::Math::IMultiGenFunction * GetObjFunction() const
Return a pointer to the objective function (FCN) If fitting directly using TBackCompFitter the pointe...
virtual void SetObjFunction(ROOT::Math::IMultiGenFunction *f)
Set the objective function for fitting Needed if fitting directly using TBackCompFitter class The cla...
bool Scan(unsigned int ipar, TGraph *gr, double xmin=0, double xmax=0)
Scan parameter ipar between value of xmin and xmax A graph must be given which will be on return fill...
ROOT::Math::IMultiGenFunction * fObjFunc
Double_t GetParameter(Int_t ipar) const override
Parameter value.
Int_t SetParameter(Int_t ipar, const char *parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh) override
Set (add) a new fit parameter passing initial value, step size (verr) and parameter limits if vlow > ...
Double_t * GetCovarianceMatrix() const override
Get the error matrix in a pointer to a NxN array.
Extends the ROOT::Fit::Result class with a TNamed inheritance providing easy possibility for I/O.
TH1 is the base class of all histogram classes in ROOT.
TObject * Clone(const char *newname="") const override
Make a clone of an object using the Streamer facility.
virtual void SetName(const char *name)
Set the name of the TNamed.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
TObject()
TObject constructor.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
void ToUpper()
Change string to upper case.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
virtual TObject * GetObjectFit() const
virtual Foption_t GetFitOption() const
static const char * GetDefaultFitter()
static: return the name of the default fitter
void(* fFCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.
std::string ToString(const T &val)
Utility function for conversion to strings.
IMultiGenFunctionTempl< double > IMultiGenFunction
IParametricFunctionMultiDim IParamMultiFunction
Double_t ChisquareQuantile(Double_t p, Double_t ndf)