CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 Modules.SwitchProducerPickleable Modules.SwitchProducerTest

Public Member Functions

def __init__
 
def __setattr__
 
def appendToProcessDescLists_
 
def caseLabel_
 
def clone
 
def directDependencies
 
def dumpPython
 
def insertInto
 
def moduleLabel_
 
def nameInProcessDesc_
 
- Public Member Functions inherited from Modules.EDProducer
def __init__
 
- Public Member Functions inherited from Modules._Module
def __init__
 
def insertInto
 
def setPrerequisites
 

Static Public Member Functions

def getCpu
 

Private Member Functions

def __addParameter
 
def __setParameters
 
def _chooseCase
 
def _clonesequence
 
def _errorstr
 
def _getProducer
 
def _placeImpl
 

Static Private Member Functions

def __typeIsValid
 

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 220 of file Modules.py.

Constructor & Destructor Documentation

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

Definition at line 249 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

Member Function Documentation

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

Definition at line 280 of file Modules.py.

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

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

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(self._caseFunctionDict.keys())))
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
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def Modules.SwitchProducer.__setattr__ (   self,
  name,
  value 
)

Definition at line 298 of file Modules.py.

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

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
def Modules.SwitchProducer.__setParameters (   self,
  parameters 
)
private

Definition at line 294 of file Modules.py.

References Mixins._Parameterizable.__addParameter(), and Modules.SwitchProducer.__addParameter().

295  def __setParameters(self, parameters):
296  for name, value in parameters.items():
297  self.__addParameter(name, value)
def Modules.SwitchProducer.__typeIsValid (   typ)
staticprivate

Definition at line 277 of file Modules.py.

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

278  def __typeIsValid(typ):
279  return (isinstance(typ, EDProducer) and not isinstance(typ, SwitchProducer)) or isinstance(typ, EDAlias)
def Modules.SwitchProducer._chooseCase (   self,
  accelerators 
)
private
Returns the name of the chosen case.

Definition at line 260 of file Modules.py.

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

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

261  def _chooseCase(self, accelerators):
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](accelerators)
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]
#define str(s)
def Modules.SwitchProducer._clonesequence (   self,
  lookuptable 
)
private

Definition at line 408 of file Modules.py.

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

409  def _clonesequence(self, lookuptable):
410  try:
411  return lookuptable[id(self)]
412  except:
raise ModuleCloneError(self._errorstr())
uint16_t *__restrict__ id
def Modules.SwitchProducer._errorstr (   self)
private

Definition at line 413 of file Modules.py.

414  def _errorstr(self):
415  return "SwitchProducer"
416 
def Modules.SwitchProducer._getProducer (   self,
  accelerators 
)
private
Returns the EDroducer of the chosen case

Definition at line 272 of file Modules.py.

References Modules.SwitchProducer._chooseCase().

273  def _getProducer(self, accelerators):
274  """Returns the EDroducer of the chosen case"""
275  return self.__dict__[self._chooseCase(accelerators)]
def Modules.SwitchProducer._placeImpl (   self,
  name,
  proc 
)
private

Definition at line 393 of file Modules.py.

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

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)
def Modules.SwitchProducer.appendToProcessDescLists_ (   self,
  modules,
  aliases,
  myname 
)

Definition at line 371 of file Modules.py.

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

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))
def Modules.SwitchProducer.caseLabel_ (   self,
  name,
  case 
)

Definition at line 369 of file Modules.py.

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

370  def caseLabel_(self, name, case):
return name+"@"+case
def Modules.SwitchProducer.clone (   self,
  params 
)

Definition at line 323 of file Modules.py.

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

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 params.items():
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
def saveOrigin
Definition: Mixins.py:701
def Modules.SwitchProducer.directDependencies (   self)

Definition at line 361 of file Modules.py.

362  def directDependencies(self):
363  # XXX FIXME handle SwitchProducer dependencies
364  return []
def Modules.SwitchProducer.dumpPython (   self,
  options = PrintOptions() 
)

Definition at line 346 of file Modules.py.

References Mixins._Parameterizable.parameterNames_().

Referenced by Modules.SwitchProducer.__addParameter(), Types._AllowedParameterTypes.__init__(), Types.VPSet.__repr__(), Modules.SwitchProducer.__setattr__(), and SequenceTypes.Schedule.__str__().

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
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 256 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
def Modules.SwitchProducer.insertInto (   self,
  parameterSet,
  myname,
  accelerators 
)

Definition at line 381 of file Modules.py.

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

382  def insertInto(self, parameterSet, myname, accelerators):
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(accelerators))
392  parameterSet.addPSet(True, self.nameInProcessDesc_(myname), newpset)
def Modules.SwitchProducer.moduleLabel_ (   self,
  myname 
)

Definition at line 367 of file Modules.py.

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

368  def moduleLabel_(self, myname):
return myname
def Modules.SwitchProducer.nameInProcessDesc_ (   self,
  myname 
)

Definition at line 365 of file Modules.py.

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

366  def nameInProcessDesc_(self, myname):
return myname

Member Data Documentation

Modules.SwitchProducer._caseFunctionDict
private

Definition at line 251 of file Modules.py.

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

Modules.SwitchProducer._isModified
private

Definition at line 253 of file Modules.py.

Referenced by Modules.SwitchProducer.__addParameter(), Modules.SwitchProducer.__setattr__(), Types.InputTag.setValue(), and Types.ESInputTag.setValue().