46 fValid(false), fNormalized(false), fNFree(0), fNdf(0), fNCalls(0),
47 fStatus(-1), fCovStatus(0), fVal(0), fEdm(-1), fChi2(-1)
75 if ( (
fMinimType.find(
"Fumili") == std::string::npos) &&
76 (
fMinimType.find(
"GSLMultiFit") == std::string::npos)
82 unsigned int npar = fconfig.
NPar();
83 for (
unsigned int i = 0; i < npar; ++i ) {
97 std::cout <<
"create fit result from config - nfree " <<
fNFree << std::endl;
113 fVal = min->MinValue();
125 if ( (
fMinimType.find(
"Fumili") == std::string::npos) &&
126 (
fMinimType.find(
"GSLMultiFit") == std::string::npos)
134 const unsigned int npar = min->NDim();
135 if (npar == 0)
return;
138 fParams = std::vector<double>(min->X(), min->X() + npar);
142 for (
unsigned int i = 0; i < npar; ++i ) {
147 if (sizeOfData > min->NFree() )
fNdf = sizeOfData - min->NFree();
161 for (
unsigned int i = 0; i < npar; ++i ) {
168 unsigned int nfree = 0;
169 for (
unsigned int ipar = 0; ipar < npar; ++ipar) {
182 MATH_ERROR_MSG(
"FitResult",
"FitConfiguration and Minimizer result are not consistent");
183 std::cout <<
"Number of free parameters from FitConfig = " << nfree << std::endl;
184 std::cout <<
"Number of free parameters from Minimizer = " <<
fNFree << std::endl;
200 if (min->Errors() != 0) {
202 fErrors = std::vector<double>(min->Errors(), min->Errors() + npar ) ;
205 unsigned int r = npar * ( npar + 1 )/2;
207 for (
unsigned int i = 0; i < npar; ++i)
208 for (
unsigned int j = 0; j <= i; ++j)
214 const std::vector<unsigned int> & ipars = fconfig.
MinosParams();
215 unsigned int n = (ipars.size() > 0) ? ipars.size() : npar;
216 for (
unsigned int i = 0; i <
n; ++i) {
218 unsigned int index = (ipars.size() > 0) ? ipars[i] : i;
219 bool ret = min->GetMinosError(index, elow, eup);
226 for (
unsigned int i = 0; i < npar; ++i) {
227 double globcc = min->GlobalCC(i);
228 if (globcc < 0)
break;
250 if (
this == &rhs)
return *
this;
291 bool FitResult::Update(
const std::shared_ptr<ROOT::Math::Minimizer> & min,
bool isValid,
unsigned int ncalls) {
297 const unsigned int npar =
fParams.size();
298 if (min->NDim() != npar ) {
302 if (min->X() == 0 ) {
307 if (
fNFree != min->NFree() ) {
314 fVal = min->MinValue();
320 if ( min->NCalls() > 0)
fNCalls = min->NCalls();
324 std::copy(min->X(), min->X() + npar,
fParams.begin());
330 if (min->Errors() != 0) {
334 std::copy(min->Errors(), min->Errors() + npar,
fErrors.begin() ) ;
339 unsigned int r = npar * ( npar + 1 )/2;
342 for (
unsigned int i = 0; i < npar; ++i) {
343 for (
unsigned int j = 0; j <= i; ++j)
350 for (
unsigned int i = 0; i < npar; ++i) {
351 double globcc = min->GlobalCC(i);
368 for (
unsigned int i = 0; i <
fErrors.size() ; ++i)
370 for (
unsigned int i = 0; i <
fCovMatrix.size() ; ++i)
384 std::map<unsigned int, std::pair<double,double> >::const_iterator itr =
fMinosErrors.find(i);
392 std::map<unsigned int, std::pair<double,double> >::const_iterator itr =
fMinosErrors.find(i);
399 std::map<unsigned int, std::pair<double,double> >::const_iterator itr =
fMinosErrors.find(i);
411 unsigned int npar =
fParams.size();
412 for (
unsigned int i = 0; i < npar; ++i)
427 std::map<unsigned int, unsigned int>::const_iterator itr =
fBoundParams.find(ipar);
429 lower = -std::numeric_limits<Double_t>::infinity();
430 upper = std::numeric_limits<Double_t>::infinity();
449 unsigned int npar =
fParams.size();
451 os <<
"<Empty FitResult>\n";
454 os <<
"\n****************************************\n";
456 if (
fStatus != gInitialResultStatus) {
457 os <<
" Invalid FitResult";
458 os <<
" (status = " <<
fStatus <<
" )";
461 os <<
" FitResult before fitting";
463 os <<
"\n****************************************\n";
467 os <<
"Minimizer is " <<
fMinimType << std::endl;
468 const unsigned int nw = 25;
469 const unsigned int nn = 12;
470 const std::ios_base::fmtflags prFmt = os.setf(std::ios::left,std::ios::adjustfield);
473 os << std::left << std::setw(nw) <<
"MinFCN" <<
" = " << std::right << std::setw(nn) <<
fVal << std::endl;
475 os << std::left << std::setw(nw) <<
"Chi2" <<
" = " << std::right << std::setw(nn) <<
fChi2 << std::endl;
476 os << std::left << std::setw(nw) <<
"NDf" <<
" = " << std::right << std::setw(nn) <<
fNdf << std::endl;
477 if (
fMinimType.find(
"Linear") == std::string::npos) {
478 if (
fEdm >=0) os << std::left << std::setw(nw) <<
"Edm" <<
" = " << std::right << std::setw(nn) <<
fEdm << std::endl;
479 os << std::left << std::setw(nw) <<
"NCalls" <<
" = " << std::right << std::setw(nn) <<
fNCalls << std::endl;
481 for (
unsigned int i = 0; i < npar; ++i) {
483 os <<
" = " << std::right << std::setw(nn) <<
fParams[i];
485 os << std::setw(9) <<
" " << std::setw(nn) <<
" " <<
" \t (fixed)";
488 os <<
" +/- " << std::left << std::setw(nn) <<
fErrors[i] << std::right;
490 os <<
" \t (limited)";
496 if (prFmt != os.flags() ) os.setf(prFmt, std::ios::adjustfield);
506 os <<
"\nCovariance Matrix:\n\n";
507 unsigned int npar =
fParams.size();
509 const int kWidth = 8;
511 const int matw = kWidth+4;
514 int prevPrec = os.precision(kPrec);
515 const std::ios_base::fmtflags prevFmt = os.flags();
517 os << std::setw(parw) <<
" " <<
"\t";
518 for (
unsigned int i = 0; i < npar; ++i) {
524 for (
unsigned int i = 0; i < npar; ++i) {
527 for (
unsigned int j = 0; j < npar; ++j) {
529 os.precision(kPrec); os.width(kWidth); os << std::right << std::setw(matw) <<
CovMatrix(i,j);
536 os <<
"\nCorrelation Matrix:\n\n";
537 os << std::setw(parw) <<
" " <<
"\t";
538 for (
unsigned int i = 0; i < npar; ++i) {
544 for (
unsigned int i = 0; i < npar; ++i) {
546 os << std::left << std::setw(parw) << std::left <<
GetParameterName(i) <<
"\t";
547 for (
unsigned int j = 0; j < npar; ++j) {
549 os.precision(kPrec); os.width(kWidth); os << std::right << std::setw(matw) <<
Correlation(i,j);
556 os.setf(prevFmt, std::ios::adjustfield);
557 os.precision(prevPrec);
569 MATH_ERROR_MSG(
"FitResult::GetConfidenceIntervals",
"Cannot compute Confidence Intervals without fit model function");
575 double corrFactor = 1;
576 if (
fChi2 <= 0 ||
fNdf == 0) norm =
false;
585 unsigned int ndim =
fFitFunc->NDim();
586 unsigned int npar =
fFitFunc->NPar();
588 std::vector<double> xpoint(ndim);
589 std::vector<double> grad(npar);
590 std::vector<double> vsum(npar);
593 for (
unsigned int ipoint = 0; ipoint <
n; ++ipoint) {
595 for (
unsigned int kdim = 0; kdim < ndim; ++kdim) {
596 unsigned int i = ipoint * stride1 + kdim * stride2;
608 for (
unsigned int ipar = 0; ipar < npar; ++ipar) {
611 grad[ipar] = d(
fParams[ipar] );
615 vsum.assign(npar,0.0);
616 for (
unsigned int ipar = 0; ipar < npar; ++ipar) {
617 for (
unsigned int jpar = 0; jpar < npar; ++jpar) {
618 vsum[ipar] +=
CovMatrix(ipar,jpar) * grad[jpar];
623 for (
unsigned int ipar = 0; ipar < npar; ++ipar) {
624 r2 += grad[ipar] * vsum[ipar];
627 ci[ipoint] = r * corrFactor;
635 unsigned int ndim = data.
NDim();
636 unsigned int np = data.
NPoints();
637 std::vector<double> xdata( ndim * np );
638 for (
unsigned int i = 0; i < np ; ++i) {
639 const double *
x = data.
Coords(i);
640 std::vector<double>::iterator itr = xdata.begin()+ ndim * i;
641 std::copy(x,x+ndim,itr);
651 std::vector<double> result;
653 result.resize(data->
NPoints() );
657 MATH_ERROR_MSG(
"FitResult::GetConfidenceIntervals",
"Cannot compute Confidence Intervals without the fit bin data");
int Index(const std::string &name) const
get index for parameter name (return -1 if not found)
bool IsFixed() const
check if is fixed
std::string ParName(unsigned int i) const
name of the parameter
std::shared_ptr< ROOT::Math::IMultiGenFunction > fObjFunc
minimizer object used for fitting
Namespace for new ROOT classes and functions.
double CovMatrix(unsigned int i, unsigned int j) const
retrieve covariance matrix element
double Error(unsigned int i) const
parameter error by index
void GetConfidenceIntervals(unsigned int n, unsigned int stride1, unsigned int stride2, const double *x, double *ci, double cl=0.95, bool norm=true) const
get confidence intervals for an array of n points x.
unsigned int NPar() const
total number of parameters (abbreviation)
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
Double_t StudentQuantile(Double_t p, Double_t ndf, Bool_t lower_tail=kTRUE)
Computes quantiles of the Student's t-distribution 1st argument is the probability, at which the quantile is computed 2nd argument - the number of degrees of freedom of the Student distribution When the 3rd argument lower_tail is kTRUE (default)- the algorithm returns such x0, that P(x < x0)=p upper tail (lower_tail is kFALSE)- the algorithm returns such x0, that P(x > x0)=p the algorithm was taken from G.W.Hill, "Algorithm 396, Student's t-quantiles" "Communications of the ACM", 13(10), October 1970.
const std::vector< unsigned int > & MinosParams() const
return vector of parameter indeces for which the Minos Error will be computed
const int gInitialResultStatus
double Value() const
copy constructor and assignment operators (leave them to the compiler)
unsigned int NPar() const
number of parameters settings
void FillResult(const std::shared_ptr< ROOT::Math::Minimizer > &min, const FitConfig &fconfig, const std::shared_ptr< IModelFunction > &f, bool isValid, unsigned int sizeOfData=0, bool binFit=true, const ROOT::Math::IMultiGenFunction *chi2func=0, unsigned int ncalls=0)
Fill the fit result from a Minimizer instance after fitting Run also Minos if requested from the conf...
FitResult & operator=(const FitResult &rhs)
Assignment operator.
double Prob() const
p value of the fit (chi2 probability)
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
bool HasMinosError(unsigned int i) const
query if parameter i has the Minos error
std::map< unsigned int, bool > fFixedParams
data set used in the fit
const double * Coords(unsigned int ipoint) const
return a pointer to the coordinates data for the given fit point
std::vector< double > fErrors
void SetFunction(const IGenFunction &f)
Set function for derivative calculation (copy the function if option has been enabled in the construc...
std::shared_ptr< FitData > fFitData
model function resulting from the fit.
virtual ~FitResult()
Destructor.
std::string GetParameterName(unsigned int ipar) const
get name of parameter (deprecated)
double LowerError(unsigned int i) const
lower Minos error. If Minos has not run for parameter i return the parabolic error ...
double StepSize() const
return step size
#define MATH_ERROR_MSG(loc, str)
std::shared_ptr< ROOT::Math::Minimizer > fMinimizer
Documentation for the abstract class IBaseFunctionMultiDim.
std::shared_ptr< IModelFunction > fFitFunc
objective function used for fitting
double Correlation(unsigned int i, unsigned int j) const
retrieve correlation elements
std::map< unsigned int, std::pair< double, double > > fMinosErrors
void SetMinosError(unsigned int i, double elow, double eup)
set the Minos errors for parameter i (called by the Fitter class when running Minos) ...
bool HasUpperLimit() const
check if parameter has upper limit
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
double UpperError(unsigned int i) const
upper Minos error. If Minos has not run for parameter i return the parabolic error ...
OneDimParamFunctionAdapter class to wrap a multi-dim parameteric function in one dimensional one...
double UpperLimit() const
return upper limit value
class containg the result of the fit and all the related information (fitted parameter values...
void PrintCovMatrix(std::ostream &os) const
print error matrix and correlations
const std::string & Name() const
return name
void NormalizeErrors()
normalize errors using chi2/ndf for chi2 fits
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
bool IsParameterFixed(unsigned int ipar) const
query if a parameter is fixed
const std::string & MinimizerType() const
return type of minimizer package
static constexpr double s
std::vector< std::pair< double, double > > fParamBounds
bool MinosErrors() const
do minos errros analysis on the parameters
std::vector< std::string > fParNames
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
const BinData * FittedBinData() const
return BinData used in the fit (return a nullptr in case a different fit is done or the data are not ...
std::string ToString(const T &val)
Utility function for conversion to strings.
double chisquared_cdf_c(double x, double r, double x0=0)
Complement of the cumulative distribution function of the distribution with degrees of freedom (upp...
std::vector< double > fGlobalCC
FitResult()
Default constructor for an empty (non valid) fit result.
std::vector< double > fCovMatrix
double LowerLimit() const
return lower limit value
std::map< unsigned int, unsigned int > fBoundParams
bool ParameterBounds(unsigned int ipar, double &lower, double &upper) const
retrieve parameter bounds - return false if parameter is not bound
std::vector< double > fParams
unsigned int NPoints() const
return number of fit points
unsigned int NDim() const
return coordinate data dimension
bool IsBound() const
check if is bound
User class for calculating the derivatives of a function.
double chisquared_quantile(double z, double r)
Inverse ( ) of the cumulative distribution function of the lower tail of the distribution with degr...
bool IsParameterBound(unsigned int ipar) const
query if a parameter is bound
bool HasLowerLimit() const
check if parameter has lower limit
bool Update(const std::shared_ptr< ROOT::Math::Minimizer > &min, bool isValid, unsigned int ncalls=0)
Update the fit result with a new minimization status To be run only if same fit is performed with sam...
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
const std::string & MinimizerAlgoType() const
return type of minimizer algorithms