5 from __future__
import print_function
6 from builtins
import range
7 import os, sys, imp, re, pprint, string, time,shutil,copy,pickle,math
8 from optparse
import OptionParser
11 from PhysicsTools.HeppyCore.utils.batchmanager
import BatchManager
12 import PhysicsTools.HeppyCore.utils.eostools
as eostools
15 import FWCore.ParameterSet.Config
as cms
16 from IOMC.RandomEngine.RandomServiceHelper
import RandomNumberServiceHelper
20 script =
"""!/usr/bin/env bash
21 #PBS -l platform=LINUX,u_sps_cmsf,M=2000MB,T=2000000
28 source $HOME/.bash_profile
30 echo '***********************'
34 # coming back to submission dir do setup the env
43 cp -r $PBS_O_WORKDIR .
51 cat > sysinfo.sh <<EOF
53 echo '************** ENVIRONMENT ****************'
58 echo '************** WORKER *********************'
65 echo '************** START *********************'
69 source sysinfo.sh > sysinfo.txt
73 # copy job dir do disk
75 cp -r $jobdir $PBS_O_WORKDIR
84 for file in *.root; do
85 newFileName=`echo $file | sed -r -e 's/\./_{index}\./'`
86 fullFileName={remoteDir}/$newFileName
87 {eos} cp $file /eos/cms/$fullFileName
88 {eos} chmod 755 /eos/cms/$fullFileName
91 """.
format(index=index, remoteDir=remoteDir, eos=eostools.eos_select)
95 '''prepare the LSF version of the batch script, to run on LSF'''
96 script =
"""#!/bin/bash
104 echo 'copying job dir to worker'
106 eval `scramv1 ru -sh`
110 cd `find . -type d | grep /`
114 echo wrong exit code! removing all root files
118 echo 'sending the job directory back'
124 script +=
'cp -rf * $LS_SUBCWD\n'
129 '''prepare a local version of the batch script, to run using nohup'''
131 script =
"""#!/bin/bash
135 echo wrong exit code! removing all root files
139 echo 'sending the job directory back'
149 '''Exception class for this script'''
159 '''Batch manager specific to cmsRun processes.'''
162 '''Prepare one job. This function is called by the base class.'''
164 process.source = fullSource.clone()
167 scriptFileName = jobDir+
'/batchScript.sh'
168 scriptFile = open(scriptFileName,
'w')
169 storeDir = self.remoteOutputDir_.
replace(
'/castor/cern.ch/cms',
'')
170 mode = self.RunningMode(options.batch)
173 elif mode ==
'LOCAL':
176 os.system(
'chmod +x %s' % scriptFileName)
181 randSvc = RandomNumberServiceHelper(process.RandomNumberGeneratorService)
184 iFileMin = (value-1)*grouping
185 iFileMax = (value)*grouping
186 process.source.fileNames = fullSource.fileNames[iFileMin:iFileMax]
187 print(process.source)
188 cfgFile = open(jobDir+
'/run_cfg.py',
'w')
189 cfgFile.write(
'import FWCore.ParameterSet.Config as cms\n\n')
190 cfgFile.write(
'import os,sys\n')
192 cfgFile.write(
"sys.path.append('%s')\n" % os.path.dirname(jobDir) )
193 cfgFile.write(
'from base_cfg import *\n')
194 cfgFile.write(
'process.source = ' + process.source.dumpPython() +
'\n')
196 cfgFile.write(
'process.RandomNumberGeneratorService = ' + process.RandomNumberGeneratorService.dumpPython() +
'\n')
203 file = open(
'cmsBatch.txt',
'w')
204 file.write(string.join(sys.argv) +
"\n")
207 batchManager.parser_.usage =
"""
208 %prog [options] <number of input files per job> <your_cfg.py>.
210 Submits a number of jobs taking your_cfg.py as a template. your_cfg.py can either read events from input files, or produce them with a generator. In the later case, the seeds are of course updated for each job.
212 A local output directory is created locally. This directory contains a job directory for each job, and a Logger/ directory containing information on the software you are using.
214 - the name of the output directory is created automatically.
215 - the output root files end up in the job directories.
217 Each job directory contains:
218 - the full python configuration for this job. You can run it interactively by doing:
220 - the batch script to run the job. You can submit it again by calling the batch command yourself, see the -b option.
221 - while running interactively: nohup.out, where the job stderr and stdout are redirected. To check the status of a job running interactively, do:
224 o the full nohup.out (your log) and your root files, in case you ran interactively
225 o the LSF directory, in case you ran on LSF
227 Also see fwBatch.py, which is a layer on top of cmsBatch.py adapted to the organization of our samples on the CMST3.
232 cd $CMSSW_BASE/src/CMGTools/Common/test
234 to run on your local machine:
235 cmsBatch.py 1 testCMGTools_cfg.py -b 'nohup ./batchScript.sh&'
237 to run on LSF (you must be logged on lxplus, not on your interactive machine, so that you have access to LSF)
238 cmsBatch.py 1 testCMGTools_cfg.py -b 'bsub -q 8nm < ./batchScript.sh'
240 batchManager.parser_.add_option(
"-p",
"--program", dest=
"prog",
241 help=
"program to run on your cfg file",
246 batchManager.parser_.add_option(
"-c",
"--command-args", dest=
"cmdargs",
247 help=
"command line arguments for the job",
249 batchManager.parser_.add_option(
"--notagCVS", dest=
"tagPackages",
250 default=
True,action=
"store_false",
251 help=
"tag the package on CVS (True)")
253 (options,args) = batchManager.parser_.parse_args()
254 batchManager.ParseOptions()
257 doCVSTag = options.tagPackages
260 batchManager.parser_.print_help()
266 runningMode = batchManager.RunningMode( options.batch )
267 except CmsBatchException
as err:
273 cfgFileName = args[1]
277 pycfg_params = options.cmdargs
279 sys.argv = [cfgFileName]
281 sys.argv.extend(pycfg_params.split(
' '))
286 handle = open(cfgFileName,
'r')
287 cfo = imp.load_source(
"pycfg", cfgFileName, handle)
288 process = cfo.process
296 fullSource = process.source.clone()
300 process.source.fileNames
302 print(
'No input file. This is a generator process.')
304 listOfValues = [i+1
for i
in range( nJobs )]
306 print(
"Number of files in the source:",len(process.source.fileNames),
":")
307 pprint.pprint(process.source.fileNames)
308 nFiles = len(process.source.fileNames)
309 nJobs = nFiles / grouping
310 if (nJobs!=0
and (nFiles % grouping) > 0)
or nJobs==0:
313 print(
"number of jobs to be created: ", nJobs)
314 listOfValues = [i+1
for i
in range( nJobs )]
317 batchManager.PrepareJobs( listOfValues )
321 cfgFile = open(batchManager.outputDir_+
'/base_cfg.py',
'w')
322 cfgFile.write( process.dumpPython() +
'\n')
328 if runningMode ==
'LOCAL':
332 batchManager.SubmitJobs( waitingTime )
337 from PhysicsTools.HeppyCore.utils.logger
import logger
340 os.chdir(batchManager.outputDir_)
342 os.system(
'mkdir ' + logDir )
348 log.addFile( oldPwd +
'/' + cfgFileName )
350 if not batchManager.options_.negate:
351 if batchManager.remoteOutputDir_ !=
"":
354 log.stageOut( batchManager.remoteOutputDir_ )