Type Casting Utilities

Template helpers for casting between C++ STL containers and PyTorch tensors, and for merging containers across threads.

cproperty<T, G> — Observable Property

cproperty is a thin property-like wrapper used throughout the C++ class hierarchy. It stores a value of type T and optionally calls a user-supplied getter or setter callback (of type void(T*, G*)) on each read or write. It models operator=(T) and implicit operator T(), so it behaves like a plain data member at call sites.

template<typename T, typename G>
class cproperty

Typed property with optional setter and getter callbacks.

Wraps a value of type T and optionally calls user-provided static (or std::function) setter and getter functions on assignment and conversion.

Registrationset_setter/ register the callbacks; set_object sets the owner pointer that is passed as the second argument to each callback.

Assignmentoperator=(const T& val) stores the value and, if a setter is registered, immediately calls setter(&data, obj). This allows side-effects such as keeping Cartesian and polar four-momentum consistent in particle_template.

Reading — the implicit conversion operator T() and the address-of operator&() both invoke getter(&data, obj) if registered before returning the value. This allows lazy computation of derived quantities such as hash or mass.

The clear() method resets data to T() only when a getter is registered (indicating the value is always computed on demand). The comparison operators operator== and operator!= test against the stored data without invoking any callback.

Template Parameters:
  • T – Value type stored by the property.

  • G – Owner class type. A pointer to the owner is passed to the setter/getter so they can read or update related state.

Public Functions

inline cproperty()

Default constructor. Initialises the wrapped value with T().

inline void set_setter(std::function<void(T*, G*)> c = x_setter<T, G>)

Register a setter callback.

Parameters:

c – Function called with a pointer to the internal value and a pointer to the owner object each time the property is assigned.

inline void set_getter(std::function<void(T*, G*)> c = x_getter<T, G>)

Register a getter callback.

Parameters:

c – Function called with a pointer to the internal value and a pointer to the owner object each time the property is read.

inline void set_object(G *_obj)

Set the owner object pointer passed to the callbacks.

Parameters:

_obj – Pointer to the owning object.

inline cproperty &operator=(const T &val)

Assign a new value and invoke the setter callback if registered.

Parameters:

val – New value.

Returns:

Reference to *this.

inline T operator+(const T &val)

Return the sum of the stored value and val.

Parameters:

val – Value to add.

Returns:

Sum.

inline bool operator==(const T &val)

Compare the stored value with val for equality.

Parameters:

val – Value to compare.

Returns:

true if equal.

inline bool operator!=(const T &val)

Compare the stored value with val for inequality.

Parameters:

val – Value to compare.

Returns:

true if not equal.

inline operator T()

Implicit conversion to T. Invokes the getter callback if registered.

Returns:

Current value.

inline T *operator&()

Address-of operator. Invokes the getter callback if registered and returns a pointer to the internal value.

Returns:

Pointer to the stored value.

inline void clear()

Reset the stored value to T() if a getter is registered.

merge_cast.h — Thread-safe Merge Helpers

Recursive template functions for merging two data structures of the same type (vectors, maps, or scalars) element-wise. Used in multi-threaded compilation to coalesce per-thread results into a single output.

  • merge_data(out, p2) — append / overwrite *p2 into *out

  • sum_data(out, p2) — add *p2 to *out (numeric accumulation)

  • reserve_count(inp, ix) — count elements for pre-allocation

Template utilities for merging, summing, and contracting typed data structures.

Provides recursive template free functions that operate on scalars, vectors, and maps uniformly.

Functions

template<typename G>
void merge_data(std::vector<G> *out, std::vector<G> *p2)

Append all elements of p2 to out.

Template Parameters:

G – Element type.

Parameters:
  • out – Target vector.

  • p2 – Source vector.

template<typename G>
void merge_data(G *out, G *p2)

Copy p2 into out (scalar overload).

Template Parameters:

G – Scalar type.

Parameters:
  • out – Target.

  • p2 – Source.

template<typename g, typename G>
void merge_data(std::map<g, G> *out, std::map<g, G> *p2)

Recursively merge map p2 into out.

Template Parameters:
  • g – Key type.

  • G – Value type.

Parameters:
  • out – Target map.

  • p2 – Source map.

template<typename G>
void sum_data(G *out, G *p2)

Add p2 to out in place (scalar overload: out += p2).

Template Parameters:

G – Scalar type.

Parameters:
  • out – Target.

  • p2 – Value to add.

template<typename G>
void sum_data(std::vector<G> *out, std::vector<G> *p2)

Append p2 to out (vector overload, equivalent to merge).

Template Parameters:

G – Element type.

Parameters:
  • out – Target vector.

  • p2 – Source vector.

template<typename g, typename G>
void sum_data(std::map<g, G> *out, std::map<g, G> *p2)

Recursively sum map p2 into out.

Template Parameters:
  • g – Key type.

  • G – Value type.

Parameters:
  • out – Target map.

  • p2 – Source map.

template<typename g>
void reserve_count(g *inp, long *ix)

Increment ix by 1 (scalar overload).

Template Parameters:

g – Scalar type.

Parameters:
  • inp – Unused.

  • ix – Counter to increment.

template<typename g>
void reserve_count(std::vector<g> *inp, long *ix)

Recursively count all leaf elements in inp.

Template Parameters:

g – Element type.

Parameters:
  • inp – Nested vector.

  • ix – Counter accumulator.

template<typename g>
void contract_data(std::vector<g> *out, g *p2)

Append the scalar p2 to out.

Template Parameters:

g – Scalar type.

Parameters:
  • out – Target vector.

  • p2 – Scalar to append.

template<typename g>
void contract_data(std::vector<g> *out, std::vector<g> *p2)

Append all elements of p2 to out.

Template Parameters:

g – Element type.

Parameters:
  • out – Target vector.

  • p2 – Source vector.

template<typename g>
void contract_data(std::vector<g> *out, std::vector<std::vector<g>> *p2)

Flatten the nested vector p2 into out.

Template Parameters:

g – Element type.

Parameters:
  • out – Target flat vector.

  • p2 – Nested source vector.

template<typename g>
void release_vector(std::vector<g> *ipt)

Call shrink_to_fit on ipt to release excess capacity.

Template Parameters:

g – Element type.

Parameters:

ipt – Vector to shrink.

tensor_cast.h — STL Container ↔ Tensor Conversion

Recursive template system for converting arbitrarily nested std::vector<std::vector<…<T>…>> into flat torch::Tensor buffers with automatic dimension tracking and null-padding for ragged tensors.

Key public functions:

  • build_tensor(data, scalar_type, prim, opts) — convert a nested vector to a padded tensor

  • tensor_to_vector(tensor, out) — round-trip back to a nested vector

Template utilities for converting nested C++ vectors to torch::Tensor objects with padding.

Provides a family of recursive template functions that determine the maximum dimension of a ragged nested vector, pad shorter rows with sentinel values, flatten the data into a contiguous array, and wrap it in a torch::Tensor.

Functions

template<typename g>
void scout_dim(g*, int*)

Base case: scalar — does nothing.

Template Parameters:

g – Scalar type.

template<typename g>
void nulls(g *d, int*)

Base case: fill scalar sentinel — sets d to -1.

Template Parameters:

g – Scalar type.

Parameters:

d – Pointer to the scalar to reset.

template<typename g>
bool standard(g*, int*)

Base case: scalar is already “standard” — returns true.

Template Parameters:

g – Scalar type.

template<typename G, typename g>
void as_primitive(G *data, std::vector<g> *lin, std::vector<signed long>*, unsigned int)

Base case: append scalar data to the linearised vector lin.

Template Parameters:
  • G – Source type.

  • g – Target primitive type.

Parameters:
  • data – Pointer to the scalar.

  • lin – Linear output buffer.

template<typename G>
void scout_dim(const std::vector<G> *vec, int *mx_dim)

Determine the maximum length at the first nesting level of vec.

Template Parameters:

G – Element type (may itself be a vector for deeper nesting).

Parameters:
  • vec – Pointer to the vector to inspect.

  • mx_dim – Running maximum dimension; updated in place.

template<typename g>
void nulls(const std::vector<g> *d, int *mx_dim)

Pad the vector d with default-constructed elements until it reaches mx_dim entries.

Template Parameters:

g – Element type.

Parameters:
  • d – Pointer to the vector to pad.

  • mx_dim – Target size.

template<typename g>
bool standard(const std::vector<g> *vec, int *mx_dim)

Recursively ensure that vec and all sub-vectors are padded to mx_dim.

Template Parameters:

g – Element type.

Parameters:
  • vec – Pointer to the vector to normalise.

  • mx_dim – Target size.

Returns:

Always false (padding was applied).

template<typename G, typename g>
static void as_primitive(std::vector<G> *data, std::vector<g> *linear, std::vector<signed long> *dims, unsigned int depth = 0)
template<typename G, typename g>
static torch::Tensor build_tensor(std::vector<G> *_data, at::ScalarType _op, g, torch::TensorOptions *op)

Convert a (possibly ragged) nested vector to a padded torch::Tensor.

Calls scout_dim, standard, and as_primitive internally.

Template Parameters:
  • G – Outer element type.

  • g – Leaf primitive type.

Parameters:
  • _data – Source nested vector.

  • _op – Scalar type for the tensor (e.g. torch::kFloat32).

  • op – Tensor options (device, dtype overridden by _op).

Returns:

Padded torch::Tensor with dimensions derived from _data.

vector_cast.h — Variable Storage and Vector Casting

Provides variable_t (a polymorphic type-erased buffer for a single named ROOT leaf column) used by data_t and bsc_t.

struct variable_t : public bsc_t

Typed ROOT TBranch write buffer derived from bsc_t.

Stores a typed pointer and creates/fills a TBranch on the first write. Supports both tensor data (via build_switch) and raw typed pointers.

Public Functions

variable_t()

Default constructor.

variable_t(bool use_external)

Construct with an explicit flag for external buffer management.

Parameters:

use_external – If true, data pointers are managed externally.

~variable_t() override

Destructor.

void create_meta(meta_t *mt)

Associate metadata mt with this variable for the ROOT file.

Parameters:

mt – Pointer to the metadata struct.

void build_switch(size_t s, torch::Tensor *tx)

Select the appropriate typed process overload based on the tensor’s dtype and call it.

Parameters:
  • s – Branch index hint.

  • tx – Pointer to the tensor to write.

void process(torch::Tensor *data, std::string *varname, TTree *tr)

Write data to the ROOT branch named varname in tree tr.

Parameters:
  • data – Source tensor.

  • varname – Branch name.

  • tr – Root TTree.

void process(std::vector<std::vector<float>> *data, std::string *varname, TTree *tr)

Write a 2D float vector to the ROOT branch.

void process(std::vector<std::vector<double>> *data, std::string *varname, TTree *tr)
void process(std::vector<std::vector<long>> *data, std::string *varname, TTree *tr)
void process(std::vector<std::vector<int>> *data, std::string *varname, TTree *tr)
void process(std::vector<std::vector<bool>> *data, std::string *varname, TTree *tr)
void process(std::vector<float> *data, std::string *varname, TTree *tr)

Write a 1D float vector to the ROOT branch.

void process(std::vector<double> *data, std::string *varname, TTree *tr)
void process(std::vector<long> *data, std::string *varname, TTree *tr)
void process(std::vector<int> *data, std::string *varname, TTree *tr)
void process(std::vector<bool> *data, std::string *varname, TTree *tr)
void process(float *data, std::string *varname, TTree *tr)

Write a scalar float to the ROOT branch.

void process(double *data, std::string *varname, TTree *tr)
void process(long *data, std::string *varname, TTree *tr)
void process(int *data, std::string *varname, TTree *tr)
void process(bool *data, std::string *varname, TTree *tr)

Public Members

std::string variable_name = ""

ROOT branch name.

bool failed_branch = false

true if TTree::Branch returned a null pointer.