New ROOT user: a basic question on Fitting. I need help


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");

        // 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];
        graphE->Fit(myF, "R");

        // 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);

        TLatex l1;
        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.}");



Use TGraphErrors::Fit() and not the other one. The other method does not consider the point weight (i.e the errors), as it is documented in … tSquareFit

TGraphErrorsFit::Fit uses by default the errors in both X and Y and it is also a least square fit.

Best Regards