ATLAS Offline Software
Loading...
Searching...
No Matches
DualUseConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3isAthena = False
4
5try:
6 from AthenaConfiguration.ComponentFactory import CompFactory
7 isAthena = True
8except ImportError:
9 pass
10
11
12def createComponent( typeName, instanceName, componentType ):
13 """Create a generic configurable
14
15 This function is used to create an component "configurable" in a
16 dual-use way, either returning an actual Athena configurable, or
17 an appropriately configured PythonConfig instance.
18
19 Keyword arguments:
20 typeName -- The C++ type name of the component
21 instanceName -- The instance name of the component to create
22 componentType -- The type of component in AnalysisBase
23
24 """
25
26 if isAthena:
27 # Try to get a configurable for this C++ class "from Athena".
28 # If this succeeds, we're obviously in an Athena environment.
29
30 # Look up the Athena configurable of this component:
31 componentClass = CompFactory.getComp(typeName)
32
33 # Return the object:
34 return componentClass( instanceName )
35
36 else:
37 # If that didn't work, then apparently we're in an EventLoop
38 # environment, so we need to use PythonConfig as the base class
39 # for the user's class.
40 from AnaAlgorithm.PythonConfig import PythonConfig
41 component = PythonConfig( f'{typeName}/{instanceName}' )
42 component.setComponentType( componentType )
43 return component
44
45
46def createAlgorithm( typeName, instanceName ):
47 """Create an algorithm configurable
48
49 This function is used to create an algorithm "configurable" in a dual-use
50 way, either returning an actual Athena configurable, or an appropriately
51 configured EL::AnaAlgorithmConfig instance.
52
53 Keyword arguments:
54 typeName -- The C++ type name of the algorithm
55 instanceName -- The instance name of the algorithm to create
56 """
57 return createComponent( typeName, instanceName, 'AnaAlgorithm' )
58
59
60def createReentrantAlgorithm( typeName, instanceName ):
61 """Create a reentrant algorithm configurable
62
63 This function is used to create an algorithm "configurable" in a dual-use
64 way, either returning an actual Athena configurable, or an appropriately
65 configured PythonConfig instance (with component type
66 AnaReentrantAlgorithm).
67
68 Keyword arguments:
69 typeName -- The C++ type name of the algorithm
70 instanceName -- The instance name of the algorithm to create
71 """
72 return createComponent( typeName, instanceName, 'AnaReentrantAlgorithm' )
73
74
75def createPublicTool( typeName, toolName ):
76 """Helper function for setting up a public tool for a dual-use algorithm
77
78 This function is meant to be used in the analysis algorithm sequence
79 configurations for setting up public tools on the analysis algorithms.
80 Public tools that could then be configured with a syntax shared between
81 Athena and EventLoop.
82
83 Keyword arguments:
84 typeName -- The C++ type name of the private tool
85 toolName -- The property name with which the tool handle was declared on
86 the algorithm. Also the instance name of the tool.
87 """
88
89 if isAthena:
90 # Look up the Athena configurable of this tool:
91 return CompFactory.getComp( typeName )( toolName )
92
93 else:
94 # If that didn't work, then apparently we're in an EventLoop
95 # environment, so let's use the EventLoop specific formalism.
96 return createComponent( typeName, toolName, 'AsgTool' )
97
98
99def createService( typeName, serviceName, sequence=None ):
100 """Helper function for setting up a service for a dual-use algorithm
101
102 This function is meant to be used to set up services in a dual-use
103 manner, particularly for the common CP algorithms. This allows to
104 use the same syntax in EventLoop and Athena, hiding the
105 differences internally. Since in EventLoop the service gets added
106 to a sequence (but in Athena does not), that sequence needs to be
107 passed into this function.
108
109 Keyword arguments:
110 typeName -- The C++ type name of the service
111 serviceName -- The name with which the service handle was configured on
112 the algorithm. Also the instance name of the service.
113 sequence -- an optional argument of an algorithm sequence to add it to
114 in EventLoop (ignored in Athena)
115
116 """
117
118 if isAthena:
119 # Look up the Athena configurable of this serivce:
120 return CompFactory.getComp( typeName )( serviceName )
121
122 else:
123 # If that didn't work, then apparently we're in an EventLoop
124 # environment, so let's use the EventLoop specific formalism.
125 service = createComponent( typeName, serviceName, 'AsgService' )
126 if sequence is not None :
127 sequence += service
128 return service
129
130
131def addPrivateTool( alg, toolName, typeName ):
132 """Helper function for declaring a private tool for a dual-use algorithm
133
134 This function is meant to be used in the analysis algorithm sequence
135 configurations for setting up private tools on the analysis algorithms.
136 Private tools that could then be configured with a syntax shared between
137 Athena and EventLoop.
138
139 Keyword arguments:
140 alg -- The algorithm to set up the private tool on
141 toolName -- The property name with which the tool handle was declared on
142 the algorithm. Also the instance name of the tool.
143 typeName -- The C++ type name of the private tool
144 """
145
146 if isAthena:
147
148 # First try to set up the private tool in an "Athena way".
149
150 # Tokenize the tool's name. In case it is a subtool of a tool, or
151 # something possibly even deeper.
152 toolNames = toolName.split( '.' )
153
154 # Look up the component that we need to set up the private tool on:
155 component = alg
156 for tname in toolNames[ 0 : -1 ]:
157 component = getattr( component, tname )
158
159 # Now look up the Athena configurable describing this tool:
160 toolClass = CompFactory.getComp(typeName)
161
162 # Finally, set up the tool handle property:
163 setattr( component, toolNames[ -1 ], toolClass( toolNames[ -1 ] ) )
164
165 else:
166
167 # If that failed, then we should be in an EventLoop environment. So
168 # let's rely on the standalone specific formalism for setting up the
169 # private tool.
170 alg.addPrivateTool( toolName, typeName )
171
172
173def addPrivateToolInArray( alg, toolName, typeName ):
174 """Helper function for declaring a private tool in an array for a
175 dual-use algorithm
176
177 This function is meant to be used in the analysis algorithm
178 sequence configurations for setting up private tools in arrays on
179 the analysis algorithms. Private tools that could then be
180 configured with a syntax shared between Athena and EventLoop.
181
182 Keyword arguments:
183 alg -- The algorithm to set up the private tool on
184 toolName -- The property name with which the tool handle was declared on
185 the algorithm. Also the instance name of the tool.
186 typeName -- The C++ type name of the private tool
187
188 """
189
190 if isAthena:
191
192 # First try to set up the private tool in an "Athena way".
193
194 # Tokenize the tool's name. In case it is a subtool of a tool, or
195 # something possibly even deeper.
196 toolNames = toolName.split( '.' )
197
198 # Look up the component that we need to set up the private tool on:
199 component = alg
200 for tname in toolNames[ 0 : -1 ]:
201 component = getattr( component, tname )
202
203 # Now look up the Athena configurable describing this tool:
204 toolClass = CompFactory.getComp(typeName)
205
206 # Finally, set up the tool handle property, and return the tool that
207 # was just appended (not the whole tool-handle array), so that callers
208 # can configure it the same way they do in EventLoop.
209 theTool = toolClass( toolNames[ -1 ] )
210 getattr( component, toolNames[ -1 ] ).append( theTool )
211 return theTool
212
213 else:
214
215 # If that failed, then we should be in an EventLoop environment. So
216 # let's rely on the standalone specific formalism for setting up the
217 # private tool.
218 return alg.addPrivateToolInArray( toolName, typeName )
createPublicTool(typeName, toolName)
createComponent(typeName, instanceName, componentType)
createReentrantAlgorithm(typeName, instanceName)
createService(typeName, serviceName, sequence=None)
createAlgorithm(typeName, instanceName)
addPrivateTool(alg, toolName, typeName)
addPrivateToolInArray(alg, toolName, typeName)