7 ''' If a module is in patDefaultSequence use the cloned module.
8 Will crash if patDefaultSequence has not been cloned with 'postfix' beforehand'''
11 if hasattr(process,
"patPF2PATSequence"):
13 if label
in defaultLabels
and hasattr(process, label+postfix):
14 result = getattr(process, label+postfix)
15 elif hasattr(process, label):
16 print "WARNING: called applyPostfix for module/sequence %s which is not in patDefaultSequence%s!"%(label,postfix)
17 result = getattr(process, label)
22 if target+postfix
in labels:
23 getattr(process, sequenceLabel+postfix).
remove(
24 getattr(process, target+postfix)
28 result = [ m.label()[:-len(postfix)]
for m
in listModules( getattr(process,sequenceLabel+postfix))]
29 result.extend([ m.label()[:-len(postfix)]
for m
in listSequences( getattr(process,sequenceLabel+postfix))] )
31 result = [ m.label()
for m
in listModules( getattr(process,sequenceLabel+postfix))]
32 result.extend([ m.label()
for m
in listSequences( getattr(process,sequenceLabel+postfix))] )
36 """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value"""
37 def __init__(self,paramName,paramSearch,paramValue,verbose=False):
50 class MassSearchReplaceAnyInputTagVisitor(
object):
51 """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value
52 It will climb down within PSets, VPSets and VInputTags to find its target"""
53 def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False):
60 if isinstance(pset, cms._Parameterizable):
61 for name
in pset.parameterNames_():
64 value = getattr(pset,name)
65 type = value.pythonTypeName()
66 if type ==
'cms.PSet':
67 self.
doIt(value,base+
"."+name)
68 elif type ==
'cms.VPSet':
69 for (i,ps)
in enumerate(value): self.
doIt(ps,
"%s.%s[%d]"%(base,name,i) )
70 elif type ==
'cms.VInputTag':
71 for (i,n)
in enumerate(value):
77 elif self.
_moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel:
78 nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel
79 if self.
_verbose:
print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep)
81 elif type.endswith(
'.InputTag'):
84 from copy
import deepcopy
85 if 'untracked' in type:
86 setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(),
87 self._paramReplace.getProductInstanceLabel(),
88 self._paramReplace.getProcessName()))
91 elif self.
_moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel:
92 from copy
import deepcopy
93 repl = deepcopy(getattr(pset, name))
94 repl.moduleLabel = self._paramReplace.moduleLabel
95 setattr(pset, name, repl)
96 if self.
_verbose:
print "Replace %s.%s %s ==> %s " % (base, name, value, repl)
101 ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str '''
102 if not isinstance(inputTag, cms.InputTag):
103 return cms.InputTag(inputTag)
108 try: label = visitee.label_()
109 except AttributeError: label =
'<Module not in a Process>'
110 self.
doIt(visitee, label)
116 """Visitor that travels within a cms.Sequence, and returns a list of objects of type gatheredInance(e.g. modules) that have it"""
122 self._modules.append(visitee)
129 """Visitor that travels within a cms.Sequence, and returns a cloned version of the Sequence.
130 All modules and sequences are cloned and a postfix is added"""
142 if isinstance(visitee,cms._Module):
143 label = visitee.label()
148 self._moduleLabels.append(label)
150 newModule = visitee.clone()
154 if isinstance(visitee,cms.Sequence):
157 self._sequenceStack.append( getattr(self.
_process, visitee.label()+self.
_postfix) )
159 self._sequenceStack.append(visitee.label())
162 if isinstance(visitee,cms.Sequence):
166 raise StandardError,
"empty Sequence encountered during cloneing. sequnece stack: %s"%self.
_sequenceStack
171 raise StandardError,
"someting went wrong, the sequence stack looks like: %s"%self.
_sequenceStack
179 oldSequenceLabel = self._sequenceStack.pop()
180 newSequenceLabel = oldSequenceLabel + self.
_postfix
181 self._sequenceStack.append(cms.Sequence(visitee))
182 if hasattr(self.
_process, newSequenceLabel):
183 raise StandardError(
"Cloning the sequence "+self.
_sequenceStack[-1].
label()+
" would overwrite existing object." )
185 self._sequenceLabels.append(oldSequenceLabel)
190 """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
198 self._modules.append(visitee)
210 sequence.visit(visitor)
211 return visitor.modules()
215 sequence.visit(visitor)
216 return visitor.modules()
219 """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
224 ------------------------------------------------------------------
225 return the string of the jet collection module depending on the
226 input vaules. The default return value will be 'patAK5CaloJets'.
228 algo : indicating the algorithm type of the jet [expected are
229 'AK5', 'IC5', 'SC7', ...]
230 type : indicating the type of constituents of the jet [expec-
231 ted are 'Calo', 'PFlow', 'JPT', ...]
232 prefix : prefix indicating the type of pat collection module (ex-
233 pected are '', 'selected', 'clean').
234 ------------------------------------------------------------------
237 jetCollectionString =
'pat'
239 jetCollectionString =prefix
240 jetCollectionString+=
'Pat'
241 jetCollectionString+=
'Jets'
242 jetCollectionString+=algo
243 jetCollectionString+=type
244 return jetCollectionString
248 ------------------------------------------------------------------
249 return True if a module with name 'module' is contained in the
250 sequence with name 'sequence' and False otherwise. This version
251 is not so nice as it also returns True for any substr of the name
252 of a contained module.
254 sequence : sequence [e.g. process.patDefaultSequence]
255 module : module name as a string
256 ------------------------------------------------------------------
258 return not sequence.__str__().
find(moduleName)==-1
264 ------------------------------------------------------------------
265 copy a sequence plus the modules and sequences therein
266 both are renamed by getting a postfix
267 input tags are automatically adjusted
268 ------------------------------------------------------------------
271 if not postfix ==
"":
273 sequence.visit(visitor)
274 result = visitor.clonedSequence()
277 if __name__==
"__main__":
284 p = cms.Process(
"test")
285 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
286 p.b = cms.EDProducer(
"b", src=cms.InputTag(
"a"))
287 p.c = cms.EDProducer(
"c", src=cms.InputTag(
"b",
"instance"))
288 p.s = cms.Sequence(p.a*p.b*p.c *p.a)
290 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')
292 p = cms.Process(
"test")
293 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
294 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
295 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"))
296 p.s1 = cms.Sequence(p.a*p.b*p.c)
297 p.s2 = cms.Sequence(p.b*p.c)
299 self.assert_(
not contains(p.s2,
"a") )
302 self.assertEqual(
jetCollectionString(prefix =
'prefix', algo =
'Foo', type =
'Bar'),
'prefixPatFooBarJets')
304 p = cms.Process(
"test")
305 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
306 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
307 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"))
308 p.s = cms.Sequence(p.a*p.b*p.c)
311 p = cms.Process(
"test")
312 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
313 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
314 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
315 nested = cms.PSet(src = cms.InputTag(
"c"))
317 p.s = cms.Sequence(p.a*p.b*p.c)
319 self.assertEqual(cms.InputTag(
"a"),p.c.src)
320 self.assertNotEqual(cms.InputTag(
"a"),p.c.nested.src)
322 p = cms.Process(
"test")
323 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
324 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
325 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
326 nested = cms.PSet(src = cms.InputTag(
"b"), src2 = cms.InputTag(
"c")),
327 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"b")), cms.PSet(src = cms.InputTag(
"d"))),
328 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
330 p.s = cms.Sequence(p.a*p.b*p.c)
332 self.assertNotEqual(cms.InputTag(
"new"), p.b.src)
333 self.assertEqual(cms.InputTag(
"new"), p.c.src)
334 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
335 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
336 self.assertNotEqual(cms.InputTag(
"new"), p.c.nested.src2)
337 self.assertEqual(cms.InputTag(
"new"), p.c.nestedv[0].src)
338 self.assertNotEqual(cms.InputTag(
"new"), p.c.nestedv[1].src)
339 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[0])
340 self.assertEqual(cms.InputTag(
"new"), p.c.vec[1])
341 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[2])
342 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[3])
def cloneProcessingSnippet
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
def __appendToTopSequence
def applyPostfix
Helpers to perform some technically boring tasks like looking for all modules with a given parameter ...
def massSearchReplaceAnyInputTag
def massSearchReplaceParam
def testMassSearchReplaceParam
def testJetCollectionString
perl if(1 lt scalar(@::datatypes))
def testMassSearchReplaceAnyInputTag