193 {
194 module.doc() = "Nanobind bindings for PythonToolHandle";
195
197 throw nb::import_error("This module can only be used in columnar access mode. Try setting up a ColumnarAnalysis release instead.");
198
199 module.attr("numberOfEventsName") = &columnar::eventRangeColumnName;
200 module.attr("eventRangeColumnName") = &columnar::eventRangeColumnName;
201
202
203#ifdef XAOD_STANDALONE
204
205 module.def("set_python_printer", &set_printer_from_callable, nb::arg("callback"),
206 "Install a Python callable(level: int, name: str, text: str) as the global "
207 "C++ message printer.");
208
209
210 module.def("set_python_printer", &clear_printer,
211 "Reset to the default stdout message printer.");
212#else
213
214
215 module.def("set_python_printer", [](nb::args, nb::kwargs) {
216 throw std::runtime_error(
217 "set_python_printer is only available in standalone "
218 "(AnalysisBase/ColumnarAnalysis) builds, not in Athena/AthAnalysis.");
219 }, "Not available in Athena/AthAnalysis builds.");
220#endif
221
223 nb::enum_<columnar::ColumnAccessMode>(module, "ColumnAccessMode")
228 switch (mode) {
230 return "<ColumnAccessMode input>";
232 return "<ColumnAccessMode output>";
234 return "<ColumnAccessMode update>";
235 default:
236 return "<ColumnAccessMode update value=" + std::to_string(static_cast<int>(mode)) + ">";
237 }
238 })
239 .export_values();
240
241 nb::enum_<MSG::Level>(module, "MsgLevel", nb::is_arithmetic())
242 .value("NIL", MSG::NIL)
243 .value("VERBOSE", MSG::VERBOSE)
244 .value("DEBUG", MSG::DEBUG)
245 .value("INFO", MSG::INFO)
246 .value("WARNING", MSG::WARNING)
247 .value("ERROR", MSG::ERROR)
248 .value("FATAL", MSG::FATAL)
249 .export_values();
250
251 nb::class_<columnar::ColumnInfo>(module, "ColumnInfo")
252 .def(nb::init<>())
257 })
269 std::string access_mode;
272 access_mode = "input";
273 break;
275 access_mode = "output";
276 break;
278 access_mode = "update";
279 break;
280 default:
281
282 access_mode = "unknown";
283 }
284 return "<ColumnInfo name='" + self.
name +
"'" +
286 ", access_mode='" + access_mode + "'" +
289 ">";
290 })
293 d[
"name"] = self.
name;
296 d[
"access_mode"] =
static_cast<int>(self.
accessMode);
307 });
308
309 nb::class_<columnar::PythonToolHandle>(module, "PythonToolHandle")
310 .def(nb::init())
311
312
317 std::cerr << "Warning: PythonToolHandle.type is empty."
318 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
319 }
321 })
322
327 std::cerr << "Warning: PythonToolHandle.name is empty."
328 << " Set with PythonToolHandle.set_type_and_name." << std::endl;
329 }
331 })
332
333
334 .def("set_type_and_name",
337 },
338 "type_and_name"_a,
339 "Set the type and name of the tool.")
340
342 "key"_a, "value"_a,
343 "Set a property on the tool.")
344
346 "key"_a, "value"_a,
347 "Set a property on the tool.")
348
349 .def("preinitialize",
351 "Preinitialize the tool.")
352
353
354 .def("rename_containers",
356 "renames"_a,
357 "Rename the columns the tool uses.")
358
359
360 .def("rename_containers",
362 std::vector<std::pair<std::string, std::string>> vectorized;
363 for (
const auto&
pair : renames)
364 vectorized.emplace_back(
pair);
365
367 },
368 "renames"_a,
369 "Rename the columns the tool uses.")
370
371 .def("initialize",
373 "Initialize the tool.")
374
375 .def("apply_systematic_variation",
378 },
379 "sys_name"_a,
380 "Apply a systematic variation to the tool.")
381
382 .def("set_column",
385 },
386 "key"_a, "column"_a,
387 "Set a float column pointer.")
388
389 .def("set_column",
392 },
393 "key"_a, "column"_a,
394 "Set a char column pointer.")
395
396 .def("set_column",
399 },
400 "key"_a, "column"_a,
401 "Set an int column pointer.")
402
403 .def("set_column",
406 },
407 "key"_a, "column"_a,
408 "Set a uint8_t column pointer.")
409
410 .def("set_column",
413 },
414 "key"_a, "column"_a,
415 "Set a uint16_t column pointer.")
416
417 .def("set_column",
420 },
421 "key"_a, "column"_a,
422 "Set a uint32_t column pointer.")
423
424 .def("set_column",
427 },
428 "key"_a, "column"_a,
429 "Set a uint64_t column pointer.")
430
432
433 "key"_a, "column"_a, "is_const"_a = true,
434 "Set a void column pointer (nanobind version).")
435
437 "key"_a, "column"_a,
438 "Set a void immutable column pointer (nanobind version).")
439
441 "key"_a,
442 "Get a column as a numpy array (zero-copy view into the tool's buffer).")
443
444 .def("keys",
446 "Return the column names (enables dict(handle)).")
447
448 .def("call",
450 "Call the tool and reset the columns.")
451
452 .def_prop_ro(
453 "columns",
455 "Get the expected column information."
456 )
457
458 .def("get_recommended_systematics",
460 "Get the recommended systematics.")
461
462
463
467 });
468}
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::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