File indexing completed on 2023-03-17 11:04:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 from __future__ import print_function
0042 import argparse
0043 import sys
0044 import os
0045 import subprocess
0046 import re
0047
0048
0049
0050 def uint(string):
0051 """Unsigned int type"""
0052 value = int(string)
0053 if value < 0:
0054 msg = '{0} is negative'.format(string)
0055 raise argparse.ArgumentTypeError(msg)
0056 return value
0057
0058
0059
0060 def adjust_pset(cmsrunfilename, savefilename, par_list):
0061 """Takes the cmsRun filem, removes all occurences of runMode, jobs,
0062 maxJobs and integrationList parameters in the process.generator
0063 part.
0064 The the parameters in par_list are set instead and saved.
0065 """
0066
0067 with open(cmsrunfilename, 'r') as readfile:
0068 parsestring = readfile.read()
0069
0070
0071 begin_gen_step = parsestring.find('(', parsestring.find('process.generator'))
0072
0073
0074 end_gen_step = begin_gen_step
0075 bracket_counter = 1
0076 for position in range(begin_gen_step+1, len(parsestring)):
0077 if parsestring[position] == '(':
0078 bracket_counter += 1
0079 if parsestring[position] == ')':
0080 bracket_counter -= 1
0081 if not bracket_counter:
0082 end_gen_step = position
0083 break
0084
0085
0086 gen_string = parsestring[begin_gen_step+1:end_gen_step]
0087
0088
0089 gen_string = re.sub(r',\s*runModeList\s*=\s*cms.untracked.string\((.*?)\)', '', gen_string)
0090 gen_string = re.sub(r',\s*jobs\s*=\s*cms.untracked.int32\((.*?)\)', '', gen_string)
0091 gen_string = re.sub(r',\s*integrationList\s*=\s*cms.untracked.string\((.*?)\)', '', gen_string)
0092 gen_string = re.sub(r',\s*maxJobs\s*=\s*cms.untracked.uint32\((.*?)\)', '', gen_string)
0093 gen_string = re.sub(r',\s*seed\s*=\s*cms.untracked.int32\((.*?)\)', '', gen_string)
0094
0095
0096
0097 with open(savefilename,'w') as savefile:
0098 savefile.write(parsestring[:begin_gen_step+1])
0099 savefile.write(gen_string)
0100 for item in par_list:
0101 savefile.write(',\n')
0102 savefile.write(item)
0103 savefile.write(parsestring[end_gen_step:])
0104
0105
0106
0107 def cleanupandexit(filelist):
0108 """Delete the files in filelist and exit"""
0109 for filename in filelist:
0110 os.remove(filename)
0111 sys.exit(0)
0112
0113
0114
0115
0116
0117
0118
0119
0120 parser = argparse.ArgumentParser()
0121
0122 parser.add_argument('cmsRunfile', help='filename of the cmsRun configuration')
0123 parser.add_argument('-b', '--build', help='set the number of build jobs', type=int, choices=range(0,11), default=0)
0124 parser.add_argument('-i', '--integrate', help='set the maximal number of integration jobs', type=uint, default=0)
0125 parser.add_argument('-r', '--run', help='set the number of run jobs', type=int, choices=range(0,11), default=0)
0126 parser.add_argument('--nointegration', help='build -i integration jobs without actually integrating', action='store_true')
0127 parser.add_argument('--keepfiles', help='don\'t delete temporary files', action='store_true')
0128 parser.add_argument('--stoprun', help='stop after creating the cmsRun files for the run step', action='store_true')
0129 parser.add_argument('--resumerun', help='use existing \'temporary\' files for the run step', action='store_true')
0130 parser.add_argument('-l', '--log', help='write the output of each process in a separate log file', action='store_true')
0131
0132 args = parser.parse_args()
0133
0134
0135 cleanupfiles = []
0136
0137
0138 template_name = args.cmsRunfile.replace('.', '_')
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 if args.build != 0:
0153
0154 parameters = ['runModeList = cms.untracked.string(\'build\')']
0155 parameters.append('jobs = cms.untracked.int32(' + str(args.build) + ')')
0156 if args.integrate != 0:
0157 parameters.append('maxJobs = cms.untracked.uint32(' + str(args.integrate) + ')')
0158
0159 build_name = template_name + '_build.py'
0160 adjust_pset(args.cmsRunfile, build_name, parameters)
0161
0162 cleanupfiles.append(build_name)
0163
0164
0165 print('Setting up {0} build jobs.'.format(str(args.build)))
0166 print('Setting up a maximum of {0} integration jobs.'.format(str(args.integrate)))
0167 print('Calling\t\'cmsRun ' + build_name + '\'')
0168
0169 if args.log:
0170 print('Writing ouput to log file: ' + build_name[:-2] + 'log')
0171 with open(build_name[:-2] + 'log', 'w') as build_log:
0172 process = subprocess.Popen(['cmsRun', build_name], stdout=build_log, stderr=subprocess.STDOUT)
0173 else:
0174 process = subprocess.Popen(['cmsRun ' + build_name], shell=True)
0175 process.wait()
0176
0177 print('--------------------')
0178 print('Build step finished.')
0179 print('--------------------')
0180
0181
0182
0183
0184
0185
0186 if args.nointegration:
0187 print('--nointegration: Run will be stopped here.')
0188 cleanupandexit(cleanupfiles)
0189
0190 if args.integrate != 0:
0191
0192 actual_int_jobs = len([string for string in os.listdir('Herwig-scratch/Build') if re.match(r'integrationJob[0-9]+', string)])
0193
0194
0195 if actual_int_jobs > args.integrate:
0196 print('Actual number of integration jobs {0} exceeds \'--integrate {1}\'.'.format(actual_int_jobs, args.integrate))
0197 print('Integration will not be performed.')
0198 cleanupandexit(cleanupfiles)
0199
0200
0201 print('Found {0} integration jobs, a maxiumum of {1} was given.'.format(actual_int_jobs, args.integrate))
0202 print('Starting all jobs.')
0203 if not args.log:
0204 print('--- Output may be cluttered. (Try the option -l/--log) ---')
0205 processes = []
0206 for i in range(actual_int_jobs):
0207
0208 parameters = ['runModeList = cms.untracked.string(\'integrate\')']
0209 parameters.append('integrationList = cms.untracked.string(\'' + str(i) + '\')')
0210
0211 integration_name = template_name + '_integrate_' + str(i) + '.py'
0212 adjust_pset(args.cmsRunfile, integration_name, parameters)
0213
0214 cleanupfiles.append(integration_name)
0215
0216 print('Calling\t\'cmsRun ' + integration_name + '\'')
0217 if args.log:
0218 print('Writing ouput to log file: ' + integration_name[:-2] + 'log')
0219 with open(integration_name[:-2] + 'log', 'w') as integration_log:
0220 processes.append( subprocess.Popen(['cmsRun', integration_name], stdout=integration_log, stderr=subprocess.STDOUT) )
0221 else:
0222 processes.append( subprocess.Popen(['cmsRun', integration_name]) )
0223
0224
0225
0226 for process in processes:
0227 process.wait()
0228 print('--------------------------')
0229 print('Integration step finished.')
0230 print('--------------------------')
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 if args.stoprun and args.resumerun:
0262 print('--stoprun AND --resumerun are chosen: run step will be omitted.')
0263 cleanupandexit(cleanupfiles)
0264
0265 if args.run != 0:
0266
0267 print('Setting up {0} runs.'.format(args.run))
0268 if not args.log:
0269 print('--- Output may be cluttered. (Try the option -l/--log) ---')
0270 processes = []
0271 for i in range(args.run):
0272 run_name = template_name + '_run_' + str(i) + '.py'
0273
0274
0275 if not args.resumerun:
0276 parameters = ['runModeList = cms.untracked.string(\'run\')']
0277
0278 parameters.append('seed = cms.untracked.int32(' + str(i) + ')')
0279 adjust_pset(args.cmsRunfile, run_name, parameters)
0280
0281
0282 if not args.stoprun:
0283
0284 if not args.resumerun:
0285 cleanupfiles.append(run_name)
0286
0287 if not os.path.isfile(run_name):
0288 print('\'' + run_name + '\' not found. It will be skipped.')
0289 continue
0290
0291 print('Calling\t\'cmsRun ' + run_name + '\'')
0292 if args.log:
0293 print('Writing ouput to log file: ' + run_name[:-2] + 'log')
0294 with open(run_name[:-2] + 'log', 'w') as run_log:
0295 processes.append( subprocess.Popen(['cmsRun', run_name], stdout=run_log, stderr=subprocess.STDOUT) )
0296 else:
0297 processes.append( subprocess.Popen(['cmsRun', run_name]) )
0298
0299
0300
0301 for process in processes:
0302 process.wait()
0303 if args.stoprun:
0304 print('--stoprun: kept run files and stopped before calling cmsRun')
0305 print('------------------')
0306 print('Run step finished.')
0307 print('------------------')
0308
0309
0310
0311 if not args.keepfiles:
0312 cleanupandexit(cleanupfiles)