Detree reader reference¶
This page is the reference for the Detree reader DSL and the compiler that consumes it. It lists every field, the contract a reader must satisfy, the Python subset the compiler accepts, and what the backend does at run time.
For the guided version, read Write a Detree reader.
Status¶
Important
The Detree route is experimental. It is the recommended way for a downstream project to add its own readers, and every built-in factory implements it, but the DSL may still change between minor releases.
Detree-VM’s official release ships the Rust machine only. The Python and C++ machines are development references and are not selectable through the public API.
Note
A Detree reader is written in Python but never runs as Python: the compiler turns it into module bytes. Two other things are also “Python” and are not this one — Detree’s Python executor (uproot_custom.detree.interp), which runs a Detree reader’s methods for debugging, and the python backend, which executes a separate IReader implementation. See choosing a technical route.
Declaring a reader¶
A Detree reader is a class decorated with @reader that inherits from Reader:
import numpy as np
from uproot_custom.detree import Buffer, Reader, reader
@reader
class IntListReader(Reader):
"""
A `uint32` count followed by that many `int32` values.
"""
# kind="offsets" allows compiler to optimize offset calculations
offsets = Buffer("q", init=(0,), kind="offsets")
values = Buffer("i")
def read(self, stream):
count = stream.read_uint32()
self.offsets.append(self.offsets[-1] + count)
for _ in range(count):
self.values.append(stream.read_int32())
def data(self):
return np.asarray(self.offsets), np.asarray(self.values)
read is compiled and runs inside the machine. data is ordinary Python that runs on the filled buffers, so keep it consistent with your factory’s make_awkward_content — the same requirement as for a Python or C++ reader.
Fields¶
Field |
Purpose |
|---|---|
|
a constructor argument fixed for the branch; readable in |
|
a typed output array. |
|
a variable that survives the entries of one basket and returns to |
|
exactly one sub-reader |
|
an ordered tuple of sub-readers; a |
The reader contract¶
The @reader decorator checks the following at class definition time:
the class is defined in a
.pyfile — the compiler re-reads its source, so a class typed into a notebook or a REPL is refused;no
__init__; declare fields instead and pass values as keyword arguments, e.g.TArrayReader(name, typecode="d")for a reader whosetypecodeis aConstfield;read(self, stream)anddata(self)are required;read_many,read_until, andread_many_memberwisetake(self, stream, argument)and default to loops overread, exceptread_many_memberwise(see below);the second parameter is named
stream;field names do not start with
_, are not inherited and overridden, and do not collide with the method names;a read path assigns only to declared
Statefields (orstream.cursor); a plain attribute is invisible to the compiled code, and aConstcannot be written.
Implement read_many_memberwise when the reader can appear in a memberwise-stored container (an element of vector<T>, or a key or value of map<K, V>). The default one throws at run time rather than silently reading the wrong bytes.
The compilable subset¶
read, read_many, read_until, and read_many_memberwise are compiled from source, so they must stay within a small subset of Python:
Allowed |
What |
|---|---|
statements |
|
integer and boolean expressions |
|
buffers |
|
stream |
the scalar |
names |
module-level |
Not allowed |
Reason |
|---|---|
imports, |
no bytecode for them; report a bad record with |
dict, set, or list literals, starred expressions |
ditto |
|
run outside the machine |
|
invisible to the compiled code |
chained assignment; |
not compiled |
string ordering ( |
only |
buffer typecodes |
platform-dependent; use a fixed-width code |
A construct outside the subset fails with ReaderContractError (when the class is defined) or UnsupportedNode (when the reader is compiled), naming the method and the offending line. Rewrite the reader, or take the Python + C++ route; there is no fallback for a reader the compiler rejects.
How compilation works¶
flowchart TB
F["Factory"] --> R["detree reader tree"]
R --> C["compile_reader"]
C --> M["Detree module"]
M --> VM["Detree machine"]
VM --> B["buffers and state"]
B --> D["Reader.data()"]
D --> A["awkward content"]
Every backend shares the same Factory: it decides which reader to build and how the raw data becomes an awkward array. Only the byte-level reader differs, so a branch decodes to the same array on every backend.
The reader is declarative. It declares its output buffers, per-basket state, constants, and sub-readers as fields; its read method is ordinary Python source that the compiler turns into bytecode. Nothing in read runs as Python during a read — data does, once per basket, to turn the filled buffers into the tuple, list, or arrays your factory consumes.
Limitations¶
A branch is compiled or it is not¶
There is no fallback to another backend. When a factory cannot build a Detree reader, the first read raises
NotImplementedError: <Factory> does not implement build_detree_reader; use a
different reader backend
The failure is deliberate: a silent switch would make a benchmark, or a bug report, describe a machine that never ran. Select another backend for that read instead.
Note that the failure happens on the first read, not when the file is opened: the interpretation and its awkward form are built before the factory is asked for a reader.
Awkward arrays only¶
All reader backends decode through AsCustom, which requires the awkward library. Reading with library="np" or library="pd" raises
AssertionError: Only awkward arrays are supported
Keep the default (library="ak"), or convert the awkward array afterwards.
The first read pays the compilation¶
Compiling costs far more than reading one basket. The compiled module bytes are kept in two places, so the cost is paid once:
on the interpretation, which compiles once per branch;
in
uproot_custom.detree.cache, keyed by the reader’s structural signature (uproot_custom.detree.compiler.frontend.describe_reader), which is shared by every branch in the process with the same reader structure.
The cache lives for the life of the process and is never evicted.
The reader itself is rebuilt per basket, and a fresh machine is built from the cached module bytes for every run, so arrays returned by an earlier basket are never overwritten by a later one.
Errors¶
Error |
Raised when |
|---|---|
|
a |
|
a compiled method uses a construct outside the subset, or |
|
the selected backend’s |
|
an |
|
a byte count was read that lacks |
|
the read asked for more bytes than the entry holds |
|
the read did not end on the entry’s offset ( |
The last four come from the machine and are subclasses of detree.spec.errors.DetreeError. Under the Python executor, uproot_custom.detree.interp, the same reader raises plain AssertionError and ValueError instead, without the function/ip/cursor fields — that difference is the quickest way to tell which executor produced an error.
See also¶
Write a Detree reader — the guided version of this page.
Debug a reader — the two executors, module dumps, and common problems.
The Python + C++ route — what to do when the subset is too small.