Problem reading a TTree with PyROOT

Greetings,

I have this minor problem where I cannot read TTrees on PyROOT, while I can perfectly read them via .cpp files. The code that I am executing is:

import ROOT

Data = ROOT.TFile.Open("data.root", "READ")
Main_Tree = Data.Get("Tree")
print(Main_Tree.GetEntries())

and I am getting

Traceback (most recent call last):
  File "/path/to/script.py", line 5, in <module>
    print(Main_Tree.GetEntries())
AttributeError: 'TObject' object has no attribute 'GetEntries'

I am pretty sure that names are all correct because as I said, everything runs fine on a separate .cpp file. Any help would be greatly appreciated.

Thank you,

Andy

_ROOT Version: 6.24/06
Platform: macosx64, Big Sur 11.6
Compiler: g++ 11


Hi Andy,

most likely it means there is no TTree called “Tree” in that file. To make sure, replace

print(Main_Tree.GetEntries())

with

if Main_Tree:
    print(Main_Tree.GetEntries())
else:
    print('No TTree with this name is present in the file!')

and rerun.

Thank you for your reply. Indeed, even if I said it in the original post that I was sure, the TTree is not found by this script (prints “No TTree…”). I noticed that the TTree is inside a directory in my root file. I changed the .Get method to FindObjectAny, which is more general and it solved the issue. The script now prints the size of my TTree.

Many thanks for the catch!

Andy