# Problem reading a TTree with PyROOT

**URL:** <https://root-forum.cern.ch/t/problem-reading-a-ttree-with-pyroot/47960>\
**Category:** Newbie\
**Tags:** python\
**Created:** [December 6, 2021, 3:52pm UTC](https://root-forum.cern.ch/t/problem-reading-a-ttree-with-pyroot/47960 "2021-12-06T15:52:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://root-forum.cern.ch/u/andrew)\
**Post date:** [December 6, 2021, 3:52pm UTC](https://root-forum.cern.ch/t/problem-reading-a-ttree-with-pyroot/47960/1 "2021-12-06T15:52:59Z")

</div>

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:

```auto
import ROOT

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

```

and I am getting

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

* * *

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [December 6, 2021, 4:50pm UTC](https://root-forum.cern.ch/t/problem-reading-a-ttree-with-pyroot/47960/2 "2021-12-06T16:50:34Z")

</div>

Hi Andy,

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

```python
print(Main_Tree.GetEntries())

```

with

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

```

and rerun.

---

<div class="post-metadata">

**Author:** ![andrew](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://root-forum.cern.ch/u/andrew)\
**Post date:** [December 6, 2021, 5:04pm UTC](https://root-forum.cern.ch/t/problem-reading-a-ttree-with-pyroot/47960/3 "2021-12-06T17:04:28Z")

</div>

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
