Accessing a branch that contains a vector and initializing a new vector to that

I’m trying to initialize a vector and assign it to a TTree branch that is a vector. So the TTree “treename” has a branch called “jetTruthLabel”. This branch contains vector. I want to be able to access that vector and use it but I can only get the branch, not the vector itself.

Current example of part of my code:

TFile *f = new TFile("filename","Update");
TTree *tree = (TTree*) f->Get("treename");

TBranch* br_ntruth = tree->GetBranch("jetTruthLabel");

Int_t nTruthB=0;
Int_t nTruthC=0;


nTruthC = count(br_ntruth->begin(), br_ntruth->end(), 4);
nTruthB = count(br_ntruth->begin(), br_ntruth->end(), 5);


tree->Branch("nTruthB", &nTruthB, "nTruthB/I");
tree->Branch("nTruthC", &nTruthC, "nTruthC/I");

ROOT Version: 6.24/06
Platform: Linux Ubuntu
Compiler: Not Provided


Hi. Maybe I can help you. If I understood your question, you have in jetTruthyLabel a vector of something for each event in the tree.I think you could try a for loop and GetEntry.
After
“TBranch* br…”
Try

vector<_something_> *vec  = new vector<_something_>;
br_ntruth->SetAddress(&vec);
for(int ev=0;ev<tree->GetEntries();ev++){
vec->clear();
tree->GetEntry(ev);........}

This way you will put in vec the vector you have in your branch (and you can read all branches for an event ev with GetBranch and SetAddress).

Or try ROOT TTree tutorial hvector.C

Oh! I think it’s working! It’s a pretty massive tree so it will take a while to run but it seems to be running cleanly now.
Thank you!

To speed something up you could replace the

tree->GetEntry(ev);

by

auto localentry = tree->LoadEntry(ev);

followed by a series of

br1->GetEntry(localentry);
br2->GetEntry(localentry);
...

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