Skip to content
Snippets Groups Projects

Resolve "Reduce the amount of uproot.open (to one)"

Merged Tamas Gal requested to merge 36-reduce-the-amount-of-uproot-open-to-one into master
3 files
+ 1
382
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 0
65
@@ -211,71 +211,6 @@ class OfflineKeys:
return km3io.definitions.trigger.data
class Reader:
"""Reader for one offline ROOT file"""
def __init__(self, file_path):
""" Reader class is an offline ROOT file reader. This class is a
"very" low level I/O.
Parameters
----------
file_path : str or file-like object
The file of interest.
"""
self._tree = uproot.open(file_path)[MAIN_TREE_NAME]
self._data = self._tree.lazyarrays(
basketcache=uproot.cache.ThreadSafeArrayCache(BASKET_CACHE_SIZE))
self._keys = None
def __getitem__(self, key):
"""reads data stored in the branch of interest in an Evt tree.
Parameters
----------
key : str
name of the branch of interest in event data.
Returns
-------
lazyarray
Lazyarray of all data stored in the branch of interest. A lazyarray
is an array-like object that reads data on demand. Here, only the
first and last chunks of data are read in memory, and not all data
in the array. The output can be used with all `Numpy's universal
functions <https://docs.scipy.org/doc/numpy/reference/ufuncs.html>`
.
Raises
------
KeyError
Some branches in an offline file structure are "fake branches" and
do not contain data. Therefore, the keys corresponding to these
fake branches are not read.
"""
keys = self.keys.valid_keys
if key not in keys and not isinstance(key, int):
raise KeyError(
"'{}' is not a valid key or is a fake branch.".format(key))
return self._data[key]
def __len__(self):
return len(self._data)
def __repr__(self):
return "<{}: {} entries>".format(self.__class__.__name__, len(self))
@cached_property
def keys(self):
"""wrapper for all keys in an offline file.
Returns
-------
Class
OfflineKeys.
"""
return OfflineKeys(self._tree)
class OfflineReader:
"""reader for offline ROOT files"""
def __init__(self, file_path=None):
Loading