74 template <
class FitObject>
77 template <
class FitObject>
80 template <
class FitObject>
90 Error(
"Fit",
"function may not be null pointer");
94 Error(
"Fit",
"function is zombie");
100 Error(
"Fit",
"function %s has illegal number of parameters = %d", f1->
GetName(), npar);
106 Error(
"Fit",
"function %s dimension, %d, is greater than fit object dimension, %d",
111 Error(
"Fit",
"function %s dimension, %d, is smaller than fit object dimension -1, %d",
123 Double_t fxmin, fymin, fzmin, fxmax, fymax, fzmax;
124 f1.
GetRange(fxmin, fymin, fzmin, fxmax, fymax, fzmax);
133 template<
class FitObject>
140 printf(
"fit function %s\n",f1->
GetName() );
146 if (iret != 0)
return iret;
152 if (fitOption.
Integral)
Info(
"Fit",
"Ignore Integral option. Model function dimension is less than the data object dimension");
153 if (fitOption.
Like)
Info(
"Fit",
"Ignore Likelihood option. Model function dimension is less than the data object dimension");
161 if (special==299+npar) linear =
kTRUE;
167 std::shared_ptr<TFitResult> tfr(
new TFitResult() );
169 std::shared_ptr<ROOT::Fit::Fitter> fitter(
new ROOT::Fit::Fitter(std::static_pointer_cast<ROOT::Fit::FitResult>(tfr) ) );
190 printf(
"use range \n" );
195 printf(
"range size %d\n", range.
Size(0) );
198 printf(
" range in x = [%f,%f] \n",x1,x2);
205 if (fitdata->Size() == 0 ) {
206 Warning(
"Fit",
"Fit data is empty ");
211 printf(
"HFit:: data size is %d \n",fitdata->Size());
212 for (
unsigned int i = 0; i < fitdata->Size(); ++i) {
213 if (fitdata->NDim() == 1) printf(
" x[%d] = %f - value = %f \n", i,*(fitdata->Coords(i)),fitdata->Value(i) );
223 if (special != 0 && !fitOption.
Bound && !linear) {
236 if ( (linear || fitOption.
Gradient) )
244 if (
int(fitdata->NDim()) == hdim -1 ) fitConfig.
SetNormErrors(
true);
252 for (
int i = 0; i < npar; ++i) {
258 if (plow*pup != 0 && plow >= pup) {
261 else if (plow < pup ) {
276 double step = 0.1 * (pup - plow);
278 if ( parSettings.
Value() < pup && pup - parSettings.
Value() < 2 * step )
279 step = (pup - parSettings.
Value() ) / 2;
280 else if ( parSettings.
Value() > plow && parSettings.
Value() - plow < 2 * step )
281 step = (parSettings.
Value() - plow ) / 2;
310 std::string
type =
"Robust";
336 if (fitOption.
Like) printf(
"do likelihood fit...\n");
337 if (linear) printf(
"do linear fit...\n");
338 printf(
"do now fit...\n");
353 if (fitOption.
User && userFcn)
354 fitok = fitter->FitFCN( userFcn );
355 else if (fitOption.
Like) {
357 bool weight = ((fitOption.
Like & 2) == 2);
359 bool extended = ((fitOption.
Like & 4 ) != 4 );
361 fitok = fitter->LikelihoodFit(*fitdata, extended);
364 fitok = fitter->Fit(*fitdata);
367 if ( !fitok && !fitOption.
Quiet )
368 Warning(
"Fit",
"Abnormal termination of minimization.");
374 iret = fitResult.
Status();
398 if (!fitOption.
Quiet) {
399 if (fitter->GetMinimizer() && fitConfig.
MinimizerType() ==
"Minuit" &&
401 fitter->GetMinimizer()->PrintResults();
406 fitResult.
Print(std::cout);
421 bcfitter->
SetFCN(userFcn);
443 name = name + h1->GetName() +
"-" + f1->
GetName();
445 title += h1->GetTitle();
447 tfr->SetTitle(title);
462 if (range.
Size(0) == 0) {
473 if (range.
Size(1) == 0) {
484 if (range.
Size(2) == 0) {
495 std::cout <<
"xmin,xmax" << xmin <<
" " <<
xmax << std::endl;
514 else if (range.
Size(0) == 0) {
516 double xmin = std::numeric_limits<double>::infinity();
517 double xmax = -std::numeric_limits<double>::infinity();
520 while ( (g = (
TGraph*) next() ) ) {
521 double x1 = 0,
x2 = 0, y1 = 0, y2 = 0;
522 g->ComputeRange(x1,y1,
x2,y2);
523 if (x1 < xmin) xmin =
x1;
524 if (
x2 > xmax) xmax =
x2;
536 if (range.
Size(0) == 0) {
541 if (range.
Size(1) == 0) {
554 for (
int i = 0; i < ndim; ++i ) {
555 if ( range.
Size(i) == 0 ) {
562 template<
class FitObject>
568 std::cout <<
"draw and store fit function " << f1->
GetName() << std::endl;
580 std::cout <<
"draw and store fit function " << f1->
GetName()
581 <<
" Range in x = [ " << xmin <<
" , " <<
xmax <<
" ]" << std::endl;
584 TList * funcList = h1->GetListOfFunctions();
586 Error(
"StoreAndDrawFitFunction",
"Function list has not been created - cannot store the fitted function");
594 bool reuseOldFunction =
false;
595 if (delOldFunction) {
598 while ((obj = next())) {
605 reuseOldFunction =
true;
617 if (!reuseOldFunction) {
618 fnew1 = (
TF1*)f1->IsA()->New();
621 funcList->
Add(fnew1);
631 }
else if (ndim < 3) {
632 if (!reuseOldFunction) {
633 fnew2 = (
TF2*)f1->IsA()->New();
636 funcList->
Add(fnew2);
639 fnew2 =
dynamic_cast<TF2*
>(
f1);
648 if (!reuseOldFunction) {
649 fnew3 = (
TF3*)f1->IsA()->New();
652 funcList->
Add(fnew3);
655 fnew2 =
dynamic_cast<TF3*
>(
f1);
666 if (drawFunction && ndim < 3 && h1->InheritsFrom(
TH1::Class() ) ) {
669 if (!
gPad || (
gPad &&
gPad->GetListOfPrimitives()->FindObject(h1) ==
NULL ) )
682 if (option == 0)
return;
683 if (!option[0])
return;
708 else if (type ==
kGraph) {
716 int start = opt.
Index(
"H=0.");
717 int numpos = start + strlen(
"H=0.");
720 while( (numpos+numlen<len) && isdigit(opt[numpos+numlen]) ) numlen++;
721 TString num = opt(numpos,numlen);
722 opt.
Remove(start+strlen(
"H"),strlen(
"=0.")+numlen);
723 h = atof(num.
Data());
741 if (fitOption.
Like == 1) {
745 if (fitOption.
Like == 2) fitOption.
Like = 6;
746 else fitOption.
Like = 4;
751 if (fitOption.
Chi2 == 1 || fitOption.
PChi2 == 1)
752 Warning(
"Fit",
"Cannot use P or X option in combination of L. Ignore the chi2 option and perform a likelihood fit");
776 Info(
"CheckGraphFitOptions",
"L (Log Likelihood fit) is an invalid option when fitting a graph. It is ignored");
780 Info(
"CheckGraphFitOptions",
"I (use function integral) is an invalid option when fitting a graph. It is ignored");
792 std::shared_ptr<ROOT::Fit::UnBinData> fitdata(data);
795 printf(
"tree data size is %d \n",fitdata->Size());
796 for (
unsigned int i = 0; i < fitdata->Size(); ++i) {
797 if (fitdata->NDim() == 1) printf(
" x[%d] = %f \n", i,*(fitdata->Coords(i) ) );
800 if (fitdata->Size() == 0 ) {
801 Warning(
"Fit",
"Fit data is empty ");
806 std::shared_ptr<TFitResult> tfr(
new TFitResult() );
812 unsigned int dim = fitdata->NDim();
818 assert ( (
int) dim == fitfunc->
GetNdim() );
827 for (
int i = 0; i < npar; ++i) {
832 if (plow*pup != 0 && plow >= pup) {
835 else if (plow < pup ) {
850 double step = 0.1 * (pup - plow);
852 if ( parSettings.
Value() < pup && pup - parSettings.
Value() < 2 * step )
853 step = (pup - parSettings.
Value() ) / 2;
854 else if ( parSettings.
Value() > plow && parSettings.
Value() - plow < 2 * step )
855 step = (parSettings.
Value() - plow ) / 2;
877 if ( (fitOption.
Like & 2) == 2)
880 bool extended = (fitOption.
Like & 1) == 1;
883 fitok = fitter->LikelihoodFit(fitdata, extended);
884 if ( !fitok && !fitOption.
Quiet )
885 Warning(
"UnBinFit",
"Abnormal termination of minimization.");
889 int iret = fitResult.
Status();
914 if (lastFitter)
delete lastFitter;
924 else if (!fitOption.
Quiet) fitResult.
Print(std::cout);
929 name = name +
"UnBinData-" + fitfunc->
GetName();
933 tfr->SetTitle(title);
948 Warning(
"HFit::FitObject",
"A weighted likelihood fit is requested but histogram is not weighted - do a standard Likelihood fit");
952 return HFit::Fit(h1,f1,foption,moption,goption,range);
959 return HFit::Fit(gr,f1,foption,moption,goption,range);
966 return HFit::Fit(gr,f1,foption,moption,goption,range);
973 return HFit::Fit(gr,f1,foption,moption,goption,range);
978 return HFit::Fit(s1,f1,foption,moption,goption,range);
1005 template<
class FitObject>
1017 if (data.
Size() == 0 ) {
1018 Warning(
"Chisquare",
"data set is empty - return -1");
virtual const char * GetName() const
Returns name of object.
virtual void SetParameters(const Double_t *params)
void SetPrintLevel(int level)
set print level
Int_t GetFirst() const
Return first bin on the axis i.e.
double Chisquare(const TH1 &h1, TF1 &f1, bool useRange, bool usePL=false)
compute the chi2 value for an histogram given a function (see TH1::Chisquare for the documentation) ...
void SetTolerance(double tol)
set the tolerance
virtual void Copy(TObject &f1) const
Copy this F1 to a new F1.
virtual void Save(Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax)
Save values of function in array fSave.
virtual void SetMethodCall(TMethodCall *m)
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
const std::vector< double > & Errors() const
parameter errors (return st::vector)
TString & ReplaceAll(const TString &s1, const TString &s2)
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
virtual void SetUserFunc(TObject *userfunc)
bool NormalizeErrors() const
flag to check if resulting errors are be normalized according to chi2/ndf
Bool_t TestBit(UInt_t f) const
Double_t GetXmax() const
Returns the X maximum.
A TMultiGraph is a collection of TGraph (or derived) objects.
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
void ToUpper()
Change string to upper case.
virtual void Save(Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax)
Save values of function in array fSave.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
double Value() const
copy constructor and assignment operators (leave them to the compiler)
unsigned int Ndf() const
Number of degree of freedom.
virtual void SetNumberFitPoints(Int_t npfits)
virtual void SetFCN(void(*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
Override setFCN to use the Adapter to Minuit2 FCN interface To set the address of the minimization fu...
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Backward compatible implementation of TVirtualFitter.
virtual Double_t GetParError(Int_t ipar) const
Return value of parameter number ipar.
Double_t GetYmax() const
Returns the Y maximum.
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
TMethodCall * GetMethodCall() const
TFitResultPtr UnBinFit(ROOT::Fit::UnBinData *data, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption)
fit an unbin data set (from tree or from histogram buffer) using a TF1 pointer and fit options...
virtual Int_t GetDimension() const
virtual void SetParent(TObject *p=0)
static const double x2[5]
void GetRange(unsigned int irange, unsigned int icoord, double &xmin, double &xmax) const
get the i-th range for given coordinate.
int GetDimension(const TH1 *h1)
Extends the ROOT::Fit::Result class with a TNamed inheritance providing easy possibility for I/O...
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
Double_t GetXmin() const
Returns the X minimum.
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
void GetDrawingRange(TH1 *h1, ROOT::Fit::DataRange &range)
void Info(const char *location, const char *msgfmt,...)
void Fix()
fix the parameter
virtual Int_t GetNdim() const
unsigned int Size() const
return number of fit points
void Error(const char *location, const char *msgfmt,...)
void SetLowerLimit(double low)
set a single lower limit
static void SetFitter(TVirtualFitter *fitter, Int_t maxpar=25)
Static function to set an alternative fitter.
int CheckFitFunction(const TF1 *f1, int hdim)
R__EXTERN TVirtualMutex * gGlobalMutex
void SetMinosErrors(bool on=true)
set Minos erros computation to be performed after fitting
Chi2FCN class for binnned fits using the least square methods.
virtual void SetObjectFit(TObject *obj)
void InitGaus(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for gaussian function given the fit data Set the sigma limits for zero top ...
virtual void SetChisquare(Double_t chi2)
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
void SetStepSize(double err)
set the step size
void FillData(BinData &dv, const TH1 *hist, TF1 *func=0)
fill the data vector from a TH1.
TAxis * GetAxis(Int_t dim) const
TH1F * GetHistogram() const
Returns a pointer to the histogram used to draw the axis.
DataOptions : simple structure holding the options on how the data are filled.
Int_t GetLast() const
Return last bin on the axis i.e.
Class to manage histogram axis.
A 3-Dim function with parameters.
Fitter class, entry point for performing all type of fits.
virtual TObject * Remove(TObject *obj)
Remove object from the list.
virtual void SetFitOption(Foption_t option)
Provides an indirection to the TFitResult class and with a semantics identical to a TFitResult pointe...
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
unsigned int Size(unsigned int icoord=0) const
return range size for coordinate icoord (starts from zero) Size == 0 indicates no range is present [-...
void FitOptionsMake(const char *option, Foption_t &fitOption)
virtual Bool_t IsLinear() const
static TVirtualFitter * GetFitter()
static: return the current Fitter
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
int Status() const
minimizer status code
A 2-Dim function with parameters.
void InitExpo(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for an exponential function given the fit data Set the constant and slope a...
void Warning(const char *location, const char *msgfmt,...)
void(* FCNFunc_t)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
void SetMinimizerOptions(const ROOT::Math::MinimizerOptions &minopt)
set all the minimizer options using class MinimizerOptions
TString & Remove(Ssiz_t pos)
void AddRange(unsigned int icoord, double xmin, double xmax)
add a range [xmin,xmax] for the new coordinate icoord Adding a range does not delete existing one...
virtual Int_t GetSumw2N() const
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
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
void GetFunctionRange(const TF1 &f1, ROOT::Fit::DataRange &range)
static const double x1[5]
class describing the range in the coordinates it supports multiple range in a coordinate.
TH1F * GetHistogram() const
Returns a pointer to the histogram used to draw the axis Takes into account the two following cases...
void Init2DGaus(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for 2D gaussian function given the fit data Set the sigma limits for zero t...
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
const std::vector< double > & Parameters() const
parameter values (return std::vector)
void SetWeightCorrection(bool on=true)
apply the weight correction for error matric computation
const std::string & MinimizerType() const
return type of minimizer package
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
void FitOptionsMake(EFitObjectType type, const char *option, Foption_t &fitOption)
Decode list of options into fitOption.
Int_t GetNdimensions() const
Abstract Base Class for Fitting.
bool IsEmpty() const
True if a fit result does not exist (even invalid) with parameter values.
Mother of all ROOT objects.
virtual Int_t GetNpar() const
virtual void GetParLimits(Int_t ipar, Double_t &parmin, Double_t &parmax) const
Return limits for parameter ipar.
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
TList * GetListOfGraphs() const
void StoreAndDrawFitFunction(FitObject *h1, TF1 *f1, const ROOT::Fit::DataRange &range, bool, bool, const char *goption)
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
std::string ToString(const T &val)
Utility function for conversion to strings.
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
virtual void Add(TObject *obj)
virtual void SetParErrors(const Double_t *errors)
Set errors for all active parameters when calling this function, the array errors must have at least ...
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
virtual Bool_t AddToGlobalList(Bool_t on=kTRUE)
Add to global list of functions (gROOT->GetListOfFunctions() ) return previous status (true if the fu...
void SetNormErrors(bool on=true)
set the option to normalize the error on the result according to chi2/ndf
void SetUpperLimit(double up)
set a single upper limit
Double_t GetYmin() const
Returns the Y minimum.
A Graph is a graphics object made of two arrays X and Y with npoints each.
double ComputeChi2(const FitObject &h1, TF1 &f1, bool useRange, bool usePL)
virtual void SetNDF(Int_t ndf)
Set the number of degrees of freedom ndf should be the number of points used in a fit - the number of...
virtual Double_t * GetParameters() const
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...
Multidimensional histogram base.
TFitResultPtr FitObject(TH1 *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
fitting function for a TH1 (called from TH1::Fit)
const Bool_t kIterBackward
void SetParabErrors(bool on=true)
set parabolic erros
Graphics object made of three arrays X, Y and Z with the same number of points each.
virtual Int_t GetNumber() const
double Chi2() const
Chi2 fit value in case of likelihood must be computed ?
void CheckGraphFitOptions(Foption_t &fitOption)
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
virtual void Save(Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax)
Save values of function in array fSave.
const char * Data() const