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)
 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.

Definition at line 23 of file tool.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 43 of file tool.py.

43 def __init__(self, type_and_name, properties=None, rename_containers=None):
44 self._handle = PythonToolHandle()
45 self._handle.set_type_and_name(type_and_name)
46 self._properties = dict(properties) if properties else {}
47
48 for key, value in self._properties.items():
49 self._handle.set_property(key, value)
50
51 self._handle.initialize()
52
53 if rename_containers:
54 self._handle.rename_containers(rename_containers)
55
56 # Cache classified column topology once after initialization
57 self._classified = classify_columns(self._handle.columns)
58
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 125 of file tool.py.

125 def __call__(self, events, systematic=None):
126 """Run the tool on events and return output columns as an ak.Array.
127
128 Parameters
129 ----------
130 events:
131 An ak.Array with fields matching the tool's input column names
132 (after any rename_containers mapping).
133 systematic:
134 Optional systematic variation name (e.g. "MUON_EFF_RECO_SYS__1up").
135 If provided, applied before running the tool and reset to nominal
136 after execution.
137
138 Returns
139 -------
140 ak.Array
141 Record array with one field per output column, each a
142 variable-length list over the per-particle values.
143 """
144 if systematic is not None:
145 self.apply_systematic_variation(systematic)
146
147 try:
148 num_events = int(ak.num(events, axis=0))
149
150 # Resolve optional columns against the actual fields present
151 effective = resolve_optional_columns(self._classified, events)
152
153 # Extract flat buffers from the awkward array
154 buffer_dict = extract_buffers(events, effective)
155
156 # Allocate zero-filled output arrays (added into buffer_dict in-place)
157 allocate_outputs(effective, buffer_dict)
158
159 # Set all columns on the handle
160 for container_name, info in effective.items():
161 # Container offset (always immutable)
162 self._handle[container_name] = np.asarray(buffer_dict[container_name])
163
164 # Nested-vector offsets and their data inputs/outputs
165 for nested_offset_name, nested in info["nested_offsets"].items():
166 self._handle[nested_offset_name] = np.asarray(
167 buffer_dict[nested_offset_name]
168 )
169 for col in nested["inputs"]:
170 self._handle[col.name] = np.asarray(buffer_dict[col.name])
171 for col in nested["outputs"]:
172 self._handle.set_column_void(col.name, buffer_dict[col.name], False)
173
174 # Flat input data columns (immutable)
175 for col in info["inputs"]:
176 self._handle[col.name] = np.asarray(buffer_dict[col.name])
177
178 # Flat output data columns (mutable)
179 for col in info["outputs"]:
180 self._handle.set_column_void(col.name, buffer_dict[col.name], False)
181
182 self._handle.call()
183
184 return reconstruct_output(effective, buffer_dict, num_events)
185 finally:
186 # Reset to nominal if systematic was applied
187 if systematic is not None:
188 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 114 of file tool.py.

114 def apply_systematic_variation(self, sys_name):
115 """Apply a systematic variation by name.
116
117 Parameters
118 ----------
119 sys_name:
120 Systematic variation name, e.g. ``"MUON_EFF_RECO_SYS__1up"``.
121 Pass ``""`` to reset to the nominal.
122 """
123 self._handle.apply_systematic_variation(sys_name)
124

◆ columns()

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

Definition at line 65 of file tool.py.

65 def columns(self):
66 """All ColumnInfo objects reported by the tool."""
67 return self._handle.columns
68

◆ input_columns()

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

Definition at line 70 of file tool.py.

70 def input_columns(self):
71 """Input (non-offset) ColumnInfo objects, including nested-vector data columns."""
72 cols = []
73 for info in self._classified.values():
74 cols.extend(info["inputs"])
75 for nested in info["nested_offsets"].values():
76 cols.extend(nested["inputs"])
77 return cols
78

◆ output_columns()

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

Definition at line 80 of file tool.py.

80 def output_columns(self):
81 """Output ColumnInfo objects, including nested-vector outputs (currently unsupported)."""
82 cols = []
83 for info in self._classified.values():
84 cols.extend(info["outputs"])
85 for nested in info["nested_offsets"].values():
86 cols.extend(nested["outputs"])
87 return cols
88

◆ properties()

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

Definition at line 60 of file tool.py.

60 def properties(self):
61 """Dict of properties set on this tool at construction time."""
62 return self._properties
63

◆ recommended_systematics()

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

Definition at line 110 of file tool.py.

110 def recommended_systematics(self):
111 """List of recommended systematic variation names."""
112 return self._handle.get_recommended_systematics()
113

◆ 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"``).
Optional inputs are excluded.

Definition at line 90 of file tool.py.

90 def required_input_branch_names(self):
91 """Deduplicated uproot branch names needed to fill non-optional inputs.
92
93 For nested-vector inputs, the trailing ``.data`` suffix is stripped so
94 the returned name matches the actual ROOT branch (e.g.
95 ``"Particles.NumTrkPt500"`` instead of ``"Particles.NumTrkPt500.data"``).
96 Optional inputs are excluded.
97 """
98 seen = []
99 seen_set = set()
100 for col in self.input_columns:
101 if col.is_optional:
102 continue
103 branch = _branch_name_for_column(col.name)
104 if branch not in seen_set:
105 seen_set.add(branch)
106 seen.append(branch)
107 return seen
108
STL class.

Member Data Documentation

◆ _classified

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

Definition at line 57 of file tool.py.

◆ _handle

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

Definition at line 44 of file tool.py.

◆ _properties

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

Definition at line 46 of file tool.py.

◆ input_columns

python.tool.Tool.input_columns

Definition at line 100 of file tool.py.


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