Cut by function in TTree

Hi all,

I want to perform a selection on a TTree to get a subsample of it as a TTree. The problem is that the selection is not a trivial function, but rather a complex one that cannot be expressed by a formula. Besides, I would like to do this selection in a way that is reusable because many different complex cuts will need to be applied.

Therefore, in PyROOT I have done something like

def cloneTreeByFunction( tree, filename , f ): newfile = TFile( filename , "RECREATE" ) newtree = tree.CloneTree(0) entry = 0 passed = 0 while ( tree.GetEntry( entry ) ): entry = entry + 1 if not f( tree ): continue newtree.Fill() passed = passed + 1 print "New tree created. Passed %s/%s entries" % ( passed , entry ) return newfile , newtree

And then I define a function that executes the cuts and returns True or False. This is convenient, but not very fast. I could probably write it in C++, but I don’t know if I would get a real speed gain in that (and besides, I would prefer to do the cut functions in Python).

My question is if this is a good way of doing it or am I just plain wrong? Is the CloneTree usage correct? Any suggestions to improve it, both in speed and flexibility? Thanks in advance!!


see example in $ROOTSYS/tutorials/tree/copytree3.C


Thanks! In fact I followed that example, I just translated it into python. It seems that there is no way to do things faster, then.


[quote]It seems that there is no way to do things faster, then. [/quote]2 ways to make it faster: one is to write in C++ and compile the code. The 2nd one is to only read the branch you really need. I.e. replace the call to tree.GetEntry( entry ) by localentry = tree.LoadTree(entry) and in your function call GetEntry( localentry) on the branches you need.
[By the way, this is handled automatically if you use MakeProxy or if you pass a C script to TTree::Draw), also pyroot might have similar facilities]