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.preexistingValidation \
35 import Alignment.OfflineValidation.TkAlAllInOneTool.globalDictionaries \
49 def __init__( self, validation, config, options ):
50 if validation[1] ==
"":
52 valString = validation[0].
split(
"->" )[0]
53 alignments = validation[0].
split(
"->" )[1]
55 if "->" in validation[0]:
56 msg = (
"Instead of using the intermediate syntax\n'"
57 +valString.strip()+
"-> "+alignments.strip()
58 +
":'\nyou have to use the now fully supported syntax \n'"
59 +valString.strip()+
": "
60 +alignments.strip()+
"'.")
61 raise AllInOneError(msg)
63 valString = validation[0]
64 alignments = validation[1]
65 valString = valString.split()
77 print (
"offlineParallel and offline are now the same. To run an offline parallel validation,\n"
78 "just set parallelJobs to something > 1. There is no reason to call it offlineParallel anymore.")
81 if not self.__config.has_section( section ):
82 raise AllInOneError(
"Validation '%s' of type '%s' is requested in"
83 " '[validation]' section, but is not defined."
84 "\nYou have to add a '[%s]' section."
91 if valType ==
"compare":
92 alignmentsList = alignments.split(
"," )
93 firstAlignList = alignmentsList[0].
split()
94 firstAlignName = firstAlignList[0].strip()
95 if firstAlignName ==
"IDEAL":
96 raise AllInOneError(
"'IDEAL' has to be the second (reference)"
97 " alignment in 'compare <val_name>: "
98 "<alignment> <reference>'.")
99 if len( firstAlignList ) > 1:
100 firstRun = firstAlignList[1]
104 firstAlignName = firstAlign.name
105 secondAlignList = alignmentsList[1].
split()
106 secondAlignName = secondAlignList[0].strip()
107 if len( secondAlignList ) > 1:
108 secondRun = secondAlignList[1]
111 if secondAlignName ==
"IDEAL":
112 secondAlign = secondAlignName
116 secondAlignName = secondAlign.name
118 validation = GeometryComparison( name, firstAlign, secondAlign,
120 self.__commandLineOptions.getImages)
121 elif valType ==
"offline":
122 validation = OfflineValidation( name,
124 elif valType ==
"preexistingoffline":
125 validation = PreexistingOfflineValidation(name, self.
__config)
126 elif valType ==
"offlineDQM":
127 validation = OfflineValidationDQM( name,
129 elif valType ==
"mcValidate":
130 validation = MonteCarloValidation( name,
132 elif valType ==
"preexistingmcValidate":
133 validation = PreexistingMonteCarloValidation(name, self.
__config)
134 elif valType ==
"split":
135 validation = TrackSplittingValidation( name,
137 elif valType ==
"preexistingsplit":
138 validation = PreexistingTrackSplittingValidation(name, self.
__config)
139 elif valType ==
"zmumu":
140 validation = ZMuMuValidation( name,
143 raise AllInOneError(
"Unknown validation mode '%s'"%valType)
148 """This private method creates the needed files for the validation job.
150 self.validation.createConfiguration( outpath )
153 self.
__scripts = sum([
addIndex(script, self.validation.NJobs)
for script
in self.validation.createScript( outpath )], [])
154 if jobMode.split(
',' )[0] ==
"crab":
155 self.validation.createCrabCfg( outpath )
159 """This is the method called to create the job files."""
161 os.path.abspath( self.__commandLineOptions.Name) )
165 if self.validation.jobid:
166 self.batchJobIds.append(self.validation.jobid)
167 log =
"> " + self.validation.name +
" is already validated."
171 general = self.__config.getGeneral()
174 name = os.path.splitext( os.path.basename( script) )[0]
175 ValidationJob.jobCount += 1
176 if self.__commandLineOptions.dryRun:
177 print "%s would run: %s"%( name, os.path.basename( script) )
179 log =
"> Validating "+name
180 print "> Validating "+name
181 if self.validation.jobmode ==
"interactive":
183 ValidationJob.interactCount += 1
184 elif self.validation.jobmode.split(
",")[0] ==
"lxBatch":
186 "commands": self.validation.jobmode.split(
",")[1],
187 "logDir": general[
"logdir"],
190 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub"
192 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
193 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
194 if os.path.exists(oldlog):
198 "-o %(logDir)s/%(jobName)s.stdout "
199 "-e %(logDir)s/%(jobName)s.stderr "
203 ValidationJob.batchJobIds.append(bsubOut.split(
"<")[1].
split(
">")[0])
205 ValidationJob.batchCount += 1
206 elif self.validation.jobmode.split(
"," )[0] ==
"crab":
207 os.chdir( general[
"logdir"] )
208 crabName =
"crab." + os.path.basename( script )[:-3]
210 options = {
"-create":
"",
211 "-cfg": crabName +
".cfg",
214 theCrab.run( options )
215 except AllInOneError
as e:
216 print "crab:", str(e).
split(
"\n")[0]
218 ValidationJob.crabCount += 1
221 raise AllInOneError(
"Unknown 'jobmode'!\n"
222 "Please change this parameter either in "
223 "the [general] or in the ["
225 +
"] section to one of the following "
227 "\tinteractive\n\tlxBatch, -q <queue>\n"
228 "\tcrab, -q <queue>")
238 repMap = offlineValidationList[0].getRepMap()
240 theFile = open( outFilePath,
"w" )
241 theFile.write(
replaceByMap( configTemplates.mergeOfflineParJobsTemplate ,repMap ) )
245 repMap = offlineValidationList[0].getRepMap()
246 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
247 repMap[
"resultPlotFile" ] = resultPlotFile
248 repMap[
"extendedInstantiation" ] =
""
250 for validation
in offlineValidationList:
251 repMap[
"extendedInstantiation" ] = validation.appendToExtendedValidation( repMap[
"extendedInstantiation" ] )
253 theFile = open( outFilePath,
"w" )
254 theFile.write(
replaceByMap( configTemplates.extendedValidationTemplate ,repMap ) )
258 repMap = trackSplittingValidationList[0].getRepMap()
259 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
260 repMap[
"trackSplitPlotInstantiation" ] =
""
262 for validation
in trackSplittingValidationList:
263 repMap[
"trackSplitPlotInstantiation" ] = validation.appendToExtendedValidation( repMap[
"trackSplitPlotInstantiation" ] )
265 theFile = open( outFilePath,
"w" )
266 theFile.write(
replaceByMap( configTemplates.trackSplitPlotTemplate ,repMap ) )
270 repMap = zMuMuValidationList[0].getRepMap()
271 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
272 repMap[
"mergeZmumuPlotsInstantiation" ] =
""
274 for validation
in zMuMuValidationList:
275 repMap[
"mergeZmumuPlotsInstantiation" ] = validation.appendToExtendedValidation( repMap[
"mergeZmumuPlotsInstantiation" ] )
277 theFile = open( outFilePath,
"w" )
278 theFile.write(
replaceByMap( configTemplates.mergeZmumuPlotsTemplate ,repMap ) )
282 if(len(validations) == 0):
283 raise AllInOneError(
"Cowardly refusing to merge nothing!")
285 repMap = validations[0].getRepMap()
288 "CompareAlignments":
"",
289 "RunExtendedOfflineValidation":
"",
290 "RunTrackSplitPlot":
"",
291 "MergeZmumuPlots":
"",
292 "CMSSW_BASE": os.environ[
"CMSSW_BASE"],
293 "SCRAM_ARCH": os.environ[
"SCRAM_ARCH"],
294 "CMSSW_RELEASE_BASE": os.environ[
"CMSSW_RELEASE_BASE"],
298 for validation
in validations:
299 for referenceName
in validation.filesToCompare:
300 validationName =
"%s.%s"%(validation.__class__.__name__, referenceName)
301 validationName = validationName.split(
".%s"%GenericValidation.defaultReferenceName )[0]
302 validationName = validationName.split(
"Preexisting")[-1]
303 if validationName
in comparisonLists:
304 comparisonLists[ validationName ].
append( validation )
306 comparisonLists[ validationName ] = [ validation ]
310 repMap[
"haddLoop"] =
"mergeRetCode=0\n"
311 repMap[
"rmUnmerged"] = (
"if [[ mergeRetCode -eq 0 ]]; then\n"
312 " echo -e \\n\"Merging succeeded, removing original files.\"\n")
313 repMap[
"copyMergeScripts"] =
""
314 repMap[
"mergeParallelFilePrefixes"] =
""
317 for validationType
in comparisonLists:
318 for validation
in comparisonLists[validationType]:
319 if isinstance(validation, PreexistingValidation)
or validation.NJobs == 1:
321 if validationType
not in anythingToMerge:
322 anythingToMerge += [validationType]
323 repMap[
"haddLoop"] +=
'\n\n\n\necho -e "\n\nMerging results from %s jobs"\n\n' % validationType
324 repMap[
"haddLoop"] = validation.appendToMerge(repMap[
"haddLoop"])
325 repMap[
"haddLoop"] +=
"tmpMergeRetCode=${?}\n"
326 repMap[
"haddLoop"] += (
"if [[ tmpMergeRetCode -eq 0 ]]; then "
328 +validation.getRepMap()[
"finalOutputFile"]
329 +
" root://eoscms//eos/cms"
330 +validation.getRepMap()[
"finalResultFile"]
332 repMap[
"haddLoop"] += (
"if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then "
333 "mergeRetCode=${tmpMergeRetCode}; fi\n")
334 for f
in validation.getRepMap()[
"outputFiles"]:
335 longName = os.path.join(
"/store/caf/user/$USER/",
336 validation.getRepMap()[
"eosdir"], f)
337 repMap[
"rmUnmerged"] +=
" $eos rm "+longName+
"\n"
338 repMap[
"rmUnmerged"] += (
"else\n"
339 " echo -e \\n\"WARNING: Merging failed, unmerged"
340 " files won't be deleted.\\n"
341 "(Ignore this warning if merging was done earlier)\"\n"
344 if "OfflineValidation" in anythingToMerge:
345 repMap[
"mergeOfflineParJobsScriptPath"] = os.path.join(path,
"TkAlOfflineJobsMerge.C")
347 repMap[
"mergeOfflineParJobsScriptPath"] )
348 repMap[
"copyMergeScripts"] += (
"cp .oO[CMSSW_BASE]Oo./src/Alignment/OfflineValidation/scripts/merge_TrackerOfflineValidation.C .\n"
349 "rfcp %s .\n" % repMap[
"mergeOfflineParJobsScriptPath"])
354 repMap[
"DownloadData"] +=
replaceByMap( configTemplates.mergeParallelResults, repMap )
356 repMap[
"DownloadData"] =
""
359 if "OfflineValidation" in comparisonLists:
360 repMap[
"extendedValScriptPath"] = os.path.join(path,
"TkAlExtendedOfflineValidation.C")
362 repMap[
"extendedValScriptPath"],
364 repMap[
"RunExtendedOfflineValidation"] = \
365 replaceByMap(configTemplates.extendedValidationExecution, repMap)
367 if "TrackSplittingValidation" in comparisonLists:
368 repMap[
"trackSplitPlotScriptPath"] = \
369 os.path.join(path,
"TkAlTrackSplitPlot.C")
371 repMap[
"trackSplitPlotScriptPath"] )
372 repMap[
"RunTrackSplitPlot"] = \
373 replaceByMap(configTemplates.trackSplitPlotExecution, repMap)
375 if "ZMuMuValidation" in comparisonLists:
376 repMap[
"mergeZmumuPlotsScriptPath"] = \
377 os.path.join(path,
"TkAlMergeZmumuPlots.C")
379 repMap[
"mergeZmumuPlotsScriptPath"] )
380 repMap[
"MergeZmumuPlots"] = \
381 replaceByMap(configTemplates.mergeZmumuPlotsExecution, repMap)
383 repMap[
"CompareAlignments"] =
"#run comparisons"
384 if "OfflineValidation" in comparisonLists:
385 compareStrings = [ val.getCompareStrings(
"OfflineValidation")
for val
in comparisonLists[
"OfflineValidation"] ]
386 compareStringsPlain = [ val.getCompareStrings(
"OfflineValidation", plain=
True)
for val
in comparisonLists[
"OfflineValidation"] ]
388 repMap.update({
"validationId":
"OfflineValidation",
389 "compareStrings":
" , ".
join(compareStrings),
390 "compareStringsPlain":
" ".
join(compareStringsPlain) })
392 repMap[
"CompareAlignments"] += \
393 replaceByMap(configTemplates.compareAlignmentsExecution, repMap)
395 filePath = os.path.join(path,
"TkAlMerge.sh")
396 theFile = open( filePath,
"w" )
397 theFile.write(
replaceByMap( configTemplates.mergeTemplate, repMap ) )
399 os.chmod(filePath,0o755)
404 if config.has_section(
"alternateTemplates"):
405 for templateName
in config.options(
"alternateTemplates"):
406 if templateName ==
"AutoAlternates":
408 newTemplateName = config.get(
"alternateTemplates", templateName )
417 optParser = optparse.OptionParser()
418 optParser.description =
"""All-in-one Alignment Validation.
419 This will run various validation procedures either on batch queues or interactively.
420 If no name is given (-N parameter) a name containing time and date is created automatically.
421 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
423 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
424 help=
"create all scripts and cfg File but do not start jobs (default=False)")
425 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
True,
426 help=
"get all Images created during the process (default= True)")
427 defaultConfig =
"TkAlConfig.ini"
428 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
429 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
430 optParser.add_option(
"-N",
"--Name", dest=
"Name",
431 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
432 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
433 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
434 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
435 help=
"get the status of the crab jobs", metavar=
"STATUS")
436 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
438 help=
"run the tool to get full traceback of errors",
440 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
441 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
442 " Works only for batch jobs")
444 (options, args) = optParser.parse_args(argv)
446 if not options.restrictTo ==
None:
447 options.restrictTo = options.restrictTo.split(
",")
449 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
450 options.config.split(
"," ) ]
451 config = BetterConfigParser()
452 outputIniFileSet = set( config.read( options.config ) )
453 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
456 if options.config == [ os.path.abspath( defaultConfig ) ]:
457 if (
not options.crabStatus )
and \
458 (
not os.path.exists( defaultConfig ) ):
459 raise AllInOneError(
"Default 'ini' file '%s' not found!\n"
460 "You can specify another name with the "
461 "command line option '-c'/'--config'."
464 for iniFile
in failedIniFiles:
465 if not os.path.exists( iniFile ):
466 raise AllInOneError(
"'%s' does not exist. Please check for "
467 "typos in the filename passed to the "
468 "'-c'/'--config' option!"
471 raise AllInOneError((
"'%s' does exist, but parsing of the "
472 "content failed!" ) % iniFile)
475 if options.Name ==
None:
476 if not options.crabStatus:
477 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
479 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
480 "alignmentValidation_*" )
481 if len( existingValDirs ) > 0:
482 options.Name = existingValDirs[-1]
484 print "Cannot guess last working directory!"
485 print (
"Please use the parameter '-N' or '--Name' to specify "
486 "the task for which you want a status report." )
490 outPath = os.path.abspath( options.Name )
493 if options.crabStatus:
495 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
496 if len( crabLogDirs ) == 0:
497 print "Found no crab tasks for job name '%s'"%( options.Name )
500 for crabLogDir
in crabLogDirs:
502 print "*" +
"=" * 78 +
"*"
503 print (
"| Status report and output retrieval for:"
504 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
506 taskName = crabLogDir.replace(
"crab.",
"" )
507 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
508 print "*" +
"=" * 78 +
"*"
510 crabOptions = {
"-getoutput":
"",
513 theCrab.run( crabOptions )
514 except AllInOneError
as e:
515 print "crab: No output retrieved for this task."
516 crabOptions = {
"-status":
"",
518 theCrab.run( crabOptions )
521 general = config.getGeneral()
522 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
523 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
524 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
525 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
527 if not os.path.exists( outPath ):
528 os.makedirs( outPath )
529 elif not os.path.isdir( outPath ):
530 raise AllInOneError(
"the file %s is in the way rename the Job or move it away"%outPath)
536 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
537 config.write( backupConfigFile )
540 for validation
in config.items(
"validation"):
541 alignmentList = [validation[1]]
542 validationsToAdd = [(validation[0],alignment) \
543 for alignment
in alignmentList]
544 validations.extend(validationsToAdd)
546 for validation
in validations ]
547 map(
lambda job: job.createJob(), jobs )
548 validations = [ job.getValidation()
for job
in jobs ]
553 map(
lambda job: job.runJob(), jobs )
555 if options.autoMerge:
558 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
559 print "> Automatically merging jobs when they have ended"
561 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
562 "jobName":
"TkAlMerge",
563 "logDir": config.getGeneral()[
"logdir"],
564 "script":
"TkAlMerge.sh",
565 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
566 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
568 for ext
in (
"stdout",
"stderr",
"stdout.gz",
"stderr.gz"):
569 oldlog =
"%(logDir)s/%(jobName)s."%repMap + ext
570 if os.path.exists(oldlog):
574 "-o %(logDir)s/%(jobName)s.stdout "
575 "-e %(logDir)s/%(jobName)s.stderr "
577 "%(logDir)s/%(script)s"%repMap)
579 if __name__ ==
"__main__":
581 if "-d" in sys.argv[1:]
or "--debug" in sys.argv[1:]:
586 except AllInOneError
as e:
587 print "\nAll-In-One Tool:", str(e)
def createTrackSplitPlotScript
— Classes —############################
def createMergeZmumuPlotsScript
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
def createOfflineParJobsMergeScript
def main
— Main —############################
def createExtendedValidationScript
def alternateTemplate
### Alternate Templates ###
static std::string join(char **cmd)
def replaceByMap
— Helpers —############################