CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlCaHLTBitMon_ParallelJobs.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #-----------------------------------------------------
4 # original author: Andrea Lucaroni
5 # Revision: $Revision: 1.53 $
6 # Last update: $Date: 2011/06/28 19:01:36 $
7 # by: $Author: mussgill $
8 #-----------------------------------------------------
9 
10 from __future__ import print_function
11 import re
12 import json
13 import os
14 import stat
15 import sys
16 
17 import array
18 import pickle as pk
19 
20 from optparse import OptionParser
21 #####DEBUG
22 DEBUG = 0
23 
25  template = os.path.expandvars('$CMSSW_BASE/src/Alignment/CommonAlignmentProducer/data/AlCaHLTBitMon_cfg_template_py')
26  if os.path.exists(template):
27  return template
28  template = os.path.expandvars('$CMSSW_RELEASE_BASE/src/Alignment/CommonAlignmentProducer/data/AlCaHLTBitMon_cfg_template_py')
29  if os.path.exists(template):
30  return template
31  return 'None'
32 
33 def mkHLTKeyListList(hltkeylistfile):
34  keylistlist = []
35  f = open(hltkeylistfile, 'r')
36  for line in f:
37  keylistlist.append(line.replace('\n',''))
38  f.close()
39  return keylistlist
40 
41 def parallelJobs(hltkeylistfile,jsonDir,globalTag,templateName,queue,cafsetup):
42  PWD = os.path.abspath('.')
43 
44  if templateName == 'default':
45  templateFile = getConfigTemplateFilename()
46  else:
47  templateFile = os.path.abspath(os.path.expandvars(templateName))
48 
49  tfile = open(templateFile, 'r')
50  template = tfile.read()
51  tfile.close()
52  template = template.replace('%%%GLOBALTAG%%%', globalTag)
53 
54  keylistlist = mkHLTKeyListList(hltkeylistfile)
55  index = 0
56  for keylist in keylistlist:
57 
58  configName = 'AlCaHLTBitMon_%d_cfg.py'%index
59  jobName = 'AlCaHLTBitMon_%d_job.csh'%index
60  jsonFile = os.path.abspath('%(dir)s/%(index)d.json' % {'dir' : jsonDir, 'index' : index})
61  dataFile = os.path.abspath('%(dir)s/%(index)d.data' % {'dir' : jsonDir, 'index' : index})
62  logFile = 'AlCaHLTBitMon_%d'%index
63 
64  dfile = open(dataFile, 'r')
65  data = dfile.read()
66  dfile.close()
67 
68  config = template.replace('%%%JSON%%%', jsonFile);
69  config = config.replace('%%%DATA%%%', data);
70  config = config.replace('%%%KEYNAME%%%', keylist);
71  config = config.replace('%%%LOGFILE%%%', logFile);
72 
73  cfile = open(configName, 'w')
74  for line in config:
75  cfile.write(line)
76  cfile.close()
77 
78  jfile = open(jobName, 'w')
79  jfile.write('#!/bin/tcsh\n\n')
80  if cafsetup == True:
81  jfile.write('source /afs/cern.ch/cms/caf/setup.csh\n\n')
82 
83  jfile.write('cd $1\n\n')
84  jfile.write('eval `scramv1 run -csh`\n\n')
85  jfile.write('cmsRun %s\n\n'%configName)
86  jfile.write('cp %s.log $2'%logFile)
87  jfile.close()
88 
89  if os.path.exists('./%s'%keylist) == False:
90  os.system('mkdir ./%s'%keylist)
91 
92  os.system('chmod u+x %s'%jobName)
93  #print('bsub -q %(queue)s %(jobname)s %(pwd)s %(pwd)s/%(outdir)s' % {'queue' : queue, 'jobname' : jobName, 'pwd' : PWD, 'outdir' : keylist})
94  os.system('bsub -q %(queue)s %(jobname)s %(pwd)s %(pwd)s/%(outdir)s' % {'queue' : queue, 'jobname' : jobName, 'pwd' : PWD, 'outdir' : keylist})
95 
96  index = index + 1
97 
99  parser = OptionParser()
100 
101  parser.add_option("-k", "--keylist",
102  dest="hltKeyListFile",
103  default="lista_key.txt",
104  help="text file with HLT keys")
105 
106  parser.add_option("-j", "--json",
107  dest="jsonDir",
108  help="directory with the corresponding json files")
109 
110  parser.add_option("-g", "--globalTag",
111  dest="globalTag",
112  help="the global tag to use in the config files")
113 
114  parser.add_option("-t", "--template",
115  dest="template",
116  default="default",
117  help="the template to use for the config files")
118 
119  parser.add_option("-q", "--queue",
120  dest="queue",
121  default="cmscaf1nd",
122  help="the queue to use (default=cmscaf1nd)")
123 
124  parser.add_option("-c", "--cafsetup", action="store_true",
125  dest="cafsetup",
126  default=False,
127  help="wether the caf setup is sourced in the scripts")
128 
129  (options, args) = parser.parse_args()
130 
131  if len(sys.argv) == 1:
132  print("\nUsage: %s --help"%sys.argv[0])
133  sys.exit(0)
134 
135  if str(options.hltKeyListFile) == 'None':
136  print("Please provide a file with HLT keys")
137  sys.exit(0)
138 
139  if str(options.jsonDir) == 'None':
140  print("Please provide a directory containing the json files")
141  sys.exit(0)
142 
143  if str(options.globalTag) == 'None':
144  print("Please provide a global tag")
145  sys.exit(0)
146 
147  return options
148 
149 
150 #---------------------------------------------MAIN
151 
152 options = defineOptions()
153 p = parallelJobs(options.hltKeyListFile,
154  options.jsonDir,
155  options.globalTag,
156  options.template,
157  options.queue,
158  options.cafsetup)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)