CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Alignment/CommonAlignmentProducer/scripts/AlCaHLTBitMon_ParallelJobs.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #-----------------------------------------------------
00004 # original author: Andrea Lucaroni
00005 # Revision:        $Revision: 1.1 $
00006 # Last update:     $Date: 2011/06/28 19:34:22 $
00007 # by:              $Author: mussgill $
00008 #-----------------------------------------------------
00009 
00010 import re
00011 import json
00012 import os 
00013 import stat
00014 import sys
00015 
00016 import array
00017 import pickle as pk
00018 
00019 from optparse import OptionParser
00020 #####DEBUG
00021 DEBUG = 0
00022 
00023 def getConfigTemplateFilename():
00024     template = os.path.expandvars('$CMSSW_BASE/src/Alignment/CommonAlignmentProducer/data/AlCaHLTBitMon_cfg_template_py')
00025     if os.path.exists(template):
00026         return template
00027     template = os.path.expandvars('$CMSSW_RELEASE_BASE/src/Alignment/CommonAlignmentProducer/data/AlCaHLTBitMon_cfg_template_py')
00028     if os.path.exists(template):
00029         return template
00030     return 'None'
00031 
00032 def mkHLTKeyListList(hltkeylistfile):
00033     keylistlist = []
00034     f = open(hltkeylistfile, 'r')
00035     for line in f:
00036         keylistlist.append(line.replace('\n',''))
00037     f.close()
00038     return keylistlist
00039 
00040 def parallelJobs(hltkeylistfile,jsonDir,globalTag,templateName,queue,cafsetup):
00041     PWD = os.path.abspath('.')
00042 
00043     if templateName == 'default':
00044         templateFile = getConfigTemplateFilename()
00045     else:
00046         templateFile = os.path.abspath(os.path.expandvars(templateName))
00047         
00048     tfile = open(templateFile, 'r')
00049     template = tfile.read()
00050     tfile.close()
00051     template = template.replace('%%%GLOBALTAG%%%', globalTag)
00052 
00053     keylistlist = mkHLTKeyListList(hltkeylistfile)
00054     index = 0
00055     for keylist in keylistlist:
00056         
00057         configName = 'AlCaHLTBitMon_%d_cfg.py'%index
00058         jobName = 'AlCaHLTBitMon_%d_job.csh'%index
00059         jsonFile = os.path.abspath('%(dir)s/%(index)d.json' % {'dir' : jsonDir, 'index' : index})
00060         dataFile = os.path.abspath('%(dir)s/%(index)d.data' % {'dir' : jsonDir, 'index' : index})
00061         logFile = 'AlCaHLTBitMon_%d'%index
00062         
00063         dfile = open(dataFile, 'r')
00064         data = dfile.read()
00065         dfile.close()
00066 
00067         config = template.replace('%%%JSON%%%', jsonFile);
00068         config = config.replace('%%%DATA%%%', data);
00069         config = config.replace('%%%KEYNAME%%%', keylist);
00070         config = config.replace('%%%LOGFILE%%%', logFile);
00071 
00072         cfile = open(configName, 'w')
00073         for line in config:
00074             cfile.write(line)
00075         cfile.close()
00076 
00077         jfile = open(jobName, 'w')
00078         jfile.write('#!/bin/tcsh\n\n')
00079         if cafsetup == True:
00080             jfile.write('source /afs/cern.ch/cms/caf/setup.csh\n\n')
00081             
00082         jfile.write('cd $1\n\n')
00083         jfile.write('eval `scramv1 run -csh`\n\n')
00084         jfile.write('cmsRun %s\n\n'%configName)
00085         jfile.write('cp %s.log $2'%logFile)
00086         jfile.close()
00087 
00088         if os.path.exists('./%s'%keylist) == False:
00089             os.system('mkdir ./%s'%keylist)
00090             
00091         os.system('chmod u+x %s'%jobName)
00092         #print('bsub -q %(queue)s %(jobname)s %(pwd)s %(pwd)s/%(outdir)s' % {'queue' : queue, 'jobname' : jobName, 'pwd' : PWD, 'outdir' : keylist})
00093         os.system('bsub -q %(queue)s %(jobname)s %(pwd)s %(pwd)s/%(outdir)s' % {'queue' : queue, 'jobname' : jobName, 'pwd' : PWD, 'outdir' : keylist})
00094 
00095         index = index + 1
00096 
00097 def defineOptions():
00098     parser = OptionParser()
00099     
00100     parser.add_option("-k", "--keylist",
00101                       dest="hltKeyListFile",
00102                       default="lista_key.txt",
00103                       help="text file with HLT keys")
00104 
00105     parser.add_option("-j", "--json",
00106                       dest="jsonDir",
00107                       help="directory with the corresponding json files")
00108     
00109     parser.add_option("-g", "--globalTag",
00110                       dest="globalTag",
00111                       help="the global tag to use in the config files")
00112 
00113     parser.add_option("-t", "--template",
00114                       dest="template",
00115                       default="default",
00116                       help="the template to use for the config files")
00117 
00118     parser.add_option("-q", "--queue",
00119                       dest="queue",
00120                       default="cmscaf1nd",
00121                       help="the queue to use (default=cmscaf1nd)")
00122 
00123     parser.add_option("-c", "--cafsetup", action="store_true",
00124                       dest="cafsetup",
00125                       default=False,
00126                       help="wether the caf setup is sourced in the scripts")
00127    
00128     (options, args) = parser.parse_args()
00129 
00130     if len(sys.argv) == 1:
00131         print("\nUsage: %s --help"%sys.argv[0])
00132         sys.exit(0)
00133 
00134     if str(options.hltKeyListFile) == 'None':
00135         print("Please provide a file with HLT keys")
00136         sys.exit(0)
00137 
00138     if str(options.jsonDir) == 'None':
00139         print("Please provide a directory containing the json files")
00140         sys.exit(0)
00141 
00142     if str(options.globalTag) == 'None':
00143         print("Please provide a global tag")
00144         sys.exit(0)
00145     
00146     return options
00147 
00148 
00149 #---------------------------------------------MAIN
00150 
00151 options = defineOptions()
00152 p = parallelJobs(options.hltKeyListFile,
00153                  options.jsonDir,
00154                  options.globalTag,
00155                  options.template,
00156                  options.queue,
00157                  options.cafsetup)