4

I just started using pyroot to read root files and I can't read the data from a file using jupyter notebook. Here is how the TBrowser looks like:

enter image description here

I started like this:

import ROOT as root
import numpy as np

f = root.TFile("RealData.root")
myTree = f.Get("tree")

entries = myTree.GetEntriesFast()

Up to here it is working and if I print entries I get the right number of entires I have in the file. But i don't know how to read actual data from the tree (event_number, n_channels, etc.) If I try something like myTree.events or myTree.event_number the kernel stops working. What should I do to read the data from the tree?

Asen Christov
  • 848
  • 6
  • 21
Silviu
  • 749
  • 3
  • 7
  • 17

1 Answers1

3

Normally with pyROOT, you can just do something like:

import ROOT as root
import numpy as np

f = root.TFile("RealData.root")
myTree = f.Get("tree")
for entry in myTree:         
     # Now you have acess to the leaves/branches of each entry in the tree, e.g.
     events = entry.events

I don't know enough about how jupyter works to know if that would cause any particular problems. Have you tried running the same script just using a regular python interpreter?

Nick Edwards
  • 630
  • 8
  • 18
  • If I do this I get this error: TypeError: requested class 'ROOT::Event' does not exist – Silviu Aug 09 '16 at 19:58
  • 1
    Sounds like you are missing ROOT dictionaries for the Event class (I assume this is a custom class you are using not a core root one?). You should be able to add a line like: `root.gInterpreter.GenerateDictionary("\path\to\Event.h")` to get ROOT to generate the dictionary on the fly. Or look at http://wlav.web.cern.ch/wlav/pyroot/tpymyclass.html – Nick Edwards Aug 09 '16 at 20:46