ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/split_tree.py
Revision: 1.1
Committed: Tue Feb 12 08:37:07 2013 UTC (12 years, 3 months ago) by bortigno
Content type: text/x-python
Branch: MAIN
CVS Tags: lhcp_UnblindFix, hcp_Unblind, lhcp_11April, LHCP_PreAppFixAfterFreeze, LHCP_PreAppFreeze, HEAD
Log Message:
@NEW. Script to split samples in fixed number of events and store them

File Contents

# Content
1 #!/usr/bin/env python
2 from optparse import OptionParser
3 from myutils import BetterConfigParser, parse_info
4 import sys
5
6 #CONFIGURE
7 argv = sys.argv
8 parser = OptionParser()
9 parser.add_option("-S","--samples", dest="samples", default='',
10 help="Sample to split. ( comma separated )")
11 parser.add_option("-C", "--config", dest="config", default=[], action="append",
12 help="configuration file")
13 parser.add_option("-M", "--max-events", dest='maxEvents', default=10000,
14 help="max number of events per file. Default 10000")
15
16
17 (opts, args) = parser.parse_args(argv)
18 if opts.config =="":
19 opts.config = "config"
20 config = BetterConfigParser()
21 config.read(opts.config)
22
23 import ROOT
24 ROOT.gROOT.SetBatch(True)
25
26 namelistIN=opts.samples
27 namelist=namelistIN.split(',')
28 samplesinfo=config.get('Directories','samplesinfo')
29 INpath=config.get('Directories','samplepath')+'/envW/sys/'
30 OUTpath=INpath # for the moemnt OUTpath = INpath ->> TO FIX
31 info = parse_info(samplesinfo,INpath)
32
33
34 for job in info:
35 if eval(job.active):
36 if job.name in namelist:
37 #get trees:
38 print INpath+'/'+job.prefix+job.identifier+'.root'
39 sourceFile = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
40 Tree = sourceFile.Get('tree')
41 obj = ROOT.TObject
42 nentries = Tree.GetEntries()
43 print nentries
44 maxEvents = long(opts.maxEvents)
45 #get the number of files to be created
46 if(maxEvents > 0.):
47 number_of_files = (nentries/maxEvents)+1
48 else :
49 sys.exit('%ERROR: Max number of events per file null or negative.')
50 print 'Splitting in ' + str(number_of_files) +' files'
51 for i in range(number_of_files):
52 print i
53 #create the output file
54 print OUTpath+'/split/'+job.prefix+job.identifier+'_'+str(i)+'.root'
55 output = ROOT.TFile.Open(OUTpath+'/split/'+job.prefix+job.identifier+'_'+str(i)+'.root','recreate')
56 #copy the histograms in the new file
57 sourceFile.cd()
58 for key in ROOT.gDirectory.GetListOfKeys():
59 obj = key.ReadObj()
60 if obj.GetName() == 'tree':
61 continue
62 output.cd()
63 obj.Write(key.GetName())
64 #now split and copy the tree
65 output.cd()
66 outTree = Tree.CopyTree("","",maxEvents*(i+1),maxEvents*i)
67 outTree.AutoSave()
68 output.Write()
69 output.Close()
70 sourceFile.Close()
71