Python list in a tree branch?

Hello,

I am trying to get the information of a list of strings into a tree branch, and I am having trouble…

If I define the branch as a TString array, I get a type mismatch (“RuntimeError: property type mismatch or assignment not allowed”).

Then, I try defining it as a vector of strings and the actual branch as follows:
event_info_tree.Branch(‘L1TriggerAV’, AddressOf(Event_Info,‘L1TriggerAV’))
rather than something that looks like that:
event_info_tree.Branch(‘L1TriggerAV’, AddressOf(Event_Info,‘L1TriggerAV’), ‘lvl1TriggerAV/S’)

and in that case, I get the definition of the branch wrong:
“Error in TBranch::TLeaf: Illegal data type…”

and also a type mismatch at the end.

Could you please tell me which would be the way to have my list of strings into a tree branch?

Thank you very much in advance for your help!

Cheers – Anna.

Hi,

an std::vector< std::string > (that is, an ROOT.std.vector(‘std::string’)) isn’t an option? Otherwise, could you please sent a code sample (i.e. the types of Event_Info etc.)?

Cheers,
Wim

Hello,
Thank you very much for your response!

Attached is a very simple sample code that is working with an integer branch, and that is giving me trouble with a vector (in comments in the python file).

Best regards,
– Anna.

[quote=“wlav”]Hi,

an std::vector< std::string > (that is, an ROOT.std.vector(‘std::string’)) isn’t an option? Otherwise, could you please sent a code sample (i.e. the types of Event_Info etc.)?

Cheers,
Wim[/quote]
SimpleTree.py (973 Bytes)

Anna,

how about this, writing.py:[code]import ROOT

o = ROOT.TFile( ‘myfile.root’, ‘RECREATE’ )

t = ROOT.TTree( ‘mytree’, ‘My Tree’ )
v = ROOT.std.vector( ROOT.std.string )()
t._v = v
t.Branch( ‘mystrings’, v )

myStrings = [ ‘aap’, ‘noot’, ‘mies’, ‘zus’, ‘jet’ ]

for s in myStrings:
v.push_back( s )
t.Fill()

t.Write()[/code]
and reading.py:[code]import ROOT

i = ROOT.TFile( ‘myfile.root’ )
mytree = i.Get( ‘mytree’ )

for t in mytree:
print list( t.mystrings )
[/code]
Is that what you are looking for?

Cheers,
Wim

Hello,
Super, this works!
However, if you wanted to put these vectors in a structure and work from there, what would be the syntax for the vector defintion inside the structure, as well as the branch definition (t.Branch( ‘mystrings’, v )? My guesses don’t seem to work… :frowning:

Thanks again!
– Anna.

This is

[quote=“wlav”]Anna,

how about this, writing.py:[code]import ROOT

o = ROOT.TFile( ‘myfile.root’, ‘RECREATE’ )

t = ROOT.TTree( ‘mytree’, ‘My Tree’ )
v = ROOT.std.vector( ROOT.std.string )()
t._v = v
t.Branch( ‘mystrings’, v )

myStrings = [ ‘aap’, ‘noot’, ‘mies’, ‘zus’, ‘jet’ ]

for s in myStrings:
v.push_back( s )
t.Fill()

t.Write()[/code]
and reading.py:[code]import ROOT

i = ROOT.TFile( ‘myfile.root’ )
mytree = i.Get( ‘mytree’ )

for t in mytree:
print list( t.mystrings )
[/code]
Is that what you are looking for?

Cheers,
Wim[/quote]

Anna,

the reason that it won’t work is b/c CINT will not properly initialize the struct when it is interpreted (it won’t work in a root.exe session either). You’ll have to use ACLiC and a separate file. For example, file EventInfoTree.h:[code]#include
#include

struct EventInfoTree {
std::vector< std::string > v;
};[/code]
and updated writing.py:[code]import ROOT

ROOT.gROOT.LoadMacro( ‘EventInfoTree.h+’ )

o = ROOT.TFile( ‘myfile.root’, ‘RECREATE’ )

t = ROOT.TTree( ‘mytree’, ‘My Tree’ )
evt = ROOT.EventInfoTree();
t._evt = evt
t.Branch( ‘mystrings’, evt.v )

myStrings = [ ‘aap’, ‘noot’, ‘mies’, ‘zus’, ‘jet’ ]

for s in myStrings:
evt.v.push_back( s )
t.Fill()

t.Write()[/code]
whereas the reading will remain the same.

HTH,
Wim