3 import os, sys, re, optparse, math
6 for i
in range(len(copyargs)):
9 if copyargs[i].
find(
" ") != -1:
10 copyargs[i] =
"\"%s\"" % copyargs[i]
11 commandline =
" ".
join(copyargs)
13 usage =
"""./%prog DIRNAME PATTERN INITIALGEOM INPUTFILES [options]
15 Creates (overwrites) a directory for each word in PATTERN and creates
16 (overwrites) submitJobs.sh with the submission sequence and
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, CSCAlignmentErrorRcd
23 INPUTFILES Python file defining 'fileNames', a list of input files as
26 parser = optparse.OptionParser(usage)
27 parser.add_option(
"-j",
"--jobs",
28 help=
"approximate number of \"gather\" 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",
35 default=
"submitJobs.sh",
37 parser.add_option(
"-b",
"--big",
38 help=
"if invoked, subjobs will also be run on cmscaf1nd",
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",
45 parser.add_option(
"--globalTag",
46 help=
"GlobalTag for calibration conditions",
48 default=
"GR_R_42_V14::All",
50 parser.add_option(
"--photogrammetry",
51 help=
"if invoked, alignment will be constrained to photogrammetry",
53 dest=
"photogrammetry")
54 parser.add_option(
"--photogrammetryOnlyholes",
55 help=
"if invoked, only missing data will be constrained to photogrammetry",
57 dest=
"photogrammetryOnlyholes")
58 parser.add_option(
"--photogrammetryOnlyOnePerRing",
59 help=
"if invoked, only one chamber per ring will be constrained to photogrammetry",
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",
66 dest=
"photogrammetryScale")
67 parser.add_option(
"--slm",
68 help=
"if invoked, apply SLM constraint",
71 parser.add_option(
"--fillME11holes",
72 help=
"use CollisionsOct2010 data to fill holes in ME1/1",
75 parser.add_option(
"--disks",
76 help=
"align whole disks, rather than individual rings",
80 parser.add_option(
"--minP",
81 help=
"minimum track momentum (measured via radial component of fringe fields)",
85 parser.add_option(
"--minHitsPerChamber",
86 help=
"minimum number of hits per chamber",
89 dest=
"minHitsPerChamber")
90 parser.add_option(
"--maxdrdz",
91 help=
"maximum dr/dz of tracklets (anti-cosmic cut)",
95 parser.add_option(
"--maxRedChi2",
96 help=
"maximum reduced chi^2 of tracks",
100 parser.add_option(
"--fiducial",
101 help=
"if invoked, select only segments within the good region of the chamber (for all 6 layers)",
104 parser.add_option(
"--useHitWeights",
105 help=
"if invoked, use hit weights in tracklet fits",
107 dest=
"useHitWeights")
108 parser.add_option(
"--truncateSlopeResid",
109 help=
"maximum allowed slope residual in mrad (like the histograms in a phiy job)",
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)",
117 dest=
"truncateOffsetResid")
118 parser.add_option(
"--combineME11",
119 help=
"if invoked, combine ME1/1a and ME1/1b chambers",
122 parser.add_option(
"--useTrackWeights",
123 help=
"if invoked, weight residuals by track uncertainties",
125 dest=
"useTrackWeights")
126 parser.add_option(
"--errorFromRMS",
127 help=
"if invoked, determine residuals uncertainties from the RMS of the residuals distribution",
130 parser.add_option(
"--minTracksPerOverlap",
131 help=
"minimum number of tracks needed for an overlap constraint to be valid",
134 dest=
"minTracksPerOverlap")
135 parser.add_option(
"--slopeFromTrackRefit",
136 help=
"if invoked, determine direction of tracklets by refitting track to all other stations",
138 dest=
"slopeFromTrackRefit")
139 parser.add_option(
"--minStationsInTrackRefits",
140 help=
"minimum number of stations in a full track refit (slopeFromTrackRefit)",
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",
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/",
155 if len(sys.argv) < 5:
156 raise SystemError,
"Too few arguments.\n\n"+parser.format_help()
158 DIRNAME = sys.argv[1]
159 PATTERN = re.split(
"\s+", sys.argv[2])
160 INITIALGEOM = sys.argv[3]
161 INPUTFILES = sys.argv[4]
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
171 fillME11holes = options.fillME11holes
172 disks = options.disks
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
188 if options.inputInBlocks: inputInBlocks =
"--inputInBlocks"
189 json_file = options.json
195 njobs = options.subjobs
196 if (options.inputInBlocks):
197 njobs = len(fileNamesBlocks)
199 print "while --inputInBlocks is specified, the INPUTFILES has no blocks!"
202 stepsize = int(math.ceil(1.*len(fileNames)/options.subjobs))
203 pwd = str(os.getcwdu())
205 bsubfile = [
"#!/bin/sh",
""]
210 for i, mode
in enumerate(PATTERN):
213 inputdb = INITIALGEOM
214 inputdbdir = directory[:]
216 inputdb = director +
".db"
217 inputdbdir = directory[:]
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)
225 bsubfile.append(
"cd %s" % directory)
227 constraints =
"""echo \"\" > constraints_cff.py
229 if photogrammetry
and (mode ==
"phipos" or mode ==
"phiz"):
231 if disks: diskswitch =
"--disks "
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
238 elif photogrammetryOnlyholes
and (mode ==
"phipos" or mode ==
"phiz"):
240 if disks: diskswitch =
"--disks "
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
247 elif photogrammetryOnlyOnePerRing
and (mode ==
"phipos" or mode ==
"phiz"):
249 if disks: diskswitch =
"--disks "
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
256 if slm
and (mode ==
"phipos" or "phiz"):
258 if disks: diskswitch =
"--disks "
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
265 if fillME11holes
and (mode ==
"phipos" or mode ==
"phiz"):
267 if disks: diskswitch =
"--disks "
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
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])
279 inputfiles =
" ".
join(fileNamesBlocks[jobnumber])
281 if len(inputfiles) > 0:
282 file(gather_fileName,
"w").
write(
"""#/bin/sh
285 export ALIGNMENT_CAFDIR=`pwd`
289 export SCRAM_ARCH=slc5_amd64_gcc434
290 echo INFO: SCRAM_ARCH $SCRAM_ARCH
292 eval `scramv1 run -sh`
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
299 export ALIGNMENT_AFSDIR=`pwd`
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
326 cp -f %(directory)sgatherBH_cfg.py %(inputdbdir)s%(inputdb)s inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
327 cd $ALIGNMENT_CAFDIR/
332 cmsRun gatherBH_cfg.py
334 cp -f *.tmp *.root $ALIGNMENT_AFSDIR/%(directory)s
336 os.system(
"chmod +x %s" % gather_fileName)
337 bsubfile.append(
"echo %sgather%03d.sh" % (directory, jobnumber))
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"
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))
347 bsubnames.append(
"ended(%s_gather%03d)" % (director, jobnumber))
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
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(\"DTAlignmentErrorRcd\"), tag = cms.string(\"DTAlignmentErrorRcd\")),
363 cms.PSet(record = cms.string(\"CSCAlignmentRcd\"), tag = cms.string(\"CSCAlignmentRcd\")),
364 cms.PSet(record = cms.string(\"CSCAlignmentErrorRcd\"), tag = cms.string(\"CSCAlignmentErrorRcd\")),
368 constraints +=
"""\ncp -f constraints_cff.py $ALIGNMENT_AFSDIR/%(directory)sconstraints_cff.py""" % vars()
370 file(
"%salign.sh" % directory,
"w").
write(
"""#!/bin/sh
373 export ALIGNMENT_CAFDIR=`pwd`
377 export SCRAM_ARCH=slc5_amd64_gcc434
378 echo INFO: SCRAM_ARCH $SCRAM_ARCH
380 eval `scramv1 run -sh`
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
387 export ALIGNMENT_AFSDIR=`pwd`
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
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`
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
425 cmsRun %(directory)sconvert-db-to-xml_cfg.py
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
434 os.system(
"chmod +x %salign.sh" % directory)
436 bsubfile.append(
"echo %salign.sh" % directory)
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)))
441 bsubfile.append(
"cd ..")
443 last_align =
"%s_align" % director
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)
static std::string join(char **cmd)