CMS 3D CMS Logo

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