CMS 3D CMS Logo

cmsDriverOptions.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # A Pyrelval Wrapper
4 
5 from __future__ import print_function
6 import optparse
7 import sys
8 import os
9 import re
10 import Configuration.Applications
11 from Configuration.Applications.ConfigBuilder import ConfigBuilder, defaultOptions
12 import traceback
13 from functools import reduce
14 
15 def checkModifier(era):
16  from FWCore.ParameterSet.Config import Modifier, ModifierChain
17  return isinstance( era, Modifier ) or isinstance( era, ModifierChain )
18 
20  return
21 
23  return
24 
25 def OptionsFromCommand(command):
26  items=command.split()
27  if items[0] != 'cmsDriver.py':
28  return None
29  items.append('--evt_type')
30  items.append(items[1])
31  options=OptionsFromItems(items[2:])
32  options.arguments = command
33  return options
34 
36  import sys
37  options=OptionsFromItems(sys.argv[1:])
38  # memorize the command line arguments
39  options.arguments = reduce(lambda x, y: x+' '+y, sys.argv[1:])
40  return options
41 
42 def OptionsFromItems(items):
43  import sys
44  from Configuration.Applications.Options import parser,threeValued
45  #three valued options
46  for (index,item) in enumerate(items):
47  for (opt,value) in threeValued:
48  if (str(item) in opt) and (index==len(items)-1 or items[index+1].startswith('-')):
49  items.insert(index+1,value)
50 
51  (options,args) = parser.parse_args(items)
52 
53  if not options.conditions or options.conditions=="help":
54  from Configuration.AlCa import autoCond
55  possible=""
56  for k in autoCond.autoCond:
57  possible+="\nauto:"+k+" -> "+str(autoCond.autoCond[k])
58  raise Exception("the --conditions option is mandatory. Possibilities are: "+possible)
59 
60 
61 
64 
65  # check in case of ALCAOUTPUT case for alca splitting
66  if options.triggerResultsProcess == None and "ALCAOUTPUT" in options.step:
67  print("ERROR: If ALCA splitting is requested, the name of the process in which the alca producers ran needs to be specified. E.g. via --triggerResultsProcess RECO")
68  sys.exit(1)
69 
70  if not options.evt_type:
71  options.evt_type=sys.argv[1]
72 
73  #now adjust the given parameters before passing it to the ConfigBuilder
74 
75  # concurrency options
76  nStreams = options.nStreams if options.nStreams != '0' else options.nThreads
77  if options.nConcurrentLumis == '0':
78  options.nConcurrentLumis = '1' if nStreams == '1' else '2'
79  if options.nConcurrentIOVs == '0':
80  options.nConcurrentIOVs = options.nConcurrentLumis
81 
82  #trail a "/" to dirin and dirout
83  if options.dirin!='' and (not options.dirin.endswith('/')): options.dirin+='/'
84  if options.dirout!='' and (not options.dirout.endswith('/')): options.dirout+='/'
85 
86  # Build the IO files if necessary.
87  # The default form of the files is:
88  # <type>_<energy>_<step>.root
89  prec_step = {"NONE":"",
90  "FILTER":"",
91  "ALL":"",
92  "LHE":"",
93  "GEN":"",
94  "reGEN":"",
95  "SIM":"GEN",
96  "reSIM":"SIM",
97  "DIGI":"SIM",
98  "reDIGI":"DIGI",
99  "L1REPACK":"RAW",
100  "HLT":"RAW",
101  "RECO":"DIGI",
102  "ALCA":"RECO",
103  "ANA":"RECO",
104  "SKIM":"RECO",
105  "DIGI2RAW":"DIGI",
106  "RAW2DIGI":"DIGI2RAW",
107  "RAW2RECO":"DIGI2RAW",
108  "DATAMIX":"DIGI",
109  "DIGI2RAW":"DATAMIX",
110  "HARVESTING":"RECO",
111  "ALCAHARVEST":"RECO",
112  "PAT":"RECO",
113  "NANO":"PAT",
114  "PATGEN":"GEN"}
115 
116  trimmedEvtType=options.evt_type.split('/')[-1]
117 
118  #get the list of steps, without their options
119  options.trimmedStep=[]
120  for s in options.step.split(','):
121  step=s.split(':')[0]
122  options.trimmedStep.append(step)
123  first_step=options.trimmedStep[0]
124 
125  #replace step aliases
126  # this does not affect options.trimmedStep which still contains 'NONE'
127  stepsAliases={
128  'NONE':'',
129  'ALL':'GEN,SIM,DIGI,L1,DIGI2RAW,HLT:GRun,RAW2DIGI,RECO,POSTRECO,VALIDATION,DQM',
130  'DATA_CHAIN':'RAW2DIGI,RECO,POSTRECO,DQM'
131  }
132  if options.step in stepsAliases:
133  options.step=stepsAliases[options.step]
134 
135  options.step = options.step.replace("SIM_CHAIN","GEN,SIM,DIGI,L1,DIGI2RAW")
136 
137  # add on the end of job sequence...
138  addEndJob = True
139  if ("FASTSIM" in options.step and not "VALIDATION" in options.step) or "HARVESTING" in options.step or "ALCAHARVEST" in options.step or "ALCAOUTPUT" in options.step or options.step == "":
140  addEndJob = False
141  if ("SKIM" in options.step and not "RECO" in options.step):
142  addEndJob = False
143  if ("ENDJOB" in options.step):
144  addEndJob = False
145  if ('DQMIO' in options.datatier):
146  addEndJob = False
147  if addEndJob:
148  options.step=options.step+',ENDJOB'
149 
150 
151  #determine the type of file on input
152  if options.filetype==defaultOptions.filetype:
153  if options.filein.lower().endswith(".lhe") or options.filein.lower().endswith(".lhef") or options.filein.startswith("lhe:"):
154  options.filetype="LHE"
155  elif options.filein.startswith("mcdb:"):
156  print("This is a deprecated way of selecting lhe files from article number. Please use lhe:article argument to --filein")
157  options.filein=options.filein.replace('mcdb:','lhe:')
158  options.filetype="LHE"
159  else:
160  options.filetype="EDM"
161 
162  filesuffix = {"LHE": "lhe", "EDM": "root", "MCDB": "", "DQM":"root"}[options.filetype]
163 
164  if options.filein=="" and not (first_step in ("ALL","GEN","LHE","SIM_CHAIN")):
165  options.dirin="file:"+options.dirin.replace('file:','')
166  options.filein=trimmedEvtType+"_"+prec_step[first_step]+"."+filesuffix
167 
168 
169  # Prepare the canonical file name for output / config file etc
170  # (EventType_STEP1_STEP2_..._PU)
171  standardFileName = ""
172  standardFileName = trimmedEvtType+"_"+"_".join(options.trimmedStep)
173  standardFileName = standardFileName.replace(",","_").replace(".","_")
174  if options.pileup != "NoPileUp":
175  standardFileName += "_PU"
176 
177 
178  # if no output file name given, set it to default
179  if options.fileout=="" and not first_step in ("HARVESTING", "ALCAHARVEST"):
180  options.fileout = standardFileName+".root"
181 
182  # Prepare the name of the config file
183  if not options.python_filename:
184  options.python_filename = standardFileName+'.py'
185 
186  print(options.step)
187 
188 
189  # Setting name of process
190  # if not set explicitly it needs some thinking
191  if not options.name:
192  if 'reSIM' in options.trimmedStep:
193  options.name = 'RESIM'
194  elif 'reDIGI' in options.trimmedStep:
195  options.name = 'REDIGI'
196  elif 'HLT' in options.trimmedStep:
197  options.name = 'HLT'
198  elif 'RECO' in options.trimmedStep:
199  options.name = 'RECO'
200  elif options.trimmedStep == ['NONE'] and options.filetype in ('LHE', 'MCDB'):
201  options.name = 'LHE'
202  elif len(options.trimmedStep)==0:
203  options.name = 'PROCESS'
204  else:
205  options.name = options.trimmedStep[-1]
206 
207  # check to be sure that people run the harvesting as a separate step
208  isHarvesting = False
209  isOther = False
210 
211  if "HARVESTING" in options.trimmedStep and len(options.trimmedStep) > 1:
212  raise Exception("The Harvesting step must be run alone")
213 
214  # if not specified by user try to guess whether MC or DATA
215  if not options.isData and not options.isMC:
216  if 'LHE' in options.trimmedStep or 'LHE' in options.datatier:
217  options.isMC=True
218  if 'GEN' in options.trimmedStep or 'GEN' in options.datatier:
219  options.isMC=True
220  if 'SIM' in options.trimmedStep:
221  options.isMC=True
222  if 'CFWRITER' in options.trimmedStep:
223  options.isMC=True
224  if 'DIGI' in options.trimmedStep:
225  options.isMC=True
226  if 'DIGI2RAW' in options.trimmedStep:
227  options.isMC=True
228  if (not (options.eventcontent == None)) and 'SIM' in options.eventcontent:
229  options.isMC=True
230  if 'SIM' in options.datatier:
231  options.isMC=True
232  if options.isMC:
233  print('We have determined that this is simulation (if not, rerun cmsDriver.py with --data)')
234  else:
235  print('We have determined that this is real data (if not, rerun cmsDriver.py with --mc)')
236 
237  if options.profile:
238  if options.profile and options.prefix:
239  raise Exception("--profile and --prefix are incompatible")
240  profilerType = 'pp'
241  profileOpts = options.profile.split(':')
242  if len(profileOpts):
243  profilerType = profileOpts[0].replace("=", " ")
244 
245  if profilerType == "pp":
246  options.profileTypeLabel = "performance"
247  elif profilerType == "mp":
248  options.profileTypeLabel = "memory"
249  elif profilerType.startswith("fp "):
250  options.profileTypeLabel = profilerType.replace("fp ", "")
251  else:
252  raise Exception("Not a valid profiler type %s. Alternatives are pp, mp, fp=<function>."%(profilerType))
253 
254  options.prefix = "igprof -t cmsRun -%s" % profilerType
255 
256  # If an "era" argument was supplied make sure it is one of the valid possibilities
257  if options.era :
258  from Configuration.StandardSequences.Eras import eras
259  # Split the string by commas to check individual eras
260  requestedEras = options.era.split(",")
261  # Check that the entry is a valid era
262  for eraName in requestedEras :
263  if not hasattr( eras, eraName ) or not checkModifier(getattr(eras,eraName)): # Not valid, so print a helpful message
264  validOptions="" # Create a stringified list of valid options to print to the user
265  for key in eras.__dict__ :
266  if checkModifier(eras.__dict__[key]):
267  if validOptions!="" : validOptions+=", "
268  validOptions+="'"+key+"'"
269  raise Exception( "'%s' is not a valid option for '--era'. Valid options are %s." % (eraName, validOptions) )
270  # If the "--fast" option was supplied automatically enable the fastSim era
271  if options.fast :
272  if options.era:
273  options.era+=",fastSim"
274  else :
275  options.era="fastSim"
276 
277  # options incompatible with fastsim
278  if options.fast and not options.scenario == "pp":
279  raise Exception("ERROR: the --option fast is only compatible with the default scenario (--scenario=pp)")
280  if options.fast and 'HLT' in options.trimmedStep:
281  raise Exception("ERROR: the --option fast is incompatible with HLT (HLT is no longer available in FastSim)")
282 
283  return options
284 
cmsDriverOptions.checkModifier
def checkModifier(era)
Definition: cmsDriverOptions.py:15
Options
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
cmsDriverOptions.OptionsFromCommandLine
def OptionsFromCommandLine()
Definition: cmsDriverOptions.py:35
str
#define str(s)
Definition: TestProcessor.cc:52
cmsDriverOptions.OptionsFromItems
def OptionsFromItems(items)
Definition: cmsDriverOptions.py:42
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
Exception
cmsDriverOptions.OptionsFromCommand
def OptionsFromCommand(command)
Definition: cmsDriverOptions.py:25
cmsDriverOptions.adaptOptions
def adaptOptions()
Definition: cmsDriverOptions.py:22
cmsDriverOptions.checkOptions
def checkOptions()
Definition: cmsDriverOptions.py:19
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444