TTreeReader in PyROOT

Hi *

I’m trying to get up and running using the TTreeReader approach to reading TTrees in PyROOT. As a guide, I am using the ROOT 6 Analysis Workshop (http://root.cern.ch/drupal/content/7-using-ttreereader) and its associated ROOT file (root.cern.ch/root/files/tutorials/mockupx.root).

from ROOT import *
fileName = "mockupx.root"
file = TFile(fileName)
tree = file.Get("MyTree")
treeReader = TTreeReader("MyTree", file)

After this, I am a bit lost. I attempt to access variable information using the TTreeReader object and it doesn’t quite work:

>>> rvMissingET = TTreeReaderValue(treeReader, "missingET")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wbm/ROOT/v6-03-01/root/lib/ROOT.py", line 198, in __call__
    result = _root.MakeRootTemplateClass( *newargs )
SystemError: error return without exception set

Could you advise me on what I’m doing wrong here? I’m working currently on using the TTreeReaderValue and TTreeReaderArray objects. I would welcome any suggestions or comments on upcoming activities concerning accessing TTree entries and variables.

Hi,

AFAIK, TTreeReader exists to make the C++ syntax nicer, i.e. closer to how one can loop over TTrees in Python, where you can just loop over the tree like so:for even in tree: print event.branch
As such, it’s puzzling what you hope to achieve by using TTreeReader in Python?

Anyway, the problem is that TTreeReaderValue is a template, so it needs to be used like so:TTreeReaderValue(template paramters)(function parameters)
Cheers,
Wim

Thank you very much for your assistance on this. I understand that the TTreeReader is the recommended way to access trees now and it offers caching too. That’s why I’m trying to use it.

What I have right now is the following:

from ROOT import *
fileName = "mockupx.root"
file = TFile(fileName)
tree = file.Get("MyTree")
treeReader = TTreeReader("MyTree", file)
missingET = TTreeReaderValue(Double)(treeReader, "missingET")
while treeReader.Next():
    print(missingET)

I am able to create the TTreeReaderValue in the way you suggested (using ROOT.Double as its type). How could I now print the missing et value for each event? Then, for branches, how could I use the TTreeReader to access, say, the electron pt values for each event?


Using the more traditional approach, I can print out the missing et values for each event in a way such as the following:

from ROOT import *
fileName = "mockupx.root"
file = TFile(fileName)
tree = file.Get("MyTree")
# alternative:
for event in xrange(tree.GetEntries()):
    tree.GetEntry(event)
    print(tree.missingET)
# alternative:
for event in tree:
    print(tree.missingET)

How could I print the values of, say, the electron pt values for each event? I am able to access the pt leaf of the electrons branch in the following way:

leaf = tree.GetBranch("electrons").FindLeaf("fPt") # (or GetLeaf("fPt")

I’m not sure how to access the individual pt values.

Thanks again for your help on this!

Recommended from C++, sure. From Python, no. TTreeCache works in both cases, so no worries there.

Cheers,
Wim