Re: [ROOT] Sorting a tree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Oct 22 2001 - 22:24:17 MEST


Hi Alberto,

If you have difficulties understandind what a TEventList is,
you will find a trivial example below. This function sorts
the Tree from the Root tutorials hsimple.root in decreasing order
of the variable pz.

void sort() {
	TFile f("hsimple.root");
	TTree *tree = (TTree*)f.Get("ntuple");
	Int_t nentries = (Int_t)tree->GetEntries();
	//Drawing variable pz with no graphics option.
	//variable pz stored in array fV1 (see TTree::Draw)
	tree->Draw("pz","","goff");
	Int_t *index = new Int_t[nentries];
	//sort array containing pz in decreasing order
	//The array index contains the entry numbers in decreasing order
of pz
	TMath::Sort(nentries,tree->GetV1(),index);
	
	//open new file to store the sorted Tree
	TFile f2("hsimple_sorted.root","recreate");
	//Create an empty clone of the original tree
	TTree *tsorted = (TTree*)tree->CloneTree(0);
	for (Int_t i=0;i<nentries;i++) {
		tree->GetEntry(index[i]);
		tsorted->Fill();
	}
	tsorted->Write();
	delete [] index;
}

Rene Brun

On Mon, 22 Oct 2001, Rene Brun wrote:

> Hi Alberto,
> 
> You can use a TEventList to sort the Tree.
> Just create an empty TEventList and enter your events in an order
> that can choose based on your own algorithm.
> Once you have the TEventlist, you can
>   - use TTree::SetEventList(mylist). When looping on the entries,
>     functions such as TTree::draw will loop in the order specified in the list
>   - write a new sorted Tree in the same loop
> 
> Rene Brun
> 
> Alberto Pulvirenti wrote:
> > 
> > Dear rooters,
> > is there anyone who knows if (and, eventually, how) it is possible to
> > sort a tree, re-ordering its entries with respect to a specified criterion?
> > 
> > Thanks.
> > 
> > Alberto
> 



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:03 MET