{ "cells": [ { "cell_type": "markdown", "id": "1b68bccb-a98b-48c2-889a-10e5c4822eeb", "metadata": { "user_expressions": [] }, "source": [ "# How to use Awkward Arrays in C++ with cppyy" ] }, { "cell_type": "markdown", "id": "f80c4c0d-f4da-4e1e-adb8-90f8482d23e8", "metadata": { "tags": [], "user_expressions": [] }, "source": [ ":::{warning}\n", "\n", "Awkward Array can only work with `cppyy` 3.1 or later.\n", ":::\n", "\n", ":::{warning}\n", "`cppyy` must be in a different venv or conda environment from ROOT, if you have installed ROOT, because the two packages define modules with conflicting names.\n", ":::\n", "\n", "The [cppyy](https://cppyy.readthedocs.io/en/latest/index.html) is an automatic, run-time, Python-C++ bindings generator, for calling C++ from Python and Python from C++. `cppyy` is based on the C++ interpreter `Cling`.\n", "\n", "`cppyy` can understand Awkward Arrays. When an {class}`ak.Array` type is passed to a C++ function defined in `cppyy`, a `__cast_cpp__` magic function of an {class}`ak.Array` is invoked. The function dynamically generates a C++ type and a view of the array, if it has not been generated yet.\n", "\n", "The view is a lightweight 40-byte C++ object dynamically allocated on the stack. This view is generated on demand - and only once per Awkward Array, the data are not copied." ] }, { "cell_type": "code", "execution_count": 1, "id": "48778e8a", "metadata": { "execution": { "iopub.execute_input": "2024-12-16T18:18:27.554494Z", "iopub.status.busy": "2024-12-16T18:18:27.554331Z", "iopub.status.idle": "2024-12-16T18:18:27.759783Z", "shell.execute_reply": "2024-12-16T18:18:27.759215Z" } }, "outputs": [ { "data": { "text/plain": [ "'2.7.2'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import awkward as ak\n", "ak.__version__" ] }, { "cell_type": "code", "execution_count": 2, "id": "dd32d294", "metadata": { "execution": { "iopub.execute_input": "2024-12-16T18:18:27.761831Z", "iopub.status.busy": "2024-12-16T18:18:27.761441Z", "iopub.status.idle": "2024-12-16T18:18:27.765017Z", "shell.execute_reply": "2024-12-16T18:18:27.764484Z" } }, "outputs": [], "source": [ "import awkward._connect.cling" ] }, { "cell_type": "code", "execution_count": 3, "id": "a87d01b8", "metadata": { "execution": { "iopub.execute_input": "2024-12-16T18:18:27.766838Z", "iopub.status.busy": "2024-12-16T18:18:27.766514Z", "iopub.status.idle": "2024-12-16T18:18:29.092648Z", "shell.execute_reply": "2024-12-16T18:18:29.092166Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(Re-)building pre-compiled headers (options: -O2 -march=native); this may take a minute ...\n", "ERROR: cannot find etc/dictpch/allHeaders.h file here ./etc/dictpch/allHeaders.h nor here etc/dictpch/allHeaders.h\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/opt/hostedtoolcache/Python/3.11.0/x64/lib/python3.11/site-packages/cppyy_backend/loader.py:139: UserWarning: No precompiled header available (failed to build); this may impact performance.\n", " warnings.warn('No precompiled header available (%s); this may impact performance.' % msg)\n", "input_line_10:2:45: error: explicit instantiation of '_M_use_local_data' does not refer to a function template, variable template, member function, member class, or static data member\n", "template std::string::pointer std::string::_M_use_local_data();\n", " ^\n", "input_line_10:3:46: error: explicit instantiation of '_M_use_local_data' does not refer to a function template, variable template, member function, member class, or static data member\n", "template std::wstring::pointer std::wstring::_M_use_local_data();\n", " ^\n" ] }, { "data": { "text/plain": [ "'3.1.0'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cppyy\n", "cppyy.__version__" ] }, { "cell_type": "markdown", "id": "39b1877e", "metadata": {}, "source": [ "Let's define an Awkward Array as a list of records:" ] }, { "cell_type": "code", "execution_count": 4, "id": "97f90216", "metadata": { "execution": { "iopub.execute_input": "2024-12-16T18:18:29.094586Z", "iopub.status.busy": "2024-12-16T18:18:29.094198Z", "iopub.status.idle": "2024-12-16T18:18:29.100682Z", "shell.execute_reply": "2024-12-16T18:18:29.100111Z" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
[[{x: 1, y: [1.1]}, {x: 2, y: [2.2, 0.2]}],\n", " [],\n", " [{x: 3, y: [3, 0.3, 3.3]}]]\n", "-------------------------------------------\n", "type: 3 * var * {\n", " x: int64,\n", " y: var * float64\n", "}" ], "text/plain": [ "