#include "TFile.h" #include "TTree.h" #include "TBranch.h" #include "TRandom3.h" void treesort() { //example of script illustrating how to sort entries in a Tree in ONE file //the algorithm assumes that all buffers of one branch can fit in memory //This algorithm should be modified in case of one single top level branch //in case the buffers do not fit in memory. Instead one should loop //on its sub-branches. //This algorithm cannot be used to sort entries in a TChain. You should merge //your files in the TChain into one single big file first. TFile *fin = new TFile("d0.root"); //the input file TTree *Tin = (TTree*)fin->Get("h1"); //get the input Tree TFile *fout = new TFile("treesort.root","recreate"); TTree *Tout = Tin->CloneTree(0); Long64_t N = Tin->GetEntries(); Long64_t *table = new Long64_t[N]; Long64_t i; //make a table with the randomized list of entries for (i=0;iGetListOfBranches()); TBranch *bin, *bout; while ((bin = (TBranch*)next())) { bout = Tout->GetBranch(bin->GetName()); printf("processing branch: %s\n",bin->GetName()); //load all baskets of this branch in memory (for performance) bin->LoadBaskets(); //loop on randomized entries and fill output Tree for (i=0;iGetEntry(table[i]); bout->Fill(); } bin->DropBaskets(); } Tout->SetEntries(N); Tout->Print(); Tout->AutoSave(); }