Fit parameter result by user-defined functions

Hi,
The question is about fitting error results.

I defined a function, and use it to fit a histogram or graph. The printed fitting error result on the screen is not same as the one printed on the canvas (if I use SetOptFit to print them on my canvas) and obtained by access methods GetParError.

An example code here:

test_fit_err() {

  double x[5] = {1, 2, 3, 4, 5};
  double y[5] = {1.3, 1.4, 1.2, 1.25, 1.35};

  TGraph * g = new TGraph(5, x, y);

  g->SetMarkerStyle(20);
  g->Draw("ap");

  TF1 * f = new TF1("func", "[0]+[1]*x", 0.2, 0.6 );
  f->SetLineStyle(2);
  f->SetLineColor(8);
  g->Fit(f);
  //g->Fit("pol0");

  TF1 *myfunc = g->GetFunction("func");
  //TF1 *myfunc = g->GetFunction("pol0");

  gStyle->SetOptFit(11111);
  
  Double_t par0 = myfunc->GetParameter(0);
  Double_t err0 = myfunc->GetParError(0);
  cout << "FitPar: " <<  par0  << " FitError: " << err0 << endl;
}

I tried pre-defined-in-root functions, the fitting error results from the screen and the canvas are consistent. I don’t know why. Which error result should be used?

Cheers,
Zhiyi.

see documentation of TGraph::Fit at root.cern.ch/root/html/TGraph.html#TGraph:Fit

in particular the following lines:

3) When fitting a TGraph (ie no errors associated to each point), a correction is applied to the errors on the parameters with the following formula: errorp *= sqrt(chisquare/(ndf-1))

Rene

Okay. But I am still confused about why predefined-function fitting gives consistent error results while user-defined-function cannot.

There is a good reason to do correction when TGraph doesn’t have errors for points by :

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

?

see doc I was referring to

Rene