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