Sort TTree entries

Hi,

Is there a way to sort the TTree entries given the values stored in one branch?

For example, I have a TTree with events. One branch contains the signal-to-noise ratio (SNR) of each events. I’d like to loop over the entries (with tree->GetEntry(t)) and read the TTree by decreasing order of SNR.

Is there a way of doing that?
Thank you.

Hi,

You can do something like:tree->BuildIndex("SNR"); TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex(); for( int i = index->GetN() - 1; i >=0 ; --i ) { Long64_t local = tree->LoadTree( index->GetIndex()[i] ); branch->GetEntry(local); .... }

Cheers,
Philippe.

Hi Philippe,

This looks interesting. I’m not so familiar with the TTreeIndex class. Where is the SNR-sorting actually performed? is it at the level of the BuildIndex function?

Thanks

is it at the level of the BuildIndex function?

yes, BuildIndex will evaluate the formula given at each entry and then sort them in increasing order.

Cheers,
Philippe.

I notice that TChainIndex does not have GetIndex. Is there a way to do this using a TChain instead of a TTree? I just want to loop in index order.

Yes, it shouldn’t make a difference.

GetIndex is spelt GetEntryWithIndex :slight_smile: