Cannot access tree branches with python

I encounter some issues when trying to loop over a TTree using python. This simple script exits with AttributeError: 'TTree' object has no attribute 'track'

import ROOT as r

f   = r.TFile.Open("ntuple.root")
t   = f.Get("trajTree")
for i in range(10):
    t.GetEntry(i)
    print(t.track.quality)

The TTree does contain the “track” branch.

A workaround is to do t.GetBranch("track").GetLeaf("quality").GetValue(). However, when I try to do it on a leaf which contains an array, for example t.GetBranch("track").GetLeaf("validbpix").GetValue(), it only returns one value (I guess it’s the first one).

Why cannot the tree.branch.leaf syntax be used? Or how can the workaround with getting a branch and a leaf work with arrays?

Attached is the .root file ntuple.root (92.6 KB)
I am also linking a data structure which, I believe,was used to create the file
SiPixelTools-PhaseIPixelNtuplizer/DataStructures_v9.h at master · CMSTrackerDPG/SiPixelTools-PhaseIPixelNtuplizer · GitHub

Thanks,
Matej


Using CMSSW_10_2_16_UL
ROOT Version: 6.12/07


I guess @pcanal can help.

With your file you can simply use:

   print(t.quality)

assuming the leaf name is not duplicated. Indeed we do not support the syntax tree.branch.leaf from python for leaflist branches (your case) (@vpadulan might be able to confirm).

t.GetBranch("track").GetLeaf("validbpix").GetValue(), it only returns one value (I guess it’s the first one)

Yes but GetValue take the index as argument:

>>> t.GetEntry(26)
836
>>> t.GetBranch("track").GetLeaf("validbpix").GetValue(0)
2.0
>>> t.GetBranch("track").GetLeaf("validbpix").GetValue(1)
1.0

You may want to consider using RDataFrame: ROOT: Dataframe tutorials
which would move make the access more regular and faster (avoiding looping in python).

1 Like

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