CMS 3D CMS Logo

createBeamHaloJobs.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from builtins import range
5 import os, sys, re, optparse, math
6 
7 copyargs = sys.argv[:]
8 for i in range(len(copyargs)):
9  if copyargs[i] == "":
10  copyargs[i] = "\"\""
11  if copyargs[i].find(" ") != -1:
12  copyargs[i] = "\"%s\"" % copyargs[i]
13 commandline = " ".join(copyargs)
14 
15 usage = """./%prog DIRNAME PATTERN INITIALGEOM INPUTFILES [options]
16 
17 Creates (overwrites) a directory for each word in PATTERN and creates
18 (overwrites) submitJobs.sh with the submission sequence and
19 dependencies.
20 
21 DIRNAME directories will be named DIRNAME01, DIRNAME02, etc.
22 PATTERN a quoted combination of "phiy", "phipos", "phiz"
23 INITIALGEOM SQLite file containing muon geometry with tag names
24  CSCAlignmentRcd, CSCAlignmentErrorExtendedRcd
25 INPUTFILES Python file defining 'fileNames', a list of input files as
26  strings"""
27 
28 parser = optparse.OptionParser(usage)
29 parser.add_option("-j", "--jobs",
30  help="approximate number of \"gather\" subjobs",
31  type="int",
32  default=50,
33  dest="subjobs")
34 parser.add_option("-s", "--submitJobs",
35  help="alternate name of submitJobs.sh script (please include .sh extension); a file with this name will be OVERWRITTEN",
36  type="string",
37  default="submitJobs.sh",
38  dest="submitJobs")
39 parser.add_option("-b", "--big",
40  help="if invoked, subjobs will also be run on cmscaf1nd",
41  action="store_true",
42  dest="big")
43 parser.add_option("-u", "--user_mail",
44  help="if invoked, send mail to a specified email destination. If \"-u\" is not present, the default destination LSB_MAILTO in lsf.conf will be used",
45  type="string",
46  dest="user_mail")
47 parser.add_option("--globalTag",
48  help="GlobalTag for calibration conditions",
49  type="string",
50  default="GR_R_42_V14::All",
51  dest="globaltag")
52 parser.add_option("--photogrammetry",
53  help="if invoked, alignment will be constrained to photogrammetry",
54  action="store_true",
55  dest="photogrammetry")
56 parser.add_option("--photogrammetryOnlyholes",
57  help="if invoked, only missing data will be constrained to photogrammetry",
58  action="store_true",
59  dest="photogrammetryOnlyholes")
60 parser.add_option("--photogrammetryOnlyOnePerRing",
61  help="if invoked, only one chamber per ring will be constrained to photogrammetry",
62  action="store_true",
63  dest="photogrammetryOnlyOnePerRing")
64 parser.add_option("--photogrammetryScale",
65  help="scale factor for photogrammetry constraint: 1 is default and 10 *weakens* the constraint by a factor of 10",
66  type="string",
67  default="1.",
68  dest="photogrammetryScale")
69 parser.add_option("--slm",
70  help="if invoked, apply SLM constraint",
71  action="store_true",
72  dest="slm")
73 parser.add_option("--fillME11holes",
74  help="use CollisionsOct2010 data to fill holes in ME1/1",
75  action="store_true",
76  dest="fillME11holes")
77 parser.add_option("--disks",
78  help="align whole disks, rather than individual rings",
79  action="store_true",
80  dest="disks")
81 
82 parser.add_option("--minP",
83  help="minimum track momentum (measured via radial component of fringe fields)",
84  type="string",
85  default="5",
86  dest="minP")
87 parser.add_option("--minHitsPerChamber",
88  help="minimum number of hits per chamber",
89  type="string",
90  default="5",
91  dest="minHitsPerChamber")
92 parser.add_option("--maxdrdz",
93  help="maximum dr/dz of tracklets (anti-cosmic cut)",
94  type="string",
95  default="0.2",
96  dest="maxdrdz")
97 parser.add_option("--maxRedChi2",
98  help="maximum reduced chi^2 of tracks",
99  type="string",
100  default="10",
101  dest="maxRedChi2")
102 parser.add_option("--fiducial",
103  help="if invoked, select only segments within the good region of the chamber (for all 6 layers)",
104  action="store_true",
105  dest="fiducial")
106 parser.add_option("--useHitWeights",
107  help="if invoked, use hit weights in tracklet fits",
108  action="store_true",
109  dest="useHitWeights")
110 parser.add_option("--truncateSlopeResid",
111  help="maximum allowed slope residual in mrad (like the histograms in a phiy job)",
112  type="string",
113  default="30.",
114  dest="truncateSlopeResid")
115 parser.add_option("--truncateOffsetResid",
116  help="maximum allowed offset residual in mm (like the histograms in a phipos or phiz job)",
117  type="string",
118  default="15.",
119  dest="truncateOffsetResid")
120 parser.add_option("--combineME11",
121  help="if invoked, combine ME1/1a and ME1/1b chambers",
122  action="store_true",
123  dest="combineME11")
124 parser.add_option("--useTrackWeights",
125  help="if invoked, weight residuals by track uncertainties",
126  action="store_true",
127  dest="useTrackWeights")
128 parser.add_option("--errorFromRMS",
129  help="if invoked, determine residuals uncertainties from the RMS of the residuals distribution",
130  action="store_true",
131  dest="errorFromRMS")
132 parser.add_option("--minTracksPerOverlap",
133  help="minimum number of tracks needed for an overlap constraint to be valid",
134  type="string",
135  default="10",
136  dest="minTracksPerOverlap")
137 parser.add_option("--slopeFromTrackRefit",
138  help="if invoked, determine direction of tracklets by refitting track to all other stations",
139  action="store_true",
140  dest="slopeFromTrackRefit")
141 parser.add_option("--minStationsInTrackRefits",
142  help="minimum number of stations in a full track refit (slopeFromTrackRefit)",
143  type="string",
144  default="2",
145  dest="minStationsInTrackRefits")
146 parser.add_option("--inputInBlocks",
147  help="if invoked, assume that INPUTFILES provides a list of files already groupped into job blocks, -j has no effect in that case",
148  action="store_true",
149  dest="inputInBlocks")
150 parser.add_option("--json",
151  help="If present with JSON file as argument, use JSON file for good lumi mask. "+\
152  "The latest JSON file is available at /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions10/7TeV/StreamExpress/",
153  type="string",
154  default="",
155  dest="json")
156 
157 if len(sys.argv) < 5:
158  raise SystemError("Too few arguments.\n\n"+parser.format_help())
159 
160 DIRNAME = sys.argv[1]
161 PATTERN = re.split("\s+", sys.argv[2])
162 INITIALGEOM = sys.argv[3]
163 INPUTFILES = sys.argv[4]
164 
165 options, args = parser.parse_args(sys.argv[5:])
166 user_mail = options.user_mail
167 globaltag = options.globaltag
168 photogrammetry = options.photogrammetry
169 photogrammetryOnlyholes = options.photogrammetryOnlyholes
170 photogrammetryOnlyOnePerRing = options.photogrammetryOnlyOnePerRing
171 photogrammetryScale = options.photogrammetryScale
172 slm = options.slm
173 fillME11holes = options.fillME11holes
174 disks = options.disks
175 minP = options.minP
176 minHitsPerChamber = options.minHitsPerChamber
177 maxdrdz = options.maxdrdz
178 maxRedChi2 = options.maxRedChi2
179 fiducial = options.fiducial
180 useHitWeights = options.useHitWeights
181 truncateSlopeResid = options.truncateSlopeResid
182 truncateOffsetResid = options.truncateOffsetResid
183 combineME11 = options.combineME11
184 useTrackWeights = options.useTrackWeights
185 errorFromRMS = options.errorFromRMS
186 minTracksPerOverlap = options.minTracksPerOverlap
187 slopeFromTrackRefit = options.slopeFromTrackRefit
188 minStationsInTrackRefits = options.minStationsInTrackRefits
189 
190 if options.inputInBlocks: inputInBlocks = "--inputInBlocks"
191 json_file = options.json
192 
193 
194 fileNames=[]
195 fileNamesBlocks=[]
196 execfile(INPUTFILES)
197 njobs = options.subjobs
198 if (options.inputInBlocks):
199  njobs = len(fileNamesBlocks)
200  if njobs==0:
201  print("while --inputInBlocks is specified, the INPUTFILES has no blocks!")
202  sys.exit()
203 
204 stepsize = int(math.ceil(1.*len(fileNames)/options.subjobs))
205 pwd = str(os.getcwd())
206 
207 bsubfile = ["#!/bin/sh", ""]
208 bsubnames = []
209 last_align = None
210 
211 directory = ""
212 for i, mode in enumerate(PATTERN):
213  iteration = i+1
214  if iteration == 1:
215  inputdb = INITIALGEOM
216  inputdbdir = directory[:]
217  else:
218  inputdb = director + ".db"
219  inputdbdir = directory[:]
220 
221  directory = "%s%02d/" % (DIRNAME, iteration)
222  director = directory[:-1]
223  os.system("rm -rf %s; mkdir %s" % (directory, directory))
224  os.system("cp gatherBH_cfg.py %s" % directory)
225  os.system("cp alignBH_cfg.py %s" % directory)
226 
227  bsubfile.append("cd %s" % directory)
228 
229  constraints = """echo \"\" > constraints_cff.py
230 """
231  if photogrammetry and (mode == "phipos" or mode == "phiz"):
232  diskswitch = ""
233  if disks: diskswitch = "--disks "
234 
235  constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
236 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py
237 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/Photogrammetry2007.%(mode)s PGFrame --scaleErrors %(photogrammetryScale)s %(diskswitch)s>> constraints_cff.py
238 """ % vars()
239 
240  elif photogrammetryOnlyholes and (mode == "phipos" or mode == "phiz"):
241  diskswitch = ""
242  if disks: diskswitch = "--disks "
243 
244  constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
245 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py
246 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/Photogrammetry2007_onlyOct2010holes.%(mode)s PGFrame --scaleErrors %(photogrammetryScale)s %(diskswitch)s>> constraints_cff.py
247 """ % vars()
248 
249  elif photogrammetryOnlyOnePerRing and (mode == "phipos" or mode == "phiz"):
250  diskswitch = ""
251  if disks: diskswitch = "--disks "
252 
253  constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
254 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py
255 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/Photogrammetry2007_onlyOnePerRing.%(mode)s PGFrame --scaleErrors %(photogrammetryScale)s %(diskswitch)s>> constraints_cff.py
256 """ % vars()
257 
258  if slm and (mode == "phipos" or "phiz"):
259  diskswitch = ""
260  if disks: diskswitch = "--disks "
261 
262  constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
263 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py
264 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/SLM_test.%(mode)s SLMFrame --scaleErrors 1.0 %(diskswitch)s>> constraints_cff.py
265 """ % vars()
266 
267  if fillME11holes and (mode == "phipos" or mode == "phiz"):
268  diskswitch = ""
269  if disks: diskswitch = "--disks "
270 
271  constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
272 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py
273 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/CollisionsOct2010_ME11holes.%(mode)s TKFrame --scaleErrors 1. %(diskswitch)s>> constraints_cff.py
274 """ % vars()
275 
276  for jobnumber in range(njobs):
277  gather_fileName = "%sgather%03d.sh" % (directory, jobnumber)
278  if not options.inputInBlocks:
279  inputfiles = " ".join(fileNames[jobnumber*stepsize:(jobnumber+1)*stepsize])
280  else:
281  inputfiles = " ".join(fileNamesBlocks[jobnumber])
282 
283  if len(inputfiles) > 0:
284  file(gather_fileName, "w").write("""#/bin/sh
285 # %(commandline)s
286 
287 export ALIGNMENT_CAFDIR=`pwd`
288 
289 cd %(pwd)s
290 
291 export SCRAM_ARCH=slc5_amd64_gcc434
292 echo INFO: SCRAM_ARCH $SCRAM_ARCH
293 
294 eval `scramv1 run -sh`
295 
296 source /afs/cern.ch/cms/caf/setup.sh
297 echo INFO: CMS_PATH $CMS_PATH
298 echo INFO: STAGE_SVCCLASS $STAGE_SVCCLASS
299 echo INFO: STAGER_TRACE $STAGER_TRACE
300 
301 export ALIGNMENT_AFSDIR=`pwd`
302 
303 export ALIGNMENT_INPUTFILES='%(inputfiles)s'
304 export ALIGNMENT_ITERATION=%(iteration)d
305 export ALIGNMENT_MODE=%(mode)s
306 export ALIGNMENT_JOBNUMBER=%(jobnumber)d
307 export ALIGNMENT_INPUTDB=%(inputdb)s
308 export ALIGNMENT_GLOBALTAG=%(globaltag)s
309 export ALIGNMENT_PHOTOGRAMMETRY='%(photogrammetry)s or %(photogrammetryOnlyholes)s or %(photogrammetryOnlyOnePerRing)s'
310 export ALIGNMENT_SLM=%(slm)s
311 export ALIGNMENT_FILLME11HOLES='%(fillME11holes)s'
312 export ALIGNMENT_DISKS=%(disks)s
313 export ALIGNMENT_minP=%(minP)s
314 export ALIGNMENT_minHitsPerChamber=%(minHitsPerChamber)s
315 export ALIGNMENT_maxdrdz=%(maxdrdz)s
316 export ALIGNMENT_maxRedChi2=%(maxRedChi2)s
317 export ALIGNMENT_fiducial=%(fiducial)s
318 export ALIGNMENT_useHitWeights=%(useHitWeights)s
319 export ALIGNMENT_truncateSlopeResid=%(truncateSlopeResid)s
320 export ALIGNMENT_truncateOffsetResid=%(truncateOffsetResid)s
321 export ALIGNMENT_combineME11=%(combineME11)s
322 export ALIGNMENT_useTrackWeights=%(useTrackWeights)s
323 export ALIGNMENT_errorFromRMS=%(errorFromRMS)s
324 export ALIGNMENT_minTracksPerOverlap=%(minTracksPerOverlap)s
325 export ALIGNMENT_slopeFromTrackRefit=%(slopeFromTrackRefit)s
326 export ALIGNMENT_minStationsInTrackRefits=%(minStationsInTrackRefits)s
327 
328 cp -f %(directory)sgatherBH_cfg.py %(inputdbdir)s%(inputdb)s inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
329 cd $ALIGNMENT_CAFDIR/
330 
331 %(constraints)s
332 
333 ls -l
334 cmsRun gatherBH_cfg.py
335 ls -l
336 cp -f *.tmp *.root $ALIGNMENT_AFSDIR/%(directory)s
337 """ % vars())
338  os.system("chmod +x %s" % gather_fileName)
339  bsubfile.append("echo %sgather%03d.sh" % (directory, jobnumber))
340 
341  if last_align is None: waiter = ""
342  else: waiter = "-w \"ended(%s)\"" % last_align
343  if options.big: queue = "cmscaf1nd"
344  else: queue = "cmscaf1nh"
345 
346  if user_mail: bsubfile.append("bsub -R \"type==SLC5_64\" -q %s -J \"%s_gather%03d\" -u %s %s gather%03d.sh" % (queue, director, jobnumber, user_mail, waiter, jobnumber))
347  else: bsubfile.append("bsub -R \"type==SLC5_64\" -q %s -J \"%s_gather%03d\" %s gather%03d.sh" % (queue, director, jobnumber, waiter, jobnumber))
348 
349  bsubnames.append("ended(%s_gather%03d)" % (director, jobnumber))
350 
351  file("%sconvert-db-to-xml_cfg.py" % directory, "w").write("""from Alignment.MuonAlignment.convertSQLitetoXML_cfg import *
352 process.PoolDBESSource.connect = \"sqlite_file:%(directory)s%(director)s.db\"
353 process.MuonGeometryDBConverter.outputXML.fileName = \"%(directory)s%(director)s.xml\"
354 process.MuonGeometryDBConverter.outputXML.relativeto = \"ideal\"
355 process.MuonGeometryDBConverter.outputXML.suppressDTChambers = True
356 process.MuonGeometryDBConverter.outputXML.suppressDTSuperLayers = True
357 process.MuonGeometryDBConverter.outputXML.suppressDTLayers = True
358 process.MuonGeometryDBConverter.outputXML.suppressCSCChambers = False
359 process.MuonGeometryDBConverter.outputXML.suppressCSCLayers = True
360 
361 process.MuonGeometryDBConverter.getAPEs = True
362 process.PoolDBESSource.toGet = cms.VPSet(
363  cms.PSet(record = cms.string(\"DTAlignmentRcd\"), tag = cms.string(\"DTAlignmentRcd\")),
364  cms.PSet(record = cms.string(\"DTAlignmentErrorExtendedRcd\"), tag = cms.string(\"DTAlignmentErrorExtendedRcd\")),
365  cms.PSet(record = cms.string(\"CSCAlignmentRcd\"), tag = cms.string(\"CSCAlignmentRcd\")),
366  cms.PSet(record = cms.string(\"CSCAlignmentErrorExtendedRcd\"), tag = cms.string(\"CSCAlignmentErrorExtendedRcd\")),
367  )
368 """ % vars())
369 
370  constraints += """\ncp -f constraints_cff.py $ALIGNMENT_AFSDIR/%(directory)sconstraints_cff.py""" % vars()
371 
372  file("%salign.sh" % directory, "w").write("""#!/bin/sh
373 # %(commandline)s
374 
375 export ALIGNMENT_CAFDIR=`pwd`
376 
377 cd %(pwd)s
378 
379 export SCRAM_ARCH=slc5_amd64_gcc434
380 echo INFO: SCRAM_ARCH $SCRAM_ARCH
381 
382 eval `scramv1 run -sh`
383 
384 source /afs/cern.ch/cms/caf/setup.sh
385 echo INFO: CMS_PATH $CMS_PATH
386 echo INFO: STAGE_SVCCLASS $STAGE_SVCCLASS
387 echo INFO: STAGER_TRACE $STAGER_TRACE
388 
389 export ALIGNMENT_AFSDIR=`pwd`
390 
391 export ALIGNMENT_ITERATION=%(iteration)d
392 export ALIGNMENT_MODE=%(mode)s
393 export ALIGNMENT_INPUTDB=%(inputdb)s
394 export ALIGNMENT_GLOBALTAG=%(globaltag)s
395 export ALIGNMENT_PHOTOGRAMMETRY='%(photogrammetry)s or %(photogrammetryOnlyholes)s or %(photogrammetryOnlyOnePerRing)s'
396 export ALIGNMENT_SLM=%(slm)s
397 export ALIGNMENT_FILLME11HOLES='%(fillME11holes)s'
398 export ALIGNMENT_DISKS=%(disks)s
399 export ALIGNMENT_minP=%(minP)s
400 export ALIGNMENT_minHitsPerChamber=%(minHitsPerChamber)s
401 export ALIGNMENT_maxdrdz=%(maxdrdz)s
402 export ALIGNMENT_maxRedChi2=%(maxRedChi2)s
403 export ALIGNMENT_fiducial=%(fiducial)s
404 export ALIGNMENT_useHitWeights=%(useHitWeights)s
405 export ALIGNMENT_truncateSlopeResid=%(truncateSlopeResid)s
406 export ALIGNMENT_truncateOffsetResid=%(truncateOffsetResid)s
407 export ALIGNMENT_combineME11=%(combineME11)s
408 export ALIGNMENT_useTrackWeights=%(useTrackWeights)s
409 export ALIGNMENT_errorFromRMS=%(errorFromRMS)s
410 export ALIGNMENT_minTracksPerOverlap=%(minTracksPerOverlap)s
411 export ALIGNMENT_slopeFromTrackRefit=%(slopeFromTrackRefit)s
412 export ALIGNMENT_minStationsInTrackRefits=%(minStationsInTrackRefits)s
413 
414 cp -f %(directory)salignBH_cfg.py %(directory)sconvert-db-to-xml_cfg.py %(inputdbdir)s%(inputdb)s %(directory)s*.tmp inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
415 cd $ALIGNMENT_CAFDIR/
416 export ALIGNMENT_ALIGNMENTTMP=`ls alignment*.tmp`
417 
418 %(constraints)s
419 
420 ls -l
421 cmsRun alignBH_cfg.py
422 cp -f report.py $ALIGNMENT_AFSDIR/%(directory)s%(director)s_report.py
423 cp -f outputdb.db $ALIGNMENT_AFSDIR/%(directory)s%(director)s.db
424 cp -f plotting.root $ALIGNMENT_AFSDIR/%(directory)s%(director)s.root
425 
426 cd $ALIGNMENT_AFSDIR
427 cmsRun %(directory)sconvert-db-to-xml_cfg.py
428 
429 export ALIGNMENT_PLOTTINGTMP=`ls %(directory)splotting0*.root 2> /dev/null`
430 if [ \"zzz$ALIGNMENT_PLOTTINGTMP\" != \"zzz\" ]; then
431  hadd -f1 %(directory)s%(director)s_plotting.root %(directory)splotting0*.root
432  #if [ $? == 0 ] && [ \"$ALIGNMENT_CLEANUP\" == \"True\" ]; then rm %(directory)splotting0*.root; fi
433 fi
434 
435 """ % vars())
436  os.system("chmod +x %salign.sh" % directory)
437 
438  bsubfile.append("echo %salign.sh" % directory)
439 
440  if user_mail: bsubfile.append("bsub -R \"type==SLC5_64\" -q cmscaf1nd -J \"%s_align\" -u %s -w \"%s\" align.sh" % (director, user_mail, " && ".join(bsubnames)))
441  else: bsubfile.append("bsub -R \"type==SLC5_64\" -q cmscaf1nd -J \"%s_align\" -w \"%s\" align.sh" % (director, " && ".join(bsubnames)))
442 
443  bsubfile.append("cd ..")
444  bsubnames = []
445  last_align = "%s_align" % director
446 
447 bsubfile.append("")
448 file(options.submitJobs, "w").write("\n".join(bsubfile))
449 os.system("chmod +x %s" % options.submitJobs)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
#define str(s)