File indexing completed on 2024-04-06 12:15:59
0001 import itertools
0002 import FWCore.ParameterSet.Config as cms
0003
0004 def producers_by_type(process, *types):
0005 "Find all EDProducers in the Process that are instances of the given C++ type."
0006 switches = (module for module in (getattr(switchproducer, case) \
0007 for switchproducer in process._Process__switchproducers.values() \
0008 for case in switchproducer.parameterNames_()) \
0009 if isinstance(module, cms.EDProducer))
0010 return (module for module in itertools.chain(process._Process__producers.values(), switches) \
0011 if module._TypedParameterizable__type in types)
0012
0013 def filters_by_type(process, *types):
0014 "Find all EDFilters in the Process that are instances of the given C++ type."
0015 return (filter for filter in process._Process__filters.values() if filter._TypedParameterizable__type in types)
0016
0017 def analyzers_by_type(process, *types):
0018 "Find all EDAnalyzers in the Process that are instances of the given C++ type."
0019 return (analyzer for analyzer in process._Process__analyzers.values() if analyzer._TypedParameterizable__type in types)
0020
0021 def esproducers_by_type(process, *types):
0022 "Find all ESProducers in the Process that are instances of the given C++ type."
0023 return (module for module in process._Process__esproducers.values() if module._TypedParameterizable__type in types)
0024
0025 def modules_by_type(process, *types):
0026 "Find all modiles or other components in the Process that are instances of the given C++ type."
0027 switches = (module for module in (getattr(switchproducer, case) \
0028 for switchproducer in process._Process__switchproducers.values() \
0029 for case in switchproducer.parameterNames_()))
0030 return (module for module in itertools.chain(process.__dict__.values(), switches) \
0031 if hasattr(module, '_TypedParameterizable__type') and module._TypedParameterizable__type in types)
0032
0033
0034 def insert_modules_before(process, target, *modules):
0035 "Add the `modules` before the `target` in any Sequence, Paths or EndPath that contains the latter."
0036 for sequence in itertools.chain(
0037 process._Process__sequences.values(),
0038 process._Process__paths.values(),
0039 process._Process__endpaths.values()
0040 ):
0041 try:
0042 position = sequence.index(target)
0043 except ValueError:
0044 continue
0045 else:
0046 for module in reversed(modules):
0047 sequence.insert(position, module)
0048
0049
0050 def insert_modules_after(process, target, *modules):
0051 "Add the `modules` after the `target` in any Sequence, Paths or EndPath that contains the latter."
0052 for sequence in itertools.chain(
0053 process._Process__sequences.values(),
0054 process._Process__paths.values(),
0055 process._Process__endpaths.values()
0056 ):
0057 try:
0058 position = sequence.index(target)
0059 except ValueError:
0060 continue
0061 else:
0062 for module in reversed(modules):
0063 sequence.insert(position+1, module)
0064
0065
0066
0067 def replace_with(fromObj, toObj):
0068 """Replace one object with a different one of the same type.
0069
0070 This function replaces the contents of `fromObj` object with those of `toObj`,
0071 so all references ot it remain valid.
0072 """
0073
0074 if not isinstance(toObj, type(fromObj)):
0075 raise TypeError('replaceWith requires both arguments to be the same type')
0076
0077 if isinstance(toObj, cms._ModuleSequenceType):
0078 fromObj._seq = toObj._seq
0079
0080 elif isinstance(toObj, cms._Parameterizable):
0081
0082 for p in fromObj.parameterNames_():
0083 delattr(fromObj, p)
0084 for p in toObj.parameterNames_():
0085 setattr(fromObj, p, getattr(toObj, p))
0086 if isinstance(toObj, cms._TypedParameterizable):
0087 fromObj._TypedParameterizable__type = toObj._TypedParameterizable__type
0088
0089 else:
0090 raise TypeError('replaceWith does not work with "%s" objects' % str(type(fromObj)))
0091
0092
0093
0094 def set_prescale(process, path, prescale):
0095 columns = len(process.PrescaleService.lvl1Labels.value())
0096 prescales = cms.vuint32([prescale] * columns)
0097
0098 for pset in process.PrescaleService.prescaleTable:
0099 if pset.pathName.value() == path:
0100 pset.prescales = prescales
0101 break
0102 else:
0103 process.PrescaleService.prescaleTable.append(cms.PSet(
0104 pathName = cms.string(path),
0105 prescales = prescales
0106 ))
0107