3 import os, sys, 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)
15 usage =
"""./%(prog)s DIRNAME ITERATIONS INITIALGEOM INPUTFILES [options]
17 Creates (overwrites) a directory for each of the iterations and creates (overwrites)
18 submitJobs.sh with the submission sequence and dependencies.
20 DIRNAME directories will be named DIRNAME01, DIRNAME02...
21 ITERATIONS number of iterations
22 INITIALGEOM SQLite file containing muon geometry with tag names
23 DTAlignmentRcd, DTAlignmentErrorRcd, CSCAlignmentRcd, CSCAlignmentErrorRcd
24 INPUTFILES Python file defining 'fileNames', a list of input files as
25 strings (create with findQualityFiles.py)""" % vars()
27 parser = optparse.OptionParser(usage)
28 parser.add_option(
"--validationLabel",
29 help=
"[REQUIRED] the label to be used to mark a run; the results will be put into a LABEL_DATESTAMP.tgz tarball",
32 dest=
"validationLabel")
33 parser.add_option(
"-j",
"--jobs",
34 help=
"approximate number of \"gather\" subjobs",
38 parser.add_option(
"-s",
"--submitJobs",
39 help=
"alternate name of submitJobs.sh script (please include .sh extension); a file with this name will be OVERWRITTEN",
41 default=
"submitJobs.sh",
43 parser.add_option(
"-b",
"--big",
44 help=
"if invoked, subjobs will also be run on cmscaf1nd",
47 parser.add_option(
"--globalTag",
48 help=
"GlobalTag for alignment/calibration conditions (typically all conditions except muon and tracker alignment)",
50 default=
"CRAFT0831X_V1::All",
52 parser.add_option(
"--trackerconnect",
53 help=
"connect string for tracker alignment (frontier://... or sqlite_file:...)",
56 dest=
"trackerconnect")
57 parser.add_option(
"--trackeralignment",
58 help=
"name of TrackerAlignmentRcd tag",
61 dest=
"trackeralignment")
62 parser.add_option(
"--trackerAPEconnect",
63 help=
"connect string for tracker APEs (frontier://... or sqlite_file:...)",
66 dest=
"trackerAPEconnect")
67 parser.add_option(
"--trackerAPE",
68 help=
"name of TrackerAlignmentErrorRcd tag (tracker APEs)",
70 default=
"AlignmentErrors",
72 parser.add_option(
"--gprcdconnect",
73 help=
"connect string for GlobalPositionRcd (frontier://... or sqlite_file:...)",
77 parser.add_option(
"--gprcd",
78 help=
"name of GlobalPositionRcd tag",
80 default=
"GlobalPosition",
82 parser.add_option(
"--iscosmics",
83 help=
"if invoked, use cosmic track refitter instead of the standard one",
86 parser.add_option(
"--station123params",
87 help=
"alignable parameters for DT stations 1, 2, 3 (see SWGuideAlignmentAlgorithms#Selection_of_what_to_align)",
90 dest=
"station123params")
91 parser.add_option(
"--station4params",
92 help=
"alignable parameters for DT station 4",
95 dest=
"station4params")
96 parser.add_option(
"--cscparams",
97 help=
"alignable parameters for CSC chambers",
101 parser.add_option(
"--minTrackPt",
102 help=
"minimum allowed track transverse momentum (in GeV)",
106 parser.add_option(
"--maxTrackPt",
107 help=
"maximum allowed track transverse momentum (in GeV)",
111 parser.add_option(
"--minTrackP",
112 help=
"minimum allowed track momentum (in GeV)",
116 parser.add_option(
"--maxTrackP",
117 help=
"maximum allowed track momentum (in GeV)",
121 parser.add_option(
"--minTrackerHits",
122 help=
"minimum number of tracker hits",
125 dest=
"minTrackerHits")
126 parser.add_option(
"--maxTrackerRedChi2",
127 help=
"maximum tracker chi^2 per degrees of freedom",
130 dest=
"maxTrackerRedChi2")
131 parser.add_option(
"--notAllowTIDTEC",
132 help=
"if invoked, do not allow tracks that pass through the tracker's TID||TEC region (not recommended)",
134 dest=
"notAllowTIDTEC")
135 parser.add_option(
"--twoBin",
136 help=
"if invoked, apply the \"two-bin method\" to control charge-antisymmetric errors",
139 parser.add_option(
"--weightAlignment",
140 help=
"if invoked, segments will be weighted by ndf/chi^2 in the alignment",
142 dest=
"weightAlignment")
143 parser.add_option(
"--minAlignmentSegments",
144 help=
"minimum number of segments required to align a chamber",
147 dest=
"minAlignmentHits")
148 parser.add_option(
"--notCombineME11",
149 help=
"if invoked, treat ME1/1a and ME1/1b as separate objects",
151 dest=
"notCombineME11")
152 parser.add_option(
"--maxEvents",
153 help=
"maximum number of events",
157 parser.add_option(
"--skipEvents",
158 help=
"number of events to be skipped",
162 parser.add_option(
"--maxResSlopeY",
163 help=
"maximum residual slope y component",
167 parser.add_option(
"--ring2only",
168 help=
"if invoked, use only ring 2 results to align all rings in corresponding disks",
171 parser.add_option(
"--createMapNtuple",
172 help=
"if invoked while mapplots are switched on, a special ntuple would be created",
174 dest=
"createMapNtuple")
175 parser.add_option(
"--inputInBlocks",
176 help=
"if invoked, assume that INPUTFILES provides a list of files already groupped into job blocks, -j has no effect in that case",
178 dest=
"inputInBlocks")
179 parser.add_option(
"--json",
180 help=
"If present with JSON file as argument, use JSON file for good lumi mask. "+\
181 "The latest JSON file is available at /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions11/7TeV/Prompt/",
185 parser.add_option(
"--preFilter",
186 help=
"if invoked, MuonAlignmentPreFilter module would be invoked in the Path's beginning. Can significantly speed up gather jobs.",
189 parser.add_option(
"--useTrackerMuons",
190 help=
"use tracker muons approach instead of the default global muons tracks based one",
192 dest=
"useTrackerMuons")
193 parser.add_option(
"--muonCollectionTag",
194 help=
"InputTag of muons collection to use in tracker muons based approach",
197 dest=
"muonCollectionTag")
198 parser.add_option(
"--maxDxy",
199 help=
"maximum track impact parameter with relation to beamline",
203 parser.add_option(
"--minNCrossedChambers",
204 help=
"minimum number of muon chambers that a track is required to cross",
207 dest=
"minNCrossedChambers")
209 if len(sys.argv) < 5:
210 raise SystemError,
"Too few arguments.\n\n"+parser.format_help()
212 DIRNAME = sys.argv[1]
213 ITERATIONS = int(sys.argv[2])
214 INITIALGEOM = sys.argv[3]
215 INPUTFILES = sys.argv[4]
217 options, args = parser.parse_args(sys.argv[5:])
218 globaltag = options.globaltag
219 trackerconnect = options.trackerconnect
220 trackeralignment = options.trackeralignment
221 trackerAPEconnect = options.trackerAPEconnect
222 trackerAPE = options.trackerAPE
223 gprcdconnect = options.gprcdconnect
224 gprcd = options.gprcd
225 iscosmics = str(options.iscosmics)
226 station123params = options.station123params
227 station4params = options.station4params
228 cscparams = options.cscparams
229 muonCollectionTag = options.muonCollectionTag
230 minTrackPt = options.minTrackPt
231 maxTrackPt = options.maxTrackPt
232 minTrackP = options.minTrackP
233 maxTrackP = options.maxTrackP
234 maxDxy = options.maxDxy
235 minTrackerHits = str(options.minTrackerHits)
236 maxTrackerRedChi2 = options.maxTrackerRedChi2
237 minNCrossedChambers = options.minNCrossedChambers
238 allowTIDTEC = str(
not options.notAllowTIDTEC)
239 twoBin = str(options.twoBin)
240 weightAlignment = str(options.weightAlignment)
241 minAlignmentHits = str(options.minAlignmentHits)
242 combineME11 = str(
not options.notCombineME11)
243 maxEvents = options.maxEvents
244 skipEvents = options.skipEvents
245 validationLabel = options.validationLabel
246 maxResSlopeY = options.maxResSlopeY
247 preFilter =
not not options.preFilter
248 useTrackerMuons = options.useTrackerMuons
250 createMapNtuple=
False
251 if options.createMapNtuple: createMapNtuple=
True
254 if options.ring2only: ring2only =
"--ring2only"
256 if options.inputInBlocks: inputInBlocks =
"--inputInBlocks"
258 json_file = options.json
260 if validationLabel ==
'':
261 print "\nOne or more of REQUIRED options is missing!\n"
268 njobs = options.subjobs
269 if (options.inputInBlocks):
270 njobs = len(fileNamesBlocks)
272 print "while --inputInBlocks is specified, the INPUTFILES has no blocks!"
275 stepsize = int(math.ceil(1.*len(fileNames)/njobs))
277 pwd = str(os.getcwdu())
279 bsubfile = [
"#!/bin/sh",
""]
285 INITIALXML = INITIALGEOM +
'.xml'
286 if INITIALGEOM[-3:]==
'.db':
287 INITIALXML = INITIALGEOM[:-3] +
'.xml'
288 print "Converting",INITIALGEOM,
"to",INITIALXML,
" ...will be done in several seconds..."
289 exit_code = os.system(
"./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py %s %s" % (INITIALGEOM,INITIALXML))
291 print "problem: conversion exited with code:", exit_code
298 for iteration
in range(1, ITERATIONS+1):
300 inputdb = INITIALGEOM
301 inputdbdir = directory[:]
302 inputxml = INITIALXML
304 inputdb = director +
".db"
305 inputdbdir = directory[:]
306 inputxml = director +
".xml"
308 directory =
"%s%02d/" % (DIRNAME, iteration)
309 director = directory[:-1]
310 directory1 =
"%s01/" % DIRNAME
311 director1 = directory1[:-1]
312 os.system(
"rm -rf %s; mkdir %s" % (directory, directory))
313 os.system(
"cp gather_cfg.py %s" % directory)
316 bsubfile.append(
"cd %s" % directory)
318 for jobnumber
in range(njobs):
319 gather_fileName =
"%sgather%03d.sh" % (directory, jobnumber)
320 if not options.inputInBlocks:
321 inputfiles =
" ".
join(fileNames[jobnumber*stepsize:(jobnumber+1)*stepsize])
323 inputfiles =
" ".
join(fileNamesBlocks[jobnumber])
325 copyplots =
"plotting*.root"
328 if trackerconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerconnect[12:]
329 if trackerAPEconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerAPEconnect[12:]
330 if gprcdconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % gprcdconnect[12:]
332 if len(inputfiles) > 0:
333 file(gather_fileName,
"w").
write(
"""#/bin/sh
336 export ALIGNMENT_CAFDIR=`pwd`
339 eval `scramv1 run -sh`
340 export ALIGNMENT_AFSDIR=`pwd`
342 export ALIGNMENT_INPUTFILES='%(inputfiles)s'
343 export ALIGNMENT_ITERATION=%(iteration)d
344 export ALIGNMENT_JOBNUMBER=%(jobnumber)d
345 export ALIGNMENT_MAPPLOTS=True
346 export ALIGNMENT_SEGDIFFPLOTS=True
347 export ALIGNMENT_CURVATUREPLOTS=False
348 export ALIGNMENT_GLOBALTAG=%(globaltag)s
349 export ALIGNMENT_INPUTDB=%(inputdb)s
350 export ALIGNMENT_TRACKERCONNECT=%(trackerconnect)s
351 export ALIGNMENT_TRACKERALIGNMENT=%(trackeralignment)s
352 export ALIGNMENT_TRACKERAPECONNECT=%(trackerAPEconnect)s
353 export ALIGNMENT_TRACKERAPE=%(trackerAPE)s
354 export ALIGNMENT_GPRCDCONNECT=%(gprcdconnect)s
355 export ALIGNMENT_GPRCD=%(gprcd)s
356 export ALIGNMENT_ISCOSMICS=%(iscosmics)s
357 export ALIGNMENT_STATION123PARAMS=%(station123params)s
358 export ALIGNMENT_STATION4PARAMS=%(station4params)s
359 export ALIGNMENT_CSCPARAMS=%(cscparams)s
360 export ALIGNMENT_MUONCOLLECTIONTAG=%(muonCollectionTag)s
361 export ALIGNMENT_MINTRACKPT=%(minTrackPt)s
362 export ALIGNMENT_MAXTRACKPT=%(maxTrackPt)s
363 export ALIGNMENT_MINTRACKP=%(minTrackP)s
364 export ALIGNMENT_MAXTRACKP=%(maxTrackP)s
365 export ALIGNMENT_MAXDXY=%(maxDxy)s
366 export ALIGNMENT_MINTRACKERHITS=%(minTrackerHits)s
367 export ALIGNMENT_MAXTRACKERREDCHI2=%(maxTrackerRedChi2)s
368 export ALIGNMENT_MINNCROSSEDCHAMBERS=%(minNCrossedChambers)s
369 export ALIGNMENT_ALLOWTIDTEC=%(allowTIDTEC)s
370 export ALIGNMENT_TWOBIN=%(twoBin)s
371 export ALIGNMENT_WEIGHTALIGNMENT=%(weightAlignment)s
372 export ALIGNMENT_MINALIGNMENTHITS=%(minAlignmentHits)s
373 export ALIGNMENT_COMBINEME11=%(combineME11)s
374 export ALIGNMENT_MAXEVENTS=%(maxEvents)s
375 export ALIGNMENT_SKIPEVENTS=%(skipEvents)s
376 export ALIGNMENT_MAXRESSLOPEY=%(maxResSlopeY)s
377 export ALIGNMENT_DO_DT='False'
378 export ALIGNMENT_DO_CSC='True'
379 export ALIGNMENT_JSON=%(json_file)s
380 export ALIGNMENT_CREATEMAPNTUPLE=%(createMapNtuple)s
381 export ALIGNMENT_PREFILTER=%(preFilter)s
382 export ALIGNMENT_USETRACKERMUONS=%(useTrackerMuons)s
384 if [ \"zzz$ALIGNMENT_JSON\" != \"zzz\" ]; then
385 cp -f $ALIGNMENT_JSON $ALIGNMENT_CAFDIR/
388 cp -f %(directory)sgather_cfg.py %(inputdbdir)s%(inputdb)s %(copytrackerdb)s $ALIGNMENT_CAFDIR/
389 cd $ALIGNMENT_CAFDIR/
393 cp -f %(copyplots)s $ALIGNMENT_AFSDIR/%(directory)s
395 os.system(
"chmod +x %s" % gather_fileName)
396 bsubfile.append(
"echo %sgather%03d.sh" % (directory, jobnumber))
398 if last_align
is None: waiter =
""
399 else: waiter =
"-w \"ended(%s)\"" % last_align
400 if options.big: queue =
"cmscaf1nd"
401 else: queue =
"cmscaf1nh"
403 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q %s -J \"%s_gather%03d\" %s gather%03d.sh" % (queue, director, jobnumber, waiter, jobnumber))
405 bsubnames.append(
"ended(%s_gather%03d)" % (director, jobnumber))
408 if trackerconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerconnect[12:]
409 if trackerAPEconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerAPEconnect[12:]
410 if gprcdconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % gprcdconnect[12:]
412 file(
"%salign.sh" % directory,
"w").
write(
"""#!/bin/sh
415 export ALIGNMENT_CAFDIR=`pwd`
420 eval `scramv1 run -sh`
421 export ALIGNMENT_AFSDIR=`pwd`
423 # combine _plotting.root files into one:
424 nfiles=$(ls %(directory)splotting0*.root 2> /dev/null | wc -l)
425 if [ \"$nfiles\" != \"0\" ]; then
427 for fn in %(directory)splotting0*.root
429 FILESIZE=$(stat -c%%s "$fn")
430 if [ $FILESIZE -gt 1000 ]; then
436 #hadd -f1 %(directory)s%(director)s_plotting.root %(directory)splotting0*.root
437 hadd -f1 %(directory)s%(director)s_plotting.root $flist
438 #if [ $? == 0 ]; then rm %(directory)splotting0*.root; fi
441 # copy plotting and db files to CAFDIR
442 cp -f %(directory)s%(director)s_plotting.root $ALIGNMENT_CAFDIR/files
443 cp -f inertGlobalPositionRcd.db %(inputdbdir)s%(inputdb)s %(inputdbdir)s%(inputxml)s %(copytrackerdb)s $ALIGNMENT_CAFDIR/
445 # copy the scripts to CAFDIR
446 cd Alignment/MuonAlignmentAlgorithms/scripts/
447 cp -f plotscripts.py $ALIGNMENT_CAFDIR/
448 cp -f mutypes.py $ALIGNMENT_CAFDIR/
449 cp -f alignmentValidation.py $ALIGNMENT_CAFDIR/
450 cp -f phiedges_fitfunctions.C $ALIGNMENT_CAFDIR/
451 cp -f convertSQLiteXML.py $ALIGNMENT_CAFDIR/
452 cp -f alignCSCRings.py $ALIGNMENT_CAFDIR/
453 cp -f signConventions.py $ALIGNMENT_CAFDIR/
456 cd $ALIGNMENT_CAFDIR/
459 # run alignment validation to produce map plots and sin fit results
460 ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out --csc --map --segdiff --createDirSructure
462 # align CSC rings using the fit results from the previous step
463 ./alignCSCRings.py -d $ALIGNMENT_CAFDIR/out -l %(validationLabel)s -x %(inputxml)s %(ring2only)s
465 # convert ring-aligned xml geometry into sqlite
466 ./convertSQLiteXML.py %(inputxml)s.ring.xml %(director)s.db
468 # convert the new sqlite into proper chambers and layers xml
469 ./convertSQLiteXML.py %(director)s.db %(director)s.xml
471 #copy all good stuff to $ALIGNMENT_AFSDIR/%(directory)s
472 tar czf %(director)s_%(validationLabel)s.tgz out
473 cp -f %(director)s_%(validationLabel)s.tgz $ALIGNMENT_AFSDIR/%(directory)s
474 cp -f out/tmp_test_results_map__%(validationLabel)s.pkl $ALIGNMENT_AFSDIR/%(directory)s%(director)s.pkl
475 cp -f %(inputxml)s.ring.xml $ALIGNMENT_AFSDIR/%(directory)s
476 cp -f %(director)s.xml $ALIGNMENT_AFSDIR/%(directory)s
477 cp -f %(director)s.db $ALIGNMENT_AFSDIR/%(directory)s
479 # if it's last iteration, apply chamber motion policy
480 #if [ \"$ALIGNMENT_ITERATION\" == 2 ]; then
481 # #nfiles=$(ls %(directory)splotting0*.root 2> /dev/null | wc -l)
485 os.system(
"chmod +x %salign.sh" % directory)
487 bsubfile.append(
"echo %salign.sh" % directory)
488 if options.big: queue =
"cmscaf1nd"
489 else: queue =
"cmscaf1nh"
490 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q %s -J \"%s_align\" -w \"%s\" align.sh" % (queue, director,
" && ".
join(bsubnames)))
492 last_align =
"%s_align" % director
495 if iteration == ITERATIONS:
497 directory1 =
"%s01/" % DIRNAME
498 director1 = directory1[:-1]
500 file(
"%svalidation.sh" % directory,
"w").
write(
"""#!/bin/sh
503 export ALIGNMENT_CAFDIR=`pwd`
509 eval `scramv1 run -sh`
510 ALIGNMENT_AFSDIR=`pwd`
512 # copy the scripts to CAFDIR
513 cd Alignment/MuonAlignmentAlgorithms/scripts/
514 cp -f plotscripts.py $ALIGNMENT_CAFDIR/
515 cp -f mutypes.py $ALIGNMENT_CAFDIR/
516 cp -f alignmentValidation.py $ALIGNMENT_CAFDIR/
517 cp -f phiedges_fitfunctions.C $ALIGNMENT_CAFDIR/
518 cp -f createTree.py $ALIGNMENT_CAFDIR/
519 cp -f signConventions.py $ALIGNMENT_CAFDIR/
521 cp Alignment/MuonAlignmentAlgorithms/test/browser/tree* $ALIGNMENT_CAFDIR/out/
523 # copy the results to CAFDIR
524 cp -f %(directory1)s%(director1)s_%(validationLabel)s.tgz $ALIGNMENT_CAFDIR/tmp/
525 cp -f %(directory)s%(director)s_%(validationLabel)s.tgz $ALIGNMENT_CAFDIR/tmp/
527 cd $ALIGNMENT_CAFDIR/
528 tar xzvf tmp/%(director1)s_%(validationLabel)s.tgz
530 mv out/iterN out/iter1
531 mv out/tmp_test_results_map__%(validationLabel)s.pkl out/tmp_test_results_map__%(validationLabel)s_1.pkl
532 tar xzvf tmp/%(director)s_%(validationLabel)s.tgz
535 echo \" ### Start running ###\"
538 # run simple diagnostic
539 ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out --csc --diagnostic
541 # fill the tree browser structure:
542 ./createTree.py -i $ALIGNMENT_CAFDIR/out
544 timestamp=`date \"+%%y-%%m-%%d %%H:%%M:%%S\"`
545 echo \"%(validationLabel)s.plots (${timestamp})\" > out/label.txt
548 timestamp=`date +%%Y%%m%%d%%H%%M%%S`
549 tar czf %(validationLabel)s_${timestamp}.tgz out
550 cp -f %(validationLabel)s_${timestamp}.tgz $ALIGNMENT_AFSDIR/
553 os.system(
"chmod +x %svalidation.sh" % directory)
555 bsubfile.append(
"echo %svalidation.sh" % directory)
556 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q cmscaf1nd -J \"%s_validation\" -w \"ended(%s)\" validation.sh" % (director, last_align))
558 bsubfile.append(
"cd ..")
563 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)