CMS 3D CMS Logo

makeHLTPrescaleTable.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from __future__ import print_function
3 from sys import stderr, exit
4 import commands, os
5 
6 from optparse import OptionParser
7 parser = OptionParser(usage=
8 """
9 usage: %prog [options] csv_output_file
10 
11 examples:
12 
13  %prog out.csv
14 
15  produces a table of ALL runs and ALL paths (can take quite some time)
16 
17  %prog --path='*ele*' --path='*photon*' out.csv
18 
19  select only paths containing 'ele' and 'photon'
20 
21 """)
22 parser.add_option("--firstRun", dest="firstRun", help="first run", type="int", metavar="RUN", default="1")
23 parser.add_option("--lastRun", dest="lastRun", help="last run", type="int", metavar="RUN", default="9999999")
24 parser.add_option("--groupName", dest="groupName", help="select runs of name like NAME", metavar="NAME", default="Collisions%")
25 parser.add_option("--overwrite", dest="overwrite", help="force overwriting of output CSV file", action="store_true", default=False)
26 
27 parser.add_option("--path",
28  dest="pathPatterns",
29  default = [],
30  action="append",
31  metavar="PATTERN",
32  help="restrict paths to PATTERN. Note that this can be a single path name or a pattern " +
33  "using wildcards (*,?) similar to those used for selecting multiple files (see " +
34  "the documentation of the fnmatch module for details). Note also that this option " +
35  "can be specified more than once to select multiple paths or patterns. If this option " +
36  "is not specified, all paths are considered. Note that the comparison is done " +
37  "in a case-INsensitive manner. " +
38  "You may have to escape wildcards (with quotes or backslash) in order to avoid "+
39  "expansion by the shell"
40  )
41 
42 # parser.add_option("--jsonOut", dest="jsonOut", help="dump prescales in JSON format on FILE", metavar="FILE")
43 (options, args) = parser.parse_args()
44 if len(args) != 1:
45  parser.print_help()
46  exit(2)
47 
48 csv_output_file = args[0]
49 
50 if os.path.exists(csv_output_file) and not options.overwrite:
51  print("cowardly refusing to overwrite existing output file '" + csv_output_file + "'. Run this script without argument to see options for overriding this check.", file=stderr)
52  exit(1)
53 
54 #----------------------------------------------------------------------
56  """ returns a dict of hlt key to vector of prescales
57  mapping """
58 
59  retval = {}
60  for entry in process.PrescaleService.prescaleTable:
61  retval[entry.pathName.value()] = entry.prescales.value()
62 
63  return retval
64 
65 #----------------------------------------------------------------------
66 
68  # print >> stderr,"\t%s ..." % hlt_key
69  cmd = "edmConfigFromDB --orcoff --configName " + hlt_key
70  # print >> stderr, "cmd=",cmd
71  res = commands.getoutput(cmd)
72 
73  # potentially dangerous: we're running python code here
74  # which we get from an external process (confDB).
75  # we trust that there are no file deletion commands
76  # in the HLT configurations...
77 
78  # for some keys, edmConfigFromDB seems to produce error messages.
79  # just return None in this case
80  try:
81  exec(res)
82  except:
83  return None
84 
85  return process
86 
87 #----------------------------------------------------------------------
88 from queryRR import queryRR
89 
90 #----------------------------------------------------------------------
91 # main
92 #----------------------------------------------------------------------
93 
94 # check whether we have a CMSSW environment initalized
95 if os.system("which edmConfigFromDB") != 0:
96  print("could not find the command edmConfigFromDB. Did you initialize your CMSSW runtime environment ?", file=stderr)
97  exit(1)
98 
99 runKeys = queryRR(options.firstRun,options.lastRun,options.groupName)
100 
101 # maps from HLT key to prescale information.
102 # indexed as: prescaleTable[hlt_key][hlt_path_name]
103 prescaleTable = {}
104 
105 # maps from
106 hlt_path_names_table = {}
107 
108 # set of all HLT paths seen so far
109 all_hlt_path_names_seen = set()
110 
111 runs = sorted(runKeys.keys())
112 
113 # loop over all hlt keys found
114 
115 all_hlt_keys_seen = set(runKeys.values())
116 
117 print("found %d runs and %d HLT menus" % ( len(runKeys), len(all_hlt_keys_seen)), file=stderr)
118 
119 index = 1
120 
121 for hlt_key in all_hlt_keys_seen:
122 
123  print("(%3d/%3d) Querying ConfDB for HLT menu %s" % (index, len(all_hlt_keys_seen) , hlt_key), file=stderr)
124  process = getProcessObjectFromConfDB(hlt_key)
125 
126  if process == None:
127  print("WARNING: unable to retrieve hlt_key '" + hlt_key + "'", file=stderr)
128  continue
129 
130  prescaleTable[hlt_key] = getPrescaleTableFromProcessObject(process)
131 
132  all_path_names = set(process.paths.keys())
133 
134  # remember which hlt paths were in this menu
135  hlt_path_names_table[hlt_key] = all_path_names
136 
137  # add this configuration's HLT paths to the list
138  # of overall path names seen
139  all_hlt_path_names_seen.update(all_path_names)
140 
141  index += 1
142 
143 # end of loop over all HLT keys
144 
145 # make sure the list of all HLT path names ever seen is sorted
146 all_hlt_path_names_seen = sorted(all_hlt_path_names_seen)
147 
148 #--------------------
149 # filter paths if required by the user
150 if len(options.pathPatterns) > 0:
151  import fnmatch
152 
153  tmp = []
154 
155  for path in all_hlt_path_names_seen:
156 
157  for pattern in options.pathPatterns:
158  if fnmatch.fnmatch(path.lower(), pattern.lower()):
159 
160  # accept this path
161  tmp.append(path)
162  break
163 
164  all_hlt_path_names_seen = tmp
165 
166 # sanity check
167 
168 if len(all_hlt_path_names_seen) == 0:
169  print("no HLT paths found, exiting", file=stderr)
170  exit(1)
171 
172 #--------------------
173 # now that we know all path names of all runs, produce the CSV
174 import csv
175 
176 previous_hlt_key = None
177 
178 fout = open(csv_output_file,"w")
179 
180 csv_writer = csv.writer(fout,delimiter=";")
181 
182 csv_writer.writerow(['Table of HLT prescale factors'])
183 csv_writer.writerow([])
184 csv_writer.writerow(['Explanation:'])
185 csv_writer.writerow(['number(s) = prescale factor(s), HLT path present in this menu'])
186 csv_writer.writerow(['empty = HLT path NOT present in this menu'])
187 csv_writer.writerow(['0 = HLT path present in this menu but prescale factor is zero'])
188 csv_writer.writerow(['U = could not retrieve menu for this HLT key from confDB'])
189 csv_writer.writerow([])
190 csv_writer.writerow([])
191 
192 # write the header line
193 column_names = [ 'run','' ]
194 column_names.extend(all_hlt_path_names_seen)
195 csv_writer.writerow(column_names)
196 
197 csv_writer.writerow([])
198 
199 for run in runs:
200  hlt_key = runKeys[run]
201 
202  if hlt_key == previous_hlt_key:
203  # the hlt key is the same as for the previous run
204  # just reuse the existing contents of the variable 'values'
205  # (apart from the run number)
206  values[0] = run
207  csv_writer.writerow(values)
208  continue
209 
210  # HLT key has changed
211 
212  # insert a line with the menu's name
213  #
214  # check whether we actually could determine the menu
215  if hlt_key not in hlt_path_names_table:
216  # could previously not retrieve the python
217  # configuration for this key
218  #
219  # put some warnings in the output table
220 
221  csv_writer.writerow([hlt_key, "COULD NOT RETRIEVE MENU FROM CONFDB"])
222 
223  values = [ run , '' ]
224  values.extend(len(all_hlt_path_names_seen) * [ "U" ])
225 
226  csv_writer.writerow(values)
227 
228  previous_hlt_key = hlt_key
229  continue
230 
231  # everything seems ok for this key
232 
233  csv_writer.writerow([hlt_key])
234 
235  # now put together the list of prescales
236  values = [ run , '' ]
237 
238  # find out which HLT keys were present and which prescale factors
239  # they had
240  for hlt_path in all_hlt_path_names_seen:
241 
242  if hlt_path in hlt_path_names_table[hlt_key]:
243  # this HLT path was present in this menu
244  # check whether there was an entry in the prescale
245  # table
246 
247  # get the prescale factors (list) or just a list with 1
248  # if this path was not present in the prescale table
249  # for this menu
250  prescales = prescaleTable[hlt_key].get(hlt_path, [ 1 ] )
251 
252  # merge the values to one comma separated string
253  # print "prescales=",prescales
254  values.append(",".join([str(x) for x in prescales]))
255 
256  else:
257  # path not found for this hlt key. append
258  # an empty string for this column
259  values.append('')
260 
261  # end loop over hlt paths
262 
263  csv_writer.writerow(values)
264 
265  previous_hlt_key = hlt_key
266 
267 # end loop over runs
268 
269 fout.close()
270 
271 print("created CSV file",csv_output_file,". Field delimiter is semicolon.", file=stderr)
272 
def getProcessObjectFromConfDB(hlt_key)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def getPrescaleTableFromProcessObject(process)
#define str(s)
T get(const Candidate &c)
Definition: component.h:55