CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Functions
helpers Namespace Reference

Classes

class  CloneSequenceVisitor
 
struct  CompositeCandidateMaker
 
struct  CompositePtrCandidateMaker
 
class  GatherAllModulesVisitor
 
class  MassSearchParamVisitor
 
class  MassSearchReplaceAnyInputTagVisitor
 
class  MassSearchReplaceParamVisitor
 
struct  MCTruthPairSelector
 
struct  NamedCompositeCandidateMaker
 
class  TestModuleCommand
 

Functions

def __labelsInSequence
 
def addESProducers
 Helpers to perform some technically boring tasks like looking for all modules with a given parameter and replacing that to a given value. More...
 
def addKeepStatement
 
def applyPostfix
 
def cloneProcessingSnippet
 
def contains
 
def extendWithPrePostfix
 
def get_path
 
def get_vars
 
def jetCollectionString
 
def listDependencyChain
 
def listModules
 
def listSequences
 
def loadWithPostfix
 
def loadWithPrefix
 
def loadWithPrePostfix
 
def massSearchReplaceAnyInputTag
 
def massSearchReplaceParam
 
def removeIfInSequence
 

Function Documentation

def helpers.__labelsInSequence (   process,
  sequenceLabel,
  postfix = "" 
)
private

Definition at line 91 of file helpers.py.

References listModules(), and listSequences().

Referenced by removeIfInSequence().

91 
92 def __labelsInSequence(process, sequenceLabel, postfix=""):
93  result = [ m.label()[:-len(postfix)] for m in listModules( getattr(process,sequenceLabel+postfix))]
94  result.extend([ m.label()[:-len(postfix)] for m in listSequences( getattr(process,sequenceLabel+postfix))] )
95  if postfix == "":
96  result = [ m.label() for m in listModules( getattr(process,sequenceLabel+postfix))]
97  result.extend([ m.label() for m in listSequences( getattr(process,sequenceLabel+postfix))] )
98  return result
def listModules
Definition: helpers.py:261
def listSequences
Definition: helpers.py:266
def __labelsInSequence
Definition: helpers.py:91
def helpers.addESProducers (   process,
  config 
)

Helpers to perform some technically boring tasks like looking for all modules with a given parameter and replacing that to a given value.

Definition at line 7 of file helpers.py.

References dir.

7 
8 def addESProducers(process,config):
9  config = config.replace("/",".")
10  #import RecoBTag.Configuration.RecoBTag_cff as btag
11  #print btag
12  module = __import__(config)
13  for name in dir(sys.modules[config]):
14  item = getattr(sys.modules[config],name)
15  if isinstance(item,cms._Labelable) and not isinstance(item,cms._ModuleSequenceType) and not name.startswith('_') and not (name == "source" or name == "looper" or name == "subProcess") and not type(item) is cms.PSet:
16  if 'ESProducer' in item.type_():
17  setattr(process,name,item)
def addESProducers
Helpers to perform some technically boring tasks like looking for all modules with a given parameter ...
Definition: helpers.py:7
dbl *** dir
Definition: mlp_gen.cc:35
def helpers.addKeepStatement (   process,
  oldKeep,
  newKeeps,
  verbose = False 
)
Add new keep statements to any PoolOutputModule of the process that has the old keep statements

Definition at line 427 of file helpers.py.

Referenced by customizeMiniAOD_MuEGFixMoriond2017.addDiscardedPFCandidates(), extraSlimmedMETs_MuEGFixMoriond2017.addExtraMETCollections(), extraSlimmedMETs_MuEGFixMoriond2017.addExtraPuppiMETCorrections(), extraJets_MuEGFixMoriond2017.backupJetsSecondStep(), customizeMiniAOD_MuEGFixMoriond2017.customizeAll(), and egammaGainSwitchFixToolsForPAT_cff.customizeGSFixForPAT().

428 def addKeepStatement(process, oldKeep, newKeeps, verbose=False):
429  """Add new keep statements to any PoolOutputModule of the process that has the old keep statements"""
430  for name,out in process.outputModules.iteritems():
431  if out.type_() == 'PoolOutputModule' and hasattr(out, "outputCommands"):
432  if oldKeep in out.outputCommands:
433  out.outputCommands += newKeeps
434  if verbose:
435  print "Adding the following keep statements to output module %s: " % name
436  for k in newKeeps: print "\t'%s'," % k
437 
438 
def addKeepStatement
Definition: helpers.py:427
def helpers.applyPostfix (   process,
  label,
  postfix 
)

Definition at line 76 of file helpers.py.

Referenced by pfTools.adaptPFMuons(), pfTools.addPFCandidates(), pfTools.reconfigurePF2PATTaus(), pfTools.switchToPFJets(), pfTools.switchToPFMET(), pfTools.tauTypeInPF2PAT(), HiCoreTools.RemoveMCMatching.toolCode(), HiCoreTools.RemoveSpecificPATObjects.toolCode(), HiCoreTools.RemoveCleaning.toolCode(), and pfTools.usePF2PAT().

76 
77 def applyPostfix(process, label, postfix):
78  result = None
79  if hasattr(process, label+postfix):
80  result = getattr(process, label + postfix)
81  else:
82  raise ValueError("Error in <applyPostfix>: No module of name = %s attached to process !!" % (label + postfix))
83  return result
def applyPostfix
Definition: helpers.py:76
def helpers.cloneProcessingSnippet (   process,
  sequence,
  postfix,
  removePostfix = "",
  noClones = [] 
)
------------------------------------------------------------------
copy a sequence plus the modules and sequences therein
both are renamed by getting a postfix
input tags are automatically adjusted
------------------------------------------------------------------

Definition at line 315 of file helpers.py.

Referenced by boostedHPSPFTaus_cfi.addBoostedTaus(), extraSlimmedMETs_MuEGFixMoriond2017.addExtraMETCollections(), extraJets_MuEGFixMoriond2017.backupJetsFirstStep(), extraJets_MuEGFixMoriond2017.makeRecoJetCollection(), pfIsolation.setupPFIso(), and pfIsolation.setupPFIsoPhoton().

316 def cloneProcessingSnippet(process, sequence, postfix, removePostfix="", noClones = []):
317  """
318  ------------------------------------------------------------------
319  copy a sequence plus the modules and sequences therein
320  both are renamed by getting a postfix
321  input tags are automatically adjusted
322  ------------------------------------------------------------------
323  """
324  result = sequence
325  if not postfix == "":
326  visitor = CloneSequenceVisitor(process, sequence.label(), postfix, removePostfix, noClones)
327  sequence.visit(visitor)
328  result = visitor.clonedSequence()
329  return result
def cloneProcessingSnippet
Definition: helpers.py:315
def helpers.contains (   sequence,
  moduleName 
)
------------------------------------------------------------------
return True if a module with name 'module' is contained in the
sequence with name 'sequence' and False otherwise. This version
is not so nice as it also returns True for any substr of the name
of a contained module.

sequence : sequence [e.g. process.patDefaultSequence]
module   : module name as a string
------------------------------------------------------------------

Definition at line 299 of file helpers.py.

References spr.find().

Referenced by helpers.TestModuleCommand.testContains().

300 def contains(sequence, moduleName):
301  """
302  ------------------------------------------------------------------
303  return True if a module with name 'module' is contained in the
304  sequence with name 'sequence' and False otherwise. This version
305  is not so nice as it also returns True for any substr of the name
306  of a contained module.
307 
308  sequence : sequence [e.g. process.patDefaultSequence]
309  module : module name as a string
310  ------------------------------------------------------------------
311  """
312  return not sequence.__str__().find(moduleName)==-1
313 
314 
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
def contains
Definition: helpers.py:299
def helpers.extendWithPrePostfix (   process,
  other,
  prefix,
  postfix,
  items = () 
)
Look in other and find types which we can use

Definition at line 30 of file helpers.py.

References cmsPerfStripChart.dict, dir, and massSearchReplaceAnyInputTag().

Referenced by loadWithPrePostfix().

30 
31 def extendWithPrePostfix(process,other,prefix,postfix,items=()):
32  """Look in other and find types which we can use"""
33  # enable explicit check to avoid overwriting of existing objects
34  #__dict__['_Process__InExtendCall'] = True
35 
36  seqs = dict()
37  sequence = cms.Sequence()
38  sequence._moduleLabels = []
39  sequence.setLabel('tempSequence')
40  for name in dir(other):
41  #'from XX import *' ignores these, and so should we.
42  if name.startswith('_'):
43  continue
44  item = getattr(other,name)
45  if name == "source" or name == "looper" or name == "subProcess":
46  continue
47  elif isinstance(item,cms._ModuleSequenceType):
48  continue
49  elif isinstance(item,cms.Schedule):
50  continue
51  elif isinstance(item,cms.VPSet) or isinstance(item,cms.PSet):
52  continue
53  elif isinstance(item,cms._Labelable):
54  if not item.hasLabel_():
55  item.setLabel(name)
56  if prefix != '' or postfix != '':
57  newModule = item.clone()
58  if isinstance(item,cms.ESProducer):
59  newLabel = item.label()
60  newName =name
61  else:
62  if 'TauDiscrimination' in name:
63  process.__setattr__(name,item)
64  newLabel = prefix+item.label()+postfix
65  newName = prefix+name+postfix
66  process.__setattr__(newName,newModule)
67  if isinstance(newModule, cms._Sequenceable) and not newName == name:
68  sequence +=getattr(process,newName)
69  sequence._moduleLabels.append(item.label())
70  else:
71  process.__setattr__(name,item)
72 
73  if prefix != '' or postfix != '':
74  for label in sequence._moduleLabels:
75  massSearchReplaceAnyInputTag(sequence, label, prefix+label+postfix,verbose=False,moduleLabelOnly=True)
def extendWithPrePostfix
Definition: helpers.py:30
def massSearchReplaceAnyInputTag
Definition: helpers.py:271
dbl *** dir
Definition: mlp_gen.cc:35
def helpers.get_path (   file_in_path)

Definition at line 6 of file helpers.py.

Referenced by get_vars().

6 
7 def get_path(file_in_path):
8  'mimics edm.FileInPath behavior'
9  search_env = os.environ.get('CMSSW_SEARCH_PATH', '')
10  if not search_env:
11  raise RuntimeError('The environmental variable CMSSW_SEARCH_PATH must be set')
12  search_paths = search_env.split(':')
13  for spath in search_paths:
14  full_path = os.path.join(spath, file_in_path)
15  if os.path.isfile(full_path):
16  return full_path
17  raise RuntimeError('No suitable path found for %s' % file_in_path)
def get_path
Definition: helpers.py:6
def helpers.get_vars (   xml_path,
  useFileInPath = True 
)

Definition at line 18 of file helpers.py.

References get_path(), and trainingvars.get_var_pset().

18 
19 def get_vars(xml_path, useFileInPath=True):
20  full_path = get_path(xml_path) if useFileInPath else xml_path
21  xml_tree = ET.parse(full_path)
22  root = xml_tree.getroot()
23  variables = None
24  for i in root:
25  if i.tag == 'Variables':
26  variables = i
27 
28  if i is None:
29  raise RuntimeError('Could not find Variables inside the xml weights')
30 
31  var_names = [i.attrib['Title'] for i in variables]
32  return [get_var_pset(i) for i in var_names]
33 
def get_path
Definition: helpers.py:6
def get_vars
Definition: helpers.py:18
def helpers.jetCollectionString (   prefix = '',
  algo = '',
  type = '' 
)
------------------------------------------------------------------
return the string of the jet collection module depending on the
input vaules. The default return value will be 'patAK5CaloJets'.

algo   : indicating the algorithm type of the jet [expected are
         'AK5', 'IC5', 'SC7', ...]
type   : indicating the type of constituents of the jet [expec-
         ted are 'Calo', 'PFlow', 'JPT', ...]
prefix : prefix indicating the type of pat collection module (ex-
         pected are '', 'selected', 'clean').
------------------------------------------------------------------

Definition at line 275 of file helpers.py.

References reco.if().

Referenced by helpers.TestModuleCommand.testJetCollectionString(), heavyIonTools.ProductionDefaults.toolCode(), HiCoreTools.RemoveMCMatching.toolCode(), coreTools.RemoveMCMatching.toolCode(), heavyIonTools.SelectionDefaults.toolCode(), and HiCoreTools.RemoveSpecificPATObjects.toolCode().

276 def jetCollectionString(prefix='', algo='', type=''):
277  """
278  ------------------------------------------------------------------
279  return the string of the jet collection module depending on the
280  input vaules. The default return value will be 'patAK5CaloJets'.
281 
282  algo : indicating the algorithm type of the jet [expected are
283  'AK5', 'IC5', 'SC7', ...]
284  type : indicating the type of constituents of the jet [expec-
285  ted are 'Calo', 'PFlow', 'JPT', ...]
286  prefix : prefix indicating the type of pat collection module (ex-
287  pected are '', 'selected', 'clean').
288  ------------------------------------------------------------------
289  """
290  if(prefix==''):
291  jetCollectionString ='pat'
292  else:
293  jetCollectionString =prefix
294  jetCollectionString+='Pat'
295  jetCollectionString+='Jets'
296  jetCollectionString+=algo
297  jetCollectionString+=type
298  return jetCollectionString
def jetCollectionString
Definition: helpers.py:275
if(dp >Float(M_PI)) dp-
def helpers.listDependencyChain (   process,
  module,
  sources,
  verbose = False 
)
Walk up the dependencies of a module to find any that depend on any of the listed sources

Definition at line 330 of file helpers.py.

References bitset_utilities.append().

Referenced by extraJets_MuEGFixMoriond2017.backupJetsFirstStep().

331 def listDependencyChain(process, module, sources, verbose=False):
332  """
333  Walk up the dependencies of a module to find any that depend on any of the listed sources
334  """
335  def allDirectInputModules(moduleOrPSet,moduleName,attrName):
336  ret = set()
337  for name,value in moduleOrPSet.parameters_().iteritems():
338  type = value.pythonTypeName()
339  if type == 'cms.PSet':
340  ret.update(allDirectInputModules(value,moduleName,moduleName+"."+name))
341  elif type == 'cms.VPSet':
342  for (i,ps) in enumerate(value):
343  ret.update(allDirectInputModules(ps,moduleName,"%s.%s[%d]"%(moduleName,name,i)))
344  elif type == 'cms.VInputTag':
345  inputs = [ MassSearchReplaceAnyInputTagVisitor.standardizeInputTagFmt(it) for it in value ]
346  inputLabels = [ tag.moduleLabel for tag in inputs if tag.processName == '' or tag.processName == process.name_() ]
347  ret.update(inputLabels)
348  if verbose and inputLabels: print "%s depends on %s via %s" % (moduleName, inputLabels, attrName+"."+name)
349  elif type.endswith('.InputTag'):
350  if value.processName == '' or value.processName == process.name_():
351  ret.add(value.moduleLabel)
352  if verbose: print "%s depends on %s via %s" % (moduleName, value.moduleLabel, attrName+"."+name)
353  ret.discard("")
354  return ret
355  def fillDirectDepGraphs(root,fwdepgraph,revdepgraph):
356  if root.label_() in fwdepgraph: return
357  deps = allDirectInputModules(root,root.label_(),root.label_())
358  fwdepgraph[root.label_()] = []
359  for d in deps:
360  fwdepgraph[root.label_()].append(d)
361  if d not in revdepgraph: revdepgraph[d] = []
362  revdepgraph[d].append(root.label_())
363  depmodule = getattr(process,d,None)
364  if depmodule:
365  fillDirectDepGraphs(depmodule,fwdepgraph,revdepgraph)
366  return (fwdepgraph,revdepgraph)
367  fwdepgraph, revdepgraph = fillDirectDepGraphs(module, {}, {})
368  #def toDot(graph,outname):
369  # ret = "digraph G { rankdir=\"LR\" \n"
370  # alls = set(graph.iterkeys())
371  # for vs in graph.itervalues(): alls.update(vs)
372  # if module.label() in alls: ret += "\t%s[shape=rect style=filled fillcolor=red];\n" % module.label()
373  # for s in sources:
374  # if s in alls: ret += "\t%s[shape=rect style=filled fillcolor=green];\n" % s
375  # alls.discard(module.label())
376  # alls.difference_update(sources)
377  # for a in alls:
378  # ret += "\t%s[shape=rect style=filled fillcolor=%s];\n" % (a, "white" if hasattr(process,a) else "gray")
379  # for k,vs in graph.iteritems():
380  # for v in vs: ret += "\t%s -> %s" % (k,v)
381  # ret += "}"
382  # fout = open(outname, "w")
383  # fout.write(ret)
384  # fout.close()
385  #toDot(fwdepgraph,"fwd.dot")
386  #toDot(revdepgraph,"rvd.dot")
387  def flattenRevDeps(flatgraph, revdepgraph, tip):
388  """Make a graph that for each module lists all the ones that depend on it, directly or indirectly"""
389  # don't do it multiple times for the same module
390  if tip in flatgraph: return
391  # if nobody depends on this module, there's nothing to do
392  if tip not in revdepgraph: return
393  # assemble my dependencies, in a depth-first approach
394  mydeps = set()
395  # start taking the direct dependencies of this module
396  for d in revdepgraph[tip]:
397  # process them
398  flattenRevDeps(flatgraph, revdepgraph, d)
399  # then add them and their processed dependencies to our deps
400  mydeps.add(d)
401  if d in flatgraph:
402  mydeps.update(flatgraph[d])
403  flatgraph[tip] = mydeps
404  flatdeps = {}
405  allmodules = set()
406  for s in sources:
407  flattenRevDeps(flatdeps, revdepgraph, s)
408  if s in flatdeps: allmodules.update(f for f in flatdeps[s])
409  livemodules = [ a for a in allmodules if hasattr(process,a) ]
410  if not livemodules: return None
411  modulelist = [livemodules.pop()]
412  for module in livemodules:
413  for i,m in enumerate(modulelist):
414  if module in flatdeps and m in flatdeps[module]:
415  modulelist.insert(i, module)
416  break
417  if module not in modulelist:
418  modulelist.append(module)
419  # Validate
420  for i,m1 in enumerate(modulelist):
421  for j,m2 in enumerate(modulelist):
422  if j <= i: continue
423  if m2 in flatdeps and m1 in flatdeps[m2]:
424  raise RuntimeError, "BAD ORDER %s BEFORE %s" % (m1,m2)
425  modules = [ getattr(process,p) for p in modulelist ]
426  return cms.Sequence(sum(modules[1:],modules[0]))
def listDependencyChain
Definition: helpers.py:330
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
def helpers.listModules (   sequence)

Definition at line 261 of file helpers.py.

Referenced by __labelsInSequence(), trigTools._modulesInPath(), runMETCorrectionsAndUncertainties.RunMETCorrectionsAndUncertainties.labelsInSequence(), helpers.TestModuleCommand.testListModules(), HiCoreTools.RemoveCleaning.toolCode(), and HiCoreTools.AddCleaning.toolCode().

262 def listModules(sequence):
263  visitor = GatherAllModulesVisitor(gatheredInstance=cms._Module)
264  sequence.visit(visitor)
265  return visitor.modules()
def listModules
Definition: helpers.py:261
def helpers.listSequences (   sequence)

Definition at line 266 of file helpers.py.

Referenced by __labelsInSequence(), and runMETCorrectionsAndUncertainties.RunMETCorrectionsAndUncertainties.labelsInSequence().

267 def listSequences(sequence):
268  visitor = GatherAllModulesVisitor(gatheredInstance=cms.Sequence)
269  sequence.visit(visitor)
270  return visitor.modules()
def listSequences
Definition: helpers.py:266
def helpers.loadWithPostfix (   process,
  moduleName,
  postfix = '' 
)

Definition at line 21 of file helpers.py.

References loadWithPrePostfix().

Referenced by pfTools.usePF2PAT().

21 
22 def loadWithPostfix(process,moduleName,postfix=''):
23  loadWithPrePostfix(process,moduleName,'',postfix)
def loadWithPostfix
Definition: helpers.py:21
def loadWithPrePostfix
Definition: helpers.py:24
def helpers.loadWithPrefix (   process,
  moduleName,
  prefix = '' 
)

Definition at line 18 of file helpers.py.

References loadWithPrePostfix().

Referenced by jetTools.setupBTagging().

18 
19 def loadWithPrefix(process,moduleName,prefix=''):
20  loadWithPrePostfix(process,moduleName,prefix,'')
def loadWithPrePostfix
Definition: helpers.py:24
def loadWithPrefix
Definition: helpers.py:18
def helpers.loadWithPrePostfix (   process,
  moduleName,
  prefix = '',
  postfix = '' 
)

Definition at line 24 of file helpers.py.

References extendWithPrePostfix().

Referenced by loadWithPostfix(), and loadWithPrefix().

24 
25 def loadWithPrePostfix(process,moduleName,prefix='',postfix=''):
26  moduleName = moduleName.replace("/",".")
27  module = __import__(moduleName)
28  #print module.PatAlgos.patSequences_cff.patDefaultSequence
29  extendWithPrePostfix(process,sys.modules[moduleName],prefix,postfix)
def extendWithPrePostfix
Definition: helpers.py:30
def loadWithPrePostfix
Definition: helpers.py:24
def helpers.massSearchReplaceAnyInputTag (   sequence,
  oldInputTag,
  newInputTag,
  verbose = False,
  moduleLabelOnly = False,
  skipLabelTest = False 
)
Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...

Definition at line 271 of file helpers.py.

Referenced by boostedHPSPFTaus_cfi.addBoostedTaus(), extraSlimmedMETs_MuEGFixMoriond2017.addExtraMETCollections(), extraJets_MuEGFixMoriond2017.backupJetsSecondStep(), patMuonsWithTrigger_cff.changeRecoMuonInput(), customiseForPremixingInput.customiseForPreMixingInput(), extendWithPrePostfix(), CustomConfigs.MassReplaceInputTag(), ConfigBuilder.MassReplaceInputTag(), ConfigBuilder.ConfigBuilder.renameInputTagsInSequence(), helpers.TestModuleCommand.testMassSearchReplaceAnyInputTag(), and patMuonsWithTrigger_cff.useExistingPATMuons().

272 def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False,skipLabelTest=False) :
273  """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
274  sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest))
def massSearchReplaceAnyInputTag
Definition: helpers.py:271
def helpers.massSearchReplaceParam (   sequence,
  paramName,
  paramOldValue,
  paramValue,
  verbose = False 
)

Definition at line 258 of file helpers.py.

Referenced by CustomConfigs.MassReplaceParameter(), pfTools.reconfigurePF2PATTaus(), switchMVAtoDB_cfi.switchMVAtoDB(), and helpers.TestModuleCommand.testMassSearchReplaceParam().

259 def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue,verbose=False):
260  sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose))
def massSearchReplaceParam
Definition: helpers.py:258
def helpers.removeIfInSequence (   process,
  target,
  sequenceLabel,
  postfix = "" 
)

Definition at line 84 of file helpers.py.

References __labelsInSequence(), and MatrixUtil.remove().

Referenced by HiCoreTools.RemoveSpecificPATObjects.toolCode().

84 
85 def removeIfInSequence(process, target, sequenceLabel, postfix=""):
86  labels = __labelsInSequence(process, sequenceLabel, postfix)
87  if target+postfix in labels:
88  getattr(process, sequenceLabel+postfix).remove(
89  getattr(process, target+postfix)
90  )
def removeIfInSequence
Definition: helpers.py:84
def __labelsInSequence
Definition: helpers.py:91