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