CMS 3D CMS Logo

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