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 ()):
    Tree.GetEntry(event)
    PVetoT = getattr(Tree,"PVeto/TMCVEvent/fHits/fTime")

Any help would be useful. TIA

Hello,

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:
        print(hit.fTime)

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:

https://root.cern/doc/master/classROOT_1_1RDataFrame.html

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.