user3458163
user3458163

Reputation: 41

Declaring a TTree Branch in PyRoot

I am trying to simply define a Root TTree using python and give it a TBranch. Sounds reasonable, right? I tried:

from ROOT import *
myvar = int()
mytree = TTree('mytree', 'mytree')
tree.Branch('myvar', AddressOf(myvar), 'myvar/I')
exit(0)

and this crashes with the error:

ValueError: invalid argument for AddressOf().

I suspected that perhaps the argument of AddressOf() needs to be a Root type like Int_t, but I didn't think python data types needed to be made explicit--and furthermore I couldn't figure out how to force the data type of of int to be Int_t. Finally, if you do the same exact thing except replace the 'int' with 'TString' and the '/I' with '/S', things do not crash. Any suggestion is appreciated.

Upvotes: 4

Views: 5156

Answers (2)

Matt
Matt

Reputation: 545

A different data type may indeed be necessary, Int_t should be correct for an integer. Take a look at the ROOT Cern Staff example with pyroot here.

Upvotes: 0

Sol Arnu
Sol Arnu

Reputation: 317

You need to use a different data type for "myvar". This is because of the way the data gets used internally in root.

from ROOT import *
from array import array
myvar = array( 'i', [ 0 ] )
mytree = TTree('mytree', 'mytree')
mytree.Branch('myvar', myvar, 'myvar/I')
exit(0)

this should work and it gets rid of the crash from your example See e.g. this web page for more information: http://wlav.web.cern.ch/wlav/pyroot/tpytree.html

Upvotes: 5

Related Questions