Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:54

0001 
0002 def ignoreAllFiltersOnPath(path):
0003   """Given a 'Path', find all EDFilters and wrap them in 'cms.ignore'
0004   """
0005   import FWCore.ParameterSet.Config as cms
0006   from FWCore.ParameterSet.SequenceTypes import _MutatingSequenceVisitor, _UnarySequenceOperator
0007 
0008   class IgnoreFilters(object):
0009     def __init__(self):
0010       self.__lastCallUnary = False
0011       self.onTask = False
0012     def __call__(self, obj):
0013       if self.onTask:
0014         return obj
0015       elif isinstance(obj,_UnarySequenceOperator):
0016         self.__lastCallUnary = True
0017       elif obj.isLeaf() and isinstance(obj, cms.EDFilter) and not self.__lastCallUnary:
0018         return cms.ignore(obj)
0019       else:
0020         self.__lastCallUnary = False
0021       return obj
0022   class IgnoreFiltersVisitor(_MutatingSequenceVisitor):
0023     def __init__(self):
0024       self.operator = IgnoreFilters()
0025       self._levelInTasks = 0
0026       super(type(self),self).__init__(self.operator)
0027     def enter(self,visitee):
0028       if isinstance(visitee, cms.Task):
0029         self._levelInTasks += 1
0030       self.operator.onTask = (self._levelInTasks > 0)
0031       super(IgnoreFiltersVisitor,self).enter(visitee)
0032     def leave(self,visitee):
0033       if self._levelInTasks > 0:
0034         if isinstance(visitee, cms.Task):
0035           self._levelInTasks -= 1
0036       super(IgnoreFiltersVisitor,self).leave(visitee)
0037 
0038   mutator = IgnoreFiltersVisitor()
0039   path.visit(mutator)
0040   if mutator._didApply():
0041     path._seq = mutator.result(path)[0]
0042     path._tasks.clear()
0043     path.associate(*mutator.result(path)[1])
0044   return path
0045 
0046 def convertToUnscheduled(proc):
0047   print("covertToUnscheduled is deprecated and no longer needed, and will be removed soon. Please update your configuration.")
0048   return proc
0049 
0050 
0051 def modulesInSequences(* sequences):
0052   from FWCore.ParameterSet.SequenceTypes import ModuleNodeVisitor
0053   modules = []
0054   for sequence in sequences:
0055     sequence.visit(ModuleNodeVisitor(modules))
0056   return modules
0057 
0058 
0059 def moduleLabelsInSequences(* sequences):
0060   return [module.label_() for module in modulesInSequences(* sequences)]
0061 
0062 def createTaskWithAllProducersAndFilters(process):
0063   from FWCore.ParameterSet.Config import Task
0064 
0065   l = [ p for p in process.producers.values()]
0066   l.extend( (f for f in process.filters.values()) )
0067   return Task(*l)
0068 
0069 def convertToSingleModuleEndPaths(process):
0070     """Remove the EndPaths in the Process with more than one module
0071     and replace with new EndPaths each with only one module.
0072     """
0073     import FWCore.ParameterSet.Config as cms
0074     toRemove =[]
0075     added = []
0076     for n,ep in process.endpaths_().items():
0077         tsks = []
0078         ep.visit(cms.TaskVisitor(tsks))
0079 
0080         names = ep.moduleNames()
0081         if 1 == len(names):
0082             continue
0083         toRemove.append(n)
0084         for m in names:
0085             epName = m+"_endpath"
0086             setattr(process,epName,cms.EndPath(getattr(process,m),*tsks))
0087             added.append(epName)
0088 
0089     s = process.schedule_()
0090     if s:
0091         pathNames = [p.label_() for p in s]
0092         for rName in toRemove:
0093             pathNames.remove(rName)
0094         for n in added:
0095             pathNames.append(n)
0096         newS = cms.Schedule(*[getattr(process,n) for n in pathNames])
0097         if s._tasks:
0098           newS.associate(*s._tasks)
0099         process.setSchedule_(newS)
0100 
0101     for r in toRemove:
0102         delattr(process,r)
0103 
0104 
0105 if __name__ == "__main__":
0106     import unittest
0107     class TestModuleCommand(unittest.TestCase):
0108         def setup(self):
0109             None
0110         def testIgnoreFiltersOnPath(self):
0111             import FWCore.ParameterSet.Config as cms
0112             process = cms.Process("Test")
0113             
0114             process.f1 = cms.EDFilter("F1")
0115             process.f2 = cms.EDFilter("F2")
0116             process.f3 = cms.EDFilter("F3")
0117             process.f4 = cms.EDFilter("F4")
0118             process.f5 = cms.EDFilter("F5")
0119             process.f6 = cms.EDFilter("F6")
0120             process.t1 = cms.Task(process.f5)
0121             process.t2 = cms.Task(process.f6)
0122             process.s = cms.Sequence(process.f4, process.t1)
0123             
0124             process.p =  cms.Path(process.f1+cms.ignore(process.f2)+process.f3+process.s, process.t2)
0125             ignoreAllFiltersOnPath(process.p)
0126             self.assertEqual(process.p.dumpPython(),'cms.Path(cms.ignore(process.f1)+cms.ignore(process.f2)+cms.ignore(process.f3)+cms.ignore(process.f4), process.t1, process.t2)\n')
0127 
0128         def testCreateTaskWithAllProducersAndFilters(self):
0129 
0130             import FWCore.ParameterSet.Config as cms
0131             process = cms.Process("TEST")
0132 
0133             process.a = cms.EDProducer("AProd")
0134             process.b = cms.EDProducer("BProd")
0135             process.c = cms.EDProducer("CProd")
0136 
0137             process.f1 = cms.EDFilter("Filter")
0138             process.f2 = cms.EDFilter("Filter2")
0139             process.f3 = cms.EDFilter("Filter3")
0140 
0141             process.out1 = cms.OutputModule("Output1")
0142             process.out2 = cms.OutputModule("Output2")
0143 
0144             process.analyzer1 = cms.EDAnalyzer("analyzerType1")
0145             process.analyzer2 = cms.EDAnalyzer("analyzerType2")
0146 
0147             process.task = createTaskWithAllProducersAndFilters(process)
0148             process.path = cms.Path(process.a, process.task)
0149 
0150             self.assertEqual(process.task.dumpPython(),'cms.Task(process.a, process.b, process.c, process.f1, process.f2, process.f3)\n')
0151             self.assertEqual(process.path.dumpPython(),'cms.Path(process.a, process.task)\n')
0152 
0153         def testConvertToSingleModuleEndPaths(self):
0154             import FWCore.ParameterSet.Config as cms
0155             process = cms.Process("TEST")
0156             process.a = cms.EDAnalyzer("A")
0157             process.b = cms.EDAnalyzer("B")
0158             process.c = cms.EDProducer("C")
0159             process.ep = cms.EndPath(process.a+process.b,cms.Task(process.c))
0160             self.assertEqual(process.ep.dumpPython(),'cms.EndPath(process.a+process.b, cms.Task(process.c))\n')
0161             convertToSingleModuleEndPaths(process)
0162             self.assertEqual(False,hasattr(process,"ep"))
0163             self.assertEqual(process.a_endpath.dumpPython(),'cms.EndPath(process.a, cms.Task(process.c))\n')
0164             self.assertEqual(process.b_endpath.dumpPython(),'cms.EndPath(process.b, cms.Task(process.c))\n')
0165 
0166     unittest.main()