ATLAS Offline Software
Loading...
Searching...
No Matches
python.tool.Tool Class Reference
Collaboration diagram for python.tool.Tool:

Public Member Functions

 __init__ (self, type_and_name, properties=None, rename_containers=None, link_target_names=None)
 properties (self)
 columns (self)
 input_columns (self)
 output_columns (self)
 required_input_branch_names (self)
 recommended_systematics (self)
 apply_systematic_variation (self, sys_name)
 __call__ (self, events, systematic=None)

Public Attributes

 input_columns

Protected Attributes

 _handle = PythonToolHandle()
 _properties = dict(properties) if properties else {}
 _classified = classify_columns(self._handle.columns)

Detailed Description

High-level wrapper around PythonToolHandle.

Handles the boilerplate of classifying columns, extracting awkward-array
buffers, allocating outputs, setting columns on the handle, calling the
tool, and reconstructing the output awkward array.

Parameters
----------
type_and_name:
    Tool type and instance name, e.g. "CP::MuonEfficiencyScaleFactors/myTool".
properties:
    Optional dict of tool properties to set before initialization.
rename_containers:
    Optional mapping from canonical container names (e.g. "Muons") to the
    branch-name prefix used in the input arrays (e.g. "AnalysisMuonsAuxDyn").
    This is passed to PythonToolHandle.rename_containers *after* initialize
    so that ColumnInfo.name values match input array fields.
link_target_names:
    Optional mapping from container names, after ``rename_containers`` is
    applied, to the names used to calculate the keys of links pointing to
    them, e.g.
    ``{"AnalysisMuonsAuxDyn": "AnalysisMuons"}``. By default that is the
    canonical name, independent of ``rename_containers``. The result is
    reported in ``ColumnInfo.name_for_links``.

Definition at line 24 of file tool.py.

Constructor & Destructor Documentation

◆ __init__()

python.tool.Tool.__init__ ( self,
type_and_name,
properties = None,
rename_containers = None,
link_target_names = None )

Definition at line 51 of file tool.py.

51 def __init__(self, type_and_name, properties=None, rename_containers=None, link_target_names=None):
52 self._handle = PythonToolHandle()
53 self._handle.set_type_and_name(type_and_name)
54 self._properties = dict(properties) if properties else {}
55
56 for key, value in self._properties.items():
57 self._handle.set_property(key, value)
58
59 self._handle.initialize()
60
61 if rename_containers:
62 self._handle.rename_containers(rename_containers)
63
64 if link_target_names:
65 self._handle.set_link_target_names(link_target_names)
66
67 # Cache classified column topology once after initialization
68 self._classified = classify_columns(self._handle.columns)
69
void initialize()

Member Function Documentation

◆ __call__()

python.tool.Tool.__call__ ( self,
events,
systematic = None )
Run the tool on events and return output columns as an ak.Array.

Parameters
----------
events:
    An ak.Array with fields matching the tool's input column names
    (after any rename_containers mapping).
systematic:
    Optional systematic variation name (e.g. "MUON_EFF_RECO_SYS__1up").
    If provided, applied before running the tool and reset to nominal
    after execution.

Returns
-------
ak.Array
    Record array with one field per output column, each a
    variable-length list over the per-particle values.

Definition at line 143 of file tool.py.

143 def __call__(self, events, systematic=None):
144 """Run the tool on events and return output columns as an ak.Array.
145
146 Parameters
147 ----------
148 events:
149 An ak.Array with fields matching the tool's input column names
150 (after any rename_containers mapping).
151 systematic:
152 Optional systematic variation name (e.g. "MUON_EFF_RECO_SYS__1up").
153 If provided, applied before running the tool and reset to nominal
154 after execution.
155
156 Returns
157 -------
158 ak.Array
159 Record array with one field per output column, each a
160 variable-length list over the per-particle values.
161 """
162 if systematic is not None:
163 self.apply_systematic_variation(systematic)
164
165 try:
166 num_events = int(ak.num(events, axis=0))
167
168 # Resolve optional columns against the actual fields present
169 effective = resolve_optional_columns(self._classified, events)
170
171 # Extract flat buffers from the awkward array
172 buffer_dict = extract_buffers(events, effective)
173
174 # Allocate zero-filled output arrays (added into buffer_dict in-place)
175 allocate_outputs(effective, buffer_dict)
176
177 # Set all columns on the handle
178 for container_name, info in effective.items():
179 # Container offset (always immutable)
180 self._handle[container_name] = np.asarray(buffer_dict[container_name])
181
182 # Nested-vector offsets and their data inputs/outputs
183 for nested_offset_name, nested in info["nested_offsets"].items():
184 self._handle[nested_offset_name] = np.asarray(
185 buffer_dict[nested_offset_name]
186 )
187 for col in nested["inputs"] + nested["event_indices"]:
188 self._handle[col.name] = np.asarray(buffer_dict[col.name])
189 for col in nested["outputs"]:
190 self._handle.set_column_void(col.name, buffer_dict[col.name], False)
191
192 # Flat input data columns and event indices (immutable)
193 for col in info["inputs"] + info["event_indices"]:
194 self._handle[col.name] = np.asarray(buffer_dict[col.name])
195
196 # Flat output data columns (mutable)
197 for col in info["outputs"]:
198 self._handle.set_column_void(col.name, buffer_dict[col.name], False)
199
200 self._handle.call()
201
202 return reconstruct_output(effective, buffer_dict, num_events)
203 finally:
204 # Reset to nominal if systematic was applied
205 if systematic is not None:
206 self.apply_systematic_variation("")

◆ apply_systematic_variation()

python.tool.Tool.apply_systematic_variation ( self,
sys_name )
Apply a systematic variation by name.

Parameters
----------
sys_name:
    Systematic variation name, e.g. ``"MUON_EFF_RECO_SYS__1up"``.
    Pass ``""`` to reset to the nominal.

Definition at line 132 of file tool.py.

132 def apply_systematic_variation(self, sys_name):
133 """Apply a systematic variation by name.
134
135 Parameters
136 ----------
137 sys_name:
138 Systematic variation name, e.g. ``"MUON_EFF_RECO_SYS__1up"``.
139 Pass ``""`` to reset to the nominal.
140 """
141 self._handle.apply_systematic_variation(sys_name)
142

◆ columns()

python.tool.Tool.columns ( self)
All ColumnInfo objects reported by the tool.

Definition at line 76 of file tool.py.

76 def columns(self):
77 """All ColumnInfo objects reported by the tool."""
78 return self._handle.columns
79

◆ input_columns()

python.tool.Tool.input_columns ( self)
Input (non-offset) ColumnInfo objects, including nested-vector data columns.

Definition at line 81 of file tool.py.

81 def input_columns(self):
82 """Input (non-offset) ColumnInfo objects, including nested-vector data columns."""
83 cols = []
84 for info in self._classified.values():
85 cols.extend(info["inputs"])
86 for nested in info["nested_offsets"].values():
87 cols.extend(nested["inputs"])
88 return cols
89

◆ output_columns()

python.tool.Tool.output_columns ( self)
Output ColumnInfo objects, including nested-vector outputs (currently unsupported).

Definition at line 91 of file tool.py.

91 def output_columns(self):
92 """Output ColumnInfo objects, including nested-vector outputs (currently unsupported)."""
93 cols = []
94 for info in self._classified.values():
95 cols.extend(info["outputs"])
96 for nested in info["nested_offsets"].values():
97 cols.extend(nested["outputs"])
98 return cols
99

◆ properties()

python.tool.Tool.properties ( self)
Dict of properties set on this tool at construction time.

Definition at line 71 of file tool.py.

71 def properties(self):
72 """Dict of properties set on this tool at construction time."""
73 return self._properties
74

◆ recommended_systematics()

python.tool.Tool.recommended_systematics ( self)
List of recommended systematic variation names.

Definition at line 128 of file tool.py.

128 def recommended_systematics(self):
129 """List of recommended systematic variation names."""
130 return self._handle.get_recommended_systematics()
131

◆ required_input_branch_names()

python.tool.Tool.required_input_branch_names ( self)
Deduplicated uproot branch names needed to fill non-optional inputs.

For nested-vector inputs, the trailing ``.data`` suffix is stripped so
the returned name matches the actual ROOT branch (e.g.
``"Particles.NumTrkPt500"`` instead of ``"Particles.NumTrkPt500.data"``).
Key and index columns of links inside a vector map to the branch of
the link itself. For one link per object they keep their names, as
those are the actual ROOT branches: the branch of the link is a
grouping branch, which ``uproot.iterate`` can't read directly.
Optional inputs are excluded.

Definition at line 101 of file tool.py.

101 def required_input_branch_names(self):
102 """Deduplicated uproot branch names needed to fill non-optional inputs.
103
104 For nested-vector inputs, the trailing ``.data`` suffix is stripped so
105 the returned name matches the actual ROOT branch (e.g.
106 ``"Particles.NumTrkPt500"`` instead of ``"Particles.NumTrkPt500.data"``).
107 Key and index columns of links inside a vector map to the branch of
108 the link itself. For one link per object they keep their names, as
109 those are the actual ROOT branches: the branch of the link is a
110 grouping branch, which ``uproot.iterate`` can't read directly.
111 Optional inputs are excluded.
112 """
113 seen = []
114 seen_set = set()
115 for col in self.input_columns:
116 if col.is_optional:
117 continue
118 if _is_key_index_column(col) and "." not in col.offset_name:
119 branch = col.name
120 else:
121 branch = _branch_name_for_column(col.name)
122 if branch not in seen_set:
123 seen_set.add(branch)
124 seen.append(branch)
125 return seen
126
STL class.

Member Data Documentation

◆ _classified

python.tool.Tool._classified = classify_columns(self._handle.columns)
protected

Definition at line 68 of file tool.py.

◆ _handle

python.tool.Tool._handle = PythonToolHandle()
protected

Definition at line 52 of file tool.py.

◆ _properties

python.tool.Tool._properties = dict(properties) if properties else {}
protected

Definition at line 54 of file tool.py.

◆ input_columns

python.tool.Tool.input_columns

Definition at line 115 of file tool.py.


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