# Problem reading TTree with PyRoot

**URL:** <https://root-forum.cern.ch/t/problem-reading-ttree-with-pyroot/54237>\
**Category:** Newbie\
**Tags:** python\
**Created:** [March 27, 2023, 12:47pm UTC](https://root-forum.cern.ch/t/problem-reading-ttree-with-pyroot/54237 "2023-03-27T12:47:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asanchezcastillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asanchezcastillo/32/32921_2.png) [@asanchezcastillo](https://root-forum.cern.ch/u/asanchezcastillo)\
**Post date:** [March 27, 2023, 12:47pm UTC](https://root-forum.cern.ch/t/problem-reading-ttree-with-pyroot/54237/1 "2023-03-27T12:47:35Z")

</div>

Hi,

I am trying to read a TTree with PyRoot. The code I am running is the following:

```auto
import ROOT 
import numpy as np

f = ROOT.TFile.Open('Files/TimingCalibration_provisional.root')
tree = f.Get("Tree")
MyCut=ROOT.TCut('')
shape = tree.Draw('slope', MyCut) 
Slope=np.array(np.ndarray((shape), 'd', tree.GetV1()))

```

Even though some of the values of the tree are read correctly, when I check the array Slope I can also see some very extreme values that should not be there (eg: -5.69861497e-100, 1.46149876e-238, -4.15805930e-280). I also happens sometimes when running the above code (not always) that my kernel gets killed yielding the following error message

```auto
Kernel Restarting
The kernel appears to have died. It will restart automatically.

```

However, I do not have any of the previous issues when reading the tree with the code below:

```auto
import ROOT 
import numpy as np

slope=[]
f = ROOT.TFile.Open('Files/TimingCalibration_provisional.root')
tree = f.Get("Tree")
for iev in range(tree.GetEntries()):
    tree.GetEntry(iev)
    slope.append(getattr(tree, 'slope'))

```

Any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [April 11, 2023, 10:02am UTC](https://root-forum.cern.ch/t/problem-reading-ttree-with-pyroot/54237/2 "2023-04-11T10:02:19Z")

</div>

Hello @asanchezcastillo, sorry for the late reply and welcome to the ROOT forum!

This code with the `tree.GetV1()` is not the right way to convert your TTree branches to NumPy arrays.

Is is better if you use [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html), the new interface to open and analyze n-tuples in `.root` files, and use `AsNumpy()` to export some of the columns to NumPy arrays (see [this tutorial](https://root.cern.ch/doc/master/df026__AsNumpyArrays_8py.html) for an example).

I hope this helps!  
Cheers,  
Jonas
