def submitToGrid(configFile = None, channel = None, sample = None, job = None, dbs_name = None, dbs_url = None, replFunction = None, replacements = "", type = None, outputFileNames = None, outputFilePath = None, submit = "yes"): # check that configFile, channel, sample and outputFilePath # parameters are defined and non-empty if configFile is None: raise ValueError("Undefined configFile Parameter !!") if channel is None: raise ValueError("Undefined channel Parameter !!") if sample is None: raise ValueError("Undefined sample Parameter !!") if job is None: raise ValueError("Undefined job Parameter !!") if dbs_name is None: raise ValueError("Undefined dbs_name Parameter !!") if dbs_url is None: raise ValueError("Undefined dbs_url Parameter !!") if outputFileNames is None: raise ValueError("Undefined outputFileNames Parameter !!") if outputFilePath is None: raise ValueError("Undefined outputFilePath Parameter !!") if type is None: raise ValueError("Undefined type Parameter !!") # in case outputFilePath parameter not terminated by "/", # add terminating "/" character to outputFilePath string if not outputFilePath.endswith("/"): outputFilePath += "/" # get name of directory in which config files will be created; # add terminating "/" character to submissionDirectory string also, # if necessary workingDirectory = os.getcwd() if not workingDirectory.endswith("/"): workingDirectory += "/" submissionDirectory = workingDirectory + "crab/" # compose name of modified config file including the replacements cmsswConfigFile_orig = workingDirectory + configFile cmsswConfigFile_mod = submissionDirectory + configFile.replace("_cfg.py", "_" + sample + "@Grid_cfg.py") cmsswConfigFile_mod = cmsswConfigFile_mod.replace("%s_%s" % (sample, sample), "%s" % sample) if replFunction is not None: replacements = replFunction(channel = channel, sample = sample, type = type, replacements = replacements) # delete previous version of modified config file if it exists if os.path.exists(cmsswConfigFile_mod): os.remove(cmsswConfigFile_mod) # create new version of modified config file prepareConfigFile(configFile_orig = cmsswConfigFile_orig, replacements = replacements, configFile_mod = cmsswConfigFile_mod) # if it exists, delete previous version of crab config file # for submission of cmsRun job to the Grid crabConfigFile_orig = workingDirectory + "crab_template.cfg" crabConfigFile_mod = submissionDirectory + "crab" + "_" + configFile.replace("_cfg.py", "_" + sample + "_" + job + ".cfg") crabConfigFile_mod = crabConfigFile_mod.replace("%s_%s" % (sample, sample), "%s" % sample) # delete previous version of crab config file if it exists if os.path.exists(crabConfigFile_mod): os.remove(crabConfigFile_mod) # create new version of crab config file maxEvents = "-1" eventsPerJob = "20000" replacements = replacements.replace(" ", "") replaceStatements = replacements.split(";") for replaceStatement in replaceStatements: paramNameValuePair = replaceStatement.split("=") if len(paramNameValuePair) != 2: raise ValueError("Invalid format of replace Statement: " + replaceStatement + " !!") if paramNameValuePair[0] == "maxEvents": maxEvents = paramNameValuePair[1] if paramNameValuePair[0] == "eventsPerJob": eventsPerJob = paramNameValuePair[1] crabDirectory = submissionDirectory + configFile.replace("_cfg.py", "_" + sample + "_" + job + "/") crabDirectory = crabDirectory.replace("%s_%s" % (sample, sample), "%s" % sample) crabOutputFiles = "" for outputFile in outputFileNames: if crabOutputFiles != "": crabOutputFiles += ", " crabOutputFiles += outputFile.value().replace("_partXX", "") replacements += "; datasetpath = " + dbs_name replacements += "; dbs_url = " + dbs_url replacements += "; pset = " + cmsswConfigFile_mod replacements += "; total_number_of_events = " + maxEvents replacements += "; events_per_job = " + eventsPerJob replacements += "; output_file = " + crabOutputFiles replacements += "; ui_working_dir = " + crabDirectory #replacements += "; logdir = " + crabDirectory + "res/" replacements += "; user_remote_dir = " + outputFilePath prepareConfigFile(configFile_orig = crabConfigFile_orig, replacements = replacements, configFile_mod = crabConfigFile_mod) # finally, submit job to the Grid if submit == "yes": crabCreateCommand = "crab -create -cfg " + crabConfigFile_mod subprocess.call(crabCreateCommand, shell = True) crabSubmitCommand = "crab -submit -c "+ crabDirectory subprocess.call(crabSubmitCommand, shell = True) crabStatusCommand = "crab -status -c "+ crabDirectory subprocess.call(crabStatusCommand, shell = True)
def submitToBatch(configFile=None, channel=None, sample=None, replFunction=None, replacements="", job="job", queue="1nd", outputFilePath=None, type="mc", resourceRequest=None, submit="yes"): # check that configFile, channel, sample, outputFilePath, and type # parameters are defined and non-empty if configFile is None: raise ValueError("Undefined configFile Parameter !!") if channel is None: raise ValueError("Undefined channel Parameter !!") if sample is None: raise ValueError("Undefined sample Parameter !!") if outputFilePath is None: raise ValueError("Undefined outputFilePath Parameter !!") if type is None: raise ValueError("Undefined type Parameter !!") # in case outputFilePath parameter not terminated by "/", # add terminating "/" character to outputFilePath string if not outputFilePath.endswith("/"): outputFilePath += "/" # get name of sample, without "part" sampleName = sample if sample.find("part") != -1: sampleName = sample[:sample.rfind("part") - 1] # get "part" number, without the sample name part = sample if sample.find("part") != -1: part = sample[sample.find("part") + 4:] # get name of directory in which config files will be created; # add terminating "/" character to submissionDirectory string also, # if necessary workingDirectory = os.getcwd() if not workingDirectory.endswith("/"): workingDirectory += "/" submissionDirectory = workingDirectory + "lxbatch/" + sampleName + "/cfg/" # make directories if necessary if (not os.path.exists(submissionDirectory)): os.makedirs(submissionDirectory) cshDirectory = submissionDirectory.replace("cfg", "csh") if not os.path.exists(cshDirectory): os.mkdir(cshDirectory) logDirectory = submissionDirectory.replace("cfg", "log") if not os.path.exists(logDirectory): os.mkdir(logDirectory) # compose name of modified config file including the replacements configFile_orig = configFile configFile_base = None if configFile.find("/") != -1: configFile_base = submissionDirectory + configFile[configFile. rfind("/") + 1::] else: configFile_base = submissionDirectory + configFile configFile_mod = configFile_base.replace("_cfg.py", "_" + part + "@Batch_cfg.py") #print("configFile_mod = " + configFile_mod) if replFunction is not None: replacements = replFunction(channel=channel, sample=sample, type=type, replacements=replacements) # delete previous version of modified config file if it exists if os.path.exists(configFile_mod): os.remove(configFile_mod) # create new version of modified config file prepareConfigFile(configFile_orig=configFile_orig, replacements=replacements, configFile_mod=configFile_mod) # if it exists, delete previous version of shell script # for submission of cmsRun job to the CERN batch system #scriptFile = cshDirectory + sample + '.csh' scriptFile = configFile_base.replace("_cfg.py", "_" + part + "@Batch.csh") scriptFile = scriptFile.replace("cfg", "csh") if os.path.exists(scriptFile): os.remove(scriptFile) #print("scriptFile = " + scriptFile) # create shell script for submission of cmsRun job to the CERN batch system # (copy all .root files produced by the cmsRun job to directory specified # by outputFilePath parameter given as function argument; # use 'rfcp' for copying to castor and 'scp' for copying to afs area) cp = None if outputFilePath.find("/castor") != -1: script = """#!/bin/csh limit vmem unlim cd %(subDir)s setenv SCRAM_ARCH slc5_ia32_gcc434 eval `scramv1 runtime -csh` cd - cmsRun %(config)s set rootFiles=(`/bin/ls *.root`) foreach rootFile (${rootFiles}) echo "Checking for pre-existing file:" rfrm %(outDir)s/${rootFile} echo "copying ${rootFile} to %(outDir)s" rfcp ${rootFile} %(outDir)s end """ % { 'subDir': submissionDirectory, 'config': configFile_mod, 'outDir': outputFilePath } else: script = """#!/bin/csh limit vmem unlim cd %(subDir)s setenv SCRAM_ARCH slc5_ia32_gcc434 eval `scramv1 runtime -csh` cd - cmsRun %(config)s set rootFiles=(`/bin/ls *.root`) foreach rootFile (${rootFiles}) echo "copying ${rootFile} to %(outDir)s" scp ${rootFile} %(outDir)s end """ % { 'subDir': submissionDirectory, 'config': configFile_mod, 'outDir': outputFilePath } scf = open(scriptFile, "w") scf.write(script) scf.close() # make shell script executable os.chmod(scriptFile, 0744) # finally, submit job to the CERN batch system if submit == "yes": logFile = configFile_base.replace("_cfg.py", "_" + sample + "@Batch.out") logFile = logFile.replace("cfg", "log") #print("logFile = " + logFile) jobName = job + channel + "_" + sample bsubCommand = 'bsub -q ' + queue + ' -J ' + jobName + ' -L /bin/csh -eo ' + logFile + ' -oo ' + logFile if resourceRequest != None: bsubCommand += ' -R \"' + resourceRequest + '\" ' bsubCommand += ' < ' + scriptFile #print("bsubCommand:") #print(bsubCommand) subprocess.call(bsubCommand, shell=True) # wait for 10 seconds, in order not to generate too many castor requests in too short a time # (maximum number of permissible requests = 900 in 180 seconds; cmsRun jobs will abort in case this limit is excdeeded) time.sleep(1)
def submitToBatch(configFile = None, channel = None, sample = None, replFunction = None, replacements = "", job = "job", queue = "1nd", outputFilePath = None, type = "mc", resourceRequest = None, submit = "yes"): # check that configFile, channel, sample, outputFilePath, and type # parameters are defined and non-empty if configFile is None: raise ValueError("Undefined configFile Parameter !!") if channel is None: raise ValueError("Undefined channel Parameter !!") if sample is None: raise ValueError("Undefined sample Parameter !!") if outputFilePath is None: raise ValueError("Undefined outputFilePath Parameter !!") if type is None: raise ValueError("Undefined type Parameter !!") # in case outputFilePath parameter not terminated by "/", # add terminating "/" character to outputFilePath string if not outputFilePath.endswith("/"): outputFilePath += "/" # get name of sample, without "part" sampleName = sample if sample.find("part") != -1: sampleName = sample[:sample.rfind("part") - 1] # get "part" number, without the sample name part = sample if sample.find("part") != -1: part = sample[sample.find("part")+4:] # get name of directory in which config files will be created; # add terminating "/" character to submissionDirectory string also, # if necessary workingDirectory = os.getcwd() if not workingDirectory.endswith("/"): workingDirectory += "/" submissionDirectory = workingDirectory + "lxbatch/"+ sampleName + "/cfg/" # make directories if necessary if (not os.path.exists(submissionDirectory)): os.makedirs(submissionDirectory) cshDirectory = submissionDirectory.replace("cfg","csh") if not os.path.exists(cshDirectory): os.mkdir(cshDirectory) logDirectory = submissionDirectory.replace("cfg","log") if not os.path.exists(logDirectory): os.mkdir(logDirectory) # compose name of modified config file including the replacements configFile_orig = configFile configFile_base = None if configFile.find("/") != -1: configFile_base = submissionDirectory + configFile[configFile.rfind("/") + 1::] else: configFile_base = submissionDirectory + configFile configFile_mod = configFile_base.replace("_cfg.py", "_" + part + "@Batch_cfg.py") #print("configFile_mod = " + configFile_mod) if replFunction is not None: replacements = replFunction(channel = channel, sample = sample, type = type, replacements = replacements) # delete previous version of modified config file if it exists if os.path.exists(configFile_mod): os.remove(configFile_mod) # create new version of modified config file prepareConfigFile(configFile_orig = configFile_orig, replacements = replacements, configFile_mod = configFile_mod) # if it exists, delete previous version of shell script # for submission of cmsRun job to the CERN batch system #scriptFile = cshDirectory + sample + '.csh' scriptFile = configFile_base.replace("_cfg.py", "_" + part + "@Batch.csh") scriptFile = scriptFile.replace("cfg","csh") if os.path.exists(scriptFile): os.remove(scriptFile) #print("scriptFile = " + scriptFile) # create shell script for submission of cmsRun job to the CERN batch system # (copy all .root files produced by the cmsRun job to directory specified # by outputFilePath parameter given as function argument; # use 'rfcp' for copying to castor and 'scp' for copying to afs area) cp = None if outputFilePath.find("/castor") != -1: script = """#!/bin/csh limit vmem unlim cd %(subDir)s setenv SCRAM_ARCH slc5_ia32_gcc434 eval `scramv1 runtime -csh` cd - cmsRun %(config)s set rootFiles=(`/bin/ls *.root`) foreach rootFile (${rootFiles}) echo "Checking for pre-existing file:" rfrm %(outDir)s/${rootFile} echo "copying ${rootFile} to %(outDir)s" rfcp ${rootFile} %(outDir)s end """ % {'subDir': submissionDirectory, 'config': configFile_mod, 'outDir':outputFilePath} else: script = """#!/bin/csh limit vmem unlim cd %(subDir)s setenv SCRAM_ARCH slc5_ia32_gcc434 eval `scramv1 runtime -csh` cd - cmsRun %(config)s set rootFiles=(`/bin/ls *.root`) foreach rootFile (${rootFiles}) echo "copying ${rootFile} to %(outDir)s" scp ${rootFile} %(outDir)s end """ % {'subDir': submissionDirectory, 'config': configFile_mod, 'outDir':outputFilePath} scf = open(scriptFile,"w") scf.write(script) scf.close() # make shell script executable os.chmod(scriptFile,0744) # finally, submit job to the CERN batch system if submit == "yes": logFile = configFile_base.replace("_cfg.py", "_" + sample + "@Batch.out") logFile = logFile.replace("cfg","log") #print("logFile = " + logFile) jobName = job + channel + "_" + sample bsubCommand = 'bsub -q ' + queue + ' -J ' + jobName + ' -L /bin/csh -eo ' + logFile + ' -oo ' + logFile if resourceRequest != None: bsubCommand += ' -R \"' + resourceRequest + '\" ' bsubCommand += ' < ' + scriptFile #print("bsubCommand:") #print(bsubCommand) subprocess.call(bsubCommand, shell = True) # wait for 10 seconds, in order not to generate too many castor requests in too short a time # (maximum number of permissible requests = 900 in 180 seconds; cmsRun jobs will abort in case this limit is excdeeded) time.sleep(1)