Filling branches with STLcollection

Dear PyROOT,

I am trying to fill a TTree with a collection of values. I have done this in cplusplus with the code below and want to translate it into PyROOT

TTree* tree = new TTree("name", "title"); unsigned int n_tracks = 10; vector<Double_t>* track_eta = new vector<Double_t>(); // Double_t eta[n_tracks]; tree->Branch("track_eta", &track_eta); tree->Branch("n_tracks", &n_tracks); for(unsigned int i=0; i<n_tracks; i++) track_eta->push_back(1); tree->Fill();

from what I have found on the forums I need to have something along the lines of

[code]gROOT.ProcessLine(‘struct MyStruct{Int_t n_tracks; vector<Double_t>* track_eta;};’)
tree = TTree(‘eta_bin_tree’, ‘eta_bin_tree’)

from ROOT import MyStruct
s = MyStruct()

tree.Branch(‘n_tracks’, AddressOf(s, ‘n_tracks’), ‘n_tracks/I’)
tree.Branch(‘track_eta’, AddressOf(s, ‘track_eta’), ‘track_eta/D’)
s.track_eta = [1, 1, 1][/code]

but I haven’t been able to get it to work. Can you help?



you don’t need a “MyStruct” and can set the vector directly. In the example code, the vector pointer of MyStruct should point to an actual vector, so if you want to use such a struct, use a vector object instead and compile rather than use CINT. Use push_back() to add elements to the vector. A vector of doubles is also not a double. List assignment won’t work, but is also not what you want, as it would normally replace the existing vector. Something like this:

[code]from ROOT import *
gROOT.ProcessLine(‘struct MyStruct{Int_t n_tracks; vector<Double_t>* track_eta;};’)
tree = TTree(‘eta_bin_tree’, ‘eta_bin_tree’)

s = MyStruct()
s.track_eta = std.vector(‘double’)()

tree.Branch(‘n_tracks’, AddressOf(s, ‘n_tracks’), ‘n_tracks/I’)
tree.Branch(‘track_eta’, s.track_eta)
for i in [1,1,1]: