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 import Alignment.OfflineValidation.TkAlAllInOneTool.globalDictionaries \
47 def __init__( self, validation, config, options ):
48 if validation[1] ==
"":
50 valString = validation[0].
split(
"->" )[0]
51 alignments = validation[0].
split(
"->" )[1]
53 if "->" in validation[0]:
54 msg = (
"Instead of using the intermediate syntax\n'"
55 +valString.strip()+
"-> "+alignments.strip()
56 +
":'\nyou have to use the now fully supported syntax \n'"
57 +valString.strip()+
": "
58 +alignments.strip()+
"'.")
59 raise AllInOneError(msg)
61 valString = validation[0]
62 alignments = validation[1]
63 valString = valString.split()
70 print (
"offlineParallel and offline are now the same. To run an offline parallel validation,\n"
71 "just set parallelJobs to something > 1. There is no reason to call it offlineParallel anymore.")
74 if not self.__config.has_section( section ):
75 raise AllInOneError, (
"Validation '%s' of type '%s' is requested in"
76 " '[validation]' section, but is not defined."
77 "\nYou have to add a '[%s]' section."
84 if valType ==
"compare":
85 alignmentsList = alignments.split(
"," )
86 firstAlignList = alignmentsList[0].
split()
87 firstAlignName = firstAlignList[0].strip()
88 if firstAlignName ==
"IDEAL":
89 raise AllInOneError, (
"'IDEAL' has to be the second (reference)"
90 " alignment in 'compare <val_name>: "
91 "<alignment> <reference>'.")
92 if len( firstAlignList ) > 1:
93 firstRun = firstAlignList[1]
96 firstAlign = Alignment( firstAlignName, self.
__config, firstRun )
97 secondAlignList = alignmentsList[1].
split()
98 secondAlignName = secondAlignList[0].strip()
99 if len( secondAlignList ) > 1:
100 secondRun = secondAlignList[1]
103 if secondAlignName ==
"IDEAL":
104 secondAlign = secondAlignName
106 secondAlign = Alignment( secondAlignName, self.
__config,
110 randomWorkdirPart = \
111 globalDictionaries.alignRandDict[firstAlignName]
113 randomWorkdirPart =
None
115 validation = GeometryComparison( name, firstAlign, secondAlign,
117 self.__commandLineOptions.getImages,
119 globalDictionaries.alignRandDict[firstAlignName] = \
120 validation.randomWorkdirPart
121 if not secondAlignName ==
"IDEAL":
122 globalDictionaries.alignRandDict[secondAlignName] = \
123 validation.randomWorkdirPart
124 elif valType ==
"offline":
125 validation = OfflineValidation( name,
127 elif valType ==
"offlineDQM":
128 validation = OfflineValidationDQM( name,
130 elif valType ==
"mcValidate":
131 validation = MonteCarloValidation( name,
133 elif valType ==
"split":
134 validation = TrackSplittingValidation( name,
136 elif valType ==
"zmumu":
137 validation = ZMuMuValidation( name,
140 raise AllInOneError,
"Unknown validation mode '%s'"%valType
144 """This private method creates the needed files for the validation job.
146 self.validation.createConfiguration( outpath )
147 self.
__scripts = sum([
addIndex(script, self.validation.NJobs)
for script
in self.validation.createScript( outpath )], [])
148 if jobMode.split(
',' )[0] ==
"crab":
149 self.validation.createCrabCfg( outpath )
153 """This is the method called to create the job files."""
155 os.path.abspath( self.__commandLineOptions.Name) )
158 general = self.__config.getGeneral()
161 name = os.path.splitext( os.path.basename( script) )[0]
162 ValidationJob.jobCount += 1
163 if self.__commandLineOptions.dryRun:
164 print "%s would run: %s"%( name, os.path.basename( script) )
166 log =
"> Validating "+name
167 print "> Validating "+name
168 if self.validation.jobmode ==
"interactive":
170 ValidationJob.interactCount += 1
171 elif self.validation.jobmode.split(
",")[0] ==
"lxBatch":
173 "commands": self.validation.jobmode.split(
",")[1],
174 "logDir": general[
"logdir"],
177 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub"
181 "-o %(logDir)s/%(jobName)s.stdout "
182 "-e %(logDir)s/%(jobName)s.stderr "
186 ValidationJob.batchJobIds.append(bsubOut.split(
"<")[1].
split(
">")[0])
188 ValidationJob.batchCount += 1
189 elif self.validation.jobmode.split(
"," )[0] ==
"crab":
190 os.chdir( general[
"logdir"] )
191 crabName =
"crab." + os.path.basename( script )[:-3]
193 options = {
"-create":
"",
194 "-cfg": crabName +
".cfg",
197 theCrab.run( options )
198 except AllInOneError, e:
199 print "crab:", str(e).
split(
"\n")[0]
201 ValidationJob.crabCount += 1
204 raise AllInOneError, (
"Unknown 'jobmode'!\n"
205 "Please change this parameter either in "
206 "the [general] or in the ["
208 +
"] section to one of the following "
210 "\tinteractive\n\tlxBatch, -q <queue>\n"
211 "\tcrab, -q <queue>")
221 repMap = offlineValidationList[0].getRepMap()
223 theFile = open( outFilePath,
"w" )
224 theFile.write(
replaceByMap( configTemplates.mergeOfflineParJobsTemplate ,repMap ) )
228 repMap = offlineValidationList[0].getRepMap()
229 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
230 repMap[
"resultPlotFile" ] = resultPlotFile
231 repMap[
"extendedInstantiation" ] =
""
233 for validation
in offlineValidationList:
234 repMap[
"extendedInstantiation" ] = validation.appendToExtendedValidation( repMap[
"extendedInstantiation" ] )
236 theFile = open( outFilePath,
"w" )
238 theFile.write(
replaceByMap( configTemplates.extendedValidationTemplate ,repMap ) )
242 repMap = trackSplittingValidationList[0].getRepMap()
243 repMap[
"CMSSW_BASE" ] = os.environ[
'CMSSW_BASE']
244 repMap[
"trackSplitPlotInstantiation" ] =
""
246 for validation
in trackSplittingValidationList:
247 repMap[
"trackSplitPlotInstantiation" ] = validation.appendToExtendedValidation( repMap[
"trackSplitPlotInstantiation" ] )
249 theFile = open( outFilePath,
"w" )
251 theFile.write(
replaceByMap( configTemplates.trackSplitPlotTemplate ,repMap ) )
255 if(len(validations) == 0):
256 raise AllInOneError(
"Cowardly refusing to merge nothing!")
258 repMap = validations[0].getRepMap()
261 "CompareAlignments":
"",
262 "RunExtendedOfflineValidation":
"",
263 "RunTrackSplitPlot":
""
267 for validation
in validations:
268 for referenceName
in validation.filesToCompare:
269 validationName =
"%s.%s"%(validation.__class__.__name__, referenceName)
270 validationName = validationName.split(
".%s"%GenericValidation.defaultReferenceName )[0]
271 if validationName
in comparisonLists:
272 comparisonLists[ validationName ].
append( validation )
274 comparisonLists[ validationName ] = [ validation ]
278 repMap[
"haddLoop"] =
"mergeRetCode=0\n"
279 repMap[
"rmUnmerged"] = (
"if [[ mergeRetCode -eq 0 ]]; then\n"
280 " echo -e \\n\"Merging succeeded, removing original files.\"\n")
281 repMap[
"copyMergeScripts"] =
""
282 repMap[
"mergeParallelFilePrefixes"] =
""
285 for validationType
in comparisonLists:
286 for validation
in comparisonLists[validationType]:
287 if validation.NJobs == 1:
289 if validationType
not in anythingToMerge:
290 anythingToMerge += [validationType]
291 repMap[
"haddLoop"] +=
'\n\n\n\necho -e "\n\nMerging results from %s jobs"\n\n' % validationType
292 repMap[
"haddLoop"] = validation.appendToMerge(repMap[
"haddLoop"])
293 repMap[
"haddLoop"] +=
"tmpMergeRetCode=${?}\n"
294 repMap[
"haddLoop"] += (
"if [[ tmpMergeRetCode -eq 0 ]]; then "
296 +validation.getRepMap()[
"finalOutputFile"]
298 +validation.getRepMap()[
"finalResultFile"]
300 repMap[
"haddLoop"] += (
"if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then "
301 "mergeRetCode=${tmpMergeRetCode}; fi\n")
302 for f
in validation.getRepMap()[
"outputFiles"]:
303 longName = os.path.join(
"/store/caf/user/$USER/",
304 validation.getRepMap()[
"eosdir"], f)
305 repMap[
"rmUnmerged"] +=
" cmsRm "+longName+
"\n"
306 repMap[
"rmUnmerged"] += (
"else\n"
307 " echo -e \\n\"WARNING: Merging failed, unmerged"
308 " files won't be deleted.\\n"
309 "(Ignore this warning if merging was done earlier)\"\n"
312 if "OfflineValidation" in anythingToMerge:
313 repMap[
"mergeOfflineParJobsScriptPath"] = os.path.join(path,
"TkAlOfflineJobsMerge.C")
315 repMap[
"mergeOfflineParJobsScriptPath"] )
316 repMap[
"copyMergeScripts"] += (
"cp .oO[CMSSW_BASE]Oo./src/Alignment/OfflineValidation/scripts/merge_TrackerOfflineValidation.C .\n"
317 "rfcp %s .\n" % repMap[
"mergeOfflineParJobsScriptPath"])
322 repMap[
"DownloadData"] +=
replaceByMap( configTemplates.mergeParallelResults, repMap )
324 repMap[
"DownloadData"] =
""
327 if "OfflineValidation" in comparisonLists:
328 repMap[
"extendedValScriptPath"] = os.path.join(path,
"TkAlExtendedOfflineValidation.C")
330 repMap[
"extendedValScriptPath"],
332 repMap[
"RunExtendedOfflineValidation"] = \
333 replaceByMap(configTemplates.extendedValidationExecution, repMap)
335 if "TrackSplittingValidation" in comparisonLists:
336 repMap[
"trackSplitPlotScriptPath"] = \
337 os.path.join(path,
"TkAlTrackSplitPlot.C")
339 repMap[
"trackSplitPlotScriptPath"] )
340 repMap[
"RunTrackSplitPlot"] = \
341 replaceByMap(configTemplates.trackSplitPlotExecution, repMap)
343 repMap[
"CompareAlignments"] =
"#run comparisons"
344 for validationId
in comparisonLists:
345 compareStrings = [ val.getCompareStrings(validationId)
for val
in comparisonLists[validationId] ]
346 compareStringsPlain = [ val.getCompareStrings(validationId, plain=
True)
for val
in comparisonLists[validationId] ]
348 repMap.update({
"validationId": validationId,
349 "compareStrings":
" , ".
join(compareStrings),
350 "compareStringsPlain":
" ".
join(compareStringsPlain) })
352 repMap[
"CompareAlignments"] += \
353 replaceByMap(configTemplates.compareAlignmentsExecution, repMap)
355 filePath = os.path.join(path,
"TkAlMerge.sh")
356 theFile = open( filePath,
"w" )
357 theFile.write(
replaceByMap( configTemplates.mergeTemplate, repMap ) )
359 os.chmod(filePath,0755)
364 if config.has_section(
"alternateTemplates"):
365 for templateName
in config.options(
"alternateTemplates"):
366 if templateName ==
"AutoAlternates":
368 newTemplateName = config.get(
"alternateTemplates", templateName )
377 optParser = optparse.OptionParser()
378 optParser.description =
"""All-in-one Alignment Validation.
379 This will run various validation procedures either on batch queues or interactviely.
380 If no name is given (-N parameter) a name containing time and date is created automatically.
381 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
383 optParser.add_option(
"-n",
"--dryRun", dest=
"dryRun", action=
"store_true", default=
False,
384 help=
"create all scripts and cfg File but do not start jobs (default=False)")
385 optParser.add_option(
"--getImages", dest=
"getImages", action=
"store_true", default=
False,
386 help=
"get all Images created during the process (default= False)")
387 defaultConfig =
"TkAlConfig.ini"
388 optParser.add_option(
"-c",
"--config", dest=
"config", default = defaultConfig,
389 help=
"configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar=
"CONFIG")
390 optParser.add_option(
"-N",
"--Name", dest=
"Name",
391 help=
"Name of this validation (default: alignmentValidation_DATE_TIME)", metavar=
"NAME")
392 optParser.add_option(
"-r",
"--restrictTo", dest=
"restrictTo",
393 help=
"restrict validations to given modes (comma seperated) (default: no restriction)", metavar=
"RESTRICTTO")
394 optParser.add_option(
"-s",
"--status", dest=
"crabStatus", action=
"store_true", default =
False,
395 help=
"get the status of the crab jobs", metavar=
"STATUS")
396 optParser.add_option(
"-d",
"--debug", dest=
"debugMode", action=
"store_true",
398 help=
"run the tool to get full traceback of errors",
400 optParser.add_option(
"-m",
"--autoMerge", dest=
"autoMerge", action=
"store_true", default =
False,
401 help=
"submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
402 " Works only for batch jobs")
404 (options, args) = optParser.parse_args(argv)
406 if not options.restrictTo ==
None:
407 options.restrictTo = options.restrictTo.split(
",")
409 options.config = [ os.path.abspath( iniFile )
for iniFile
in \
410 options.config.split(
"," ) ]
411 config = BetterConfigParser()
412 outputIniFileSet = set( config.read( options.config ) )
413 failedIniFiles = [ iniFile
for iniFile
in options.config
if iniFile
not in outputIniFileSet ]
416 if options.config == [ os.path.abspath( defaultConfig ) ]:
417 if (
not options.crabStatus )
and \
418 (
not os.path.exists( defaultConfig ) ):
419 raise AllInOneError, (
"Default 'ini' file '%s' not found!\n"
420 "You can specify another name with the "
421 "command line option '-c'/'--config'."
424 for iniFile
in failedIniFiles:
425 if not os.path.exists( iniFile ):
426 raise AllInOneError, (
"'%s' does not exist. Please check for "
427 "typos in the filename passed to the "
428 "'-c'/'--config' option!"
431 raise AllInOneError, (
"'%s' does exist, but parsing of the "
435 if options.Name ==
None:
436 if not options.crabStatus:
437 options.Name =
"alignmentValidation_%s"%(datetime.datetime.now().strftime(
"%y%m%d_%H%M%S"))
439 existingValDirs = fnmatch.filter( os.walk(
'.' ).
next()[1],
440 "alignmentValidation_*" )
441 if len( existingValDirs ) > 0:
442 options.Name = existingValDirs[-1]
444 print "Cannot guess last working directory!"
445 print (
"Please use the parameter '-N' or '--Name' to specify "
446 "the task for which you want a status report." )
450 outPath = os.path.abspath( options.Name )
453 if options.crabStatus:
455 crabLogDirs = fnmatch.filter( os.walk(
'.').
next()[1],
"crab.*" )
456 if len( crabLogDirs ) == 0:
457 print "Found no crab tasks for job name '%s'"%( options.Name )
460 for crabLogDir
in crabLogDirs:
462 print "*" +
"=" * 78 +
"*"
463 print (
"| Status report and output retrieval for:"
464 +
" " * (77 - len(
"Status report and output retrieval for:" ) )
466 taskName = crabLogDir.replace(
"crab.",
"" )
467 print "| " + taskName +
" " * (77 - len( taskName ) ) +
"|"
468 print "*" +
"=" * 78 +
"*"
470 crabOptions = {
"-getoutput":
"",
473 theCrab.run( crabOptions )
474 except AllInOneError, e:
475 print "crab: No output retrieved for this task."
476 crabOptions = {
"-status":
"",
478 theCrab.run( crabOptions )
481 general = config.getGeneral()
482 config.set(
"internals",
"workdir",os.path.join(general[
"workdir"],options.Name) )
483 config.set(
"general",
"datadir",os.path.join(general[
"datadir"],options.Name) )
484 config.set(
"general",
"logdir",os.path.join(general[
"logdir"],options.Name) )
485 config.set(
"general",
"eosdir",os.path.join(
"AlignmentValidation", general[
"eosdir"], options.Name) )
489 if os.path.isdir( outPath ):
490 shutil.rmtree( outPath )
492 if not os.path.exists( outPath ):
493 os.makedirs( outPath )
494 elif not os.path.isdir( outPath ):
495 raise AllInOneError,
"the file %s is in the way rename the Job or move it away"%outPath
501 backupConfigFile = open( os.path.join( outPath,
"usedConfiguration.ini" ) ,
"w" )
502 config.write( backupConfigFile )
505 for validation
in config.items(
"validation"):
506 alignmentList = validation[1].
split(config.getSep())
507 validationsToAdd = [(validation[0],alignment) \
508 for alignment
in alignmentList]
509 validations.extend(validationsToAdd)
511 for validation
in validations ]
512 map(
lambda job: job.createJob(), jobs )
513 validations = [ job.getValidation()
for job
in jobs ]
518 map(
lambda job: job.runJob(), jobs )
520 if options.autoMerge:
523 if ValidationJob.jobCount == ValidationJob.batchCount
and config.getGeneral()[
"jobmode"].
split(
",")[0] ==
"lxBatch":
524 print "> Automatically merging jobs when they have ended"
526 "commands": config.getGeneral()[
"jobmode"].
split(
",")[1],
527 "jobName":
"TkAlMerge",
528 "logDir": config.getGeneral()[
"logdir"],
529 "script":
"TkAlMerge.sh",
530 "bsub":
"/afs/cern.ch/cms/caf/scripts/cmsbsub",
531 "conditions":
'"' +
" && ".
join([
"ended(" + jobId +
")" for jobId
in ValidationJob.batchJobIds]) +
'"'
534 "-o %(logDir)s/%(jobName)s.stdout "
535 "-e %(logDir)s/%(jobName)s.stderr "
537 "%(logDir)s/%(script)s"%repMap)
539 if __name__ ==
"__main__":
541 if "-d" in sys.argv[1:]
or "--debug" in sys.argv[1:]:
546 except AllInOneError, e:
547 print "\nAll-In-One Tool:", str(e)
def createTrackSplitPlotScript
— Classes —############################
def createOfflineParJobsMergeScript
def main
— Main —############################
def createExtendedValidationScript
def alternateTemplate
### Alternate Templates ###
static std::string join(char **cmd)
def replaceByMap
— Helpers —############################
if(conf.exists("allCellsPositionCalc"))