# Reading branch leafs with pyROOT

**URL:** <https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913>\
**Category:** ROOT\
**Tags:** python\
**Created:** [March 10, 2021, 4:59pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913 "2021-03-10T16:59:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [March 10, 2021, 4:59pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/1 "2021-03-10T16:59:02Z")

</div>

I have a tree structure as following:

![image](https://root-forum.cern.ch/uploads/default/original/3X/5/2/521e565a6bd72147ed1588dcd8204bfd580132d4.png)

I want to get values of the “global\_time\_stamp” leaf in the Plane\_0 object.

My code I **would expect** to work:

```auto
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:

```auto
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:

```auto
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](https://drive.google.com/file/d/1OK4XPBMsYjP0W1DpT4BBVUoVwGH096dj/view?usp=sharing) for testing.

cheers,  
Bohdan

_ROOT Version:_ 6.22/00  
_Platform:_ Centos 7  
_Python:_ 3.7.6

* * *

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [March 11, 2021, 7:30am UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/2 "2021-03-11T07:30:34Z")

</div>

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.

```auto
GetLeaf("Plane_1", "global_time_stamp")

```

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [March 11, 2021, 1:58pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/3 "2021-03-11T13:58:19Z")

</div>

> You can call `GetLeaf()` with two parameters

This doesn’t work for me…

Example:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [March 11, 2021, 2:25pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/4 "2021-03-11T14:25:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [March 11, 2021, 2:29pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/5 "2021-03-11T14:29:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [March 11, 2021, 4:24pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/6 "2021-03-11T16:24:31Z")

</div>

Ok, true, both work on master…

cheers,  
Bohdan

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 25, 2021, 4:24pm UTC](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913/7 "2021-03-25T16:24:38Z")

</div>

Closed on March 25, 2021, 4:24pm UTC

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