From d0f435a7ab30317e433c9e4e45436729e74dd05a Mon Sep 17 00:00:00 2001 From: froejdh_e Date: Fri, 10 Jan 2025 19:02:50 +0100 Subject: [PATCH] bounds checking on subfiles --- python/examples/play.py | 130 +++++++++++++++++++++------------------- src/RawFile.cpp | 12 ++++ 2 files changed, 79 insertions(+), 63 deletions(-) diff --git a/python/examples/play.py b/python/examples/play.py index ca232ba2..588662d0 100644 --- a/python/examples/play.py +++ b/python/examples/play.py @@ -14,99 +14,103 @@ f = File(base/'Moench03new/cu_half_speed_master_4.json') +for i, frame in enumerate(f): + print(f'{i}', end='\r') +print() + from aare._aare import ClusterFinderMT, ClusterCollector, ClusterFileSink -cf = ClusterFinderMT((400,400), (3,3), n_threads = 3) -# collector = ClusterCollector(cf) -out_file = ClusterFileSink(cf, "test.clust") +# cf = ClusterFinderMT((400,400), (3,3), n_threads = 3) +# # collector = ClusterCollector(cf) +# out_file = ClusterFileSink(cf, "test.clust") -for i in range(1000): - img = f.read_frame() - cf.push_pedestal_frame(img) -print('Pedestal done') -cf.sync() +# for i in range(1000): +# img = f.read_frame() +# cf.push_pedestal_frame(img) +# print('Pedestal done') +# cf.sync() -for i in range(100): - img = f.read_frame() - cf.find_clusters(img) +# for i in range(100): +# img = f.read_frame() +# cf.find_clusters(img) +# # time.sleep(1) +# cf.stop() # time.sleep(1) -cf.stop() -time.sleep(1) -print('Second run') -cf.start() -for i in range(100): - img = f.read_frame() - cf.find_clusters(img) +# print('Second run') +# cf.start() +# for i in range(100): +# img = f.read_frame() +# cf.find_clusters(img) -cf.stop() -print('Third run') -cf.start() -for i in range(129): - img = f.read_frame() - cf.find_clusters(img) +# cf.stop() +# print('Third run') +# cf.start() +# for i in range(129): +# img = f.read_frame() +# cf.find_clusters(img) -cf.stop() -out_file.stop() -print('Done') +# cf.stop() +# out_file.stop() +# print('Done') -cfile = ClusterFile("test.clust") -i = 0 -while True: - try: - cv = cfile.read_frame() - i+=1 - except RuntimeError: - break -print(f'Read {i} frames') +# cfile = ClusterFile("test.clust") +# i = 0 +# while True: +# try: +# cv = cfile.read_frame() +# i+=1 +# except RuntimeError: +# break +# print(f'Read {i} frames') -# cf = ClusterFinder((400,400), (3,3)) -# for i in range(1000): -# cf.push_pedestal_frame(f.read_frame()) +# # cf = ClusterFinder((400,400), (3,3)) +# # for i in range(1000): +# # cf.push_pedestal_frame(f.read_frame()) -# fig, ax = plt.subplots() -# im = ax.imshow(cf.pedestal()) -# cf.pedestal() -# cf.noise() +# # fig, ax = plt.subplots() +# # im = ax.imshow(cf.pedestal()) +# # cf.pedestal() +# # cf.noise() -# N = 500 -# t0 = time.perf_counter() -# hist1 = bh.Histogram(bh.axis.Regular(40, -2, 4000)) -# f.seek(0) +# # N = 500 +# # t0 = time.perf_counter() +# # hist1 = bh.Histogram(bh.axis.Regular(40, -2, 4000)) +# # f.seek(0) -# t0 = time.perf_counter() -# data = f.read_n(N) -# t_elapsed = time.perf_counter()-t0 +# # t0 = time.perf_counter() +# # data = f.read_n(N) +# # t_elapsed = time.perf_counter()-t0 -# n_bytes = data.itemsize*data.size +# # n_bytes = data.itemsize*data.size -# print(f'Reading {N} frames took {t_elapsed:.3f}s {N/t_elapsed:.0f} FPS, {n_bytes/1024**2:.4f} GB/s') +# # print(f'Reading {N} frames took {t_elapsed:.3f}s {N/t_elapsed:.0f} FPS, {n_bytes/1024**2:.4f} GB/s') -# for frame in data: -# a = cf.find_clusters(frame) +# # for frame in data: +# # a = cf.find_clusters(frame) -# clusters = cf.steal_clusters() +# # clusters = cf.steal_clusters() -# t_elapsed = time.perf_counter()-t0 -# print(f'Clustering {N} frames took {t_elapsed:.2f}s {N/t_elapsed:.0f} FPS') +# # t_elapsed = time.perf_counter()-t0 +# # print(f'Clustering {N} frames took {t_elapsed:.2f}s {N/t_elapsed:.0f} FPS') -# t0 = time.perf_counter() -# total_clusters = clusters.size +# # t0 = time.perf_counter() +# # total_clusters = clusters.size -# hist1.fill(clusters.sum()) +# # hist1.fill(clusters.sum()) -# t_elapsed = time.perf_counter()-t0 -# print(f'Filling histogram with the sum of {total_clusters} clusters took: {t_elapsed:.3f}s, {total_clusters/t_elapsed:.3g} clust/s') -# print(f'Average number of clusters per frame {total_clusters/N:.3f}') \ No newline at end of file +# # t_elapsed = time.perf_counter()-t0 +# # print(f'Filling histogram with the sum of {total_clusters} clusters took: {t_elapsed:.3f}s, {total_clusters/t_elapsed:.3g} clust/s') +# # print(f'Average number of clusters per frame {total_clusters/N:.3f}') \ No newline at end of file diff --git a/src/RawFile.cpp b/src/RawFile.cpp index 744064f9..b8c49cff 100644 --- a/src/RawFile.cpp +++ b/src/RawFile.cpp @@ -278,6 +278,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect if (n_subfile_parts != 1) { for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) { auto subfile_id = frame_index / m_master.max_frames_per_file(); + if (subfile_id >= subfiles.size()) { + throw std::runtime_error(LOCATION + + " Subfile out of range. Possible missing data."); + } frame_numbers[part_idx] = subfiles[subfile_id][part_idx]->frame_number( frame_index % m_master.max_frames_per_file()); @@ -311,6 +315,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) { auto corrected_idx = frame_indices[part_idx]; auto subfile_id = corrected_idx / m_master.max_frames_per_file(); + if (subfile_id >= subfiles.size()) { + throw std::runtime_error(LOCATION + + " Subfile out of range. Possible missing data."); + } // This is where we start writing auto offset = (m_module_pixel_0[part_idx].y * m_cols + @@ -343,6 +351,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect auto pos = m_module_pixel_0[part_idx]; auto corrected_idx = frame_indices[part_idx]; auto subfile_id = corrected_idx / m_master.max_frames_per_file(); + if (subfile_id >= subfiles.size()) { + throw std::runtime_error(LOCATION + + " Subfile out of range. Possible missing data."); + } subfiles[subfile_id][part_idx]->seek(corrected_idx % m_master.max_frames_per_file()); subfiles[subfile_id][part_idx]->read_into(part_buffer, header);