Tree#
- class heptools.root.chunk.Chunk(source, name='Events', branches=..., num_entries=..., entry_start=..., entry_stop=..., fetch=False)[source]#
A chunk of
TTree
stored in a ROOT file.- Parameters:
source (PathLike or tuple[PathLike, UUID]) – Path to ROOT file with optional UUID
name (str, optional, default='Events') – Name of
TTree
.branches (Iterable[str], optional) – Name of branches. If not given, read from
source
.num_entries (int, optional) – Number of entries. If not given, read from
source
.entry_start (int, optional) – Start entry. If not given, set to
0
.entry_stop (int, optional) – Stop entry. If not given, set to
num_entries
.fetch (bool, optional, default=False) – Fetch missing metadata from
source
immediately after initialization.
Notes
The following special methods are implemented:
__hash__()
__eq__()
__len__()
__repr__()
- integrity()[source]#
Check and report the following:
path
not existsuuid
different from filenum_entries
different from filebranches
not in fileentry_start
out of rangeentry_stop
out of range
- Returns:
Chunk or None – A deep copy of
self
with corrected metadata. If file not exists, returnNone
.
- deepcopy(**kwargs)[source]#
- Parameters:
**kwargs (dict, optional) – Override
entry_start
,entry_stop
orbranches
.- Returns:
Chunk – A deep copy of
self
.
- slice(start, stop)[source]#
- Parameters:
- Returns:
Chunk – A sliced
deepcopy()
ofself
fromstart
+offset
tostop
+offset
.
- classmethod from_path(*paths, executor=None)[source]#
Create
Chunk
frompaths
and fetch metadata in parallel.
- classmethod partition(size, *chunks, common_branches=False)[source]#
Partition
chunks
into groups. The sum of entries in each group is equal tosize
except for the last one. The order of chunks is preserved.
- classmethod balance(size, *chunks, common_branches=False)[source]#
Split
chunks
into smaller pieces withsize
entries in each. If not possible, will try to find another size minimizing the average deviation.
- class heptools.root.chain.Chain[source]#
A
TChain
like object to manage multipleChunk
andFriend
.The structure of output record is given by the following pseudo code:
library='ak'
:record[main.branch] = array record[friend.name][friend.branch] = array
library='pd', 'np'
:record[main.branch] = array record[friend.branch] = array
If duplicate branches are found after rename, the one in the friend tree that appears last will be kept.
Notes
The following special methods are implemented:
- add_friend(*friends, renaming=None)[source]#
Add new
Friend
to this chain or merge to the existing ones.- Parameters:
- Returns:
self (Chain)
Notes
The following keys are available for renaming:
{friend}
:Friend.name
{branch}
: branch name
If the renaming function returns a tuple, the data will be stored in a nested record.
- concat(library='ak', reader_options=None, friend_only=False)[source]#
Read all chunks and friend trees into one record.
- Parameters:
library (Literal['ak', 'np', 'pd'], optional, default='ak') – The library used to represent arrays.
reader_options (dict, optional) – Additional options passed to
TreeReader
.friend_only (bool, optional, default=False) – If
True
, only read friend trees.
- Returns:
RecordLike – Concatenated data.
- iterate(step=..., library='ak', mode='partition', reader_options=None, friend_only=False)[source]#
Iterate over chunks and friend trees.
- Parameters:
step (int, optional) – Number of entries to read in each iteration step. If not given, the chunk size will be used and the
mode
will be ignored.library (Literal['ak', 'np', 'pd'], optional, default='ak') – The library used to represent arrays.
mode (Literal['balance', 'partition'], optional, default='partition') – The mode to generate iteration steps. See
iterate()
for details.reader_options (dict, optional) – Additional options passed to
TreeReader
.friend_only (bool, optional, default=False) – If
True
, only read friend trees.
- Yields:
RecordLike – A chunk of merged data from main and friend
TTree
.
- dask(partition=..., library='ak', reader_options=None, friend_only=False)[source]#
Read chunks and friend trees into delayed arrays.
Warning
The
renaming
option will be ignored when usinglibrary='ak'
.- Parameters:
partition (int, optional) – If given, the
sources
will be splitted into smaller chunks targetingpartition
entries.library (Literal['ak', 'np'], optional, default='ak') – The library used to represent arrays.
reader_options (dict, optional) – Additional options passed to
TreeReader
.friend_only (bool, optional, default=False) – If
True
, only read friend trees.
- Returns:
DelayedRecordLike – Delayed data from main and friend
TTree
.