I need to find a fast method to sort events in a tchain according to a variable in the chain. The tchain consists of typically two 2GB root files, with a sum of about 500 000 events, so its a quite cpu-demanding task.
I have tried the sorting function attached below that uses the TMath::Sort-function to sort an array containing the variable that determines the order I would like the tree to be in (this array is extracted from the tree previously in the program). Events are then fetched from the tchain according to the sorted array and a new TTree is filled which then will be ordered wit respect to the sorting-variable. This function is very fast as long as it doesnt have to fetch events from different files in the tchain alternatively, but then it takes about 1 second per event (on my rather slow laptop, but anyway) and this is way to slow.
So my question: Is it possible to speed up this sorting-function somehow, or do you know a faster way to do this? Many thanks for your help!
cheers
Petter
void orbitsort(TTree *sorted,TTree *unsorted, Double_t *obts){
Int_t nentries=unsorted->GetEntries();
Int_t *index = new Int_t[nentries];
Int_t *index_pnt=index;
int ijk;
TMath::Sort(nentries, obts, index, 0);
for (ijk=0;ijkGetEntry(*index_pnt++);
sorted->Fill();
}
}
see the example in attachement and modify it according to your needs.
See the limitations inside. I have no time now to make it more general than
it is and remove the limitations.