CMS 3D CMS Logo

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