Home | News | Documentation | Download

Accessing subdirectory in PyROOT

I have a root file which has two trees in it. Using pyroot I’m able to find the correct tree and cycle through the entries in the tree but I can’t access the data in the subdirectories, it gives the error: AttributeError: 'TTree' object has no attribute 'PVeto.TMCVEvent.fHits.fTime'

The structure is as shown here:

And my current code is:

import ROOT
from ROOT import TH1F, TFile, TCanvas, TTree
import os

MCFile = TFile.Open("BhabhaSCh100kMCDataSingleParticle.root","READ")

mcTreeName = "MC"
Tree = MCFile.Get(mcTreeName)
print "Looking for tree named ",mcTreeName

nEntries = Tree.GetEntries()

print nEntries

for event in range (0,  Tree.GetEntries ()):
    PVetoT = getattr(Tree,"PVeto/TMCVEvent/fHits/fTime")

Any help would be useful. TIA


What’s the type of fHits? Is it an array?

If so, your loop would look like:

for event in Tree:
    hits = event.PVeto.TMCVEvent.fHits
    for hit in hits:

Also, please note that looping over a TTree in Python should only be done for small / exploratory use cases. If the input data set is big it will be too slow. In that case, you can use RDataFrame:


so that the event loop runs in C++. And you also get other goodies like implicit multi-threading (it exploits all the cores of your machine). We’d be happy to help you write your analysis in RDataFrame form.