1 from __future__
import print_function
2 from abc
import ABCMeta, abstractmethod, abstractproperty
6 import globalDictionaries
8 from dataset
import Dataset
9 from helperFunctions
import replaceByMap, addIndex, getCommandOutput2, boolfromstring, pythonboolstring
10 from TkAlExceptions
import AllInOneError
13 sets = [
"mandatories",
"optionals",
"needpackages"]
16 for setname
in cls.
sets:
17 if setname
not in dct: dct[setname] = set()
18 dct[setname] = set.union(dct[setname], *(getattr(base, setname)
for base
in bases
if hasattr(base, setname)))
20 for dictname
in cls.
dicts:
21 if dictname
not in dct: dct[dictname] = {}
23 if not hasattr(base, dictname):
continue 24 newdict = getattr(base, dictname)
25 for key
in set(newdict) & set(dct[dictname]):
26 if newdict[key] != dct[dictname][key]:
27 raise ValueError(
"Inconsistent values of defaults[{}]: {}, {}".
format(key, newdict[key], dct[dictname][key]))
28 dct[dictname].
update(newdict)
30 for setname
in cls.
sets:
32 if "remove"+setname
not in dct: dct[
"remove"+setname] = set()
33 dct[
"remove"+setname] = set.union(dct[
"remove"+setname], *(getattr(base,
"remove"+setname)
for base
in bases
if hasattr(base,
"remove"+setname)))
35 dct[setname] -= dct[
"remove"+setname]
37 return super(ValidationMetaClass, cls).
__new__(cls, clsname, bases, dct)
40 __metaclass__ = ValidationMetaClass
41 defaultReferenceName =
"DEFAULT" 44 "cmssw": os.environ[
'CMSSW_BASE'],
47 "needsproxy":
"false",
49 needpackages = {
"Alignment/OfflineValidation"}
50 optionals = {
"jobmode"}
52 def __init__(self, valName, alignment, config):
62 theUpdate = config.getResultingSection(self.valType+
":"+self.
name,
65 self.general.update(theUpdate)
72 maximumNumberJobs = 40
73 if self.
NJobs > maximumNumberJobs:
74 msg = (
"Maximum allowed number of parallel jobs " 75 +
str(maximumNumberJobs)+
" exceeded!!!")
77 if self.
NJobs > 1
and not isinstance(self, ParallelValidation):
79 "Please set parallelJobs = 1.".
format(type(self).__name__))
85 if "is not found" in output:
raise RuntimeError
91 for character
in badcharacters:
92 if character
in self.
cmssw:
93 raise AllInOneError(
"The bad characters " + badcharacters +
" are not allowed in the cmssw\n" 94 "path name. If you really have it in such a ridiculously named location,\n" 95 "try making a symbolic link somewhere with a decent name.")
97 os.listdir(self.
cmssw)
101 if self.
cmssw == os.environ[
"CMSSW_BASE"]:
105 command = (
"cd '" + self.
cmssw +
"' && eval `scramv1 ru -sh 2> /dev/null`" 106 ' && echo "$CMSSW_BASE\n$SCRAM_ARCH\n$CMSSW_RELEASE_BASE"')
108 self.
cmssw = commandoutput[0]
115 pkgpath = os.path.join(placetolook,
"src", package)
116 if os.path.exists(pkgpath):
123 if config.has_option(
"alternateTemplates",
"AutoAlternates"):
125 self.
AutoAlternates = json.loads(config.get(
"alternateTemplates",
"AutoAlternates").lower())
127 raise AllInOneError(
"AutoAlternates needs to be true or false, not %s" % config.get(
"alternateTemplates",
"AutoAlternates"))
131 config.checkInput(self.valType+
":"+self.
name,
132 knownSimpleOptions = knownOpts,
133 ignoreOptions = ignoreOpts)
136 from plottingOptions
import PlottingOptions
137 if alignment ==
None:
143 result.update(alignment.getRepMap())
146 "workdir": os.path.join(self.
general[
"workdir"],
148 "datadir": self.
general[
"datadir"],
149 "logdir": self.
general[
"logdir"],
150 "CommandLineTemplate": (
"#run configfile and post-proccess it\n" 151 "cmsRun %(cfgFile)s\n" 153 "CMSSW_BASE": self.
cmssw,
156 "alignmentName": alignment.name,
157 "condLoad": alignment.getConditions(),
158 "LoadGlobalTagTemplate": configTemplates.loadGlobalTagTemplate,
169 repMap = self.getRepMap().
copy()
170 for validationId
in self.filesToCompare:
171 repMap[
"file"] = self.filesToCompare[ validationId ]
172 if repMap[
"file"].startswith(
"/castor/" ):
173 repMap[
"file"] =
"rfio:%(file)s"%repMap
174 elif repMap[
"file"].startswith(
"/store/" ):
175 repMap[
"file"] =
"root://eoscms.cern.ch//eos/cms%(file)s"%repMap
177 result[validationId]=repMap[
"file"]
179 result[validationId]=
"%(file)s=%(title)s|%(color)s|%(style)s"%repMap
180 if requestId ==
None:
183 if not "." in requestId:
184 requestId +=
".%s"%self.defaultReferenceName
185 if not requestId.split(
".")[-1]
in result:
186 msg = (
"could not find %s in reference Objects!" 187 %requestId.split(
".")[-1])
188 raise AllInOneError(msg)
189 return result[ requestId.split(
".")[-1] ]
191 def createFiles(self, fileContents, path, repMap = None, repMaps = None):
192 """repMap: single map for all files 193 repMaps: a dict, with the filenames as the keys""" 194 if repMap
is not None and repMaps
is not None:
195 raise AllInOneError(
"createFiles can only take repMap or repMaps (or neither), not both")
197 for fileName
in fileContents:
198 filePath = os.path.join(path, fileName)
199 result.append(filePath)
201 for (i, filePathi)
in enumerate(
addIndex(filePath, self.
NJobs)):
202 theFile = open( filePathi,
"w" )
203 fileContentsi = fileContents[ fileName ]
204 if repMaps
is not None:
205 repMap = repMaps[fileName]
206 if repMap
is not None:
207 repMap.update({
"nIndex":
str(i)})
209 theFile.write( fileContentsi )
216 path, repMap = repMap, repMaps = repMaps)
217 if not schedule ==
None:
218 schedule = [os.path.join( path, cfgName)
for cfgName
in schedule]
219 for cfgName
in schedule:
221 msg = (
"scheduled %s missing in generated configfiles: %s" 225 if not cfgName
in schedule:
226 msg = (
"generated configuration %s not scheduled: %s" 227 %(cfgName, schedule))
232 def createScript(self, fileContents, path, downloadFiles=[], repMap = None, repMaps = None):
234 path, repMap = repMap, repMaps = repMaps)
237 os.chmod(scriptwithindex,0o755)
242 msg = (
"jobmode 'crab' not supported for parallel validation." 243 " Please set parallelJobs = 1.")
251 Subclass of `GenericValidation` which is the base for validations using 254 needParentFiles =
False 255 mandatories = {
"dataset",
"maxevents"}
263 "dasinstance":
"prod/global",
264 "ttrhbuilder":
"WithAngleAndTemplate",
265 "usepixelqualityflag":
"True",
267 optionals = {
"magneticfield"}
271 This method adds additional items to the `self.general` dictionary 272 which are only needed for validations using datasets. 275 - `valName`: String which identifies individual validation instances 276 - `alignment`: `Alignment` instance to validate 277 - `config`: `BetterConfigParser` instance which includes the 278 configuration of the validations 281 super(GenericValidationData, self).
__init__(valName, alignment, config)
286 msg = (
"Maximum number of events (maxevents) not specified: " 287 "cannot use parallel jobs.")
290 msg = (
"maxevents has to be divisible by parallelJobs")
293 tryPredefinedFirst = (
not self.jobmode.split(
',' )[0] ==
"crab" and self.
general[
"JSON"] ==
"" 294 and self.
general[
"firstRun"] ==
"" and self.
general[
"lastRun"] ==
"" 297 if self.
general[
"dataset"]
not in globalDictionaries.usedDatasets:
298 globalDictionaries.usedDatasets[self.
general[
"dataset"]] = {}
300 if self.
cmssw not in globalDictionaries.usedDatasets[self.
general[
"dataset"]]:
301 if globalDictionaries.usedDatasets[self.
general[
"dataset"]] != {}:
302 print((
"Warning: you use the same dataset '%s' in multiple cmssw releases.\n" 303 "This is allowed, but make sure it's not a mistake") % self.
general[
"dataset"])
304 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw] = {
False:
None,
True:
None}
306 Bfield = self.general.get(
"magneticfield",
None)
307 if globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][tryPredefinedFirst]
is None:
309 self.
general[
"dataset"], tryPredefinedFirst = tryPredefinedFirst,
311 dasinstance = self.
general[
"dasinstance"])
312 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][tryPredefinedFirst] = dataset
313 if tryPredefinedFirst
and not dataset.predefined():
314 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][
False] = dataset
317 self.
general[
"magneticField"] = self.dataset.magneticField()
318 self.
general[
"defaultMagneticField"] =
"MagneticField" 319 if self.
general[
"magneticField"] ==
"unknown":
320 print(
"Could not get the magnetic field for this dataset.")
321 print(
"Using the default: ", self.
general[
"defaultMagneticField"])
322 self.
general[
"magneticField"] =
'.oO[defaultMagneticField]Oo.' 324 if not self.jobmode.split(
',' )[0] ==
"crab":
326 self.
general[
"datasetDefinition"] = self.dataset.datasetSnippet(
327 jsonPath = self.
general[
"JSON"],
328 firstRun = self.
general[
"firstRun"],
329 lastRun = self.
general[
"lastRun"],
333 except AllInOneError
as e:
334 msg =
"In section [%s:%s]: "%(self.valType, self.
name)
338 if self.dataset.predefined():
339 msg = (
"For jobmode 'crab' you cannot use predefined datasets " 340 "(in your case: '%s')."%( self.dataset.name() ))
343 theUpdate = config.getResultingSection(self.valType+
":"+self.
name,
344 demandPars = [
"parallelJobs"])
345 except AllInOneError
as e:
346 msg =
str(e)[:-1]+
" when using 'jobmode: crab'." 348 self.general.update(theUpdate)
353 self.
general[
"lastRun"] ) = self.dataset.convertTimeToRun(
354 firstRun = self.
general[
"firstRun"],
355 lastRun = self.
general[
"lastRun"],
359 if self.
general[
"begin"] ==
None:
361 if self.
general[
"end"] ==
None:
365 if (
not self.
general[
"firstRun"] )
and \
368 self.dataset.runList()[0][
"run_number"])
369 if (
not self.
general[
"lastRun"] )
and \
372 self.dataset.runList()[-1][
"run_number"])
375 msg = (
"The lower time/runrange limit ('begin'/'firstRun') " 376 "chosen is greater than the upper time/runrange limit " 377 "('end'/'lastRun').")
380 +
'-' + self.
general[
"lastRun"])
382 self.
general[
"datasetDefinition"] = self.dataset.datasetSnippet(
383 jsonPath = self.
general[
"JSON"],
384 firstRun = self.
general[
"firstRun"],
385 lastRun = self.
general[
"lastRun"],
389 except AllInOneError
as e:
390 msg =
"In section [%s:%s]: "%(self.valType, self.
name)
397 result = super(GenericValidationData, self).
getRepMap(alignment)
399 "%s_%s_.oO[name]Oo..root" % (self.outputBaseName, self.
name)
401 resultfile = os.path.expandvars(
replaceByMap((
"/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./" +
402 "%s_%s_.oO[name]Oo..root" % (self.resultBaseName, self.
name))
405 "resultFile":
".oO[resultFiles[.oO[nIndex]Oo.]]Oo.",
407 "finalResultFile": resultfile,
408 "outputFile":
".oO[outputFiles[.oO[nIndex]Oo.]]Oo.",
410 "finalOutputFile": outputfile,
423 return "%s.%s.%s_cfg.py"%( self.configBaseName, self.
name,
424 self.alignmentToValidate.name )
431 return configTemplates.cfgTemplate
446 def createScript(self, path, template = configTemplates.scriptTemplate, downloadFiles=[], repMap = None, repMaps = None):
447 scriptName =
"%s.%s.%s.sh"%(self.scriptBaseName, self.
name,
448 self.alignmentToValidate.name )
449 if repMap
is None and repMaps
is None:
451 repMap[
"CommandLine"]=
"" 453 repMap[
"CommandLine"]+= repMap[
"CommandLineTemplate"]%{
"cfgFile":
addIndex(cfg, self.
NJobs,
".oO[nIndex]Oo."),
456 scripts = {scriptName: template}
457 return super(GenericValidationData, self).
createScript(scripts, path, downloadFiles = downloadFiles,
458 repMap = repMap, repMaps = repMaps)
462 Method which creates a `crab.cfg` for a validation on datasets. 465 - `path`: Path at which the file will be stored. 466 - `crabCfgBaseName`: String which depends on the actual type of 467 validation calling this method. 469 crabCfgName =
"crab.%s.%s.%s.cfg"%( crabCfgBaseName, self.
name,
470 self.alignmentToValidate.name )
472 repMap[
"script"] =
"dummy_script.sh" 474 repMap[
"crabWorkingDir"] = crabCfgName.split(
'.cfg' )[0]
476 repMap[
"numberOfJobs"] = self.
general[
"parallelJobs"]
478 repMap[
"queue"] = self.jobmode.split(
',' )[1].
split(
'-q' )[1]
479 if self.dataset.dataType() ==
"mc":
480 repMap[
"McOrData"] =
"events = .oO[nEvents]Oo." 481 elif self.dataset.dataType() ==
"data":
482 repMap[
"McOrData"] =
"lumis = -1" 483 if self.jobmode.split(
',' )[0] ==
"crab":
484 print (
"For jobmode 'crab' the parameter 'maxevents' will be " 485 "ignored and all events will be processed.")
487 raise AllInOneError(
"Unknown data type! Can't run in crab mode")
488 crabCfg = {crabCfgName:
replaceByMap( configTemplates.crabCfgTemplate,
490 return super(GenericValidationData, self).
createCrabCfg( crabCfg, path )
494 return configTemplates.Bookkeeping
497 return configTemplates.LoadBasicModules
503 return configTemplates.FileOutputTemplate
508 class GenericValidationData_CTSR(GenericValidationData):
511 "momentumconstraint":
"None",
512 "openmasswindow":
"False",
513 "cosmicsdecomode":
"True",
514 "removetrackhitfiltercommands":
"",
515 "appendtrackhitfiltercommands":
"",
518 result = super(GenericValidationData_CTSR, self).
getRepMap(alignment)
520 from trackSplittingValidation
import TrackSplittingValidation
523 "istracksplitting":
str(isinstance(self, TrackSplittingValidation)),
530 for removeorappend
in "remove",
"append":
531 optionname = removeorappend +
"trackhitfiltercommands" 532 if result[optionname]:
533 for command
in result[optionname].
split(
","):
534 command = command.strip()
535 commands.append(
'process.TrackerTrackHitFilter.commands.{}("{}")'.
format(removeorappend, command))
536 result[
"trackhitfiltercommands"] =
"\n".
join(commands)
541 return "Cosmics" not in self.
general[
"trackcollection"]
547 return configTemplates.CommonTrackSelectionRefitting
550 return configTemplates.DefinePath_CommonSelectionRefitting
556 if "Cosmics" not in self.
general[
"trackcollection"]:
return False 557 Bfield = self.dataset.magneticFieldForRun()
558 if Bfield < 0.5:
return True 559 if isinstance(Bfield, str):
560 if "unknown " in Bfield:
561 msg = Bfield.replace(
"unknown ",
"",1)
562 elif Bfield ==
"unknown":
563 msg =
"Can't get the B field for %s." % self.dataset.name()
565 msg =
"B field = {}???".
format(Bfield)
567 "To use this dataset, specify magneticfield = [value] in your .ini config file.")
580 from plottingOptions
import PlottingOptions
583 if result
and result[-1] !=
"\n": result +=
"\n" 587 if result[-1] !=
"\n": result +=
"\n" 588 result += (
"if [[ tmpMergeRetCode -eq 0 ]]; then\n" 589 " xrdcp -f .oO[finalOutputFile]Oo. root://eoscms//eos/cms.oO[finalResultFile]Oo.\n" 591 "if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then\n" 592 " mergeRetCode=${tmpMergeRetCode}\n" 600 return (
"rfcp .oO[plottingscriptpath]Oo. .\n" 601 "root -x -b -q .oO[plottingscriptname]Oo.++")
607 """override with a classmethod""" 610 """override with a classmethod""" 613 """override with a classmethod""" 617 from plottingOptions
import PlottingOptions
621 if result
and result[-1] !=
"\n": result +=
"\n" 625 from plottingOptions
import PlottingOptions
627 filename =
replaceByMap(
".oO[plottingscriptpath]Oo.", repmap)
628 repmap[
"PlottingInstantiation"] =
"\n".
join(
629 replaceByMap(v.appendToPlots(), v.getRepMap()).rstrip(
"\n")
633 with open(filename,
'w')
as f:
634 f.write(plottingscript)
638 def __init__(self, name, values, format=None, latexname=None, latexformat=None):
640 name: name of the summary item, goes on top of the column 641 values: value for each alignment (in order of rows) 642 format: python format string (default: {:.3g}, meaning up to 3 significant digits) 643 latexname: name in latex form, e.g. if name=sigma you might want latexname=\sigma (default: name) 644 latexformat: format for latex (default: format) 646 if format
is None: format =
"{:.3g}" 647 if latexname
is None: latexname = name
648 if latexformat
is None: latexformat = format
667 if re.match(
".*[{][^}]*[fg][}].*", fmt):
669 return fmt.format(value)
676 return self.
values(latex)[i]
680 """override with a classmethod that returns a list of SummaryItems 681 based on the plots saved in folder""" 683 __summaryitems =
None 689 if folder.startswith(
"/castor/" ):
690 folder =
"rfio:%(file)s"%repMap
691 elif folder.startswith(
"/store/" ):
692 folder =
"root://eoscms.cern.ch//eos/cms%(file)s"%repMap
702 size = {len(_.values(latex))
for _
in summaryitems}
704 raise AllInOneError(
"Some summary items have different numbers of values\n{}".
format(size))
708 columnwidths = ([
max(len(_.name(latex))
for _
in summaryitems)]
709 + [
max(len(_.value(i, latex))
for _
in summaryitems)
for i
in range(size)])
711 columnwidths = [
max(len(entry)
for entry
in [_.name(latex)] + _.values(latex))
for _
in summaryitems]
717 row = join.join(
"{{:{}}}".
format(width)
for width
in columnwidths)
720 rows = [row.format(*[_.name(latex)]+_.values(latex))
for _
in summaryitems]
723 rows.append(row.format(*(_.name
for _
in summaryitems)))
724 for i
in range(size):
725 rows.append(row.format(*(_.value(i, latex)
for _
in summaryitems)))
731 result = join.join(rows)
733 result = (
r"\begin{{tabular}}{{{}}}".
format(
"|" +
"|".
join(
"c"*(len(columnwidths))) +
"|") +
"\n" 743 with open(filename,
"w")
as f:
750 with open(os.path.join(folder,
"{}Summary.txt".
format(cls.__name__)))
as f:
752 split = line.rstrip(
"\n").
split(
"\t")
754 for thing
in split[:]:
755 if thing.startswith(
"format="):
756 kwargs[
"format"] = thing.replace(
"format=",
"", 1)
758 if thing.startswith(
"latexname="):
759 kwargs[
"latexname"] = thing.replace(
"latexname=",
"", 1)
761 if thing.startswith(
"latexformat="):
762 kwargs[
"latexformat"] = thing.replace(
"latexformat=",
"", 1)
767 result.append(cls.
SummaryItem(name, values, **kwargs))
773 from plottingOptions
import PlottingOptions
775 repmap[
"compareStrings"] =
" , ".
join(v.getCompareStrings(
"OfflineValidation")
for v
in validations)
776 repmap[
"compareStringsPlain"] =
" , ".
join(v.getCompareStrings(
"OfflineValidation",
True)
for v
in validations)
782 return configTemplates.compareAlignmentsExecution
785 return ".oO[Alignment/OfflineValidation]Oo./scripts/.oO[compareAlignmentsName]Oo." 790 class ValidationForPresentation(ValidationWithPlots):
def createPlottingScript(cls, validations)
def format(self, value, latex=False)
def __init__(self, valName, alignment, config)
def pythonboolstring(string, name)
def TrackSelectionRefitting(self)
def printsummaryitems(cls, args, kwargs)
def comparealignmentspath(cls)
def getRepMap(self, alignment=None)
def LoadBasicModules(self)
def createConfiguration(self, fileContents, path, schedule=None, repMap=None, repMaps=None)
def createConfiguration(self, path)
def ValidationTemplate(self)
def plottingscripttemplate(cls)
S & print(S &os, JobReport::InputFile const &f)
def getCommandOutput2(command)
def createScript(self, fileContents, path, downloadFiles=[], repMap=None, repMaps=None)
def writesummaryitems(cls, filename, args, kwargs)
def __init__(self, valName, alignment, config)
def plottingscriptname(cls)
def TrackSelectionRefitting(self)
def FileOutputTemplate(self)
def name(self, latex=False)
def ValidationSequence(self)
def addIndex(filename, njobs, index=None)
def createCrabCfg(self, path, crabCfgBaseName)
def doRunPlots(cls, validations)
def doComparison(cls, validations)
def runPlots(cls, validations)
def value(self, i, latex)
def getRepMap(self, alignment=None)
def comparisontemplate(cls)
def summaryitemsstring(cls, folder=None, latex=False, transpose=True)
def PlottingOptions(config, valType)
def replaceByMap(target, the_map)
— Helpers —############################
def values(self, latex=False)
def createFiles(self, fileContents, path, repMap=None, repMaps=None)
static std::string join(char **cmd)
string defaultReferenceName
def __init__(self, name, values, format=None, latexname=None, latexformat=None)
def boolfromstring(string, name)
def createScript(self, path, template=configTemplates.scriptTemplate, downloadFiles=[], repMap=None, repMaps=None)
def createCrabCfg(self, fileContents, path)
def getRepMap(self, alignment=None)
def presentationsubsections(cls)
def getCompareStrings(self, requestId=None, plain=False)
def getsummaryitems(cls, folder)
def getsummaryitems(cls, folder)
def comparealignmentsname(cls)