Setbranchaddress (of an array) in pyroot

hi all

i have a tree with branches which have arrays of floats saved in them. and i’d like a way to do a SetBranchAddress in pyroot. i tried the following:

#! /usr/bin/python
from ROOT import *

file = TFile("/afs/")
tree = file.Get("mt2")

var = vector('double')()
tree.SetBranchAddress("jet_pt", var)


print "tree.jet_pt[0]: " + str(tree.jet_pt[0])
print "tree.jet_pt[1]: " + str(tree.jet_pt[1])
print "tree.jet_pt[2]: " + str(tree.jet_pt[2])

print "var[0]: " + str(var[0])

which you can easily execute yourself from AFS, and the last command crashes.

what am i doing wrong?

thanks in advance

1 Like


like you say, it’s an array of floats in the tree, so not an std::vector, and certainly not an std::vector. If you know the maximum possible size, you can use an array(‘f’, [0.]*max_size) from module array with SetBranchAddress, but it is way easier to use the standard python syntax (tree.jet_pt), as you already have in the example as well.