195 {
196 module.doc() = "Nanobind bindings for PythonToolHandle";
197
199 throw nb::import_error("This module can only be used in columnar access mode. Try setting up a ColumnarAnalysis release instead.");
200
201 module.attr("numberOfEventsName") = &columnar::eventRangeColumnName;
202 module.attr("eventRangeColumnName") = &columnar::eventRangeColumnName;
203
204
205 module.attr("invalid_link_value") = columnar::ColumnarModeArray::invalidLinkValue;
206
207 module.def("crc64",
208 [](const std::string& data) { return CxxUtils::crc64(data); },
209 "data"_a,
210 "CRC-64 of a string, using the Athena default polynomial "
211 "(CxxUtils::crc64).");
212
213 module.def("crc64addint", &CxxUtils::crc64addint,
214 "crc"_a, "x"_a,
215 "Extend a previously-calculated CRC-64 to include an integer "
216 "(CxxUtils::crc64addint).");
217
218 module.def("sg_key",
219 &columnar::computeSgKey,
220 "name"_a, "clid"_a = 0,
221 "StoreGate hash (sgkey) of a container name, optionally mixed with "
222 "its CLID. ElementLink m_persKey values in Athena-written files are "
223 "sg_key(container_name, container_clid).");
224
225
226#ifdef XAOD_STANDALONE
227
228 module.def("set_python_printer", &set_printer_from_callable, nb::arg("callback"),
229 "Install a Python callable(level: int, name: str, text: str) as the global "
230 "C++ message printer.");
231
232
233 module.def("set_python_printer", &clear_printer,
234 "Reset to the default stdout message printer.");
235#else
236
237
238 module.def("set_python_printer", [](nb::args, nb::kwargs) {
239 throw std::runtime_error(
240 "set_python_printer is only available in standalone "
241 "(AnalysisBase/ColumnarAnalysis) builds, not in Athena/AthAnalysis.");
242 }, "Not available in Athena/AthAnalysis builds.");
243#endif
244
246 nb::enum_<columnar::ColumnAccessMode>(module, "ColumnAccessMode")
251 switch (mode) {
253 return "<ColumnAccessMode input>";
255 return "<ColumnAccessMode output>";
257 return "<ColumnAccessMode update>";
258 default:
259 return "<ColumnAccessMode update value=" + std::to_string(static_cast<int>(mode)) + ">";
260 }
261 })
262 .export_values();
263
264 nb::enum_<MSG::Level>(module, "MsgLevel", nb::is_arithmetic())
265 .value("NIL", MSG::NIL)
266 .value("VERBOSE", MSG::VERBOSE)
267 .value("DEBUG", MSG::DEBUG)
268 .value("INFO", MSG::INFO)
269 .value("WARNING", MSG::WARNING)
270 .value("ERROR", MSG::ERROR)
271 .value("FATAL", MSG::FATAL)
272 .export_values();
273
274 nb::class_<columnar::ColumnInfo>(module, "ColumnInfo")
275 .def(nb::init<>())
280 })
293 std::string access_mode;
296 access_mode = "input";
297 break;
299 access_mode = "output";
300 break;
302 access_mode = "update";
303 break;
304 default:
305
306 access_mode = "unknown";
307 }
308 return "<ColumnInfo name='" + self.
name +
"'" +
310 ", access_mode='" + access_mode + "'" +
313 ">";
314 })
317 d[
"name"] = self.
name;
320 d[
"access_mode"] =
static_cast<int>(self.
accessMode);
332 });
333
334 nb::class_<columnar::PythonToolHandle>(module, "PythonToolHandle")
335 .def(nb::init())
336
337
342 std::cerr << "Warning: PythonToolHandle.type is empty."
343 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
344 }
346 })
347
352 std::cerr << "Warning: PythonToolHandle.name is empty."
353 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
354 }
356 })
357
358
359 .def("set_type_and_name",
362 },
363 "type_and_name"_a,
364 "Set the type and name of the tool.")
365
367 "key"_a, "value"_a,
368 "Set a property on the tool.")
369
371 "key"_a, "value"_a,
372 "Set a property on the tool.")
373
374 .def("preinitialize",
376 "Preinitialize the tool.")
377
378
379 .def("rename_containers",
381 "renames"_a,
382 "Rename the columns the tool uses.")
383
384
385 .def("rename_containers",
387 std::vector<std::pair<std::string, std::string>> vectorized;
388 for (
const auto&
pair : renames)
389 vectorized.emplace_back(
pair);
390
392 },
393 "renames"_a,
394 "Rename the columns the tool uses.")
395
396 .def("initialize",
398 "Initialize the tool.")
399
400 .def("apply_systematic_variation",
403 },
404 "sys_name"_a,
405 "Apply a systematic variation to the tool.")
406
407 .def("set_column",
410 },
411 "key"_a, "column"_a,
412 "Set a float column pointer.")
413
414 .def("set_column",
417 },
418 "key"_a, "column"_a,
419 "Set a char column pointer.")
420
421 .def("set_column",
424 },
425 "key"_a, "column"_a,
426 "Set an int column pointer.")
427
428 .def("set_column",
431 },
432 "key"_a, "column"_a,
433 "Set a uint8_t column pointer.")
434
435 .def("set_column",
438 },
439 "key"_a, "column"_a,
440 "Set a uint16_t column pointer.")
441
442 .def("set_column",
445 },
446 "key"_a, "column"_a,
447 "Set a uint32_t column pointer.")
448
449 .def("set_column",
452 },
453 "key"_a, "column"_a,
454 "Set a uint64_t column pointer.")
455
457
458 "key"_a, "column"_a, "is_const"_a = true,
459 "Set a void column pointer (nanobind version).")
460
462 "key"_a, "column"_a,
463 "Set a void immutable column pointer (nanobind version).")
464
466 "key"_a,
467 "Get a column as a numpy array (zero-copy view into the tool's buffer).")
468
469 .def("keys",
471 "Return the column names (enables dict(handle)).")
472
473 .def("call",
475 "Call the tool and reset the columns.")
476
477 .def_prop_ro(
478 "columns",
480 "Get the expected column information."
481 )
482
483 .def("get_recommended_systematics",
485 "Get the recommended systematics.")
486
487
488
492 });
493}
nb::object getColumnVoid(columnar::PythonToolHandle &self, const std::string &key)
std::string get_type_name(const std::type_info &type_info)
void setColumnVoid(columnar::PythonToolHandle &self, const std::string &key, nb::ndarray<> column, bool is_const=true)
void setProperty(columnar::PythonToolHandle &self, const std::string &key, nb::object value)
void setImmutableColumnVoid(columnar::PythonToolHandle &self, const std::string &key, nb::ndarray<> column)
std::string getAddressString(const columnar::PythonToolHandle &obj)
constexpr unsigned columnarAccessMode
ColumnAccessMode
an enum for the different access modes for a column
@ update
an updateable column
a struct that contains meta-information about each column that's needed to interface the column with ...
std::string offsetName
the name of the offset column used for this column (or empty string for none)
std::string soleLinkTargetName
for simple link columns: the name of the target container
std::string keyColumnForVariantLink
if this is a key column for a variant link, the name of the associated link column
std::uint32_t soleLinkTargetClid
for simple link columns: the CLID of the target container
std::vector< unsigned > fixedDimensions
the fixed dimensions this column has (if any)
bool isOptional
whether this column is optional
std::vector< std::string > variantLinkTargetNames
for variant link key columns: the names of the containers we can link to
std::string name
the name of the column
bool isOffset
whether this is an offset column
ColumnAccessMode accessMode
the access mode for the column
std::string replacesColumn
whether this replaces another column
bool isVariantLink
whether this is a variant link column
unsigned index
the index of the column in the data array
const std::type_info * type
the type of the individual entries in the column