3 from __future__
import print_function
4 from builtins
import range
5 import os, sys, re, optparse, math
8 for i
in range(len(copyargs)):
11 if copyargs[i].
find(
" ") != -1:
12 copyargs[i] =
"\"%s\"" % copyargs[i]
13 commandline =
" ".
join(copyargs)
15 usage =
"""./%prog DIRNAME PATTERN INITIALGEOM INPUTFILES [options]
17 Creates (overwrites) a directory for each word in PATTERN and creates
18 (overwrites) submitJobs.sh with the submission sequence and
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
28 parser = optparse.OptionParser(usage)
29 parser.add_option(
"-j",
"--jobs",
30 help=
"approximate number of \"gather\" 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",
37 default=
"submitJobs.sh",
39 parser.add_option(
"-b",
"--big",
40 help=
"if invoked, subjobs will also be run on cmscaf1nd",
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",
47 parser.add_option(
"--globalTag",
48 help=
"GlobalTag for calibration conditions",
50 default=
"GR_R_42_V14::All",
52 parser.add_option(
"--photogrammetry",
53 help=
"if invoked, alignment will be constrained to photogrammetry",
55 dest=
"photogrammetry")
56 parser.add_option(
"--photogrammetryOnlyholes",
57 help=
"if invoked, only missing data will be constrained to photogrammetry",
59 dest=
"photogrammetryOnlyholes")
60 parser.add_option(
"--photogrammetryOnlyOnePerRing",
61 help=
"if invoked, only one chamber per ring will be constrained to photogrammetry",
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",
68 dest=
"photogrammetryScale")
69 parser.add_option(
"--slm",
70 help=
"if invoked, apply SLM constraint",
73 parser.add_option(
"--fillME11holes",
74 help=
"use CollisionsOct2010 data to fill holes in ME1/1",
77 parser.add_option(
"--disks",
78 help=
"align whole disks, rather than individual rings",
82 parser.add_option(
"--minP",
83 help=
"minimum track momentum (measured via radial component of fringe fields)",
87 parser.add_option(
"--minHitsPerChamber",
88 help=
"minimum number of hits per chamber",
91 dest=
"minHitsPerChamber")
92 parser.add_option(
"--maxdrdz",
93 help=
"maximum dr/dz of tracklets (anti-cosmic cut)",
97 parser.add_option(
"--maxRedChi2",
98 help=
"maximum reduced chi^2 of tracks",
102 parser.add_option(
"--fiducial",
103 help=
"if invoked, select only segments within the good region of the chamber (for all 6 layers)",
106 parser.add_option(
"--useHitWeights",
107 help=
"if invoked, use hit weights in tracklet fits",
109 dest=
"useHitWeights")
110 parser.add_option(
"--truncateSlopeResid",
111 help=
"maximum allowed slope residual in mrad (like the histograms in a phiy job)",
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)",
119 dest=
"truncateOffsetResid")
120 parser.add_option(
"--combineME11",
121 help=
"if invoked, combine ME1/1a and ME1/1b chambers",
124 parser.add_option(
"--useTrackWeights",
125 help=
"if invoked, weight residuals by track uncertainties",
127 dest=
"useTrackWeights")
128 parser.add_option(
"--errorFromRMS",
129 help=
"if invoked, determine residuals uncertainties from the RMS of the residuals distribution",
132 parser.add_option(
"--minTracksPerOverlap",
133 help=
"minimum number of tracks needed for an overlap constraint to be valid",
136 dest=
"minTracksPerOverlap")
137 parser.add_option(
"--slopeFromTrackRefit",
138 help=
"if invoked, determine direction of tracklets by refitting track to all other stations",
140 dest=
"slopeFromTrackRefit")
141 parser.add_option(
"--minStationsInTrackRefits",
142 help=
"minimum number of stations in a full track refit (slopeFromTrackRefit)",
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",
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/",
157 if len(sys.argv) < 5:
158 raise SystemError(
"Too few arguments.\n\n"+parser.format_help())
160 DIRNAME = sys.argv[1]
161 PATTERN = re.split(
"\s+", sys.argv[2])
162 INITIALGEOM = sys.argv[3]
163 INPUTFILES = sys.argv[4]
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
173 fillME11holes = options.fillME11holes
174 disks = options.disks
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
190 if options.inputInBlocks: inputInBlocks =
"--inputInBlocks"
191 json_file = options.json
197 njobs = options.subjobs
198 if (options.inputInBlocks):
199 njobs = len(fileNamesBlocks)
201 print(
"while --inputInBlocks is specified, the INPUTFILES has no blocks!")
204 stepsize = int(math.ceil(1.*len(fileNames)/options.subjobs))
207 bsubfile = [
"#!/bin/sh",
""]
212 for i, mode
in enumerate(PATTERN):
215 inputdb = INITIALGEOM
216 inputdbdir = directory[:]
218 inputdb = director +
".db"
219 inputdbdir = directory[:]
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)
227 bsubfile.append(
"cd %s" % directory)
229 constraints =
"""echo \"\" > constraints_cff.py
231 if photogrammetry
and (mode ==
"phipos" or mode ==
"phiz"):
233 if disks: diskswitch =
"--disks "
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
240 elif photogrammetryOnlyholes
and (mode ==
"phipos" or mode ==
"phiz"):
242 if disks: diskswitch =
"--disks "
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
249 elif photogrammetryOnlyOnePerRing
and (mode ==
"phipos" or mode ==
"phiz"):
251 if disks: diskswitch =
"--disks "
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
258 if slm
and (mode ==
"phipos" or "phiz"):
260 if disks: diskswitch =
"--disks "
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
267 if fillME11holes
and (mode ==
"phipos" or mode ==
"phiz"):
269 if disks: diskswitch =
"--disks "
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
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])
281 inputfiles =
" ".
join(fileNamesBlocks[jobnumber])
283 if len(inputfiles) > 0:
284 file(gather_fileName,
"w").
write(
"""#/bin/sh
287 export ALIGNMENT_CAFDIR=`pwd`
291 export SCRAM_ARCH=slc5_amd64_gcc434
292 echo INFO: SCRAM_ARCH $SCRAM_ARCH
294 eval `scramv1 run -sh`
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
301 export ALIGNMENT_AFSDIR=`pwd`
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
328 cp -f %(directory)sgatherBH_cfg.py %(inputdbdir)s%(inputdb)s inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
329 cd $ALIGNMENT_CAFDIR/
334 cmsRun gatherBH_cfg.py
336 cp -f *.tmp *.root $ALIGNMENT_AFSDIR/%(directory)s
338 os.system(
"chmod +x %s" % gather_fileName)
339 bsubfile.append(
"echo %sgather%03d.sh" % (directory, jobnumber))
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"
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))
349 bsubnames.append(
"ended(%s_gather%03d)" % (director, jobnumber))
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
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\")),
370 constraints +=
"""\ncp -f constraints_cff.py $ALIGNMENT_AFSDIR/%(directory)sconstraints_cff.py""" % vars()
372 file(
"%salign.sh" % directory,
"w").
write(
"""#!/bin/sh
375 export ALIGNMENT_CAFDIR=`pwd`
379 export SCRAM_ARCH=slc5_amd64_gcc434
380 echo INFO: SCRAM_ARCH $SCRAM_ARCH
382 eval `scramv1 run -sh`
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
389 export ALIGNMENT_AFSDIR=`pwd`
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
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`
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
427 cmsRun %(directory)sconvert-db-to-xml_cfg.py
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
436 os.system(
"chmod +x %salign.sh" % directory)
438 bsubfile.append(
"echo %salign.sh" % directory)
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)))
443 bsubfile.append(
"cd ..")
445 last_align =
"%s_align" % director
449 os.system(
"chmod +x %s" % options.submitJobs)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
const uint16_t range(const Frame &aFrame)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
static std::string join(char **cmd)