CMS 3D CMS Logo

MatrixReader.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import sys, os
3 
4 from Configuration.PyReleaseValidation.WorkFlow import WorkFlow
5 from Configuration.PyReleaseValidation.MatrixUtil import InputInfo
6 
7 # ================================================================================
8 
10  def __init__(self, msg):
11  self.msg = msg
12  def __str__(self):
13  return self.msg
14 
15 # ================================================================================
16 
18 
19  def __init__(self, opt):
20 
21  self.reset(opt.what)
22 
23  self.wm=opt.wmcontrol
24  self.revertDqmio=opt.revertDqmio
25  self.addCommand=opt.command
26  self.apply=opt.apply
27  self.commandLineWf=opt.workflow
28  self.overWrite=opt.overWrite
29 
30  self.noRun = opt.noRun
31  return
32 
33  def reset(self, what='all'):
34 
35  self.what = what
36 
37  #a bunch of information, but not yet the WorkFlow object
38  self.workFlowSteps = {}
39  #the actual WorkFlow objects
40  self.workFlows = []
41  self.nameList = {}
42 
43  self.filesPrefMap = {'relval_standard' : 'std-' ,
44  'relval_highstats': 'hi-' ,
45  'relval_pileup': 'PU-' ,
46  'relval_generator': 'gen-',
47  'relval_extendedgen': 'genExt-',
48  'relval_production': 'prod-' ,
49  'relval_ged': 'ged-',
50  'relval_upgrade':'upg-',
51  'relval_gpu':'gpu-',
52  'relval_2017':'2017-',
53  'relval_2026':'2026-',
54  'relval_identity':'id-',
55  'relval_machine': 'mach-',
56  'relval_premix': 'premix-'
57  }
58 
59  self.files = ['relval_standard' ,
60  'relval_highstats',
61  'relval_pileup',
62  'relval_generator',
63  'relval_extendedgen',
64  'relval_production',
65  'relval_ged',
66  'relval_upgrade',
67  'relval_gpu',
68  'relval_2017',
69  'relval_2026',
70  'relval_identity',
71  'relval_machine',
72  'relval_premix'
73  ]
74  self.filesDefault = {'relval_standard':True ,
75  'relval_highstats':True ,
76  'relval_pileup':True,
77  'relval_generator':True,
78  'relval_extendedgen':True,
79  'relval_production':True,
80  'relval_ged':True,
81  'relval_upgrade':False,
82  'relval_gpu':False,
83  'relval_2017':True,
84  'relval_2026':True,
85  'relval_identity':False,
86  'relval_machine':True,
87  'relval_premix':True
88  }
89 
90  self.relvalModule = None
91 
92  return
93 
94  def makeCmd(self, step):
95 
96  cmd = ''
97  cfg = None
98  input = None
99  for k,v in step.items():
100  if 'no_exec' in k : continue # we want to really run it ...
101  if k.lower() == 'cfg':
102  cfg = v
103  continue # do not append to cmd, return separately
104  if k.lower() == 'input':
105  input = v
106  continue # do not append to cmd, return separately
107 
108  #chain the configs
109  #if k.lower() == '--python':
110  # v = 'step%d_%s'%(index,v)
111  cmd += ' ' + k + ' ' + str(v)
112  return cfg, input, cmd
113 
114  def makeStep(self,step,overrides):
116  if len(overrides) > 0:
117  copyStep=merge([overrides]+[step])
118  return copyStep
119  else:
120  return step
121 
122  def readMatrix(self, fileNameIn, useInput=None, refRel=None, fromScratch=None):
123 
124  prefix = self.filesPrefMap[fileNameIn]
125 
126  print("processing", fileNameIn)
127 
128  try:
129  _tmpMod = __import__( 'Configuration.PyReleaseValidation.'+fileNameIn )
130  self.relvalModule = sys.modules['Configuration.PyReleaseValidation.'+fileNameIn]
131  except Exception as e:
132  print("ERROR importing file ", fileNameIn, str(e))
133  return
134 
135  if useInput is not None:
136  print("request for INPUT for ", useInput)
137 
138 
139  fromInput={}
140 
141  if useInput:
142  for i in useInput:
143  if ':' in i:
144  (ik,il)=i.split(':')
145  if ik=='all':
146  for k in self.relvalModule.workflows.keys():
147  fromInput[float(k)]=int(il)
148  else:
149  fromInput[float(ik)]=int(il)
150  else:
151  if i=='all':
152  for k in self.relvalModule.workflows.keys():
153  fromInput[float(k)]=0
154  else:
155  fromInput[float(i)]=0
156 
157  if fromScratch:
158  fromScratch=map(float,fromScratch)
159  for num in fromScratch:
160  if num in fromInput:
161  fromInput.pop(num)
162  #overwrite steps
163  if self.overWrite:
164  for p in self.overWrite:
165  self.relvalModule.steps.overwrite(p)
166 
167  #change the origin of dataset on the fly
168  if refRel:
169  if ',' in refRel:
170  refRels=refRel.split(',')
171  if len(refRels)!=len(self.relvalModule.baseDataSetRelease):
172  return
174  self.relvalModule.steps,
175  list(zip(self.relvalModule.baseDataSetRelease,refRels))
176  )
177  else:
179  self.relvalModule.steps,
180  [(x,refRel) for x in self.relvalModule.baseDataSetRelease]
181  )
182 
183 
184  for num, wfInfo in self.relvalModule.workflows.items():
185  commands=[]
186  wfName = wfInfo[0]
187  stepList = wfInfo[1]
188  stepOverrides=wfInfo.overrides
189  # upgrade case: workflow has basic name, key[, suffix (only special workflows)]
190  wfKey = ""
191  wfSuffix = ""
192  if isinstance(wfName, list) and len(wfName)>1:
193  if len(wfName)>2: wfSuffix = wfName[2]
194  wfKey = wfName[1]
195  wfName = wfName[0]
196  # if no explicit name given for the workflow, use the name of step1
197  if wfName.strip() == '': wfName = stepList[0]
198  # option to specialize the wf as the third item in the WF list
199  addTo=None
200  addCom=None
201  if len(wfInfo)>=3:
202  addCom=wfInfo[2]
203  if not isinstance(addCom, list): addCom=[addCom]
204  #print 'added dict',addCom
205  if len(wfInfo)>=4:
206  addTo=wfInfo[3]
207  #pad with 0
208  while len(addTo)!=len(stepList):
209  addTo.append(0)
210 
211  name=wfName
212  # separate suffixes by + because show() excludes first part of name
213  if len(wfKey)>0:
214  name = name+'+'+wfKey
215  if len(wfSuffix)>0: name = name+wfSuffix
216  stepIndex=0
217  ranStepList=[]
218 
219  #first resolve INPUT possibilities
220  if num in fromInput:
221  ilevel=fromInput[num]
222  #print num,ilevel
223  for (stepIr,step) in enumerate(reversed(stepList)):
224  stepName=step
225  stepI=(len(stepList)-stepIr)-1
226  #print stepIr,step,stepI,ilevel
227  if stepI>ilevel:
228  #print "ignoring"
229  continue
230  if stepI!=0:
231  testName='__'.join(stepList[0:stepI+1])+'INPUT'
232  else:
233  testName=step+'INPUT'
234  #print "JR",stepI,stepIr,testName,stepList
235  if testName in self.relvalModule.steps:
236  #print "JR",stepI,stepIr
237  stepList[stepI]=testName
238  #pop the rest in the list
239  #print "\tmod prepop",stepList
240  for p in range(stepI):
241  stepList.pop(0)
242  #print "\t\tmod",stepList
243  break
244 
245 
246  for (stepI,step) in enumerate(stepList):
247  stepName=step
248  if self.relvalModule.steps[stepName] is None:
249  continue
250  if self.wm:
251  #cannot put a certain number of things in wm
252  if stepName in ['SKIMD','SKIMCOSD','SKIMDreHLT']:
253  continue
254 
255  #replace stepName is needed
256  #if stepName in self.replaceStep
257  if len(name) > 0 : name += '+'
258  #any step can be mirrored with INPUT
259 
260  """
261  if num in fromInput:
262  if step+'INPUT' in self.relvalModule.steps.keys():
263  stepName = step+"INPUT"
264  stepList.remove(step)
265  stepList.insert(stepIndex,stepName)
266  """
267  stepNameTmp = stepName
268  if len(wfKey)>0: stepNameTmp = stepNameTmp.replace('_'+wfKey,"")
269  if len(wfSuffix)>0: stepNameTmp = stepNameTmp.replace(wfSuffix,"")
270  name += stepNameTmp
271  if addCom and (not addTo or addTo[stepIndex]==1):
273  copyStep=merge(addCom+[self.makeStep(self.relvalModule.steps[stepName],stepOverrides)])
274  cfg, input, opts = self.makeCmd(copyStep)
275  else:
276  cfg, input, opts = self.makeCmd(self.makeStep(self.relvalModule.steps[stepName],stepOverrides))
277 
278  if input and cfg :
279  msg = "FATAL ERROR: found both cfg and input for workflow "+str(num)+' step '+stepName
280  raise MatrixException(msg)
281 
282  if input:
283  cmd = input
284  if self.noRun:
285  cmd.run=[]
286  else:
287  if cfg:
288  cmd = 'cmsDriver.py '+cfg+' '+opts
289  else:
290  cmd = 'cmsDriver.py step'+str(stepIndex+1)+' '+opts
291  if self.wm:
292  cmd+=' --io %s.io --python %s.py'%(stepName,stepName)
293  if self.addCommand:
294  if self.apply:
295  if stepIndex in self.apply or stepName in self.apply:
296  cmd +=' '+self.addCommand
297  else:
298  cmd +=' '+self.addCommand
299  if self.wm and self.revertDqmio=='yes':
300  cmd=cmd.replace('DQMIO','DQM')
301  cmd=cmd.replace('--filetype DQM','')
302  commands.append(cmd)
303  ranStepList.append(stepName)
304  stepIndex+=1
305 
306  self.workFlowSteps[(num,prefix)] = (num, name, commands, ranStepList)
307 
308  return
309 
310 
311  def showRaw(self, useInput, refRel=None, fromScratch=None, what='all',step1Only=False,selected=None):
312 
313  if selected:
314  selected=map(float,selected)
315  for matrixFile in self.files:
316 
317  self.reset(what)
318 
319  if self.what != 'all' and not any(el in matrixFile for el in self.what.split(",")):
320  print("ignoring non-requested file",matrixFile)
321  continue
322 
323  if self.what == 'all' and not self.filesDefault[matrixFile]:
324  print("ignoring file not used by default (enable with -w)",matrixFile)
325  continue
326 
327  try:
328  self.readMatrix(matrixFile, useInput, refRel, fromScratch)
329  except Exception as e:
330  print("ERROR reading file:", matrixFile, str(e))
331  raise
332 
333  if not self.workFlowSteps: continue
334 
335  dataFileName = matrixFile.replace('relval_', 'cmsDriver_')+'_hlt.txt'
336  outFile = open(dataFileName,'w')
337 
338  print("found ", len(self.workFlowSteps), ' workflows for ', dataFileName)
339  ids = sorted(self.workFlowSteps.keys())
340  indexAndSteps=[]
341 
342  writtenWF=0
343  for key in ids:
344  if selected and not (key[0] in selected):
345  continue
346  #trick to skip the HImix IB test
347  if key[0]==203.1 or key[0]==204.1 or key[0]==205.1 or key[0]==4.51 or key[0]==4.52: continue
348  num, name, commands, stepList = self.workFlowSteps[key]
349 
350  wfName,stepNames= name.split('+',1)
351 
352  stepNames=stepNames.replace('+SKIMCOSD','')
353  stepNames=stepNames.replace('+SKIMD','')
354  if 'HARVEST' in stepNames:
355  #find out automatically what to remove
356  exactb=stepNames.index('+HARVEST')
357  exacte=stepNames.index('+',exactb+1) if ('+' in stepNames[exactb+1:]) else (len(stepNames))
358  stepNames=stepNames.replace(stepNames[exactb:exacte],'')
359  otherSteps = None
360  if '+' in stepNames:
361  step1,otherSteps = stepNames.split('+',1)
362 
363  line = str(num) + ' ++ '+ wfName
364  if otherSteps and not step1Only:
365  line += ' ++ ' +otherSteps.replace('+',',')
366  else:
367  line += ' ++ none'
368  inputInfo=None
369  if not isinstance(commands[0],str):
370  inputInfo=commands[0]
371  if otherSteps:
372  for (i,c) in enumerate(otherSteps.split('+')):
373  #pad with set
374  for p in range(len(indexAndSteps),i+2):
375  indexAndSteps.append(set())
376  indexAndSteps[i+1].add((c,commands[i+1]))
377 
378  if inputInfo :
379  #skip the samples from INPUT when step1Only is on
380  if step1Only: continue
381  line += ' ++ REALDATA: '+inputInfo.dataSet
382  if inputInfo.run!=[]: line += ', RUN:'+'|'.join(map(str,inputInfo.run))
383  line += ', FILES: ' +str(inputInfo.files)
384  line += ', EVENTS: '+str(inputInfo.events)
385  if inputInfo.label!='':
386  line += ', LABEL: ' +inputInfo.label
387  line += ', LOCATION:'+inputInfo.location
388  line += ' @@@'
389  else:
390  line += ' @@@ '+commands[0]
391  if self.revertDqmio=='yes':
392  line=line.replace('DQMIO','DQM')
393  writtenWF+=1
394  outFile.write(line+'\n')
395 
396 
397  outFile.write('\n'+'\n')
398  if step1Only: continue
399 
400  for (index,s) in enumerate(indexAndSteps):
401  for (stepName,cmd) in s:
402  stepIndex=index+1
403  if 'dasquery.log' in cmd: continue
404  line = 'STEP%d ++ '%(stepIndex,) +stepName + ' @@@ '+cmd
405  if self.revertDqmio=='yes':
406  line=line.replace('DQMIO','DQM')
407  outFile.write(line+'\n')
408  outFile.write('\n'+'\n')
409  outFile.close()
410  print("wrote ",writtenWF, ' workflow'+('s' if (writtenWF!=1) else ''),' to ', outFile.name)
411  return
412 
413  def workFlowsByLocation(self, cafVeto=True):
414  # Check if we are on CAF
415  onCAF = False
416  if 'cms/caf/cms' in os.environ['CMS_PATH']:
417  onCAF = True
418 
419  workflows = []
420  for workflow in self.workFlows:
421  if isinstance(workflow.cmds[0], InputInfo):
422  if cafVeto and (workflow.cmds[0].location == 'CAF' and not onCAF):
423  continue
424  workflows.append(workflow)
425 
426  return workflows
427 
428  def showWorkFlows(self, selected=None, extended=True, cafVeto=True):
429  if selected: selected = list(map(float,selected))
430  wfs = self.workFlowsByLocation(cafVeto)
431  maxLen = 100 # for summary, limit width of output
432  fmt1 = "%-6s %-35s [1]: %s ..."
433  fmt2 = " %35s [%d]: %s ..."
434  print("\nfound a total of ", len(wfs), ' workflows:')
435  if selected:
436  print(" of which the following", len(selected), 'were selected:')
437  #-ap for now:
438  maxLen = -1 # for individual listing, no limit on width
439  fmt1 = "%-6s %-35s [1]: %s "
440  fmt2 = " %35s [%d]: %s"
441 
442  N=[]
443  for wf in wfs:
444  if selected and float(wf.numId) not in selected: continue
445  if extended: print('')
446  #pad with zeros
447  for i in range(len(N),len(wf.cmds)): N.append(0)
448  N[len(wf.cmds)-1]+=1
449  wfName, stepNames = wf.nameId.split('+',1)
450  for i,s in enumerate(wf.cmds):
451  if extended:
452  if i==0:
453  print(fmt1 % (wf.numId, stepNames, (str(s)+' ')[:maxLen]))
454  else:
455  print(fmt2 % ( ' ', i+1, (str(s)+' ')[:maxLen]))
456  else:
457  print("%-6s %-35s "% (wf.numId, stepNames))
458  break
459  print('')
460  for i,n in enumerate(N):
461  if n: print(n,'workflows with',i+1,'steps')
462 
463  return
464 
465  def createWorkFlows(self, fileNameIn):
466 
467  prefixIn = self.filesPrefMap[fileNameIn]
468 
469  # get through the list of items and update the requested workflows only
470  keyList = self.workFlowSteps.keys()
471  ids = []
472  for item in keyList:
473  id, pref = item
474  if pref != prefixIn : continue
475  ids.append(id)
476  ids.sort()
477  for key in ids:
478  val = self.workFlowSteps[(key,prefixIn)]
479  num, name, commands, stepList = val
480  nameId = str(num)+'_'+name
481  if nameId in self.nameList:
482  print("==> duplicate name found for ", nameId)
483  print(' keeping : ', self.nameList[nameId])
484  print(' ignoring : ', val)
485  else:
486  self.nameList[nameId] = val
487 
488  self.workFlows.append(WorkFlow(num, name, commands=commands))
489 
490  return
491 
492  def prepare(self, useInput=None, refRel='', fromScratch=None):
493 
494  for matrixFile in self.files:
495  if self.what != 'all' and not any(el in matrixFile for el in self.what.split(",")):
496  print("ignoring non-requested file",matrixFile)
497  continue
498  if self.what == 'all' and not self.filesDefault[matrixFile]:
499  print("ignoring",matrixFile,"from default matrix")
500  continue
501 
502  try:
503  self.readMatrix(matrixFile, useInput, refRel, fromScratch)
504  except Exception as e:
505  print("ERROR reading file:", matrixFile, str(e))
506  raise
507 
508  try:
509  self.createWorkFlows(matrixFile)
510  except Exception as e:
511  print("ERROR creating workflows :", str(e))
512  raise
513 
514 
515  def show(self, selected=None, extended=True, cafVeto=True):
516 
517  self.showWorkFlows(selected, extended, cafVeto)
518  print('\n','-'*80,'\n')
519 
520 
521  def updateDB(self):
522 
523  import pickle
524  pickle.dump(self.workFlows, open('theMatrix.pkl', 'w') )
525 
526  return
527 
MatrixReader.MatrixReader.wm
wm
Definition: MatrixReader.py:23
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
MatrixReader.MatrixReader.workFlowsByLocation
def workFlowsByLocation(self, cafVeto=True)
Definition: MatrixReader.py:413
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
MatrixReader.MatrixReader.createWorkFlows
def createWorkFlows(self, fileNameIn)
Definition: MatrixReader.py:465
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
relval_steps
MatrixReader.MatrixReader.prepare
def prepare(self, useInput=None, refRel='', fromScratch=None)
Definition: MatrixReader.py:492
MatrixReader.MatrixReader.apply
apply
Definition: MatrixReader.py:26
MatrixReader.MatrixReader.commandLineWf
commandLineWf
Definition: MatrixReader.py:27
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
MatrixReader.MatrixReader.what
what
Definition: MatrixReader.py:35
MatrixReader.MatrixReader.relvalModule
relvalModule
Definition: MatrixReader.py:90
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
MatrixReader.MatrixReader.files
files
Definition: MatrixReader.py:59
MatrixReader.MatrixReader.noRun
noRun
Definition: MatrixReader.py:30
any
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:38
MatrixReader.MatrixReader.readMatrix
def readMatrix(self, fileNameIn, useInput=None, refRel=None, fromScratch=None)
Definition: MatrixReader.py:122
MatrixReader.MatrixReader.makeCmd
def makeCmd(self, step)
Definition: MatrixReader.py:94
submitPVValidationJobs.split
def split(sequence, size)
Definition: submitPVValidationJobs.py:352
MatrixReader.MatrixReader
Definition: MatrixReader.py:17
MatrixReader.MatrixReader.show
def show(self, selected=None, extended=True, cafVeto=True)
Definition: MatrixReader.py:515
str
#define str(s)
Definition: TestProcessor.cc:52
MatrixReader.MatrixReader.showRaw
def showRaw(self, useInput, refRel=None, fromScratch=None, what='all', step1Only=False, selected=None)
Definition: MatrixReader.py:311
MatrixReader.MatrixReader.makeStep
def makeStep(self, step, overrides)
Definition: MatrixReader.py:114
MatrixReader.MatrixException
Definition: MatrixReader.py:9
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
MatrixUtil.changeRefRelease
def changeRefRelease(steps, listOfPairs)
Definition: MatrixUtil.py:236
PVValHelper::add
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Definition: PVValidationHelpers.cc:12
Exception
MatrixReader.MatrixReader.filesPrefMap
filesPrefMap
Definition: MatrixReader.py:43
mps_setup.append
append
Definition: mps_setup.py:85
createfilelist.int
int
Definition: createfilelist.py:10
MatrixReader.MatrixReader.reset
def reset(self, what='all')
Definition: MatrixReader.py:33
MatrixReader.MatrixReader.workFlowSteps
workFlowSteps
Definition: MatrixReader.py:38
MatrixReader.MatrixReader.workFlows
workFlows
Definition: MatrixReader.py:40
MatrixReader.MatrixReader.updateDB
def updateDB(self)
Definition: MatrixReader.py:521
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
WorkFlow
Definition: WorkFlow.py:1
MatrixReader.MatrixReader.showWorkFlows
def showWorkFlows(self, selected=None, extended=True, cafVeto=True)
Definition: MatrixReader.py:428
MatrixReader.MatrixException.__str__
def __str__(self)
Definition: MatrixReader.py:12
MatrixReader.MatrixReader.nameList
nameList
Definition: MatrixReader.py:41
MatrixReader.MatrixReader.filesDefault
filesDefault
Definition: MatrixReader.py:74
MatrixReader.MatrixReader.addCommand
addCommand
Definition: MatrixReader.py:25
MatrixReader.MatrixException.msg
msg
Definition: MatrixReader.py:11
genParticles_cff.map
map
Definition: genParticles_cff.py:11
MatrixReader.MatrixReader.overWrite
overWrite
Definition: MatrixReader.py:28
MatrixReader.MatrixException.__init__
def __init__(self, msg)
Definition: MatrixReader.py:10
MatrixReader.MatrixReader.revertDqmio
revertDqmio
maybe we want too level deep input
Definition: MatrixReader.py:24
MatrixReader.MatrixReader.__init__
def __init__(self, opt)
Definition: MatrixReader.py:19
merge
Definition: merge.py:1