CMS 3D CMS Logo

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