TFile.Get("path/to/TTree") returns a <class 'ROOT.TObject'> instead of <class 'ROOT.TTree'>


_ROOT Version:6.15/01
_Platform:Ubuntu
_Python version:2.7.15rc1


Hello everyone,
I need to import some root files and convert trees to arrays. To do that, I must pass a TTree object to tree2array() function. According to most tutorials, the TFile.Get() method automatically returns a TTree object if the the path provided points to a TTree. But this doesn’t seem to be the case with me:

>>>tree = tfile.Get("2los_1tau_forBDTTraining/sel/evtntuple/signal/evtTree")
>>>type(tree)
<class 'ROOT.TObject'>

Any way to get a TTree object from the TObject class or directly using Get() method?
Any help is appreciated.

Hi,

thanks for the report.
do you get the same with

tree = tfile.2los_1tau_forBDTTraining.sel.evtntuple.signal.evtTree

?

Cheers,
D

Note that TDirectoryFile::Get ALWAYS returns a TObject*.
You should be using TDirectoryFile::GetObject instead.

@Danilo

    >>> tree = tfile.2los_1tau_forBDTTraining.sel.evtntuple.signal.evtTree
    File "<stdin>", line 1
    tree = tfile.2los_1tau_forBDTTraining.sel.evtntuple.signal.evtTree
                ^
    SyntaxError: invalid syntax

@Wile_E_Coyote

What is its second argument/syntax?

>>> intree = rfile.GetObject('2los_1tau_forBDTTraining/sel/evtntuple/signal/evtTree')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: TDirectory::GetObject() takes exactly 2 arguments (1 given)

PyROOT casts objects to the actual one, so Get() normally works fine. Above, you only print the type, but I’m willing to bet that it’s a nullptr: auto-casting can’t work unless there is an actual object to retrieve the type from. (Check ‘not not tree’, ‘tree == None’, or simply print ‘tree’ instead of ‘type(tree)’.)

@Danilo @Wile_E_Coyote @wlav

thanks for your responses but it’s my stupidity that caused the problem. Turns out, the directory structure string “2los_1tau_forBDTTraining/sel/evtntuple/signal/evtTree” is case-sensitive and since i put it in by hand, there was a mistake it really is ‘2los_1tau_forBDTtraining’, not 2los_1tau_forBDTTraining’’. Sorry for my baseless complaint guys, it works now.

1 Like

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