1 |
antoniov |
1.1 |
#!/usr/bin/env python
|
2 |
|
|
|
3 |
|
|
import os,sys
|
4 |
|
|
from subprocess import call
|
5 |
|
|
import optparse
|
6 |
|
|
|
7 |
|
|
def mergeTTree(inputdir,groupFiles=100,filename='merge.root',outputdir='.',type='root',execute=True):
|
8 |
|
|
files = os.listdir(inputdir)
|
9 |
|
|
files = [ item for item in files if item.find(type) != -1 ]
|
10 |
|
|
files = [ item.rstrip() for item in files ]
|
11 |
|
|
# Divide in groups of files
|
12 |
|
|
nFilesPerSection = groupFiles
|
13 |
|
|
sections = []
|
14 |
|
|
while len(files):
|
15 |
|
|
sections.append(files[:nFilesPerSection])
|
16 |
|
|
files = files[nFilesPerSection:]
|
17 |
|
|
|
18 |
|
|
outfiletype = filename
|
19 |
|
|
index = 0
|
20 |
|
|
for sec in sections:
|
21 |
|
|
outfile = '%s/%s_%s.%s' % (os.path.abspath(outputdir),outfiletype.split('.')[0],index,outfiletype.split('.')[1])
|
22 |
|
|
cmd = 'hadd ' + outfile
|
23 |
|
|
for file in sec: cmd += ' %s/%s' % (inputdir,file)
|
24 |
|
|
print cmd
|
25 |
|
|
if execute: call(cmd,shell=True)
|
26 |
|
|
index += 1
|
27 |
|
|
|
28 |
|
|
if __name__ == '__main__':
|
29 |
|
|
parser = optparse.OptionParser(usage="usage: %prog [options]")
|
30 |
|
|
parser.add_option("-i","--inputdir", dest="inputdir", metavar="DIR", help="add files in DIR")
|
31 |
|
|
parser.add_option("-o","--outputdir", dest="outputdir", default='.', metavar="DIR", help="copy output to DIR (Default: local dir)")
|
32 |
|
|
parser.add_option("-t","--type", dest="type", default="root", metavar="TYPE", help="select only files with substring TYPE (Default: 'root')")
|
33 |
|
|
parser.add_option("-f","--filename", dest="filename", default="merge.root", metavar="FILE", help="output file name (Default: 'merge.root')")
|
34 |
|
|
parser.add_option("-g","--group", dest="group", type="int", default=100, metavar="GROUP", help="merge files in groups of GROUP (Default: 100)")
|
35 |
|
|
parser.add_option("--no_exec", action="store_true", dest="no_exec", default=False, help="script will not execute")
|
36 |
|
|
|
37 |
|
|
(input, args) = parser.parse_args()
|
38 |
|
|
|
39 |
|
|
if not input.inputdir: parser.error('must set input directory')
|
40 |
|
|
|
41 |
|
|
execute = True
|
42 |
|
|
if input.no_exec: execute = False
|
43 |
|
|
|
44 |
|
|
mergeTTree(inputdir=input.inputdir,
|
45 |
|
|
groupFiles=input.group,
|
46 |
|
|
filename=input.filename,
|
47 |
|
|
outputdir=input.outputdir,
|
48 |
|
|
type=input.type,
|
49 |
|
|
execute=execute)
|
50 |
|
|
|
51 |
|
|
sys.exit(0)
|