CMS 3D CMS Logo

runTheMatrix.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 from __future__ import print_function
3 import sys, os
4 
5 from Configuration.PyReleaseValidation.MatrixReader import MatrixReader
6 from Configuration.PyReleaseValidation.MatrixRunner import MatrixRunner
7 from Configuration.PyReleaseValidation.MatrixInjector import MatrixInjector,performInjectionOptionTest
8 
9 # ================================================================================
10 
11 def showRaw(opt):
12 
13  mrd = MatrixReader(opt)
14  mrd.showRaw(opt.useInput, opt.refRel, opt.fromScratch, opt.raw, opt.step1Only, selected=opt.testList)
15 
16  return 0
17 
18 # ================================================================================
19 
20 def runSelected(opt):
21 
22  mrd = MatrixReader(opt)
23  mrd.prepare(opt.useInput, opt.refRel, opt.fromScratch)
24 
25  # test for wrong input workflows
26  if opt.testList:
27  definedWf = [dwf.numId for dwf in mrd.workFlows]
28  definedSet = set(definedWf)
29  testSet = set(opt.testList)
30  undefSet = testSet - definedSet
31  if len(undefSet)>0: raise ValueError('Undefined workflows: '+', '.join(map(str,list(undefSet))))
32  duplicates = [wf for wf in testSet if definedWf.count(wf)>1 ]
33  if len(duplicates)>0: raise ValueError('Duplicated workflows: '+', '.join(map(str,list(duplicates))))
34 
35  ret = 0
36  if opt.show:
37  mrd.show(opt.testList, opt.extended, opt.cafVeto)
38  if opt.testList : print('testListected items:', opt.testList)
39  else:
40  mRunnerHi = MatrixRunner(mrd.workFlows, opt.nProcs, opt.nThreads)
41  ret = mRunnerHi.runTests(opt)
42 
43  if opt.wmcontrol:
44  if ret!=0:
45  print('Cannot go on with wmagent injection with failing workflows')
46  else:
47  wfInjector = MatrixInjector(opt,mode=opt.wmcontrol,options=opt.wmoptions)
48  ret= wfInjector.prepare(mrd,
49  mRunnerHi.runDirs)
50  if ret==0:
51  wfInjector.upload()
52  wfInjector.submit()
53  return ret
54 
55 # ================================================================================
56 
57 if __name__ == '__main__':
58 
59  #this can get out of here
60  predefinedSet={
61  'limited' : [5.1, #FastSim ttbar
62  7.3, #CosmicsSPLoose_UP17
63  8, #BH/Cosmic MC
64  25, #MC ttbar
65  4.22, #cosmic data
66  4.53, #run1 data + miniAOD
67  9.0, #Higgs200 charged taus
68  1000, #data+prompt
69  1001, #data+express
70  101.0, #SingleElectron120E120EHCAL
71  136.731, #2016B Photon data
72  136.7611, #2016E JetHT reMINIAOD from 80X legacy
73  136.8311, #2017F JetHT reMINIAOD from 94X reprocessing
74  136.88811,#2018D JetHT reMINIAOD from UL processing
75  136.793, #2017C DoubleEG
76  136.874, #2018C EGamma
77  138.4, #2021 MinimumBias prompt reco
78  138.5, #2021 MinimumBias express
79  139.001, #2021 MinimumBias offline with HLT step
80  140.53, #2011 HI data
81  140.56, #2018 HI data
82  158.01, #reMiniAOD of 2018 HI MC with pp-like reco
83  312.0, #2021/Run3 HI MC Pyquen_ZeemumuJets_pt10 with pp-like reco
84  1306.0, #SingleMu Pt1 UP15
85  1325.81, #test NanoAOD from existing MINI UL 106Xv1
86  136.8523, #test NanoAOD from existing reMINI UL 106Xv2
87  1330, #Run2 2015/2016 MC Zmm
88  135.4, #Run 2 2015/2016 Zee ttbar fastsim
89  10042.0, #2017 ZMM
90  10024.0, #2017 ttbar
91  10824.0, #2018 ttbar
92  2018.1, #2018 ttbar fastsim
93  11634.911, #2021 DD4hep ttbar reading geometry from XML
94  11634.914, #2021 DDD ttbar reading geometry from the DB
95  11634.0, #2021 ttbar (switching to DD4hep by default)
96  11634.301, #2021 ttbar fastsim
97  11634.7, #2021 ttbar mkFit
98  11834.0, #2021 ttbar PU
99  12434.0, #2023 ttbar
100  23234.0, #2026D49 ttbar (HLT TDR baseline w/ HGCal v11)
101  28234.0, #2026D60 (exercise HF nose)
102  35034.0, #2026D77 ttbar
103  39434.0, #2026D88 ttbar (2022 new baseline)
104  39434.75, #2026D88 ttbar with HLT75e33
105  #39434.911, #2026D88 ttbar DD4hep XML
106  39634.999, #2026D88 ttbar premixing stage1+stage2, PU50
107  39496.0, #CE_E_Front_120um D88
108  39500.0, #CE_H_Coarse_Scint D88
109  25202.0, #2016 ttbar UP15 PU
110  250202.181, #2018 ttbar stage1 + stage2 premix
111  ],
112  'jetmc': [5.1, 13, 15, 25, 38, 39], #MC
113  'metmc' : [5.1, 15, 25, 37, 38, 39], #MC
114  'muonmc' : [5.1, 124.4, 124.5, 20, 21, 22, 23, 25, 30], #MC
115  }
116 
117 
118  import argparse
119  usage = 'usage: runTheMatrix.py --show -s '
120 
121  parser = argparse.ArgumentParser(usage,formatter_class=argparse.ArgumentDefaultsHelpFormatter)
122 
123  parser.add_argument('-b','--batchName',
124  help='relval batch: suffix to be appended to Campaign name',
125  dest='batchName',
126  default='')
127 
128  parser.add_argument('-m','--memoryOffset',
129  help='memory of the wf for single core',
130  dest='memoryOffset',
131  type=int,
132  default=3000)
133 
134  parser.add_argument('--addMemPerCore',
135  help='increase of memory per each n > 1 core: memory(n_core) = memoryOffset + (n_core-1) * memPerCore',
136  dest='memPerCore',
137  type=int,
138  default=1500)
139 
140  parser.add_argument('-j','--nproc',
141  help='number of processes. 0 Will use 4 processes, not execute anything but create the wfs',
142  dest='nProcs',
143  type=int,
144  default=4)
145 
146  parser.add_argument('-t','--nThreads',
147  help='number of threads per process to use in cmsRun.',
148  dest='nThreads',
149  type=int,
150  default=1)
151 
152  parser.add_argument('--nStreams',
153  help='number of streams to use in cmsRun.',
154  dest='nStreams',
155  type=int,
156  default=0)
157 
158  parser.add_argument('--numberEventsInLuminosityBlock',
159  help='number of events in a luminosity block',
160  dest='numberEventsInLuminosityBlock',
161  type=int,
162  default=-1)
163 
164  parser.add_argument('-n','--showMatrix',
165  help='Only show the worflows. Use --ext to show more',
166  dest='show',
167  default=False,
168  action='store_true')
169 
170  parser.add_argument('-e','--extended',
171  help='Show details of workflows, used with --show',
172  dest='extended',
173  default=False,
174  action='store_true')
175 
176  parser.add_argument('-s','--selected',
177  help='Run a pre-defined selected matrix of wf. Deprecated, please use -l limited',
178  dest='restricted',
179  default=False,
180  action='store_true')
181 
182  parser.add_argument('-l','--list',
183  help='Comma separated list of workflow to be shown or ran. Possible keys are also '+str(predefinedSet.keys())+'. and wild card like muon, or mc',
184  dest='testList',
185  default=None)
186 
187  parser.add_argument('-r','--raw',
188  help='Temporary dump the .txt needed for prodAgent interface. To be discontinued soon. Argument must be the name of the set (standard, pileup,...)',
189  dest='raw')
190 
191  parser.add_argument('-i','--useInput',
192  help='Use recyling where available. Either all, or a comma separated list of wf number.',
193  dest='useInput',
194  type=lambda x: x.split(','),
195  default=None)
196 
197  parser.add_argument('-w','--what',
198  help='Specify the set to be used. Argument must be the name of a set (standard, pileup,...) or multiple sets separated by commas (--what standard,pileup )',
199  dest='what',
200  default='all')
201 
202  parser.add_argument('--step1',
203  help='Used with --raw. Limit the production to step1',
204  dest='step1Only',
205  default=False)
206 
207  parser.add_argument('--maxSteps',
208  help='Only run maximum on maxSteps. Used when we are only interested in first n steps.',
209  dest='maxSteps',
210  default=9999,
211  type=int)
212 
213  parser.add_argument('--fromScratch',
214  help='Comma separated list of wf to be run without recycling. all is not supported as default.',
215  dest='fromScratch',
216  type=lambda x: x.split(','),
217  default=None)
218 
219  parser.add_argument('--refRelease',
220  help='Allow to modify the recycling dataset version',
221  dest='refRel',
222  default=None)
223 
224  parser.add_argument('--wmcontrol',
225  help='Create the workflows for injection to WMAgent. In the WORKING. -wmcontrol init will create the the workflows, -wmcontrol test will dryRun a test, -wmcontrol submit will submit to wmagent',
226  choices=['init','test','submit','force'],
227  dest='wmcontrol',
228  default=None)
229 
230  parser.add_argument('--revertDqmio',
231  help='When submitting workflows to wmcontrol, force DQM outout to use pool and not DQMIO',
232  choices=['yes','no'],
233  dest='revertDqmio',
234  default='no')
235 
236  parser.add_argument('--optionswm',
237  help='Specify a few things for wm injection',
238  default='',
239  dest='wmoptions')
240 
241  parser.add_argument('--keep',
242  help='allow to specify for which comma separated steps the output is needed',
243  default=None)
244 
245  parser.add_argument('--label',
246  help='allow to give a special label to the output dataset name',
247  default='')
248 
249  parser.add_argument('--command',
250  help='provide a way to add additional command to all of the cmsDriver commands in the matrix',
251  dest='command',
252  action='append',
253  default=None)
254 
255  parser.add_argument('--apply',
256  help='allow to use the --command only for 1 comma separeated',
257  dest='apply',
258  default=None)
259 
260  parser.add_argument('--workflow',
261  help='define a workflow to be created or altered from the matrix',
262  action='append',
263  dest='workflow',
264  default=None)
265 
266  parser.add_argument('--dryRun',
267  help='do not run the wf at all',
268  action='store_true',
269  dest='dryRun',
270  default=False)
271 
272  parser.add_argument('--testbed',
273  help='workflow injection to cmswebtest (you need dedicated rqmgr account)',
274  dest='testbed',
275  default=False,
276  action='store_true')
277 
278  parser.add_argument('--noCafVeto',
279  help='Run from any source, ignoring the CAF label',
280  dest='cafVeto',
281  default=True,
282  action='store_false')
283 
284  parser.add_argument('--overWrite',
285  help='Change the content of a step for another. List of pairs.',
286  dest='overWrite',
287  default=None)
288 
289  parser.add_argument('--noRun',
290  help='Remove all run list selection from wfs',
291  dest='noRun',
292  default=False,
293  action='store_true')
294 
295  parser.add_argument('--das-options',
296  help='Options to be passed to dasgoclient.',
297  dest='dasOptions',
298  default="--limit 0",
299  action='store')
300 
301  parser.add_argument('--job-reports',
302  help='Dump framework job reports',
303  dest='jobReports',
304  default=False,
305  action='store_true')
306 
307  parser.add_argument('--ibeos',
308  help='Use IB EOS site configuration',
309  dest='IBEos',
310  default=False,
311  action='store_true')
312 
313  parser.add_argument('--sites',
314  help='Run DAS query to get data from a specific site. Set it to empty string to search all sites.',
315  dest='dasSites',
316  default='T2_CH_CERN',
317  action='store')
318 
319  parser.add_argument('--interactive',
320  help="Open the Matrix interactive shell",
321  action='store_true',
322  default=False)
323 
324  parser.add_argument('--dbs-url',
325  help='Overwrite DbsUrl value in JSON submitted to ReqMgr2',
326  dest='dbsUrl',
327  default=None,
328  action='store')
329 
330  gpugroup = parser.add_argument_group('GPU-related options','These options are only meaningful when --gpu is used, and is not set to forbidden.')
331 
332  gpugroup.add_argument('--gpu','--requires-gpu',
333  help='Enable GPU workflows. Possible options are "forbidden" (default), "required" (implied if no argument is given), or "optional".',
334  dest='gpu',
335  choices=['forbidden', 'optional', 'required'],
336  nargs='?',
337  const='required',
338  default='forbidden',
339  action='store')
340 
341  gpugroup.add_argument('--gpu-memory',
342  help='Specify the minimum amount of GPU memory required by the job, in MB.',
343  dest='GPUMemoryMB',
344  type=int,
345  default=8000)
346 
347  gpugroup.add_argument('--cuda-capabilities',
348  help='Specify a comma-separated list of CUDA "compute capabilities", or GPU hardware architectures, that the job can use.',
349  dest='CUDACapabilities',
350  type=lambda x: x.split(','),
351  default='6.0,6.1,6.2,7.0,7.2,7.5,8.0,8.6')
352 
353  # read the CUDA runtime version included in CMSSW
354  cudart_version = None
355  libcudart = os.path.realpath(os.path.expandvars('$CMSSW_RELEASE_BASE/external/$SCRAM_ARCH/lib/libcudart.so'))
356  if os.path.isfile(libcudart):
357  cudart_basename = os.path.basename(libcudart)
358  cudart_version = '.'.join(cudart_basename.split('.')[2:4])
359  gpugroup.add_argument('--cuda-runtime',
360  help='Specify major and minor version of the CUDA runtime used to build the application.',
361  dest='CUDARuntime',
362  default=cudart_version)
363 
364  gpugroup.add_argument('--force-gpu-name',
365  help='Request a specific GPU model, e.g. "Tesla T4" or "NVIDIA GeForce RTX 2080". The default behaviour is to accept any supported GPU.',
366  dest='GPUName',
367  default='')
368 
369  gpugroup.add_argument('--force-cuda-driver-version',
370  help='Request a specific CUDA driver version, e.g. 470.57.02. The default behaviour is to accept any supported CUDA driver version.',
371  dest='CUDADriverVersion',
372  default='')
373 
374  gpugroup.add_argument('--force-cuda-runtime-version',
375  help='Request a specific CUDA runtime version, e.g. 11.4. The default behaviour is to accept any supported CUDA runtime version.',
376  dest='CUDARuntimeVersion',
377  default='')
378 
379  opt = parser.parse_args()
380  if opt.command: opt.command = ' '.join(opt.command)
381  os.environ["CMSSW_DAS_QUERY_SITES"]=opt.dasSites
382  if opt.IBEos:
383  from subprocess import getstatusoutput as run_cmd
384 
385  ibeos_cache = os.path.join(os.getenv("LOCALRT"), "ibeos_cache.txt")
386  if not os.path.exists(ibeos_cache):
387  err, out = run_cmd("curl -L -s -o %s https://raw.githubusercontent.com/cms-sw/cms-sw.github.io/master/das_queries/ibeos.txt" % ibeos_cache)
388  if err:
389  run_cmd("rm -f %s" % ibeos_cache)
390  print("Error: Unable to download ibeos cache information")
391  print(out)
392  sys.exit(err)
393 
394  for cmssw_env in [ "CMSSW_BASE", "CMSSW_RELEASE_BASE" ]:
395  cmssw_base = os.getenv(cmssw_env,None)
396  if not cmssw_base: continue
397  cmssw_base = os.path.join(cmssw_base,"src/Utilities/General/ibeos")
398  if os.path.exists(cmssw_base):
399  os.environ["PATH"]=cmssw_base+":"+os.getenv("PATH")
400  os.environ["CMS_PATH"]="/cvmfs/cms-ib.cern.ch"
401  os.environ["CMSSW_USE_IBEOS"]="true"
402  print(">> WARNING: You are using SITECONF from /cvmfs/cms-ib.cern.ch")
403  break
404  if opt.restricted:
405  print('Deprecated, please use -l limited')
406  if opt.testList: opt.testList+=',limited'
407  else: opt.testList='limited'
408 
409  def stepOrIndex(s):
410  if s.isdigit():
411  return int(s)
412  else:
413  return s
414  if opt.apply:
415  opt.apply=map(stepOrIndex,opt.apply.split(','))
416  if opt.keep:
417  opt.keep=map(stepOrIndex,opt.keep.split(','))
418 
419  if opt.testList:
420  testList=[]
421  for entry in opt.testList.split(','):
422  if not entry: continue
423  mapped=False
424  for k in predefinedSet:
425  if k.lower().startswith(entry.lower()) or k.lower().endswith(entry.lower()):
426  testList.extend(predefinedSet[k])
427  mapped=True
428  break
429  if not mapped:
430  try:
431  testList.append(float(entry))
432  except:
433  print(entry,'is not a possible selected entry')
434 
435  opt.testList = list(set(testList))
436 
437  if opt.wmcontrol:
439  if opt.overWrite:
440  opt.overWrite=eval(opt.overWrite)
441  if opt.interactive:
442  import cmd
443 
444  class TheMatrix(cmd.Cmd):
445  intro = "Welcome to the Matrix (? for help)"
446  prompt = "matrix> "
447 
448  def __init__(self, opt):
449  cmd.Cmd.__init__(self)
450  self.opt_ = opt
451  self.matrices_ = {}
452  tmp = MatrixReader(self.opt_)
453  for what in tmp.files:
454  what = what.replace('relval_','')
455  self.opt_.what = what
456  self.matrices_[what] = MatrixReader(self.opt_)
457  self.matrices_[what].prepare(self.opt_.useInput, self.opt_.refRel,
458  self.opt_.fromScratch)
459  os.system("clear")
460 
461  def do_clear(self, arg):
462  """Clear the screen, put prompt at the top"""
463  os.system("clear")
464 
465  def do_exit(self, arg):
466  print("Leaving the Matrix")
467  return True
468 
469  def default(self, inp):
470  if inp == 'x' or inp == 'q':
471  return self.do_exit(inp)
472 
473  def help_predefined(self):
474  print("\n".join(["predefined [predef1 [...]]\n",
475  "Run w/o argument, it will print the list of known predefined workflows.",
476  "Run with space-separated predefined workflows, it will print the workflow-ids registered to them"]))
477 
478  def complete_predefined(self, text, line, start_idx, end_idx):
479  if text and len(text) > 0:
480  return [t for t in predefinedSet.keys() if t.startswith(text)]
481  else:
482  return predefinedSet.keys()
483 
484  def do_predefined(self, arg):
485  """Print the list of predefined workflows"""
486  print("List of predefined workflows")
487  if arg:
488  for w in arg.split():
489  if w in predefinedSet.keys():
490  print("Predefined Set: %s" % w)
491  print(predefinedSet[w])
492  else:
493  print("Unknown Set: %s" % w)
494  else:
495  print(predefinedSet.keys())
496 
497  def help_showWorkflow(self):
498  print("\n".join(["showWorkflow [workflow1 [...]]\n",
499  "Run w/o arguments, it will print the list of registered macro-workflows.",
500  "Run with space-separated workflows, it will print the full list of workflow-ids registered to them"]))
501 
502  def complete_showWorkflow(self, text, line, start_idx, end_idx):
503  if text and len(text) > 0:
504  return [t for t in self.matrices_.keys() if t.startswith(text)]
505  else:
506  return self.matrices_.keys()
507 
508  def do_showWorkflow(self, arg):
509  if arg == '':
510  print("Available workflows:")
511  for k in self.matrices_.keys():
512  print(k)
513  else:
514  selected = arg.split()
515  for k in selected:
516  if k not in self.matrices_.keys():
517  print("Unknown workflow %s: skipping" % k)
518  else:
519  for wfl in self.matrices_[k].workFlows:
520  wfName, stepNames = wfl.nameId.split('+',1)
521  print("%s %s %s" % (wfl.numId, wfName, stepNames))
522  print("%s contains %d workflows" % (k, len(self.matrices_[k].workFlows)))
523 
525  print("\n".join(["searchInWorkflow wfl_name search_regexp\n",
526  "This command will search for a match within all workflows registered to wfl_name.",
527  "The search is done on both the workflow name and the names of steps registered to it."]))
528 
529  def complete_searchInWorkflow(self, text, line, start_idx, end_idx):
530  if text and len(text) > 0:
531  return [t for t in self.matrices_.keys() if t.startswith(text)]
532  else:
533  return self.matrices_.keys()
534 
535  def do_searchInWorkflow(self, arg):
536  args = arg.split()
537  if len(args) < 2:
538  print("searchInWorkflow name regexp")
539  return
540  if args[0] not in self.matrices_.keys():
541  print("Unknown workflow")
542  return
543  import re
544  pattern = None
545  try:
546  pattern = re.compile(args[1])
547  except:
548  print("Failed to compile regexp %s" % args[1])
549  return
550  counter = 0
551  for wfl in self.matrices_[args[0]].workFlows:
552  wfName, stepNames = wfl.nameId.split('+',1)
553  if re.match(pattern, wfName) or re.match(pattern, stepNames):
554  print("%s %s %s" % (wfl.numId, wfName, stepNames))
555  counter += 1
556  print("Found %d compatible workflows inside %s" % (counter, args[0]))
557 
558  def help_search(self):
559  print("\n".join(["search search_regexp\n",
560  "This command will search for a match within all workflows registered.",
561  "The search is done on both the workflow name and the names of steps registered to it."]))
562 
563  def do_search(self, arg):
564  args = arg.split()
565  if len(args) < 1:
566  print("search regexp")
567  return
568  for wfl in self.matrices_.keys():
569  self.do_searchInWorkflow(' '.join([wfl, args[0]]))
570 
572  print("\n".join(["dumpWorkflowId [wfl-id1 [...]]\n",
573  "Dumps the details (cmsDriver commands for all steps) of the space-separated workflow-ids in input."]))
574 
575  def do_dumpWorkflowId(self, arg):
576  wflids = arg.split()
577  if len(wflids) == 0:
578  print("dumpWorkflowId [wfl-id1 [...]]")
579  return
580 
581  fmt = "[%d]: %s\n"
582  maxLen = 100
583  for wflid in wflids:
584  dump = True
585  for key, mrd in self.matrices_.items():
586  for wfl in mrd.workFlows:
587  if wfl.numId == float(wflid):
588  wfName, stepNames = wfl.nameId.split('+',1)
589  if dump:
590  dump = False
591  print(wfl.numId, stepNames)
592  for i,s in enumerate(wfl.cmds):
593  print(fmt % (i+1, (str(s)+' ')))
594  print("\nWorkflow found in %s." % key)
595  else:
596  print("Workflow also found in %s." % key)
597 
598  do_EOF = do_exit
599 
600  TheMatrix(opt).cmdloop()
601  sys.exit(0)
602 
603  if opt.raw and opt.show:
604  ret = showRaw(opt)
605  else:
606  ret = runSelected(opt)
607 
608 
609  sys.exit(ret)
def performInjectionOptionTest(opt)
def runSelected(opt)
Definition: runTheMatrix.py:20
def do_predefined(self, arg)
def do_showWorkflow(self, arg)
def complete_showWorkflow(self, text, line, start_idx, end_idx)
def do_search(self, arg)
def showRaw(opt)
Definition: runTheMatrix.py:11
def do_clear(self, arg)
def help_dumpWorkflowId(self)
def complete_predefined(self, text, line, start_idx, end_idx)
def help_searchInWorkflow(self)
def do_dumpWorkflowId(self, arg)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def __init__(self, opt)
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def do_exit(self, arg)
def default(self, inp)
def complete_searchInWorkflow(self, text, line, start_idx, end_idx)
def stepOrIndex(s)
#define str(s)
def do_searchInWorkflow(self, arg)