test
CMS 3D CMS Logo

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