Reading vectors from a TTree

Dear ROOT experts,

I have a tree containing vector* type in it. For reading the entries from the tree I do the usual:

TChain fChain;
vector<double>  *elPt;
TBranch             *b_elPt;  
elPt=0;

fChain->SetBranchAddress("elPt", &elPt, &b_elPt);

for (int entry=0; entry<fChain->GetEntries(); entry++){
   fChain->GetEntry(entry)
}

But it happens that my vector (*elPt)[] is not always filled. My tree contains events where (elPt->size()==0).
When my loop reaches such an event, I just noticed that the ->GetEntry() method is not setting elPt to an empty vector but instead elPt holds the values of the last event where elPt was indeed filled.

Could you please tell me what should be the most efficient method to avoid this problem, and correctly fill the elPt vector, even in the cases where this should be empty.

Thanks in advance,
Miguel

What do you get from (you should see “entries” with zero “size”): for (Long64_t entry = 0; entry < fChain->GetEntries(); entry++) { std::cout << "entry = " << entry << " bytes = " << fChain->GetEntry(entry) << " size = " << elPt->size() << std::endl; }

Yes, the vector size is 0 which is the correct value. One should primarily check for the vector size, which is a good programming practice.

Nevertheless, I think it would be important to have a strong warning in the TTree class documentation for this case, since this is a feature that might originate bugs that are very hard to reveal themselves. I could not find, but in case such a message is already in the documentation, please ignore my suggestion.

Thanks,
Miguel

The std::vector::size returns the number of actual objects / elements held in the vector container.
If you want to be punished when you try to access a non-existent object / element, use std::vector::at instead of std::vector::operator[].