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
+ 28
29
@@ -198,57 +198,57 @@ class OfflineKeys:
"""
return [k.replace('.', '_') for k in self.events_keys]
@cached_property
def trigger(self):
"""trigger parameters and their index from km3net-Dataformat.
Returns
-------
dict
dictionary of trigger parameters and their index in an Offline
file.
"""
return km3io.definitions.trigger.data
class OfflineReader:
"""reader for offline ROOT files"""
def __init__(self, file_path=None):
def __init__(self, file_path=None, fobj=None, data=None):
""" OfflineReader class is an offline ROOT file wrapper
Parameters
----------
file_path : path-like object
Path to the file of interest. It can be a str or any python
path-like object that points to the file of ineterst.
path-like object that points to the file.
"""
self._file_path = file_path
if file_path is not None:
self._tree = uproot.open(self._file_path)[MAIN_TREE_NAME]
self._fobj = uproot.open(self._file_path)
self._tree = self._fobj[MAIN_TREE_NAME]
self._data = self._tree.lazyarrays(
basketcache=uproot.cache.ThreadSafeArrayCache(
BASKET_CACHE_SIZE))
else:
self._fobj = fobj
self._tree = self._fobj[MAIN_TREE_NAME]
self._data = data
@classmethod
def from_tree(cls, tree, data):
instance = cls()
instance._tree = tree
instance._data = data
def from_index(cls, source, index):
"""Create an instance with a subtree of a given index
Parameters
----------
source: ROOTDirectory
The source file.
index: index or slice
The index or slice to create the subtree.
"""
instance = cls(fobj=source._fobj, data=source._data[index])
return instance
def __getitem__(self, item):
return OfflineReader.from_tree(tree=self._tree, data=self._data[item])
def __getitem__(self, index):
return OfflineReader.from_index(source=self, index=index)
def __len__(self):
return len(self._data)
@cached_property
def header(self):
fobj = uproot.open(self._file_path)
if 'Head' in fobj:
if 'Head' in self._fobj:
header = {}
for n, x in fobj['Head']._map_3c_string_2c_string_3e_.items():
for n, x in self._fobj['Head']._map_3c_string_2c_string_3e_.items():
header[n.decode("utf-8")] = x.decode("utf-8").strip()
return header
else:
@@ -331,7 +331,7 @@ class OfflineReader:
@cached_property
def usr(self):
return Usr(self._file_path)
return Usr(self._tree)
def get_best_reco(self):
"""returns the best reconstructed track fit data. The best fit is defined
@@ -674,15 +674,14 @@ class OfflineReader:
class Usr:
"""Helper class to access AAObject usr stuff"""
def __init__(self, filepath):
self._f = uproot.open(filepath)
def __init__(self, tree):
# Here, we assume that every event has the same names in the same order
# to massively increase the performance. This needs triple check if it's
# always the case; the usr-format is simply a very bad design.
try:
self._usr_names = [
n.decode("utf-8")
for n in self._f['E']['Evt']['usr_names'].array()[0]
for n in tree['Evt']['usr_names'].array()[0]
]
except (KeyError, IndexError): # e.g. old aanet files
self._usr_names = []
@@ -691,7 +690,7 @@ class Usr:
name: index
for index, name in enumerate(self._usr_names)
}
self._usr_data = self._f['E']['Evt']['usr'].lazyarray(
self._usr_data = tree['Evt']['usr'].lazyarray(
basketcache=uproot.cache.ThreadSafeArrayCache(
BASKET_CACHE_SIZE))
for name in self._usr_names:
Loading