CMS 3D CMS Logo

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