/
TBasket.py
327 lines (266 loc) · 11 KB
/
TBasket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE
"""
This module defines a versionless model for ``TBasket``, including much of the
functionality of basket-reading.
Includes both "embedded" ``TBaskets`` (as a member of TBranch) and "free"
``TBaskets`` (top-level objects, located by ``TKeys``).
"""
from __future__ import annotations
import struct
import numpy
import uproot
_tbasket_format1 = struct.Struct(">ihiIhh")
_tbasket_format2 = struct.Struct(">Hiiii")
_tbasket_offsets_dtype = numpy.dtype(">i4")
class Model_TBasket(uproot.model.Model):
"""
A versionless :doc:`uproot.model.Model` for ``TBasket``.
Since this model is versionless and most of its functionality is internal
(not to be directly accessed by most users), it is defined on the model
instead of creating a behavior class to mix in functionality.
"""
def __repr__(self):
basket_num = self._basket_num if self._basket_num is not None else "(unknown)"
return f"<TBasket {basket_num} of {self._parent.name!r} at 0x{id(self):012x}>"
@property
def raw_data(self):
"""
The raw but uncompressed data in the ``TBasket``, which combines data
content with entry offsets, if the latter exists.
If there are no entry offsets, this is identical to
:ref:`uproot.models.TBasket.Model_TBasket.data`.
"""
return self._raw_data
@property
def data(self):
"""
The uncompressed data content in the ``TBasket``, not including any
entry offsets, if they exist.
If there are no entry offsets, this is identical to
:ref:`uproot.models.TBasket.Model_TBasket.raw_data`.
"""
return self._data
@property
def byte_offsets(self):
"""
The index where each entry starts and stops in the
:ref:`uproot.models.TBasket.Model_TBasket.data`, not including header.
The first offset is ``0`` and the number of offsets is one greater than
the number of entries, such that the last offset is the length of
:ref:`uproot.models.TBasket.Model_TBasket.data`.
"""
return self._byte_offsets
def array(self, interpretation=None, library="ak", ak_add_doc=False):
"""
The ``TBasket`` data and entry offsets as an array, given an
:doc:`uproot.interpretation.Interpretation` (or the ``TBranch`` parent's
:ref:`uproot.behaviors.TBranch.TBranch.interpretation`) and a
``library``.
"""
if interpretation is None:
interpretation = self._parent.interpretation
library = uproot.interpretation.library._regularize_library(library)
interp_options = {"ak_add_doc": ak_add_doc}
basket_array = interpretation.basket_array(
self.data,
self.byte_offsets,
self,
self._parent,
self._parent.context,
self._members["fKeylen"],
library,
interp_options,
)
return interpretation.final_array(
[basket_array],
0,
self.num_entries,
[0, self.num_entries],
library,
self._parent,
interp_options,
)
@property
def counts(self):
"""
The number of items in each entry as a NumPy array, derived from the
parent ``TBranch``'s
:ref:`uproot.behaviors.TBranch.TBranch.count_branch`. If there is
no such branch (e.g. the data are ``std::vector``), then this method
returns None.
"""
count_branch = self._parent.count_branch
if count_branch is not None:
entry_offsets = count_branch.entry_offsets
entry_start = entry_offsets[self._basket_num]
entry_stop = entry_offsets[self._basket_num + 1]
return count_branch.array(
entry_start=entry_start, entry_stop=entry_stop, library="np"
)
else:
return None
@property
def basket_num(self):
"""
The index of this ``TBasket`` within its ``TBranch``.
"""
return self._basket_num
@property
def entry_start_stop(self):
"""
The starting and stopping entry numbers for this ``TBasket``.
"""
return self._parent.basket_entry_start_stop(self._basket_num)
@property
def key_version(self):
"""
The instance version of the ``TKey`` for this ``TBasket`` (which is
deserialized along with the ``TBasket``, unlike normal objects).
"""
return self._key_version
@property
def num_entries(self):
"""
The number of entries in this ``TBasket``.
"""
return self._members["fNevBuf"]
@property
def is_embedded(self):
"""
If this ``TBasket`` is embedded within its ``TBranch`` (i.e. must be
deserialized as part of the ``TBranch``), then ``is_embedded`` is True.
If this ``TBasket`` is a free-standing object, then ``is_embedded`` is
False.
"""
return self._members["fNbytes"] <= self._members["fKeylen"]
@property
def uncompressed_bytes(self):
"""
The number of bytes for the uncompressed data, including the TKey header.
If the ``TBasket`` is uncompressed, this is equal to
:ref:`uproot.models.TBasket.Model_TBasket.compressed_bytes`.
"""
return self._members["fKeylen"] + self._members["fObjlen"]
@property
def compressed_bytes(self):
"""
The number of bytes for the compressed data, including the TKey header
(which is always uncompressed).
If the ``TBasket`` is uncompressed, this is equal to
:ref:`uproot.models.TBasket.Model_TBasket.uncompressed_bytes`.
"""
return self._members["fNbytes"]
@property
def block_compression_info(self):
"""
For compressed ``TBaskets``, a tuple of 3-tuples containing
``(name of algorithm, num compressed bytes, num uncompressed bytes)``
to describe the actual compression algorithms and sizes encountered in
each block of data.
The name of the algorithm can be ``"ZLIB"``, ``"LZMA"``, ``"LZ4"``, or
``"ZSTD"``.
For uncompressed ``TBaskets``, this is None.
"""
return self._block_compression_info
@property
def border(self):
"""
The byte position of the boundary between data content and entry offsets.
Equal to ``self.member("fLast") - self.member("fKeylen")``.
"""
return self._members["fLast"] - self._members["fKeylen"]
def read_numbytes_version(self, chunk, cursor, context):
pass
def read_members(self, chunk, cursor, context, file):
if uproot._awkwardforth.get_forth_obj(context) is not None:
raise uproot.interpretation.objects.CannotBeForth()
assert isinstance(self._parent, uproot.behaviors.TBranch.TBranch)
self._basket_num = context.get("basket_num")
(
self._members["fNbytes"],
self._key_version,
self._members["fObjlen"],
self._members["fDatime"],
self._members["fKeylen"],
self._members["fCycle"],
) = cursor.fields(chunk, _tbasket_format1, context)
# skip the class name, name, and title
cursor.move_to(
self._cursor.index + self._members["fKeylen"] - _tbasket_format2.size - 1
)
(
self._members["fVersion"],
self._members["fBufferSize"],
self._members["fNevBufSize"],
self._members["fNevBuf"],
self._members["fLast"],
) = cursor.fields(chunk, _tbasket_format2, context)
cursor.skip(1)
self._block_compression_info = None
if not context.get("read_basket", True):
self._byte_offsets = None
self._raw_data = None
self._data = None
return
if self.is_embedded:
# https://github.com/root-project/root/blob/0e6282a641b65bdf5ad832882e547ca990e8f1a5/tree/tree/inc/TBasket.h#L62-L65
maybe_entry_size = self._members["fNevBufSize"]
num_entries = self._members["fNevBuf"]
key_length = self._members["fKeylen"]
# Embedded TBaskets are always uncompressed; be sure to copy any memmap arrays
chunk = chunk.detach_memmap()
if maybe_entry_size * num_entries + key_length != self._members["fLast"]:
raw_byte_offsets = cursor.bytes(
chunk, 8 + self.num_entries * 4, context
).view(_tbasket_offsets_dtype)
cursor.skip(-4)
# subtracting fKeylen makes a new buffer and converts to native endian
self._byte_offsets = raw_byte_offsets[1:] - self._members["fKeylen"]
# so modifying it in place doesn't have non-local consequences
self._byte_offsets[-1] = self.border
else:
self._byte_offsets = None
# second key has no new information
cursor.skip(self._members["fKeylen"])
self._raw_data = None
if self.border == 0:
self._data = numpy.empty(0, dtype=numpy.uint8)
else:
self._data = cursor.bytes(chunk, self.border, context)
else:
compressed_bytes = self._members["fNbytes"] - self._members["fKeylen"]
uncompressed_bytes = self._members["fObjlen"]
if compressed_bytes != uncompressed_bytes:
self._block_compression_info = []
uncompressed = uproot.compression.decompress(
chunk,
cursor,
{},
compressed_bytes,
uncompressed_bytes,
self._block_compression_info,
)
self._block_compression_info = tuple(self._block_compression_info)
self._raw_data = uncompressed.get(
0,
uncompressed_bytes,
uproot.source.cursor.Cursor(0),
context,
)
else:
# Uncompressed; be sure to copy any memmap arrays
chunk = chunk.detach_memmap()
self._raw_data = cursor.bytes(chunk, uncompressed_bytes, context)
if self.border != uncompressed_bytes:
self._data = self._raw_data[: self.border]
raw_byte_offsets = self._raw_data[self.border :].view(
_tbasket_offsets_dtype
)
# subtracting fKeylen makes a new buffer and converts to native endian
self._byte_offsets = raw_byte_offsets[1:] - self._members["fKeylen"]
# so modifying it in place doesn't have non-local consequences
self._byte_offsets[-1] = self.border
else:
self._data = self._raw_data
self._byte_offsets = None
uproot.classes["TBasket"] = Model_TBasket