How to import data from ROOT in Jupyter Notebook?

I want to import data as a .root file in Jupyter Notebook but did not understand how to open the trees inside the file after importing?

if I am doing this:
import ROOT

f = ROOT.TFile.Open(“signal.root”)
for event in f.tagsDumper :
print (event.tress)

obtaining this as output

TypeError Traceback (most recent call last)
in
2
3 f = ROOT.TFile.Open(“signal.root”)
----> 4 for event in f.tagsDumper :
5 print (event.tress)

TypeError: ‘TDirectoryFile’ object is not iterable

ROOT Version: 6.22.06
Platform: Linux
Compiler: Jupyter notebook


Hi @Shivam_Raj ,
you need to extract the dataset (i.e. the TTree object) from the file you opened and then iterate on that.
However note that explicit Python for loops are the slowest way to run over ROOT data in Python. @etejedor might be able to point you to some learning resources.

Cheers,
Enrico

Hi,

As @eguiraud pointed out, you are probably trying to iterate on a directory (i.e. tagsDumper is not a tree, but a directory).

Here’s some docs on TTree from Python (look for PyROOT box):

https://root.cern.ch/doc/master/classTTree.html

also this example might help

https://swan-gallery.web.cern.ch/notebooks/root_primer/OldSummerStudentsCourse/2017/examples/notebooks/TTreeAccess_Example_py.html

2 Likes

Thank you for clarification @eguiraud

Thank you @etejedor for sending helpful links.