Problem reading TTree with PyRoot


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

import ROOT 
import numpy as np

f = ROOT.TFile.Open('Files/TimingCalibration_provisional.root')
tree =  f.Get("Tree")
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

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:

import ROOT 
import numpy as np

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

Any help would be appreciated.


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, 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 for an example).

I hope this helps!