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 ):
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!!