|
ROOT
Reference Guide |
|
Go to the documentation of this file.
73 template <
class FitObject>
76 template <
class FitObject>
79 template <
class FitObject>
89 Error(
"Fit",
"function may not be null pointer");
93 Error(
"Fit",
"function is zombie");
99 Error(
"Fit",
"function %s has illegal number of parameters = %d",
f1->
GetName(), npar);
105 Error(
"Fit",
"function %s dimension, %d, is greater than fit object dimension, %d",
110 Error(
"Fit",
"function %s dimension, %d, is smaller than fit object dimension -1, %d",
122 Double_t fxmin, fymin, fzmin, fxmax, fymax, fzmax;
123 f1.
GetRange(fxmin, fymin, fzmin, fxmax, fymax, fzmax);
132 template<
class FitObject>
139 printf(
"fit function %s\n",
f1->
GetName() );
145 if (iret != 0)
return iret;
151 if (fitOption.
Integral)
Info(
"Fit",
"Ignore Integral option. Model function dimension is less than the data object dimension");
152 if (fitOption.
Like)
Info(
"Fit",
"Ignore Likelihood option. Model function dimension is less than the data object dimension");
160 if (special==299+npar) linear =
kTRUE;
166 std::shared_ptr<TFitResult> tfr(
new TFitResult() );
168 std::shared_ptr<ROOT::Fit::Fitter> fitter(
new ROOT::Fit::Fitter(std::static_pointer_cast<ROOT::Fit::FitResult>(tfr) ) );
189 printf(
"use range \n" );
194 printf(
"range size %d\n", range.
Size(0) );
197 printf(
" range in x = [%f,%f] \n",
x1,
x2);
204 if (fitdata->Size() == 0 ) {
205 Warning(
"Fit",
"Fit data is empty ");
210 printf(
"HFit:: data size is %d \n",fitdata->Size());
211 for (
unsigned int i = 0; i < fitdata->Size(); ++i) {
212 if (fitdata->NDim() == 1) printf(
" x[%d] = %f - value = %f \n", i,*(fitdata->Coords(i)),fitdata->Value(i) );
222 if (special != 0 && !fitOption.
Bound && !linear) {
235 if ( (linear || fitOption.
Gradient) )
237 #ifdef R__HAS_VECCORE
249 if (
int(fitdata->NDim()) == hdim -1 ) fitConfig.
SetNormErrors(
true);
257 for (
int i = 0; i < npar; ++i) {
263 if (plow*pup != 0 && plow >= pup) {
266 else if (plow < pup ) {
281 double step = 0.1 * (pup - plow);
283 if ( parSettings.
Value() < pup && pup - parSettings.
Value() < 2 * step )
284 step = (pup - parSettings.
Value() ) / 2;
285 else if ( parSettings.
Value() > plow && parSettings.
Value() - plow < 2 * step )
286 step = (parSettings.
Value() - plow ) / 2;
315 std::string
type =
"Robust";
341 if (fitOption.
Like) printf(
"do likelihood fit...\n");
342 if (linear) printf(
"do linear fit...\n");
343 printf(
"do now fit...\n");
358 if (fitOption.
User && userFcn)
359 fitok = fitter->FitFCN( userFcn );
360 else if (fitOption.
Like) {
362 bool weight = ((fitOption.
Like & 2) == 2);
364 bool extended = ((fitOption.
Like & 4 ) != 4 );
368 fitok = fitter->LikelihoodFit(fitdata, extended, fitOption.
ExecPolicy);
371 fitok = fitter->Fit(fitdata, fitOption.
ExecPolicy);
373 if ( !fitok && !fitOption.
Quiet )
374 Warning(
"Fit",
"Abnormal termination of minimization.");
380 iret = fitResult.
Status();
404 if (!fitOption.
Quiet) {
405 if (fitter->GetMinimizer() && fitConfig.
MinimizerType() ==
"Minuit" &&
407 fitter->GetMinimizer()->PrintResults();
412 fitResult.
Print(std::cout);
427 bcfitter->
SetFCN(userFcn);
453 tfr->SetTitle(title);
468 if (range.
Size(0) == 0) {
479 if (range.
Size(1) == 0) {
490 if (range.
Size(2) == 0) {
501 std::cout <<
"xmin,xmax" <<
xmin <<
" " <<
xmax << std::endl;
520 else if (range.
Size(0) == 0) {
522 double xmin = std::numeric_limits<double>::infinity();
523 double xmax = -std::numeric_limits<double>::infinity();
524 TIter next(
mg->GetListOfGraphs() );
526 while ( (
g = (
TGraph*) next() ) ) {
527 double x1 = 0,
x2 = 0, y1 = 0, y2 = 0;
528 g->ComputeRange(
x1,y1,
x2,y2);
542 if (range.
Size(0) == 0) {
543 double xmin =
gr->GetXmin();
544 double xmax =
gr->GetXmax();
547 if (range.
Size(1) == 0) {
548 double ymin =
gr->GetYmin();
549 double ymax =
gr->GetYmax();
560 for (
int i = 0; i < ndim; ++i ) {
561 if ( range.
Size(i) == 0 ) {
568 template<
class FitObject>
574 std::cout <<
"draw and store fit function " <<
f1->
GetName() << std::endl;
586 std::cout <<
"draw and store fit function " <<
f1->
GetName()
587 <<
" Range in x = [ " <<
xmin <<
" , " <<
xmax <<
" ]" << std::endl;
592 Error(
"StoreAndDrawFitFunction",
"Function list has not been created - cannot store the fitted function");
600 bool reuseOldFunction =
false;
601 if (delOldFunction) {
604 while ((obj = next())) {
611 reuseOldFunction =
true;
623 if (!reuseOldFunction) {
624 fnew1 = (
TF1*)
f1->IsA()->New();
627 funcList->
Add(fnew1);
637 }
else if (ndim < 3) {
638 if (!reuseOldFunction) {
639 fnew2 = (
TF2*)
f1->IsA()->New();
642 funcList->
Add(fnew2);
645 fnew2 =
dynamic_cast<TF2*
>(
f1);
654 if (!reuseOldFunction) {
655 fnew3 = (
TF3*)
f1->IsA()->New();
658 funcList->
Add(fnew3);
661 fnew2 =
dynamic_cast<TF3*
>(
f1);
672 if (drawFunction && ndim < 3 && h1->InheritsFrom(
TH1::Class() ) ) {
675 if (!
gPad || (
gPad &&
gPad->GetListOfPrimitives()->FindObject(
h1) == NULL ) )
691 if (option == 0)
return;
692 if (!option[0])
return;
740 int start = opt.
Index(
"H=0.");
741 int numpos = start + strlen(
"H=0.");
744 while( (numpos+numlen<len) && isdigit(opt[numpos+numlen]) ) numlen++;
745 TString num = opt(numpos,numlen);
746 opt.
Remove(start+strlen(
"H"),strlen(
"=0.")+numlen);
747 h = atof(num.
Data());
765 if (fitOption.
Like == 1) {
769 if (fitOption.
Like == 2) fitOption.
Like = 6;
770 else fitOption.
Like = 4;
775 if (fitOption.
Chi2 == 1 || fitOption.
PChi2 == 1)
776 Warning(
"Fit",
"Cannot use P or X option in combination of L. Ignore the chi2 option and perform a likelihood fit");
783 if (fitOption.
PChi2 && fitOption.
W1) {
784 Warning(
"FitOptionsMake",
"Ignore option W or WW when used together with option P (Pearson chi2)");
804 Info(
"CheckGraphFitOptions",
"L (Log Likelihood fit) is an invalid option when fitting a graph. It is ignored");
808 Info(
"CheckGraphFitOptions",
"I (use function integral) is an invalid option when fitting a graph. It is ignored");
820 std::shared_ptr<ROOT::Fit::UnBinData> fitdata(data);
823 printf(
"tree data size is %d \n",fitdata->Size());
824 for (
unsigned int i = 0; i < fitdata->Size(); ++i) {
825 if (fitdata->NDim() == 1) printf(
" x[%d] = %f \n", i,*(fitdata->Coords(i) ) );
828 if (fitdata->Size() == 0 ) {
829 Warning(
"Fit",
"Fit data is empty ");
834 std::shared_ptr<TFitResult> tfr(
new TFitResult() );
840 unsigned int dim = fitdata->NDim();
846 assert ( (
int) dim == fitfunc->
GetNdim() );
855 for (
int i = 0; i < npar; ++i) {
860 if (plow*pup != 0 && plow >= pup) {
863 else if (plow < pup ) {
878 double step = 0.1 * (pup - plow);
880 if ( parSettings.
Value() < pup && pup - parSettings.
Value() < 2 * step )
881 step = (pup - parSettings.
Value() ) / 2;
882 else if ( parSettings.
Value() > plow && parSettings.
Value() - plow < 2 * step )
883 step = (parSettings.
Value() - plow ) / 2;
905 if ( (fitOption.
Like & 2) == 2)
908 bool extended = (fitOption.
Like & 1) == 1;
911 fitok = fitter->LikelihoodFit(fitdata, extended, fitOption.
ExecPolicy);
912 if ( !fitok && !fitOption.
Quiet )
913 Warning(
"UnBinFit",
"Abnormal termination of minimization.");
917 int iret = fitResult.
Status();
942 if (lastFitter)
delete lastFitter;
952 else if (!fitOption.
Quiet) fitResult.
Print(std::cout);
961 tfr->SetTitle(title);
976 Warning(
"HFit::FitObject",
"A weighted likelihood fit is requested but histogram is not weighted - do a standard Likelihood fit");
1033 template<
class FitObject>
1045 if (data.
Size() == 0 ) {
1046 Warning(
"Chisquare",
"data set is empty - return -1");
static constexpr double mg
const std::string & MinimizerType() const
return type of minimizer package
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
Class to manage histogram axis.
void GetFunctionRange(const TF1 &f1, ROOT::Fit::DataRange &range)
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
void SetWeightCorrection(bool on=true)
apply the weight correction for error matric computation
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
bool fUseEmpty
normalize data by a normalized the bin volume (bin volume divided by a reference value)
virtual Int_t GetSumw2N() const
void SetParabErrors(bool on=true)
set parabolic erros
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
void SetMinimizerOptions(const ROOT::Math::MinimizerOptions &minopt)
set all the minimizer options using class MinimizerOptions
void GetDrawingRange(TH1 *h1, ROOT::Fit::DataRange &range)
void StoreAndDrawFitFunction(FitObject *h1, TF1 *f1, const ROOT::Fit::DataRange &range, bool, bool, const char *goption)
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
DataOptions : simple structure holding the options on how the data are filled.
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Chi2FCN class for binnned fits using the least square methods.
bool fExpErrors
use all errors equal to 1, i.e. fit without errors (default is false)
void SetLowerLimit(double low)
set a single lower limit
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
double Value() const
copy constructor and assignment operators (leave them to the compiler)
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
virtual Double_t * GetParameters() const
void GetRange(unsigned int irange, unsigned int icoord, double &xmin, double &xmax) const
get the i-th range for given coordinate.
const char * Data() const
virtual const char * GetTitle() const
Returns title of object.
virtual Int_t GetNdim() const
void FitOptionsMake(EFitObjectType type, const char *option, Foption_t &fitOption)
Decode list of options into fitOption.
virtual Int_t GetNpar() const
virtual void SetMethodCall(TMethodCall *m)
void FillData(BinData &dv, const TH1 *hist, TF1 *func=0)
fill the data vector from a TH1.
static void SetFitter(TVirtualFitter *fitter, Int_t maxpar=25)
Static function to set an alternative fitter.
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...
Extends the ROOT::Fit::Result class with a TNamed inheritance providing easy possibility for I/O.
bool fCoordErrors
use expected errors from the function and not from the data
Int_t GetFirst() const
Return first bin on the axis i.e.
static constexpr double s
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.
int GetDimension(const THnBase *s1)
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)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
bool fUseRange
use empty bins (default is false) with a fixed error of 1
void SetNormErrors(bool on=true)
set the option to normalize the error on the result according to chi2/ndf
virtual void SetParErrors(const Double_t *errors)
Set errors for all active parameters when calling this function, the array errors must have at least ...
TList * GetListOfFunctions() const
virtual void SetObjectFit(TObject *obj)
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.
R__EXTERN TVirtualMutex * gGlobalMutex
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
void Fix()
fix the parameter
Abstract Base Class for Fitting.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
unsigned int Ndf() const
Number of degree of freedom.
Backward compatible implementation of TVirtualFitter.
void SetPrintLevel(int level)
set print level
virtual void GetParLimits(Int_t ipar, Double_t &parmin, Double_t &parmax) const
Return limits for parameter ipar.
TString & ReplaceAll(const TString &s1, const TString &s2)
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...
const Bool_t kIterBackward
static const double x1[5]
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
virtual Int_t GetNumber() const
virtual Int_t GetDimension() const
virtual void SetParameters(const Double_t *params)
void ToUpper()
Change string to upper case.
void SetStepSize(double err)
set the step size
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,...
Graphics object made of three arrays X, Y and Z with the same number of points each.
void PrintCovMatrix(std::ostream &os) const
print error matrix and correlations
Provides an indirection to the TFitResult class and with a semantics identical to a TFitResult pointe...
TH1F * GetHistogram() const
Returns a pointer to the histogram used to draw the axis Takes into account the two following cases.
Fitter class, entry point for performing all type of fits.
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
double ComputeChi2(const FitObject &h1, TF1 &f1, bool useRange, bool usePL)
TString & Remove(Ssiz_t pos)
void SetUpperLimit(double up)
set a single upper limit
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
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...
std::string ToString(const T &val)
Utility function for conversion to strings.
class containg the result of the fit and all the related information (fitted parameter values,...
virtual void SetChisquare(Double_t chi2)
virtual void Copy(TObject &f1) const
Copy this F1 to a new F1.
bool IsEmpty() const
True if a fit result does not exist (even invalid) with parameter values.
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...
class describing the range in the coordinates it supports multiple range in a coordinate.
Int_t GetLast() const
Return last bin on the axis i.e.
int GetDimension(const TH1 *h1)
A 3-Dim function with parameters.
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
void SetTolerance(double tol)
set the tolerance
void CheckGraphFitOptions(Foption_t &fitOption)
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
virtual void SetNumberFitPoints(Int_t npfits)
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)
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
bool NormalizeErrors() const
flag to check if resulting errors are be normalized according to chi2/ndf
bool fErrors1
use the function range when creating the fit data (default is false)
A 2-Dim function with parameters.
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
R__ALWAYS_INLINE Bool_t IsZombie() const
A TMultiGraph is a collection of TGraph (or derived) objects.
bool fBinVolume
use integral of bin content instead of bin center (default is false)
unsigned int Size() const
return number of fit points
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
virtual Bool_t AddToGlobalList(Bool_t on=kTRUE)
Add to global list of functions (gROOT->GetListOfFunctions() ) return previous status (true if the fu...
ROOT::EExecutionPolicy ExecPolicy
unsigned int Size(unsigned int icoord=0) const
return range size for coordinate icoord (starts from zero) Size == 0 indicates no range is present [-...
virtual Double_t GetParError(Int_t ipar) const
Return value of parameter number ipar.
A TGraph is an object made of two arrays X and Y with npoints each.
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 TObject * Remove(TObject *obj)
Remove object from the list.
TMethodCall * GetMethodCall() const
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
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...
virtual void Add(TObject *obj)
Mother of all ROOT objects.
void SetMinosErrors(bool on=true)
set Minos erros computation to be performed after fitting
TH1 is the base class of all histogram classes in ROOT.
double Chi2() const
Chi2 fit value in case of likelihood must be computed ?
int CheckFitFunction(const TF1 *f1, int hdim)
int Status() const
minimizer status code
static const double x2[5]
void(* FCNFunc_t)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
virtual void SetFitOption(Foption_t option)
virtual const char * GetName() const
Returns name of object.
virtual void SetParent(TObject *p=0)
static TVirtualFitter * GetFitter()
static: return the current Fitter
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
const std::vector< double > & Errors() const
parameter errors (return st::vector)
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.
void FitOptionsMake(const char *option, Foption_t &fitOption)
const std::vector< double > & Parameters() const
parameter values (return std::vector)
bool fNormBinVolume
normalize data by the bin volume (it is used in the Poisson likelihood fits)
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Int_t Finite(Double_t x)
Check if it is finite with a mask in order to be consistent in presence of fast math.
virtual Bool_t IsLinear() const
virtual void SetUserFunc(TObject *userfunc)
Multidimensional histogram base.
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
virtual void Draw(Option_t *option="")
Draw this histogram with options.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
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 ...