# How to plot a TGraphError with branches

**URL:** <https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730>\
**Category:** ROOT\
**Created:** [February 27, 2021, 11:07am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730 "2021-02-27T11:07:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 11:07am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/1 "2021-02-27T11:07:29Z")

</div>

Hello everyone,  
as the title says I would like to graph points taken from the entries with which the branches of a tree have been filled.  
Taking a cue from a similar topic ([How to plot TGraphErrors from a tree - #5 by theovaf](https://root-forum.cern.ch/t/how-to-plot-tgrapherrors-from-a-tree/9233/5)) I wrote the following code:

```auto
  TFile *f = new TFile("example.root");
  TTree *t = (TTree *)f->Get("DataFit");
  
  TBranch *TX = t->GetBranch("X"); TBranch *TXerror = t->GetBranch("Xerr");
  TBranch *TY = t->GetBranch("Y"); TBranch *TYerror = t->GetBranch("Yerr");
  
  Int_t N = t->GetEntries();
  
  Double_t xval, xerrval, yval, yerrval;
  Double_t x[N], xerr[N], y[N], yerr[N];
  
  TX->SetAddress(&xval); TXerror->SetAddress(&xerrval);
  TY->SetAddress(&yval); TYerror->SetAddress(&yerrval);
  
  for(Int_t k=0;k<N;k++) {
    x[k] = TX->GetEntry(k);
     xerr[k] = TXerror->GetEntry(k);
     y[k] = TY->GetEntry(k);
     yerr[k] = TYerror->GetEntry(k);
    }
 
  TGraphErrors *gr = new TGraphErrors(N, x, xerr, y, yerr); 
  gr->Draw("AP");

```

but it doesn’t work.  
There is most likely an address problem that I cannot see. Could anyone help me?  
Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 27, 2021, 11:11am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/2 "2021-02-27T11:11:23Z")

</div>

Post the output of: `t->Print();`

BTW. You probably want:

```cpp
  Double_t xval, xerrval, yval, yerrval;
  TX->SetAddress(&xval); TXerror->SetAddress(&xerrval);
  TY->SetAddress(&yval); TYerror->SetAddress(&yerrval);
  
  Long64_t N = t->GetEntries();
  TGraphErrors *gr = new TGraphErrors(N);
  
  for(Long64_t k = 0; k < N; k++) {
    TX->GetEntry(k); TXerror->GetEntry(k);
    TY->GetEntry(k); TYerror->GetEntry(k);
    gr->SetPoint(k, xval, yval);
    gr->SetPointError(k, xerrval, yerrval);
  }

```

---

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 11:33am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/3 "2021-02-27T11:33:30Z")

</div>

That’s the output:

```auto
******************************************************************************
*Tree :DataFit : *
*Entries : 26 : Total = 15166 bytes File Size = 6518 *
* : : Tree compression factor = 1.02 *
******************************************************************************
*Br 0 :kaonm : kaonm/D *
*Entries : 26 : Total Size= 776 bytes File Size = 272 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 1 :ekaonm : ekaonm/D *
*Entries : 26 : Total Size= 781 bytes File Size = 271 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 2 :sigma1 : sigma1/D *
*Entries : 26 : Total Size= 781 bytes File Size = 284 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 3 :esigma1 : esigma1/D *
*Entries : 26 : Total Size= 786 bytes File Size = 285 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 4 :sigma2 : sigma2/D *
*Entries : 26 : Total Size= 781 bytes File Size = 284 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 5 :esigma2 : esigma2/D *
*Entries : 26 : Total Size= 786 bytes File Size = 285 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 6 :alpha1 : alpha1/D *
*Entries : 26 : Total Size= 781 bytes File Size = 284 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 7 :ealpha1 : ealpha1/D *
*Entries : 26 : Total Size= 786 bytes File Size = 285 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 8 :alpha2 : alpha2/D *
*Entries : 26 : Total Size= 781 bytes File Size = 284 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 9 :ealpha2 : ealpha2/D *
*Entries : 26 : Total Size= 786 bytes File Size = 285 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 10 :n1 : n1/D *
*Entries : 26 : Total Size= 761 bytes File Size = 280 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 11 :en1 : en1/D *
*Entries : 26 : Total Size= 766 bytes File Size = 281 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 12 :n2 : n2/D *
*Entries : 26 : Total Size= 761 bytes File Size = 280 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 13 :en2 : en2/D *
*Entries : 26 : Total Size= 766 bytes File Size = 281 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 14 :fsig : fsig/D *
*Entries : 26 : Total Size= 771 bytes File Size = 282 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 15 :efsig : efsig/D *
*Entries : 26 : Total Size= 776 bytes File Size = 283 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 16 :chi2 : chi2/D *
*Entries : 26 : Total Size= 771 bytes File Size = 282 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 17 :ndof : ndof/D *
*Entries : 26 : Total Size= 771 bytes File Size = 178 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.58 *
*............................................................................*
*Br 18 :prob : prob/D *
*Entries : 26 : Total Size= 771 bytes File Size = 282 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*

```

P.S: I tried with your listing but the problem is the same  
I corrected the oversight … thanks for letting me know 🙂

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 27, 2021, 11:35am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/4 "2021-02-27T11:35:47Z")

</div>

Clearly, there are no “X”, “Y”, “Xerr”, nor “Yerr” branches in your tree.

---

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 11:37am UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/5 "2021-02-27T11:37:54Z")

</div>

Yes @Wile_E_Coyote . I changed the name when I created the topic, in order to make it more understandable.  
These are the branches I take:

```auto
TBranch *TX = t->GetBranch("fsig"); TBranch *TXerror = t->GetBranch("efisg");
TBranch *TY = t->GetBranch("kaonm"); TBranch *TYerror = t->GetBranch("ekaonm");
  

```

---

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 12:01pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/6 "2021-02-27T12:01:17Z")

</div>

This is the output that appears

```auto
#0 0x00007f539d0fe64a in __GI___ wait4 (pid=6094, stat_loc=stat_loc
entry=0x7fff34eb37e8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1 0x00007f539d0fe60b in __GI___ waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7fff34eb37e8, options=options
entry=0) at waitpid.c:38
#2 0x00007f539d070017 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3 0x00007f539d721075 in TUnixSystem::StackTrace() () from /home/emanuele/root/root_v6.22.02.source/root-6.22.02/buildroot/lib/libCore.so
#4 0x00007f53999e3a50 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/emanuele/root/root_v6.22.02.source/root-6.22.02/buildroot/lib/libCling.so
#5 0x00007f53999e3392 in cling_runtime_internal_throwIfInvalidPointer () from /home/emanuele/root/root_v6.22.02.source/root-6.22.02/buildroot/lib/libCling.so
#6 0x00007f539d408359 in ?? ()
#7 0x0000560f65505d90 in ?? ()
#8 0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_11:1:
/home/emanuele/Scrivania/02_03_21/due_corpi_study.C:13:3: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
  TX->SetAddress(&xval); TXerror->SetAddress(&xerrval);
  ^~

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 27, 2021, 12:13pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/7 "2021-02-27T12:13:15Z")

</div>

```cpp
{
  TFile *f = TFile::Open("example.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  TTree *t; f->GetObject("DataFit", t);
  if (!t) { delete f; return; } // just a precaution
  // gROOT->cd(); // any newly created histograms should go here
  
  TBranch *TX = t->GetBranch("fsig");
  TBranch *TXerror = t->GetBranch("efsig");
  TBranch *TY = t->GetBranch("kaonm");
  TBranch *TYerror = t->GetBranch("ekaonm");
  if (!(TX && TXerror && TY && TYerror))
    { delete f; return; } // just a precaution
  
  Double_t xval, xerrval, yval, yerrval;
  TX->SetAddress(&xval); TXerror->SetAddress(&xerrval);
  TY->SetAddress(&yval); TYerror->SetAddress(&yerrval);
  
  Long64_t N = t->GetEntries();
  TGraphErrors *gr = new TGraphErrors(N);
  
  for(Long64_t k = 0; k < N; k++) {
    TX->GetEntry(k); TXerror->GetEntry(k);
    TY->GetEntry(k); TYerror->GetEntry(k);
    gr->SetPoint(k, xval, yval);
    gr->SetPointError(k, xerrval, yerrval);
  }
  
  gr->Draw("AP");
  
  // t->ResetBranchAddresses(); // disconnect from local variables
  delete f; // automatically deletes "t", too
}

```

---

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 12:41pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/8 "2021-02-27T12:41:13Z")

</div>

Thank you @Wile_E_Coyote .  
The errors have disappeared but the graph I request to draw does not appear.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 27, 2021, 12:48pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/9 "2021-02-27T12:48:43Z")

</div>

At least, there was a bug in one branch name (“efisg” versus “efsig”).

---

<div class="post-metadata">

**Author:** ![emanuele\_cardinali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emanuele_cardinali/32/23918_2.png) [@emanuele\_cardinali](https://root-forum.cern.ch/u/emanuele_cardinali)\
**Post date:** [February 27, 2021, 12:52pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/10 "2021-02-27T12:52:52Z")

</div>

I guess this was the main mistake … the others I guess were “warnings”  
Thanks for the help!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 13, 2021, 12:53pm UTC](https://root-forum.cern.ch/t/how-to-plot-a-tgrapherror-with-branches/43730/11 "2021-03-13T12:53:13Z")

</div>

Closed on March 13, 2021, 12:53pm UTC

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