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