3 from __future__
import print_function
4 import os, sys, optparse, math
7 for i
in range(len(copyargs)):
10 if copyargs[i].
find(
" ") != -1:
11 copyargs[i] =
"\"%s\"" % copyargs[i]
12 commandline =
" ".
join(copyargs)
16 usage =
"""./%(prog)s DIRNAME ITERATIONS INITIALGEOM INPUTFILES [options] 18 Creates (overwrites) a directory for each of the iterations and creates (overwrites) 19 submitJobs.sh with the submission sequence and dependencies. 21 DIRNAME directories will be named DIRNAME01, DIRNAME02... 22 ITERATIONS number of iterations 23 INITIALGEOM SQLite file containing muon geometry with tag names 24 DTAlignmentRcd, DTAlignmentErrorExtendedRcd, CSCAlignmentRcd, CSCAlignmentErrorExtendedRcd 25 INPUTFILES Python file defining 'fileNames', a list of input files as 26 strings (create with findQualityFiles.py)""" % vars()
28 parser = optparse.OptionParser(usage)
29 parser.add_option(
"--validationLabel",
30 help=
"[REQUIRED] the label to be used to mark a run; the results will be put into a LABEL_DATESTAMP.tgz tarball",
33 dest=
"validationLabel")
34 parser.add_option(
"-j",
"--jobs",
35 help=
"approximate number of \"gather\" subjobs",
39 parser.add_option(
"-s",
"--submitJobs",
40 help=
"alternate name of submitJobs.sh script (please include .sh extension); a file with this name will be OVERWRITTEN",
42 default=
"submitJobs.sh",
44 parser.add_option(
"-b",
"--big",
45 help=
"if invoked, subjobs will also be run on cmscaf1nd",
48 parser.add_option(
"--globalTag",
49 help=
"GlobalTag for alignment/calibration conditions (typically all conditions except muon and tracker alignment)",
51 default=
"CRAFT0831X_V1::All",
53 parser.add_option(
"--trackerconnect",
54 help=
"connect string for tracker alignment (frontier://... or sqlite_file:...)",
57 dest=
"trackerconnect")
58 parser.add_option(
"--trackeralignment",
59 help=
"name of TrackerAlignmentRcd tag",
62 dest=
"trackeralignment")
63 parser.add_option(
"--trackerAPEconnect",
64 help=
"connect string for tracker APEs (frontier://... or sqlite_file:...)",
67 dest=
"trackerAPEconnect")
68 parser.add_option(
"--trackerAPE",
69 help=
"name of TrackerAlignmentErrorExtendedRcd tag (tracker APEs)",
71 default=
"AlignmentErrorsExtended",
73 parser.add_option(
"--gprcdconnect",
74 help=
"connect string for GlobalPositionRcd (frontier://... or sqlite_file:...)",
78 parser.add_option(
"--gprcd",
79 help=
"name of GlobalPositionRcd tag",
81 default=
"GlobalPosition",
83 parser.add_option(
"--iscosmics",
84 help=
"if invoked, use cosmic track refitter instead of the standard one",
87 parser.add_option(
"--station123params",
88 help=
"alignable parameters for DT stations 1, 2, 3 (see SWGuideAlignmentAlgorithms#Selection_of_what_to_align)",
91 dest=
"station123params")
92 parser.add_option(
"--station4params",
93 help=
"alignable parameters for DT station 4",
96 dest=
"station4params")
97 parser.add_option(
"--cscparams",
98 help=
"alignable parameters for CSC chambers",
102 parser.add_option(
"--minTrackPt",
103 help=
"minimum allowed track transverse momentum (in GeV)",
107 parser.add_option(
"--maxTrackPt",
108 help=
"maximum allowed track transverse momentum (in GeV)",
112 parser.add_option(
"--minTrackP",
113 help=
"minimum allowed track momentum (in GeV)",
117 parser.add_option(
"--maxTrackP",
118 help=
"maximum allowed track momentum (in GeV)",
122 parser.add_option(
"--minTrackerHits",
123 help=
"minimum number of tracker hits",
126 dest=
"minTrackerHits")
127 parser.add_option(
"--maxTrackerRedChi2",
128 help=
"maximum tracker chi^2 per degrees of freedom",
131 dest=
"maxTrackerRedChi2")
132 parser.add_option(
"--notAllowTIDTEC",
133 help=
"if invoked, do not allow tracks that pass through the tracker's TID||TEC region (not recommended)",
135 dest=
"notAllowTIDTEC")
136 parser.add_option(
"--twoBin",
137 help=
"if invoked, apply the \"two-bin method\" to control charge-antisymmetric errors",
140 parser.add_option(
"--weightAlignment",
141 help=
"if invoked, segments will be weighted by ndf/chi^2 in the alignment",
143 dest=
"weightAlignment")
144 parser.add_option(
"--minAlignmentSegments",
145 help=
"minimum number of segments required to align a chamber",
148 dest=
"minAlignmentHits")
149 parser.add_option(
"--notCombineME11",
150 help=
"if invoked, treat ME1/1a and ME1/1b as separate objects",
152 dest=
"notCombineME11")
153 parser.add_option(
"--maxEvents",
154 help=
"maximum number of events",
158 parser.add_option(
"--skipEvents",
159 help=
"number of events to be skipped",
163 parser.add_option(
"--maxResSlopeY",
164 help=
"maximum residual slope y component",
168 parser.add_option(
"--ring2only",
169 help=
"if invoked, use only ring 2 results to align all rings in corresponding disks",
172 parser.add_option(
"--createMapNtuple",
173 help=
"if invoked while mapplots are switched on, a special ntuple would be created",
175 dest=
"createMapNtuple")
176 parser.add_option(
"--inputInBlocks",
177 help=
"if invoked, assume that INPUTFILES provides a list of files already groupped into job blocks, -j has no effect in that case",
179 dest=
"inputInBlocks")
180 parser.add_option(
"--json",
181 help=
"If present with JSON file as argument, use JSON file for good lumi mask. "+\
182 "The latest JSON file is available at /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions11/7TeV/Prompt/",
186 parser.add_option(
"--preFilter",
187 help=
"if invoked, MuonAlignmentPreFilter module would be invoked in the Path's beginning. Can significantly speed up gather jobs.",
190 parser.add_option(
"--useTrackerMuons",
191 help=
"use tracker muons approach instead of the default global muons tracks based one",
193 dest=
"useTrackerMuons")
194 parser.add_option(
"--muonCollectionTag",
195 help=
"InputTag of muons collection to use in tracker muons based approach",
198 dest=
"muonCollectionTag")
199 parser.add_option(
"--maxDxy",
200 help=
"maximum track impact parameter with relation to beamline",
204 parser.add_option(
"--minNCrossedChambers",
205 help=
"minimum number of muon chambers that a track is required to cross",
208 dest=
"minNCrossedChambers")
210 if len(sys.argv) < 5:
211 raise SystemError(
"Too few arguments.\n\n"+parser.format_help())
213 DIRNAME = sys.argv[1]
214 ITERATIONS =
int(sys.argv[2])
215 INITIALGEOM = sys.argv[3]
216 INPUTFILES = sys.argv[4]
218 options, args = parser.parse_args(sys.argv[5:])
219 globaltag = options.globaltag
220 trackerconnect = options.trackerconnect
221 trackeralignment = options.trackeralignment
222 trackerAPEconnect = options.trackerAPEconnect
223 trackerAPE = options.trackerAPE
224 gprcdconnect = options.gprcdconnect
225 gprcd = options.gprcd
226 iscosmics =
str(options.iscosmics)
227 station123params = options.station123params
228 station4params = options.station4params
229 cscparams = options.cscparams
230 muonCollectionTag = options.muonCollectionTag
231 minTrackPt = options.minTrackPt
232 maxTrackPt = options.maxTrackPt
233 minTrackP = options.minTrackP
234 maxTrackP = options.maxTrackP
235 maxDxy = options.maxDxy
236 minTrackerHits =
str(options.minTrackerHits)
237 maxTrackerRedChi2 = options.maxTrackerRedChi2
238 minNCrossedChambers = options.minNCrossedChambers
239 allowTIDTEC =
str(
not options.notAllowTIDTEC)
240 twoBin =
str(options.twoBin)
241 weightAlignment =
str(options.weightAlignment)
242 minAlignmentHits =
str(options.minAlignmentHits)
243 combineME11 =
str(
not options.notCombineME11)
244 maxEvents = options.maxEvents
245 skipEvents = options.skipEvents
246 validationLabel = options.validationLabel
247 maxResSlopeY = options.maxResSlopeY
248 preFilter =
not not options.preFilter
249 useTrackerMuons = options.useTrackerMuons
251 createMapNtuple=
False 252 if options.createMapNtuple: createMapNtuple=
True 255 if options.ring2only: ring2only =
"--ring2only" 257 if options.inputInBlocks: inputInBlocks =
"--inputInBlocks" 259 json_file = options.json
261 if validationLabel ==
'':
262 print(
"\nOne or more of REQUIRED options is missing!\n")
269 njobs = options.subjobs
270 if (options.inputInBlocks):
271 njobs = len(fileNamesBlocks)
273 print(
"while --inputInBlocks is specified, the INPUTFILES has no blocks!")
276 stepsize =
int(math.ceil(1.*len(fileNames)/njobs))
280 bsubfile = [
"#!/bin/sh",
""]
286 INITIALXML = INITIALGEOM +
'.xml' 287 if INITIALGEOM[-3:]==
'.db':
288 INITIALXML = INITIALGEOM[:-3] +
'.xml' 289 print(
"Converting",INITIALGEOM,
"to",INITIALXML,
" ...will be done in several seconds...")
290 exit_code = os.system(
"./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py %s %s" % (INITIALGEOM,INITIALXML))
292 print(
"problem: conversion exited with code:", exit_code)
299 for iteration
in range(1, ITERATIONS+1):
301 inputdb = INITIALGEOM
302 inputdbdir = directory[:]
303 inputxml = INITIALXML
305 inputdb = director +
".db" 306 inputdbdir = directory[:]
307 inputxml = director +
".xml" 309 directory =
"%s%02d/" % (DIRNAME, iteration)
310 director = directory[:-1]
311 directory1 =
"%s01/" % DIRNAME
312 director1 = directory1[:-1]
313 os.system(
"rm -rf %s; mkdir %s" % (directory, directory))
314 os.system(
"cp gather_cfg.py %s" % directory)
317 bsubfile.append(
"cd %s" % directory)
319 for jobnumber
in range(njobs):
320 gather_fileName =
"%sgather%03d.sh" % (directory, jobnumber)
321 if not options.inputInBlocks:
322 inputfiles =
" ".
join(fileNames[jobnumber*stepsize:(jobnumber+1)*stepsize])
324 inputfiles =
" ".
join(fileNamesBlocks[jobnumber])
326 copyplots =
"plotting*.root" 329 if trackerconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerconnect[12:]
330 if trackerAPEconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerAPEconnect[12:]
331 if gprcdconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % gprcdconnect[12:]
333 if len(inputfiles) > 0:
334 file(gather_fileName,
"w").
write(
"""#/bin/sh 337 export ALIGNMENT_CAFDIR=`pwd` 340 eval `scramv1 run -sh` 341 export ALIGNMENT_AFSDIR=`pwd` 343 export ALIGNMENT_INPUTFILES='%(inputfiles)s' 344 export ALIGNMENT_ITERATION=%(iteration)d 345 export ALIGNMENT_JOBNUMBER=%(jobnumber)d 346 export ALIGNMENT_MAPPLOTS=True 347 export ALIGNMENT_SEGDIFFPLOTS=True 348 export ALIGNMENT_CURVATUREPLOTS=False 349 export ALIGNMENT_GLOBALTAG=%(globaltag)s 350 export ALIGNMENT_INPUTDB=%(inputdb)s 351 export ALIGNMENT_TRACKERCONNECT=%(trackerconnect)s 352 export ALIGNMENT_TRACKERALIGNMENT=%(trackeralignment)s 353 export ALIGNMENT_TRACKERAPECONNECT=%(trackerAPEconnect)s 354 export ALIGNMENT_TRACKERAPE=%(trackerAPE)s 355 export ALIGNMENT_GPRCDCONNECT=%(gprcdconnect)s 356 export ALIGNMENT_GPRCD=%(gprcd)s 357 export ALIGNMENT_ISCOSMICS=%(iscosmics)s 358 export ALIGNMENT_STATION123PARAMS=%(station123params)s 359 export ALIGNMENT_STATION4PARAMS=%(station4params)s 360 export ALIGNMENT_CSCPARAMS=%(cscparams)s 361 export ALIGNMENT_MUONCOLLECTIONTAG=%(muonCollectionTag)s 362 export ALIGNMENT_MINTRACKPT=%(minTrackPt)s 363 export ALIGNMENT_MAXTRACKPT=%(maxTrackPt)s 364 export ALIGNMENT_MINTRACKP=%(minTrackP)s 365 export ALIGNMENT_MAXTRACKP=%(maxTrackP)s 366 export ALIGNMENT_MAXDXY=%(maxDxy)s 367 export ALIGNMENT_MINTRACKERHITS=%(minTrackerHits)s 368 export ALIGNMENT_MAXTRACKERREDCHI2=%(maxTrackerRedChi2)s 369 export ALIGNMENT_MINNCROSSEDCHAMBERS=%(minNCrossedChambers)s 370 export ALIGNMENT_ALLOWTIDTEC=%(allowTIDTEC)s 371 export ALIGNMENT_TWOBIN=%(twoBin)s 372 export ALIGNMENT_WEIGHTALIGNMENT=%(weightAlignment)s 373 export ALIGNMENT_MINALIGNMENTHITS=%(minAlignmentHits)s 374 export ALIGNMENT_COMBINEME11=%(combineME11)s 375 export ALIGNMENT_MAXEVENTS=%(maxEvents)s 376 export ALIGNMENT_SKIPEVENTS=%(skipEvents)s 377 export ALIGNMENT_MAXRESSLOPEY=%(maxResSlopeY)s 378 export ALIGNMENT_DO_DT='False' 379 export ALIGNMENT_DO_CSC='True' 380 export ALIGNMENT_JSON=%(json_file)s 381 export ALIGNMENT_CREATEMAPNTUPLE=%(createMapNtuple)s 382 export ALIGNMENT_PREFILTER=%(preFilter)s 383 export ALIGNMENT_USETRACKERMUONS=%(useTrackerMuons)s 385 if [ \"zzz$ALIGNMENT_JSON\" != \"zzz\" ]; then 386 cp -f $ALIGNMENT_JSON $ALIGNMENT_CAFDIR/ 389 cp -f %(directory)sgather_cfg.py %(inputdbdir)s%(inputdb)s %(copytrackerdb)s $ALIGNMENT_CAFDIR/ 390 cd $ALIGNMENT_CAFDIR/ 394 cp -f %(copyplots)s $ALIGNMENT_AFSDIR/%(directory)s 396 os.system(
"chmod +x %s" % gather_fileName)
397 bsubfile.append(
"echo %sgather%03d.sh" % (directory, jobnumber))
399 if last_align
is None: waiter =
"" 400 else: waiter =
"-w \"ended(%s)\"" % last_align
401 if options.big: queue =
"cmscaf1nd" 402 else: queue =
"cmscaf1nh" 404 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q %s -J \"%s_gather%03d\" %s gather%03d.sh" % (queue, director, jobnumber, waiter, jobnumber))
406 bsubnames.append(
"ended(%s_gather%03d)" % (director, jobnumber))
409 if trackerconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerconnect[12:]
410 if trackerAPEconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % trackerAPEconnect[12:]
411 if gprcdconnect[0:12] ==
"sqlite_file:": copytrackerdb +=
"%s " % gprcdconnect[12:]
413 file(
"%salign.sh" % directory,
"w").
write(
"""#!/bin/sh 416 export ALIGNMENT_CAFDIR=`pwd` 421 eval `scramv1 run -sh` 422 export ALIGNMENT_AFSDIR=`pwd` 424 # combine _plotting.root files into one: 425 nfiles=$(ls %(directory)splotting0*.root 2> /dev/null | wc -l) 426 if [ \"$nfiles\" != \"0\" ]; then 428 for fn in %(directory)splotting0*.root 430 FILESIZE=$(stat -c%%s "$fn") 431 if [ $FILESIZE -gt 1000 ]; then 437 #hadd -f1 %(directory)s%(director)s_plotting.root %(directory)splotting0*.root 438 hadd -f1 %(directory)s%(director)s_plotting.root $flist 439 #if [ $? == 0 ]; then rm %(directory)splotting0*.root; fi 442 # copy plotting and db files to CAFDIR 443 cp -f %(directory)s%(director)s_plotting.root $ALIGNMENT_CAFDIR/files 444 cp -f inertGlobalPositionRcd.db %(inputdbdir)s%(inputdb)s %(inputdbdir)s%(inputxml)s %(copytrackerdb)s $ALIGNMENT_CAFDIR/ 446 # copy the scripts to CAFDIR 447 cd Alignment/MuonAlignmentAlgorithms/scripts/ 448 cp -f plotscripts.py $ALIGNMENT_CAFDIR/ 449 cp -f mutypes.py $ALIGNMENT_CAFDIR/ 450 cp -f alignmentValidation.py $ALIGNMENT_CAFDIR/ 451 cp -f phiedges_fitfunctions.C $ALIGNMENT_CAFDIR/ 452 cp -f convertSQLiteXML.py $ALIGNMENT_CAFDIR/ 453 cp -f alignCSCRings.py $ALIGNMENT_CAFDIR/ 454 cp -f signConventions.py $ALIGNMENT_CAFDIR/ 457 cd $ALIGNMENT_CAFDIR/ 460 # run alignment validation to produce map plots and sin fit results 461 ./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 463 # align CSC rings using the fit results from the previous step 464 ./alignCSCRings.py -d $ALIGNMENT_CAFDIR/out -l %(validationLabel)s -x %(inputxml)s %(ring2only)s 466 # convert ring-aligned xml geometry into sqlite 467 ./convertSQLiteXML.py %(inputxml)s.ring.xml %(director)s.db 469 # convert the new sqlite into proper chambers and layers xml 470 ./convertSQLiteXML.py %(director)s.db %(director)s.xml 472 #copy all good stuff to $ALIGNMENT_AFSDIR/%(directory)s 473 tar czf %(director)s_%(validationLabel)s.tgz out 474 cp -f %(director)s_%(validationLabel)s.tgz $ALIGNMENT_AFSDIR/%(directory)s 475 cp -f out/tmp_test_results_map__%(validationLabel)s.pkl $ALIGNMENT_AFSDIR/%(directory)s%(director)s.pkl 476 cp -f %(inputxml)s.ring.xml $ALIGNMENT_AFSDIR/%(directory)s 477 cp -f %(director)s.xml $ALIGNMENT_AFSDIR/%(directory)s 478 cp -f %(director)s.db $ALIGNMENT_AFSDIR/%(directory)s 480 # if it's last iteration, apply chamber motion policy 481 #if [ \"$ALIGNMENT_ITERATION\" == 2 ]; then 482 # #nfiles=$(ls %(directory)splotting0*.root 2> /dev/null | wc -l) 486 os.system(
"chmod +x %salign.sh" % directory)
488 bsubfile.append(
"echo %salign.sh" % directory)
489 if options.big: queue =
"cmscaf1nd" 490 else: queue =
"cmscaf1nh" 491 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q %s -J \"%s_align\" -w \"%s\" align.sh" % (queue, director,
" && ".
join(bsubnames)))
493 last_align =
"%s_align" % director
496 if iteration == ITERATIONS:
498 directory1 =
"%s01/" % DIRNAME
499 director1 = directory1[:-1]
501 file(
"%svalidation.sh" % directory,
"w").
write(
"""#!/bin/sh 504 export ALIGNMENT_CAFDIR=`pwd` 510 eval `scramv1 run -sh` 511 ALIGNMENT_AFSDIR=`pwd` 513 # copy the scripts to CAFDIR 514 cd Alignment/MuonAlignmentAlgorithms/scripts/ 515 cp -f plotscripts.py $ALIGNMENT_CAFDIR/ 516 cp -f mutypes.py $ALIGNMENT_CAFDIR/ 517 cp -f alignmentValidation.py $ALIGNMENT_CAFDIR/ 518 cp -f phiedges_fitfunctions.C $ALIGNMENT_CAFDIR/ 519 cp -f createTree.py $ALIGNMENT_CAFDIR/ 520 cp -f signConventions.py $ALIGNMENT_CAFDIR/ 522 cp Alignment/MuonAlignmentAlgorithms/test/browser/tree* $ALIGNMENT_CAFDIR/out/ 524 # copy the results to CAFDIR 525 cp -f %(directory1)s%(director1)s_%(validationLabel)s.tgz $ALIGNMENT_CAFDIR/tmp/ 526 cp -f %(directory)s%(director)s_%(validationLabel)s.tgz $ALIGNMENT_CAFDIR/tmp/ 528 cd $ALIGNMENT_CAFDIR/ 529 tar xzvf tmp/%(director1)s_%(validationLabel)s.tgz 531 mv out/iterN out/iter1 532 mv out/tmp_test_results_map__%(validationLabel)s.pkl out/tmp_test_results_map__%(validationLabel)s_1.pkl 533 tar xzvf tmp/%(director)s_%(validationLabel)s.tgz 536 echo \" ### Start running ###\" 539 # run simple diagnostic 540 ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out --csc --diagnostic 542 # fill the tree browser structure: 543 ./createTree.py -i $ALIGNMENT_CAFDIR/out 545 timestamp=`date \"+%%y-%%m-%%d %%H:%%M:%%S\"` 546 echo \"%(validationLabel)s.plots (${timestamp})\" > out/label.txt 549 timestamp=`date +%%Y%%m%%d%%H%%M%%S` 550 tar czf %(validationLabel)s_${timestamp}.tgz out 551 cp -f %(validationLabel)s_${timestamp}.tgz $ALIGNMENT_AFSDIR/ 554 os.system(
"chmod +x %svalidation.sh" % directory)
556 bsubfile.append(
"echo %svalidation.sh" % directory)
557 bsubfile.append(
"bsub -R \"type==SLC5_64\" -q cmscaf1nd -J \"%s_validation\" -w \"ended(%s)\" validation.sh" % (director, last_align))
559 bsubfile.append(
"cd ..")
564 os.system(
"chmod +x %s" % options.submitJobs)
S & print(S &os, JobReport::InputFile const &f)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
static std::string join(char **cmd)