Hello,
I am new to ROOT and have a very simple question regarding a linear fit on a TGrapgErrors. I am in desperate need of help today.
I have looked in the Forum but could not find an answer. I apologize for such a simple question. Basically, I would like to ask how to read the errors in fit parameters if I use the TGRaphErrors->LeastSquareFit() method. I have attached a basic script at the bottom and would very much appreciate any help with this.
Here is what I am trying to do:
Create a TGraphError object and make a straight line fit to a small interval. I want to take into account errors in both “x” and “y”.
What I have attempted: I have tried it using two methods (1) define a function and fit the TGraphError object, and (2) Use the LeastSquareFit method of TGraphErrors.
I see the fits are slightly different in these two methods. If I use (1), I get read the fit parameters and errorbars on the fit parameters. If I use (2), I can read the fit parameters but not the error bars. Please tell me how I can do this.
Also, if I use method (1), can I specify ROOT to use Least Square Fit method? I thought that was the default method, but the parameters come out different in these two fits.
Thank you so very much for any help.
{
// create a simple TGraphErrors and make a linear fit on a portion
// of the graph.
const Int_t n = 10;
Double_t x[n] = {2.0, 3.0, 3.9, 5.0, 6.0, 6.8, 8.0, 9.0, 10.0, 11.0};
Double_t y[n] = {4.0, 3.6, 4.6, 5.0, 3.0, 4.1, 4.0, 6.0, 6.2, 3.9};
Double_t dx[n] = {0.01, 0.02, 0.06, 0.01, 0.03, 0.04, 0.02, 0.013, 0.07, 0.02};
Double_t dy[n] = {0.02, 0.03, 0.06, 0.02, 0.01, 0.04, 0.03, 0.010, 0.02, 0.05};
TGraphErrors * graphE = new TGraphErrors(n, x, y, dx, dy);
graphE->SetTitle("Two linear fits");
graphE->GetXaxis()->SetTitle("some x values");
graphE->GetYaxis()->SetTitle("some y values");
graphE->Draw("ALP");
// define a linear function and fit in a range
TF1* myF = new TF1("myF", "[0]+[1]*x", 2.0, 7.0);
Double_t myFPar[2];
Double_t myFParEr[2];
myF->SetLineColor(2);
graphE->Fit(myF, "R");
myF->GetParameters(&myFPar[0]);
myFParEr[0]=myF->GetParError(0);
myFParEr[1]=myF->GetParError(1);
// now use the LeastSquareFit in the same range
Double_t leastSqPar[2];
graphE->LeastSquareFit(2, leastSqPar, 2.0, 7.0);
cout<<" least sq fit pars are: "<<leastSqPar[0]<<" and "<<leastSqPar[1]<<endl;
// now draw a line using results of this LeastSquareFit, in color Blue.
// this is to compare the two linear fits.
TF1 * leastSqFitFn = new TF1("leastSqFitFn", "[0]+[1]*x", 2.0, 7.0);
leastSqFitFn->SetParameters(leastSqPar);
leastSqFitFn->SetLineColor(4);
leastSqFitFn->Draw("same");
TLatex l1;
l1.SetTextSize(0.03);
l1->DrawLatex(2.0, 6.3,"#color[2]{Red curve is fit using: [0]+[1]*x}");
l1->DrawLatex(2.0, 6.1,"#color[4]{Blue curve is fit using: TGraphErrors->LeastSquareFit}");
l1->DrawLatex(2.0, 5.9,"#color[6]{I want to use the TGraphErrors->LeastSquareFit but do not know how to}");
l1->DrawLatex(2.0, 5.7,"#color[6]{read the errors in fit parameters using this method. Please help! Thank you.}");
c1.SaveAs("GraphErrorsFitTest.png");
}