Getting error in TGraphError plot

Hello,
I am trying to get errors in a TGraphError plot. I have tried GetErrorY, but it has not worked so far. Following is a simple snippet of my code -

double mass[] = {12, 16, 20, 25, 30, 40, 50, 60, 77};
double sigma[] = {0.263,0.345,0.442,0.53,0.633,0.897,1.106,1.283,1.892};
double err[9];
TCanvas* c1 = new TCanvas("c1", "c1", 600, 600);
auto res = new TGraphErrors(9,mass,sigma);
for(int i = 0; i<9; i++){
  res->SetPoint(i, mass[i], sigma[i]);
  err[i] = res->GetEY()[i];
  res->SetPointError(i, 0, err[i]); 
}
res->Draw("ALP");
c1->Update();

But I do not see any errors in the plot I get, is there anything I am missing here ?
Thanks !
Best,
Shreya

What are you trying to do ? The errors in TGraphErrors should be defined by the user. ie:

{
   double mass[]  = {12, 16, 20, 25, 30, 40, 50, 60, 77};
   double sigma[] = {0.263,0.345,0.442,0.53,0.633,0.897,1.106,1.283,1.892};
   double ex[]    = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};
   double ey[]    = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};
   auto res = new TGraphErrors(9,mass,sigma,ex,ey);
   res->Draw("AL*");
}

Hi @couet,
Okay I see, I misunderstood that there is a method for TGraphError to calculate the errors, similar to GetBinError in histograms.
Thanks for the reply !

Best,
Shreya

err[i] = std::sqrt(std::abs(res->GetY()[i]));

Yes, you should set the errors yourself (see @Wile_E_Coyote post), TGraphErrors won’t calculate them for you.

Thanks a lot for the clarifications @Wile_E_Coyote and @couet ! I solved it now, after setting the errors :slight_smile:
Best,
Shreya

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.