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.
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
)
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:
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
)
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
)