4

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.

Sol Arnu
  • 317
  • 4
  • 16
user3458163
  • 41
  • 1
  • 3

2 Answers2

5

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

Sol Arnu
  • 317
  • 4
  • 16
0

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.

Matt
  • 545
  • 3
  • 16