CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
Modules.SwitchProducer Class Reference
Inheritance diagram for Modules.SwitchProducer:
Modules.EDProducer Modules._Module Mixins._ConfigureComponent Mixins._TypedParameterizable Mixins._Labelable SequenceTypes._SequenceLeaf Mixins._Parameterizable SequenceTypes._Sequenceable Config.SwitchProducerTest Modules.SwitchProducerPickleable Modules.SwitchProducerTest Modules.SwitchProducerTest1Dis Modules.SwitchProducerTest2Dis

Public Member Functions

def __init__ (self, caseFunctionDict, **kargs)
 
def __setattr__ (self, name, value)
 
def appendToProcessDescLists_ (self, modules, aliases, myname)
 
def caseLabel_ (self, name, case)
 
def clone (self, **params)
 
def directDependencies (self)
 
def dumpPython (self, options=PrintOptions())
 
def insertInto (self, parameterSet, myname)
 
def moduleLabel_ (self, myname)
 
def nameInProcessDesc_ (self, myname)
 
- Public Member Functions inherited from Modules.EDProducer
def __init__ (self, type_, *arg, **kargs)
 
- Public Member Functions inherited from Modules._Module
def setPrerequisites (self, *libs)
 
- Public Member Functions inherited from Mixins._TypedParameterizable
def appendToProcessDescList_ (self, lst, myname)
 
def clone (self, *args, **params)
 
def copy (self)
 
def dumpConfig (self, options=PrintOptions())
 
def dumpPythonAttributes (self, myname, options)
 
def type_ (self)
 
- Public Member Functions inherited from Mixins._Parameterizable
def __delattr__ (self, name)
 
def __init__ (self, *arg, **kargs)
 
def __repr__ (self)
 
def getParameter (self, params)
 
def hasParameter (self, params)
 
def insertContentsInto (self, parameterSet)
 
def isFrozen (self)
 
def isModified (self)
 
def parameterNames_ (self)
 
def parameters_ (self)
 
def setIsFrozen (self)
 
- Public Member Functions inherited from Mixins._Labelable
def __str__ (self)
 
def dumpSequenceConfig (self)
 
def dumpSequencePython (self, options=PrintOptions())
 
def hasLabel_ (self)
 
def label (self)
 
def label_ (self)
 
def setLabel (self, label)
 
- Public Member Functions inherited from SequenceTypes._SequenceLeaf
def __init__ (self)
 
def isLeaf (self)
 
- Public Member Functions inherited from SequenceTypes._Sequenceable
def __add__ (self, rhs)
 
def __invert__ (self)
 
def __mul__ (self, rhs)
 
def isOperation (self)
 
def resolve (self, processDict, keepIfCannotResolve=False)
 
def visitNode (self, visitor)
 

Static Public Member Functions

def getCpu ()
 

Private Member Functions

def __addParameter (self, name, value)
 
def __setParameters (self, parameters)
 
def _chooseCase (self)
 
def _clonesequence (self, lookuptable)
 
def _errorstr (self)
 
def _getProducer (self)
 
def _placeImpl (self, name, proc)
 

Static Private Member Functions

def __typeIsValid (typ)
 

Private Attributes

 _caseFunctionDict
 
 _isModified
 

Detailed Description

This purpose class is to provide a switch of EDProducers for a single module/product label.

The decision is done at the time when the python configuration is
translated to C++. This class is generic, and intended to be
inherited for concrete switches. Example:

class SwitchProducerFoo(SwitchProducer):
    def __init__(self, **kargs):
        super(SwitchProducerFoo,self).__init__(
            dict(case1 = case1Func, case2 = case2Func),
            **kargs
        )

foo = SwitchProducerFoo(
    case1 = EDProducer("Producer1"),
    case2 = EDProducer("Producer2")
)

Here case1Func and case2Func are functions that return a (bool,
int) tuple, where the bool tells whether that case is enabled or
not, and the int tells the priority of that case. The case with
the highest priority among those that are enabled will get chosen.

The end result is that the product(s) labeled as "foo" will be
produced with one of the producers. It would be good if their
output product types and instance names would be the same (or very
close).

Definition at line 221 of file Modules.py.

Constructor & Destructor Documentation

◆ __init__()

def Modules.SwitchProducer.__init__ (   self,
  caseFunctionDict,
**  kargs 
)

Definition at line 250 of file Modules.py.

250  def __init__(self, caseFunctionDict, **kargs):
251  super(SwitchProducer,self).__init__(None)
252  self._caseFunctionDict = copy.copy(caseFunctionDict)
253  self.__setParameters(kargs)
254  self._isModified = False
255 

Member Function Documentation

◆ __addParameter()

def Modules.SwitchProducer.__addParameter (   self,
  name,
  value 
)
private

Reimplemented from Mixins._Parameterizable.

Definition at line 281 of file Modules.py.

281  def __addParameter(self, name, value):
282  if not self.__typeIsValid(value):
283  raise TypeError(name+" does not already exist, so it can only be set to a cms.EDProducer or cms.EDAlias")
284  if name not in self._caseFunctionDict:
285  raise ValueError("Case '%s' is not allowed (allowed ones are %s)" % (name, ",".join(six.iterkeys(self._caseFunctionDict))))
286  if name in self.__dict__:
287  message = "Duplicate insert of member " + name
288  message += "\nThe original parameters are:\n"
289  message += self.dumpPython() + '\n'
290  raise ValueError(message)
291  self.__dict__[name]=value
292  self._Parameterizable__parameterNames.append(name)
293  self._isModified = True
294 

References Modules.SwitchProducer.__typeIsValid(), Modules.SwitchProducer._caseFunctionDict, Mixins._ParameterTypeBase._isModified, Mixins._SimpleParameterTypeBase._isModified, Mixins._Parameterizable._isModified, mps_setup.append, Mixins._ParameterTypeBase.dumpPython(), Mixins.UsingBlock.dumpPython(), Mixins._Parameterizable.dumpPython(), Config.Process.dumpPython(), Config.SubProcess.dumpPython(), and join().

◆ __setattr__()

def Modules.SwitchProducer.__setattr__ (   self,
  name,
  value 
)

Reimplemented from Mixins._Parameterizable.

Definition at line 299 of file Modules.py.

299  def __setattr__(self, name, value):
300  # Following snippet copied and customized from
301  # _Parameterizable in order to support Modifier.toModify
302  #
303  #since labels are not supposed to have underscores at the beginning
304  # I will assume that if we have such then we are setting an internal variable
305  if self.isFrozen() and not (name in ["_Labelable__label","_isFrozen"] or name.startswith('_')):
306  message = "Object already added to a process. It is read only now\n"
307  message += " %s = %s" %(name, value)
308  message += "\nThe original parameters are:\n"
309  message += self.dumpPython() + '\n'
310  raise ValueError(message)
311  # underscored names bypass checking for _ParameterTypeBase
312  if name[0]=='_':
313  super(SwitchProducer, self).__setattr__(name,value)
314  elif not name in self.__dict__:
315  self.__addParameter(name, value)
316  self._isModified = True
317  else:
318  if not self.__typeIsValid(value):
319  raise TypeError(name+" can only be set to a cms.EDProducer or cms.EDAlias")
320  # We should always receive an cms.EDProducer
321  self.__dict__[name] = value
322  self._isModified = True
323 

References Mixins._Parameterizable.__addParameter(), Modules.SwitchProducer.__typeIsValid(), Mixins._ParameterTypeBase._isModified, Mixins._SimpleParameterTypeBase._isModified, Mixins._Parameterizable._isModified, Mixins._ParameterTypeBase.dumpPython(), Mixins.UsingBlock.dumpPython(), Mixins._Parameterizable.dumpPython(), Config.Process.dumpPython(), Config.SubProcess.dumpPython(), Mixins._ParameterTypeBase.isFrozen(), and Mixins._Parameterizable.isFrozen().

Referenced by GenObject.GenObject.setValue().

◆ __setParameters()

def Modules.SwitchProducer.__setParameters (   self,
  parameters 
)
private

Reimplemented from Mixins._Parameterizable.

Definition at line 295 of file Modules.py.

295  def __setParameters(self, parameters):
296  for name, value in six.iteritems(parameters):
297  self.__addParameter(name, value)
298 

References Mixins._Parameterizable.__addParameter().

◆ __typeIsValid()

def Modules.SwitchProducer.__typeIsValid (   typ)
staticprivate

Definition at line 278 of file Modules.py.

278  def __typeIsValid(typ):
279  return (isinstance(typ, EDProducer) and not isinstance(typ, SwitchProducer)) or isinstance(typ, EDAlias)
280 

Referenced by Modules.SwitchProducer.__addParameter(), and Modules.SwitchProducer.__setattr__().

◆ _chooseCase()

def Modules.SwitchProducer._chooseCase (   self)
private
Returns the name of the chosen case.

Definition at line 261 of file Modules.py.

261  def _chooseCase(self):
262  """Returns the name of the chosen case."""
263  cases = self.parameterNames_()
264  bestCase = None
265  for case in cases:
266  (enabled, priority) = self._caseFunctionDict[case]()
267  if enabled and (bestCase is None or bestCase[0] < priority):
268  bestCase = (priority, case)
269  if bestCase is None:
270  raise RuntimeError("All cases '%s' were disabled" % (str(cases)))
271  return bestCase[1]
272 

References Modules.SwitchProducer._caseFunctionDict, Mixins._Parameterizable.parameterNames_(), and str.

Referenced by Modules.SwitchProducer._getProducer(), and Modules.SwitchProducer.insertInto().

◆ _clonesequence()

def Modules.SwitchProducer._clonesequence (   self,
  lookuptable 
)
private

Reimplemented from Modules._Module.

Definition at line 409 of file Modules.py.

409  def _clonesequence(self, lookuptable):
410  try:
411  return lookuptable[id(self)]
412  except:
413  raise ModuleCloneError(self._errorstr())

References Modules._Module._errorstr(), and triggerObjects_cff.id.

◆ _errorstr()

def Modules.SwitchProducer._errorstr (   self)
private

Reimplemented from Modules._Module.

Definition at line 414 of file Modules.py.

414  def _errorstr(self):
415  return "SwitchProducer"
416 
417 

◆ _getProducer()

def Modules.SwitchProducer._getProducer (   self)
private
Returns the EDroducer of the chosen case

Definition at line 273 of file Modules.py.

273  def _getProducer(self):
274  """Returns the EDroducer of the chosen case"""
275  return self.__dict__[self._chooseCase()]
276 

References Modules.SwitchProducer._chooseCase().

◆ _placeImpl()

def Modules.SwitchProducer._placeImpl (   self,
  name,
  proc 
)
private

Reimplemented from Modules.EDProducer.

Definition at line 394 of file Modules.py.

394  def _placeImpl(self,name,proc):
395  proc._placeSwitchProducer(name,self)
396 # for case in self.parameterNames_():
397 # caseLabel = self.caseLabel_(name, case)
398 # caseObj = self.__dict__[case]
399 #
400 # if isinstance(caseObj, EDAlias):
401 # # EDAliases end up in @all_aliases automatically
402 # proc._placeAlias(caseLabel, caseObj)
403 # else:
404 # # Note that these don't end up in @all_modules
405 # # automatically because they're not part of any
406 # # Task/Sequence/Path
407 # proc._placeProducer(caseLabel, caseObj)
408 

Referenced by SequenceTypes._ModuleSequenceType._place(), and Mixins._TypedParameterizable._place().

◆ appendToProcessDescLists_()

def Modules.SwitchProducer.appendToProcessDescLists_ (   self,
  modules,
  aliases,
  myname 
)

Definition at line 372 of file Modules.py.

372  def appendToProcessDescLists_(self, modules, aliases, myname):
373  # This way we can insert the chosen EDProducer to @all_modules
374  # so that we get easily a worker for it
375  modules.append(myname)
376  for case in self.parameterNames_():
377  if isinstance(self.__dict__[case], EDAlias):
378  aliases.append(self.caseLabel_(myname, case))
379  else:
380  modules.append(self.caseLabel_(myname, case))
381 

References Modules.SwitchProducer.caseLabel_(), and Mixins._Parameterizable.parameterNames_().

◆ caseLabel_()

def Modules.SwitchProducer.caseLabel_ (   self,
  name,
  case 
)

Definition at line 370 of file Modules.py.

370  def caseLabel_(self, name, case):
371  return name+"@"+case

Referenced by Modules.SwitchProducer.appendToProcessDescLists_(), and Modules.SwitchProducer.insertInto().

◆ clone()

def Modules.SwitchProducer.clone (   self,
**  params 
)

Definition at line 324 of file Modules.py.

324  def clone(self, **params):
325  returnValue = SwitchProducer.__new__(type(self))
326 
327  # Need special treatment as cms.EDProducer is not a valid parameter type (except in this case)
328  myparams = dict()
329  for name, value in six.iteritems(params):
330  if value is None:
331  continue
332  elif isinstance(value, dict):
333  myparams[name] = self.__dict__[name].clone(**value)
334  else: # value is an EDProducer
335  myparams[name] = value.clone()
336 
337  # Add the ones that were not customized
338  for name in self.parameterNames_():
339  if name not in params:
340  myparams[name] = self.__dict__[name].clone()
341  returnValue.__init__(**myparams)
342  returnValue._isModified = False
343  returnValue._isFrozen = False
344  saveOrigin(returnValue, 1)
345  return returnValue
346 

References Mixins._Parameterizable.parameterNames_(), and Mixins.saveOrigin().

◆ directDependencies()

def Modules.SwitchProducer.directDependencies (   self)

Reimplemented from Mixins._TypedParameterizable.

Definition at line 362 of file Modules.py.

362  def directDependencies(self):
363  # XXX FIXME handle SwitchProducer dependencies
364  return []
365 

◆ dumpPython()

def Modules.SwitchProducer.dumpPython (   self,
  options = PrintOptions() 
)

Reimplemented from Mixins._TypedParameterizable.

Definition at line 347 of file Modules.py.

347  def dumpPython(self, options=PrintOptions()):
348  # Note that if anyone uses the generic SwitchProducer instead
349  # of a derived-one, the information on the functions for the
350  # producer decision is lost
351  specialImportRegistry.registerUse(self)
352  result = "%s(" % self.__class__.__name__ # not including cms. since the deriving classes are not in cms "namespace"
353  options.indent()
354  for resource in sorted(self.parameterNames_()):
355  result += "\n" + options.indentation() + resource + " = " + getattr(self, resource).dumpPython(options).rstrip() + ","
356  if result[-1] == ",":
357  result = result.rstrip(",")
358  options.unindent()
359  result += "\n)\n"
360  return result
361 

References __class__< T >.__class__(), and Mixins._Parameterizable.parameterNames_().

Referenced by Types._AllowedParameterTypes.__init__().

◆ getCpu()

def Modules.SwitchProducer.getCpu ( )
static
Returns a function that returns the priority for a CPU "computing device". Intended to be used by deriving classes.

Definition at line 257 of file Modules.py.

257  def getCpu():
258  """Returns a function that returns the priority for a CPU "computing device". Intended to be used by deriving classes."""
259  return _switch_cpu
260 

◆ insertInto()

def Modules.SwitchProducer.insertInto (   self,
  parameterSet,
  myname 
)

Reimplemented from Modules._Module.

Definition at line 382 of file Modules.py.

382  def insertInto(self, parameterSet, myname):
383  for case in self.parameterNames_():
384  producer = self.__dict__[case]
385  producer.insertInto(parameterSet, self.caseLabel_(myname, case))
386  newpset = parameterSet.newPSet()
387  newpset.addString(True, "@module_label", self.moduleLabel_(myname))
388  newpset.addString(True, "@module_type", "SwitchProducer")
389  newpset.addString(True, "@module_edm_type", "EDProducer")
390  newpset.addVString(True, "@all_cases", [myname+"@"+p for p in self.parameterNames_()])
391  newpset.addString(False, "@chosen_case", myname+"@"+self._chooseCase())
392  parameterSet.addPSet(True, self.nameInProcessDesc_(myname), newpset)
393 

References Modules.SwitchProducer._chooseCase(), Modules.SwitchProducer.caseLabel_(), edm::ModuleProcessName.moduleLabel_, FWJobMetadataManager::Data.moduleLabel_, PFMatchedCandidateRefExtractor.moduleLabel_, edm::ModuleLabelMatch.moduleLabel_, GBRForestWriter.moduleLabel_, edm::BranchKey.moduleLabel_, MuonMETcorrInputProducer.moduleLabel_, ShiftedJetProducerByMatchedObjectT< T >.moduleLabel_, SysShiftMETcorrInputProducer.moduleLabel_, Type0PFMETcorrInputProducer.moduleLabel_, NoPileUpPFMEtProducer.moduleLabel_, PFCandMETcorrInputProducer.moduleLabel_, PFchsMETcorrInputProducer.moduleLabel_, ShiftedPFCandidateProducerForPFMVAMEt.moduleLabel_, MultShiftMETcorrInputProducer.moduleLabel_, ShiftedParticleProducer.moduleLabel_, edm::ESTagGetter::Info.moduleLabel_, MultShiftMETcorrDBInputProducer.moduleLabel_, NoPileUpPFMEtDataProducer.moduleLabel_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >.moduleLabel_, ShiftedPFCandidateProducerForPFNoPUMEt.moduleLabel_, RecoTauGenericJetRegionProducer< JetType, CandType >.moduleLabel_, BoostedTauSeedsProducer.moduleLabel_, edm::ESConsumesInfoEntry.moduleLabel_, TauDiscriminationAgainstElectronDeadECAL< TauType, TauDiscriminator >.moduleLabel_, SubjetFilterAlgorithm.moduleLabel_, pat::TauJetCorrFactorsProducer.moduleLabel_, TauDiscriminationAgainstElectronMVA6< TauType, TauDiscriminator, ElectronType >.moduleLabel_, JetIdSelector< T >.moduleLabel_, edm::ModuleDescription.moduleLabel_, PFRecoTauChargedHadronProducer.moduleLabel_, edm::ProductSelectorRules::Rule.moduleLabel_, ObjectViewMatcher< T1, T2 >.moduleLabel_, MinMETProducerT< T >.moduleLabel_, PFRecoTauDiscriminationAgainstMuon2.moduleLabel_, TauTagValidation.moduleLabel_, ObjectViewCleaner< T >.moduleLabel_, PFRecoTauDiscriminationAgainstMuon2Container.moduleLabel_, PFRecoTauDiscriminationAgainstMuonSimple.moduleLabel_, ShiftedParticleProducerT< T >.moduleLabel_, CorrectedMETProducerT< T >.moduleLabel_, PFRecoTauDiscriminationAgainstMuonMVA.moduleLabel_, TauDiscriminationProducerBase< TauType, TauDiscriminator, TauDiscriminatorDataType, ConsumeType >.moduleLabel_, HLTMuonMatchAndPlot.moduleLabel_, PFRecoTauDiscriminationByIsolationMVA2.moduleLabel_, edm::test::TestProcessorConfig::ProduceEntry.moduleLabel_, VirtualJetProducer.moduleLabel_, reco::tau::PFRecoTauDiscriminationByMVAIsolationRun2.moduleLabel_, reco::tau::PATTauDiscriminationByMVAIsolationRun2.moduleLabel_, ShiftedJetProducerT< T, Textractor >.moduleLabel_, CaloJetMETcorrInputProducerT< T, Textractor >.moduleLabel_, PFRecoTauDiscriminationByIsolation.moduleLabel_, JetCleanerForType1METT< T, Textractor >.moduleLabel_, edm::PoolOutputModule.moduleLabel_, edm::BranchDescription.moduleLabel_, PFRecoTauDiscriminationByIsolationContainer.moduleLabel_, PFJetMETcorrInputProducerT< T, Textractor >.moduleLabel_, edm::ProductResolverIndexHelper::Item.moduleLabel_, Mixins._TypedParameterizable.moduleLabel_(), Mixins._TypedParameterizable.nameInProcessDesc_(), Config.SubProcess.nameInProcessDesc_(), and Mixins._Parameterizable.parameterNames_().

◆ moduleLabel_()

def Modules.SwitchProducer.moduleLabel_ (   self,
  myname 
)

Reimplemented from Mixins._TypedParameterizable.

Definition at line 368 of file Modules.py.

368  def moduleLabel_(self, myname):
369  return myname

Referenced by ExternalGeneratorFilter.ExternalGeneratorFilter.insertInto().

◆ nameInProcessDesc_()

def Modules.SwitchProducer.nameInProcessDesc_ (   self,
  myname 
)

Reimplemented from Mixins._TypedParameterizable.

Definition at line 366 of file Modules.py.

366  def nameInProcessDesc_(self, myname):
367  return myname

Referenced by Types.EDAlias.appendToProcessDescList_(), ExternalGeneratorFilter.ExternalGeneratorFilter.insertInto(), and Types.EDAlias.insertInto().

Member Data Documentation

◆ _caseFunctionDict

Modules.SwitchProducer._caseFunctionDict
private

◆ _isModified

Modules.SwitchProducer._isModified
private

Definition at line 254 of file Modules.py.

join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
clone
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
str
#define str(s)
Definition: TestProcessor.cc:52
mps_setup.append
append
Definition: mps_setup.py:85
Mixins.saveOrigin
def saveOrigin(obj, level)
Definition: Mixins.py:697
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
ConfigBuilder.dumpPython
def dumpPython(process, name)
Definition: ConfigBuilder.py:93