How to plot a TGraphError with branches

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) I wrote the following code:

  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 :slight_smile:

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

BTW. You probably want:

  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);
  }

That’s the output:

******************************************************************************
*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 :slight_smile:

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

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:

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

This is the output that appears

#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);
  ^~

{
  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
}

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

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

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

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