Storing string variables in Trees

Hi,

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
var.st = st

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

tree.Fill()

rfile.Write()
rfile.Close()

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

However I don’t get my original string back from mytree.st. 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?

Thanks.

Hi,

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/staff.py.

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

Cheers,
Wim