Declaring a TTree Branch in PyRoot
pyroot, python, root-framework
Solution
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
Problem
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.