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, plain = False ):
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  if plain:
52  result[validationId]=repMap["file"]
53  else:
54  result[validationId]= "%(file)s=%(name)s|%(color)s|%(style)s"%repMap
55  if requestId == None:
56  return result
57  else:
58  if not "." in requestId:
59  requestId += ".%s"%GenericValidation.defaultReferenceName
60  if not requestId.split(".")[-1] in result:
61  msg = ("could not find %s in reference Objects!"
62  %requestId.split(".")[-1])
63  raise AllInOneError(msg)
64  return result[ requestId.split(".")[-1] ]
65 
66  def createFiles( self, fileContents, path ):
67  result = []
68  for fileName in fileContents:
69  filePath = os.path.join( path, fileName)
70  theFile = open( filePath, "w" )
71  theFile.write( fileContents[ fileName ] )
72  theFile.close()
73  result.append( filePath )
74  return result
75 
76  def createConfiguration(self, fileContents, path, schedule= None):
77  self.configFiles = GenericValidation.createFiles(self, fileContents,
78  path)
79  if not schedule == None:
80  schedule = [os.path.join( path, cfgName) for cfgName in schedule]
81  for cfgName in schedule:
82  if not cfgName in self.configFiles:
83  msg = ("scheduled %s missing in generated configfiles: %s"
84  %(cfgName, self.configFiles))
85  raise AllInOneError(msg)
86  for cfgName in self.configFiles:
87  if not cfgName in schedule:
88  msg = ("generated configuration %s not scheduled: %s"
89  %(cfgName, schedule))
90  raise AllInOneError(msg)
91  self.configFiles = schedule
92  return self.configFiles
93 
94  def createScript(self, fileContents, path, downloadFiles=[] ):
95  self.scriptFiles = GenericValidation.createFiles(self, fileContents,
96  path)
97  for script in self.scriptFiles:
98  os.chmod(script,0755)
99  return self.scriptFiles
100 
101  def createCrabCfg(self, fileContents, path ):
102  self.crabConfigFiles = GenericValidation.createFiles(self, fileContents,
103  path)
104  return self.crabConfigFiles
105 
106 
108  """
109  Subclass of `GenericValidation` which is the base for validations using
110  datasets.
111  """
112 
113  def __init__(self, valName, alignment, config, valType,
114  addDefaults = {}, addMandatories=[]):
115  """
116  This method adds additional items to the `self.general` dictionary
117  which are only needed for validations using datasets.
118 
119  Arguments:
120  - `valName`: String which identifies individual validation instances
121  - `alignment`: `Alignment` instance to validate
122  - `config`: `BetterConfigParser` instance which includes the
123  configuration of the validations
124  - `valType`: String which specifies the type of validation
125  - `addDefaults`: Dictionary which contains default values for individual
126  validations in addition to the general default values
127  - `addMandatories`: List which contains mandatory parameters for
128  individual validations in addition to the general
129  mandatory parameters
130  (currently there are no general mandatories)
131  """
132 
133  GenericValidation.__init__(self, valName, alignment, config)
134  defaults = {"jobmode": self.jobmode,
135  "runRange": "",
136  "firstRun": "",
137  "lastRun": "",
138  "begin": "",
139  "end": "",
140  "JSON": ""
141  }
142  defaults.update(addDefaults)
143  mandatories = []
144  mandatories += addMandatories
145  theUpdate = config.getResultingSection(valType+":"+self.name,
146  defaultDict = defaults,
147  demandPars = mandatories)
148  self.general.update(theUpdate)
149  self.jobmode = self.general["jobmode"]
150 
151  knownOpts = defaults.keys()+mandatories
152  ignoreOpts = []
153  if self.jobmode.split(",")[0] == "crab" \
154  or self.__class__.__name__=="OfflineValidationParallel":
155  knownOpts.append("parallelJobs")
156  else:
157  ignoreOpts.append("parallelJobs")
158  config.checkInput(valType+":"+self.name,
159  knownSimpleOptions = knownOpts,
160  ignoreOptions = ignoreOpts)
161 
162  if self.general["dataset"] not in globalDictionaries.usedDatasets:
163  globalDictionaries.usedDatasets[self.general["dataset"]] = Dataset(
164  self.general["dataset"] )
165  self.dataset = globalDictionaries.usedDatasets[self.general["dataset"]]
166 
167  if not self.jobmode.split( ',' )[0] == "crab":
168  try:
169  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
170  jsonPath = self.general["JSON"],
171  nEvents = self.general["maxevents"],
172  firstRun = self.general["firstRun"],
173  lastRun = self.general["lastRun"],
174  begin = self.general["begin"],
175  end = self.general["end"] )
176  except AllInOneError, e:
177  msg = "In section [%s:%s]: "%(valType, self.name)
178  msg += str(e)
179  raise AllInOneError(msg)
180  else:
181  if self.dataset.predefined():
182  msg = ("For jobmode 'crab' you cannot use predefined datasets "
183  "(in your case: '%s')."%( self.dataset.name() ))
184  raise AllInOneError( msg )
185  try:
186  theUpdate = config.getResultingSection(valType+":"+self.name,
187  demandPars = ["parallelJobs"])
188  except AllInOneError, e:
189  msg = str(e)[:-1]+" when using 'jobmode: crab'."
190  raise AllInOneError(msg)
191  self.general.update(theUpdate)
192  if self.general["begin"] or self.general["end"]:
193  ( self.general["begin"],
194  self.general["end"],
195  self.general["firstRun"],
196  self.general["lastRun"] ) = self.dataset.convertTimeToRun(
197  firstRun = self.general["firstRun"],
198  lastRun = self.general["lastRun"],
199  begin = self.general["begin"],
200  end = self.general["end"],
201  shortTuple = False)
202  if self.general["begin"] == None:
203  self.general["begin"] = ""
204  if self.general["end"] == None:
205  self.general["end"] = ""
206  self.general["firstRun"] = str( self.general["firstRun"] )
207  self.general["lastRun"] = str( self.general["lastRun"] )
208  if ( not self.general["firstRun"] ) and \
209  ( self.general["end"] or self.general["lastRun"] ):
210  self.general["firstRun"] = str(
211  self.dataset.runList()[0]["run_number"])
212  if ( not self.general["lastRun"] ) and \
213  ( self.general["begin"] or self.general["firstRun"] ):
214  self.general["lastRun"] = str(
215  self.dataset.runList()[-1]["run_number"])
216  if self.general["firstRun"] and self.general["lastRun"]:
217  if int(self.general["firstRun"]) > int(self.general["lastRun"]):
218  msg = ( "The lower time/runrange limit ('begin'/'firstRun') "
219  "chosen is greater than the upper time/runrange limit "
220  "('end'/'lastRun').")
221  raise AllInOneError( msg )
222  self.general["runRange"] = (self.general["firstRun"]
223  + '-' + self.general["lastRun"])
224  try:
225  self.general["datasetDefinition"] = self.dataset.datasetSnippet(
226  jsonPath = self.general["JSON"],
227  nEvents = self.general["maxevents"],
228  firstRun = self.general["firstRun"],
229  lastRun = self.general["lastRun"],
230  begin = self.general["begin"],
231  end = self.general["end"],
232  crab = True )
233  except AllInOneError, e:
234  msg = "In section [%s:%s]: "%(valType, self.name)
235  msg += str( e )
236  raise AllInOneError( msg )
237 
238  def createCrabCfg(self, path, crabCfgBaseName):
239  """
240  Method which creates a `crab.cfg` for a validation on datasets.
241 
242  Arguments:
243  - `path`: Path at which the file will be stored.
244  - `crabCfgBaseName`: String which depends on the actual type of
245  validation calling this method.
246  """
247  crabCfgName = "crab.%s.%s.%s.cfg"%( crabCfgBaseName, self.name,
248  self.alignmentToValidate.name )
249  repMap = self.getRepMap()
250  repMap["script"] = "dummy_script.sh"
251  # repMap["crabOutputDir"] = os.path.basename( path )
252  repMap["crabWorkingDir"] = crabCfgName.split( '.cfg' )[0]
253  self.crabWorkingDir = repMap["crabWorkingDir"]
254  repMap["numberOfJobs"] = self.general["parallelJobs"]
255  repMap["cfgFile"] = self.configFiles[0]
256  repMap["queue"] = self.jobmode.split( ',' )[1].split( '-q' )[1]
257  if self.dataset.dataType() == "mc":
258  repMap["McOrData"] = "events = .oO[nEvents]Oo."
259  elif self.dataset.dataType() == "data":
260  repMap["McOrData"] = "lumis = -1"
261  if self.jobmode.split( ',' )[0] == "crab":
262  print ("For jobmode 'crab' the parameter 'maxevents' will be "
263  "ignored and all events will be processed.")
264  crabCfg = {crabCfgName: replaceByMap( configTemplates.crabCfgTemplate,
265  repMap ) }
266  return GenericValidation.createCrabCfg( self, crabCfg, path )
def replaceByMap
— Helpers —############################
double split
Definition: MVATrainer.cc:139