# PyRoot reading leaves

**URL:** <https://root-forum.cern.ch/t/pyroot-reading-leaves/45547>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [June 25, 2021, 3:14am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547 "2021-06-25T03:14:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 25, 2021, 3:14am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/1 "2021-06-25T03:14:54Z")

</div>

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.

 ![Screen Shot 2021-06-24 at 10.52.12 PM](https://root-forum.cern.ch/uploads/default/original/3X/2/4/2412c777860f58d108fc38256ac0814b4147d6d0.jpeg)

_ROOT Version:_ 6.22  
_Platform:_ Mac  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 25, 2021, 1:35pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/2 "2021-06-25T13:35:23Z")

</div>

Hello,

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

> **[Jupyter Notebook Viewer](https://nbviewer.jupyter.org/github/root-project/training/blob/master/OldSummerStudentsCourse/2017/examples/notebooks/TTreeAccess_Example_py.ipynb)**
>
> Check out this Jupyter notebook!

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/doc/master/classROOT_1_1RDataFrame.html)  
[https://root.cern.ch/doc/master/group\_\_tutorial\_\_dataframe.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!

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 25, 2021, 5:24pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/3 "2021-06-25T17:24:10Z")

</div>

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?

 ![Screen Shot 2021-06-25 at 1.21.41 PM](https://root-forum.cern.ch/uploads/default/original/3X/0/e/0ef380241e04b67ad8cbcf6249cd99d3e3d07a53.png)

---

<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:** [June 26, 2021, 6:07pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/4 "2021-06-26T18:07:49Z")

</div>

Hi,

I stumbled on the same problem, you can check the post [here](https://root-forum.cern.ch/t/reading-branch-leafs-with-pyroot/43913)

you may want to try:

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

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 26, 2021, 6:38pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/5 "2021-06-26T18:38:27Z")

</div>

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.

---

<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:** [June 26, 2021, 7:07pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/6 "2021-06-26T19:07:40Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 26, 2021, 7:57pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/7 "2021-06-26T19:57:08Z")

</div>

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

I really appreciate you helping me out.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 28, 2021, 7:34am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/8 "2021-06-28T07:34:44Z")

</div>

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?

```python
....
for elem in entry.Tower:
    print(elem.Eta)
    print(elem.Phi)
...

```

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 28, 2021, 7:21pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/9 "2021-06-28T19:21:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 30, 2021, 6:59am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/10 "2021-06-30T06:59:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 30, 2021, 7:01am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/11 "2021-06-30T07:01:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [June 30, 2021, 4:35pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/12 "2021-06-30T16:35:49Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [July 1, 2021, 6:43am UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/13 "2021-07-01T06:43:58Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![UneasyPlum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/bbe5ce/32.png) [@UneasyPlum](https://root-forum.cern.ch/u/UneasyPlum)\
**Post date:** [July 1, 2021, 1:24pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/14 "2021-07-01T13:24:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [July 1, 2021, 1:56pm UTC](https://root-forum.cern.ch/t/pyroot-reading-leaves/45547/15 "2021-07-01T13:56:00Z")

</div>

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:

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

```
