1 |
paus |
1.2 |
#!/usr/bin/env python
|
2 |
|
|
#---------------------------------------------------------------------------------------------------
|
3 |
|
|
# Script to test, create and submit one complete production task
|
4 |
|
|
#
|
5 |
|
|
# Complete refers here to the proper preparation of the ultimate storage location (a storge element
|
6 |
|
|
# with a given storage path etc.), the submission of a test job to evaluate timing and data output
|
7 |
|
|
# size and finally the creation of the job configurations and the submission of the jobs to the grid
|
8 |
|
|
# sites. The grid is accessed via crab tools.
|
9 |
|
|
#
|
10 |
|
|
# While the script is pretty complete it has become a bit long and messy. Some cleanup will
|
11 |
|
|
# hopefully soon be performed.
|
12 |
|
|
#
|
13 |
|
|
# Author: C.Paus (July 1, 2008)
|
14 |
|
|
#---------------------------------------------------------------------------------------------------
|
15 |
|
|
import os,sys,getopt,re,string
|
16 |
paus |
1.3 |
import task,translator
|
17 |
paus |
1.2 |
|
18 |
|
|
#===================================================================================================
|
19 |
|
|
def searchReplace(line,mitCfg,version,mitDataset, \
|
20 |
|
|
cmsDataset='X',cmsswPy='X',dbs='X',sched='X',blacklist='X',skpEvts='X'):
|
21 |
|
|
# compile search and replacement sequences
|
22 |
|
|
pCmsDset = re.compile('XX-CMSDATASET-XX')
|
23 |
|
|
pMitDset = re.compile('XX-MITDATASET-XX')
|
24 |
|
|
pCmsswPy = re.compile('XX-CMSSWPY-XX')
|
25 |
|
|
pMitCfg = re.compile('XX-MITCFG-XX')
|
26 |
|
|
pMitVers = re.compile('XX-MITVERSION-XX')
|
27 |
|
|
pDbs = re.compile('XX-DBS-XX')
|
28 |
|
|
pSched = re.compile('XX-SCHED-XX')
|
29 |
|
|
pBlacklist = re.compile('XX-BLACKLIST-XX')
|
30 |
|
|
pSkpEvts = re.compile('XX-SKIPEVTS-XX')
|
31 |
|
|
# perform all search and replaces
|
32 |
|
|
line = pCmsDset .sub(cmsDataset, line);
|
33 |
|
|
line = pMitDset .sub(mitDataset, line);
|
34 |
|
|
line = pCmsswPy .sub(cmsswPy, line);
|
35 |
|
|
line = pMitCfg .sub(mitCfg, line);
|
36 |
|
|
line = pMitVers .sub(version, line);
|
37 |
|
|
line = pDbs .sub(dbs, line);
|
38 |
|
|
line = pSched .sub(sched, line);
|
39 |
|
|
line = pBlacklist.sub(blacklist, line);
|
40 |
|
|
return line
|
41 |
|
|
|
42 |
|
|
#===================================================================================================
|
43 |
|
|
def adjustCfg(line,nevents,crabId):
|
44 |
|
|
# compile search and replacement sequences
|
45 |
|
|
pNevents = re.compile('XX-NEVENTS-XX')
|
46 |
|
|
pCrabId = re.compile('XX-CRABID-XX')
|
47 |
|
|
# perform all search and replaces
|
48 |
|
|
line = pNevents .sub(str(nevents),line);
|
49 |
|
|
line = pCrabId .sub(crabId, line);
|
50 |
|
|
return line
|
51 |
|
|
|
52 |
|
|
#===================================================================================================
|
53 |
|
|
def findStoragePath(mitCfg,version,mitDataset):
|
54 |
|
|
# find the forseen storage place
|
55 |
paus |
1.4 |
cmd = 'grep ^storage_path ' + os.environ['MIT_PROD_DIR'] \
|
56 |
|
|
+ '/' + mitCfg + '/' + version + '/crab.cfg'
|
57 |
paus |
1.2 |
for file in os.popen(cmd).readlines():
|
58 |
|
|
line = file[:-1]
|
59 |
|
|
line = searchReplace(line,mitCfg,version,mitDataset);
|
60 |
|
|
# decode the storage directory name
|
61 |
|
|
names = line.split("=")
|
62 |
|
|
names = names[1:]
|
63 |
|
|
storagePath = "=".join(names)
|
64 |
|
|
storagePath = re.sub("\s", "",storagePath)
|
65 |
paus |
1.4 |
cmd = 'grep ^user_remote_dir ' + os.environ['MIT_PROD_DIR'] \
|
66 |
|
|
+ '/' + mitCfg + '/' + version + '/crab.cfg'
|
67 |
paus |
1.2 |
for file in os.popen(cmd).readlines():
|
68 |
|
|
line = file[:-1]
|
69 |
|
|
line = searchReplace(line,mitCfg,version,mitDataset);
|
70 |
|
|
# decode the storage directory name
|
71 |
|
|
names = line.split(" ")
|
72 |
|
|
storagePath += names[-1]
|
73 |
|
|
return storagePath
|
74 |
|
|
|
75 |
|
|
#===================================================================================================
|
76 |
|
|
def createDirCern(storagePath):
|
77 |
|
|
# check whether path exists
|
78 |
|
|
cmd = 'rfdir ' + storagePath + ' >& /dev/null'
|
79 |
|
|
status = os.system(cmd)
|
80 |
|
|
|
81 |
|
|
# create it if missing
|
82 |
|
|
if status == 0:
|
83 |
|
|
print ' Castor directory exists: ' + storagePath + '\n --> Moving on.'
|
84 |
|
|
else:
|
85 |
|
|
print ' Castor directory needs to be created.'
|
86 |
|
|
cmd = 'rfmkdir -p ' + storagePath
|
87 |
|
|
status = os.system(cmd)
|
88 |
|
|
if status == 0:
|
89 |
|
|
print ' --> Created: ' + storagePath
|
90 |
|
|
|
91 |
|
|
# always set the permissions
|
92 |
|
|
cmd = 'rfchmod 777 ' + storagePath
|
93 |
|
|
status = os.system(cmd)
|
94 |
|
|
if status == 0:
|
95 |
|
|
print ' --> Set permissions: 777\n'
|
96 |
|
|
else:
|
97 |
|
|
print ' --> Setting permissions failed. EXIT now.\n'
|
98 |
|
|
sys.exit(1)
|
99 |
|
|
|
100 |
|
|
#===================================================================================================
|
101 |
|
|
def createDirGeneral(storageEle,storagePath):
|
102 |
|
|
# create all relevant subdirectories
|
103 |
|
|
f = storagePath.split('/') # splitting every '/'
|
104 |
|
|
storagePath2 = "/".join(f[:-1])
|
105 |
|
|
storagePath1 = "/".join(f[:-2])
|
106 |
|
|
storagePath0 = "/".join(f[:-3])
|
107 |
|
|
|
108 |
|
|
# set the storage URL
|
109 |
|
|
storageUrl = 'srm://' + storageEle + ':8443' + storagePath
|
110 |
|
|
|
111 |
|
|
# check whether path exists
|
112 |
|
|
cmd = 'srmls ' + storageUrl + ' >& /dev/null'
|
113 |
|
|
status = os.system(cmd)
|
114 |
|
|
|
115 |
|
|
# create it only if missing
|
116 |
|
|
if status == 0:
|
117 |
|
|
print ' '
|
118 |
|
|
print ' Directory already found.... moving on.'
|
119 |
|
|
|
120 |
|
|
else:
|
121 |
|
|
# create all relevant directories
|
122 |
|
|
cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath0 + ' >& /dev/null'
|
123 |
|
|
print ' srmmkdir: ' + cmd
|
124 |
|
|
status = os.system(cmd)
|
125 |
|
|
print ' srmmkdir: status %d'%(status)
|
126 |
|
|
cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath1 + ' >& /dev/null'
|
127 |
|
|
print ' srmmkdir: ' + cmd
|
128 |
|
|
status = os.system(cmd)
|
129 |
|
|
print ' srmmkdir: status %d'%(status)
|
130 |
|
|
cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath2 + ' >& /dev/null'
|
131 |
|
|
print ' srmmkdir: ' + cmd
|
132 |
|
|
status = os.system(cmd)
|
133 |
|
|
print ' srmmkdir: status %d'%(status)
|
134 |
|
|
|
135 |
|
|
# create the main storage directory
|
136 |
|
|
cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath + ' >& /dev/null'
|
137 |
|
|
print ' srmmkdir: ' + cmd
|
138 |
|
|
status = os.system(cmd)
|
139 |
|
|
print ' srmmkdir: status %d'%(status)
|
140 |
|
|
if status == 0:
|
141 |
|
|
print ' '
|
142 |
|
|
print ' Directory was created.'
|
143 |
|
|
else:
|
144 |
|
|
print ' '
|
145 |
|
|
print ' '
|
146 |
|
|
print ' '
|
147 |
|
|
print ' ERROR - failed to create the Storage Area.'
|
148 |
|
|
print ' '
|
149 |
|
|
print ' '
|
150 |
|
|
|
151 |
|
|
print ' Check permissions with: srmls -l -count=1 ' + storageUrl + '\n'
|
152 |
|
|
cmd = 'srmls -l -count=1 ' + storageUrl + ' | grep GroupPermission | grep RWX'
|
153 |
|
|
status = -1
|
154 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
155 |
|
|
line = line[:-1]
|
156 |
|
|
print " Permissions? " + line
|
157 |
|
|
status = 0
|
158 |
|
|
#status = os.system(cmd)
|
159 |
|
|
print ' '
|
160 |
|
|
|
161 |
|
|
if status != 0:
|
162 |
|
|
print ' --> Directory creation or permissions failed. EXIT now.\n'
|
163 |
|
|
sys.exit(1)
|
164 |
|
|
|
165 |
|
|
#===================================================================================================
|
166 |
|
|
# Main starts here
|
167 |
|
|
#===================================================================================================
|
168 |
|
|
# Define string to explain usage of the script
|
169 |
|
|
usage = "Usage: submit.py --cmsDataset=<name> | --mitDataset=<name>\n"
|
170 |
|
|
usage += " --cmssw=<name>\n"
|
171 |
|
|
usage += " --mitCfg=<name>\n"
|
172 |
|
|
usage += " --version=<version>\n"
|
173 |
|
|
usage += " --dbs=<name>\n"
|
174 |
|
|
usage += " --sched=<name>\n"
|
175 |
|
|
usage += " --blacklist=<name>\n"
|
176 |
|
|
usage += " --nSubmit=<submittedJobs>\n"
|
177 |
|
|
usage += " --skipEvents=<'nRunX:nEventY','nRunXX:nEventYY',...>\n"
|
178 |
|
|
usage += " --complete\n"
|
179 |
|
|
usage += " --testJob\n"
|
180 |
|
|
usage += " --noTestJob\n"
|
181 |
|
|
usage += " --help\n"
|
182 |
|
|
|
183 |
|
|
# Define the valid options which can be specified and check out the command line
|
184 |
|
|
valid = ['cmsDataset=','mitDataset=','cmssw=','mitCfg=','version=','dbs=','sched=','blacklist=',
|
185 |
|
|
'nSubmit=','skipEvents=','complete','testJob','noTestJob','test','help']
|
186 |
|
|
try:
|
187 |
|
|
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
188 |
|
|
except getopt.GetoptError, ex:
|
189 |
|
|
print usage
|
190 |
|
|
print str(ex)
|
191 |
|
|
sys.exit(1)
|
192 |
|
|
|
193 |
|
|
# --------------------------------------------------------------------------------------------------
|
194 |
|
|
# Get all parameters for the production
|
195 |
|
|
# --------------------------------------------------------------------------------------------------
|
196 |
|
|
# Set defaults for each option
|
197 |
|
|
cmsDataset = None
|
198 |
|
|
mitDataset = None
|
199 |
|
|
cmd = "date +crab_0_%y%m%d_%H%M%S"
|
200 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
201 |
|
|
line = line[:-1]
|
202 |
|
|
crabId = line
|
203 |
|
|
print "\n This job will be CrabId: " + crabId + "\n"
|
204 |
|
|
|
205 |
|
|
cmssw = "cmssw"
|
206 |
paus |
1.3 |
mitCfg = "filefi"
|
207 |
|
|
version = "014"
|
208 |
paus |
1.2 |
#dbs = "https://cmsdbsprod.cern.ch:8443/cms_dbs_prod_global/servlet/DBSServlet"
|
209 |
|
|
dbs = "http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
|
210 |
|
|
sched = "glite"
|
211 |
|
|
blacklist = ""
|
212 |
|
|
nSubmit = -1
|
213 |
|
|
skpEvts = ''
|
214 |
|
|
complete = 0
|
215 |
|
|
noTestJob = 0
|
216 |
|
|
testJob = 0
|
217 |
|
|
test = 0
|
218 |
|
|
|
219 |
|
|
# Read new values from the command line
|
220 |
|
|
for opt, arg in opts:
|
221 |
|
|
if opt == "--help":
|
222 |
|
|
print usage
|
223 |
|
|
sys.exit(0)
|
224 |
|
|
if opt == "--cmsDataset":
|
225 |
|
|
cmsDataset = arg
|
226 |
|
|
if opt == "--mitDataset":
|
227 |
|
|
mitDataset = arg
|
228 |
|
|
if opt == "--cmssw":
|
229 |
|
|
cmssw = arg
|
230 |
|
|
if opt == "--mitCfg":
|
231 |
|
|
mitCfg = arg
|
232 |
|
|
if opt == "--version":
|
233 |
|
|
version = arg
|
234 |
|
|
if opt == "--dbs":
|
235 |
|
|
dbs = arg
|
236 |
|
|
if opt == "--sched":
|
237 |
|
|
sched = arg
|
238 |
|
|
if opt == "--blacklist":
|
239 |
|
|
blacklist = arg
|
240 |
|
|
if opt == "--nSubmit":
|
241 |
|
|
nSubmit = arg
|
242 |
|
|
if opt == "--skipEvents":
|
243 |
|
|
skpEvts = arg
|
244 |
|
|
if opt == "--complete":
|
245 |
|
|
complete = 1
|
246 |
|
|
if opt == "--noTestJob":
|
247 |
|
|
noTestJob = 1
|
248 |
|
|
testJob = 0
|
249 |
|
|
if opt == "--testJob":
|
250 |
|
|
testJob = 1
|
251 |
|
|
noTestJob = 0
|
252 |
|
|
if opt == "--test":
|
253 |
|
|
test = 1
|
254 |
|
|
|
255 |
|
|
# Make sure we have the right 'database' and the right config file
|
256 |
|
|
database = 'Productions'
|
257 |
paus |
1.3 |
cmsswPy = cmssw + '_' + crabId + '.py'
|
258 |
paus |
1.2 |
if cmssw != 'cmssw':
|
259 |
|
|
database += '.' + cmssw
|
260 |
|
|
|
261 |
|
|
# Deal with obvious problems
|
262 |
|
|
if cmsDataset == None and mitDataset == None:
|
263 |
|
|
cmd = "--cmsDataset | --mitDataset options not provided. One of them is required."
|
264 |
|
|
raise RuntimeError, cmd
|
265 |
|
|
|
266 |
paus |
1.3 |
crabFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'crab.cfg'
|
267 |
paus |
1.2 |
if not os.path.exists(crabFile):
|
268 |
|
|
cmd = "Crab file not found: %s" % crabFile
|
269 |
|
|
raise RuntimeError, cmd
|
270 |
paus |
1.3 |
cmsswFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + cmssw + '.py'
|
271 |
paus |
1.2 |
if not os.path.exists(cmsswFile):
|
272 |
|
|
cmd = "Cmssw file not found: %s" % cmsswFile
|
273 |
|
|
cmd = " XXXX ERROR no valid configuration found XXXX"
|
274 |
|
|
raise RuntimeError, cmd
|
275 |
|
|
|
276 |
paus |
1.3 |
|
277 |
|
|
# Prepare the ce/se translator
|
278 |
|
|
translator = translator.Translator(os.environ['MIT_PROD_DIR']+'/'+mitCfg+'/'+version+'/ceTable',
|
279 |
|
|
os.environ['MIT_PROD_DIR']+'/'+mitCfg+'/'+version+'/seTable')
|
280 |
|
|
|
281 |
paus |
1.2 |
# Resolve the other mitCfg parameters from the configuration file
|
282 |
paus |
1.3 |
cmd = 'cat ' + os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + database
|
283 |
paus |
1.2 |
|
284 |
|
|
join = 0
|
285 |
|
|
fullLine = ""
|
286 |
|
|
bSlash = "\\";
|
287 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
288 |
|
|
line = line[:-1]
|
289 |
|
|
# get ride of empty or commented lines
|
290 |
|
|
if line == '' or line[0] == '#':
|
291 |
|
|
continue
|
292 |
|
|
|
293 |
|
|
# join lines
|
294 |
|
|
if join == 1:
|
295 |
|
|
fullLine += line
|
296 |
|
|
else:
|
297 |
|
|
fullLine = line
|
298 |
|
|
|
299 |
|
|
# determine if finished or more is coming
|
300 |
|
|
if fullLine[-1] == bSlash:
|
301 |
|
|
join = 1
|
302 |
|
|
fullLine = fullLine[:-1]
|
303 |
|
|
else:
|
304 |
|
|
join = 0
|
305 |
|
|
# test whether there is a directory
|
306 |
|
|
names = fullLine.split() # splitting every blank
|
307 |
|
|
if names[0] == cmsDataset or names[1] == mitDataset:
|
308 |
|
|
cmsDataset = names[0] # CMS name of the dataset
|
309 |
|
|
mitDataset = names[1] # equivalent MIT name of the dataset
|
310 |
|
|
nevents = int(names[2]) # number of events to be used in the production
|
311 |
|
|
if names[4] != "-":
|
312 |
|
|
localPath = names[4]
|
313 |
|
|
print "\n Sample info from database %s for CMSSW config %s\n %s"\
|
314 |
|
|
%(database,cmsswPy,fullLine)
|
315 |
|
|
if len(names) == 6:
|
316 |
|
|
dbs = names[5]
|
317 |
|
|
dbs = 'https://cmsdbsprod.cern.ch:8443/cms_dbs_' + dbs + '/servlet/DBSServlet'
|
318 |
|
|
print ' dbs: ' + dbs + '\n'
|
319 |
|
|
else:
|
320 |
|
|
print ''
|
321 |
|
|
|
322 |
|
|
if mitDataset == None or cmsDataset == None:
|
323 |
|
|
print "ERROR - dataset not defined."
|
324 |
|
|
sys.exit(1)
|
325 |
|
|
|
326 |
|
|
# Prepare file based processing input
|
327 |
paus |
1.3 |
runFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'run.sh'
|
328 |
paus |
1.2 |
if not os.path.exists(runFile):
|
329 |
|
|
cmd = "Run file not found: %s" % runFile
|
330 |
|
|
raise RuntimeError, cmd
|
331 |
|
|
cmd = 'cp ' + runFile + ' ./'
|
332 |
|
|
os.system(cmd)
|
333 |
paus |
1.3 |
writeCfgFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'writeCfg.py'
|
334 |
|
|
cmd = 'cp ' + writeCfgFile + ' ./'
|
335 |
|
|
os.system(cmd)
|
336 |
paus |
1.2 |
|
337 |
|
|
lfnFile = mitCfg + '/' + version + '/' + mitDataset + '.lfns'
|
338 |
|
|
if os.path.exists(lfnFile):
|
339 |
|
|
cmd = "Lfn file found: %s. This means someone already worked on this dataset." % lfnFile
|
340 |
|
|
#### raise RuntimeError, cmd
|
341 |
|
|
else:
|
342 |
|
|
cmd = 'input.py --option=lfn --dataset=' + cmsDataset + ' > ' + lfnFile
|
343 |
|
|
print ' Input: ' + cmd
|
344 |
|
|
os.system(cmd)
|
345 |
|
|
|
346 |
|
|
# Create the corresponding crab task
|
347 |
|
|
crabTask = task.Task(crabId,cmsDataset,mitCfg,version)
|
348 |
|
|
crabTask.storagePath = findStoragePath(mitCfg,version,mitDataset)
|
349 |
|
|
crabTask.loadAllLfns(lfnFile)
|
350 |
|
|
crabTask.loadCompletedLfns()
|
351 |
|
|
crabTask.createMissingLfns(lfnFile,lfnFile + '_' + crabTask.tag)
|
352 |
paus |
1.4 |
if crabTask.nLfnMissing == 0:
|
353 |
|
|
print ' All requested LFNs are available. EXIT now.'
|
354 |
|
|
sys.exit()
|
355 |
|
|
|
356 |
paus |
1.2 |
crabTask.createSubTasks(lfnFile + '_' + crabTask.tag)
|
357 |
|
|
cmd = 'cp ' + lfnFile + '_' + crabTask.tag + '_*' + ' ./'
|
358 |
|
|
os.system(cmd)
|
359 |
|
|
|
360 |
|
|
nevents = len(crabTask.lfns)
|
361 |
|
|
|
362 |
|
|
# Say what we do now
|
363 |
|
|
print ' Preparing dataset: ' + cmsDataset + ' [MIT: ' + mitDataset + ' with ' + str(nevents) + \
|
364 |
|
|
' total jobs(=lfns), nEvtsTotal: %d]' % crabTask.nTotalEvts
|
365 |
|
|
|
366 |
|
|
# --------------------------------------------------------------------------------------------------
|
367 |
|
|
# Prepare the config files
|
368 |
|
|
# --------------------------------------------------------------------------------------------------
|
369 |
|
|
# Cleaning up
|
370 |
paus |
1.3 |
cmd = "rm -f crab_" + crabTask.tag + ".cfg crab_" + crabTask.tag + ".cfg-Template " + cmsswPy
|
371 |
paus |
1.2 |
os.system(cmd)
|
372 |
|
|
|
373 |
|
|
# Parse template input and write the crab configuration file
|
374 |
|
|
fileInput = open(crabFile,'r')
|
375 |
paus |
1.3 |
fileOutput = open("crab_" + crabTask.tag + ".cfg-Template",'w')
|
376 |
paus |
1.2 |
line = fileInput.readline()
|
377 |
|
|
while (line != ''):
|
378 |
|
|
if line[0] != '#':
|
379 |
|
|
line = searchReplace(line,mitCfg,version,mitDataset, \
|
380 |
|
|
cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
|
381 |
|
|
fileOutput.write(line)
|
382 |
|
|
line = fileInput.readline()
|
383 |
|
|
fileInput .close()
|
384 |
|
|
fileOutput.close()
|
385 |
|
|
|
386 |
|
|
# Parse template input and write the crab configuration file
|
387 |
|
|
fileInput = open(cmsswFile,'r')
|
388 |
|
|
fileOutput = open(cmsswPy,'w')
|
389 |
|
|
line = fileInput.readline()
|
390 |
|
|
while (line != ''):
|
391 |
|
|
if line[0] != '#':
|
392 |
|
|
line = searchReplace(line,mitCfg,version,mitDataset, \
|
393 |
|
|
cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
|
394 |
|
|
fileOutput.write(line)
|
395 |
|
|
line = fileInput.readline()
|
396 |
|
|
fileInput .close()
|
397 |
|
|
fileOutput.close()
|
398 |
|
|
|
399 |
|
|
# --------------------------------------------------------------------------------------------------
|
400 |
|
|
# Job creation and submission
|
401 |
|
|
# --------------------------------------------------------------------------------------------------
|
402 |
|
|
print '\n Using CMSSW version: ' + os.environ['CMSSW_VERSION']
|
403 |
|
|
print ' Using CRAB version: ' + os.environ['CRAB_VERS'] + '\n'
|
404 |
|
|
|
405 |
|
|
pattern1 = 'working directory'
|
406 |
|
|
pattern2 = 'job\(s\) can run on'
|
407 |
|
|
|
408 |
|
|
# Go through the crabTask and submit each subtask to the grid
|
409 |
|
|
for subTask in crabTask.subTasks:
|
410 |
|
|
|
411 |
|
|
tag = crabTask.tag + '_' + subTask.tag()
|
412 |
|
|
print ' Working on subtask: ' + tag
|
413 |
|
|
|
414 |
|
|
nJobsTotal = 0
|
415 |
|
|
crabIdCheck = ''
|
416 |
|
|
|
417 |
|
|
# adjust crab config
|
418 |
paus |
1.3 |
fileInput = open("crab_" + crabTask.tag + ".cfg-Template",'r')
|
419 |
|
|
fileOutput = open("crab_" + crabTask.tag + ".cfg",'w')
|
420 |
paus |
1.2 |
line = fileInput.readline()
|
421 |
|
|
while (line != ''):
|
422 |
|
|
if line[0] != '#':
|
423 |
|
|
line = adjustCfg(line,subTask.nSubTaskLfn,tag)
|
424 |
|
|
fileOutput.write(line)
|
425 |
|
|
line = fileInput.readline()
|
426 |
|
|
fileInput .close()
|
427 |
|
|
fileOutput.close()
|
428 |
|
|
|
429 |
|
|
# ----------------------------------------------------------------------------------------------
|
430 |
|
|
# Deal with storage element area
|
431 |
|
|
# ----------------------------------------------------------------------------------------------
|
432 |
|
|
# find the forseen storage place
|
433 |
paus |
1.3 |
cmd = 'grep ^storage_element crab_' + crabTask.tag + '.cfg-Template'
|
434 |
paus |
1.2 |
for file in os.popen(cmd).readlines(): # run command
|
435 |
|
|
line = file[:-1] # strip '\n'
|
436 |
|
|
# decode the storage element name
|
437 |
|
|
names = line.split("=") # splitting every '='
|
438 |
|
|
storageEle = names.pop()
|
439 |
|
|
storageEle = re.sub("\s", "",storageEle)
|
440 |
paus |
1.3 |
cmd = 'grep ^storage_path crab_' + crabTask.tag + '.cfg-Template'
|
441 |
paus |
1.2 |
for file in os.popen(cmd).readlines(): # run command
|
442 |
|
|
line = file[:-1] # strip '\n'
|
443 |
|
|
# decode the storage directory name
|
444 |
|
|
names = line.split("=") # splitting every '='
|
445 |
|
|
names = names[1:]
|
446 |
|
|
storagePath = "=".join(names)
|
447 |
|
|
storagePath = re.sub("\s", "",storagePath)
|
448 |
paus |
1.3 |
cmd = 'grep ^user_remote_dir crab_' + crabTask.tag + '.cfg-Template'
|
449 |
paus |
1.2 |
for file in os.popen(cmd).readlines(): # run command
|
450 |
|
|
line = file[:-1] # strip '\n'
|
451 |
|
|
# decode the storage directory name
|
452 |
|
|
names = line.split(" ") # splitting every '='
|
453 |
|
|
storagePath += names[-1]
|
454 |
|
|
storageUrl = 'srm://' + storageEle + ':8443' + storagePath
|
455 |
|
|
|
456 |
|
|
storagePath = adjustCfg(storagePath,subTask.nSubTaskLfn,tag)
|
457 |
|
|
storageUrl = adjustCfg(storageUrl, subTask.nSubTaskLfn,tag)
|
458 |
|
|
|
459 |
|
|
print ' StorageUrl: ' + storageUrl
|
460 |
|
|
|
461 |
|
|
# Create storage area
|
462 |
|
|
if test == 0: # for testing we do not create the directories
|
463 |
|
|
if (storageEle == 'srm.cern.ch'):
|
464 |
|
|
createDirCern(storagePath)
|
465 |
|
|
else:
|
466 |
|
|
createDirGeneral(storageEle,storagePath)
|
467 |
|
|
|
468 |
|
|
# cmd = "crab -create -debug 3 -USER.ui_working_dir=" + tag + " | tee forDaniele "
|
469 |
paus |
1.3 |
cmd = "crab -create -cfg crab_" + crabTask.tag + ".cfg -USER.ui_working_dir=" + tag
|
470 |
paus |
1.2 |
print ' -> ' + cmd
|
471 |
|
|
if test != 0:
|
472 |
|
|
cmd = 'echo ' + cmd
|
473 |
|
|
print ' ' + cmd
|
474 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
475 |
|
|
line = line[:-1] # strip '\n'
|
476 |
|
|
print ' CRAB: ' + line
|
477 |
|
|
if re.search(pattern1,line):
|
478 |
|
|
f = line.split("/")
|
479 |
|
|
crabIdCheck = f[-2]
|
480 |
|
|
if re.search(pattern2,line):
|
481 |
|
|
f = line.split(" ")
|
482 |
|
|
nJobsTotal = f[1]
|
483 |
|
|
if nJobsTotal == '':
|
484 |
|
|
nJobsTotal = f[2]
|
485 |
paus |
1.3 |
# report
|
486 |
paus |
1.2 |
print ' --> %s jobs created (%s).\n'%(nJobsTotal,tag)
|
487 |
paus |
1.3 |
# and cleanup the temporary file for the subtask
|
488 |
|
|
cmd = "rm -f " + mitDataset + ".lfns_" + tag
|
489 |
|
|
os.system(cmd)
|
490 |
|
|
|
491 |
paus |
1.2 |
# adjust arguments
|
492 |
|
|
cmd = 'input.py --db=' + lfnFile + '_' + tag + ' --option=xml --dataset=' + cmsDataset + \
|
493 |
|
|
' > ' + tag + '/share/arguments.xml'
|
494 |
|
|
print ' update arguments: ' + cmd
|
495 |
|
|
if test == 0:
|
496 |
|
|
os.system(cmd)
|
497 |
|
|
|
498 |
|
|
# loop through the file and determine the submission parameters
|
499 |
|
|
block = ''
|
500 |
|
|
blocks = []
|
501 |
|
|
idx = 0
|
502 |
|
|
minIdxs = []
|
503 |
|
|
maxIdxs = []
|
504 |
|
|
|
505 |
|
|
fileInput = open(lfnFile + '_' + tag,'r')
|
506 |
|
|
line = fileInput.readline()
|
507 |
|
|
while (line != ''):
|
508 |
|
|
line = line[:-1]
|
509 |
|
|
if line[0] != '#':
|
510 |
|
|
idx += 1
|
511 |
|
|
f = line.split()
|
512 |
|
|
block = f[0]
|
513 |
|
|
lfn = f[1]
|
514 |
|
|
nEvents = f[2]
|
515 |
|
|
if len(blocks) == 0 or block != blocks[-1]:
|
516 |
|
|
# new block found
|
517 |
|
|
blocks .append(block)
|
518 |
|
|
minIdxs.append(idx)
|
519 |
|
|
maxIdxs.append(idx)
|
520 |
|
|
else:
|
521 |
|
|
maxIdxs[-1] = idx
|
522 |
|
|
# keep the reading going
|
523 |
|
|
line = fileInput.readline()
|
524 |
|
|
fileInput .close()
|
525 |
|
|
|
526 |
|
|
# merge blocks together if they use the same sites
|
527 |
|
|
lastSites = ''
|
528 |
|
|
idx = 0
|
529 |
|
|
mergedIdx = 0
|
530 |
|
|
mergedBlocks = []
|
531 |
|
|
mergedSites = []
|
532 |
|
|
mergedMinIdxs = []
|
533 |
|
|
mergedMaxIdxs = []
|
534 |
|
|
print '\n Show the unmerged blocks:'
|
535 |
|
|
for block in blocks:
|
536 |
|
|
cmd = "sites.py --block=" + block
|
537 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
538 |
|
|
line = line[:-1]
|
539 |
|
|
sites = line
|
540 |
|
|
print ' Block ' + block + ' process: %d to %d'%(minIdxs[idx],maxIdxs[idx]) + \
|
541 |
|
|
' at\n > ' + sites
|
542 |
|
|
# block with different sites found
|
543 |
|
|
if sites != lastSites:
|
544 |
|
|
lastSites = sites
|
545 |
|
|
mergedSites .append(sites)
|
546 |
|
|
mergedBlocks .append(blocks[idx])
|
547 |
|
|
mergedMinIdxs.append(minIdxs[idx])
|
548 |
|
|
mergedMaxIdxs.append(maxIdxs[idx])
|
549 |
|
|
mergedIdx =+ 1
|
550 |
|
|
else:
|
551 |
|
|
mergedMaxIdxs[mergedIdx-1] = maxIdxs[idx]
|
552 |
|
|
# last action in the loop: increment the unmerged blocks
|
553 |
|
|
idx += 1
|
554 |
|
|
|
555 |
|
|
# Show already what we will do
|
556 |
|
|
idx = 0
|
557 |
|
|
print '\n Show the merged blocks:'
|
558 |
|
|
for block in mergedBlocks:
|
559 |
|
|
print ' Merged Block ' + block + ' process: %d to %d'\
|
560 |
|
|
%(mergedMinIdxs[idx],mergedMaxIdxs[idx]) + \
|
561 |
|
|
' at\n > ' + mergedSites[idx]
|
562 |
|
|
# last action in the loop: increment the merged blocks
|
563 |
|
|
idx += 1
|
564 |
|
|
|
565 |
|
|
# perfrom the submission block by block (using the merged blocks of course)
|
566 |
|
|
nSubmission = len(mergedBlocks)
|
567 |
|
|
idx = 0
|
568 |
|
|
print '\n Submit the merged blocks:'
|
569 |
|
|
for block in mergedBlocks:
|
570 |
|
|
print ' Merged Block ' + block + ' process: %d to %d'\
|
571 |
|
|
%(mergedMinIdxs[idx],mergedMaxIdxs[idx]) + \
|
572 |
|
|
' at\n > ' + mergedSites[idx]
|
573 |
|
|
|
574 |
|
|
nSubmit = '%d-%d'%(mergedMinIdxs[idx],mergedMaxIdxs[idx])
|
575 |
|
|
if mergedMinIdxs[idx] == mergedMaxIdxs[idx]:
|
576 |
|
|
nSubmit = '%d,%d'%(mergedMinIdxs[idx],100000000)
|
577 |
paus |
1.3 |
translatedSites = translator.translateSes(mergedSites[idx])
|
578 |
|
|
cmd = 'crab -submit %s -continue %s -GRID.ce_white_list=%s'%(nSubmit,tag,translatedSites)
|
579 |
paus |
1.2 |
print ' ' + cmd
|
580 |
paus |
1.3 |
status = os.system(cmd)
|
581 |
|
|
while status != 0:
|
582 |
|
|
print ' Submission failed (%s) --> retry'%(cmd)
|
583 |
|
|
status = os.system(cmd)
|
584 |
|
|
|
585 |
paus |
1.2 |
# last action in the loop: increment the merged blocks
|
586 |
|
|
idx += 1
|
587 |
|
|
|
588 |
|
|
print ' Number of blocks submitted: %d' % nSubmission
|
589 |
|
|
|
590 |
paus |
1.3 |
|
591 |
|
|
# and cleanup the temporary file for the task
|
592 |
|
|
cmd = "rm -f crab_" + crabTask.tag + ".cfg crab_" + crabTask.tag + ".cfg-Template " \
|
593 |
|
|
+ cmsswPy + ' ' + cmsswPy + 'c'
|
594 |
|
|
os.system(cmd)
|
595 |
|
|
|
596 |
paus |
1.2 |
print ' Done... keep watching it...'
|
597 |
|
|
sys.exit(0)
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
## if nSubmit != -1:
|
601 |
|
|
## cmd = 'crab -continue %s -submit %s'%(crabId,nSubmit)
|
602 |
|
|
## print 'SUBMIT.PY: ' + cmd
|
603 |
|
|
## status = os.system(cmd)
|
604 |
|
|
## if status == 0:
|
605 |
|
|
## print ' --> job submitted\n'
|
606 |
|
|
## else:
|
607 |
|
|
## leftOver = int(nJobsTotal)
|
608 |
|
|
## nSubmitted = 0
|
609 |
|
|
## nSubBatch = 80
|
610 |
|
|
## while (nSubmitted < int(nJobsTotal)):
|
611 |
|
|
## if leftOver < nSubBatch:
|
612 |
|
|
## nSubBatch = leftOver
|
613 |
|
|
##
|
614 |
|
|
## cmd = 'crab -continue %s -submit %d'%(crabId,nSubBatch)
|
615 |
|
|
## print 'SUBMIT.PY: ' + cmd
|
616 |
|
|
## status = os.system(cmd)
|
617 |
|
|
## if status == 0:
|
618 |
|
|
## print ' --> %d job submitted\n'%(nSubBatch)
|
619 |
|
|
## leftOver = leftOver - nSubBatch
|
620 |
|
|
## nSubmitted = nSubmitted + nSubBatch
|
621 |
|
|
##
|
622 |
|
|
## #print 'SUBMIT.PY: ' + cmd
|
623 |
|
|
## #status = os.system(cmd)
|
624 |
|
|
## #if status == 0:
|
625 |
|
|
## # print ' --> job submitted\n'
|
626 |
|
|
##
|
627 |
|
|
|
628 |
|
|
# --------------------------------------------------------------------------------------------------
|
629 |
|
|
# Run a test job to test the configuration and measure the expected output size
|
630 |
|
|
# --------------------------------------------------------------------------------------------------
|
631 |
|
|
if noTestJob == 0:
|
632 |
|
|
#-----------------------------------------------------------------------------------------------
|
633 |
|
|
# use a specific file for test
|
634 |
|
|
#-----------------------------------------------------------------------------------------------
|
635 |
|
|
# first check whether we already have a local file
|
636 |
|
|
print '\n Try to find an existing local file using "find"'
|
637 |
|
|
f = cmsDataset.split("/")
|
638 |
|
|
name = f[1]
|
639 |
|
|
vers = f[2]
|
640 |
|
|
tier = f[3]
|
641 |
|
|
file = ""
|
642 |
|
|
lfn = ""
|
643 |
|
|
cmd = 'find ./store/ -name ' + name
|
644 |
|
|
print 'Searching: ' + cmd
|
645 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
646 |
|
|
file = line[:-1] # strip '\n'
|
647 |
|
|
|
648 |
|
|
# looks like there could be a file we found a directory, confirm
|
649 |
|
|
if file != "":
|
650 |
|
|
print '\n We have a directory ' + file + ' .. confirming'
|
651 |
|
|
file = file + '/' + tier
|
652 |
|
|
cmd = 'find ' + file + ' -name \*.root'
|
653 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
654 |
|
|
if line.find(vers):
|
655 |
|
|
file = line[:-1] # strip '\n'
|
656 |
|
|
lfn = file[1:]
|
657 |
|
|
|
658 |
|
|
if os.path.exists(file):
|
659 |
|
|
print ' moving on with locally found file: \n' + ' ' + file
|
660 |
|
|
else:
|
661 |
|
|
print ' no local file found'
|
662 |
|
|
file = ""
|
663 |
|
|
lfn = ""
|
664 |
|
|
|
665 |
|
|
# now try to see whether we can find a file to download
|
666 |
|
|
if file == "":
|
667 |
|
|
cmd = './bin/findLfn.py --input=' + cmsDataset + ' | grep /store/'
|
668 |
|
|
print '\n Find an LFN to download: ' + cmd
|
669 |
|
|
for line in os.popen(cmd).readlines(): # run command
|
670 |
|
|
if line.find("/store") != -1:
|
671 |
|
|
lfn = line[:-1] # strip '\n'
|
672 |
|
|
break
|
673 |
|
|
if lfn == "":
|
674 |
|
|
print "\n WARNING: No file found, continue assuming it is a simulation job.\n\n"
|
675 |
|
|
else:
|
676 |
|
|
print ' --> LFN: ' + lfn
|
677 |
|
|
file = '.' + lfn
|
678 |
|
|
|
679 |
|
|
if os.path.exists(file):
|
680 |
|
|
print ' --> File already exists: ' + file
|
681 |
|
|
else:
|
682 |
|
|
cmd = './bin/downloadLfn.py ' + lfn
|
683 |
|
|
print ' --> downloading: ' + cmd
|
684 |
|
|
status = os.system(cmd)
|
685 |
|
|
if status != 0:
|
686 |
|
|
print ' ERROR - failed to copy LFN. EXIT now!'
|
687 |
|
|
sys.exit(1)
|
688 |
|
|
|
689 |
|
|
# Parse template input and adjust the input file to the newly copied lfn
|
690 |
|
|
fileInput = open(cmsswPy,'r')
|
691 |
|
|
fileOutput = open("test-"+cmsswPy,'w')
|
692 |
|
|
line = fileInput.readline()
|
693 |
|
|
while (line != ''):
|
694 |
|
|
if line.find("file:") != -1:
|
695 |
|
|
line = '"file:' + lfn[1:] + '"\n'
|
696 |
|
|
fileOutput.write(line)
|
697 |
|
|
line = fileInput.readline()
|
698 |
|
|
fileInput .close()
|
699 |
|
|
fileOutput.close()
|
700 |
|
|
|
701 |
|
|
# Setting the number of events (hard coded in the file so far)
|
702 |
|
|
nTryEvts = 1000.
|
703 |
|
|
|
704 |
|
|
print '\n --> Please wait, running a test job now! Should be short (trying %.0f'%nTryEvts + \
|
705 |
|
|
' evts). Check log: cmssw.log'
|
706 |
|
|
cmd = 'rm -f cmssw.log; /usr/bin/time --format "%e %U %S" cmsRun test-' + cmsswPy + \
|
707 |
|
|
' >& cmssw.log'
|
708 |
|
|
print ' CMD: ' + cmd
|
709 |
|
|
status = os.system(cmd)
|
710 |
|
|
|
711 |
|
|
cmd = 'tail -1 cmssw.log'
|
712 |
|
|
for file in os.popen(cmd).readlines(): # run command
|
713 |
|
|
line = file[:-1] # strip '\n'
|
714 |
|
|
f = line.split() # splitting every blank
|
715 |
|
|
rtime = float(f[0]) # wall clock time
|
716 |
|
|
utime = float(f[1]) # user time
|
717 |
|
|
stime = float(f[2]) # system time
|
718 |
|
|
|
719 |
|
|
nEvtsTest = 1000
|
720 |
|
|
cmd = 'grep \'Begin processing\' cmssw.log | tail -1'
|
721 |
|
|
for file in os.popen(cmd).readlines(): # run command
|
722 |
|
|
line = file[:-1] # strip '\n'
|
723 |
|
|
# test whether there is a directory
|
724 |
|
|
f = line.split() # splitting every blank
|
725 |
|
|
nEvtsTest = f[3] # this is the number of records processed
|
726 |
|
|
nEvtsTest = int(nEvtsTest[:-2]) # strip 'th'
|
727 |
|
|
cmd = 'ls -s ' + mitDataset + '*.root'
|
728 |
|
|
size = 0
|
729 |
|
|
for file in os.popen(cmd).readlines(): # run command
|
730 |
|
|
line = file[:-1] # strip '\n'
|
731 |
|
|
f = line.split() # splitting every blank
|
732 |
|
|
size += int(f[0])/1000. # size in MB
|
733 |
|
|
|
734 |
|
|
cmd = 'tail -1 cmssw.log'
|
735 |
|
|
for file in os.popen(cmd).readlines(): # run command
|
736 |
|
|
line = file[:-1] # strip '\n'
|
737 |
|
|
# get total, user and system times
|
738 |
|
|
names = line.split() # splitting every blank
|
739 |
|
|
|
740 |
|
|
if nEvtsTest != nTryEvts:
|
741 |
|
|
print ' WARNING - Instead of %f did %d'%(nTryEvts,nEvtsTest)
|
742 |
|
|
|
743 |
|
|
print ' '
|
744 |
|
|
print ' Number of test events produced: %d'%nEvtsTest
|
745 |
|
|
print ' File size for all events: %.2f MB'%size
|
746 |
|
|
print ' Processing time for all events: %.2f secs (u: %.2f s: %.2f)'%(rtime,utime,stime)
|
747 |
|
|
print ' '
|
748 |
|
|
print ' --> 1 event == %.2f secs'%(rtime/nEvtsTest)
|
749 |
|
|
print ' --> 1.00 GB == %d events'%(nEvtsTest/size*1024.)
|
750 |
|
|
print ' --> %.2f GB == %d events'%(nevents/(nEvtsTest/size*1024.),nevents)
|
751 |
|
|
print ' '
|
752 |
|
|
|
753 |
|
|
if testJob == 1:
|
754 |
|
|
print '\n Test job finished, stopping now.\n'
|
755 |
|
|
sys.exit(0)
|
756 |
|
|
|
757 |
|
|
|
758 |
|
|
## # are we just completing an existing production? and is there something to complete?
|
759 |
|
|
## if complete == 1:
|
760 |
|
|
## f = storagePath.split('=')
|
761 |
|
|
## rfDir = f[-1]
|
762 |
|
|
## #cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,rfDir)
|
763 |
|
|
## cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,storageUrl)
|
764 |
|
|
## #print ' CMD: ' + cmd
|
765 |
|
|
## for line in os.popen(cmd).readlines(): # run command
|
766 |
|
|
## line = line[:-1] # strip '\n'
|
767 |
|
|
## f = line.split(':')
|
768 |
|
|
## nSubmit = f[1].strip()
|
769 |
|
|
## f = nSubmit.split(',')
|
770 |
|
|
## if len(f) == 0 or nSubmit == '':
|
771 |
|
|
## print ' No more jobs left it seems, nSubmit=' + nSubmit
|
772 |
|
|
## cmd = 'rm -rf ' + crabId
|
773 |
|
|
## print ' Cleanup: ' + cmd + '\n\n'
|
774 |
|
|
## status = os.system(cmd)
|
775 |
|
|
## sys.exit(0)
|
776 |
|
|
## elif len(f) == 1:
|
777 |
|
|
## nInvalid = str(int(nJobsTotal) + 1000)
|
778 |
|
|
## print ' One more jobs left, careful, adjusted, nSubmit=' + nSubmit
|
779 |
|
|
## nSubmit = nSubmit + ',' + nInvalid
|
780 |
|
|
##
|
781 |
|
|
## ### nSubmit = ",".join(f[:-1])
|
782 |
|
|
## print ' Missing jobs are: ' + nSubmit
|
783 |
|
|
##
|
784 |
|
|
## sys.exit(0)
|