ATLAS Offline Software
Loading...
Searching...
No Matches
python.AnaAlgorithmConfig.AnaAlgorithmConfig Class Reference
Inheritance diagram for python.AnaAlgorithmConfig.AnaAlgorithmConfig:
Collaboration diagram for python.AnaAlgorithmConfig.AnaAlgorithmConfig:

Public Member Functions

 __init__ (self, typeAndName, **kwargs)
 getName (self)
 getType (self)
 __getattr__ (self, name)
 __setattr__ (self, key, value)
 __eq__ (self, other)
 __str__ (self)
 addPrivateTool (self, name, type)
 addPrivateToolInArray (self, name, type)

Static Public Attributes

int printHeaderWidth = 80
int printHeaderPre = 3

Static Protected Member Functions

 _printHeader (title)
 _printFooter (title)

Protected Attributes

dict _props = {}

Detailed Description

Standalone Analysis Algorithm Configuration

This class is used to describe the configuration of an analysis algorithm
(a C++ class inheriting from EL::AnaAlgorithm) in Python. It behaves
similar to an Athena configurable, but is implemented in a much simpler
way.

An example of using it in configuring an EventLoop job could look like:

   job = ROOT.EL.Job()
   ...
   from AnaAlgorithm.AnaAlgorithmConfig import AnaAlgorithmConfig
   alg = AnaAlgorithmConfig( "EL::UnitTestAlg2/TestAlg",
                             property = 1.23 )
   alg.string_property = "Foo"
   job.algsAdd( alg )

Note that the python code doesn't know what properties can actually be set
on any given C++ algorithm. Any mistake made in the Python configuration
(apart from syntax errors) is only discovered while initialising the
analysis job.

Definition at line 8 of file AnaAlgorithmConfig.py.

Constructor & Destructor Documentation

◆ __init__()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.__init__ ( self,
typeAndName,
** kwargs )
Constructor for an algorithm configuration object

Keyword arguments:
  typeAndName -- The type/instance name of the algorithm

Note that you can pass (initial) properties to the constructor like:

   alg = AnaAlgorithmConfig( "EL::UnitTestAlg2/TestAlg",
                             property = 1.23 )

Definition at line 36 of file AnaAlgorithmConfig.py.

36 def __init__( self, typeAndName, **kwargs ):
37 """Constructor for an algorithm configuration object
38
39 Keyword arguments:
40 typeAndName -- The type/instance name of the algorithm
41
42 Note that you can pass (initial) properties to the constructor like:
43
44 alg = AnaAlgorithmConfig( "EL::UnitTestAlg2/TestAlg",
45 property = 1.23 )
46 """
47
48 # Call the base class's constructor. Use the default constructor instead
49 # of the one receiving the type and name, to avoid ROOT-10872.
50 super().__init__()
51 self.setTypeAndName( typeAndName )
52
53 # Initialise the properties of the algorihm:
54 self._props = {}
55
56 # Set the properties on the object:
57 for key, value in kwargs.items():
58 self.setPropertyFromString( key, stringPropValue( value ) )
59 self._props[ key ] = copy.deepcopy( value )
60

Member Function Documentation

◆ __eq__()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.__eq__ ( self,
other )
Check for equality with another object

The implementation of this is very simple. We only check that the type
and the name of the algorithms would match.

Definition at line 127 of file AnaAlgorithmConfig.py.

127 def __eq__( self, other ):
128 """Check for equality with another object
129
130 The implementation of this is very simple. We only check that the type
131 and the name of the algorithms would match.
132 """
133
134 # First check that the other object is also an AnaAlgorithmConfig one:
135 if not isinstance( other, AnaAlgorithmConfig ):
136 return False
137
138 # Now check whether the type and the name of the algorithms agree:
139 return ( ( self.type() == other.type() ) and
140 ( self.name() == other.name() ) )
141

◆ __getattr__()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.__getattr__ ( self,
name )
Get a previously set property value from the configuration

This function allows us to retrieve the value of a property that was
already set for the algorithm, to possibly use it in some configuration
decisions in the Python code itself.

Keyword arguments:
  name -- The name of the property

Definition at line 79 of file AnaAlgorithmConfig.py.

79 def __getattr__( self, name ):
80 """Get a previously set property value from the configuration
81
82 This function allows us to retrieve the value of a property that was
83 already set for the algorithm, to possibly use it in some configuration
84 decisions in the Python code itself.
85
86 Keyword arguments:
87 name -- The name of the property
88 """
89
90 # Short-circuit internal/dunder attribute lookups, which are never
91 # properties (and would otherwise recurse if '_props' is missing):
92 if name.startswith( '_' ):
93 raise AttributeError( name )
94
95 # Fail if the property was not (yet) set:
96 if name not in self._props:
97 raise AttributeError( f"Property '{name}' was not set on "
98 f"'{self.type()}/{self.name()}'" )
99
100 # Return the property value:
101 return self._props[ name ]
102

◆ __setattr__()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.__setattr__ ( self,
key,
value )
Set an algorithm property on an existing configuration object

This function allows us to set/override properties on an algorithm
configuration object. Allowing for the following syntax:

   alg = ...
   alg.IntProperty = 66
   alg.FloatProperty = 3.141592
   alg.StringProperty = "Foo"

Keyword arguments:
  key   -- The key/name of the property
  value -- The value to set for the property

Definition at line 103 of file AnaAlgorithmConfig.py.

103 def __setattr__( self, key, value ):
104 """Set an algorithm property on an existing configuration object
105
106 This function allows us to set/override properties on an algorithm
107 configuration object. Allowing for the following syntax:
108
109 alg = ...
110 alg.IntProperty = 66
111 alg.FloatProperty = 3.141592
112 alg.StringProperty = "Foo"
113
114 Keyword arguments:
115 key -- The key/name of the property
116 value -- The value to set for the property
117 """
118
119 # Private variables should be set directly:
120 if key[ 0 ] == '_':
121 return super().__setattr__( key, value )
122
123 # Set the property, and remember its value:
124 super().setPropertyFromString( key, stringPropValue( value ) )
125 self._props[ key ] = copy.deepcopy( value )
126

◆ __str__()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.__str__ ( self)
Print the algorithm configuration in a user friendly way

This is just to help with debugging configurations, allowing
the user to get a nice printout of their job configuration.

Definition at line 142 of file AnaAlgorithmConfig.py.

142 def __str__( self ):
143 """Print the algorithm configuration in a user friendly way
144
145 This is just to help with debugging configurations, allowing
146 the user to get a nice printout of their job configuration.
147 """
148
149 if self.isPublicTool():
150 name = f'Public Tool {self.type()}/{self.name()}'
151 else:
152 name = f'Algorithm {self.type()}/{self.name()}'
153 result = AnaAlgorithmConfig._printHeader( name )
154 result += '\n'
155 for key, value in sorted( self._props.items() ):
156 if isinstance( value, str ):
157 printedValue = f"'{value}'"
158 else:
159 printedValue = value
160 result += f"|- {key}: {indentBy( printedValue, '| ' )}\n"
161 result += AnaAlgorithmConfig._printFooter( name )
162 return result
163

◆ _printFooter()

python.AnaAlgorithmConfig.AnaAlgorithmConfig._printFooter ( title)
staticprotected
Produce a nice footer when printing the configuration

This function is used for printing the footer of both algorithms
and tools.

Keyword arguments:
  indentString -- String used as indentation
  title        -- The title of the algorithm/tool

Definition at line 261 of file AnaAlgorithmConfig.py.

261 def _printFooter( title ):
262 """Produce a nice footer when printing the configuration
263
264 This function is used for printing the footer of both algorithms
265 and tools.
266
267 Keyword arguments:
268 indentString -- String used as indentation
269 title -- The title of the algorithm/tool
270 """
271
272 preLength = AnaAlgorithmConfig.printHeaderPre
273 postLength = AnaAlgorithmConfig.printHeaderWidth - 12 - preLength - \
274 len( title )
275 return f"\\{preLength * '-'} (End of {title}) {postLength * '-'}"
276
277

◆ _printHeader()

python.AnaAlgorithmConfig.AnaAlgorithmConfig._printHeader ( title)
staticprotected
Produce a nice header when printing the configuration

This function is used for printing the header of both algorithms
and tools.

Keyword arguments:
  indentString -- String used as indentation
  title        -- The title of the algorithm/tool

Definition at line 244 of file AnaAlgorithmConfig.py.

244 def _printHeader( title ):
245 """Produce a nice header when printing the configuration
246
247 This function is used for printing the header of both algorithms
248 and tools.
249
250 Keyword arguments:
251 indentString -- String used as indentation
252 title -- The title of the algorithm/tool
253 """
254
255 preLength = AnaAlgorithmConfig.printHeaderPre
256 postLength = AnaAlgorithmConfig.printHeaderWidth - 3 - preLength - \
257 len( title )
258 return f"/{preLength * '*'} {title} {postLength * '*'}"
259

◆ addPrivateTool()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.addPrivateTool ( self,
name,
type )
Create a private tool for the algorithm

This function is used in 'standalone' mode to declare a private tool
for the algorithm, or a private tool for an already declared private
tool.

Can be used like:
  config.addPrivateTool( 'tool1', 'ToolType1' )
  config.addPrivateTool( 'tool1.tool2', 'ToolType2' )

Keyword arguments:
  name -- The full name of the private tool
  type -- The C++ type of the private tool

Definition at line 164 of file AnaAlgorithmConfig.py.

164 def addPrivateTool( self, name, type ):
165 """Create a private tool for the algorithm
166
167 This function is used in 'standalone' mode to declare a private tool
168 for the algorithm, or a private tool for an already declared private
169 tool.
170
171 Can be used like:
172 config.addPrivateTool( 'tool1', 'ToolType1' )
173 config.addPrivateTool( 'tool1.tool2', 'ToolType2' )
174
175 Keyword arguments:
176 name -- The full name of the private tool
177 type -- The C++ type of the private tool
178 """
179
180 # And now set up the Python object that will take care of setting
181 # properties on this tool.
182
183 # Tokenize the tool's name. In case it is a subtool of a tool, or
184 # something possibly even deeper.
185 toolNames = name.split( '.' )
186
187 # Look up the component that we need to set up the private tool on.
188 component = self
189 for tname in toolNames[ 0 : -1 ]:
190 component = getattr( component, tname )
191
192 # Check that the component doesn't have such a (tool) property yet.
193 if hasattr( component, toolNames[ -1 ] ):
194 raise RuntimeError( f"Tool with name '{name}' already exists" )
195
196 # Now set up a smart object as a property on that component.
197 component._props[ toolNames[ -1 ] ] = PrivateToolConfig( self, name,
198 type )
199
200 # Finally, tell the C++ code what to do.
201 self.createPrivateTool( name, type ).ignore()
202

◆ addPrivateToolInArray()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.addPrivateToolInArray ( self,
name,
type )
Create a private tool in an array for the algorithm

This function is used in 'standalone' mode to declare a
private tool in a tool array for the algorithm, or a private
tool in a tool array for an already declared private tool.

Can be used like:
  tool = config.addPrivateToolInArray( 'tool1', 'ToolType1' )
  tool = config.addPrivateToolInArray( 'tool1.tool2', 'ToolType2' )

Keyword arguments:
  name -- The full name of the private tool
  type -- The C++ type of the private tool

Definition at line 203 of file AnaAlgorithmConfig.py.

203 def addPrivateToolInArray( self, name, type ):
204 """Create a private tool in an array for the algorithm
205
206 This function is used in 'standalone' mode to declare a
207 private tool in a tool array for the algorithm, or a private
208 tool in a tool array for an already declared private tool.
209
210 Can be used like:
211 tool = config.addPrivateToolInArray( 'tool1', 'ToolType1' )
212 tool = config.addPrivateToolInArray( 'tool1.tool2', 'ToolType2' )
213
214 Keyword arguments:
215 name -- The full name of the private tool
216 type -- The C++ type of the private tool
217 """
218
219 # And now set up the Python object that will take care of setting
220 # properties on this tool.
221
222 # Tokenize the tool's name. In case it is a subtool of a tool, or
223 # something possibly even deeper.
224 toolNames = name.split( '.' )
225
226 # Look up the component that we need to set up the private tool on.
227 component = self
228 for tname in toolNames[ 0 : -1 ]:
229 component = getattr( component, tname )
230
231 # Finally, tell the C++ code what to do.
232 actualName = self.createPrivateToolInArray( name, type )
233
234 # Tokenize the actual tool's name. In case it is a subtool of
235 # a tool, or something possibly even deeper.
236 actualToolNames = actualName.split( '.' )
237
238 # Now set up a smart object as a property on that component.
239 config = PrivateToolConfig( self, actualName, type )
240 component._props[ actualToolNames[ -1 ] ] = config
241 return config
242

◆ getName()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.getName ( self)
Get the instance name of the algorithm

This is for compatibility with the getName() function of Athena
configurables.

Definition at line 61 of file AnaAlgorithmConfig.py.

61 def getName( self ):
62 """Get the instance name of the algorithm
63
64 This is for compatibility with the getName() function of Athena
65 configurables.
66 """
67
68 return self.name()
69

◆ getType()

python.AnaAlgorithmConfig.AnaAlgorithmConfig.getType ( self)
Get the type name of the algorithm

This is for compatibility with the getType() function of Athena
configurables.

Definition at line 70 of file AnaAlgorithmConfig.py.

70 def getType( self ):
71 """Get the type name of the algorithm
72
73 This is for compatibility with the getType() function of Athena
74 configurables.
75 """
76
77 return self.type()
78

Member Data Documentation

◆ _props

dict python.AnaAlgorithmConfig.AnaAlgorithmConfig._props = {}
protected

Definition at line 54 of file AnaAlgorithmConfig.py.

◆ printHeaderPre

int python.AnaAlgorithmConfig.AnaAlgorithmConfig.printHeaderPre = 3
static

Definition at line 34 of file AnaAlgorithmConfig.py.

◆ printHeaderWidth

int python.AnaAlgorithmConfig.AnaAlgorithmConfig.printHeaderWidth = 80
static

Definition at line 33 of file AnaAlgorithmConfig.py.


The documentation for this class was generated from the following file: