Home | News | Documentation | Download

Sorting a tree by entries



Hey there,

I’m looking for a way to sort trees by a specific field of a tree (event number). Can you show me a way to create a sorted TEntryList/TEventlist? In the class reference I saw that the TEventlist has a Sort() method but I don’t quite understand how to specify the operation to sort by (neither the field to sort by).

Adam Hunyadi


Hi Adam,
sorry for the late reply – do you have a single file or multiple files?

I would not know how to sort a full TTree (TEventList::Sort does not take a custom comparison function, it is a no-go for what you are asking) – are you double sure that this is what you need though? Sorting large amounts of data that might not even fit in memory (or alternatively, loop over unsorted data jumping back and forth to follow some sort of external ordering) is very inefficient.

Pinging @pcanal and @Axel for extra help :slight_smile:


The “manual” way: calculate some “ordering value” from all events. Then sort a vector<pair<event_number, order_value>>. Then loop over the sorted vector, read the event and write it to the sorted file. You can use tree->LoadBaskets(large_value) to preload the data and increase speed.

If your tree is large/huge, then split it into smaller trees, for example a few 100 MB each (so that you can load the whole tree), sort them, and then merge the sorted subtrees.

The real question is: why do you need your tree to be sorted? Is there a different way to achieve what you really need?


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