C++ API reference¶
This section documents the C++ API provided by the uproot_custom module. Any downstream project that wants to use the C++ functionalities provided by uproot_custom should include the header file uproot-custom.hh.
Defines
-
IMPORT_UPROOT_CUSTOM_CPP¶
Macro to import the uproot_custom.cpp module.
Note
This macro should be used in the
PYBIND11_MODULEdefinition of your module.
-
namespace uproot_custom¶
Typedefs
Shortcut for shared pointer to IReader.
Note
When a reader requires another reader as a member, it must use
std::shared_ptr<IReader>to properly handle lifetime management.
Functions
Helper function to create a shared pointer to a reader. Pybind11 requires shared_ptr to handle object lifetime correctly.
- Template Parameters:
ReaderType – The type of the reader.
Args – The argument types for the reader constructor.
- Parameters:
args – The arguments for the reader constructor.
- Returns:
The shared pointer to the created reader.
-
template<typename ReaderType, typename ...Args>
void declare_reader(py::module &m, const char *name)¶ Helper function to declare a reader class in a pybind11 module. Automatically wraps the class’ constructor to return a shared_ptr. User should always use this function to declare reader classes.
- Template Parameters:
ReaderType – The type of the reader.
Args – The argument types for the reader constructor.
- Parameters:
m – The declaring pybind11 module.
name – The name of the reader class in Python.
Convert a shared pointer to a std::vector<T> to a numpy array without copying. User can use this function to return numpy arrays from reader’s data() method.
- Template Parameters:
T – The element type of the vector.
- Parameters:
seq – The shared pointer to the std::vector<T>.
- Returns:
The numpy array wrapping the vector data.
-
template<typename ...Args>
inline void debug_printf(const char *msg, Args... args)¶ Debug print function. Prints only when environment variable with name
UPROOT_CUSTOM_DEBUGis defined. Use this function likeprintf(). DO NOT use this function inread()methods of the reader, since it may significantly slow down the reading process due to frequently retrieving the environment variable.- Template Parameters:
Args – Argument types. No need to specify explicitly.
- Parameters:
msg – The format string.
args – Arguments to format.
-
inline void debug_printf(uproot_custom::BinaryStream &stream, size_t n = 100)¶
Debug print function for BinaryStream. Prints only when environment variable with name
UPROOT_CUSTOM_DEBUGis defined. Call BinaryStream::debug_print() internally.- Parameters:
stream – The BinaryStream to print.
n – Number of bytes to print.
Variables
-
constexpr uint32_t kNewClassTag = 0xFFFFFFFF¶
-
constexpr uint32_t kClassMask = 0x80000000¶
-
constexpr uint32_t kByteCountMask = 0x40000000¶
-
constexpr uint32_t kMaxMapCount = 0x3FFFFFFE¶
-
constexpr uint16_t kByteCountVMask = 0x4000¶
-
constexpr uint16_t kMaxVersion = 0x3FFF¶
-
constexpr int32_t kMapOffset = 2¶
-
constexpr uint16_t kStreamedMemberWise = 1 << 14¶
-
class BinaryStream¶
- #include <uproot-custom.hh>
A binary stream reader that wraps numpy arrays for efficient ROOT binary parsing.
Provides endianness-aware reading of primitive types and manages references between objects during deserialization.
Public Types
-
enum EStatusBits¶
Status bits for TObject, used to track object state flags.
Values:
-
enumerator kCanDelete¶
if object in a list can be deleted
-
enumerator kMustCleanup¶
if object destructor must call RecursiveRemove()
-
enumerator kIsReferenced¶
if object is referenced by a TRef or TRefArray
-
enumerator kHasUUID¶
if object has a TUUID (its fUniqueID=UUIDNumber)
-
enumerator kCannotPick¶
if object in a pad cannot be picked
-
enumerator kNoContextMenu¶
if object does not want context menu
-
enumerator kInvalidObject¶
if object ctor succeeded but object should not be used
-
enumerator kCanDelete¶
Public Functions
-
inline BinaryStream(py::array_t<uint8_t> data, py::array_t<uint32_t> offsets, uint32_t initial_cursor_position)¶
Construct a BinaryStream from numpy arrays.
- Parameters:
data – A numpy array of uint8_t containing the raw data.
offsets – A numpy array of uint32_t containing the offsets for each entry.
initial_cursor_position – Initial cursor position offset for the stream.
-
template<typename T>
inline const T read()¶ Read a value of type T from the stream, handling endianness.
- Template Parameters:
T – The type to read.
- Returns:
The value read from the stream.
-
inline const int16_t read_fVersion()¶
Read the fVersion field from the stream.
- Returns:
The fVersion value
-
inline const uint32_t read_fNBytes()¶
Read the fNBytes field from the stream, checking the byte count mask.
- Throws:
std::runtime_error – if the byte count mask is not set.
- Returns:
The fNBytes value without the mask.
-
inline const std::string read_null_terminated_string()¶
Read a null-terminated (
\0) string from the stream.- Returns:
The string read from the stream.
-
inline const std::string read_obj_header()¶
Read an object’s header from the stream. The object header has
fNBytes,fVersion,fTag. IffTag == kNewClassTag, then a null-terminated class name follows.- Returns:
The class name if the object is a new class, empty string otherwise.
-
inline const std::string read_TString()¶
Read a TString from the stream. A TString has
length(uint8_t). Iflength == 255, then thelengthis a following uint32_t. Then followinglengthbytes of string data.- Returns:
The TString data read from the stream, as a std::string.
-
inline void skip(const size_t n)¶
Skip
nbytes in the stream.- Parameters:
n – Number of bytes to skip.
-
inline void skip_fNBytes()¶
Skip the fNBytes field. Equivalent to read_fNBytes() but does not return the value, since the mask need to be checked.
-
inline void skip_fVersion()¶
Skip the fVersion field.
-
inline void skip_null_terminated_string()¶
Skip a null-terminated (
\0) string in the stream.
-
inline void skip_obj_header()¶
Skip an object’s header in the stream. The object header has
fNBytes,fVersion,fTag. IffTag == kNewClassTag, then a null-terminated class name follows.
-
inline void skip_TObject()¶
Skip a TObject in the stream. A TObject has
fVersion(2 bytes),fUniqueID(4 bytes),fBits(4 bytes). IffBits & kIsReferenced, then apidf(2 bytes) follows.
-
inline CountedRegion check_nbytes()¶
Open a counted region for the enclosing scope.
- Returns:
The guard; bind it, e.g.
auto frame = stream.check_nbytes();.
-
inline void check_nbytes_start()¶
Read a masked
fNBytesand remember where the counted region ends.The call opens a frame and check_nbytes_end() closes it. Frames nest, so a sub-reader that is framed itself does not disturb its caller.
-
inline void check_nbytes_end()¶
Fail unless the cursor is at the end of the innermost counted region.
- Throws:
std::runtime_error – if there is no open region, or the cursor is elsewhere.
-
inline void check_nbytes_end(const uint8_t *expected)¶
Fail unless the cursor is at
expected.For a region whose size the caller computed instead of reading; the innermost frame is then left open, so the two forms do not pair with each other.
- Parameters:
expected – The expected cursor position, as returned by get_cursor().
- Throws:
std::runtime_error – if the cursor is elsewhere.
-
inline size_t open_frames() const¶
Get the number of counted regions still waiting for their end.
-
inline const uint8_t *get_data() const¶
Get the raw data pointer.
-
inline const uint8_t *get_cursor() const¶
Get the current cursor pointer.
-
inline const uint32_t *get_offsets() const¶
Get the entry offsets array pointer.
-
inline const uint32_t get_index() const¶
Get the index object.
- Returns:
const uint32_t
-
inline const uint32_t get_initial_cursor_offset() const¶
Get the initial cursor offset, which is used for calculating relative offsets.
- Returns:
const uint32_t
-
inline std::map<uint32_t, Reference> &get_refs()¶
Get the references map, which is used for pointer reading.
- Returns:
The references map.
-
inline const uint64_t entries() const¶
Get the number of entries.
-
inline void debug_print(size_t n = 100) const¶
Debug print the next
nbytes from the current cursor.- Parameters:
n – Number of bytes to print.
Private Functions
-
inline void abandon_nbytes()¶
Drop the innermost counted region without checking it: the block raised.
Private Members
-
uint8_t *m_cursor¶
current cursor position
-
const uint64_t m_entries¶
number of entries
-
const uint8_t *m_data¶
raw data pointer
-
const uint32_t *m_offsets¶
entry offsets pointer
-
const uint32_t m_initial_cursor_offset¶
initial cursor position, used for calculating relative offsets
-
const bool m_debug_print¶
whether to print debug information
-
std::vector<const uint8_t*> m_nbytes_marks¶
End positions of the open counted regions, innermost last.
-
struct RefCls¶
- #include <uproot-custom.hh>
Reference to a class by name.
Public Members
-
std::string name¶
The name of the referenced class.
-
std::string name¶
-
struct RefObj¶
- #include <uproot-custom.hh>
Reference to an object by index.
Public Members
-
int64_t index¶
The index of the referenced object.
-
int64_t index¶
-
class CountedRegion¶
- #include <uproot-custom.hh>
A counted region, checked when the enclosing scope ends.
The C++ form of Python’s
with stream.check_nbytes():: bind the guard and the region is checked at the end of the block.auto frame = stream.check_nbytes(); // ... members ...
A mismatch throws
std::runtime_errorfrom the destructor, which is why it isnoexcept( false ). A block that raised is dropped without a second exception, so the original error survives.Public Functions
-
CountedRegion(const CountedRegion&) = delete¶
-
CountedRegion &operator=(const CountedRegion&) = delete¶
-
inline CountedRegion(CountedRegion &&other) noexcept¶
-
inline ~CountedRegion()¶
Private Functions
-
inline explicit CountedRegion(BinaryStream &stream)¶
Private Members
-
BinaryStream *m_stream¶
the stream the region belongs to
-
bool m_active = true¶
false once moved from, or checked
-
int m_uncaught¶
uncaught exceptions when the block started
Friends
- friend class BinaryStream
-
CountedRegion(const CountedRegion&) = delete¶
-
enum EStatusBits¶
-
class IReader¶
- #include <uproot-custom.hh>
Interface for element readers. All element readers must inherit from this class.
Subclassed by uproot_custom::AnyClassReader, uproot_custom::AnyPointerReader, uproot_custom::CStyleArrayReader, uproot_custom::EmptyReader, uproot_custom::GroupReader, uproot_custom::PrimitiveReader< T >, uproot_custom::STLMapReader, uproot_custom::STLSeqReader, uproot_custom::STLStringReader, uproot_custom::TArrayReader< T >, uproot_custom::TObjectReader, uproot_custom::TStringReader
Public Functions
-
inline IReader(std::string name)¶
Construct a new IReader object.
- Parameters:
name – Name of the reader.
-
virtual ~IReader() = default¶
-
inline virtual const std::string name() const¶
Get the name of the reader.
- Returns:
Name of the reader.
-
virtual void read(BinaryStream &stream) = 0¶
Read an element from the stream.
- Parameters:
stream – The binary stream to read from.
-
virtual py::object data() const = 0¶
Get the data read by the reader. This should be called after the whole reading process.
- Returns:
The data read by the reader.
-
inline virtual uint32_t read_many(BinaryStream &stream, const int64_t count)¶
Read multiple elements from the stream in one go. Repeatedly calls read() by default.
Note
When multiple elements are stored together, some classes may have “one common
header + multiple data objects” format. This method can be overridden to handle such cases more efficiently.
- Parameters:
stream – The binary stream to read from.
count – Number of elements to read.
- Returns:
Number of elements read.
-
inline virtual uint32_t read_until(BinaryStream &stream, const uint8_t *end_pos)¶
Read elements from the stream until reaching the end position. Repeatedly calls read() method by default.
Note
When multiple elements are stored together, some classes may have “one common
header + multiple data objects” format. This method can be overridden to handle such cases more efficiently.
- Parameters:
stream – The binary stream to read from.
end_pos – The end position pointer.
- Returns:
Number of elements read.
-
inline virtual uint32_t read_many_memberwise(BinaryStream &stream, const int64_t count)¶
Read multiple elements from the stream in member-wise fashion. Readers that need to handle member-wise data reading must implement this method.
- Parameters:
stream – The binary stream to read from.
count – Number of elements to read.
- Returns:
Number of elements read.
Protected Functions
-
template<typename ...Args>
inline void debug_printf(const char *msg, Args... args) const¶ Print a debug message if debugging is enabled.
- Parameters:
msg – printf-style format string.
args – Arguments forwarded to printf.
-
inline void debug_printf(uproot_custom::BinaryStream &stream, const size_t n = 100)¶
Print the next
nbytes of the stream if debugging is enabled.- Parameters:
stream – The stream whose bytes are dumped.
n – Number of bytes to print.
-
inline IReader(std::string name)¶
-
namespace uproot¶