# How to plot TGraphErrors from a tree

**URL:** <https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233>\
**Category:** ROOT\
**Created:** [November 13, 2009, 8:09pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233 "2009-11-13T20:09:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![theovaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theovaf](https://root-forum.cern.ch/u/theovaf)\
**Post date:** [November 13, 2009, 8:09pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/1 "2009-11-13T20:09:42Z")

</div>

Dear all,  
I have created a tree with 3 branches x, y, yerror and then i want to read the branches and make a plot with errors.I do the following and it doesn’t work:

```cpp
TBranch *TX=tree->GetBranch("X");
TBranch *TY=tree->GetBranch("Y");
TBranch *TYerror=tree->GetBranch("Yerror");

double x[N],y[N],yerr[N];

TX->SetAddress(&x);
TY->SetAddress(&y);
TYerror->SetAddress(&yerr);
    
   for (int k=0;k<N;k++)
    {
      x[k]=TX->GetEntry(k);
      y[k]=TY->GetEntry(k);
      yerr[k]=TYerror->GetEntry(k);
    }
   
   TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500);
   
   TGraphErrors *gr = new TGraphErrors(N,x,y,yerr);

```

the branches have the correct values (i can verify this with the TBrowser) but if i plot it like this i get crazy values  
thanks  
t

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 14, 2009, 6:09pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/2 "2009-11-14T18:09:04Z")

</div>

You can do:

```cpp
TBranch *TX=tree->GetBranch("X"); 
TBranch *TY=tree->GetBranch("Y"); 
TBranch *TYerror=tree->GetBranch("Yerror"); 

double x[N],y[N],yerr[N]; 

TX->SetAddress(x); 
TY->SetAddress(y); 
TYerror->SetAddress(yerr); 

for (int k=0;k<N;k++) {
   x[k]=TX->GetEntry(k); 
   y[k]=TY->GetEntry(k); 
   yerr[k]=TYerror->GetEntry(k); 
} 

TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500); 

TGraphErrors *gr = new TGraphErrors(N,x,y,yerr); 

```

or simply

```cpp
Long64_t N = tree.Draw("X:Y:Yerror","","goff");
TGraphErrors *gr = new TGraphErrors(N,tree->GetV1(),tree->GetV2(),tree->GetV3()); 

```

Rene

---

<div class="post-metadata">

**Author:** ![theovaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theovaf](https://root-forum.cern.ch/u/theovaf)\
**Post date:** [November 15, 2009, 1:44pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/3 "2009-11-15T13:44:04Z")

</div>

Hallo rene,  
thanks for the quick reply.  
The First solution did not work but the second one did.  
I cannot keep thinking why the first solution didn’t work.can you please revise it and give me any more ideas?  
also is there a way to smooth the lines in the TGraphError directly, i.e. take the mean of each 10 points, or i have to do it before in the trees.  
thanks again,  
cheers  
thodoros

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 15, 2009, 5:12pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/4 "2009-11-15T17:12:00Z")

</div>

Could you post your exact first version? In particular, did you notice teh difference between my first version and yours, in particular  
`TX->SetAddress(x); TY->SetAddress(y); TYerror->SetAddress(yerr);`  
instead of  
`TX->SetAddress(&x); TY->SetAddress(&y); TYerror->SetAddress(&yerr);`  
Rene

---

<div class="post-metadata">

**Author:** ![theovaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theovaf](https://root-forum.cern.ch/u/theovaf)\
**Post date:** [November 15, 2009, 6:02pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/5 "2009-11-15T18:02:48Z")

</div>

yes i have noticed and implemented your change but had the same results.  
so my exact code is the following:

```cpp
// creating the tree:

sprintf(treename,"Radon Data",200000);
    sprintf(treetitle,"Tree of the Radon Data");

    TTree *tree = (TTree*)outRf->Get(treename);

    if (tree==NULL)
    {
	tree = new TTree(treename,treetitle);

	tree->Branch("labviewtime", &labviewtime, "labviewtime/D");
	tree->Branch("HumidityRel.", &data[0], "HumidityRel./D");
	tree->Branch("AirPressure", &data[1], "AirPressure/D");
	tree->Branch("Rn222", &data[2], "Rn222/D");
	tree->Branch("Rn222error", &data[3], "Rn222error/D");
	tree->Branch("Temperature", &data[4], "Temperature/D");
    }
    
}
// i fill the Ttree named tree..
}

const int N=nlines-1;
    double x[N],y[N],yerr[N];
    
      for (int k=0;k<N;k++)
      {x[N]=0;y[N]=0;yerr[N]=0;
      }
      
    TBranch *Tlabviewtime=tree->GetBranch("labviewtime");
    TBranch *TRn222=tree->GetBranch("Rn222");
    TBranch *TRn222error=tree->GetBranch("Rn222error");
    Tlabviewtime->SetAddress(x);
    TRn222->SetAddress(y);
    TRn222error->SetAddress(yerr);
    
   for (int k=0;k<N-1;k++)
    {
      x[k]=Tlabviewtime->GetEntry(k);
      cout<<x[k]<<endl;
      y[k]=TRn222->GetEntry(k);
      yerr[k]=TRn222error->GetEntry(k);
    }
    
   
   TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500);

   c1->SetFillColor(42);
   c1->SetGrid();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);

   //const Int_t n = 10;
   
   TGraphErrors *gr = new TGraphErrors(N,x,y,0,yerr);
   
   gr->SetTitle("Radon");
   //gr->SetMarkerColor(4);
   //gr->SetMarkerStyle(21);
   //gr->SetLineColor(4);
   gr->Draw("LPA");
   c1->Update();

```

hope this helps  
cheers

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 16, 2009, 9:22pm UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/6 "2009-11-16T21:22:05Z")

</div>

Hi,

`TBranch::GetEntry` returns the number of bytes read from the disk (after decompression), so doing `x[k]=TX->GetEntry(k);` is clearly not what you intended. Instead use:

```cpp
TBranch *TX=tree->GetBranch("X"); 
TBranch *TY=tree->GetBranch("Y"); 
TBranch *TYerror=tree->GetBranch("Yerror"); 

double xval,yval,yerrval
double x[N],y[N],yerr[N]; 

// Here guessing that the branch contains each a single double.
TX->SetAddress(&xval); 
TY->SetAddress(&yval); 
TYerror->SetAddress(&yerr); 

for (int k=0;k<N;k++) { 
   TX->GetEntry(k);
   x[k] = xval;
   TY->GetEntry(k);
   y[k] = yval;
   TYerror->GetEntry(k);
   yerr[k] = yerrval;
} 

TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500); 

TGraphErrors *gr = new TGraphErrors(N,x,y,yerr);[/code]

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![theovaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theovaf](https://root-forum.cern.ch/u/theovaf)\
**Post date:** [November 17, 2009, 10:17am UTC](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/7 "2009-11-17T10:17:30Z")

</div>

yes this was the problem.  
thanks a lot!!!
