PyRoot reading leaves

I’m very new to Root and I’m struggling to read a Delphes generated file in PyRoot. I need to access the leaves Tower.Eta and Tower.Phi to read their values, but I can’t for my life figure out how to use exroottreereader or anything else on Python. I’ve search for hours through this forum, but haven’t found a successful method. Any help is appreciated.

ROOT Version: 6.22
Platform: Mac
Compiler: Not Provided


Hello,

Here’s an example of iteration of a TTree in Python:

In your case the tree is Delphes, and you should access entry.Tower.Eta and entry.Tower.Phi inside the loop (provided that entry is the name of the loop variable as in the example above).

That being said, looping over a TTree in Python is slow. What we recommend to process and analyse your tree data is RDataFrame:

https://root.cern/doc/master/classROOT_1_1RDataFrame.html
https://root.cern.ch/doc/master/group__tutorial__dataframe.html

where the event loop happens in C++ and you get goodies like implicit parallelisation on your cores.

It can also be used from Python. We would be happy to help you start using it!

Hi,

Thanks for the quick response. For the time being, I think I’m going to focus doing it without an RDataFrame because I’m sorta new to everything, and I don’t want to overcomplicate it. I’ve tried doing something similar to that Jupyter example, and I get this error: “AttributeError: ‘TClonesArray’ object has no attribute ‘Eta’”. For some reason, the leaves are unable to be read. Do you have any idea why it won’t work?

Hi,

I stumbled on the same problem, you can check the post here

you may want to try:

for entry in tree_file.Delphes:
    eta = entry.GetLeaf("Tower.Eta").GetValue()
    phi = entry.GetLeaf("Tower.Phi").GetValue()

I think this should work.

If not, try to upgrade to root 6.22/06 .
I think your method eta = entry.Tower.Eta also should work there, but I am not sure.

cheers

Hi,

Just tried that code and it runs without error, but it’s giving weird values when I try printing them. Phi should range between ~-5 and ~5, but the only values I see in 10000 entries are between -4.7 and -4.9. Looking at the root histogram in treebrowser, this definitely shouldn’t be the case. Did anything like this happen to you? I’m pretty sure I’m running the latest root version.

Could you upload the root file with few events, so I could see the structure?

Is Tower is an array?

Then we take only 1st element of the array which would explain that if it is sorted.

Then we need to iterate on its elements as well.

for entry in tree_file.Delphes:
    eta_array = entry.GetLeaf("Tower.Eta").GetValue()
   print(len(eta_array))
    for eta in eta_array:
        print(eta)

Will it work?

It gives this error: “TypeError: ‘float’ object is not iterable”; i.e., eta is a float. I’m attaching a sample of 100 events, let me know if you figure out anything. I can’t fit the sample as an attachment, so here’s a link to a google drive upload of it: ppTOtt_100.root - Google Drive .

I really appreciate you helping me out.

Hello,

Coming back to this code, the error “AttributeError: ‘TClonesArray’ object has no attribute ‘Eta’” indicates that Tower is a TClonesArray. What happens if you try to iterate on it?

....
for elem in entry.Tower:
    print(elem.Eta)
    print(elem.Phi)
...
1 Like

Gives the same error: “AttributeError: ‘TClonesArray’ object has no attribute ‘Eta’”

Hi,

So we know the type of entry.Tower is TClonesArray (from the first error you saw when trying to access entry.Tower.Eta) and if you iterate over that array, the type of its elements is also TClonesArray (from the last error you reported)? Does this make sense given the type of the data you have in the tree?

Just to be sure, did you remove the entry.Tower.Eta access before trying to iterate on entry.Tower?

Just redid it, now I get a different error: “AttributeError: ‘TObject’ object has no attribute ‘Eta’” . I have no clue why it wouldn’t be able to read Eta, it’s definitely there…

Let’s try this: can you print just elem in the loop?

for elem in entry.Tower:
    print(elem)

What do you see? Are the elems empty (i.e. containing a pointer to null)? They are of type TObject I guess.

It says that there’s a TObject at some location.

Ok, what’s the expected type for the elements of the Tower TClonesArray? It seems a cast is necessary. The cast can be done with:

for elem in entry.Tower:
    cast_elem = ROOT.BindObject(elem, "ElemClass")