Extracting information from root trees

Hello All,

I have two root trees : Singles and Hits.
No of entries in Hits >> Singles.
I am reading all Singles and would like to extract events from the Hits tree corresponding to the singles. There usually are more than one hit entry with the same eventID as the single. What is a fast method to do this? Right now I have this horribly inefficient code:

for (i=0;i<NoOfSingles;i++) { nbytes += Coincidences->GetEntry(i); ... for (hitindex=0;hitindex<NoOfHits;hitindex++){ nhitbytes += Hits->GetEntry(hitindex); if (HitEventID == SingleEventID) { ... ... } } }

My root file has 200326 singles and 1173180 Hits and I really do not know how the hits are ordered.



You might be able to use a TTreeIndex. See TTree::BuildIndex and TTree::GetEntryWithIndex


Thanks for the reply. I looked up the user guide and tried modifying the code. I have changed it to:

Single->SetBranchAddress("runID",&SinglerunID); Single->SetBranchAddress("eventID",&SingleEventID); Hits->BuildIndex("runID","eventID"); for (i=0;i<NoOfSingles;i++) { nbytes += Single->GetEntry(i); Hits->GetEntryWithIndex(SinglerunID,SingleEventID); }

Am I doing this right? There are several hits corresponding to a Single. Hits->GetEntryWithIndex(SinglerunID,SingleEventID); gets me (I think) the first of many records with the same run and eventID as the single. How do I get the other recrods from the Hit tree?

Thanks in advance.


There is no easy interface for that. However the following might work for you:TTreeIndex *index = (TTreeIndex*)hits->GetTreeIndex(); if (index && index->GetN()) { Long64_t value = Long64_t(SinglerunID)<<31; value += SingleEventID; Int_t i = TMath::BinarySearch(index->GetN(), index->GetIndexValues(), value); if (i >= 0) { while( index->GetIndexValues()[i] == value ) { Hits->GetEntry( index->GetIndex()[i] ); // Do the work. } } }