CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
genericValidation.py
Go to the documentation of this file.
1 import os
2 import globalDictionaries
3 import configTemplates
4 from dataset import Dataset
5 from helperFunctions import replaceByMap
6 from TkAlExceptions import AllInOneError
7 
8 
10  defaultReferenceName = "DEFAULT"
11  def __init__(self, valName, alignment, config):
12  import random
13  self.name = valName
14  self.alignmentToValidate = alignment
15  self.general = config.getGeneral()
16  self.randomWorkdirPart = "%0i"%random.randint(1,10e9)
17  self.configFiles = []
18  self.filesToCompare = {}
19  self.jobmode = self.general["jobmode"]
20  self.config = config
21 
22  def getRepMap(self, alignment = None):
23  if alignment == None:
24  alignment = self.alignmentToValidate
25  result = alignment.getRepMap()
26  result.update( self.general )
27  result.update({
28  "workdir": os.path.join(self.general["workdir"],
29  self.randomWorkdirPart),
30  "datadir": self.general["datadir"],
31  "logdir": self.general["logdir"],
32  "CommandLineTemplate": ("#run configfile and post-proccess it\n"
33  "cmsRun %(cfgFile)s\n"
34  "%(postProcess)s "),
35  "CMSSW_BASE": os.environ['CMSSW_BASE'],
36  "SCRAM_ARCH": os.environ['SCRAM_ARCH'],
37  "alignmentName": alignment.name,
38  "condLoad": alignment.getConditions()
39  })
40  return result
41 
42  def getCompareStrings( self, requestId = None ):
43  result = {}
44  repMap = self.alignmentToValidate.getRepMap()
45  for validationId in self.filesToCompare:
46  repMap["file"] = self.filesToCompare[ validationId ]
47  if repMap["file"].startswith( "/castor/" ):
48  repMap["file"] = "rfio:%(file)s"%repMap
49  elif repMap["file"].startswith( "/store/" ):
50  repMap["file"] = "root://eoscms.cern.ch//eos/cms%(file)s"%repMap
51  result[validationId]= "%(file)s=%(name)s|%(color)s|%(style)s"%repMap
52  if requestId == None:
53  return result
54  else:
55  if not "." in requestId:
56  requestId += ".%s"%GenericValidation.defaultReferenceName
57  if not requestId.split(".")[-1] in result:
58  msg = ("could not find %s in reference Objects!"
59  %requestId.split(".")[-1])
60  raise AllInOneError(msg)
61  return result[ requestId.split(".")[-1] ]
62 
63  def createFiles( self, fileContents, path ):
64  result = []
65  for fileName in fileContents:
66  filePath = os.path.join( path, fileName)
67  theFile = open( filePath, "w" )
68  theFile.write( fileContents[ fileName ] )
69  theFile.close()
70  result.append( filePath )
71  return result
72 
73  def createConfiguration(self, fileContents, path, schedule= None):
74  self.configFiles = GenericValidation.createFiles(self, fileContents,
75  path)
76  if not schedule == None:
77  schedule = [os.path.join( path, cfgName) for cfgName in schedule]
78  for cfgName in schedule:
79  if not cfgName in self.configFiles:
80  msg = ("scheduled %s missing in generated configfiles: %s"
81  %(cfgName, self.configFiles))
82  raise AllInOneError(msg)
83  for cfgName in self.configFiles:
84  if not cfgName in schedule:
85  msg = ("generated configuration %s not scheduled: %s"
86  %(cfgName, schedule))
87  raise AllInOneError(msg)
88  self.configFiles = schedule
89  return self.configFiles
90 
91  def createScript(self, fileContents, path, downloadFiles=[] ):
92  self.scriptFiles = GenericValidation.createFiles(self, fileContents,
93  path)
94  for script in self.scriptFiles:
95  os.chmod(script,0755)
96  return self.scriptFiles
97 
98  def createCrabCfg(self, fileContents, path ):
99  self.crabConfigFiles = GenericValidation.createFiles(self, fileContents,
100  path)
101  return self.crabConfigFiles
102 
103 
105  """
106  Subclass of `GenericValidation` which is the base for validations using
107  datasets.
108  """
109 
110  def __init__(self, valName, alignment, config, valType,
111  addDefaults = {}, addMandatories=[]):
112  """
113  This method adds additional items to the `self.general` dictionary
114  which are only needed for validations using datasets.
115 
116  Arguments:
117  - `valName`: String which identifies individual validation instances
118  - `alignment`: `Alignment` instance to validate
119  - `config`: `BetterConfigParser` instance which includes the
120  configuration of the validations
121  - `valType`: String which specifies the type of validation
122  - `addDefaults`: Dictionary which contains default values for individual
123  validations in addition to the general default values
124  - `addMandatories`: List which contains mandatory parameters for
125  individual validations in addition to the general
126  mandatory parameters
127  (currently there are no general mandatories)
128  """
129 
130  GenericValidation.__init__(self, valName, alignment, config)
131  defaults = {"jobmode": self.jobmode,
132  "runRange": "",
133  "firstRun": "",
134  "lastRun": "",
135  "begin": "",
136  "end": "",
137  "JSON": ""
138  }
139  defaults.update(addDefaults)
140  mandatories = []
141  mandatories += addMandatories
142  theUpdate = config.getResultingSection(valType+":"+self.name,
143  defaultDict = defaults,
144  demandPars = mandatories)
145  self.general.update(theUpdate)
146  self.jobmode = self.general["jobmode"]
147 
148  knownOpts = defaults.keys()+mandatories
149  ignoreOpts = []
150  if self.jobmode.split(",")[0] == "crab" \
151  or self.__class__.__name__=="OfflineValidationParallel":
152  knownOpts.append("parallelJobs")
153  else:
154  ignoreOpts.append("parallelJobs")
155  config.checkInput(valType+":"+self.name,
156  knownSimpleOptions = knownOpts,
157  ignoreOptions = ignoreOpts)
158 
159  if self.general["dataset"] not in globalDictionaries.usedDatasets:
160  globalDictionaries.usedDatasets[self.general["dataset"]] = Dataset(
161  self.general["dataset"] )
162  self.dataset = globalDictionaries.usedDatasets[self.general["dataset"]]
163 
164  if not self.jobmode.split( ',' )[0] == "crab":
165  try:
166  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
167  jsonPath = self.general["JSON"],
168  nEvents = self.general["maxevents"],
169  firstRun = self.general["firstRun"],
170  lastRun = self.general["lastRun"],
171  begin = self.general["begin"],
172  end = self.general["end"] )
173  except AllInOneError, e:
174  msg = "In section [%s:%s]: "%(valType, self.name)
175  msg += str(e)
176  raise AllInOneError(msg)
177  else:
178  if self.dataset.predefined():
179  msg = ("For jobmode 'crab' you cannot use predefined datasets "
180  "(in your case: '%s')."%( self.dataset.name() ))
181  raise AllInOneError( msg )
182  try:
183  theUpdate = config.getResultingSection(valType+":"+self.name,
184  demandPars = ["parallelJobs"])
185  except AllInOneError, e:
186  msg = str(e)[:-1]+" when using 'jobmode: crab'."
187  raise AllInOneError(msg)
188  self.general.update(theUpdate)
189  if self.general["begin"] or self.general["end"]:
190  ( self.general["begin"],
191  self.general["end"],
192  self.general["firstRun"],
193  self.general["lastRun"] ) = self.dataset.convertTimeToRun(
194  firstRun = self.general["firstRun"],
195  lastRun = self.general["lastRun"],
196  begin = self.general["begin"],
197  end = self.general["end"],
198  shortTuple = False)
199  if self.general["begin"] == None:
200  self.general["begin"] = ""
201  if self.general["end"] == None:
202  self.general["end"] = ""
203  self.general["firstRun"] = str( self.general["firstRun"] )
204  self.general["lastRun"] = str( self.general["lastRun"] )
205  if ( not self.general["firstRun"] ) and \
206  ( self.general["end"] or self.general["lastRun"] ):
207  self.general["firstRun"] = str(
208  self.dataset.runList()[0]["run_number"])
209  if ( not self.general["lastRun"] ) and \
210  ( self.general["begin"] or self.general["firstRun"] ):
211  self.general["lastRun"] = str(
212  self.dataset.runList()[-1]["run_number"])
213  if self.general["firstRun"] and self.general["lastRun"]:
214  if int(self.general["firstRun"]) > int(self.general["lastRun"]):
215  msg = ( "The lower time/runrange limit ('begin'/'firstRun') "
216  "chosen is greater than the upper time/runrange limit "
217  "('end'/'lastRun').")
218  raise AllInOneError( msg )
219  self.general["runRange"] = (self.general["firstRun"]
220  + '-' + self.general["lastRun"])
221  try:
222  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
223  jsonPath = self.general["JSON"],
224  nEvents = self.general["maxevents"],
225  firstRun = self.general["firstRun"],
226  lastRun = self.general["lastRun"],
227  begin = self.general["begin"],
228  end = self.general["end"],
229  crab = True )
230  except AllInOneError, e:
231  msg = "In section [%s:%s]: "%(valType, self.name)
232  msg += str( e )
233  raise AllInOneError( msg )
234 
235  def createCrabCfg(self, path, crabCfgBaseName):
236  """
237  Method which creates a `crab.cfg` for a validation on datasets.
238 
239  Arguments:
240  - `path`: Path at which the file will be stored.
241  - `crabCfgBaseName`: String which depends on the actual type of
242  validation calling this method.
243  """
244  crabCfgName = "crab.%s.%s.%s.cfg"%( crabCfgBaseName, self.name,
245  self.alignmentToValidate.name )
246  repMap = self.getRepMap()
247  repMap["script"] = "dummy_script.sh"
248  # repMap["crabOutputDir"] = os.path.basename( path )
249  repMap["crabWorkingDir"] = crabCfgName.split( '.cfg' )[0]
250  self.crabWorkingDir = repMap["crabWorkingDir"]
251  repMap["numberOfJobs"] = self.general["parallelJobs"]
252  repMap["cfgFile"] = self.configFiles[0]
253  repMap["queue"] = self.jobmode.split( ',' )[1].split( '-q' )[1]
254  if self.dataset.dataType() == "mc":
255  repMap["McOrData"] = "events = .oO[nEvents]Oo."
256  elif self.dataset.dataType() == "data":
257  repMap["McOrData"] = "lumis = -1"
258  if self.jobmode.split( ',' )[0] == "crab":
259  print ("For jobmode 'crab' the parameter 'maxevents' will be "
260  "ignored and all events will be processed.")
261  crabCfg = {crabCfgName: replaceByMap( configTemplates.crabCfgTemplate,
262  repMap ) }
263  return GenericValidation.createCrabCfg( self, crabCfg, path )
def replaceByMap
— Helpers —############################
double split
Definition: MVATrainer.cc:139