I’m trying to write a ROOT.std.string to a TTree using the following code:
from ROOT import *
tree = TTree( 't', 'mytree' )
s = std.string
tree.Branch('mystr',s)
for word in ['hello','cruel','world']:
s=word
tree.Fill()
tree.Scan()
I’m doing something wrong in the tree.Branch(‘mystr’,s) line, but I can’t figure out how make it work.
arguably the problems are here:s = std.string
and here:s=word
rather than in the Branch() call.
The former only assigns the std::string class to ‘s’, it does not create a string object from which an address can be taken. The latter just replaces ‘s’ with a reference to the python string, of which the TTree knows nothing.
This code might be closer to what you want to achieve:[code]from ROOT import *
tree = TTree( ‘t’, ‘mytree’ )
s = std.string()
tree.Branch(‘mystr’,s)
for word in [‘hello’,‘cruel’,‘world’]:
s.replace(0, std.string.npos, word)
tree.Fill()