Writing std.string to ttree

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.

Thanks,
Andrew

Hi,

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()

tree.Scan()
[/code]
Cheers,
Wim

Thanks very much, sorry for posting such a silly error!

Hi all,

This post was use ful for me.

But, I would like save the array of strings in a branch. How I can do this in python?

with regards,
Ram