10 import Alignment.OfflineValidation.TkAlAllInOneTool.configTemplates \
12 import Alignment.OfflineValidation.TkAlAllInOneTool.crabWrapper
as crabWrapper
13 from Alignment.OfflineValidation.TkAlAllInOneTool.TkAlExceptions \
15 from Alignment.OfflineValidation.TkAlAllInOneTool.helperFunctions \
16 import replaceByMap, getCommandOutput2, addIndex
17 from Alignment.OfflineValidation.TkAlAllInOneTool.betterConfigParser \
18 import BetterConfigParser
19 from Alignment.OfflineValidation.TkAlAllInOneTool.alignment
import Alignment
21 from Alignment.OfflineValidation.TkAlAllInOneTool.genericValidation \
22 import GenericValidation
23 from Alignment.OfflineValidation.TkAlAllInOneTool.geometryComparison \
24 import GeometryComparison
25 from Alignment.OfflineValidation.TkAlAllInOneTool.offlineValidation \
26 import OfflineValidation, OfflineValidationDQM
27 from Alignment.OfflineValidation.TkAlAllInOneTool.monteCarloValidation \
28 import MonteCarloValidation
29 from Alignment.OfflineValidation.TkAlAllInOneTool.trackSplittingValidation \
30 import TrackSplittingValidation
31 from Alignment.OfflineValidation.TkAlAllInOneTool.zMuMuValidation \
32 import ZMuMuValidation
33 from Alignment.OfflineValidation.TkAlAllInOneTool.primaryVertexValidation \
34 import PrimaryVertexValidation
35 from Alignment.OfflineValidation.TkAlAllInOneTool.preexistingValidation \
37 from Alignment.OfflineValidation.TkAlAllInOneTool.plottingOptions \
38 import PlottingOptions
39 import Alignment.OfflineValidation.TkAlAllInOneTool.globalDictionaries \
53 def __init__( self, validation, config, options ):
54 if validation[1] ==
"":
56 valString = validation[0].
split(
"->" )[0]
57 alignments = validation[0].
split(
"->" )[1]
59 if "->" in validation[0]:
60 msg = (
"Instead of using the intermediate syntax\n'" 61 +valString.strip()+
"-> "+alignments.strip()
62 +
":'\nyou have to use the now fully supported syntax \n'" 63 +valString.strip()+
": " 64 +alignments.strip()+
"'.")
65 raise AllInOneError(msg)
67 valString = validation[0]
68 alignments = validation[1]
69 valString = valString.split()
81 print (
"offlineParallel and offline are now the same. To run an offline parallel validation,\n" 82 "just set parallelJobs to something > 1. There is no reason to call it offlineParallel anymore.")
85 if not self.__config.has_section( section ):
86 raise AllInOneError(
"Validation '%s' of type '%s' is requested in" 87 " '[validation]' section, but is not defined." 88 "\nYou have to add a '[%s]' section." 95 if valType ==
"compare":
96 alignmentsList = alignments.split(
"," )
97 firstAlignList = alignmentsList[0].
split()
98 firstAlignName = firstAlignList[0].
strip()
99 if firstAlignName ==
"IDEAL":
100 raise AllInOneError(
"'IDEAL' has to be the second (reference)" 101 " alignment in 'compare <val_name>: " 102 "<alignment> <reference>'.")
103 if len( firstAlignList ) > 1:
104 firstRun = firstAlignList[1]
108 firstAlignName = firstAlign.name
109 secondAlignList = alignmentsList[1].
split()
110 secondAlignName = secondAlignList[0].
strip()
111 if len( secondAlignList ) > 1:
112 secondRun = secondAlignList[1]
115 if secondAlignName ==
"IDEAL":
116 secondAlign = secondAlignName
120 secondAlignName = secondAlign.name
122 validation = GeometryComparison( name, firstAlign, secondAlign,
124 self.__commandLineOptions.getImages)
125 elif valType ==
"offline":
126 validation = OfflineValidation( name,
128 elif valType ==
"preexistingoffline":
129 validation = PreexistingOfflineValidation(name, self.
__config)
130 elif valType ==
"offlineDQM":
131 validation = OfflineValidationDQM( name,
133 elif valType ==
"mcValidate":
134 validation = MonteCarloValidation( name,
136 elif valType ==
"preexistingmcValidate":
137 validation = PreexistingMonteCarloValidation(name, self.
__config)
138 elif valType ==
"split":
139 validation = TrackSplittingValidation( name,
141 elif valType ==
"preexistingsplit":
142 validation = PreexistingTrackSplittingValidation(name, self.
__config)
143 elif valType ==
"zmumu":
144 validation = ZMuMuValidation( name,
146 elif valType ==
"primaryvertex":
150 raise AllInOneError(
"Unknown validation mode '%s'"%valType)
155 """This private method creates the needed files for the validation job. 157 self.validation.createConfiguration( outpath )
160 self.
__scripts = sum([
addIndex(script, self.validation.NJobs)
for script
in self.validation.createScript( outpath )], [])
161 if jobMode.split(
',' )[0] ==
"crab":
162 self.validation.createCrabCfg( outpath )
166 """This is the method called to create the job files.""" 168 os.path.abspath( self.__commandLineOptions.Name) )
172 if self.validation.jobid:
173 self.batchJobIds.append(self.validation.jobid)
174 log =
"> " + self.validation.name +
" is already validated." 178 if self.validation.jobid:
179 print "jobid {} will be ignored, since the validation {} is not preexisting".
format(self.validation.jobid, self.validation.name)
181 general = self.__config.getGeneral()
184 name = os.path.splitext( os.path.basename( script) )[0]
185 ValidationJob.jobCount += 1
186 if self.__commandLineOptions.dryRun:
187 print "%s would run: %s"%( name, os.path.basename( script) )
189 log =
"> Validating "+name
190 print "> Validating "+name
191 if self.validation.jobmode ==
"interactive":
193 ValidationJob.interactCount += 1
194 elif self.validation.jobmode.split(
",")[0] ==
"lxBatch":
196 "commands": self.validation.jobmode.split(
",")[1],
197 "logDir": general[
"logdir"],
200 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub" 202 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
203 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
204 if os.path.exists(oldlog):
208 "-o %(logDir)s/%(jobName)s.stdout " 209 "-e %(logDir)s/%(jobName)s.stderr " 213 ValidationJob.batchJobIds.append(bsubOut.split(
"<")[1].
split(
">")[0])
215 ValidationJob.batchCount += 1
216 elif self.validation.jobmode.split(
"," )[0] ==
"crab":
217 os.chdir( general[
"logdir"] )
218 crabName =
"crab." + os.path.basename( script )[:-3]
220 options = {
"-create":
"",
221 "-cfg": crabName +
".cfg",
224 theCrab.run( options )
225 except AllInOneError
as e:
226 print "crab:",
str(e).
split(
"\n")[0]
228 ValidationJob.crabCount += 1
231 raise AllInOneError(
"Unknown 'jobmode'!\n" 232 "Please change this parameter either in " 233 "the [general] or in the [" 235 +
"] section to one of the following " 237 "\tinteractive\n\tlxBatch, -q <queue>\n" 238 "\tcrab, -q <queue>")
248 repMap = offlineValidationList[0].getRepMap()
250 theFile = open( outFilePath,
"w" )
251 theFile.write(
replaceByMap( configTemplates.mergeOfflineParJobsTemplate ,repMap ) )
255 config = offlineValidationList[0].config
257 repMap[
"resultPlotFile" ] = resultPlotFile
258 repMap[
"extendedInstantiation" ] =
"" 260 for validation
in offlineValidationList:
261 repMap[
"extendedInstantiation" ] = validation.appendToExtendedValidation( repMap[
"extendedInstantiation" ] )
263 theFile = open( outFilePath,
"w" )
264 theFile.write(
replaceByMap( configTemplates.extendedValidationTemplate ,repMap ) )
268 config = trackSplittingValidationList[0].config
270 repMap[
"trackSplitPlotInstantiation" ] =
"" 272 for validation
in trackSplittingValidationList:
273 repMap[
"trackSplitPlotInstantiation" ] = validation.appendToExtendedValidation( repMap[
"trackSplitPlotInstantiation" ] )
275 theFile = open( outFilePath,
"w" )
276 theFile.write(
replaceByMap( configTemplates.trackSplitPlotTemplate ,repMap ) )
280 repMap = PrimaryVertexValidationList[0].getRepMap()
281 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
282 repMap[
"PrimaryVertexPlotInstantiation" ] =
"" 284 for validation
in PrimaryVertexValidationList:
285 repMap[
"PrimaryVertexPlotInstantiation" ] = validation.appendToExtendedValidation( repMap[
"PrimaryVertexPlotInstantiation" ] )
287 theFile = open( outFilePath,
"w" )
288 theFile.write(
replaceByMap( configTemplates.PrimaryVertexPlotTemplate ,repMap ) )
292 config = zMuMuValidationList[0].config
294 repMap[
"mergeZmumuPlotsInstantiation" ] =
"" 296 for validation
in zMuMuValidationList:
297 repMap[
"mergeZmumuPlotsInstantiation" ] = validation.appendToExtendedValidation( repMap[
"mergeZmumuPlotsInstantiation" ] )
299 theFile = open( outFilePath,
"w" )
300 theFile.write(
replaceByMap( configTemplates.mergeZmumuPlotsTemplate ,repMap ) )
304 if(len(validations) == 0):
305 raise AllInOneError(
"Cowardly refusing to merge nothing!")
307 config = validations[0].config
308 repMap = config.getGeneral()
311 "CompareAlignments":
"",
312 "RunExtendedOfflineValidation":
"",
313 "RunTrackSplitPlot":
"",
314 "MergeZmumuPlots":
"",
315 "RunPrimaryVertexPlot":
"",
316 "CMSSW_BASE": os.environ[
"CMSSW_BASE"],
317 "SCRAM_ARCH": os.environ[
"SCRAM_ARCH"],
318 "CMSSW_RELEASE_BASE": os.environ[
"CMSSW_RELEASE_BASE"],
322 for validation
in validations:
323 for referenceName
in validation.filesToCompare:
324 validationName =
"%s.%s"%(validation.__class__.__name__, referenceName)
325 validationName = validationName.split(
".%s"%GenericValidation.defaultReferenceName )[0]
326 validationName = validationName.split(
"Preexisting")[-1]
327 if validationName
in comparisonLists:
328 comparisonLists[ validationName ].
append( validation )
330 comparisonLists[ validationName ] = [ validation ]
334 repMap[
"haddLoop"] =
"mergeRetCode=0\n" 335 repMap[
"rmUnmerged"] = (
"if [[ mergeRetCode -eq 0 ]]; then\n" 336 " echo -e \\n\"Merging succeeded, removing original files.\"\n")
337 repMap[
"copyMergeScripts"] =
"" 338 repMap[
"mergeParallelFilePrefixes"] =
"" 342 for validationType
in comparisonLists:
343 for validation
in comparisonLists[validationType]:
344 if isinstance(validation, PreexistingValidation)
or validation.NJobs == 1:
346 if validationType
not in anythingToMerge:
347 anythingToMerge += [validationType]
348 repMap[
"haddLoop"] +=
'\n\n\n\necho -e "\n\nMerging results from %s jobs"\n\n' % validationType
349 repMap[
"haddLoop"] = validation.appendToMerge(repMap[
"haddLoop"])
350 repMap[
"haddLoop"] +=
"tmpMergeRetCode=${?}\n" 351 repMap[
"haddLoop"] += (
"if [[ tmpMergeRetCode -eq 0 ]]; then " 353 +validation.getRepMap()[
"finalOutputFile"]
354 +
" root://eoscms//eos/cms" 355 +validation.getRepMap()[
"finalResultFile"]
357 repMap[
"haddLoop"] += (
"if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then " 358 "mergeRetCode=${tmpMergeRetCode}; fi\n")
359 for f
in validation.getRepMap()[
"outputFiles"]:
360 longName = os.path.join(
"/store/caf/user/$USER/",
361 validation.getRepMap()[
"eosdir"], f)
362 repMap[
"rmUnmerged"] +=
" $eos rm "+longName+
"\n" 363 repMap[
"rmUnmerged"] += (
"else\n" 364 " echo -e \\n\"WARNING: Merging failed, unmerged" 365 " files won't be deleted.\\n" 366 "(Ignore this warning if merging was done earlier)\"\n" 369 if "OfflineValidation" in anythingToMerge:
370 repMap[
"mergeOfflineParJobsScriptPath"] = os.path.join(path,
"TkAlOfflineJobsMerge.C")
373 repMap[
"mergeOfflineParJobsScriptPath"] )
374 repMap[
"copyMergeScripts"] += (
"cp .oO[Alignment/OfflineValidation]Oo./scripts/merge_TrackerOfflineValidation.C .\n" 375 "rfcp %s .\n" % repMap[
"mergeOfflineParJobsScriptPath"])
376 repMap_offline = repMap.copy()
378 repMap[
"copyMergeScripts"] = \
379 replaceByMap(repMap[
"copyMergeScripts"], repMap_offline)
384 repMap[
"DownloadData"] +=
replaceByMap( configTemplates.mergeParallelResults, repMap )
386 repMap[
"DownloadData"] =
"" 388 if "OfflineValidation" in comparisonLists:
389 repMap[
"extendedValScriptPath"] = os.path.join(path,
"TkAlExtendedOfflineValidation.C")
391 repMap[
"extendedValScriptPath"],
393 repMap_offline = repMap.copy()
395 repMap[
"RunExtendedOfflineValidation"] = \
396 replaceByMap(configTemplates.extendedValidationExecution, repMap_offline)
398 if "TrackSplittingValidation" in comparisonLists:
399 repMap[
"trackSplitPlotScriptPath"] = \
400 os.path.join(path,
"TkAlTrackSplitPlot.C")
402 repMap[
"trackSplitPlotScriptPath"] )
403 repMap_split = repMap.copy()
405 repMap[
"RunTrackSplitPlot"] = \
406 replaceByMap(configTemplates.trackSplitPlotExecution, repMap_split)
408 if "ZMuMuValidation" in comparisonLists:
409 repMap[
"mergeZmumuPlotsScriptPath"] = \
410 os.path.join(path,
"TkAlMergeZmumuPlots.C")
412 repMap[
"mergeZmumuPlotsScriptPath"] )
413 repMap_zMuMu = repMap.copy()
415 repMap[
"MergeZmumuPlots"] = \
416 replaceByMap(configTemplates.mergeZmumuPlotsExecution, repMap_zMuMu)
418 if "PrimaryVertexValidation" in comparisonLists:
419 repMap[
"PrimaryVertexPlotScriptPath"] = \
420 os.path.join(path,
"TkAlPrimaryVertexValidationPlot.C")
423 repMap[
"PrimaryVertexPlotScriptPath"] )
424 repMap_PVVal = repMap.copy()
426 repMap[
"RunPrimaryVertexPlot"] = \
427 replaceByMap(configTemplates.PrimaryVertexPlotExecution, repMap_PVVal)
429 repMap[
"CompareAlignments"] =
"#run comparisons" 430 if "OfflineValidation" in comparisonLists:
431 compareStrings = [ val.getCompareStrings(
"OfflineValidation")
for val
in comparisonLists[
"OfflineValidation"] ]
432 compareStringsPlain = [ val.getCompareStrings(
"OfflineValidation", plain=
True)
for val
in comparisonLists[
"OfflineValidation"] ]
434 repMap_offline = repMap.copy()
436 repMap_offline.update({
"validationId":
"OfflineValidation",
437 "compareStrings":
" , ".
join(compareStrings),
438 "compareStringsPlain":
" ".
join(compareStringsPlain) })
440 repMap[
"CompareAlignments"] += \
441 replaceByMap(configTemplates.compareAlignmentsExecution, repMap_offline)
443 filePath = os.path.join(path,
"TkAlMerge.sh")
444 theFile = open( filePath,
"w" )
445 theFile.write(
replaceByMap( configTemplates.mergeTemplate, repMap ) )
447 os.chmod(filePath,0o755)
452 if config.has_section(
"alternateTemplates"):
453 for templateName
in config.options(
"alternateTemplates"):
454 if templateName ==
"AutoAlternates":
456 newTemplateName = config.get(
"alternateTemplates", templateName )
465 optParser = optparse.OptionParser()
466 optParser.description =
"""All-in-one Alignment Validation. 467 This will run various validation procedures either on batch queues or interactively. 468 If no name is given (-N parameter) a name containing time and date is created automatically. 469 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory. 471 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
472 help=
"create all scripts and cfg File but do not start jobs (default=False)")
473 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
True,
474 help=
"get all Images created during the process (default= True)")
475 defaultConfig =
"TkAlConfig.ini" 476 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
477 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
478 optParser.add_option(
"-N",
"--Name", dest=
"Name",
479 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
480 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
481 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
482 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
483 help=
"get the status of the crab jobs", metavar=
"STATUS")
484 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
486 help=
"run the tool to get full traceback of errors",
488 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
489 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)." 490 " Works only for batch jobs")
492 (options, args) = optParser.parse_args(argv)
494 if not options.restrictTo ==
None:
495 options.restrictTo = options.restrictTo.split(
",")
497 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
498 options.config.split(
"," ) ]
499 config = BetterConfigParser()
500 outputIniFileSet = set( config.read( options.config ) )
501 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
504 if options.config == [ os.path.abspath( defaultConfig ) ]:
505 if (
not options.crabStatus )
and \
506 (
not os.path.exists( defaultConfig ) ):
507 raise AllInOneError(
"Default 'ini' file '%s' not found!\n" 508 "You can specify another name with the " 509 "command line option '-c'/'--config'." 512 for iniFile
in failedIniFiles:
513 if not os.path.exists( iniFile ):
514 raise AllInOneError(
"'%s' does not exist. Please check for " 515 "typos in the filename passed to the " 516 "'-c'/'--config' option!" 519 raise AllInOneError((
"'%s' does exist, but parsing of the " 520 "content failed!" ) % iniFile)
523 if options.Name ==
None:
524 if not options.crabStatus:
525 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
527 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
528 "alignmentValidation_*" )
529 if len( existingValDirs ) > 0:
530 options.Name = existingValDirs[-1]
532 print "Cannot guess last working directory!" 533 print (
"Please use the parameter '-N' or '--Name' to specify " 534 "the task for which you want a status report." )
538 outPath = os.path.abspath( options.Name )
541 if options.crabStatus:
543 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
544 if len( crabLogDirs ) == 0:
545 print "Found no crab tasks for job name '%s'"%( options.Name )
548 for crabLogDir
in crabLogDirs:
550 print "*" +
"=" * 78 +
"*" 551 print (
"| Status report and output retrieval for:" 552 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
554 taskName = crabLogDir.replace(
"crab.",
"" )
555 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|" 556 print "*" +
"=" * 78 +
"*" 558 crabOptions = {
"-getoutput":
"",
561 theCrab.run( crabOptions )
562 except AllInOneError
as e:
563 print "crab: No output retrieved for this task." 564 crabOptions = {
"-status":
"",
566 theCrab.run( crabOptions )
569 general = config.getGeneral()
570 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
571 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
572 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
573 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
575 if not os.path.exists( outPath ):
576 os.makedirs( outPath )
577 elif not os.path.isdir( outPath ):
578 raise AllInOneError(
"the file %s is in the way rename the Job or move it away"%outPath)
584 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
585 config.write( backupConfigFile )
588 for validation
in config.items(
"validation"):
589 alignmentList = [validation[1]]
590 validationsToAdd = [(validation[0],alignment) \
591 for alignment
in alignmentList]
592 validations.extend(validationsToAdd)
594 for validation
in validations ]
595 map(
lambda job: job.createJob(), jobs )
596 validations = [ job.getValidation()
for job
in jobs ]
601 map(
lambda job: job.runJob(), jobs )
603 if options.autoMerge:
606 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
607 print "> Automatically merging jobs when they have ended" 609 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
610 "jobName":
"TkAlMerge",
611 "logDir": config.getGeneral()[
"logdir"],
612 "script":
"TkAlMerge.sh",
613 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
614 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"' 616 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
617 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
618 if os.path.exists(oldlog):
622 "-o %(logDir)s/%(jobName)s.stdout " 623 "-e %(logDir)s/%(jobName)s.stderr " 625 "%(logDir)s/%(script)s"%repMap)
627 if __name__ ==
"__main__":
629 if "-d" in sys.argv[1:]
or "--debug" in sys.argv[1:]:
634 except AllInOneError
as e:
635 print "\nAll-In-One Tool:",
str(e)
def createMergeZmumuPlotsScript(zMuMuValidationList, outFilePath)
def createExtendedValidationScript(offlineValidationList, outFilePath, resultPlotFile)
— Classes —############################
def main(argv=None)
— Main —############################
def loadTemplates(config)
def createMergeScript(path, validations)
def getCommandOutput2(command)
def addIndex(filename, njobs, index=None)
def createOfflineParJobsMergeScript(offlineValidationList, outFilePath)
def __getValidation(self, valType, name, alignments, config, options)
def PlottingOptions(config, valType)
def replaceByMap(target, the_map)
— Helpers —############################
def __createJob(self, jobMode, outpath)
static std::string join(char **cmd)
def createPrimaryVertexPlotScript(PrimaryVertexValidationList, outFilePath)
def alternateTemplate(templateName, alternateTemplateName)
Alternate Templates ###
def __init__(self, validation, config, options)
def createTrackSplitPlotScript(trackSplittingValidationList, outFilePath)