# Problem drawing TGraphError error bars

**URL:** <https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053>\
**Category:** Newbie\
**Created:** [May 22, 2022, 6:48am UTC](https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053 "2022-05-22T06:48:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorsedh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gorsedh/32/25831_2.png) [@gorsedh](https://root-forum.cern.ch/u/gorsedh)\
**Post date:** [May 22, 2022, 6:48am UTC](https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053/1 "2022-05-22T06:48:49Z")

</div>

Hello there,  
I’m trying to draw a TGraphError but i can’t understand why i can’t get error bars to show.

Here’s my code:

```auto
void errplot(TString channel = "pk", TString bdtCut = "0.0", TString configuration = "3.3.3.-3"){

  using namespace std;
  using namespace RooFit;
  using namespace TMath;
  TString pidEffs_dir = "/home/path";

  TString pidEffs_name = pidEffs_dir + "/pidEffs_"+ bdtCut+ "_"+configuration+".root";
  TFile *pidEffs_file = new TFile(pidEffs_name, "READ");
  int pid1 = ((TObjString *) configuration.Tokenize(".")->At(0))->String().Atoi();
  int pid2 = ((TObjString *) configuration.Tokenize(".")->At(1))->String().Atoi();
  int pid3 = ((TObjString *) configuration.Tokenize(".")->At(2))->String().Atoi();
  int pid4 = ((TObjString *) configuration.Tokenize(".")->At(3))->String().Atoi();
  float bdt = bdtCut.Atof();

  TCanvas * c = new TCanvas("c", "c", 2200, 1800);
   c->Divide(2,2);
  c->cd(1);
  gPad->SetLeftMargin(0.15);
  ///////////////////////////////1
  auto g1 = new TGraphErrors();
  int pointCounter1 = 0;
  for(int pid1var:{1,3,5,7,9,11,13}){
    if(channel == "pk" ||(channel == "ppi" && pid4 <= pid1var)){
    TString currentConfiguration = Form("%s_%d.%d.%d.%d", bdtCut.Data(), pid1var, pid2, pid3, pid4);
    double sum=0;
    double sumErr=0;
    double sumOfSquares=0;
    TString toyResFile=Form("/home/path2.root");
    TFile * file = new TFile(toyResFile, "READ");
    int counter1 = 0;
    int currentPoint = 1;
    double mean = 1;
    double errErr = 0;
for(int i = 0;i<10;i++){
      TString nome_result = Form("result_0%d", i);
      auto result = (RooFitResult*)file->Get(nome_result);
      auto l = result->floatParsFinal();
      auto var = (RooRealVar*)l.at(2);
      double err = var->getError();
      auto edmRes = result->edm();
      auto covQualRes = result->covQual();
      if (covQualRes == 3 && edmRes < 0.001){
        sum+=err;
        sumOfSquares+=(err*err);
        counter1++;
      }
    }
    if(counter1>0){
      mean = sum/counter1;
    }
for(int i = 0;i<10;i++){
      TString nome_result = Form("result_0%d", i);
      auto result = (RooFitResult*)file->Get(nome_result);
      auto l = result->floatParsFinal();
      auto var = (RooRealVar*)l.at(2);
      double err = var->getError();
      auto edmRes = result->edm();
      auto covQualRes = result->covQual();
      if (covQualRes == 3 && edmRes < 0.001){
        sumErr+=((err-mean)*(err-mean));
      }
    }
    if(counter1>0){
      double RMSerr = Sqrt(sumErr/counter1);
      cout << pid1var << " " << counter1<< " " << mean << " +- " << RMSerr <<"\n";
      g1->SetPoint(currentPoint,pid1var,mean);
      g1->SetPointError(currentPoint, 0, RMSerr);
      currentPoint++;
    }

    file->Close();
    }
  }
//TGraphErrors *g1 = new TGraphErrors(pointCounter1, &x1[0], &y1[0], nullptr, &yerr1[0]);
  g1->SetLineWidth(0);
  g1->SetMarkerStyle(20);
  g1->SetMarkerSize(2);
    if(channel == "pk"){ g1->SetTitle(";#Delta log L_{p-#pi}(p); #sigma(A_{raw}(pK))");}
  else if(channel == "ppi"){ g1->SetTitle(";#Delta log L_{p-#pi}(p); #sigma(A_{raw}(p#pi))");}
  g1->GetYaxis()->SetMaxDigits(2);
  g1->GetYaxis()->SetLabelSize(0.04);
  g1->GetXaxis()->SetLabelSize(0.04);
  g1->GetXaxis()->SetTitleSize(0.04);
  g1->GetYaxis()->SetTitleSize(0.04);

  g1->Draw();

```

If i change  
g1-\>SetPoint(currentPoint,pid1var,mean);  
g1-\>SetPointError(currentPoint, 0, RMSerr);  
with  
g1-\>AddPoint(pid1var,mean);  
I get the expected result (without the error bars that i need)  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/c/2c3d27e104a3a983fa0a1e5d21f5b84884af6bc4.png)  
or with  
g1-\>SetPoint(g1-\>GetN(),pid1var,mean);  
g1-\>SetPointError(g1-\>GetN()-1, 0, RMSerr);  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/c/2cf5e28ffc0a11603d2dea3f718fb7756ac05494.png)  
which seems to have left space for the error bars (should be around 1% and 0.5% of the value) //5: 0.0100437 ± 0.000124156

Sidenote: A TgraphError method to add points with error should be very much needed  
g1-\>AddPoint(x,y,ex,ey);  
if it already exists i wasn’t able to find it

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 23, 2022, 6:39am UTC](https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053/2 "2022-05-23T06:39:58Z")

</div>

You set the errors with:

```auto
g1->SetPointError(currentPoint, 0, RMSerr);

```

So you have no errors along X and along Y they are equal to `RMSerr`. Very likely `RMSerr ` is very small compare to the Y axis units,

---

<div class="post-metadata">

**Author:** ![gorsedh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gorsedh/32/25831_2.png) [@gorsedh](https://root-forum.cern.ch/u/gorsedh)\
**Post date:** [May 23, 2022, 10:45am UTC](https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053/3 "2022-05-23T10:45:36Z")

</div>

I have the rmserr output from terminal and i would expect something like this.  
(the rms value in the previous post was wrong, don’t watch it, these error bars are the right ones)

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/1/81bb5bf751d7ddb0146950b6e3e3e1fa4d455f8a.png)

I also tried

```auto
   g1->AddPoint(pid1var,mean);
   g1->SetPointError(currentPoint, 0, RMSerr);

```

and got

![image](https://root-forum.cern.ch/uploads/default/original/3X/c/4/c4ef050a84ac50a8f3db9ff757f43f30dbb5b8c8.png)

while with

```auto
g1->SetPoint(currentPoint, pid1var,mean);
g1->SetPointError(currentPoint, 0, RMSerr);

```

the even stranger  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/c/bc787e660ab62f21ed023e9c816f484d65de5ba9.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 23, 2022, 11:17am UTC](https://root-forum.cern.ch/t/problem-drawing-tgrapherror-error-bars/50053/4 "2022-05-23T11:17:14Z")

</div>

Can you send a reproducer example ? the one you sent is to specific to be run anywhere.
