Fastest way to sample a TTree and write to new Ttree

Dear experts,

I am sort of new to ROOT and pyROOT, so please excuse my ignorance. I am looking for the fastest way to randomly select 1000 (or better, 10% of the original TTRee) events from a given TTree and write these events to a new TTree.

My current approach is long-winded and buggy, attached.

[code]from ROOT import *
import math

fin = TFile(’/vols/lhcb/palvare1/B2pphgamma/B2ppKgamma_MC/B2ppKgamma_S21.root’)
tin = fin.Get(‘B2pphgamma_Tuple/DecayTree’)
fout = TFile(‘sampled_signal’, ‘recreate’)
tout = tin.CloneTree(0)

nentries = tin.GetEntries()
print “tree has %d entries”%nentries

rand = TRandom3(0)
N = 1000

for i in range(0, N):
j = rand.Integer(nentries)

nentries = tout.GetEntries()
print “tree has %d entries”%nentries

Thank you for your help.

Consider using

for i in range(0, nentries): if (rand.Rndm() < 0.1): # Keep only 10% tin.GetEntry(i) tout.Fill()