1 |
#!/usr/bin/env python
|
2 |
import errno
|
3 |
|
4 |
import os
|
5 |
import os.path
|
6 |
import commands
|
7 |
import re
|
8 |
import sys
|
9 |
from copy import deepcopy
|
10 |
|
11 |
header_eq="="*50
|
12 |
header_dash="-"*50
|
13 |
|
14 |
def chunks(l, n):
|
15 |
for i in xrange(0, len(l), n):
|
16 |
yield l[i:i+n]
|
17 |
|
18 |
# def ensure_dir(f):
|
19 |
# d = os.path.dirname(f)
|
20 |
# if not os.path.exists(d):
|
21 |
# os.makedirs(d)
|
22 |
|
23 |
def ensure_dir(path):
|
24 |
try:
|
25 |
os.makedirs(path)
|
26 |
except OSError as exc: # Python >2.5
|
27 |
if exc.errno == errno.EEXIST:
|
28 |
pass
|
29 |
else: raise
|
30 |
|
31 |
|
32 |
YourSusyDir = commands.getoutput('echo $SUSY_WORKING_SW_DIR') #Get the susy working Dir
|
33 |
print YourSusyDir
|
34 |
YourScriptsDir = commands.getoutput('pwd')
|
35 |
|
36 |
|
37 |
def BatchRun(samples,analysis,config,outputDir,nFilesPerJob):
|
38 |
|
39 |
print header_eq
|
40 |
print "Running Batch Analysis"
|
41 |
print header_eq
|
42 |
print "On %d sample(s):" % len(samples)
|
43 |
for sample in samples:
|
44 |
print "\t%s " % (sample.Name)
|
45 |
print header_eq
|
46 |
|
47 |
#copy contents of file
|
48 |
folder = YourScriptsDir+"/tmp/"
|
49 |
ensure_dir(folder) # where we'll put all our files
|
50 |
filename = sys.argv[0]
|
51 |
file = open(filename,"r")
|
52 |
code = file.read()
|
53 |
|
54 |
#re for things we need to change
|
55 |
p = re.compile('samples\s?=\s?\[*?.*?\]', re.DOTALL)
|
56 |
q = re.compile('BatchRun\(*?.*?\)')
|
57 |
|
58 |
#loop over each suplied sample, then fragment in to subsamples each of 10 files
|
59 |
for sample in samples:
|
60 |
tmpSamplePset = deepcopy(sample)
|
61 |
filesList = list(chunks(sample.File,nFilesPerJob))
|
62 |
|
63 |
#Loop over fragments of samples
|
64 |
for files,i in zip(filesList,range(len(filesList))) :
|
65 |
fragName = sample.Name +"_part"+str(i)
|
66 |
print "submitting " + folder + fragName + " to batch queue."
|
67 |
|
68 |
#replace samples in copy of file to fragment samples
|
69 |
tmpSamplePset.Name = "\"" + fragName + "\""
|
70 |
tmpSamplePset.File = files
|
71 |
string = "sample = PSet("
|
72 |
for (k,v) in tmpSamplePset._flatten().iteritems() :
|
73 |
string += "\n\t%s = %s ,"% (k,v)
|
74 |
string +="\n)"
|
75 |
string += "\nsamples=[sample]"
|
76 |
newCode = p.sub(string, code)
|
77 |
ensure_dir(outputDir+"/"+sample.Name)
|
78 |
#replace batch run with normal "a.run()"
|
79 |
string = analysis+".Run( \""+outputDir +sample.Name +"/\"," +config +", samples)"
|
80 |
newCode = q.sub(string, newCode)
|
81 |
|
82 |
#write .py
|
83 |
temp = open(folder+filename[:-3]+analysis+"_"+fragName+".py", 'w')
|
84 |
temp.write(newCode)
|
85 |
temp.close()
|
86 |
|
87 |
#write .sh that we will submit to batch
|
88 |
temp = open(folder+filename[:-3]+analysis+"_"+fragName+".sh", 'w')
|
89 |
script = '\n'.join([
|
90 |
'#!/bin/sh',
|
91 |
'source '+YourSusyDir+'/setup.sh',#Need to be changed to be more portable
|
92 |
'cd '+ YourScriptsDir+"/tmp/",# There is a $SUSY_WORKING_DIR path, " how to make python expand this"
|
93 |
filename[:-3]+analysis+"_"+fragName+".py"
|
94 |
])
|
95 |
temp.write(script)
|
96 |
temp.close()
|
97 |
|
98 |
#make executable
|
99 |
os.chmod(folder+filename[:-3]+analysis+"_"+fragName+".py",0777)
|
100 |
|
101 |
#Submit
|
102 |
output = commands.getstatusoutput("qsub -q hep.q " + folder+filename[:-3]+analysis+"_"+fragName+".sh")
|
103 |
#Succesful?
|
104 |
if output[0] != 0 :
|
105 |
print "\tError occured:"
|
106 |
print output
|
107 |
break
|
108 |
else :
|
109 |
print output[1]
|
110 |
|
111 |
|
112 |
#make clean up script that
|
113 |
#hadd root files
|
114 |
#deletes fragments of root files
|
115 |
#deletes .py and .sh
|
116 |
|