Home | News | Documentation | Download

Reading branch leafs with pyROOT

I have a tree structure as following:

image

I want to get values of the “global_time_stamp” leaf in the Plane_0 object.

My code I would expect to work:

import ROOT

f_flame = ROOT.TFile("/afs/desy.de/user/d/dudarboh/FCAL/synchronization/fire1036_srs184_FIRE.root")
t_flame = f_flame.TreeOnFire
for event in t_flame:
    event_time = event.GetLeaf("Plane_0.global_time_stamp").GetValue()
    print(event_time)

return:

TClass::Init:0: RuntimeWarning: no dictionary for class Plane is available
TClass::Init:0: RuntimeWarning: no dictionary for class TimeFrame is available
Traceback (most recent call last):
  File "sync.py", line 33, in <module>
    event_time = event.GetLeaf("Plane_0.global_time_stamp").GetValue()
ReferenceError: attempt to access a null-pointer

or accessing it by dot operators like this:
event_time = event.Plane_0.global_time_stamp

return:

TClass::Init:0: RuntimeWarning: no dictionary for class Plane is available
TClass::Init:0: RuntimeWarning: no dictionary for class TimeFrame is available
Traceback (most recent call last):
  File "sync.py", line 33, in <module>
    event_time = event.Plane_0.global_time_stamp
AttributeError: 'Plane' object has no attribute 'global_time_stamp'

BUT

I can acces these variables by:

event_time = event.GetLeaf("global_time_stamp").GetValue()
return:
expected behavior

Additional info:
print(event.GetLeaf("global_time_stamp").GetFullName())
return:
Plane_0.global_time_stamp

Questions:
Is this expected behaviour? Or what am I doing wrong?
How do I access Plane_1, Plane_2, etc…

ROOT file is here for testing.

cheers,
Bohdan

ROOT Version: 6.22/00
Platform: Centos 7
Python: 3.7.6


Hi Bohdan,

The GetLeaf() call by default returns the first leaf with a matching name, therefore it picks the one in the Plane_0 branch. You can call GetLeaf() with two parameters to ask for a leaf in a given branch, e.g.

GetLeaf("Plane_1", "global_time_stamp")

Cheers,
Jakob

You can call GetLeaf() with two parameters

This doesn’t work for me…

Example:

import ROOT

f_fire = ROOT.TFile("/afs/desy.de/user/d/dudarboh/FCAL/synchronization/fire1036_srs184_FIRE.root")
t_fire = f_fire.TreeOnFire

for event in t_fire:
    print(event.GetLeaf("Plane_0","global_time_stamp").GetValue())

Output:

TClass::Init:0: RuntimeWarning: no dictionary for class Plane is available
TClass::Init:0: RuntimeWarning: no dictionary for class TimeFrame is available
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    print(event.GetLeaf("Plane_0","global_time_stamp").GetValue())
ReferenceError: attempt to access a null-pointer

ROOT version: 6.22/00
Python version: 3.7.6
System: RedHat CentOS 7.9.2009
Data file to reproduce the error is attached in the previous post.

Strange, I just tried your script and for it works actually with both, the dot notation and the call with two parameters. Can you try with the latest ROOT 6.22/06.

I just checked with ROOT 6.22/06 on lxplus.cern.ch: the dot notation works, the two parameter version doesn’t. Both versions work with the ROOT master branch.

Ok, true, both work on master…

cheers,
Bohdan

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