CMS 3D CMS Logo

common.py
Go to the documentation of this file.
1 import itertools
2 import six
3 import FWCore.ParameterSet.Config as cms
4 
5 def producers_by_type(process, *types):
6  "Find all EDProducers in the Process that are instances of the given C++ type."
7  switches = (module for module in (getattr(switchproducer, case) \
8  for switchproducer in six.itervalues(process._Process__switchproducers) \
9  for case in switchproducer.parameterNames_()) \
10  if isinstance(module, cms.EDProducer))
11  return (module for module in itertools.chain(six.itervalues(process._Process__producers), switches) \
12  if module._TypedParameterizable__type in types)
13 
14 def filters_by_type(process, *types):
15  "Find all EDFilters in the Process that are instances of the given C++ type."
16  return (filter for filter in process._Process__filters.values() if filter._TypedParameterizable__type in types)
17 
18 def analyzers_by_type(process, *types):
19  "Find all EDAnalyzers in the Process that are instances of the given C++ type."
20  return (analyzer for analyzer in process._Process__analyzers.values() if analyzer._TypedParameterizable__type in types)
21 
22 def esproducers_by_type(process, *types):
23  "Find all ESProducers in the Process that are instances of the given C++ type."
24  return (module for module in process._Process__esproducers.values() if module._TypedParameterizable__type in types)
25 
26 def modules_by_type(process, *types):
27  "Find all modiles or other components in the Process that are instances of the given C++ type."
28  switches = (module for module in (getattr(switchproducer, case) \
29  for switchproducer in six.itervalues(process._Process__switchproducers) \
30  for case in switchproducer.parameterNames_()))
31  return (module for module in itertools.chain(six.itervalues(process.__dict__), switches) \
32  if hasattr(module, '_TypedParameterizable__type') and module._TypedParameterizable__type in types)
33 
34 
35 def insert_modules_before(process, target, *modules):
36  "Add the `modules` before the `target` in any Sequence, Paths or EndPath that contains the latter."
37  for sequence in itertools.chain(
38  six.itervalues(process._Process__sequences),
39  six.itervalues(process._Process__paths),
40  six.itervalues(process._Process__endpaths)
41  ):
42  try:
43  position = sequence.index(target)
44  except ValueError:
45  continue
46  else:
47  for module in reversed(modules):
48  sequence.insert(position, module)
49 
50 
51 def insert_modules_after(process, target, *modules):
52  "Add the `modules` after the `target` in any Sequence, Paths or EndPath that contains the latter."
53  for sequence in itertools.chain(
54  six.itervalues(process._Process__sequences),
55  six.itervalues(process._Process__paths),
56  six.itervalues(process._Process__endpaths)
57  ):
58  try:
59  position = sequence.index(target)
60  except ValueError:
61  continue
62  else:
63  for module in reversed(modules):
64  sequence.insert(position+1, module)
65 
66 
67 # logic from Modifier.toModify from FWCore/ParameterSet/python/Config.py
68 def replace_with(fromObj, toObj):
69  """Replace one object with a different one of the same type.
70 
71  This function replaces the contents of `fromObj` object with those of `toObj`,
72  so all references ot it remain valid.
73  """
74 
75  if not isinstance(toObj, type(fromObj)):
76  raise TypeError('replaceWith requires both arguments to be the same type')
77 
78  if isinstance(toObj, cms._ModuleSequenceType):
79  fromObj._seq = toObj._seq
80 
81  elif isinstance(toObj, cms._Parameterizable):
82  # delete the old items, in case `toObj` is not a complete superset of `fromObj`
83  for p in fromObj.parameterNames_():
84  delattr(fromObj, p)
85  for p in toObj.parameterNames_():
86  setattr(fromObj, p, getattr(toObj, p))
87  if isinstance(toObj, cms._TypedParameterizable):
88  fromObj._TypedParameterizable__type = toObj._TypedParameterizable__type
89 
90  else:
91  raise TypeError('replaceWith does not work with "%s" objects' % str(type(fromObj)))
92 
common.replace_with
def replace_with(fromObj, toObj)
Definition: common.py:68
str
#define str(s)
Definition: TestProcessor.cc:51
common.esproducers_by_type
def esproducers_by_type(process, *types)
Definition: common.py:22
common.modules_by_type
def modules_by_type(process, *types)
Definition: common.py:26
common.analyzers_by_type
def analyzers_by_type(process, *types)
Definition: common.py:18
common.insert_modules_after
def insert_modules_after(process, target, *modules)
Definition: common.py:51
common.filters_by_type
def filters_by_type(process, *types)
Definition: common.py:14
common.producers_by_type
def producers_by_type(process, *types)
Definition: common.py:5
common.insert_modules_before
def insert_modules_before(process, target, *modules)
Definition: common.py:35