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 
5 def producers_by_type(process, *types):
6  "Find all EDProducers in the Process that are instances of the given C++ type."
7  return (module for module in process._Process__producers.values() if module._TypedParameterizable__type in types)
8 
9 def filters_by_type(process, *types):
10  "Find all EDFilters in the Process that are instances of the given C++ type."
11  return (filter for filter in process._Process__filters.values() if filter._TypedParameterizable__type in types)
12 
13 def analyzers_by_type(process, *types):
14  "Find all EDAnalyzers in the Process that are instances of the given C++ type."
15  return (analyzer for analyzer in process._Process__analyzers.values() if analyzer._TypedParameterizable__type in types)
16 
17 def esproducers_by_type(process, *types):
18  "Find all ESProducers in the Process that are instances of the given C++ type."
19  return (module for module in process._Process__esproducers.values() if module._TypedParameterizable__type in types)
20 
21 
22 def insert_modules_before(process, target, *modules):
23  "Add the `modules` before the `target` in any Sequence, Paths or EndPath that contains the latter."
24  for sequence in itertools.chain(
25  process._Process__sequences.itervalues(),
26  process._Process__paths.itervalues(),
27  process._Process__endpaths.itervalues()
28  ):
29  try:
30  position = sequence.index(target)
31  except ValueError:
32  continue
33  else:
34  for module in reversed(modules):
35  sequence.insert(position, module)
36 
37 
38 # logic from Modifier.toModify from FWCore/ParameterSet/python/Config.py
39 def replace_with(fromObj, toObj):
40  """Replace one object with a different one of the same type.
41 
42  This function replaces the contents of `fromObj` object with those of `toObj`,
43  so all references ot it remain valid.
44  """
45 
46  if type(toObj) != type(fromObj):
47  raise TypeError('replaceWith requires both arguments to be the same type')
48 
49  if isinstance(toObj, cms._ModuleSequenceType):
50  fromObj._seq = toObj._seq
51 
52  elif isinstance(toObj, cms._Parameterizable):
53  # delete the old items, in case `toObj` is not a complete superset of `fromObj`
54  for p in fromObj.parameterNames_():
55  delattr(fromObj, p)
56  for p in toObj.parameterNames_():
57  setattr(fromObj, p, getattr(toObj, p))
58  if isinstance(toObj, cms._TypedParameterizable):
59  fromObj._TypedParameterizable__type = toObj._TypedParameterizable__type
60 
61  else:
62  raise TypeError('replaceWith does not work with "%s" objects' % str(type(fromObj)))
63 
def replace_with(fromObj, toObj)
Definition: common.py:39
def filters_by_type(process, types)
Definition: common.py:9
def insert_modules_before(process, target, modules)
Definition: common.py:22
def producers_by_type(process, types)
Definition: common.py:5
def analyzers_by_type(process, types)
Definition: common.py:13
def esproducers_by_type(process, types)
Definition: common.py:17