1 from __future__
import print_function
2 from __future__
import absolute_import
3 from builtins
import range
4 from abc
import ABCMeta, abstractmethod, abstractproperty
8 from .
import globalDictionaries
9 from .
import configTemplates
10 from .dataset
import Dataset
11 from .helperFunctions
import replaceByMap, addIndex, getCommandOutput2, boolfromstring, pythonboolstring
12 from .TkAlExceptions
import AllInOneError
15 sets = [
"mandatories",
"optionals",
"needpackages"]
18 for setname
in cls.sets:
19 if setname
not in dct: dct[setname] = set()
20 dct[setname] = set.union(dct[setname], *(getattr(base, setname)
for base
in bases
if hasattr(base, setname)))
22 for dictname
in cls.dicts:
23 if dictname
not in dct: dct[dictname] = {}
25 if not hasattr(base, dictname):
continue
26 newdict = getattr(base, dictname)
27 for key
in set(newdict) & set(dct[dictname]):
28 if newdict[key] != dct[dictname][key]:
29 raise ValueError(
"Inconsistent values of defaults[{}]: {}, {}".
format(key, newdict[key], dct[dictname][key]))
30 dct[dictname].
update(newdict)
32 for setname
in cls.sets:
34 if "remove"+setname
not in dct: dct[
"remove"+setname] = set()
35 dct[
"remove"+setname] = set.union(dct[
"remove"+setname], *(getattr(base,
"remove"+setname)
for base
in bases
if hasattr(base,
"remove"+setname)))
37 dct[setname] -= dct[
"remove"+setname]
39 return super(ValidationMetaClass, cls).
__new__(cls, clsname, bases, dct)
42 defaultReferenceName =
"DEFAULT"
45 "cmssw": os.environ[
'CMSSW_BASE'],
48 "needsproxy":
"false",
50 needpackages = {
"Alignment/OfflineValidation"}
51 optionals = {
"jobmode"}
53 def __init__(self, valName, alignment, config):
63 theUpdate = config.getResultingSection(self.valType+
":"+self.
name,
66 self.general.update(theUpdate)
73 maximumNumberJobs = 40
74 if self.
NJobs > maximumNumberJobs:
75 msg = (
"Maximum allowed number of parallel jobs "
76 +
str(maximumNumberJobs)+
" exceeded!!!")
77 raise AllInOneError(msg)
78 if self.
NJobs > 1
and not isinstance(self, ParallelValidation):
79 raise AllInOneError(
"Parallel jobs not implemented for {}!\n"
80 "Please set parallelJobs = 1.".
format(type(self).__name__))
86 if "is not found" in output:
raise RuntimeError
88 raise AllInOneError(
"%s is not a valid jobid.\nMaybe it finished already?"%self.
jobid)
92 for character
in badcharacters:
93 if character
in self.
cmssw:
94 raise AllInOneError(
"The bad characters " + badcharacters +
" are not allowed in the cmssw\n"
95 "path name. If you really have it in such a ridiculously named location,\n"
96 "try making a symbolic link somewhere with a decent name.")
98 os.listdir(self.
cmssw)
100 raise AllInOneError(
"Your cmssw release " + self.
cmssw +
' does not exist')
102 if self.
cmssw == os.environ[
"CMSSW_BASE"]:
106 command = (
"cd '" + self.
cmssw +
"' && eval `scramv1 ru -sh 2> /dev/null`"
107 ' && echo "$CMSSW_BASE\n$SCRAM_ARCH\n$CMSSW_RELEASE_BASE"')
109 self.
cmssw = commandoutput[0]
116 pkgpath = os.path.join(placetolook,
"src", package)
117 if os.path.exists(pkgpath):
124 if config.has_option(
"alternateTemplates",
"AutoAlternates"):
126 self.
AutoAlternates = json.loads(config.get(
"alternateTemplates",
"AutoAlternates").lower())
128 raise AllInOneError(
"AutoAlternates needs to be true or false, not %s" % config.get(
"alternateTemplates",
"AutoAlternates"))
132 config.checkInput(self.valType+
":"+self.
name,
133 knownSimpleOptions = knownOpts,
134 ignoreOptions = ignoreOpts)
137 from .plottingOptions
import PlottingOptions
138 if alignment ==
None:
144 result.update(alignment.getRepMap())
147 "workdir": os.path.join(self.
general[
"workdir"],
149 "datadir": self.
general[
"datadir"],
150 "logdir": self.
general[
"logdir"],
151 "CommandLineTemplate": (
"#run configfile and post-proccess it\n"
152 "cmsRun %(cfgFile)s\n"
154 "CMSSW_BASE": self.
cmssw,
157 "alignmentName": alignment.name,
158 "condLoad": alignment.getConditions(),
159 "LoadGlobalTagTemplate": configTemplates.loadGlobalTagTemplate,
170 repMap = self.getRepMap().
copy()
171 for validationId
in self.filesToCompare:
172 repMap[
"file"] = self.filesToCompare[ validationId ]
173 if repMap[
"file"].startswith(
"/castor/" ):
174 repMap[
"file"] =
"rfio:%(file)s"%repMap
175 elif repMap[
"file"].startswith(
"/store/" ):
176 repMap[
"file"] =
"root://eoscms.cern.ch//eos/cms%(file)s"%repMap
178 result[validationId]=repMap[
"file"]
180 result[validationId]=
"%(file)s=%(title)s|%(color)s|%(style)s"%repMap
181 if requestId ==
None:
184 if not "." in requestId:
185 requestId +=
".%s"%self.defaultReferenceName
186 if not requestId.split(
".")[-1]
in result:
187 msg = (
"could not find %s in reference Objects!"
188 %requestId.split(
".")[-1])
189 raise AllInOneError(msg)
190 return result[ requestId.split(
".")[-1] ]
192 def createFiles(self, fileContents, path, repMap = None, repMaps = None):
193 """repMap: single map for all files
194 repMaps: a dict, with the filenames as the keys"""
195 if repMap
is not None and repMaps
is not None:
196 raise AllInOneError(
"createFiles can only take repMap or repMaps (or neither), not both")
198 for fileName
in fileContents:
199 filePath = os.path.join(path, fileName)
200 result.append(filePath)
202 for (i, filePathi)
in enumerate(
addIndex(filePath, self.
NJobs)):
203 theFile = open( filePathi,
"w" )
204 fileContentsi = fileContents[ fileName ]
205 if repMaps
is not None:
206 repMap = repMaps[fileName]
207 if repMap
is not None:
208 repMap.update({
"nIndex":
str(i)})
210 theFile.write( fileContentsi )
217 path, repMap = repMap, repMaps = repMaps)
218 if not schedule ==
None:
219 schedule = [os.path.join( path, cfgName)
for cfgName
in schedule]
220 for cfgName
in schedule:
222 msg = (
"scheduled %s missing in generated configfiles: %s"
224 raise AllInOneError(msg)
226 if not cfgName
in schedule:
227 msg = (
"generated configuration %s not scheduled: %s"
228 %(cfgName, schedule))
229 raise AllInOneError(msg)
233 def createScript(self, fileContents, path, downloadFiles=[], repMap = None, repMaps = None):
235 path, repMap = repMap, repMaps = repMaps)
238 os.chmod(scriptwithindex,0o755)
243 msg = (
"jobmode 'crab' not supported for parallel validation."
244 " Please set parallelJobs = 1.")
245 raise AllInOneError(msg)
252 Subclass of `GenericValidation` which is the base for validations using
255 needParentFiles =
False
256 mandatories = {
"dataset",
"maxevents"}
264 "dasinstance":
"prod/global",
265 "ttrhbuilder":
"WithAngleAndTemplate",
266 "usepixelqualityflag":
"True",
268 optionals = {
"magneticfield"}
272 This method adds additional items to the `self.general` dictionary
273 which are only needed for validations using datasets.
276 - `valName`: String which identifies individual validation instances
277 - `alignment`: `Alignment` instance to validate
278 - `config`: `BetterConfigParser` instance which includes the
279 configuration of the validations
282 super(GenericValidationData, self).
__init__(valName, alignment, config)
286 if int( self.
general[
"maxevents"] ) < 0
and self.
NJobs > 1:
287 msg = (
"Maximum number of events (maxevents) not specified: "
288 "cannot use parallel jobs.")
289 raise AllInOneError(msg)
291 msg = (
"maxevents has to be divisible by parallelJobs")
292 raise AllInOneError(msg)
294 tryPredefinedFirst = (
not self.jobmode.split(
',' )[0] ==
"crab" and self.
general[
"JSON"] ==
""
295 and self.
general[
"firstRun"] ==
"" and self.
general[
"lastRun"] ==
""
298 if self.
general[
"dataset"]
not in globalDictionaries.usedDatasets:
299 globalDictionaries.usedDatasets[self.
general[
"dataset"]] = {}
301 if self.
cmssw not in globalDictionaries.usedDatasets[self.
general[
"dataset"]]:
302 if globalDictionaries.usedDatasets[self.
general[
"dataset"]] != {}:
303 print((
"Warning: you use the same dataset '%s' in multiple cmssw releases.\n"
304 "This is allowed, but make sure it's not a mistake") % self.
general[
"dataset"])
305 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw] = {
False:
None,
True:
None}
307 Bfield = self.general.get(
"magneticfield",
None)
308 if globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][tryPredefinedFirst]
is None:
310 self.
general[
"dataset"], tryPredefinedFirst = tryPredefinedFirst,
312 dasinstance = self.
general[
"dasinstance"])
313 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][tryPredefinedFirst] = dataset
314 if tryPredefinedFirst
and not dataset.predefined():
315 globalDictionaries.usedDatasets[self.
general[
"dataset"]][self.
cmssw][
False] = dataset
318 self.
general[
"magneticField"] = self.dataset.magneticField()
319 self.
general[
"defaultMagneticField"] =
"MagneticField"
320 if self.
general[
"magneticField"] ==
"unknown":
321 print(
"Could not get the magnetic field for this dataset.")
322 print(
"Using the default: ", self.
general[
"defaultMagneticField"])
323 self.
general[
"magneticField"] =
'.oO[defaultMagneticField]Oo.'
325 if not self.jobmode.split(
',' )[0] ==
"crab":
327 self.
general[
"datasetDefinition"] = self.dataset.datasetSnippet(
328 jsonPath = self.
general[
"JSON"],
329 firstRun = self.
general[
"firstRun"],
330 lastRun = self.
general[
"lastRun"],
334 except AllInOneError
as e:
335 msg =
"In section [%s:%s]: "%(self.valType, self.
name)
337 raise AllInOneError(msg)
339 if self.dataset.predefined():
340 msg = (
"For jobmode 'crab' you cannot use predefined datasets "
341 "(in your case: '%s')."%( self.dataset.name() ))
342 raise AllInOneError( msg )
344 theUpdate = config.getResultingSection(self.valType+
":"+self.
name,
345 demandPars = [
"parallelJobs"])
346 except AllInOneError
as e:
347 msg =
str(e)[:-1]+
" when using 'jobmode: crab'."
348 raise AllInOneError(msg)
349 self.general.update(theUpdate)
354 self.
general[
"lastRun"] ) = self.dataset.convertTimeToRun(
355 firstRun = self.
general[
"firstRun"],
356 lastRun = self.
general[
"lastRun"],
360 if self.
general[
"begin"] ==
None:
362 if self.
general[
"end"] ==
None:
366 if (
not self.
general[
"firstRun"] )
and \
369 self.dataset.runList()[0][
"run_number"])
370 if (
not self.
general[
"lastRun"] )
and \
373 self.dataset.runList()[-1][
"run_number"])
376 msg = (
"The lower time/runrange limit ('begin'/'firstRun') "
377 "chosen is greater than the upper time/runrange limit "
378 "('end'/'lastRun').")
379 raise AllInOneError( msg )
381 +
'-' + self.
general[
"lastRun"])
383 self.
general[
"datasetDefinition"] = self.dataset.datasetSnippet(
384 jsonPath = self.
general[
"JSON"],
385 firstRun = self.
general[
"firstRun"],
386 lastRun = self.
general[
"lastRun"],
390 except AllInOneError
as e:
391 msg =
"In section [%s:%s]: "%(self.valType, self.
name)
393 raise AllInOneError( msg )
398 result = super(GenericValidationData, self).
getRepMap(alignment)
400 "%s_%s_.oO[name]Oo..root" % (self.outputBaseName, self.
name)
402 resultfile = os.path.expandvars(
replaceByMap((
"/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./" +
403 "%s_%s_.oO[name]Oo..root" % (self.resultBaseName, self.
name))
406 "resultFile":
".oO[resultFiles[.oO[nIndex]Oo.]]Oo.",
408 "finalResultFile": resultfile,
409 "outputFile":
".oO[outputFiles[.oO[nIndex]Oo.]]Oo.",
411 "finalOutputFile": outputfile,
424 return "%s.%s.%s_cfg.py"%( self.configBaseName, self.
name,
425 self.alignmentToValidate.name )
432 return configTemplates.cfgTemplate
447 def createScript(self, path, template = configTemplates.scriptTemplate, downloadFiles=[], repMap = None, repMaps = None):
448 scriptName =
"%s.%s.%s.sh"%(self.scriptBaseName, self.
name,
449 self.alignmentToValidate.name )
450 if repMap
is None and repMaps
is None:
452 repMap[
"CommandLine"]=
""
454 repMap[
"CommandLine"]+= repMap[
"CommandLineTemplate"]%{
"cfgFile":
addIndex(cfg, self.
NJobs,
".oO[nIndex]Oo."),
457 scripts = {scriptName: template}
458 return super(GenericValidationData, self).
createScript(scripts, path, downloadFiles = downloadFiles,
459 repMap = repMap, repMaps = repMaps)
463 Method which creates a `crab.cfg` for a validation on datasets.
466 - `path`: Path at which the file will be stored.
467 - `crabCfgBaseName`: String which depends on the actual type of
468 validation calling this method.
470 crabCfgName =
"crab.%s.%s.%s.cfg"%( crabCfgBaseName, self.
name,
471 self.alignmentToValidate.name )
473 repMap[
"script"] =
"dummy_script.sh"
475 repMap[
"crabWorkingDir"] = crabCfgName.split(
'.cfg' )[0]
477 repMap[
"numberOfJobs"] = self.
general[
"parallelJobs"]
479 repMap[
"queue"] = self.jobmode.split(
',' )[1].
split(
'-q' )[1]
480 if self.dataset.dataType() ==
"mc":
481 repMap[
"McOrData"] =
"events = .oO[nEvents]Oo."
482 elif self.dataset.dataType() ==
"data":
483 repMap[
"McOrData"] =
"lumis = -1"
484 if self.jobmode.split(
',' )[0] ==
"crab":
485 print (
"For jobmode 'crab' the parameter 'maxevents' will be "
486 "ignored and all events will be processed.")
488 raise AllInOneError(
"Unknown data type! Can't run in crab mode")
489 crabCfg = {crabCfgName:
replaceByMap( configTemplates.crabCfgTemplate,
491 return super(GenericValidationData, self).
createCrabCfg( crabCfg, path )
495 return configTemplates.Bookkeeping
498 return configTemplates.LoadBasicModules
504 return configTemplates.FileOutputTemplate
509 class GenericValidationData_CTSR(GenericValidationData):
512 "momentumconstraint":
"None",
513 "openmasswindow":
"False",
514 "cosmicsdecomode":
"True",
515 "removetrackhitfiltercommands":
"",
516 "appendtrackhitfiltercommands":
"",
519 result = super(GenericValidationData_CTSR, self).
getRepMap(alignment)
521 from .trackSplittingValidation
import TrackSplittingValidation
524 "istracksplitting":
str(isinstance(self, TrackSplittingValidation)),
531 for removeorappend
in "remove",
"append":
532 optionname = removeorappend +
"trackhitfiltercommands"
533 if result[optionname]:
534 for command
in result[optionname].
split(
","):
535 command = command.strip()
536 commands.append(
'process.TrackerTrackHitFilter.commands.{}("{}")'.
format(removeorappend, command))
537 result[
"trackhitfiltercommands"] =
"\n".
join(commands)
542 return "Cosmics" not in self.
general[
"trackcollection"]
548 return configTemplates.CommonTrackSelectionRefitting
551 return configTemplates.DefinePath_CommonSelectionRefitting
557 if "Cosmics" not in self.
general[
"trackcollection"]:
return False
558 Bfield = self.dataset.magneticFieldForRun()
559 if Bfield < 0.5:
return True
560 if isinstance(Bfield, str):
561 if "unknown " in Bfield:
562 msg = Bfield.replace(
"unknown ",
"",1)
563 elif Bfield ==
"unknown":
564 msg =
"Can't get the B field for %s." % self.dataset.name()
566 msg =
"B field = {}???".
format(Bfield)
567 raise AllInOneError(msg +
"\n"
568 "To use this dataset, specify magneticfield = [value] in your .ini config file.")
581 from .plottingOptions
import PlottingOptions
582 result = cls.initMerge()
584 if result
and result[-1] !=
"\n": result +=
"\n"
588 if result[-1] !=
"\n": result +=
"\n"
589 result += (
"if [[ tmpMergeRetCode -eq 0 ]]; then\n"
590 " xrdcp -f .oO[finalOutputFile]Oo. root://eoscms//eos/cms.oO[finalResultFile]Oo.\n"
592 "if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then\n"
593 " mergeRetCode=${tmpMergeRetCode}\n"
601 return (
"cp .oO[plottingscriptpath]Oo. .\n"
602 "root -x -b -q .oO[plottingscriptname]Oo.++")
608 """override with a classmethod"""
611 """override with a classmethod"""
614 """override with a classmethod"""
618 from .plottingOptions
import PlottingOptions
619 cls.createPlottingScript(validations)
620 result = cls.runPlots(validations)
622 if result
and result[-1] !=
"\n": result +=
"\n"
626 from .plottingOptions
import PlottingOptions
628 filename =
replaceByMap(
".oO[plottingscriptpath]Oo.", repmap)
629 repmap[
"PlottingInstantiation"] =
"\n".
join(
630 replaceByMap(v.appendToPlots(), v.getRepMap()).rstrip(
"\n")
633 plottingscript =
replaceByMap(cls.plottingscripttemplate(), repmap)
634 with open(filename,
'w')
as f:
635 f.write(plottingscript)
639 def __init__(self, name, values, format=None, latexname=None, latexformat=None):
641 name: name of the summary item, goes on top of the column
642 values: value for each alignment (in order of rows)
643 format: python format string (default: {:.3g}, meaning up to 3 significant digits)
644 latexname: name in latex form, e.g. if name=sigma you might want latexname=\sigma (default: name)
645 latexformat: format for latex (default: format)
647 if format
is None: format =
"{:.3g}"
648 if latexname
is None: latexname = name
649 if latexformat
is None: latexformat = format
668 if re.match(
".*[{][^}]*[fg][}].*", fmt):
670 return fmt.format(value)
677 return self.
values(latex)[i]
681 """override with a classmethod that returns a list of SummaryItems
682 based on the plots saved in folder"""
684 __summaryitems =
None
689 if folder
is None: folder = cls.plotsdirname()
690 if folder.startswith(
"/castor/" ):
691 folder =
"rfio:%(file)s"%repMap
692 elif folder.startswith(
"/store/" ):
693 folder =
"root://eoscms.cern.ch//eos/cms%(file)s"%repMap
695 if cls.__summaryitems
is None or cls.__lastfolder != folder:
696 cls.__lastfolder = folder
697 cls.__summaryitems = cls.getsummaryitems(folder)
699 summaryitems = cls.__summaryitems
702 raise AllInOneError(
"No summary items!")
703 size = {len(_.values(latex))
for _
in summaryitems}
705 raise AllInOneError(
"Some summary items have different numbers of values\n{}".
format(size))
709 columnwidths = ([
max(len(_.name(latex))
for _
in summaryitems)]
710 + [
max(len(_.value(i, latex))
for _
in summaryitems)
for i
in range(size)])
712 columnwidths = [
max(len(entry)
for entry
in [_.name(latex)] + _.values(latex))
for _
in summaryitems]
718 row = join.join(
"{{:{}}}".
format(width)
for width
in columnwidths)
721 rows = [row.format(*[_.name(latex)]+_.values(latex))
for _
in summaryitems]
724 rows.append(row.format(*(_.name
for _
in summaryitems)))
725 for i
in range(size):
726 rows.append(row.format(*(_.value(i, latex)
for _
in summaryitems)))
732 result = join.join(rows)
734 result = (
r"\begin{{tabular}}{{{}}}".
format(
"|" +
"|".
join(
"c"*(len(columnwidths))) +
"|") +
"\n"
741 print(cls.summaryitemsstring(*args, **kwargs))
744 with open(filename,
"w")
as f:
745 f.write(cls.summaryitemsstring(*args, **kwargs)+
"\n")
751 with open(os.path.join(folder,
"{}Summary.txt".
format(cls.__name__)))
as f:
753 split = line.rstrip(
"\n").
split(
"\t")
755 for thing
in split[:]:
756 if thing.startswith(
"format="):
757 kwargs[
"format"] = thing.replace(
"format=",
"", 1)
759 if thing.startswith(
"latexname="):
760 kwargs[
"latexname"] = thing.replace(
"latexname=",
"", 1)
762 if thing.startswith(
"latexformat="):
763 kwargs[
"latexformat"] = thing.replace(
"latexformat=",
"", 1)
768 result.append(cls.SummaryItem(name, values, **kwargs))
774 from .plottingOptions
import PlottingOptions
776 repmap[
"compareStrings"] =
" , ".
join(v.getCompareStrings(
"OfflineValidation")
for v
in validations)
777 repmap[
"compareStringsPlain"] =
" , ".
join(v.getCompareStrings(
"OfflineValidation",
True)
for v
in validations)
778 comparison =
replaceByMap(cls.comparisontemplate(), repmap)
783 return configTemplates.compareAlignmentsExecution
786 return ".oO[Alignment/OfflineValidation]Oo./scripts/.oO[compareAlignmentsName]Oo."
791 class ValidationForPresentation(ValidationWithPlots):
def comparealignmentsname
def presentationsubsections
const uint16_t range(const Frame &aFrame)
def TrackSelectionRefitting
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
static std::string join(char **cmd)
string defaultReferenceName
def plottingscripttemplate
def replaceByMap
— Helpers —############################
def comparealignmentspath
def TrackSelectionRefitting