8 config = config.replace(
"/",
".")
11 module = __import__(config)
12 for name
in dir(sys.modules[config]):
13 item = getattr(sys.modules[config],name)
14 if isinstance(item,_Labelable)
and not isinstance(item,_ModuleSequenceType)
and not name.startswith(
'_')
and not (name ==
"source" or name ==
"looper" or name ==
"subProcess")
and not type(item)
is cms.PSet:
15 if 'ESProducer' in item.type_():
16 setattr(process,name,item)
19 moduleName = moduleName.replace(
"/",
".")
20 module = __import__(moduleName)
25 """Look in other and find types which we can use"""
30 sequence = cms.Sequence()
31 sequence._moduleLabels = []
32 sequence.setLabel(
'tempSequence')
33 for name
in dir(other):
35 if name.startswith(
'_'):
37 item = getattr(other,name)
38 if name ==
"source" or name ==
"looper" or name ==
"subProcess":
40 elif isinstance(item,cms._ModuleSequenceType):
42 elif isinstance(item,cms.Schedule):
44 elif isinstance(item,cms.VPSet)
or isinstance(item,cms.PSet):
46 elif isinstance(item,cms._Labelable):
47 if not item.hasLabel_():
50 newModule = item.clone()
51 if isinstance(item,cms.ESProducer):
52 newLabel = item.label()
55 if 'TauDiscrimination' in name:
56 process.__setattr__(name,item)
57 newLabel = item.label()+postfix
58 newName = name+postfix
59 process.__setattr__(newName,newModule)
60 if isinstance(newModule, cms._Sequenceable)
and not newName == name:
61 sequence +=getattr(process,newName)
62 sequence._moduleLabels.append(item.label())
64 process.__setattr__(name,item)
67 for label
in sequence._moduleLabels:
72 if hasattr(process, label+postfix):
73 result = getattr(process, label + postfix)
75 raise ValueError(
"Error in <applyPostfix>: No module of name = %s attached to process !!" % (label + postfix))
80 if target+postfix
in labels:
81 getattr(process, sequenceLabel+postfix).
remove(
82 getattr(process, target+postfix)
86 result = [ m.label()[:-len(postfix)]
for m
in listModules( getattr(process,sequenceLabel+postfix))]
87 result.extend([ m.label()[:-len(postfix)]
for m
in listSequences( getattr(process,sequenceLabel+postfix))] )
89 result = [ m.label()
for m
in listModules( getattr(process,sequenceLabel+postfix))]
90 result.extend([ m.label()
for m
in listSequences( getattr(process,sequenceLabel+postfix))] )
94 """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value"""
95 def __init__(self,paramName,paramSearch,paramValue,verbose=False):
108 class MassSearchReplaceAnyInputTagVisitor(
object):
109 """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value
110 It will climb down within PSets, VPSets and VInputTags to find its target"""
111 def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False,skipLabelTest=False):
119 if isinstance(pset, cms._Parameterizable):
120 for name
in pset.parameterNames_():
123 value = getattr(pset,name)
124 type = value.pythonTypeName()
125 if type ==
'cms.PSet':
126 self.
doIt(value,base+
"."+name)
127 elif type ==
'cms.VPSet':
128 for (i,ps)
in enumerate(value): self.
doIt(ps,
"%s.%s[%d]"%(base,name,i) )
129 elif type ==
'cms.VInputTag':
130 for (i,n)
in enumerate(value):
136 elif self.
_moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel:
137 nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel
138 if self.
_verbose:
print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep)
140 elif type.endswith(
'.InputTag'):
143 from copy
import deepcopy
144 if 'untracked' in type:
145 setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(),
146 self._paramReplace.getProductInstanceLabel(),
147 self._paramReplace.getProcessName()))
150 elif self.
_moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel:
151 from copy
import deepcopy
152 repl = deepcopy(getattr(pset, name))
153 repl.moduleLabel = self._paramReplace.moduleLabel
154 setattr(pset, name, repl)
155 if self.
_verbose:
print "Replace %s.%s %s ==> %s " % (base, name, value, repl)
160 ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str '''
161 if not isinstance(inputTag, cms.InputTag):
162 return cms.InputTag(inputTag)
168 try: label = visitee.label_()
169 except AttributeError: label =
'<Module not in a Process>'
171 label =
'<Module label not tested>'
172 self.
doIt(visitee, label)
178 """Visitor that travels within a cms.Sequence, and returns a list of objects of type gatheredInance(e.g. modules) that have it"""
184 self._modules.append(visitee)
191 """Visitor that travels within a cms.Sequence, and returns a cloned version of the Sequence.
192 All modules and sequences are cloned and a postfix is added"""
193 def __init__(self, process, label, postfix, removePostfix=""):
202 if isinstance(visitee, cms._Module):
203 label = visitee.label()
208 self._moduleLabels.append(label)
209 newModule = visitee.clone()
217 for label
in self._moduleLabels:
219 self._moduleLabels = []
220 return self._clonedSequence
227 raise StandardError(
"Tried to remove postfix %s from label %s, but it wasn't there" % (self.
_removePostfix, label))
234 """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
242 self._modules.append(visitee)
254 sequence.visit(visitor)
255 return visitor.modules()
259 sequence.visit(visitor)
260 return visitor.modules()
263 """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
268 ------------------------------------------------------------------
269 return the string of the jet collection module depending on the
270 input vaules. The default return value will be 'patAK5CaloJets'.
272 algo : indicating the algorithm type of the jet [expected are
273 'AK5', 'IC5', 'SC7', ...]
274 type : indicating the type of constituents of the jet [expec-
275 ted are 'Calo', 'PFlow', 'JPT', ...]
276 prefix : prefix indicating the type of pat collection module (ex-
277 pected are '', 'selected', 'clean').
278 ------------------------------------------------------------------
281 jetCollectionString =
'pat'
283 jetCollectionString =prefix
284 jetCollectionString+=
'Pat'
285 jetCollectionString+=
'Jets'
286 jetCollectionString+=algo
287 jetCollectionString+=type
288 return jetCollectionString
292 ------------------------------------------------------------------
293 return True if a module with name 'module' is contained in the
294 sequence with name 'sequence' and False otherwise. This version
295 is not so nice as it also returns True for any substr of the name
296 of a contained module.
298 sequence : sequence [e.g. process.patDefaultSequence]
299 module : module name as a string
300 ------------------------------------------------------------------
302 return not sequence.__str__().
find(moduleName)==-1
308 ------------------------------------------------------------------
309 copy a sequence plus the modules and sequences therein
310 both are renamed by getting a postfix
311 input tags are automatically adjusted
312 ------------------------------------------------------------------
315 if not postfix ==
"":
317 sequence.visit(visitor)
318 result = visitor.clonedSequence()
321 if __name__==
"__main__":
328 p = cms.Process(
"test")
329 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
330 p.b = cms.EDProducer(
"b", src=cms.InputTag(
"a"))
331 p.c = cms.EDProducer(
"c", src=cms.InputTag(
"b",
"instance"))
332 p.s = cms.Sequence(p.a*p.b*p.c *p.a)
334 self.assertEqual(p.dumpPython(),
'import FWCore.ParameterSet.Config as cms\n\nprocess = cms.Process("test")\n\nprocess.a = cms.EDProducer("a",\n src = cms.InputTag("gen")\n)\n\n\nprocess.c = cms.EDProducer("c",\n src = cms.InputTag("b","instance")\n)\n\n\nprocess.cNew = cms.EDProducer("c",\n src = cms.InputTag("bNew","instance")\n)\n\n\nprocess.bNew = cms.EDProducer("b",\n src = cms.InputTag("aNew")\n)\n\n\nprocess.aNew = cms.EDProducer("a",\n src = cms.InputTag("gen")\n)\n\n\nprocess.b = cms.EDProducer("b",\n src = cms.InputTag("a")\n)\n\n\nprocess.s = cms.Sequence(process.a*process.b*process.c*process.a)\n\n\nprocess.sNew = cms.Sequence(process.aNew+process.bNew+process.cNew)\n\n\n')
336 p = cms.Process(
"test")
337 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
338 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
339 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"))
340 p.s1 = cms.Sequence(p.a*p.b*p.c)
341 p.s2 = cms.Sequence(p.b*p.c)
343 self.assert_(
not contains(p.s2,
"a") )
346 self.assertEqual(
jetCollectionString(prefix =
'prefix', algo =
'Foo', type =
'Bar'),
'prefixPatFooBarJets')
348 p = cms.Process(
"test")
349 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
350 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
351 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"))
352 p.s = cms.Sequence(p.a*p.b*p.c)
355 p = cms.Process(
"test")
356 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
357 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
358 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
359 nested = cms.PSet(src = cms.InputTag(
"c"))
361 p.s = cms.Sequence(p.a*p.b*p.c)
363 self.assertEqual(cms.InputTag(
"a"),p.c.src)
364 self.assertNotEqual(cms.InputTag(
"a"),p.c.nested.src)
366 p = cms.Process(
"test")
367 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
368 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
369 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
370 nested = cms.PSet(src = cms.InputTag(
"b"), src2 = cms.InputTag(
"c")),
371 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"b")), cms.PSet(src = cms.InputTag(
"d"))),
372 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
374 p.s = cms.Sequence(p.a*p.b*p.c)
376 self.assertNotEqual(cms.InputTag(
"new"), p.b.src)
377 self.assertEqual(cms.InputTag(
"new"), p.c.src)
378 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
379 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
380 self.assertNotEqual(cms.InputTag(
"new"), p.c.nested.src2)
381 self.assertEqual(cms.InputTag(
"new"), p.c.nestedv[0].src)
382 self.assertNotEqual(cms.InputTag(
"new"), p.c.nestedv[1].src)
383 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[0])
384 self.assertEqual(cms.InputTag(
"new"), p.c.vec[1])
385 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[2])
386 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[3])
def addESProducers
Helpers to perform some technically boring tasks like looking for all modules with a given parameter ...
def cloneProcessingSnippet
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
def __appendToTopSequence
def massSearchReplaceAnyInputTag
def massSearchReplaceParam
def testMassSearchReplaceParam
def testJetCollectionString
def testMassSearchReplaceAnyInputTag