Storing string variables in Trees


I am just starting to use PyROOT, so this is probably a very basic question. I am trying to store string variables in a Tree and read them back.
So far the best I could do is:

[code] gROOT.ProcessLine(
“struct invar_t {
Int_t num;
Char_t str;
from ROOT import invar_t

var = invar_t()
num = 7
st = 'something’
var.num = num = st

rfile = TFile(“test.root”, “recreate”)
treename = “tree"
tree = TTree(treename,”")
bname = "sbranch"
leaf = "num/I:st/C"
tree.Branch(bname, var, leaf)



rfile = TFile(“test.root”, “read”)
mytree = gDirectory.Get(‘tree’)
print “num=”,mytree.num
print “str=”,[/code]

However I don’t get my original string back from Could someone tell me what I am doing wrong?
Also, is there a more direct way of storing a string variable without having to create a class through gROOT.ProcessLine?



there a quite a number of problems. To wit, the code assigns to ‘st’ rather than ‘str’; ‘str’ is defined as a Char_t, which is a single character, not a string; and the address is given of the variable object, not the data members.

Here’s an example that may be of use: $ROOTSYS/tutorials/pyroot/

As for alternatives, the easiest would be to work with an std::string object (ROOT.std.string) rather than a character array.