Reading a tree with a particular form

Hi…
I am a beginner. I am trying to extract values from leafs of a tree. I have a tree of this form.

I want to extract value in each entry of leaf “multi”. And entries of all the other branches ID, Px,Py…etc.
I was using TTree::GetArgs() to extract a particular entry but it was not working.

I have also tried TTree::GetEntry() is showing segmentation fault.

void treeread() 
{
   Float_t *px, *py , *pz, *Pt, *x, *y, *z, *t;
   Int_t *id, *event;
  
   TFile *f = new TFile("PbPb_2760_10.root");
   TTree *t4 = (TTree*)f->Get("AMPT");
      
  t4->SetBranchAddress("Event",&event);
  t4->SetBranchAddress("ID",&id);
  t4->SetBranchAddress("Px",&px);
  t4->SetBranchAddress("Py",&py);
  t4->SetBranchAddress("Pz",&pz);
  t4->SetBranchAddress("X",&x);
  t4->SetBranchAddress("Y",&y);
  t4->SetBranchAddress("Z",&z);
  t4->SetBranchAddress("Time",&t);        

   Int_t nevent = t4->GetEntries(); 
  
   for (Int_t i=0; i<nevent; i++) 
    {
      t4->GetEntry(i);  //Here is the Problem GetEntry() is not reading individual entries.
      
          
      printf("px=%lf\tand Py=%lf\tand Pz=%lf\ti=%d\tid=%d\n",px,py,pz,i,id);
    }
}    

Please tell me where I am wrong.
Thanks in advance.

Hi. You need to make your variables used in SetBranchAddress not be pointers. That is, you need to change

Float_t *px, *py , *pz, *Pt, *x, *y, *z, *t;
Int_t *id, *event;

into

Float_t px, py, pz, Pt, x, y, z, t;
Int_t id, event;

When you pass the variable as in t4->SetBranchAddress("Event",&event); with &, you are passing the address of the pointer. You need to pass a pointer there, that is, the address of a variable, or a real pointer without taking its address.

Cheers,

Thanks amadio. I had tried both. Now I have changed to px, py, pz…etc. Thats ok.
I wanted to know

  1. how to get a particular entry of a leaf of a branch.
    for example “multi” is leaf of the branch 'Event". Now i want to get the value of “multi” in 5th entry.

Hi,
t4->GetEntry(5) sets all the variables linked to the tree via SetBranchAddress to their values for the 5th entry.
To access leaf multi of branch Event you can probably just use event.multi (where event is the variable of type Event that you have “connected” to the tree, as per your snippet).

You could try a ROOT generated “analysis skeleton”.

Thanks eguiraud .
I have made following change.

but now I am getting weird errors. Braces are closed as in the snippet but still this error is shown over and over.

root [0] .x treeread.cxx
Error: Missing closing brace for the block opened around line 50.
Error: Unexpected end of file (G__exec_statement()) treeread.cxx:51:

*** Break *** segmentation violation
nevent=100

regards

Hi,
I don’t think the error message you posted refers to the exact code you posted because there is no unexpected end of file at line 51. Are you sure you are executing precisely this file?

As a side note, your loop starts from 1 rather than from 0, and in C++ it’s best practice to declare variables exactly where you need them as to not pollute outer scopes, e.g. for(int i=0; i<nevent; ++i).

Yeah I am executing this file that’s why its weird.
And you are right about loop. When I started loop from 0 , I got this error

root [0] .x treeread.cxx

 *** Break *** segmentation violation
nevent=100


===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007ff1c2fb74ca in __GI___waitpid (pid=13097, stat_loc=stat_loc
entry=0x7ffc18e0f740, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007ff1c2f30fbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007ff1c3ca8dd4 in TUnixSystem::StackTrace() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#3  0x00007ff1c3cab03c in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#4  <signal handler called>
#5  0x00007ff1c2f6204d in _IO_getc (fp=0x1d58a20) at getc.c:37
#6  0x00007ff1c26b69e6 in G__fgetc () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#7  0x00007ff1c26d82f5 in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#8  0x00007ff1c26e383c in ?? () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#9  0x00007ff1c26e082b in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#10 0x00007ff1c26423e0 in G__interpret_func () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#11 0x00007ff1c2713ce0 in G__getfunction () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#12 0x00007ff1c26082a6 in G__getitem () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#13 0x00007ff1c260e6e0 in G__getexpr () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#14 0x00007ff1c2618274 in G__calc_internal () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#15 0x00007ff1c271dfb4 in G__process_cmd () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#16 0x00007ff1c3c6d8b0 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#17 0x00007ff1c3c6c5f7 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#18 0x00007ff1c3c477dc in TApplication::ExecuteFile(char const*, int*, bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#19 0x00007ff1c3c48880 in TApplication::ProcessLine(char const*, bool, int*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#20 0x00007ff1c38693fb in TRint::HandleTermInput() () from /usr/lib/x86_64-linux-gnu/libRint.so.5.34
#21 0x00007ff1c3caa70c in TUnixSystem::CheckDescriptors() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#22 0x00007ff1c3cab36a in TUnixSystem::DispatchOneEvent(bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#23 0x00007ff1c3c1b904 in TSystem::InnerLoop() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#24 0x00007ff1c3c19bdf in TSystem::Run() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#25 0x00007ff1c3c45e1f in TApplication::Run(bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#26 0x00007ff1c386a4bb in TRint::Run(bool) () from /usr/lib/x86_64-linux-gnu/libRint.so.5.34
#27 0x0000000000400ffc in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007ff1c2f6204d in _IO_getc (fp=0x1d58a20) at getc.c:37
===========================================================


Root > Function treeread() busy flag cleared

The interpreter is not being our friend here.
Please take your treeread function and make it a proper c++ source file, so we can compile it and debug it.
The steps required are the following:

  1. add int main() { treeread(); return 0; } at the end of the file
  2. add all missing #include at the beginning of the file (I might be missing some):
#include "TTree.h"
#include "TFile.h"
#include "TBranch.h"
#include "Event.h" // or whatever the file where the Event class is declared is called
#include <cstdio>

assuming Linux:
3) compile with g++ -Wall -o main main.cxx $(root-config --libs --cflags)
4) run inside the debugger: gdb ./main and then type run and press Enter.

It is asking for some libraries as shown below, that I could not find. (I am using Linux and root version 5.34/30 23 April 2015)

g++ -Wall -o main main.cxx $(root-config --libs --cflags)
/usr/bin/ld: cannot find -lGraf3d
/usr/bin/ld: cannot find -lPostscript
/usr/bin/ld: cannot find -lPhysics
collect2: error: ld returned 1 exit status

regards

Hi…
In the above code. I am getting segmentation fault at t4->GetEntry(i). Is this something to do with structure of the tree.
.root file is ok, there is data (I can see it in TBrowser and with t4->Print(); ).
Where is the problem?

regards

Hi Pramod,
please post a complete “working” piece of code that is as small as possible but reproduces your problem.
I am not sure what you are doing so I am not sure how to help.

The simplest thing you can try is (then inspect the newly created files):
AMPT->MakeClass();

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