Link for Root Page :

// Fit this graph with function f1. // // f1 is an already predefined function created by TF1.

// Predefined functions such as gaus, expo and poln are automatically

// created by ROOT.


// The list of fit options is given in parameter option.

// option = "W" Set all weights to 1; ignore error bars

// = "U" Use a User specified fitting algorithm (via SetFCN)

// = "Q" Quiet mode (minimum printing)

// = "V" Verbose mode (default is between Q and V)

// = "E" Perform better Errors estimation using Minos technique

// = "B" User defined parameter settings are used for predefined functions

// like "gaus", "expo", "poln", "landau".

// Use this option when you want to fix one or more parameters for these functions.

// = "M" More. Improve fit results.

// It uses the IMPROVE command of TMinuit (see TMinuit::mnimpr)

// This algorithm attempts to improve the found local minimum by

// searching for a better one.

// = "R" Use the Range specified in the function range

// = "N" Do not store the graphics function, do not draw

// = "0" Do not plot the result of the fit. By default the fitted function

// is drawn unless the option "N" above is specified.

// = "+" Add this new fitted function to the list of fitted functions

// (by default, any previous function is deleted)

// = "C" In case of linear fitting, do not calculate the chisquare

// (saves time) // = "F" If fitting a polN, use the minuit fitter

// = "EX0" When fitting a TGraphErrors do not consider errors in the coordinate

// = "ROB" In case of linear fitting, compute the LTS regression

// coefficients (robust (resistant) regression), using

// the default fraction of good points

// "ROB=0.x" - compute the LTS regression coefficients, using

// 0.x as a fraction of good points

// = "S" The result of the fit is returned in the TFitResultPtr

// (see below Access to the Fit Result)


// When the fit is drawn (by default), the parameter goption may be used

// to specify a list of graphics options. See TGraphPainter for a complete

// list of these options.


// In order to use the Range option, one must first create a function

// with the expression to be fitted. For example, if your graph

// has a defined range between -4 and 4 and you want to fit a gaussian

// only in the interval 1 to 3, you can do:

// TF1 *f1 = new TF1("f1","gaus",1,3);

// graph->Fit("f1","R");



// Who is calling this function:


// Note that this function is called when calling TGraphErrors::Fit // or TGraphAsymmErrors::Fit ot TGraphBentErrors::Fit

// See the discussion below on error calulation.


// Linear fitting: // ===========


// When the fitting function is linear (contains the "++" sign) or the fitting

// function is a polynomial, a linear fitter is initialised.

// To create a linear function, use the following syntax: linear parts

// separated by "++" sign.

// Example: to fit the parameters of "[0]*x + [1]*sin(x)", create a

// TF1 *f1=new TF1("f1", "x++sin(x)", xmin, xmax);

// For such a TF1 you don't have to set the initial conditions.

// Going via the linear fitter for functions, linear in parameters, gives a

// considerable advantage in speed.


// Setting initial conditions:

// =======================


// Parameters must be initialized before invoking the Fit function.

// The setting of the parameter initial values is automatic for the

// predefined functions : poln, expo, gaus, landau. One can however disable

// this automatic computation by specifying the option "B".

// You can specify boundary limits for some or all parameters via

// f1->SetParLimits(p_number, parmin, parmax);

// If parmin>=parmax, the parameter is fixed

// Note that you are not forced to fix the limits for all parameters.

// For example, if you fit a function with 6 parameters, you can do:

// func->SetParameters(0,3.1,1.e-6,0.1,-8,100);

// func->SetParLimits(4,-10,-4);

// func->SetParLimits(5, 1,1);

// With this setup, parameters 0->3 can vary freely.

// Parameter 4 has boundaries [-10,-4] with initial value -8.

// Parameter 5 is fixed to 100.


// Fit range:

// ======


// The fit range can be specified in two ways:

// - specify rxmax > rxmin (default is rxmin=rxmax=0)

// - specify the option "R". In this case, the function will be taken

// instead of the full graph range.


// Changing the fitting function:

// ==========================


// By default a chi2 fitting function is used for fitting a TGraph.

// The function is implemented in FitUtil::EvaluateChi2.

// In case of TGraphErrors an effective chi2 is used (see below TGraphErrors fit)

// To specify a User defined fitting function, specify option "U" and

// call the following functions:

// TVirtualFitter::Fitter(mygraph)->SetFCN(MyFittingFunction)

// where MyFittingFunction is of type:

// extern void MyFittingFunction(Int_t &npar, Double_t *gin, Double_t &f,

// Double_t *u, Int_t flag);



// TGraphErrors fit:

// =============


// In case of a TGraphErrors object, when x errors are present, the error along x,

// is projected along the y-direction by calculating the function at the points x-exlow and

// x+exhigh. The chisquare is then computed as the sum of the quantity below at each point:


// Begin_Latex

// #frac{(y-f(x))^{2}}{ey^{2}+(#frac{1}{2}(exl+exh)f'(x))^{2}}

// End_Latex


// where x and y are the point coordinates, and f'(x) is the derivative of the

// function f(x).


// In case the function lies below (above) the data point, ey is ey_low (ey_high).


// thanks to Andy Haas ( for adding the case with TGraphAsymmErrors

// University of Washington


// The approach used to approximate the uncertainty in y because of the

// errors in x is to make it equal the error in x times the slope of the line.

// The improvement, compared to the first method (f(x+ exhigh) - f(x-exlow))/2

// is of (error of x)**2 order. This approach is called "effective variance method".

// This improvement has been made in version 4.00/08 by Anna Kreshuk.

// The implementation is provided in the function FitUtil::EvaluateChi2Effective


// NOTE:

// 1) By using the "effective variance" method a simple linear regression

// becomes a non-linear case, which takes several iterations

// instead of 0 as in the linear case.


// 2) The effective variance technique assumes that there is no correlation

// between the x and y coordinate.


// 3) The standard chi2 (least square) method without error in the coordinates (x) can

// be forced by using option "EX0"


// 4) The linear fitter doesn't take into account the errors in x. When fitting a

// TGraphErrors with a linear functions the errors in x willnot be considere.

// If errors in x are important, go through minuit (use option "F" for polynomial fitting).


// 5) When fitting a TGraph (i.e. no errors associated with each point),

// a correction is applied to the errors on the parameters with the following

// formula:

// errorp *= sqrt(chisquare/(ndf-1))


// Access to the fit result

// ====================

// The function returns a TFitResultPtr which can hold a pointer to a TFitResult object.

// By default the TFitResultPtr contains only the status of the fit which is return by an

// automatic conversion of the TFitResultPtr to an integer. One can write in this case

// directly:

// Int_t fitStatus = h->Fit(myFunc)


// If the option "S" is instead used, TFitResultPtr contains the TFitResult and behaves

// as a smart pointer to it. For example one can do:

// TFitResultPtr r = h->Fit(myFunc,"S");

// TMatrixDSym cov = r->GetCovarianceMatrix(); // to access the covariance matrix

// Double_t chi2 = r->Chi2(); // to retrieve the fit chi2

// Double_t par0 = r->Value(0); // retrieve the value for the parameter 0

// Double_t err0 = r->Error(0); // retrieve the error for the parameter 0

// r->Print("V"); // print full information of fit including covariance matrix

// r->Write(); // store the result in a file


// The fit parameters, error and chi2 (but not covariance matrix) can be retrieved also

// from the fitted function.

// If the histogram is made persistent, the list of

// associated functions is also persistent. Given a pointer (see above)

// to an associated function myfunc, one can retrieve the function/fit

// parameters with calls such as:

// Double_t chi2 = myfunc->GetChisquare();

// Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter

// Double_t err0 = myfunc->GetParError(0); //error on first parameter



// Access to the fit status

// =================

// The status of the fit can be obtained converting the TFitResultPtr to an integer

// indipendently if the fit option "S" is used or not:

// TFitResultPtr r = h=>Fit(myFunc,opt);

// Int_t fitStatus = r;


// The fitStatus is 0 if the fit is OK (i.e. no error occurred).

// The value of the fit status code is negative in case of an error not connected with the

// minimization procedure, for example when a wrong function is used.

// Otherwise the return value is the one returned from the minimization procedure.

// When TMinuit (default case) or Minuit2 are used as minimizer the status returned is :

// fitStatus = migradResult + 10*minosResult + 100*hesseResult + 1000*improveResult.

// TMinuit will return 0 (for migrad, minos, hesse or improve) in case of success and 4 in

// case of error (see the documentation of TMinuit::mnexcm). So for example, for an error

// only in Minos but not in Migrad a fitStatus of 40 will be returned.

// Minuit2 will return also 0 in case of success and different values in migrad, minos or

// hesse depending on the error. See in this case the documentation of

// Minuit2Minimizer::Minimize for the migradResult, Minuit2Minimizer::GetMinosError for the

// minosResult and Minuit2Minimizer::Hesse for the hesseResult.

// If other minimizers are used see their specific documentation for the status code

// returned. For example in the case of Fumili, for the status returned see TFumili::Minimize.


// Associated functions:

// =================


// One or more object (typically a TF1*) can be added to the list

// of functions (fFunctions) associated with each graph.

// When TGraph::Fit is invoked, the fitted function is added to this list.

// Given a graph gr, one can retrieve an associated function

// with: TF1 *myfunc = gr->GetFunction("myfunc");


// If the graph is made persistent, the list of associated functions is also

// persistent. Given a pointer (see above) to an associated function myfunc,

// one can retrieve the function/fit parameters with calls such as:

// Double_t chi2 = myfunc->GetChisquare();

// Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter

// Double_t err0 = myfunc->GetParError(0); //error on first parameter


// Fit Statistics

// ==========


// You can change the statistics box to display the fit parameters with

// the TStyle::SetOptFit(mode) method. This mode has four digits.

// mode = pcev (default = 0111)

// v = 1; print name/values of parameters

// e = 1; print errors (if e=1, v must be 1)

// c = 1; print Chisquare/Number of degress of freedom

// p = 1; print Probability


// For example: gStyle->SetOptFit(1011);

// prints the fit probability, parameter names/values, and errors.

// You can change the position of the statistics box with these lines

// (where g is a pointer to the TGraph):


// Root > TPaveStats st = (TPaveStats)g->GetListOfFunctions()->FindObject("stats")

// Root > st->SetX1NDC(newx1); //new x start position

// Root > st->SetX2NDC(newx2); //new x end position


Foption_t fitOption;

ROOT::Fit::FitOptionsMake(option, fitOption);

// create range and minimizer options with default values

ROOT::Fit::DataRange range(rxmin, rxmax);

ROOT::Math::MinimizerOptions minOption;

return ROOT::Fit::FitObject(this, f1 , fitOption , minOption, goption, range);


Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2013-02-04 - SubhashSingha
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Sandbox All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback