195 {
196 module.doc() = "Nanobind bindings for PythonToolHandle";
197
198 static_assert(std::is_same_v<columnar::ColumnarMode,columnar::ColumnarModeArray>, "this module is only defined for array mode");
199
200 module.attr("numberOfEventsName") = &columnar::eventRangeColumnName;
201 module.attr("eventRangeColumnName") = &columnar::eventRangeColumnName;
202
203
204 module.attr("invalid_link_value") = columnar::ColumnarModeArray::invalidLinkValue;
205
206 module.def("crc64",
207 [](const std::string& data) { return CxxUtils::crc64(data); },
208 "data"_a,
209 "CRC-64 of a string, using the Athena default polynomial "
210 "(CxxUtils::crc64).");
211
212 module.def("crc64addint", &CxxUtils::crc64addint,
213 "crc"_a, "x"_a,
214 "Extend a previously-calculated CRC-64 to include an integer "
215 "(CxxUtils::crc64addint).");
216
217 module.def("sg_key",
218 &columnar::computeSgKey,
219 "name"_a, "clid"_a = 0,
220 "StoreGate hash (sgkey) of a container name, optionally mixed with "
221 "its CLID. ElementLink m_persKey values in Athena-written files are "
222 "sg_key(container_name, container_clid).");
223
224
225#ifdef XAOD_STANDALONE
226
227 module.def("set_python_printer", &set_printer_from_callable, nb::arg("callback"),
228 "Install a Python callable(level: int, name: str, text: str) as the global "
229 "C++ message printer.");
230
231
232 module.def("set_python_printer", &clear_printer,
233 "Reset to the default stdout message printer.");
234#else
235
236
237 module.def("set_python_printer", [](nb::args, nb::kwargs) {
238 throw std::runtime_error(
239 "set_python_printer is only available in standalone "
240 "(AnalysisBase/ColumnarAnalysis) builds, not in Athena/AthAnalysis.");
241 }, "Not available in Athena/AthAnalysis builds.");
242#endif
243
245 nb::enum_<columnar::ColumnAccessMode>(module, "ColumnAccessMode")
250 switch (mode) {
252 return "<ColumnAccessMode input>";
254 return "<ColumnAccessMode output>";
256 return "<ColumnAccessMode update>";
257 default:
258 return "<ColumnAccessMode update value=" + std::to_string(static_cast<int>(mode)) + ">";
259 }
260 })
261 .export_values();
262
263 nb::enum_<MSG::Level>(module, "MsgLevel", nb::is_arithmetic())
264 .value("NIL", MSG::NIL)
265 .value("VERBOSE", MSG::VERBOSE)
266 .value("DEBUG", MSG::DEBUG)
267 .value("INFO", MSG::INFO)
268 .value("WARNING", MSG::WARNING)
269 .value("ERROR", MSG::ERROR)
270 .value("FATAL", MSG::FATAL)
271 .export_values();
272
273 nb::class_<columnar::ColumnInfo>(module, "ColumnInfo")
274 .def(nb::init<>())
279 })
292 std::string access_mode;
295 access_mode = "input";
296 break;
298 access_mode = "output";
299 break;
301 access_mode = "update";
302 break;
303 default:
304
305 access_mode = "unknown";
306 }
307 return "<ColumnInfo name='" + self.
name +
"'" +
309 ", access_mode='" + access_mode + "'" +
312 ">";
313 })
316 d[
"name"] = self.
name;
319 d[
"access_mode"] =
static_cast<int>(self.
accessMode);
331 });
332
333 nb::class_<columnar::PythonToolHandle>(module, "PythonToolHandle")
334 .def(nb::init())
335
336
341 std::cerr << "Warning: PythonToolHandle.type is empty."
342 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
343 }
345 })
346
351 std::cerr << "Warning: PythonToolHandle.name is empty."
352 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
353 }
355 })
356
357
358 .def("set_type_and_name",
361 },
362 "type_and_name"_a,
363 "Set the type and name of the tool.")
364
366 "key"_a, "value"_a,
367 "Set a property on the tool.")
368
370 "key"_a, "value"_a,
371 "Set a property on the tool.")
372
373 .def("preinitialize",
375 "Preinitialize the tool.")
376
377
378 .def("rename_containers",
380 "renames"_a,
381 "Rename the columns the tool uses.")
382
383
384 .def("rename_containers",
386 std::vector<std::pair<std::string, std::string>> vectorized;
387 for (
const auto&
pair : renames)
388 vectorized.emplace_back(
pair);
389
391 },
392 "renames"_a,
393 "Rename the columns the tool uses.")
394
395 .def("initialize",
397 "Initialize the tool.")
398
399 .def("apply_systematic_variation",
402 },
403 "sys_name"_a,
404 "Apply a systematic variation to the tool.")
405
406 .def("set_column",
409 },
410 "key"_a, "column"_a,
411 "Set a float column pointer.")
412
413 .def("set_column",
416 },
417 "key"_a, "column"_a,
418 "Set a char column pointer.")
419
420 .def("set_column",
423 },
424 "key"_a, "column"_a,
425 "Set an int column pointer.")
426
427 .def("set_column",
430 },
431 "key"_a, "column"_a,
432 "Set a uint8_t column pointer.")
433
434 .def("set_column",
437 },
438 "key"_a, "column"_a,
439 "Set a uint16_t column pointer.")
440
441 .def("set_column",
444 },
445 "key"_a, "column"_a,
446 "Set a uint32_t column pointer.")
447
448 .def("set_column",
451 },
452 "key"_a, "column"_a,
453 "Set a uint64_t column pointer.")
454
456
457 "key"_a, "column"_a, "is_const"_a = true,
458 "Set a void column pointer (nanobind version).")
459
461 "key"_a, "column"_a,
462 "Set a void immutable column pointer (nanobind version).")
463
465 "key"_a,
466 "Get a column as a numpy array (zero-copy view into the tool's buffer).")
467
468 .def("keys",
470 "Return the column names (enables dict(handle)).")
471
472 .def("call",
474 "Call the tool and reset the columns.")
475
476 .def_prop_ro(
477 "columns",
479 "Get the expected column information."
480 )
481
482 .def("get_recommended_systematics",
484 "Get the recommended systematics.")
485
486
487
491 });
492}
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)
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