5 import os, sys, imp, re, pprint, string, time,shutil,copy,pickle,math
6 from optparse
import OptionParser
9 from PhysicsTools.HeppyCore.utils.batchmanager
import BatchManager
10 import PhysicsTools.HeppyCore.utils.eostools
as eostools
13 import FWCore.ParameterSet.Config
as cms
14 from IOMC.RandomEngine.RandomServiceHelper
import RandomNumberServiceHelper
18 script =
"""!/usr/bin/env bash
19 #PBS -l platform=LINUX,u_sps_cmsf,M=2000MB,T=2000000
26 source $HOME/.bash_profile
28 echo '***********************'
32 # coming back to submission dir do setup the env
41 cp -r $PBS_O_WORKDIR .
49 cat > sysinfo.sh <<EOF
51 echo '************** ENVIRONMENT ****************'
56 echo '************** WORKER *********************'
63 echo '************** START *********************'
67 source sysinfo.sh > sysinfo.txt
71 # copy job dir do disk
73 cp -r $jobdir $PBS_O_WORKDIR
82 for file in *.root; do
83 newFileName=`echo $file | sed -r -e 's/\./_{index}\./'`
84 fullFileName={remoteDir}/$newFileName
85 {eos} cp $file /eos/cms/$fullFileName
86 {eos} chmod 755 /eos/cms/$fullFileName
89 """.
format(index=index, remoteDir=remoteDir, eos=eostools.eos_select)
93 '''prepare the LSF version of the batch script, to run on LSF'''
94 script =
"""#!/bin/bash
102 echo 'copying job dir to worker'
104 eval `scramv1 ru -sh`
108 cd `find . -type d | grep /`
112 echo wrong exit code! removing all root files
116 echo 'sending the job directory back'
122 script +=
'cp -rf * $LS_SUBCWD\n'
127 '''prepare a local version of the batch script, to run using nohup'''
129 script =
"""#!/bin/bash
133 echo wrong exit code! removing all root files
137 echo 'sending the job directory back'
147 '''Exception class for this script'''
153 return str( self.
value)
157 '''Batch manager specific to cmsRun processes.'''
160 '''Prepare one job. This function is called by the base class.'''
162 process.source = fullSource.clone()
165 scriptFileName = jobDir+
'/batchScript.sh'
166 scriptFile = open(scriptFileName,
'w')
167 storeDir = self.remoteOutputDir_.replace(
'/castor/cern.ch/cms',
'')
168 mode = self.RunningMode(options.batch)
171 elif mode ==
'LOCAL':
174 os.system(
'chmod +x %s' % scriptFileName)
179 randSvc = RandomNumberServiceHelper(process.RandomNumberGeneratorService)
182 iFileMin = (value-1)*grouping
183 iFileMax = (value)*grouping
184 process.source.fileNames = fullSource.fileNames[iFileMin:iFileMax]
186 cfgFile = open(jobDir+
'/run_cfg.py',
'w')
187 cfgFile.write(
'import FWCore.ParameterSet.Config as cms\n\n')
188 cfgFile.write(
'import os,sys\n')
190 cfgFile.write(
"sys.path.append('%s')\n" % os.path.dirname(jobDir) )
191 cfgFile.write(
'from base_cfg import *\n')
192 cfgFile.write(
'process.source = ' + process.source.dumpPython() +
'\n')
194 cfgFile.write(
'process.RandomNumberGeneratorService = ' + process.RandomNumberGeneratorService.dumpPython() +
'\n')
201 file = open(
'cmsBatch.txt',
'w')
202 file.write(string.join(sys.argv) +
"\n")
205 batchManager.parser_.usage =
"""
206 %prog [options] <number of input files per job> <your_cfg.py>.
208 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.
210 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.
212 - the name of the output directory is created automatically.
213 - the output root files end up in the job directories.
215 Each job directory contains:
216 - the full python configuration for this job. You can run it interactively by doing:
218 - the batch script to run the job. You can submit it again by calling the batch command yourself, see the -b option.
219 - while running interactively: nohup.out, where the job stderr and stdout are redirected. To check the status of a job running interactively, do:
222 o the full nohup.out (your log) and your root files, in case you ran interactively
223 o the LSF directory, in case you ran on LSF
225 Also see fwBatch.py, which is a layer on top of cmsBatch.py adapted to the organization of our samples on the CMST3.
230 cd $CMSSW_BASE/src/CMGTools/Common/test
232 to run on your local machine:
233 cmsBatch.py 1 testCMGTools_cfg.py -b 'nohup ./batchScript.sh&'
235 to run on LSF (you must be logged on lxplus, not on your interactive machine, so that you have access to LSF)
236 cmsBatch.py 1 testCMGTools_cfg.py -b 'bsub -q 8nm < ./batchScript.sh'
238 batchManager.parser_.add_option(
"-p",
"--program", dest=
"prog",
239 help=
"program to run on your cfg file",
244 batchManager.parser_.add_option(
"-c",
"--command-args", dest=
"cmdargs",
245 help=
"command line arguments for the job",
247 batchManager.parser_.add_option(
"--notagCVS", dest=
"tagPackages",
248 default=
True,action=
"store_false",
249 help=
"tag the package on CVS (True)")
251 (options,args) = batchManager.parser_.parse_args()
252 batchManager.ParseOptions()
255 doCVSTag = options.tagPackages
258 batchManager.parser_.print_help()
264 runningMode = batchManager.RunningMode( options.batch )
265 except CmsBatchException
as err:
269 grouping = int(args[0])
271 cfgFileName = args[1]
275 pycfg_params = options.cmdargs
277 sys.argv = [cfgFileName]
279 sys.argv.extend(pycfg_params.split(
' '))
284 handle = open(cfgFileName,
'r')
285 cfo = imp.load_source("pycfg", cfgFileName, handle)
286 process = cfo.process
294 fullSource = process.source.clone()
298 process.source.fileNames
300 print 'No input file. This is a generator process.'
302 listOfValues = [i+1
for i
in range( nJobs )]
304 print "Number of files in the source:",len(process.source.fileNames),
":"
305 pprint.pprint(process.source.fileNames)
306 nFiles = len(process.source.fileNames)
307 nJobs = nFiles / grouping
308 if (nJobs!=0
and (nFiles % grouping) > 0)
or nJobs==0:
311 print "number of jobs to be created: ", nJobs
312 listOfValues = [i+1
for i
in range( nJobs )]
315 batchManager.PrepareJobs( listOfValues )
319 cfgFile = open(batchManager.outputDir_+
'/base_cfg.py',
'w')
320 cfgFile.write( process.dumpPython() +
'\n')
326 if runningMode ==
'LOCAL':
330 batchManager.SubmitJobs( waitingTime )
335 from PhysicsTools.HeppyCore.utils.logger
import logger
338 os.chdir(batchManager.outputDir_)
340 os.system(
'mkdir ' + logDir )
346 log.addFile( oldPwd +
'/' + cfgFileName )
348 if not batchManager.options_.negate:
349 if batchManager.remoteOutputDir_ !=
"":
352 log.stageOut( batchManager.remoteOutputDir_ )
def rootfiles_to_eos_script