Access arbitrary branches given at run time

I’d like to access TTree branches that are unknown until the user gives their names at the run time. The only thing I know is they should be Float. So I try the following code using C struct for each variable and putting them in a list:

[code]#!/usr/bin/env python
import sys
args = sys.argv[1:]
from ROOT import *

print "scanning variables ", args
gROOT.ProcessLine( "struct mydata_t { Float_t avar; } ; " )

T = TChain(“tr”)

for var in args:
mydata = mydata_t()
T.SetBranchAddress( var, AddressOf(mydata, ‘avar’) )

for i in xrange(10):
for v in vlist:
print v.avar,


It works but I am wondering if this is the most efficient way to do it. Suggestions are appreciated.


if the variables are of type float, simply doing “getattr(T, var)” will do to access them. Also, if you want to loop over all entries in the tree, simply doing “for t in T: getattr(t, var)” would be enough.

Using SetBranchAddress() is more efficient, though (to be more efficient than that, you’d have to go to C++).

Note that you can also add the mydata_t instances to T as data members. That way, they are guaranteed to have the same lifetime.