This works very well for track_id, but not for a_string. I also tried TString , TObjString , none of it apparently worked.
But:
tree.a_string
works fine in my loop over all events, but I dislike that workaround and I guess it is even slower?
Is there a way to integrate strings into my struct?
suppose to have a tree called “myTree” in a ROOT file “myFile” with a branch “myString” of type std::string and other branches (b1, b2, …, bn)
What one would do to access it in a pythonic way is:
t = myFile.myTree
for entry in t:
print entry.myString
print entry.b1
# ...
print entry.bN
Are all different types; only actual type will work. Check with tree.Print(): it may be char*. Using ‘tree.a_string’ is dog in python (fine in pypy-c); SetBranchAddress() is faster.
Before I started to create my struct I tried tree.Print() of course, but I guess it is a “normal” string, at least
I am using a std::string in my C++ code.
tree.a_string = ROOT.std.string()
tree.SetBranchAddress('a_string', tree.a_string)
for i in xrange(tree.GetEntries()):
tree.GetEntry(i)
print tree.a_string
-Dom
[quote=“Dominique”]tree.a_string = ROOT.std.string()
tree.SetBranchAddress('a_string', tree.a_string)
for i in xrange(tree.GetEntries()):
tree.GetEntry(i)
print tree.a_string
-Dom[/quote]
Thanks this does increase the reading speed by roughly 25%.
Can this be incorporated into my struct?
No, can’t grab objet:[code]>>> toto = ROOT.complete_t()
print type(toto.a_string)
<type ‘str’>[/code]
Can workaround, but need keep alive:toto = ROOT.complete_t()
toto._cppstr = ROOT.BindObject(ROOT.AddressOf(toto, "a_string"), ROOT.std.string)
tree.SetBranchAddress('a_string', toto._cppstr)
for i in xrange(tree.GetEntries()):
tree.GetEntry(i)
print toto.a_string
Aside, can then also use directly for bit more speedup:...
toto._cppstr = ROOT.BindObject(ROOT.AddressOf(toto, "a_string"), ROOT.std.string)
rapid_cppstr = toto._cppstr
... # loop etc
print rapid_cppstr
-Dom