Home | News | Documentation | Download

Obtain every nth value from a TBranch array in PyRoot


ROOT Version: Not Provided
Platform: Linux
Compiler: Not Provided


Hello, I am attempting to iterate over the values of one TBranch in a TTree that is located in a TDirectory under a TFile. I am fairly new to ROOT, though I have history with Python, so I thought it best to use PyROOT to complete the iteration. However, if anyone can help me get this to work in either PyROOT or ROOT, it would be very helpful.

so far I have this:
"
myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mytree = ROOT.TDirectory(myfile.GENIE/Event) # this is the name of the tree in the file
for e in mytree:
print (e.mc_Momentum) # mc_Momentum is the name of branch of the tree
)
"
I know this does not work and the issue is mainly to do with my lack of understanding of how to tell PyROOT that I want to values of the TBranch named mc_Momentum. The location of the TBranch (mc_Momentum) I am interested in is: GENIE_atm.root/GENIE/Event/mc_Momentum

I am not sure how I need to write the code such that PyROOT knows that I am interested in the values of mc_Momentum. Any ideas?

For clarification, the TFile is is GENIE_atm.root, the TDirectory is GENIE, the TTree is Event, and the TBranch is mc_Momentum.

Dear Hilary,

Can you try:

myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mytree = myfile.GENIE.Event
for e in mytree:
    print (e.mc_Momentum) # mc_Momentum is the name of branch of the tree
)

Hello,

When I try this, I get the following error code:

However, after tinkering around with my code, I managed to get PyROOT to understand the complexity of my TFile. I ended up with this code:

file = str("/home/hilary/root/compile/Research/GENIE_atm.root")
Tdir = ROOT.TChain(“GENIE/Event”)
Tdir.Add(file)
num = Tdir.GetEntries()

for i in range (num):
Entry = Tdir.GetEntry(i)
EntryFromBranch = Tdir.GetEntries()
for j in range(EntryFromBranch):
PT = Tdir.GetLeaf(“mc_Momentum”).GetValue(j)

So I think PyROOT now understands the TBranch I am trying to access, however, I still do not know how to iterate over the values contained in the TBranch such that I can extract every 4th number. I now get this error when I run my newly edited code:

I think this might have to do with the fact that mc_Momentum might be an array? But I am not completely sure. Do you have any ideas?

Hi Hilary,
Would it be possible for you to share your input so I can inspect it?

And I would also try:

myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mydir = myfile.GENIE
mytree = mydir.Get("Event")
for e in mytree:
    print (e.mc_Momentum) # mc_Momentum is the name of branch of the tree
)

Hello again,

What do you mean by input?

I meant your input file,

GENIE_atm.root

Did my other suggestion work?

Here’s the GENIE_atm.root file:

I could not get it to upload on here, I think the file is too big for the fourm’s servers. However, do not worry, the file is only 250,000 KB.

I tried the last suggestion you gave and it resulted in the following:

Any ideas as to why it is printing out the data type and its size instead of the numbers (values) contained in each Double_t?

Hi Hilary,
Ok, then it seems my last suggestion is working.
From the prints, it seems that ec_Momentum is not of type double, it is an array of doubles of variable size. That is why the print is telling you the type of the array values (Double_t) and the size of the array. To get the values in the arrays, you can do:

myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mydir = myfile.GENIE
mytree = mydir.Get("Event")
for e in mytree:
    for v in e.mc_Momentum:
        print(v) # This will print the values of each array
)
1 Like

This one worked! Thank you so much!