Why is there no method to get TString Value from TLeafElement in pyROOT?


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,
I have a root file to analyze.
The root file has a tree with some branch with the name “F.” and its leaves, “F.mName”, “F.mOpenTime”.
If I access the tree in pyROOT like so:
for i in xrange(entries):
tree.GetEntry(i)
for l in tree.GetListOfLeaves():
print l.GetName(), tree.GetLeaf(l.GetName()).GetValue(i), tree.GetLeaf(l.GetName()).GetTypeName()

, the output looks like:
F. 0.0 SXrdFileInfo
F.mName 0.0 TString
F.mOpenTime 1554101997.0 Long64_t

F.mName is supposed to be some file name like ‘/store/user/…’, a TString, instead it print 0.
Instead of GetValue(), I think it should be accessed using GetValuePointer() method.
That seems the only relevant method.
In root C, this may be achieved by
sprintf(val,"%s",(char*)leaf->GetValuePointer());

But In pyROOT python, how can I typecast this with (char *) to obtain the correct TString value:
tree.GetLeaf(“F.mName”).GetValuePointer()
Should there be a method similar to GetValue(i) for TString?
Thanks,
Bockjoo

@etejedor, can you help here please?

Oksana.

Dear @bockjoo

If performance is not really a concern, you can iterate a TTree in python like so:

for entry in tree:
    print(entry.mName)
    ...

That should give you the right value for the TString branch.

@etejedor

It did not work. I tried also entry.F.mName, but it did not work.

Please check this illustration out:

Thanks,
Bockjoo

Hi,
Can you try:

for entry in tree:
   print(getattr(entry, "F.mName"))

Cheers,
Enric

@etejedor, That worked!
Thanks,
Bockjoo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.