Home | News | Documentation | Download

How to plot error bars along with fitthing the curve in TGraph?

ROOT Version: 6.22
Platform: Ubuntu


I want to plot error bars and fit the graph along with it. The code I have written is,

{
  f = new TF1("fit", "[0]*x + [1]",0,5);
  f -> SetParNames("Slope", "Interceptor");
  double x[5]={2.68,4.73,0.415, 4.34, 4.73}, y[5] = {661.66, 1274.54,122.061, 1173.24, 1332.51}, xerr[5]= {0.119,0.10625, 0.10625, 0.204, 0.21675}, yerr[5]={0,0,0,0,0};
  g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(fit, "R");  // Had also put f instead of fit and trying without "R" also but same graph showing
  g->Draw(); // Also tried with "AP" , "A", "A*" but nothing happened
}

But there is still a problem remains. the fitting line did not show up on the graph.
please help me on it.

Thank you,
Dsubhro.

Your points along X are not ordered. The fit did not work

Warning in <Fit>: Abnormal termination of minimization.

the following simple TGraph fitting example works:

{
   gStyle->SetOptFit(1);
   Double_t x[] = {1., 2., 3.};
   Double_t y[] = {2., 3., 2.};
   auto g = new TGraph(3, x, y);
   g->Fit("gaus");
   g->Draw("A*");
}

Sir,
I need to plot the error bars also along with the fitting curve. Can you please tell me how to do that??

Use TGraphErrors as you did beforeā€¦ but as I sais your point along X are not ordered and the fit did not work. Fix that and you will get the plot.

Sir, I have written the code like,

{
  TF1 * f = new TF1("fit", "[0]*x + [1]",0,5);
  gStyle -> SetOptFit(1);
  f -> SetParNames("Slope", "Interceptor");
  double x[5]={0.415,2.68,4.34,4.73,4.73}, y[5] = {122.061,661.66,1173.24,1274.54,1332.51}, xerr[5]= {0.10625,0.119,0.204,0.10625,0.21675}, yerr[5]={0,0,0,0,0};
  TGraphErrors *g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(f);
  g->Draw("A*");
}

but the output:

Warning in <Fit>: Abnormal termination of minimization.
 FCN=0 FROM MIGRAD    STATUS=FAILED        102 CALLS         103 TOTAL
                     EDM=44636    STRATEGY= 1  ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER                APPROXIMATE        STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Slope        0.00000e+00   5.72655e-32   0.00000e+00  -3.68935e+33
   2  Interceptor   0.00000e+00   1.41421e+00  -0.00000e+00   0.00000e+00
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

and the output file: graph.pdf (13.6 KB)

Notice the fit did not work. You have the message:

Warning in <Fit>: Abnormal termination of minimization.

May be @moneta can help.

Yes sir, I did notice that even after ordering the the points along X axis.

You should also set some initial value for the parameters:

{
  TF1 * f = new TF1("fit", "[0]*x + [1]",0,5);
  gStyle -> SetOptFit(1);
  f->SetParNames("Slope", "Interceptor");
  f->SetParameter(0, 10);
  f->SetParameter(1, 10);
  double x[5]={0.415,2.68,4.34,4.73,4.73},
          y[5] = {122.061,661.66,1173.24,1274.54,1332.51},
          xerr[5]= {0.10625,0.119,0.204,0.10625,0.21675},
          yerr[5]={0,0,0,0,0};
  TGraphErrors *g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(f);
  g->Draw("A*");
}

Thank you very much.