CMS 3D CMS Logo

genericValidation.py
Go to the documentation of this file.
1 from abc import ABCMeta, abstractmethod, abstractproperty
2 import os
3 import re
4 import json
5 import globalDictionaries
6 import configTemplates
7 from dataset import Dataset
8 from helperFunctions import replaceByMap, addIndex, getCommandOutput2, boolfromstring, pythonboolstring
9 from TkAlExceptions import AllInOneError
10 
11 class ValidationMetaClass(ABCMeta):
12  sets = ["mandatories", "optionals", "needpackages"]
13  dicts = ["defaults"]
14  def __new__(cls, clsname, bases, dct):
15  for setname in cls.sets:
16  if setname not in dct: dct[setname] = set()
17  dct[setname] = set.union(dct[setname], *(getattr(base, setname) for base in bases if hasattr(base, setname)))
18 
19  for dictname in cls.dicts:
20  if dictname not in dct: dct[dictname] = {}
21  for base in bases:
22  if not hasattr(base, dictname): continue
23  newdict = getattr(base, dictname)
24  for key in set(newdict) & set(dct[dictname]):
25  if newdict[key] != dct[dictname][key]:
26  raise ValueError("Inconsistent values of defaults[{}]: {}, {}".format(key, newdict[key], dct[dictname][key]))
27  dct[dictname].update(newdict)
28 
29  for setname in cls.sets: #e.g. removemandatories, used in preexistingvalidation
30  #use with caution
31  if "remove"+setname not in dct: dct["remove"+setname] = set()
32  dct["remove"+setname] = set.union(dct["remove"+setname], *(getattr(base, "remove"+setname) for base in bases if hasattr(base, "remove"+setname)))
33 
34  dct[setname] -= dct["remove"+setname]
35 
36  return super(ValidationMetaClass, cls).__new__(cls, clsname, bases, dct)
37 
39  __metaclass__ = ValidationMetaClass
40  defaultReferenceName = "DEFAULT"
41  mandatories = set()
42  defaults = {
43  "cmssw": os.environ['CMSSW_BASE'],
44  "parallelJobs": "1",
45  "jobid": "",
46  "needsproxy": "false",
47  }
48  needpackages = {"Alignment/OfflineValidation"}
49  optionals = {"jobmode"}
50 
51  def __init__(self, valName, alignment, config):
52  import random
53  self.name = valName
54  self.alignmentToValidate = alignment
55  self.general = config.getGeneral()
56  self.randomWorkdirPart = "%0i"%random.randint(1,10e9)
57  self.configFiles = []
58  self.config = config
59  self.jobid = ""
60 
61  theUpdate = config.getResultingSection(self.valType+":"+self.name,
62  defaultDict = self.defaults,
63  demandPars = self.mandatories)
64  self.general.update(theUpdate)
65  self.jobmode = self.general["jobmode"]
66  self.NJobs = int(self.general["parallelJobs"])
67  self.needsproxy = boolfromstring(self.general["needsproxy"], "needsproxy")
68 
69  # limit maximum number of parallel jobs to 40
70  # (each output file is approximately 20MB)
71  maximumNumberJobs = 40
72  if self.NJobs > maximumNumberJobs:
73  msg = ("Maximum allowed number of parallel jobs "
74  +str(maximumNumberJobs)+" exceeded!!!")
75  raise AllInOneError(msg)
76  if self.NJobs > 1 and not isinstance(self, ParallelValidation):
77  raise AllInOneError("Parallel jobs not implemented for {}!\n"
78  "Please set parallelJobs = 1.".format(type(self).__name__))
79 
80  self.jobid = self.general["jobid"]
81  if self.jobid:
82  try: #make sure it's actually a valid jobid
83  output = getCommandOutput2("bjobs %(jobid)s 2>&1"%self.general)
84  if "is not found" in output: raise RuntimeError
85  except RuntimeError:
86  raise AllInOneError("%s is not a valid jobid.\nMaybe it finished already?"%self.jobid)
87 
88  self.cmssw = self.general["cmssw"]
89  badcharacters = r"\'"
90  for character in badcharacters:
91  if character in self.cmssw:
92  raise AllInOneError("The bad characters " + badcharacters + " are not allowed in the cmssw\n"
93  "path name. If you really have it in such a ridiculously named location,\n"
94  "try making a symbolic link somewhere with a decent name.")
95  try:
96  os.listdir(self.cmssw)
97  except OSError:
98  raise AllInOneError("Your cmssw release " + self.cmssw + ' does not exist')
99 
100  if self.cmssw == os.environ["CMSSW_BASE"]:
101  self.scramarch = os.environ["SCRAM_ARCH"]
102  self.cmsswreleasebase = os.environ["CMSSW_RELEASE_BASE"]
103  else:
104  command = ("cd '" + self.cmssw + "' && eval `scramv1 ru -sh 2> /dev/null`"
105  ' && echo "$CMSSW_BASE\n$SCRAM_ARCH\n$CMSSW_RELEASE_BASE"')
106  commandoutput = getCommandOutput2(command).split('\n')
107  self.cmssw = commandoutput[0]
108  self.scramarch = commandoutput[1]
109  self.cmsswreleasebase = commandoutput[2]
110 
111  self.packages = {}
112  for package in self.needpackages:
113  for placetolook in self.cmssw, self.cmsswreleasebase:
114  pkgpath = os.path.join(placetolook, "src", package)
115  if os.path.exists(pkgpath):
116  self.packages[package] = pkgpath
117  break
118  else:
119  raise AllInOneError("Package {} does not exist in {} or {}!".format(package, self.cmssw, self.cmsswreleasebase))
120 
121  self.AutoAlternates = True
122  if config.has_option("alternateTemplates","AutoAlternates"):
123  try:
124  self.AutoAlternates = json.loads(config.get("alternateTemplates","AutoAlternates").lower())
125  except ValueError:
126  raise AllInOneError("AutoAlternates needs to be true or false, not %s" % config.get("alternateTemplates","AutoAlternates"))
127 
128  knownOpts = set(self.defaults.keys())|self.mandatories|self.optionals
129  ignoreOpts = []
130  config.checkInput(self.valType+":"+self.name,
131  knownSimpleOptions = knownOpts,
132  ignoreOptions = ignoreOpts)
133 
134  def getRepMap(self, alignment = None):
135  from plottingOptions import PlottingOptions
136  if alignment == None:
137  alignment = self.alignmentToValidate
138  try:
139  result = PlottingOptions(self.config, self.valType)
140  except KeyError:
141  result = {}
142  result.update(alignment.getRepMap())
143  result.update(self.general)
144  result.update({
145  "workdir": os.path.join(self.general["workdir"],
146  self.randomWorkdirPart),
147  "datadir": self.general["datadir"],
148  "logdir": self.general["logdir"],
149  "CommandLineTemplate": ("#run configfile and post-proccess it\n"
150  "cmsRun %(cfgFile)s\n"
151  "%(postProcess)s "),
152  "CMSSW_BASE": self.cmssw,
153  "SCRAM_ARCH": self.scramarch,
154  "CMSSW_RELEASE_BASE": self.cmsswreleasebase,
155  "alignmentName": alignment.name,
156  "condLoad": alignment.getConditions(),
157  "LoadGlobalTagTemplate": configTemplates.loadGlobalTagTemplate,
158  })
159  result.update(self.packages)
160  return result
161 
162  @abstractproperty
163  def filesToCompare(self):
164  pass
165 
166  def getCompareStrings( self, requestId = None, plain = False ):
167  result = {}
168  repMap = self.getRepMap().copy()
169  for validationId in self.filesToCompare:
170  repMap["file"] = self.filesToCompare[ validationId ]
171  if repMap["file"].startswith( "/castor/" ):
172  repMap["file"] = "rfio:%(file)s"%repMap
173  elif repMap["file"].startswith( "/store/" ):
174  repMap["file"] = "root://eoscms.cern.ch//eos/cms%(file)s"%repMap
175  if plain:
176  result[validationId]=repMap["file"]
177  else:
178  result[validationId]= "%(file)s=%(title)s|%(color)s|%(style)s"%repMap
179  if requestId == None:
180  return result
181  else:
182  if not "." in requestId:
183  requestId += ".%s"%self.defaultReferenceName
184  if not requestId.split(".")[-1] in result:
185  msg = ("could not find %s in reference Objects!"
186  %requestId.split(".")[-1])
187  raise AllInOneError(msg)
188  return result[ requestId.split(".")[-1] ]
189 
190  def createFiles(self, fileContents, path, repMap = None, repMaps = None):
191  """repMap: single map for all files
192  repMaps: a dict, with the filenames as the keys"""
193  if repMap is not None and repMaps is not None:
194  raise AllInOneError("createFiles can only take repMap or repMaps (or neither), not both")
195  result = []
196  for fileName in fileContents:
197  filePath = os.path.join(path, fileName)
198  result.append(filePath)
199 
200  for (i, filePathi) in enumerate(addIndex(filePath, self.NJobs)):
201  theFile = open( filePathi, "w" )
202  fileContentsi = fileContents[ fileName ]
203  if repMaps is not None:
204  repMap = repMaps[fileName]
205  if repMap is not None:
206  repMap.update({"nIndex": str(i)})
207  fileContentsi = replaceByMap(fileContentsi, repMap)
208  theFile.write( fileContentsi )
209  theFile.close()
210 
211  return result
212 
213  def createConfiguration(self, fileContents, path, schedule = None, repMap = None, repMaps = None):
214  self.configFiles = self.createFiles(fileContents,
215  path, repMap = repMap, repMaps = repMaps)
216  if not schedule == None:
217  schedule = [os.path.join( path, cfgName) for cfgName in schedule]
218  for cfgName in schedule:
219  if not cfgName in self.configFiles:
220  msg = ("scheduled %s missing in generated configfiles: %s"
221  %(cfgName, self.configFiles))
222  raise AllInOneError(msg)
223  for cfgName in self.configFiles:
224  if not cfgName in schedule:
225  msg = ("generated configuration %s not scheduled: %s"
226  %(cfgName, schedule))
227  raise AllInOneError(msg)
228  self.configFiles = schedule
229  return self.configFiles
230 
231  def createScript(self, fileContents, path, downloadFiles=[], repMap = None, repMaps = None):
232  self.scriptFiles = self.createFiles(fileContents,
233  path, repMap = repMap, repMaps = repMaps)
234  for script in self.scriptFiles:
235  for scriptwithindex in addIndex(script, self.NJobs):
236  os.chmod(scriptwithindex,0o755)
237  return self.scriptFiles
238 
239  def createCrabCfg(self, fileContents, path ):
240  if self.NJobs > 1:
241  msg = ("jobmode 'crab' not supported for parallel validation."
242  " Please set parallelJobs = 1.")
243  raise AllInOneError(msg)
244  self.crabConfigFiles = self.createFiles(fileContents, path)
245  return self.crabConfigFiles
246 
247 
249  """
250  Subclass of `GenericValidation` which is the base for validations using
251  datasets.
252  """
253  needParentFiles = False
254  mandatories = {"dataset", "maxevents"}
255  defaults = {
256  "runRange": "",
257  "firstRun": "",
258  "lastRun": "",
259  "begin": "",
260  "end": "",
261  "JSON": "",
262  "dasinstance": "prod/global",
263  "ttrhbuilder":"WithAngleAndTemplate",
264  "usepixelqualityflag": "True",
265  }
266  optionals = {"magneticfield"}
267 
268  def __init__(self, valName, alignment, config):
269  """
270  This method adds additional items to the `self.general` dictionary
271  which are only needed for validations using datasets.
272 
273  Arguments:
274  - `valName`: String which identifies individual validation instances
275  - `alignment`: `Alignment` instance to validate
276  - `config`: `BetterConfigParser` instance which includes the
277  configuration of the validations
278  """
279 
280  super(GenericValidationData, self).__init__(valName, alignment, config)
281 
282  # if maxevents is not specified, cannot calculate number of events for
283  # each parallel job, and therefore running only a single job
284  if int( self.general["maxevents"] ) < 0 and self.NJobs > 1:
285  msg = ("Maximum number of events (maxevents) not specified: "
286  "cannot use parallel jobs.")
287  raise AllInOneError(msg)
288  if int( self.general["maxevents"] ) / self.NJobs != float( self.general["maxevents"] ) / self.NJobs:
289  msg = ("maxevents has to be divisible by parallelJobs")
290  raise AllInOneError(msg)
291 
292  tryPredefinedFirst = (not self.jobmode.split( ',' )[0] == "crab" and self.general["JSON"] == ""
293  and self.general["firstRun"] == "" and self.general["lastRun"] == ""
294  and self.general["begin"] == "" and self.general["end"] == "")
295 
296  if self.general["dataset"] not in globalDictionaries.usedDatasets:
297  globalDictionaries.usedDatasets[self.general["dataset"]] = {}
298 
299  if self.cmssw not in globalDictionaries.usedDatasets[self.general["dataset"]]:
300  if globalDictionaries.usedDatasets[self.general["dataset"]] != {}:
301  print ("Warning: you use the same dataset '%s' in multiple cmssw releases.\n"
302  "This is allowed, but make sure it's not a mistake") % self.general["dataset"]
303  globalDictionaries.usedDatasets[self.general["dataset"]][self.cmssw] = {False: None, True: None}
304 
305  Bfield = self.general.get("magneticfield", None)
306  if globalDictionaries.usedDatasets[self.general["dataset"]][self.cmssw][tryPredefinedFirst] is None:
307  dataset = Dataset(
308  self.general["dataset"], tryPredefinedFirst = tryPredefinedFirst,
309  cmssw = self.cmssw, cmsswrelease = self.cmsswreleasebase, magneticfield = Bfield,
310  dasinstance = self.general["dasinstance"])
311  globalDictionaries.usedDatasets[self.general["dataset"]][self.cmssw][tryPredefinedFirst] = dataset
312  if tryPredefinedFirst and not dataset.predefined(): #No point finding the data twice in that case
313  globalDictionaries.usedDatasets[self.general["dataset"]][self.cmssw][False] = dataset
314 
315  self.dataset = globalDictionaries.usedDatasets[self.general["dataset"]][self.cmssw][tryPredefinedFirst]
316  self.general["magneticField"] = self.dataset.magneticField()
317  self.general["defaultMagneticField"] = "MagneticField"
318  if self.general["magneticField"] == "unknown":
319  print "Could not get the magnetic field for this dataset."
320  print "Using the default: ", self.general["defaultMagneticField"]
321  self.general["magneticField"] = '.oO[defaultMagneticField]Oo.'
322 
323  if not self.jobmode.split( ',' )[0] == "crab":
324  try:
325  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
326  jsonPath = self.general["JSON"],
327  firstRun = self.general["firstRun"],
328  lastRun = self.general["lastRun"],
329  begin = self.general["begin"],
330  end = self.general["end"],
331  parent = self.needParentFiles )
332  except AllInOneError as e:
333  msg = "In section [%s:%s]: "%(self.valType, self.name)
334  msg += str(e)
335  raise AllInOneError(msg)
336  else:
337  if self.dataset.predefined():
338  msg = ("For jobmode 'crab' you cannot use predefined datasets "
339  "(in your case: '%s')."%( self.dataset.name() ))
340  raise AllInOneError( msg )
341  try:
342  theUpdate = config.getResultingSection(self.valType+":"+self.name,
343  demandPars = ["parallelJobs"])
344  except AllInOneError as e:
345  msg = str(e)[:-1]+" when using 'jobmode: crab'."
346  raise AllInOneError(msg)
347  self.general.update(theUpdate)
348  if self.general["begin"] or self.general["end"]:
349  ( self.general["begin"],
350  self.general["end"],
351  self.general["firstRun"],
352  self.general["lastRun"] ) = self.dataset.convertTimeToRun(
353  firstRun = self.general["firstRun"],
354  lastRun = self.general["lastRun"],
355  begin = self.general["begin"],
356  end = self.general["end"],
357  shortTuple = False)
358  if self.general["begin"] == None:
359  self.general["begin"] = ""
360  if self.general["end"] == None:
361  self.general["end"] = ""
362  self.general["firstRun"] = str( self.general["firstRun"] )
363  self.general["lastRun"] = str( self.general["lastRun"] )
364  if ( not self.general["firstRun"] ) and \
365  ( self.general["end"] or self.general["lastRun"] ):
366  self.general["firstRun"] = str(
367  self.dataset.runList()[0]["run_number"])
368  if ( not self.general["lastRun"] ) and \
369  ( self.general["begin"] or self.general["firstRun"] ):
370  self.general["lastRun"] = str(
371  self.dataset.runList()[-1]["run_number"])
372  if self.general["firstRun"] and self.general["lastRun"]:
373  if int(self.general["firstRun"]) > int(self.general["lastRun"]):
374  msg = ( "The lower time/runrange limit ('begin'/'firstRun') "
375  "chosen is greater than the upper time/runrange limit "
376  "('end'/'lastRun').")
377  raise AllInOneError( msg )
378  self.general["runRange"] = (self.general["firstRun"]
379  + '-' + self.general["lastRun"])
380  try:
381  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
382  jsonPath = self.general["JSON"],
383  firstRun = self.general["firstRun"],
384  lastRun = self.general["lastRun"],
385  begin = self.general["begin"],
386  end = self.general["end"],
387  crab = True )
388  except AllInOneError as e:
389  msg = "In section [%s:%s]: "%(self.valType, self.name)
390  msg += str( e )
391  raise AllInOneError( msg )
392 
393  self.general["usepixelqualityflag"] = pythonboolstring(self.general["usepixelqualityflag"], "usepixelqualityflag")
394 
395  def getRepMap(self, alignment = None):
396  result = super(GenericValidationData, self).getRepMap(alignment)
397  outputfile = os.path.expandvars(replaceByMap(
398  "%s_%s_.oO[name]Oo..root" % (self.outputBaseName, self.name)
399  , result))
400  resultfile = os.path.expandvars(replaceByMap(("/store/caf/user/$USER/.oO[eosdir]Oo./" +
401  "%s_%s_.oO[name]Oo..root" % (self.resultBaseName, self.name))
402  , result))
403  result.update({
404  "resultFile": ".oO[resultFiles[.oO[nIndex]Oo.]]Oo.",
405  "resultFiles": addIndex(resultfile, self.NJobs),
406  "finalResultFile": resultfile,
407  "outputFile": ".oO[outputFiles[.oO[nIndex]Oo.]]Oo.",
408  "outputFiles": addIndex(outputfile, self.NJobs),
409  "finalOutputFile": outputfile,
410  "ProcessName": self.ProcessName,
411  "Bookkeeping": self.Bookkeeping,
412  "LoadBasicModules": self.LoadBasicModules,
413  "TrackSelectionRefitting": self.TrackSelectionRefitting,
414  "ValidationConfig": self.ValidationTemplate,
415  "FileOutputTemplate": self.FileOutputTemplate,
416  "DefinePath": self.DefinePath,
417  })
418  return result
419 
420  @property
421  def cfgName(self):
422  return "%s.%s.%s_cfg.py"%( self.configBaseName, self.name,
423  self.alignmentToValidate.name )
424  @abstractproperty
425  def ProcessName(self):
426  pass
427 
428  @property
429  def cfgTemplate(self):
430  return configTemplates.cfgTemplate
431 
432  @abstractproperty
434  pass
435 
436  @property
437  def filesToCompare(self):
438  return {self.defaultReferenceName: self.getRepMap()["finalResultFile"]}
439 
440  def createConfiguration(self, path ):
441  repMap = self.getRepMap()
442  cfgs = {self.cfgName: self.cfgTemplate}
443  super(GenericValidationData, self).createConfiguration(cfgs, path, repMap=repMap)
444 
445  def createScript(self, path, template = configTemplates.scriptTemplate, downloadFiles=[], repMap = None, repMaps = None):
446  scriptName = "%s.%s.%s.sh"%(self.scriptBaseName, self.name,
447  self.alignmentToValidate.name )
448  if repMap is None and repMaps is None:
449  repMap = self.getRepMap()
450  repMap["CommandLine"]=""
451  for cfg in self.configFiles:
452  repMap["CommandLine"]+= repMap["CommandLineTemplate"]%{"cfgFile":addIndex(cfg, self.NJobs, ".oO[nIndex]Oo."),
453  "postProcess":""
454  }
455  scripts = {scriptName: template}
456  return super(GenericValidationData, self).createScript(scripts, path, downloadFiles = downloadFiles,
457  repMap = repMap, repMaps = repMaps)
458 
459  def createCrabCfg(self, path, crabCfgBaseName):
460  """
461  Method which creates a `crab.cfg` for a validation on datasets.
462 
463  Arguments:
464  - `path`: Path at which the file will be stored.
465  - `crabCfgBaseName`: String which depends on the actual type of
466  validation calling this method.
467  """
468  crabCfgName = "crab.%s.%s.%s.cfg"%( crabCfgBaseName, self.name,
469  self.alignmentToValidate.name )
470  repMap = self.getRepMap()
471  repMap["script"] = "dummy_script.sh"
472  # repMap["crabOutputDir"] = os.path.basename( path )
473  repMap["crabWorkingDir"] = crabCfgName.split( '.cfg' )[0]
474  self.crabWorkingDir = repMap["crabWorkingDir"]
475  repMap["numberOfJobs"] = self.general["parallelJobs"]
476  repMap["cfgFile"] = self.configFiles[0]
477  repMap["queue"] = self.jobmode.split( ',' )[1].split( '-q' )[1]
478  if self.dataset.dataType() == "mc":
479  repMap["McOrData"] = "events = .oO[nEvents]Oo."
480  elif self.dataset.dataType() == "data":
481  repMap["McOrData"] = "lumis = -1"
482  if self.jobmode.split( ',' )[0] == "crab":
483  print ("For jobmode 'crab' the parameter 'maxevents' will be "
484  "ignored and all events will be processed.")
485  else:
486  raise AllInOneError("Unknown data type! Can't run in crab mode")
487  crabCfg = {crabCfgName: replaceByMap( configTemplates.crabCfgTemplate,
488  repMap ) }
489  return super(GenericValidationData, self).createCrabCfg( crabCfg, path )
490 
491  @property
492  def Bookkeeping(self):
493  return configTemplates.Bookkeeping
494  @property
495  def LoadBasicModules(self):
496  return configTemplates.LoadBasicModules
497  @abstractproperty
499  pass
500  @property
502  return configTemplates.FileOutputTemplate
503  @abstractproperty
504  def DefinePath(self):
505  pass
506 
507 class GenericValidationData_CTSR(GenericValidationData):
508  #common track selection and refitting
509  defaults = {
510  "momentumconstraint": "None",
511  "openmasswindow": "False",
512  "cosmicsdecomode": "True",
513  "removetrackhitfiltercommands": "",
514  "appendtrackhitfiltercommands": "",
515  }
516  def getRepMap(self, alignment=None):
517  result = super(GenericValidationData_CTSR, self).getRepMap(alignment)
518 
519  from trackSplittingValidation import TrackSplittingValidation
520  result.update({
521  "ValidationSequence": self.ValidationSequence,
522  "istracksplitting": str(isinstance(self, TrackSplittingValidation)),
523  "cosmics0T": str(self.cosmics0T),
524  "use_d0cut": str(self.use_d0cut),
525  })
526 
527  commands = []
528  for removeorappend in "remove", "append":
529  optionname = removeorappend + "trackhitfiltercommands"
530  if result[optionname]:
531  for command in result[optionname].split(","):
532  command = command.strip()
533  commands.append('process.TrackerTrackHitFilter.commands.{}("{}")'.format(removeorappend, command))
534  result["trackhitfiltercommands"] = "\n".join(commands)
535 
536  return result
537  @property
538  def use_d0cut(self):
539  return "Cosmics" not in self.general["trackcollection"] #use it for collisions only
540  @property
542  return configTemplates.CommonTrackSelectionRefitting
543  @property
544  def DefinePath(self):
545  return configTemplates.DefinePath_CommonSelectionRefitting
546  @abstractproperty
548  pass
549  @property
550  def cosmics0T(self):
551  if "Cosmics" not in self.general["trackcollection"]: return False
552  Bfield = self.dataset.magneticFieldForRun()
553  if Bfield < 0.5: return True
554  if isinstance(Bfield, str):
555  if "unknown " in Bfield:
556  msg = Bfield.replace("unknown ","",1)
557  elif Bfield == "unknown":
558  msg = "Can't get the B field for %s." % self.dataset.name()
559  else:
560  msg = "B field = {}???".format(Bfield)
561  raise AllInOneError(msg + "\n"
562  "To use this dataset, specify magneticfield = [value] in your .ini config file.")
563  return False
564 
566  @classmethod
567  def initMerge(cls):
568  return ""
569  @abstractmethod
570  def appendToMerge(self):
571  pass
572 
573  @classmethod
574  def doInitMerge(cls):
575  from plottingOptions import PlottingOptions
576  result = cls.initMerge()
577  result = replaceByMap(result, PlottingOptions(None, cls))
578  if result and result[-1] != "\n": result += "\n"
579  return result
580  def doMerge(self):
581  result = self.appendToMerge()
582  if result[-1] != "\n": result += "\n"
583  result += ("if [[ tmpMergeRetCode -eq 0 ]]; then\n"
584  " xrdcp -f .oO[finalOutputFile]Oo. root://eoscms//eos/cms.oO[finalResultFile]Oo.\n"
585  "fi\n"
586  "if [[ ${tmpMergeRetCode} -gt ${mergeRetCode} ]]; then\n"
587  " mergeRetCode=${tmpMergeRetCode}\n"
588  "fi\n")
589  result = replaceByMap(result, self.getRepMap())
590  return result
591 
593  @classmethod
594  def runPlots(cls, validations):
595  return ("rfcp .oO[plottingscriptpath]Oo. .\n"
596  "root -x -b -q .oO[plottingscriptname]Oo.++")
597  @abstractmethod
598  def appendToPlots(self):
599  pass
600  @abstractmethod
602  """override with a classmethod"""
603  @abstractmethod
605  """override with a classmethod"""
606  @abstractmethod
607  def plotsdirname(cls):
608  """override with a classmethod"""
609 
610  @classmethod
611  def doRunPlots(cls, validations):
612  from plottingOptions import PlottingOptions
613  cls.createPlottingScript(validations)
614  result = cls.runPlots(validations)
615  result = replaceByMap(result, PlottingOptions(None, cls))
616  if result and result[-1] != "\n": result += "\n"
617  return result
618  @classmethod
619  def createPlottingScript(cls, validations):
620  from plottingOptions import PlottingOptions
621  repmap = PlottingOptions(None, cls).copy()
622  filename = replaceByMap(".oO[plottingscriptpath]Oo.", repmap)
623  repmap["PlottingInstantiation"] = "\n".join(
624  replaceByMap(v.appendToPlots(), v.getRepMap()).rstrip("\n")
625  for v in validations
626  )
627  plottingscript = replaceByMap(cls.plottingscripttemplate(), repmap)
628  with open(filename, 'w') as f:
629  f.write(plottingscript)
630 
633  def __init__(self, name, values, format=None, latexname=None, latexformat=None):
634  """
635  name: name of the summary item, goes on top of the column
636  values: value for each alignment (in order of rows)
637  format: python format string (default: {:.3g}, meaning up to 3 significant digits)
638  latexname: name in latex form, e.g. if name=sigma you might want latexname=\sigma (default: name)
639  latexformat: format for latex (default: format)
640  """
641  if format is None: format = "{:.3g}"
642  if latexname is None: latexname = name
643  if latexformat is None: latexformat = format
644 
645  self.__name = name
646  self.__values = values
647  self.__format = format
648  self.__latexname = latexname
649  self.__latexformat = latexformat
650 
651  def name(self, latex=False):
652  if latex:
653  return self.__latexname
654  else:
655  return self.__name
656 
657  def format(self, value, latex=False):
658  if latex:
659  fmt = self.__latexformat
660  else:
661  fmt = self.__format
662  if re.match(".*[{][^}]*[fg][}].*", fmt):
663  value = float(value)
664  return fmt.format(value)
665 
666  def values(self, latex=False):
667  result = [self.format(v, latex=latex) for v in self.__values]
668  return result
669 
670  def value(self, i, latex):
671  return self.values(latex)[i]
672 
673  @abstractmethod
674  def getsummaryitems(cls, folder):
675  """override with a classmethod that returns a list of SummaryItems
676  based on the plots saved in folder"""
677 
678  __summaryitems = None
679  __lastfolder = None
680 
681  @classmethod
682  def summaryitemsstring(cls, folder=None, latex=False, transpose=True):
683  if folder is None: folder = cls.plotsdirname()
684  if folder.startswith( "/castor/" ):
685  folder = "rfio:%(file)s"%repMap
686  elif folder.startswith( "/store/" ):
687  folder = "root://eoscms.cern.ch//eos/cms%(file)s"%repMap
688 
689  if cls.__summaryitems is None or cls.__lastfolder != folder:
690  cls.__lastfolder = folder
691  cls.__summaryitems = cls.getsummaryitems(folder)
692 
693  summaryitems = cls.__summaryitems
694 
695  if not summaryitems:
696  raise AllInOneError("No summary items!")
697  size = {len(_.values(latex)) for _ in summaryitems}
698  if len(size) != 1:
699  raise AllInOneError("Some summary items have different numbers of values\n{}".format(size))
700  size = size.pop()
701 
702  if transpose:
703  columnwidths = ([max(len(_.name(latex)) for _ in summaryitems)]
704  + [max(len(_.value(i, latex)) for _ in summaryitems) for i in range(size)])
705  else:
706  columnwidths = [max(len(entry) for entry in [_.name(latex)] + _.values(latex)) for _ in summaryitems]
707 
708  if latex:
709  join = " & "
710  else:
711  join = " "
712  row = join.join("{{:{}}}".format(width) for width in columnwidths)
713 
714  if transpose:
715  rows = [row.format(*[_.name(latex)]+_.values(latex)) for _ in summaryitems]
716  else:
717  rows = []
718  rows.append(row.format(*(_.name for _ in summaryitems)))
719  for i in range(size):
720  rows.append(row.format(*(_.value(i, latex) for _ in summaryitems)))
721 
722  if latex:
723  join = " \\\\\n"
724  else:
725  join = "\n"
726  result = join.join(rows)
727  if latex:
728  result = (r"\begin{{tabular}}{{{}}}".format("|" + "|".join("c"*(len(columnwidths))) + "|") + "\n"
729  + result + "\n"
730  + r"\end{tabular}")
731  return result
732 
733  @classmethod
734  def printsummaryitems(cls, *args, **kwargs):
735  print cls.summaryitemsstring(*args, **kwargs)
736  @classmethod
737  def writesummaryitems(cls, filename, *args, **kwargs):
738  with open(filename, "w") as f:
739  f.write(cls.summaryitemsstring(*args, **kwargs)+"\n")
740 
742  @classmethod
743  def getsummaryitems(cls, folder):
744  result = []
745  with open(os.path.join(folder, "{}Summary.txt".format(cls.__name__))) as f:
746  for line in f:
747  split = line.rstrip("\n").split("\t")
748  kwargs = {}
749  for thing in split[:]:
750  if thing.startswith("format="):
751  kwargs["format"] = thing.replace("format=", "", 1)
752  split.remove(thing)
753  if thing.startswith("latexname="):
754  kwargs["latexname"] = thing.replace("latexname=", "", 1)
755  split.remove(thing)
756  if thing.startswith("latexformat="):
757  kwargs["latexformat"] = thing.replace("latexformat=", "", 1)
758  split.remove(thing)
759 
760  name = split[0]
761  values = split[1:]
762  result.append(cls.SummaryItem(name, values, **kwargs))
763  return result
764 
766  @classmethod
767  def doComparison(cls, validations):
768  from plottingOptions import PlottingOptions
769  repmap = PlottingOptions(None, cls).copy()
770  repmap["compareStrings"] = " , ".join(v.getCompareStrings("OfflineValidation") for v in validations)
771  repmap["compareStringsPlain"] = " , ".join(v.getCompareStrings("OfflineValidation", True) for v in validations)
772  comparison = replaceByMap(cls.comparisontemplate(), repmap)
773  return comparison
774 
775  @classmethod
777  return configTemplates.compareAlignmentsExecution
778  @classmethod
780  return ".oO[Alignment/OfflineValidation]Oo./scripts/.oO[compareAlignmentsName]Oo."
781  @abstractmethod
783  """classmethod"""
784 
785 class ValidationForPresentation(ValidationWithPlots):
786  @abstractmethod
788  """classmethod"""
def __init__(self, valName, alignment, config)
def pythonboolstring(string, name)
def createConfiguration(self, fileContents, path, schedule=None, repMap=None, repMaps=None)
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 addIndex(filename, njobs, index=None)
def createCrabCfg(self, path, crabCfgBaseName)
def __new__(cls, clsname, bases, dct)
def summaryitemsstring(cls, folder=None, latex=False, transpose=True)
def PlottingOptions(config, valType)
def replaceByMap(target, the_map)
— Helpers —############################
def createFiles(self, fileContents, path, repMap=None, repMaps=None)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
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)
#define update(a, b)
def getRepMap(self, alignment=None)
def getCompareStrings(self, requestId=None, plain=False)
double split
Definition: MVATrainer.cc:139