CMS 3D CMS Logo

HltComparatorCreateWorkflow.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Original Author: James Jackson
3 
4 # MakeValidationConfig.py
5 # Makes CMSSW config for prompt validation of a given run number,
6 # HLT Key or HLT Config
7 
8 from __future__ import print_function
9 from optparse import OptionParser
10 import os, time, re
11 
12 jobHash = "%s_%s_%s" % (os.getuid(), os.getpid(), int(time.time()))
13 
14 usage = '%prog [options]. \n\t-a and -k required, -h for help.'
15 parser = OptionParser(usage)
16 parser.add_option("-a", "--analysis", dest="analysis",
17  help="analysis configuration file")
18 # not yet implemented
19 #parser.add_option("-r", "--run", dest="run",
20 # help="construct config for runnumber RUN", metavar="RUN")
21 parser.add_option("-k", "--hltkey", dest="hltkey",
22  help="ignore RunRegistry and force the use of HLT key KEY",
23  metavar="KEY")
24 parser.add_option("-c", "--hltcff", dest="hltcff",
25  help="use the config fragment CFF to define HLT configuration",
26  metavar="CFF")
27 parser.add_option("-f", "--frontier", dest="frontier",
28  help="frontier connection string to use, defaults to frontier://FrontierProd/CMS_COND_21X_GLOBALTAG",
29  default="frontier://FrontierProd/CMS_COND_31X_GLOBALTAG")
30 
31 # Parse options and perform sanity checks
32 (options, args) = parser.parse_args()
33 #if options.run == None and options.hltkey == None and options.hltcff == None:
34 if options.hltkey == None and options.hltcff == None:
35  parser.error("I don't have all the required options.")
36  raise SystemExit(
37  "Please specify one of --hltkey (-k) or --hltcff (-s)")
38 if options.hltkey != None and options.hltcff != None:
39  raise SystemExit("Please only specify --hltkey (-k) or --hltcff (-c)")
40 # if options.run != None and options.hltkey != None:
41 # raise SystemExit("Please only specify --run (-r) or --hltkey (-k)")
42 # if options.run != None and options.hltcff != None:
43 # raise SystemExit("Please only specify --run (-r) or --hltcff (-c)")
44 if options.analysis == None:
45  raise SystemExit(
46  "Please specify an analysis configuration: -a or --analysis")
47 
48 # Checks the runtime environment is suitable
50  cwd = os.getcwd()
51  t = cwd.split("/")
52  if t[-1] != "python":
53  raise SystemExit("Must run from a Module/Package/python directory")
54 
55 # Converts an online HLT configuration into offline suitable format
56 def ConvertHltOnlineToOffline(config, frontierString):
57  # Replace the frontier string
58  onlineFrontier = re.search('"(frontier:.*)"', config)
59  if not onlineFrontier:
60  print("WARNING: Could not find Frontier string in HLT configuration. Will ignore.")
61  else:
62  config = config.replace(onlineFrontier.group(1), frontierString)
63 
64  # Replace the global tag
65  config = config.replace("H::All", "P::All")
66 
67  # print config -- debugging
68  # Remove unwanted PSets
69  config = RemovePSet(config, "MessageLogger")
70 # config = RemovePSet(config, "DQMStore")
71  config = RemovePSet(config, "DQM")
72  config = RemovePSet(config, "FUShmDQMOutputService")
73 
74  return config
75 
76 def RemovePSet(config, pset):
77  startLoc = config.find(pset)
78  started = False
79  count = 0
80  curLoc = startLoc
81  endLoc = 0
82 
83  # Find starting parenthesis
84  while not started:
85  if config[curLoc] == "(":
86  started = True
87  count = 1
88  curLoc += 1
89 
90  # Find end parenthesis
91  while endLoc == 0:
92  if config[curLoc] == "(":
93  count += 1
94  elif config[curLoc] == ")":
95  count -= 1
96  if count == 0:
97  endLoc = curLoc
98  curLoc += 1
99 
100  config = config.replace(config[startLoc:endLoc + 1], "")
101 
102  return config
103 
104 # Fetches the HLT configuration from ConfDB
105 def GetHltConfiguration(hltKey, frontierString):
106  # Format the config path for include
107  cwd = os.getcwd()
108  t = cwd.split("/")
109  module = t[-3]
110  package = t[-2]
111 
112  # Get the HLT config
113  config = os.popen2('wget "http://cms-project-confdb-hltdev.web.cern.ch/cms-project-confdb-hltdev/get.jsp?dbName=ORCOFF&configName=%s&cff=&nooutput=&format=Python" -O- -o /dev/null' % hltKey)[1].read()
114  #config = os.popen2("edmConfigFromDB --debug --configName %s --orcoff --format Python --nooutput --cff" % hltKey)[1].read()
115  configName = "JobHLTConfig_%s_cff.py" % jobHash
116 
117  # Perform online --> offline conversion
118  config = ConvertHltOnlineToOffline(config, frontierString)
119 
120  # Write the config
121  f = open(configName, "w")
122  f.write(config)
123  f.close()
124 
125  return 'process.load("%s.%s.%s")' % (module, package, configName.split(".")[0])
126 
127 # Fetches the HLT key from ConfDB - turned off in options for now
129  raise SystemExit("Not implemented yet")
130 
131 # Formats an HLT CFF path into a suitable python include statement
132 def FormatHltCff(cffPath):
133  pathParts = cffPath.split(".")
134  if not re.match("^[_A-Za-z0-9]*\.[_A-Za-z0-9]*\.[_A-Za-z0-9]*$", cffPath):
135  raise SystemExit("Expected cff in form Package.Module.configName_cff")
136  return 'process.load("%s")' % cffPath
137 
138 # Returns python code to compile an HLT configuration
139 def GetHltCompileCode(subsystem, package, hltConfig):
140  tmpCode = compileCode.replace("SUBSYSTEM", subsystem)
141  tmpCode = tmpCode.replace("PACKAGE", package)
142  tmpCode = tmpCode.replace("CONFIG", hltConfig + "c")
143  return tmpCode
144 
145 # Prepares the analysis configuration
146 def CreateAnalysisConfig(analysis, hltInclude):
147  anaName = "JobAnalysisConfig_%s_cfg.py" % jobHash
148  f = open(analysis)
149  g = open(anaName, "w")
150  g.write(f.read())
151  g.write("\n")
152  g.write(hltInclude)
153  g.close()
154  f.close()
155  return anaName
156 
157 # Quick sanity check of the environment
159 
160 # Get the required HLT config snippet
161 hltConfig = None
162 if options.hltkey != None:
163  hltConfig = GetHltConfiguration(options.hltkey, options.frontier)
164 elif options.hltcff != None:
165  hltConfig = FormatHltCff(options.hltcff)
166 else:
167  hltKey = GetHltKeyForRun(0)
168  hltConfig = GetHltConfiguration(hltKey)
169 
170 # Prepare the analysis configuration
171 anaConfig = CreateAnalysisConfig(options.analysis, hltConfig)
172 
173 if options.hltcff:
174  print("Using HLT configuration: %s" % hltConfig)
175 else:
176  print("Created HLT configuration: %s" % hltConfig)
177 print("Created analysis configuration: %s" % anaConfig)
178 
def ConvertHltOnlineToOffline(config, frontierString)
def GetHltConfiguration(hltKey, frontierString)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def CreateAnalysisConfig(analysis, hltInclude)
def GetHltCompileCode(subsystem, package, hltConfig)