Reading vector of structs from TTree

Hi,

I have filled a TTree according to the struct ‘particle’, which contains a vector of structs of the type ‘cluster’:

struct cluster {
    
    Double_t x0, y0, z0, t0, ec;
    Int_t nc;
    std::vector<Double_t> x1, y1, z1, t1, e1, vx1, vy1, vz1;
};

struct particle {
    
    TString type;
    Double_t x0, y0, z0, vx0, vy0, vz0, e0, t0;
    Int_t noClusters;
    Double_t lambda, stoppingPower;
    std::vector<cluster> clusters;
};

For filling the TTree, I have made the dictionaries for both particle and the cluster structures. Now I want to read the tree (e.g. the size of the cluster vector) with the following code:

gROOT->ProcessLine(".L loader.c+"); // loading dictionaries
TTree *pTree = (TTree*)f->Get("pTree"); // read TTree from file
std::vector<cluster> *c;
pTree->SetBranchAddress("clusters", &c);

Int_t it = (Int_t)pTree->GetEntries();
for(int i=0; i<it; i++) {

    pTree->GetEntry(i);
    std::cout << c.size() std::endl;
}

When I run this code, ROOT crashes with the following errors:

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007f66937e1d60 in ROOT::TCollectionProxyInfo::Pushback<std::vector<cluster, std::allocator<cluster> > >::resize(void*, unsigned long) () from /afs/cern.ch/work/j/jeyserma/private/GEMSimulations/timeResolution/./loader_c.so
#6  0x00007f669c3df070 in TGenCollectionProxy::Allocate(unsigned int, bool) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc46-opt/root/lib/libRIO.so
#7  0x00007f669aa9a8e0 in TBranchElement::ReadLeavesCollection(TBuffer&) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc46-opt/root/lib/libTree.so
#8  0x00007f669aa91aaf in TBranch::GetEntry(long long, int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc46-opt/root/lib/libTree.so
#9  0x00007f669aa9cba6 in TBranchElement::GetEntry(long long, int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc46-opt/root/lib/libTree.so
#10 0x00007f669aadbe8c in TTree::GetEntry(long long, int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc46-opt/root/lib/libTree.so
#11 0x00000000004185aa in main ()

Does anybody know what I’m doing wrong?
I’m using ROOT version 5.34/10 on lxplus.

Thanks,
Jan

Hi Jan,

could you post your loader.c file and the code you wrote to fill the tree and write it in the root file?

Cheers,
Danilo

std::vector<cluster> *c = 0;
or
std::vector<cluster> *c = new std::vector<cluster>;
and
std::cout << c->size() << std::endl;

Thanks, the initialization of the std::vector<cluster> did the job!

Jan

Hi,

Could someone point the code please? i have a similar issue when booking an std vector of struct inside AthAlgorithm code.

Mateus

Hi @mhufnage,

See the fix in the post above, i.e. Reading vector of structs from TTree - #3 by Wile_E_Coyote. Otherwise, feel free to open a new topic.

Cheers,
J.