CMS 3D CMS Logo

Utilities.py
Go to the documentation of this file.
1 
3  """Given a 'Path', find all EDFilters and wrap them in 'cms.ignore'
4  """
5  import FWCore.ParameterSet.Config as cms
6  from FWCore.ParameterSet.SequenceTypes import _MutatingSequenceVisitor, _UnarySequenceOperator
7 
8  class IgnoreFilters(object):
9  def __init__(self):
10  self.__lastCallUnary = False
11  self.onTask = False
12  def __call__(self, obj):
13  if self.onTask:
14  return obj
15  elif isinstance(obj,_UnarySequenceOperator):
16  self.__lastCallUnary = True
17  elif obj.isLeaf() and isinstance(obj, cms.EDFilter) and not self.__lastCallUnary:
18  return cms.ignore(obj)
19  else:
20  self.__lastCallUnary = False
21  return obj
22  class IgnoreFiltersVisitor(_MutatingSequenceVisitor):
23  def __init__(self):
24  self.operator = IgnoreFilters()
25  self._levelInTasks = 0
26  super(type(self),self).__init__(self.operator)
27  def enter(self,visitee):
28  if isinstance(visitee, cms.Task):
29  self._levelInTasks += 1
30  self.operator.onTask = (self._levelInTasks > 0)
31  super(IgnoreFiltersVisitor,self).enter(visitee)
32  def leave(self,visitee):
33  if self._levelInTasks > 0:
34  if isinstance(visitee, cms.Task):
35  self._levelInTasks -= 1
36  super(IgnoreFiltersVisitor,self).leave(visitee)
37 
38  mutator = IgnoreFiltersVisitor()
39  path.visit(mutator)
40  if mutator._didApply():
41  path._seq = mutator.result(path)[0]
42  path._tasks.clear()
43  path.associate(*mutator.result(path)[1])
44  return path
45 
47  import FWCore.ParameterSet.Config as cms
48  """Given a 'Process', convert the python configuration from scheduled execution to unscheduled. This is done by
49  1. Pulling EDProducers off Path and EndPath Sequences and putting them on an associated Task.
50  2. Pulling EDFilters whose filter results are ignored off Path and EndPath Sequences and putting
51  them on an associated Task.
52  3. Fixing up the Schedule if needed
53  """
54  # Warning: It is not always possible to convert a configuration
55  # where EDProducers are all run on Paths to an unscheduled
56  # configuration by modifying only the python configuration.
57  # There is more than one kind of pathological condition
58  # that can cause this conversion to produce a configuration
59  # that gives different results than the original configuration
60  # when run under cmsRun. One should view the converted configuration
61  # as a thing which needs to be validated. It is possible for there
62  # to be pathologies that cannot be resolved by modifying only the
63  # python configuration and may require redesign inside the C++ code
64  # of the modules and redesign of the logic. For example,
65  # an EDAnalyzer might try to get a product and check if the
66  # returned handle isValid. Then it could behave differently
67  # depending on whether or not the product was present.
68  # The behavior when the product is not present could
69  # be meaningful and important. In the unscheduled case,
70  # the EDProducer will always run and the product could
71  # always be there.
72 
73  proc.resolve()
74 
75  proc=cleanUnscheduled(proc)
76  return proc
77 
78 def cleanUnscheduled(proc):
79  import FWCore.ParameterSet.Config as cms
80 
81  pathsAndEndPaths = dict(proc.paths)
82  pathsAndEndPaths.update( dict(proc.endpaths) )
83 
84  #have to get them now since switching them after the
85  # paths have been changed gives null labels
86  if proc.schedule:
87  pathNamesInScheduled = [p.label_() for p in proc.schedule]
88  else:
89  pathNamesInScheduled = False
90 
91  def getUnqualifiedName(name):
92  if name[0] in set(['!','-']):
93  return name[1:]
94  return name
95 
96  def getQualifiedModule(name,proc):
97  unqual_name = getUnqualifiedName(name)
98  p=getattr(proc,unqual_name)
99  if unqual_name != name:
100  if name[0] == '!':
101  p = ~p
102  elif name[0] == '-':
103  p = cms.ignore(p)
104  return p
105 
106  # Loop over paths
107  # On each path we move EDProducers and EDFilters that
108  # are ignored to Tasks
109  producerList = list()
110  import six
111  for pName, originalPath in six.iteritems(pathsAndEndPaths):
112  producerList[:] = []
113  qualified_names = []
114  v = cms.DecoratedNodeNamePlusVisitor(qualified_names)
115  originalPath.visit(v)
116  remaining =[]
117 
118  for n in qualified_names:
119  unqual_name = getUnqualifiedName(n)
120  mod = getattr(proc,unqual_name)
121 
122  #remove EDProducer's and EDFilter's which are set to ignore
123  if not (isinstance(mod, cms.EDProducer) or
124  (n[0] =='-' and isinstance(mod, cms.EDFilter)) ):
125  remaining.append(n)
126  else:
127  producerList.append(mod)
128 
129  taskList = []
130  if v.leavesOnTasks():
131  taskList.append(cms.Task(*(v.leavesOnTasks())))
132  if (producerList):
133  taskList.append(cms.Task(*producerList))
134 
135  if remaining:
136  p = getQualifiedModule(remaining[0],proc)
137  for m in remaining[1:]:
138  p+=getQualifiedModule(m,proc)
139  setattr(proc,pName,type(getattr(proc,pName))(p))
140  else:
141  setattr(proc,pName,type(getattr(proc,pName))())
142 
143  newPath = getattr(proc,pName)
144  if (taskList):
145  newPath.associate(*taskList)
146 
147  # If there is a schedule then it needs to point at
148  # the new Path objects
149  if proc.schedule:
150  listOfTasks = list(proc.schedule._tasks)
151  proc.schedule = cms.Schedule([getattr(proc,p) for p in pathNamesInScheduled])
152  proc.schedule.associate(*listOfTasks)
153  return proc
154 
155 
156 def modulesInSequences(* sequences):
157  from FWCore.ParameterSet.SequenceTypes import ModuleNodeVisitor
158  modules = []
159  for sequence in sequences:
160  sequence.visit(ModuleNodeVisitor(modules))
161  return modules
162 
163 
164 def moduleLabelsInSequences(* sequences):
165  return [module.label() for module in modulesInSequences(* sequences)]
166 
168  from FWCore.ParameterSet.Config import Task
169  import six
170 
171  l = [ p for p in six.itervalues(process.producers)]
172  l.extend( (f for f in six.itervalues(process.filters)) )
173  return Task(*l)
174 
176  """Remove the EndPaths in the Process with more than one module
177  and replace with new EndPaths each with only one module.
178  """
179  import FWCore.ParameterSet.Config as cms
180  import six
181  toRemove =[]
182  added = []
183  for n,ep in six.iteritems(process.endpaths_()):
184  tsks = []
185  ep.visit(cms.TaskVisitor(tsks))
186 
187  names = ep.moduleNames()
188  if 1 == len(names):
189  continue
190  toRemove.append(n)
191  for m in names:
192  epName = m+"_endpath"
193  setattr(process,epName,cms.EndPath(getattr(process,m),*tsks))
194  added.append(epName)
195 
196  s = process.schedule_()
197  if s:
198  pathNames = [p.label_() for p in s]
199  for rName in toRemove:
200  pathNames.remove(rName)
201  for n in added:
202  pathNames.append(n)
203  newS = cms.Schedule(*[getattr(process,n) for n in pathNames])
204  if s._tasks:
205  newS.associate(*s._tasks)
206  process.setSchedule_(newS)
207 
208  for r in toRemove:
209  delattr(process,r)
210 
211 
212 if __name__ == "__main__":
213  import unittest
214  class TestModuleCommand(unittest.TestCase):
215  def setup(self):
216  None
218  import FWCore.ParameterSet.Config as cms
219  process = cms.Process("Test")
220 
221  process.f1 = cms.EDFilter("F1")
222  process.f2 = cms.EDFilter("F2")
223  process.f3 = cms.EDFilter("F3")
224  process.f4 = cms.EDFilter("F4")
225  process.f5 = cms.EDFilter("F5")
226  process.f6 = cms.EDFilter("F6")
227  process.t1 = cms.Task(process.f5)
228  process.t2 = cms.Task(process.f6)
229  process.s = cms.Sequence(process.f4, process.t1)
230 
231  process.p = cms.Path(process.f1+cms.ignore(process.f2)+process.f3+process.s, process.t2)
232  ignoreAllFiltersOnPath(process.p)
233  self.assertEqual(process.p.dumpPython(None),'cms.Path(cms.ignore(process.f1)+cms.ignore(process.f2)+cms.ignore(process.f3)+cms.ignore(process.f4), process.t1, process.t2)\n')
234 
235  def testNoSchedule(self):
236  import FWCore.ParameterSet.Config as cms
237  process = cms.Process("TEST")
238 
239  process.a = cms.EDProducer("AProd")
240  process.b = cms.EDProducer("BProd")
241  process.c = cms.EDProducer("CProd")
242  process.d = cms.EDProducer("DProd")
243  process.m = cms.EDProducer("MProd")
244  process.n = cms.EDProducer("NProd")
245  process.r = cms.EDProducer("RProd")
246  process.s = cms.EDProducer("SProd")
247 
248  process.t1 = cms.Task(process.m)
249  t2 = cms.Task(process.n)
250 
251  process.f1 = cms.EDFilter("Filter")
252  process.f2 = cms.EDFilter("Filter2")
253  process.f3 = cms.EDFilter("Filter3")
254  process.f4 = cms.EDFilter("FIlter4")
255 
256  process.out1 = cms.OutputModule("Output1")
257  process.out2 = cms.OutputModule("Output2")
258 
259  process.analyzer1 = cms.EDAnalyzer("analyzerType1")
260  process.analyzer2 = cms.EDAnalyzer("analyzerType2")
261 
262  process.p1 = cms.Path(process.a+process.b+process.f1+process.analyzer1+cms.ignore(process.d)+cms.ignore(process.f2))
263  process.p4 = cms.Path(process.a+process.f2+process.b+~process.f1+cms.ignore(process.f4))
264  process.p2 = cms.Path(process.a+process.b)
265  process.p3 = cms.Path(process.f1, process.t1, t2)
266 
267  process.t3 = cms.Task(process.r)
268  process.t4 = cms.Task(process.s)
269  process.s1 = cms.Sequence(~process.a, process.t3)
270  process.p5 = cms.Path(process.b + process.s1, process.t4)
271  process.end1 = cms.EndPath(process.out1+process.out2+process.analyzer1+process.analyzer2+process.a+process.b+cms.ignore(process.f1))
272  process.end2 = cms.EndPath()
273  convertToUnscheduled(process)
274  self.assert_(hasattr(process,'p2'))
275  self.assert_(hasattr(process,'a'))
276  self.assert_(hasattr(process,'b'))
277  self.assert_(hasattr(process,'c'))
278  self.assert_(hasattr(process,'d'))
279  self.assert_(hasattr(process,'f1'))
280  self.assert_(hasattr(process,'f2'))
281  self.assert_(hasattr(process,'f3'))
282  self.assert_(hasattr(process,'f4'))
283  self.assert_(hasattr(process,'out1'))
284  self.assert_(hasattr(process,'out2'))
285  self.assert_(hasattr(process,'analyzer1'))
286  self.assert_(hasattr(process,'analyzer2'))
287 
288  self.assertEqual(process.p1.dumpPython(None),'cms.Path(process.f1+process.analyzer1, cms.Task(process.a, process.b, process.d, process.f2))\n')
289  self.assertEqual(process.p2.dumpPython(None),'cms.Path(cms.Task(process.a, process.b))\n')
290  self.assertEqual(process.p3.dumpPython(None),'cms.Path(process.f1, cms.Task(process.m, process.n))\n')
291  self.assertEqual(process.p4.dumpPython(None),'cms.Path(process.f2+~process.f1, cms.Task(process.a, process.b, process.f4))\n')
292  self.assertEqual(process.p5.dumpPython(None),'cms.Path(cms.Task(process.a, process.b), cms.Task(process.r, process.s))\n')
293  self.assertEqual(process.end1.dumpPython(None),'cms.EndPath(process.out1+process.out2+process.analyzer1+process.analyzer2, cms.Task(process.a, process.b, process.f1))\n')
294  self.assertEqual(process.end2.dumpPython(None),'cms.EndPath()\n')
295 
296  def testWithSchedule(self):
297  import FWCore.ParameterSet.Config as cms
298  process = cms.Process("TEST")
299 
300  process.a = cms.EDProducer("AProd")
301  process.b = cms.EDProducer("BProd")
302  process.c = cms.EDProducer("CProd")
303  process.d = cms.EDProducer("DProd")
304  process.m = cms.EDProducer("MProd")
305  process.n = cms.EDProducer("NProd")
306 
307  process.t1 = cms.Task(process.m)
308  t2 = cms.Task(process.n)
309 
310  process.f1 = cms.EDFilter("Filter")
311  process.f2 = cms.EDFilter("Filter2")
312  process.f3 = cms.EDFilter("Filter3")
313  process.f4 = cms.EDFilter("Filter4")
314 
315  process.out1 = cms.OutputModule("Output1")
316  process.out2 = cms.OutputModule("Output2")
317 
318  process.analyzer1 = cms.EDAnalyzer("analyzerType1")
319  process.analyzer2 = cms.EDAnalyzer("analyzerType2")
320 
321  process.p1 = cms.Path(process.a+process.b+cms.ignore(process.f1)+process.d+process.f2)
322  process.p4 = cms.Path(process.a+process.f2+process.b+~process.f1)
323  process.p2 = cms.Path(process.a+process.b)
324  process.p3 = cms.Path(process.f1)
325  process.p5 = cms.Path(process.a+process.f4) #not used on schedule
326  process.end1 = cms.EndPath(process.out1+process.out2+process.analyzer1+process.analyzer2+process.a+process.b+cms.ignore(process.f1))
327  process.end2 = cms.EndPath()
328 
329  process.schedule = cms.Schedule(process.p1,process.p4,process.p2,process.p3,process.end1,process.end2,tasks=[process.t1,t2])
330  convertToUnscheduled(process)
331  self.assert_(hasattr(process,'p2'))
332  self.assert_(hasattr(process,'a'))
333  self.assert_(hasattr(process,'b'))
334  self.assert_(hasattr(process,'c'))
335  self.assert_(hasattr(process,'d'))
336  self.assert_(hasattr(process,'f1'))
337  self.assert_(hasattr(process,'f2'))
338  self.assert_(hasattr(process,'f3'))
339  self.assert_(hasattr(process,"f4"))
340  self.assert_(hasattr(process,"p5"))
341 
342  self.assertEqual(process.p1.dumpPython(None),'cms.Path(process.f2, cms.Task(process.a, process.b, process.d, process.f1))\n')
343  self.assertEqual(process.p2.dumpPython(None),'cms.Path(cms.Task(process.a, process.b))\n')
344  self.assertEqual(process.p3.dumpPython(None),'cms.Path(process.f1)\n')
345  self.assertEqual(process.p4.dumpPython(None),'cms.Path(process.f2+~process.f1, cms.Task(process.a, process.b))\n')
346  self.assertEqual(process.p5.dumpPython(None),'cms.Path(process.f4, cms.Task(process.a))\n')
347  self.assertEqual(process.end1.dumpPython(None),'cms.EndPath(process.out1+process.out2+process.analyzer1+process.analyzer2, cms.Task(process.a, process.b, process.f1))\n')
348  self.assertEqual(process.end2.dumpPython(None),'cms.EndPath()\n')
349 
350  self.assertEqual([p for p in process.schedule],[process.p1,process.p4,process.p2,process.p3,process.end1,process.end2])
351  listOfTasks = list(process.schedule._tasks)
352  self.assertEqual(listOfTasks, [process.t1,t2])
353 
355 
356  import FWCore.ParameterSet.Config as cms
357  process = cms.Process("TEST")
358 
359  process.a = cms.EDProducer("AProd")
360  process.b = cms.EDProducer("BProd")
361  process.c = cms.EDProducer("CProd")
362 
363  process.f1 = cms.EDFilter("Filter")
364  process.f2 = cms.EDFilter("Filter2")
365  process.f3 = cms.EDFilter("Filter3")
366 
367  process.out1 = cms.OutputModule("Output1")
368  process.out2 = cms.OutputModule("Output2")
369 
370  process.analyzer1 = cms.EDAnalyzer("analyzerType1")
371  process.analyzer2 = cms.EDAnalyzer("analyzerType2")
372 
373  process.task = createTaskWithAllProducersAndFilters(process)
374  process.path = cms.Path(process.a, process.task)
375 
376  self.assertEqual(process.task.dumpPython(None),'cms.Task(process.a, process.b, process.c, process.f1, process.f2, process.f3)\n')
377  self.assertEqual(process.path.dumpPython(None),'cms.Path(process.a, process.task)\n')
378 
380  import FWCore.ParameterSet.Config as cms
381  process = cms.Process("TEST")
382  process.a = cms.EDAnalyzer("A")
383  process.b = cms.EDAnalyzer("B")
384  process.c = cms.EDProducer("C")
385  process.ep = cms.EndPath(process.a+process.b,cms.Task(process.c))
386  self.assertEqual(process.ep.dumpPython(None),'cms.EndPath(process.a+process.b, cms.Task(process.c))\n')
388  self.assertEqual(False,hasattr(process,"ep"))
389  self.assertEqual(process.a_endpath.dumpPython(None),'cms.EndPath(process.a, cms.Task(process.c))\n')
390  self.assertEqual(process.b_endpath.dumpPython(None),'cms.EndPath(process.b, cms.Task(process.c))\n')
391 
392  unittest.main()
def cleanUnscheduled(proc)
Definition: Utilities.py:78
def testIgnoreFiltersOnPath(self)
Definition: Utilities.py:217
def moduleLabelsInSequences(sequences)
Definition: Utilities.py:164
def modulesInSequences(sequences)
Definition: Utilities.py:156
def convertToSingleModuleEndPaths(process)
Definition: Utilities.py:175
def testConvertToSingleModuleEndPaths(self)
Definition: Utilities.py:379
def ignoreAllFiltersOnPath(path)
Definition: Utilities.py:2
def createTaskWithAllProducersAndFilters(process)
Definition: Utilities.py:167
def convertToUnscheduled(proc)
Definition: Utilities.py:46
def testCreateTaskWithAllProducersAndFilters(self)
Definition: Utilities.py:354
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run