CMS 3D CMS Logo

validateAlignments.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #test execute: export CMSSW_BASE=/tmp/CMSSW && ./validateAlignments.py -c defaultCRAFTValidation.ini,test.ini -n -N test
3 import os
4 import sys
5 import optparse
6 import datetime
7 import shutil
8 import fnmatch
9 
10 import Alignment.OfflineValidation.TkAlAllInOneTool.configTemplates \
11  as configTemplates
12 import Alignment.OfflineValidation.TkAlAllInOneTool.crabWrapper as crabWrapper
13 from Alignment.OfflineValidation.TkAlAllInOneTool.TkAlExceptions \
14  import AllInOneError
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
20 
22  import GenericValidation, ParallelValidation, ValidationWithComparison, ValidationWithPlots
23 from Alignment.OfflineValidation.TkAlAllInOneTool.geometryComparison \
24  import GeometryComparison
26  import OfflineValidation, OfflineValidationDQM
27 from Alignment.OfflineValidation.TkAlAllInOneTool.monteCarloValidation \
28  import MonteCarloValidation
30  import TrackSplittingValidation
32  import ZMuMuValidation
34  import PrimaryVertexValidation
35 from Alignment.OfflineValidation.TkAlAllInOneTool.preexistingValidation \
36  import *
37 from Alignment.OfflineValidation.TkAlAllInOneTool.plottingOptions \
38  import PlottingOptions
39 import Alignment.OfflineValidation.TkAlAllInOneTool.globalDictionaries \
40  as globalDictionaries
41 
42 
43 ####################--- Classes ---############################
45 
46  # these count the jobs of different varieties that are being run
47  crabCount = 0
48  interactCount = 0
49  batchCount = 0
50  batchJobIds = []
51  jobCount = 0
52 
53  def __init__( self, validation, config, options ):
54  if validation[1] == "":
55  # intermediate syntax
56  valString = validation[0].split( "->" )[0]
57  alignments = validation[0].split( "->" )[1]
58  # force user to use the normal syntax
59  if "->" in validation[0]:
60  msg = ("Instead of using the intermediate syntax\n'"
61  +valString.strip()+"-> "+alignments.strip()
62  +":'\nyou have to use the now fully supported syntax \n'"
63  +valString.strip()+": "
64  +alignments.strip()+"'.")
65  raise AllInOneError(msg)
66  else:
67  valString = validation[0]
68  alignments = validation[1]
69  valString = valString.split()
70  self.__valType = valString[0]
71  self.__valName = valString[1]
72  self.__commandLineOptions = options
73  self.__config = config
74  self.__preexisting = ("preexisting" in self.__valType)
75  if self.__valType[0] == "*":
76  self.__valType = self.__valType[1:]
77  self.__preexisting = True
78 
79  # workaround for intermediate parallel version
80  if self.__valType == "offlineParallel":
81  print ("offlineParallel and offline are now the same. To run an offline parallel validation,\n"
82  "just set parallelJobs to something > 1. There is no reason to call it offlineParallel anymore.")
83  self.__valType = "offline"
84  section = self.__valType + ":" + self.__valName
85  if not self.__config.has_section( section ):
86  raise AllInOneError("Validation '%s' of type '%s' is requested in"
87  " '[validation]' section, but is not defined."
88  "\nYou have to add a '[%s]' section."
89  %( self.__valName, self.__valType, section ))
90  self.validation = self.__getValidation( self.__valType, self.__valName,
91  alignments, self.__config,
92  options )
93 
94  def __getValidation( self, valType, name, alignments, config, options ):
95  if valType == "compare":
96  alignmentsList = alignments.split( "," )
97  firstAlignList = alignmentsList[0].split()
98  firstAlignName = firstAlignList[0].strip()
99  if firstAlignName == "IDEAL":
100  raise AllInOneError("'IDEAL' has to be the second (reference)"
101  " alignment in 'compare <val_name>: "
102  "<alignment> <reference>'.")
103  if len( firstAlignList ) > 1:
104  firstRun = firstAlignList[1]
105  else:
106  firstRun = "1"
107  firstAlign = Alignment( firstAlignName, self.__config, firstRun )
108  firstAlignName = firstAlign.name
109  secondAlignList = alignmentsList[1].split()
110  secondAlignName = secondAlignList[0].strip()
111  if len( secondAlignList ) > 1:
112  secondRun = secondAlignList[1]
113  else:
114  secondRun = "1"
115  if secondAlignName == "IDEAL":
116  secondAlign = secondAlignName
117  else:
118  secondAlign = Alignment( secondAlignName, self.__config,
119  secondRun )
120  secondAlignName = secondAlign.name
121 
122  validation = GeometryComparison( name, firstAlign, secondAlign,
123  self.__config,
124  self.__commandLineOptions.getImages)
125  elif valType == "offline":
126  validation = OfflineValidation( name,
127  Alignment( alignments.strip(), self.__config ), self.__config )
128  elif valType == "preexistingoffline":
129  validation = PreexistingOfflineValidation(name, self.__config)
130  elif valType == "offlineDQM":
131  validation = OfflineValidationDQM( name,
132  Alignment( alignments.strip(), self.__config ), self.__config )
133  elif valType == "mcValidate":
134  validation = MonteCarloValidation( name,
135  Alignment( alignments.strip(), self.__config ), self.__config )
136  elif valType == "preexistingmcValidate":
137  validation = PreexistingMonteCarloValidation(name, self.__config)
138  elif valType == "split":
139  validation = TrackSplittingValidation( name,
140  Alignment( alignments.strip(), self.__config ), self.__config )
141  elif valType == "preexistingsplit":
142  validation = PreexistingTrackSplittingValidation(name, self.__config)
143  elif valType == "zmumu":
144  validation = ZMuMuValidation( name,
145  Alignment( alignments.strip(), self.__config ), self.__config )
146  elif valType == "primaryvertex":
147  validation = PrimaryVertexValidation( name,
148  Alignment( alignments.strip(), self.__config ), self.__config )
149  else:
150  raise AllInOneError("Unknown validation mode '%s'"%valType)
151 
152  return validation
153 
154  def __createJob( self, jobMode, outpath ):
155  """This private method creates the needed files for the validation job.
156  """
157  self.validation.createConfiguration( outpath )
158  if self.__preexisting:
159  return
160  self.__scripts = sum([addIndex(script, self.validation.NJobs) for script in self.validation.createScript( outpath )], [])
161  if jobMode.split( ',' )[0] == "crab":
162  self.validation.createCrabCfg( outpath )
163  return None
164 
165  def createJob(self):
166  """This is the method called to create the job files."""
167  self.__createJob( self.validation.jobmode,
168  os.path.abspath( self.__commandLineOptions.Name) )
169 
170  def runJob( self ):
171  if self.__preexisting:
172  if self.validation.jobid:
173  self.batchJobIds.append(self.validation.jobid)
174  log = "> " + self.validation.name + " is already validated."
175  print log
176  return log
177  else:
178  if self.validation.jobid:
179  print "jobid {} will be ignored, since the validation {} is not preexisting".format(self.validation.jobid, self.validation.name)
180 
181  general = self.__config.getGeneral()
182  log = ""
183  for script in self.__scripts:
184  name = os.path.splitext( os.path.basename( script) )[0]
185  ValidationJob.jobCount += 1
186  if self.__commandLineOptions.dryRun:
187  print "%s would run: %s"%( name, os.path.basename( script) )
188  continue
189  log = "> Validating "+name
190  print "> Validating "+name
191  if self.validation.jobmode == "interactive":
192  log += getCommandOutput2( script )
193  ValidationJob.interactCount += 1
194  elif self.validation.jobmode.split(",")[0] == "lxBatch":
195  repMap = {
196  "commands": self.validation.jobmode.split(",")[1],
197  "logDir": general["logdir"],
198  "jobName": name,
199  "script": script,
200  "bsub": "/afs/cern.ch/cms/caf/scripts/cmsbsub"
201  }
202  for ext in ("stdout", "stderr", "stdout.gz", "stderr.gz"):
203  oldlog = "%(logDir)s/%(jobName)s."%repMap + ext
204  if os.path.exists(oldlog):
205  os.remove(oldlog)
206  bsubOut=getCommandOutput2("%(bsub)s %(commands)s "
207  "-J %(jobName)s "
208  "-o %(logDir)s/%(jobName)s.stdout "
209  "-e %(logDir)s/%(jobName)s.stderr "
210  "%(script)s"%repMap)
211  #Attention: here it is assumed that bsub returns a string
212  #containing a job id like <123456789>
213  ValidationJob.batchJobIds.append(bsubOut.split("<")[1].split(">")[0])
214  log+=bsubOut
215  ValidationJob.batchCount += 1
216  elif self.validation.jobmode.split( "," )[0] == "crab":
217  os.chdir( general["logdir"] )
218  crabName = "crab." + os.path.basename( script )[:-3]
219  theCrab = crabWrapper.CrabWrapper()
220  options = { "-create": "",
221  "-cfg": crabName + ".cfg",
222  "-submit": "" }
223  try:
224  theCrab.run( options )
225  except AllInOneError as e:
226  print "crab:", str(e).split("\n")[0]
227  exit(1)
228  ValidationJob.crabCount += 1
229 
230  else:
231  raise AllInOneError("Unknown 'jobmode'!\n"
232  "Please change this parameter either in "
233  "the [general] or in the ["
234  + self.__valType + ":" + self.__valName
235  + "] section to one of the following "
236  "values:\n"
237  "\tinteractive\n\tlxBatch, -q <queue>\n"
238  "\tcrab, -q <queue>")
239 
240  return log
241 
242  def getValidation( self ):
243  return self.validation
244 
245  @property
246  def needsproxy(self):
247  return self.validation.needsproxy and not self.__preexisting and not self.__commandLineOptions.dryRun
248 
249 
250 ####################--- Functions ---############################
251 def createMergeScript( path, validations ):
252  if(len(validations) == 0):
253  raise AllInOneError("Cowardly refusing to merge nothing!")
254 
255  config = validations[0].config
256  repMap = config.getGeneral()
257  repMap.update({
258  "DownloadData":"",
259  "CompareAlignments":"",
260  "RunValidationPlots":"",
261  "CMSSW_BASE": os.environ["CMSSW_BASE"],
262  "SCRAM_ARCH": os.environ["SCRAM_ARCH"],
263  "CMSSW_RELEASE_BASE": os.environ["CMSSW_RELEASE_BASE"],
264  })
265 
266  comparisonLists = {} # directory of lists containing the validations that are comparable
267  for validation in validations:
268  for referenceName in validation.filesToCompare:
269  validationtype = type(validation)
270  if isinstance(validationtype, PreexistingValidation):
271  #find the actual validationtype
272  for parentclass in validationtype.mro():
273  if not issubclass(parentclass, PreexistingValidation):
274  validationtype = parentclass
275  break
276  key = (validationtype, referenceName)
277  if key in comparisonLists:
278  comparisonLists[key].append(validation)
279  else:
280  comparisonLists[key] = [validation]
281 
282  # introduced to merge individual validation outputs separately
283  # -> avoids problems with merge script
284  repMap["doMerge"] = "mergeRetCode=0\n"
285  repMap["rmUnmerged"] = ("if [[ mergeRetCode -eq 0 ]]; then\n"
286  " echo -e \\n\"Merging succeeded, removing original files.\"\n")
287  repMap["beforeMerge"] = ""
288  repMap["mergeParallelFilePrefixes"] = ""
289 
290  anythingToMerge = []
291 
292  for (validationType, referencename), validations in comparisonLists.iteritems():
293  for validation in validations:
294  if (isinstance(validation, PreexistingValidation)
295  or validation.NJobs == 1
296  or not isinstance(validation, ParallelValidation)):
297  continue
298  if validationType not in anythingToMerge:
299  anythingToMerge += [validationType]
300  repMap["doMerge"] += '\n\n\n\necho -e "\n\nMerging results from %s jobs"\n\n' % validationType.valType
301  repMap["beforeMerge"] += validationType.doInitMerge()
302  repMap["doMerge"] += validation.doMerge()
303  for f in validation.getRepMap()["outputFiles"]:
304  longName = os.path.join("/store/caf/user/$USER/",
305  validation.getRepMap()["eosdir"], f)
306  repMap["rmUnmerged"] += " $eos rm "+longName+"\n"
307  repMap["rmUnmerged"] += ("else\n"
308  " echo -e \\n\"WARNING: Merging failed, unmerged"
309  " files won't be deleted.\\n"
310  "(Ignore this warning if merging was done earlier)\"\n"
311  "fi\n")
312 
313  if anythingToMerge:
314  repMap["DownloadData"] += replaceByMap( configTemplates.mergeParallelResults, repMap )
315  else:
316  repMap["DownloadData"] = ""
317 
318  repMap["RunValidationPlots"] = ""
319  for (validationType, referencename), validations in comparisonLists.iteritems():
320  if issubclass(validationType, ValidationWithPlots):
321  repMap["RunValidationPlots"] += validationType.doRunPlots(validations)
322 
323  repMap["CompareAlignments"] = "#run comparisons"
324  for (validationType, referencename), validations in comparisonLists.iteritems():
325  if issubclass(validationType, ValidationWithComparison):
326  repMap["CompareAlignments"] += validationType.doComparison(validations)
327 
328  filePath = os.path.join(path, "TkAlMerge.sh")
329  theFile = open( filePath, "w" )
330  theFile.write( replaceByMap( configTemplates.mergeTemplate, repMap ) )
331  theFile.close()
332  os.chmod(filePath,0o755)
333 
334  return filePath
335 
336 def loadTemplates( config ):
337  if config.has_section("alternateTemplates"):
338  for templateName in config.options("alternateTemplates"):
339  if templateName == "AutoAlternates":
340  continue
341  newTemplateName = config.get("alternateTemplates", templateName )
342  #print "replacing default %s template by %s"%( templateName, newTemplateName)
343  configTemplates.alternateTemplate(templateName, newTemplateName)
344 
345 
346 ####################--- Main ---############################
347 def main(argv = None):
348  if argv == None:
349  argv = sys.argv[1:]
350  optParser = optparse.OptionParser()
351  optParser.description = """All-in-one Alignment Validation.
352 This will run various validation procedures either on batch queues or interactively.
353 If no name is given (-N parameter) a name containing time and date is created automatically.
354 To merge the outcome of all validation procedures run TkAlMerge.sh in your validation's directory.
355 """
356  optParser.add_option("-n", "--dryRun", dest="dryRun", action="store_true", default=False,
357  help="create all scripts and cfg File but do not start jobs (default=False)")
358  optParser.add_option( "--getImages", dest="getImages", action="store_true", default=True,
359  help="get all Images created during the process (default= True)")
360  defaultConfig = "TkAlConfig.ini"
361  optParser.add_option("-c", "--config", dest="config", default = defaultConfig,
362  help="configuration to use (default TkAlConfig.ini) this can be a comma-seperated list of all .ini file you want to merge", metavar="CONFIG")
363  optParser.add_option("-N", "--Name", dest="Name",
364  help="Name of this validation (default: alignmentValidation_DATE_TIME)", metavar="NAME")
365  optParser.add_option("-r", "--restrictTo", dest="restrictTo",
366  help="restrict validations to given modes (comma seperated) (default: no restriction)", metavar="RESTRICTTO")
367  optParser.add_option("-s", "--status", dest="crabStatus", action="store_true", default = False,
368  help="get the status of the crab jobs", metavar="STATUS")
369  optParser.add_option("-d", "--debug", dest="debugMode", action="store_true",
370  default = False,
371  help="run the tool to get full traceback of errors",
372  metavar="DEBUG")
373  optParser.add_option("-m", "--autoMerge", dest="autoMerge", action="store_true", default = False,
374  help="submit TkAlMerge.sh to run automatically when all jobs have finished (default=False)."
375  " Works only for batch jobs")
376 
377  (options, args) = optParser.parse_args(argv)
378 
379  if not options.restrictTo == None:
380  options.restrictTo = options.restrictTo.split(",")
381 
382  options.config = [ os.path.abspath( iniFile ) for iniFile in \
383  options.config.split( "," ) ]
384  config = BetterConfigParser()
385  outputIniFileSet = set( config.read( options.config ) )
386  failedIniFiles = [ iniFile for iniFile in options.config if iniFile not in outputIniFileSet ]
387 
388  # Check for missing ini file
389  if options.config == [ os.path.abspath( defaultConfig ) ]:
390  if ( not options.crabStatus ) and \
391  ( not os.path.exists( defaultConfig ) ):
392  raise AllInOneError( "Default 'ini' file '%s' not found!\n"
393  "You can specify another name with the "
394  "command line option '-c'/'--config'."
395  %( defaultConfig ))
396  else:
397  for iniFile in failedIniFiles:
398  if not os.path.exists( iniFile ):
399  raise AllInOneError( "'%s' does not exist. Please check for "
400  "typos in the filename passed to the "
401  "'-c'/'--config' option!"
402  %( iniFile ))
403  else:
404  raise AllInOneError(( "'%s' does exist, but parsing of the "
405  "content failed!" ) % iniFile)
406 
407  # get the job name
408  if options.Name == None:
409  if not options.crabStatus:
410  options.Name = "alignmentValidation_%s"%(datetime.datetime.now().strftime("%y%m%d_%H%M%S"))
411  else:
412  existingValDirs = fnmatch.filter( os.walk( '.' ).next()[1],
413  "alignmentValidation_*" )
414  if len( existingValDirs ) > 0:
415  options.Name = existingValDirs[-1]
416  else:
417  print "Cannot guess last working directory!"
418  print ( "Please use the parameter '-N' or '--Name' to specify "
419  "the task for which you want a status report." )
420  return 1
421 
422  # set output path
423  outPath = os.path.abspath( options.Name )
424 
425  # Check status of submitted jobs and return
426  if options.crabStatus:
427  os.chdir( outPath )
428  crabLogDirs = fnmatch.filter( os.walk('.').next()[1], "crab.*" )
429  if len( crabLogDirs ) == 0:
430  print "Found no crab tasks for job name '%s'"%( options.Name )
431  return 1
432  theCrab = crabWrapper.CrabWrapper()
433  for crabLogDir in crabLogDirs:
434  print
435  print "*" + "=" * 78 + "*"
436  print ( "| Status report and output retrieval for:"
437  + " " * (77 - len( "Status report and output retrieval for:" ) )
438  + "|" )
439  taskName = crabLogDir.replace( "crab.", "" )
440  print "| " + taskName + " " * (77 - len( taskName ) ) + "|"
441  print "*" + "=" * 78 + "*"
442  print
443  crabOptions = { "-getoutput":"",
444  "-c": crabLogDir }
445  try:
446  theCrab.run( crabOptions )
447  except AllInOneError as e:
448  print "crab: No output retrieved for this task."
449  crabOptions = { "-status": "",
450  "-c": crabLogDir }
451  theCrab.run( crabOptions )
452  return
453 
454  general = config.getGeneral()
455  config.set("internals","workdir",os.path.join(general["workdir"],options.Name) )
456  config.set("internals","scriptsdir",outPath)
457  config.set("general","datadir",os.path.join(general["datadir"],options.Name) )
458  config.set("general","logdir",os.path.join(general["logdir"],options.Name) )
459  config.set("general","eosdir",os.path.join("AlignmentValidation", general["eosdir"], options.Name) )
460 
461  if not os.path.exists( outPath ):
462  os.makedirs( outPath )
463  elif not os.path.isdir( outPath ):
464  raise AllInOneError("the file %s is in the way rename the Job or move it away"%outPath)
465 
466  # replace default templates by the ones specified in the "alternateTemplates" section
467  loadTemplates( config )
468 
469  #save backup configuration file
470  backupConfigFile = open( os.path.join( outPath, "usedConfiguration.ini" ) , "w" )
471  config.write( backupConfigFile )
472 
473  #copy proxy, if there is one
474  try:
475  proxyexists = int(getCommandOutput2("voms-proxy-info --timeleft")) > 10
476  except RuntimeError:
477  proxyexists = False
478 
479  if proxyexists:
480  shutil.copyfile(getCommandOutput2("voms-proxy-info --path").strip(), os.path.join(outPath, ".user_proxy"))
481 
482  validations = []
483  for validation in config.items("validation"):
484  alignmentList = [validation[1]]
485  validationsToAdd = [(validation[0],alignment) \
486  for alignment in alignmentList]
487  validations.extend(validationsToAdd)
488  jobs = [ ValidationJob( validation, config, options) \
489  for validation in validations ]
490  for job in jobs:
491  if job.needsproxy and not proxyexists:
492  raise AllInOneError("At least one job needs a grid proxy, please init one.")
493  map( lambda job: job.createJob(), jobs )
494  validations = [ job.getValidation() for job in jobs ]
495 
496  createMergeScript(outPath, validations)
497 
498  print
499  map( lambda job: job.runJob(), jobs )
500 
501  if options.autoMerge:
502  # if everything is done as batch job, also submit TkAlMerge.sh to be run
503  # after the jobs have finished
504  if ValidationJob.jobCount == ValidationJob.batchCount and config.getGeneral()["jobmode"].split(",")[0] == "lxBatch":
505  print "> Automatically merging jobs when they have ended"
506  repMap = {
507  "commands": config.getGeneral()["jobmode"].split(",")[1],
508  "jobName": "TkAlMerge",
509  "logDir": config.getGeneral()["logdir"],
510  "script": "TkAlMerge.sh",
511  "bsub": "/afs/cern.ch/cms/caf/scripts/cmsbsub",
512  "conditions": '"' + " && ".join(["ended(" + jobId + ")" for jobId in ValidationJob.batchJobIds]) + '"'
513  }
514  for ext in ("stdout", "stderr", "stdout.gz", "stderr.gz"):
515  oldlog = "%(logDir)s/%(jobName)s."%repMap + ext
516  if os.path.exists(oldlog):
517  os.remove(oldlog)
518 
519  getCommandOutput2("%(bsub)s %(commands)s "
520  "-o %(logDir)s/%(jobName)s.stdout "
521  "-e %(logDir)s/%(jobName)s.stderr "
522  "-w %(conditions)s "
523  "%(logDir)s/%(script)s"%repMap)
524 
525 if __name__ == "__main__":
526  # main(["-n","-N","test","-c","defaultCRAFTValidation.ini,latestObjects.ini","--getImages"])
527  if "-d" in sys.argv[1:] or "--debug" in sys.argv[1:]:
528  main()
529  else:
530  try:
531  main()
532  except AllInOneError as e:
533  print "\nAll-In-One Tool:", str(e)
534  exit(1)
— Classes —############################
def main(argv=None)
— Main —############################
def createMergeScript(path, validations)
def getCommandOutput2(command)
def addIndex(filename, njobs, index=None)
def __getValidation(self, valType, name, alignments, config, options)
def replaceByMap(target, the_map)
— Helpers —############################
def __createJob(self, jobMode, outpath)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def alternateTemplate(templateName, alternateTemplateName)
Alternate Templates ###
if(dp >Float(M_PI)) dp-
def __init__(self, validation, config, options)
Definition: main.py:1
double split
Definition: MVATrainer.cc:139