CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
hltFindDuplicates.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import sys, imp, re, itertools
4 from HLTrigger.Configuration.Tools.frozendict import frozendict
5 import FWCore.ParameterSet.Config as cms
6 
7 debug = False
8 
9 whitelist_types = [
10  'HLTPrescaler',
11  'HLTBool',
12 ]
13 
14 whitelist_labels = [
15  'hltPreExpressSmart',
16  'hltPreEventDisplaySmart',
17 ]
18 
19 def whitelist(module):
20  return module.label in whitelist_labels or module.type in whitelist_types
21 
22 
23 def freeze(arg):
24  if type(arg) == dict:
25  return frozendict((k, freeze(v)) for (k, v) in arg.iteritems())
26  elif '__iter__' in dir(arg):
27  return tuple( freeze(v) for v in arg )
28  else:
29  return arg
30 
31 def unfreeze(arg):
32  if type(arg) == frozendict:
33  return dict((k, unfreeze(v)) for (k, v) in arg.iteritems())
34  elif '__iter__' in dir(arg):
35  return list( unfreeze(v) for v in arg )
36  else:
37  return arg
38 
39 def pythonize(arg):
40  if 'parameters_' in dir(arg):
41  arg = arg.parameters_()
42 
43  if 'value' in dir(arg):
44  arg = arg.value()
45 
46  if type(arg) == dict:
47  return frozendict((k, pythonize(v)) for (k, v) in arg.iteritems())
48  elif '__iter__' in dir(arg):
49  return tuple( pythonize(v) for v in arg )
50  else:
51  return arg
52 
53 
54 class Module(object):
55  type = ''
56  label = ''
57  params = frozendict()
58  hash = 0
59 
60  def __init__(self, module):
61  self.label = module.label_()
62  self.type = module.type_()
63  self.params = pythonize(module.parameters_())
64  self.__rehash()
65 
66 
67  def key(self):
68  return self.hash
69 
70  def __rehash(self):
71  self.hash = (hash(self.type) << 4) + hash(self.params)
72 
73  def __check(self, value, group):
74  return type(value) is str and bool(group.match(value))
75 
76  def __sub(self, value, group, label):
77  if type(value) is str:
78  return group.sub(r'%s\2' % label, value)
79  else:
80  return value
81 
82  def apply_rename(self, groups):
83  modified = False
84  newparams = unfreeze(self.params)
85  for label, (group, check) in groups.iteritems():
86  for k, p in newparams.iteritems():
87  if '__iter__' in dir(p):
88  if any(self.__check(v, check) for v in p):
89  newparams[k] = tuple(self.__sub(v, check, label) for v in p)
90  modified = True
91  else:
92  if self.__check(p, check):
93  newparams[k] = self.__sub(p, check, label)
94  modified = True
95 
96  if modified:
97  self.params = frozendict(newparams)
98  self.__rehash()
99 
100 
101 
102 
104  modules = []
105 
106  def append(self, module):
107  m = Module(module)
108  if not whitelist(m):
109  self.modules.append(m)
110 
111  def extend(self, modules):
112  for module in modules:
113  self.append(module)
114 
115  def __init__(self, *args):
116  for arg in args:
117  if '__iter__' in dir(arg):
118  self.extend(arg)
119  else:
120  self.append(arg)
121 
122  def sort(self):
123  self.modules.sort(key = Module.key)
124 
125  def group(self):
126  groups = dict()
127  self.sort()
128  i = 0
129  for v, g in itertools.groupby(self.modules, Module.key):
130  group = list(g)
131  if len(group) > 1:
132  i = i + 1
133  g = [ m.label for m in group ]
134  g.sort()
135  l = 'hltGroup%d' %i
136  r = re.compile(r'^(%s)($|:)' % r'|'.join(g))
137  groups[l] = (g, r)
138  return groups
139 
140  def apply_rename(self, groups):
141  for module in self.modules:
142  module.apply_rename(groups)
143 
144  def dump(self):
145  for m in self.modules:
146  print "%s = (%s) {" % (m.label, m.type)
147  for k, v in m.params.iteritems():
148  print "\t%s = %s" % (k, v)
149  print '}'
150  print
151 
152 
153 
154 def findDuplicates(process):
155  modules = ModuleList(
156  process._Process__analyzers.itervalues(),
157  process._Process__producers.itervalues(),
158  process._Process__filters.itervalues()
159  )
160 
161  oldups = 0
162  groups = modules.group()
163  dups = sum(len(g[0]) for g in groups.itervalues()) - len(groups)
164 
165  index = 1
166  while(dups != oldups):
167  if debug:
168  dump = open('step%d.sed' % index, 'w')
169  for target, (group, regexp) in groups.iteritems():
170  dump.write('s#\\<\\(%s\\)\\>#%s#\n' % ('\\|'.join(group), target))
171  dump.close()
172  print "found %d duplicates" % dups
173  oldups = dups
174  modules.apply_rename(groups)
175  groups = modules.group()
176  dups = sum(len(g[0]) for g in groups.itervalues()) - len(groups)
177  index = index + 1
178 
179  dump = open('groups.sed', 'w')
180  for target, (group, regexp) in groups.iteritems():
181  dump.write('s#\\<\\(%s\\)\\>#%s#\n' % ('\\|'.join(group), target))
182  dump.close()
183 
184  dump = open('groups.txt', 'w')
185  for target, (group, regexp) in groups.iteritems():
186  dump.write('#%s\n%s\n\n' % ( target, '\n'.join(group)))
187  dump.close()
188 
189 
190 
191 def main():
192  # parse the HLT configuration from standard input or from the given file
193  hlt = imp.new_module('hlt')
194  try:
195  configname = sys.argv[1]
196  except:
197  config = sys.stdin
198  else:
199  config = open(configname)
200  exec config in globals(), hlt.__dict__
201  config.close()
202  findDuplicates(hlt.process)
203 
204 
205 if __name__ == "__main__":
206  main()
207 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
list object
Definition: dbtoconf.py:77
dbl *** dir
Definition: mlp_gen.cc:35
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run