uproot_custom.detree package

Subpackages

Submodules

uproot_custom.detree.bufferproxy module

class uproot_custom.detree.bufferproxy.BufferProxy(typecode: Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'], init=(), *, kind: str = 'data')[source]

Bases: object

adopt(values: ndarray)[source]

Take the array the machine filled, without copying it.

append(value)[source]
property dtype
extend(values)[source]
init
kind
reset()[source]

Empty the buffer, and let go of the storage a previous run filled.

typecode

uproot_custom.detree.builtin module

The built-in readers, written in the detree reader API.

These are the same twelve reads as uproot_custom.python, kept separate on purpose: that module stays an independent implementation of the same format, so a disagreement between the two is a finding rather than a shared assumption. What differs is that a reader here declares its buffers, constants and sub-readers as fields and leaves read free of allocation and of raise, which is what lets the compiler lower it.

Buffers are declared with a struct typecode (“i”, “q”, “d”, …), a NumPy dtype or a dtype string; Buffer normalises all three to one fixed-width typecode and Buf maps it to the array dtype.

Nothing is interpreted at run time: read / read_many / read_until / read_many_memberwise exist only as source for the frontend, and data runs in Python over the buffers the virtual machine filled.

class uproot_custom.detree.builtin.AnyClassReader(name: str, **values)[source]

Bases: Reader

Any object framed by its byte count, which the read must consume exactly.

data()[source]
elements: tuple[Reader, ...]
read(stream: Stream)[source]
read_many_memberwise(stream, count)[source]
class uproot_custom.detree.builtin.AnyPointerReader(name: str, **values)[source]

Bases: Reader

A pointer written by TBuffer::WriteObjectAny.

The tag decides what follows: nothing (a null pointer), a back reference, a new class name, or a new object of a class already named. Class names and object indexes survive across events, so they are declared as state.

data()[source]
read(stream: Stream)[source]
class uproot_custom.detree.builtin.CStyleArrayReader(name: str, **values)[source]

Bases: Reader

A C-style array, sized by the streamer info or by the entry boundary.

data()[source]
read(stream: Stream)[source]
read_many(stream, count)[source]
read_until(stream, end_pos)[source]
class uproot_custom.detree.builtin.EmptyReader(name: str, **values)[source]

Bases: Reader

Reads nothing; used where a member’s value is not wanted.

data()[source]
read(stream: Stream)[source]
class uproot_custom.detree.builtin.GroupReader(name: str, **values)[source]

Bases: Reader

Several readers over one object, in declaration order.

data()[source]
elements: tuple[Reader, ...]
read(stream: Stream)[source]
read_many_memberwise(stream, count)[source]
class uproot_custom.detree.builtin.PrimitiveReader(name: str, **values)[source]

Bases: Reader

A leaf of a built-in type; the value is read into a buffer of its own.

data()[source]
read(stream: Stream)[source]
class uproot_custom.detree.builtin.STLMapReader(name: str, **values)[source]

Bases: Reader

A mapping-like STL container; keys and values are separate buffers.

data()[source]
key: Reader
read(stream: Stream)[source]
read_many(stream, count)[source]
read_until(stream, end_pos)[source]
value: Reader
with_header: bool
class uproot_custom.detree.builtin.STLSeqReader(name: str, **values)[source]

Bases: Reader

A sequence-like STL container (vector, list, set, …).

data()[source]
element: Reader
read(stream: Stream)[source]
read_many(stream, count)[source]
read_until(stream, end_pos)[source]
with_header: bool
class uproot_custom.detree.builtin.STLStringReader(name: str, **values)[source]

Bases: Reader

A std::string, which keeps its size before the characters.

data()[source]
read(stream: Stream)[source]
read_many(stream, count)[source]
read_until(stream, end_pos)[source]
with_header: bool
class uproot_custom.detree.builtin.TArrayReader(name: str, **values)[source]

Bases: Reader

A TArray/std::array: a uint32 size then a bulk read.

data()[source]
read(stream: Stream)[source]
class uproot_custom.detree.builtin.TObjectReader(name: str, **values)[source]

Bases: Reader

The TObject base, read for its header and (optionally) its bytes.

data()[source]
read(stream: Stream)[source]
class uproot_custom.detree.builtin.TStringReader(name: str, **values)[source]

Bases: Reader

A length-prefixed byte string: TString, or a header-less one.

data()[source]
read(stream: Stream)[source]
read_many(stream, count)[source]
read_until(stream, end_pos)[source]
with_header: bool

uproot_custom.detree.cache module

uproot_custom.detree.cache.clear()[source]
uproot_custom.detree.cache.get(signature: str) → bytes | None[source]
uproot_custom.detree.cache.put(signature: str, codes: bytes) → bytes[source]
uproot_custom.detree.cache.size()[source]

uproot_custom.detree.decl module

class uproot_custom.detree.decl.Buffer(type: Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'] | dtype | type | str | None = None, *, init=(), from_const: str | None = None, kind: Literal['data', 'offsets'] = 'data')[source]

Bases: _Field

A typed buffer that a read path appends to.

The element type is a struct typecode ("i"), a NumPy dtype (np.int32, np.dtype("i4")) or a dtype string ("int32"); all forms normalise to the fixed-width typecode the machine stores.

resolve_typecode(reader) → Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'][source]

The normalised typecode of this buffer in reader, after its constants are set.

A reader whose element type is a constructor argument (a primitive, a TArray) names that Const field in from_const, so one class serves every dtype. The constant has to be declared before the buffer, which is also the order the fields are initialised in.

class uproot_custom.detree.decl.Child[source]

Bases: _Field

Standalone child field.

class uproot_custom.detree.decl.ChildDict[source]

Bases: _Field

Dictionary of child fields, not enabled yet.

class uproot_custom.detree.decl.ChildList[source]

Bases: _Field

List of child fields.

class uproot_custom.detree.decl.Const(default=<object object>, *, type=None, choices=None, ge=None, le=None)[source]

Bases: _Field

coerce(name: str, value)[source]

Coerce the given value to the expected type, and check its bounds.

class uproot_custom.detree.decl.Reader(name: str, **values)[source]

Bases: object

property buffers
property children
data()[source]
read(stream: Stream)[source]
read_many(stream: Stream, count: int)[source]
read_many_memberwise(stream: Stream, count: int)[source]
read_until(stream: Stream, end_pos: int)[source]
reset()[source]
exception uproot_custom.detree.decl.ReaderContractError[source]

Bases: TypeError

A reader class violates the contract reader enforces.

class uproot_custom.detree.decl.State(type, init=None)[source]

Bases: _Field

property is_string
uproot_custom.detree.decl.reader(cls)[source]

Class decorator enforcing the reader contract.

uproot_custom.detree.interp module

Run a detree reader tree in Python, without the virtual machine.

The reader’s read / read_many / read_until / read_many_memberwise are ordinary Python methods; uproot_custom.detree.compiler.frontend only compiles them so the machine can execute them faster. This module runs them directly, through uproot_custom.detree.stream.Stream, which implements the same operations as the machine’s opcodes.

Two uses:

  • debugging a reader – print, pdb and a Python traceback all work;

  • a second opinion on the machine and on the compiler – the three executors (uproot_custom.python, this one, and the VM) must agree on every basket.

State needs no write-back here: the reader’s own attributes are the state.

uproot_custom.detree.interp.read_basket(reader: Reader, data: bytes, offsets: ndarray | None, cursor_offset=0, nbyte=None)[source]

Run reader’s methods over one basket; return the reader.

The reader is uproot_custom.detree.decl.Reader.reset first, so one tree reads one basket after another and the arrays an earlier call returned keep their values.

The cursor runs on across events, exactly as it does in the machine and in uproot_custom.python.read_data, and each event must end on its own entry offset.

Parameters:
  • reader – the detree reader tree to run.

  • data – the basket bytes.

  • offsets – the entry offsets, or None to derive them from nbyte.

  • cursor_offset – the basket’s fKeylen, which is what uproot passes to a backend. An object reference is keyed by its position within the file (pos + fKeylen + kMapOffset), so a pointer member reads the wrong bytes when this is 0 – silently, without an error.

  • nbyte – the fixed entry size, used when offsets is None.

Returns:

The reader, holding the buffers and states of the basket just read.

uproot_custom.detree.interp.read_data(reader: Reader, data: bytes, offsets: ndarray | None, cursor_offset=0, nbyte=None)[source]

Read one basket in Python and return whatever reader.data() returns.

Parameters:
  • reader – the detree reader tree to run.

  • data – the basket bytes.

  • offsets – the entry offsets, or None to derive them from nbyte.

  • cursor_offset – the basket’s fKeylen.

  • nbyte – the fixed entry size, used when offsets is None.

Returns:

Whatever reader.data() returns.

uproot_custom.detree.run module

What compiling produces, and the one seam that runs it.

The frontend compiles a reader tree to detree-vm IR and detree.compile turns it into canonical module bytes. Those bytes are the artefact this module caches; a detree.Machine is built for every run, because a machine holds the mutable per-basket state and is not thread-safe.

detree.Machine.run resets the machine it is called on, so a caller that keeps a machine can serve basket after basket; the read path here builds a fresh one instead, which keeps concurrent readers independent without a thread-local cache.

uproot_custom.detree.run.apply_outputs(reader: Reader, outputs: dict, states=None, path='root')[source]

Write the buffers the machine filled back into the reader and its children.

A native buffer adopts the array without copying. states is the {name: value} mapping the machine returns; when given, the Python side of a reader ends up with the values the run ended with.

uproot_custom.detree.run.backend_name()[source]

Which detree-vm machine run selects.

Detree-VM’s official release ships the Rust machine only. The value comes from uproot_custom.detree.get_impl, an internal test helper; the environment variable it reads is only for development.

uproot_custom.detree.run.compiled_for_reader(reader: Reader) → bytes[source]

Return the cached compiled bytes of a reader tree, compiling it on first use.

uproot_custom.detree.run.core()[source]

detree-vm when it has a compiled backend, or None.

The Python reference machine is always available; this probe is only used to decide whether a compiled machine can be exercised in tests and benchmarks.

uproot_custom.detree.run.entry_offsets(data, offsets, nbyte=None)[source]

The entry offsets of a basket, derived from nbyte when absent.

A fixed-width branch has no byte_offsets; every entry is then the same size and the offsets are just i * nbyte. Every basket of a branch but the last has the same length, so the array is built once per length.

uproot_custom.detree.run.read_data(reader: Reader, data: bytes | ndarray, offsets: ndarray | None, cursor_offset: int = 0, nbyte: int | None = None)[source]

Read one basket: compile reader, run it, hand the buffers back.

cursor_offset is the basket’s fKeylen, as uproot passes it; an object reference is keyed by its position within the file, so a pointer member reads the wrong bytes when this is 0.

The reader is reset first, exactly as uproot_custom.detree.interp does, so one tree reads basket after basket on either executor and the arrays a previous call returned stay valid.

Parameters:
  • reader – the detree reader tree to compile and run.

  • data – the basket bytes.

  • offsets – the entry offsets, or None to derive them from nbyte.

  • cursor_offset – the basket’s fKeylen.

  • nbyte – the fixed entry size, used when offsets is None.

Returns:

Whatever reader.data() returns; the caller turns it into an awkward array, usually through the factory that built the reader.

uproot_custom.detree.run.run(code: bytes, data: bytes | ndarray, offsets: ndarray | None, cursor_offset: int = 0, nbyte: int | None = None)[source]

Run code over one basket; return (buffers, states).

buffers is {declared name: 1-D array} and states is {declared name: int | str | None} (a name slot comes back decoded from the string arena, None when it is empty).

A fresh detree.Machine is built for every call, so two threads may run the same module at the same time without sharing registers or buffers.

Parameters:
  • code – the compiled reader bytes to run.

  • data – the basket bytes.

  • offsets – the entry offsets, or None to derive them from nbyte.

  • cursor_offset – the basket’s fKeylen.

  • nbyte – the fixed entry size, used when offsets is None; a fixed-width branch is then i * nbyte per event.

Returns:

(buffers, states) as described above.

uproot_custom.detree.stream module

class uproot_custom.detree.stream.Stream(data: bytes, offsets: list[int], initial_cursor_position: int = 0)[source]

Bases: object

A byte buffer with a cursor, the entry offsets and the object references.

check_class_name(expected: str, actual: str)[source]
property entries
has_ref(tag)[source]
next_offset_after(pos: int)[source]

Smallest entry offset strictly greater than pos.

read_bool()[source]
read_bulk(buffer: BufferProxy, count: int)[source]

Read count values of buffer’s typecode, in one extend.

read_double()[source]
read_fNBytes()[source]
read_fVersion()[source]
read_float()[source]
read_int16()[source]
read_int32()[source]
read_int64()[source]
read_int8()[source]
read_into(buffer: BufferProxy)[source]

Read one value of buffer’s own dtype and append it.

read_null_terminated_string()[source]
read_uint16()[source]
read_uint32()[source]
read_uint64()[source]
read_uint8()[source]
ref_class_name(tag)[source]

The class name of a class reference; None otherwise.

ref_count()[source]
ref_object_index(tag)[source]

The object index of an object reference; -1 for a class one.

refs

tag -> ("object", index) or ("class", name)

set_class_ref(tag, class_name: str)[source]
set_object_ref(tag, object_index: int)[source]
skip(nbytes)[source]
skip_TObject()[source]
skip_fNBytes()[source]
skip_fVersion()[source]
skip_null_terminated_string()[source]
skip_obj_header()[source]

Module contents

Detree: the reader DSL, its compiler, and the detree-vm execution seam.

  • uproot_custom.detree.decl + uproot_custom.detree.builtin – the declarative readers the frontend compiles, and the built-in implementations of the ROOT types;

  • uproot_custom.detree.compiler.frontend – compile_reader builds detree-vm IR and hands it to detree.compile for optimization and lowering;

  • uproot_custom.detree.run – caches the compiled module bytes per reader structure, builds a machine for one basket, and writes its buffers back into the reader.

uproot_custom.detree.interp is the Python executor of the same reader methods, used for debugging and as an oracle in the differential tests.

get_impl, set_impl and use_impl exist for internal tests only and are not part of the public API; Detree-VM’s official release ships the Rust machine only.

class uproot_custom.detree.Buffer(type: Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'] | dtype | type | str | None = None, *, init=(), from_const: str | None = None, kind: Literal['data', 'offsets'] = 'data')[source]

Bases: _Field

A typed buffer that a read path appends to.

The element type is a struct typecode ("i"), a NumPy dtype (np.int32, np.dtype("i4")) or a dtype string ("int32"); all forms normalise to the fixed-width typecode the machine stores.

resolve_typecode(reader) → Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'][source]

The normalised typecode of this buffer in reader, after its constants are set.

A reader whose element type is a constructor argument (a primitive, a TArray) names that Const field in from_const, so one class serves every dtype. The constant has to be declared before the buffer, which is also the order the fields are initialised in.

class uproot_custom.detree.BufferProxy(typecode: Literal['b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'], init=(), *, kind: str = 'data')[source]

Bases: object

adopt(values: ndarray)[source]

Take the array the machine filled, without copying it.

append(value)[source]
property dtype
extend(values)[source]
init
kind
reset()[source]

Empty the buffer, and let go of the storage a previous run filled.

typecode
class uproot_custom.detree.Child[source]

Bases: _Field

Standalone child field.

class uproot_custom.detree.ChildList[source]

Bases: _Field

List of child fields.

class uproot_custom.detree.Const(default=<object object>, *, type=None, choices=None, ge=None, le=None)[source]

Bases: _Field

coerce(name: str, value)[source]

Coerce the given value to the expected type, and check its bounds.

class uproot_custom.detree.Reader(name: str, **values)[source]

Bases: object

property buffers
property children
data()[source]
read(stream: Stream)[source]
read_many(stream: Stream, count: int)[source]
read_many_memberwise(stream: Stream, count: int)[source]
read_until(stream: Stream, end_pos: int)[source]
reset()[source]
class uproot_custom.detree.State(type, init=None)[source]

Bases: _Field

property is_string
class uproot_custom.detree.Stream(data: bytes, offsets: list[int], initial_cursor_position: int = 0)[source]

Bases: object

A byte buffer with a cursor, the entry offsets and the object references.

check_class_name(expected: str, actual: str)[source]
property entries
has_ref(tag)[source]
next_offset_after(pos: int)[source]

Smallest entry offset strictly greater than pos.

read_bool()[source]
read_bulk(buffer: BufferProxy, count: int)[source]

Read count values of buffer’s typecode, in one extend.

read_double()[source]
read_fNBytes()[source]
read_fVersion()[source]
read_float()[source]
read_int16()[source]
read_int32()[source]
read_int64()[source]
read_int8()[source]
read_into(buffer: BufferProxy)[source]

Read one value of buffer’s own dtype and append it.

read_null_terminated_string()[source]
read_uint16()[source]
read_uint32()[source]
read_uint64()[source]
read_uint8()[source]
ref_class_name(tag)[source]

The class name of a class reference; None otherwise.

ref_count()[source]
ref_object_index(tag)[source]

The object index of an object reference; -1 for a class one.

refs

tag -> ("object", index) or ("class", name)

set_class_ref(tag, class_name: str)[source]
set_object_ref(tag, object_index: int)[source]
skip(nbytes)[source]
skip_TObject()[source]
skip_fNBytes()[source]
skip_fVersion()[source]
skip_null_terminated_string()[source]
skip_obj_header()[source]
uproot_custom.detree.reader(cls)[source]

Class decorator enforcing the reader contract.