I have the following code, which reads and prints out information for events in a tree:
import ROOT file = ROOT.TFile("main.root","read") tree = file.Get("MyTree") for event in tree: print event.pT_1jet, event.pT_2jet, event.pT_3jet
Here I’m giving the explicit name of my variables because my problem is actually about generalizing this code to many more jets (without knowing a priori how many actually exist in the root file) and being able to access the ones of interest (satisfying a given condition). I tried the following:
import ROOT file = ROOT.TFile("main.root","read") tree = file.Get("MyTree") for event in tree: for jetnumber in range(1,event.nj_good): #nj_good is the number of "good" jets in the event thisJetPTvariable = "pT_" + str(jetnumber) +"jet" print entry.thisJetPTvariable
which causes an error
AttributeError: 'TTree' object has no attribute 'thisJetPTvariable'
So my question is then: how do I call a function whose name I’m looping over?