How to efficiently sort a TTree and save the sorted TTree?

I have a TTree containing some parameters and time (actually unix time as Double_t).
I would like to sort the entries of the TTree in growing time and then save the sorted TTree.
The code below works fine but the “saving” part of the code takes about 1 hours to complete for a tree of 2M entries.
It sounds strange to me as it takes about O(minutes) to create the tree and 0(seconds) to create the sorted index.
Is my approach efficient ?

Thanks !

Code :

// TTree to be sorted, containing 2M entries, 35 branches (no arrays).
TTree *t_raw…

// Create index on tunix branch (time)
Int_t nb_idx = t_raw->BuildIndex(“tunix”);
TTreeIndex att_index = (TTreeIndex)t_raw->GetTreeIndex();
TTree* t = (TTree*)t_raw->CloneTree(0);

// Loop on t_raw entries and fill t
for( Long64_t i = 0; i < att_index->GetN(); i++ ) {
t_raw->GetEntry( att_index->GetIndex()[i] );

Sorting trees has been discussed here several times.

Main takeaway: use tree->LoadBaskets(large value) if you want fast random access (requires some free memory…).

Adding t_raw_>LoadBaskets(); works great. Random access is now very fast, randomly parsing the TTree takes time of order of seconds.
Thanks for the quick answer.

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