Skip to content

Commit

Permalink
Check file integrity in readers
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaponte committed Nov 6, 2024
1 parent f613a5c commit 5d2ccab
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions invisible_cities/io/pmaps_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import tables as tb
import pandas as pd

from .. core.exceptions import InvalidInputFileStructure

from .. evm .pmaps import PMTResponses
from .. evm .pmaps import SiPMResponses
from .. evm .pmaps import S1
from .. evm .pmaps import S2
from .. evm .pmaps import PMap
from .. evm import nh5 as table_formats

from .. reco.tbl_functions import filters as tbl_filters



def store_peak(pmt_table, pmti_table, si_table,
peak, peak_number, event_number):
pmt_row = pmt_table.row
Expand Down Expand Up @@ -78,13 +82,23 @@ def _make_tables(hdf5_file, compression):
return pmp_tables


def check_file_integrity(file):
events_run = file.root.Run .events.read(field="evt_number")
events_pmaps_s1 = file.root.PMAPS.S1 .read(field="event")
events_pmaps_s2 = file.root.PMAPS.S2 .read(field="event")
if set(events_run) != set(events_pmaps_s1.tolist() + events_pmaps_s2.tolist()):
raise InvalidInputFileStructure("Inconsistent data: event number mismatch")


def load_pmaps_as_df(filename, lazy=False):
loader = load_pmaps_as_df_lazy if lazy else load_pmaps_as_df_eager
return loader(filename)


def load_pmaps_as_df_eager(filename):
with tb.open_file(filename, 'r') as h5f:
check_file_integrity(h5f)

pmap = h5f.root.PMAPS
to_df = pd.DataFrame.from_records
return (to_df(pmap.S1 .read()),
Expand All @@ -102,6 +116,8 @@ def read_event(table, event):

tables = "S1 S2 S2Si S1Pmt S2Pmt".split()
with tb.open_file(filename, 'r') as h5f:
check_file_integrity(h5f)

events = h5f.root.Run.events.read(field="evt_number")
tables = [getattr(h5f.root.PMAPS, table, None) for table in tables]
for event in events:
Expand Down

0 comments on commit 5d2ccab

Please sign in to comment.