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