CMS 3D CMS Logo

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