CMS 3D CMS Logo

SequenceVisitors.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 from .SequenceTypes import *
3 from .Modules import OutputModule, EDProducer, EDFilter, EDAnalyzer, Service, ESProducer, ESSource, _Module
4 from .Mixins import _Labelable
5 
6 # Use this on Tasks in the Schedule
8  def __init__(self):
9  pass
10  def enter(self,visitee):
11  if visitee.isLeaf():
12  if isinstance(visitee, _Labelable):
13  if not visitee.hasLabel_():
14  raise ValueError("A task associated with the Schedule contains a module of type '"+visitee.type_()+"'\nwhich has no assigned label.")
15  elif isinstance(visitee, Service):
16  if not visitee._inProcess:
17  raise ValueError("A task associated with the Schedule contains a service of type '"+visitee.type_()+"'\nwhich is not attached to the process.")
18  def leave(self,visitee):
19  pass
20 
21 # Use this on Paths
22 class PathValidator(object):
23  def __init__(self):
24  self.__label = ''
25  def setLabel(self,label):
26  self.__label = "'"+label+"' "
27  def enter(self,visitee):
28  if isinstance(visitee,OutputModule):
29  raise ValueError("Path "+self.__label+"cannot contain an OutputModule, '"+visitee.type_()+"', with label '"+visitee.label_()+"'")
30  if visitee.isLeaf():
31  if isinstance(visitee, _Labelable):
32  if not visitee.hasLabel_():
33  raise ValueError("Path "+self.__label+"contains a module of type '"+visitee.type_()+"' which has no assigned label.")
34  elif isinstance(visitee, Service):
35  if not visitee._inProcess:
36  raise ValueError("Path "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
37  def leave(self,visitee):
38  pass
39 
40 # Use this on EndPaths
41 class EndPathValidator(object):
42  _presetFilters = ["TriggerResultsFilter", "HLTPrescaler"]
43  def __init__(self):
45  self.__label = ''
46  self._levelInTasks = 0
47  def setLabel(self,label):
48  self.__label = "'"+label+"' "
49  def enter(self,visitee):
50  if visitee.isLeaf():
51  if isinstance(visitee, _Labelable):
52  if not visitee.hasLabel_():
53  raise ValueError("EndPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
54  elif isinstance(visitee, Service):
55  if not visitee._inProcess:
56  raise ValueError("EndPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
57  if isinstance(visitee, Task):
58  self._levelInTasks += 1
59  if self._levelInTasks > 0:
60  return
61  if isinstance(visitee,EDFilter):
62  if (visitee.type_() in self._presetFilters):
63  if (visitee.type_() not in self.filtersOnEndpaths):
64  self.filtersOnEndpaths.append(visitee.type_())
65  def leave(self,visitee):
66  if self._levelInTasks > 0:
67  if isinstance(visitee, Task):
68  self._levelInTasks -= 1
69 
70 # Use this on EndPaths
72  def __init__(self):
73  self.__label = ''
74  self._levelInTasks = 0
77  def setLabel(self,label):
78  self.__label = "'"+label+"' "
79  def enter(self,visitee):
80  if visitee.isLeaf():
81  if isinstance(visitee, _Labelable):
82  if not visitee.hasLabel_():
83  raise ValueError("FinalPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
84  elif isinstance(visitee, Service):
85  if not visitee._inProcess:
86  raise ValueError("FinalPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
87  if isinstance(visitee, Task):
88  self._levelInTasks += 1
89  if self._levelInTasks > 0:
90  return
91  if isinstance(visitee,EDFilter):
92  self.filtersOnFinalpaths.append(visitee.type_())
93  if isinstance(visitee,EDProducer):
94  self.producersOnFinalpaths.append(visitee.type_())
95  def leave(self,visitee):
96  if self._levelInTasks > 0:
97  if isinstance(visitee, Task):
98  self._levelInTasks -= 1
99 
101  """Form sets of all modules, ESProducers, ESSources and Services in visited objects. Can be used
102  to visit Paths, EndPaths, Sequences or Tasks. Includes in sets objects on sub-Sequences and sub-Tasks"""
103  def __init__(self):
104  self.modules = set()
105  self.esProducers = set()
106  self.esSources = set()
107  self.services = set()
108  def enter(self,visitee):
109  if visitee.isLeaf():
110  if isinstance(visitee, _Module):
111  self.modules.add(visitee)
112  elif isinstance(visitee, ESProducer):
113  self.esProducers.add(visitee)
114  elif isinstance(visitee, ESSource):
115  self.esSources.add(visitee)
116  elif isinstance(visitee, Service):
117  self.services.add(visitee)
118  def leave(self,visitee):
119  pass
120 
121 class CompositeVisitor(object):
122  """ Combines 3 different visitor classes in 1 so we only have to visit all the paths and endpaths once"""
123  def __init__(self, validator, node, decorated, optional=None):
124  self._validator = validator
125  self._node = node
126  self._decorated = decorated
127  self._optional = optional
128  def enter(self, visitee):
129  self._validator.enter(visitee)
130  self._node.enter(visitee)
131  self._decorated.enter(visitee)
132  if self._optional:
133  self._optional.enter(visitee)
134  def leave(self, visitee):
135  self._validator.leave(visitee)
136  # The node visitor leave function does nothing
137  #self._node.leave(visitee)
138  self._decorated.leave(visitee)
139  if self._optional:
140  self._optional.leave(visitee)
141 
143  """Fill a list with the names of Event module types in a sequence. The names are determined
144  by using globals() to lookup the variable names assigned to the modules. This
145  allows the determination of the labels before the modules have been attached to a Process."""
146  def __init__(self,globals_,l):
147  self._moduleToName = { v[1]:v[0] for v in globals_.items() if isinstance(v[1],_Module) }
148  self._names =l
149  def enter(self,node):
150  if isinstance(node,_Module):
151  self._names.append(self._moduleToName[node])
152  def leave(self,node):
153  return
154 
155 if __name__=="__main__":
156  import unittest
157  class TestModuleCommand(unittest.TestCase):
158  def setUp(self):
159  """Nothing to do """
160  pass
161  def testValidators(self):
162  producer = EDProducer("Producer")
163  analyzer = EDAnalyzer("Analyzer")
164  output = OutputModule("Out")
165  filter = EDFilter("Filter")
166  unlabeled = EDAnalyzer("UnLabeled")
167  producer.setLabel("producer")
168  analyzer.setLabel("analyzer")
169  output.setLabel("output")
170  filter.setLabel("filter")
171  s1 = Sequence(analyzer*producer)
172  s2 = Sequence(output+filter)
173  p1 = Path(s1)
174  p2 = Path(s1*s2)
175  p3 = Path(s1+unlabeled)
176  ep1 = EndPath(producer+output+analyzer)
177  ep2 = EndPath(filter+output)
178  ep3 = EndPath(s2)
179  ep4 = EndPath(unlabeled)
180  pathValidator = PathValidator()
181  endpathValidator = EndPathValidator()
182  p1.visit(pathValidator)
183  self.assertRaises(ValueError, p2.visit, pathValidator)
184  self.assertRaises(ValueError, p3.visit, pathValidator)
185  ep1.visit(endpathValidator)
186  ep2.visit(endpathValidator)
187  ep3.visit(endpathValidator)
188  self.assertRaises(ValueError, ep4.visit, endpathValidator)
189 
190  unittest.main()
191 
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
def __init__(self, validator, node, decorated, optional=None)