From b2e5c71f9cd3bb0cf3ddfcd5ee7f7d2d1d9dac74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Wed, 6 Nov 2024 21:32:03 +0100 Subject: [PATCH] MH02 1-4 counters --- include/aare/CtbRawFile.hpp | 2 + include/aare/PixelMap.hpp | 8 ++- include/aare/RawMasterFile.hpp | 3 + include/aare/SubFile.hpp | 2 +- python/aare/CtbRawFile.py | 10 +++ python/aare/transform.py | 13 ++++ python/examples/play.py | 120 +++++++++++++++++++++++++++++---- python/src/file.hpp | 4 +- python/src/pixel_map.hpp | 12 +++- src/CtbRawFile.cpp | 2 + src/PixelMap.cpp | 30 +++++++-- src/RawMasterFile.cpp | 13 ++++ 12 files changed, 195 insertions(+), 24 deletions(-) diff --git a/include/aare/CtbRawFile.hpp b/include/aare/CtbRawFile.hpp index a615188..68dab23 100644 --- a/include/aare/CtbRawFile.hpp +++ b/include/aare/CtbRawFile.hpp @@ -27,6 +27,8 @@ public: size_t image_size_in_bytes() const; size_t frames_in_file() const; + + RawMasterFile master() const; private: void find_subfiles(); size_t sub_file_index(size_t frame_index) const { diff --git a/include/aare/PixelMap.hpp b/include/aare/PixelMap.hpp index 4646476..4838737 100644 --- a/include/aare/PixelMap.hpp +++ b/include/aare/PixelMap.hpp @@ -5,7 +5,11 @@ namespace aare { -NDArray GenerateMoench03PixelMap(); -NDArray GenerateMoench05PixelMap(); +NDArray GenerateMoench03PixelMap(); +NDArray GenerateMoench05PixelMap(); + +NDArrayGenerateMH02SingleCounterPixelMap(); +NDArray GenerateMH02FourCounterPixelMap(); + } // namespace aare \ No newline at end of file diff --git a/include/aare/RawMasterFile.hpp b/include/aare/RawMasterFile.hpp index 96c5adb..7861fb9 100644 --- a/include/aare/RawMasterFile.hpp +++ b/include/aare/RawMasterFile.hpp @@ -65,9 +65,11 @@ class RawMasterFile { //TODO! should these be bool? uint8_t m_analog_flag{}; uint8_t m_digital_flag{}; + uint8_t m_transceiver_flag{}; std::optional m_analog_samples; std::optional m_digital_samples; + std::optional m_transceiver_samples; std::optional m_number_of_rows; std::optional m_quad; @@ -93,6 +95,7 @@ class RawMasterFile { std::optional analog_samples() const; std::optional digital_samples() const; + std::optional transceiver_samples() const; std::optional number_of_rows() const; std::optional quad() const; private: diff --git a/include/aare/SubFile.hpp b/include/aare/SubFile.hpp index 7e59d3e..47cc07c 100644 --- a/include/aare/SubFile.hpp +++ b/include/aare/SubFile.hpp @@ -74,7 +74,7 @@ class SubFile { size_t n_frames{}; int m_sub_file_index_{}; DetectorType m_detector_type; - std::optional> pixel_map; + std::optional> pixel_map; }; } // namespace aare \ No newline at end of file diff --git a/python/aare/CtbRawFile.py b/python/aare/CtbRawFile.py index c14675d..1925ef5 100644 --- a/python/aare/CtbRawFile.py +++ b/python/aare/CtbRawFile.py @@ -36,6 +36,7 @@ class CtbRawFile(_aare.CtbRawFile): if frame_index is not None: self.seek(frame_index) + header, data = super().read_frame() if header.shape == (1,): header = header[0] @@ -47,6 +48,8 @@ class CtbRawFile(_aare.CtbRawFile): return header, *res else: return header, res + else: + return header, data def read_n(self, n_frames:int) -> tuple: """Read several frames from the file. @@ -82,6 +85,13 @@ class CtbRawFile(_aare.CtbRawFile): return header, data + def read(self) -> tuple: + """Read the entire file. + + Returns: + tuple: header, data + """ + return self.read_n(self.frames_in_file) def seek(self, frame_index:int) -> None: """Seek to a specific frame in the file. diff --git a/python/aare/transform.py b/python/aare/transform.py index 17e520e..47a7efc 100644 --- a/python/aare/transform.py +++ b/python/aare/transform.py @@ -12,4 +12,17 @@ class Moench05Transform: return np.take(data.view(np.uint16), self.pixel_map) +class Matterhorn02Transform: + def __init__(self): + self.pixel_map = _aare.GenerateMH02FourCounterPixelMap() + + def __call__(self, data): + counters = int(data.size / 48**2 / 2) + if counters == 1: + return np.take(data.view(np.uint16), self.pixel_map[0]) + else: + return np.take(data.view(np.uint16), self.pixel_map[0:counters]) + +#on import generate the pixel maps to avoid doing it every time moench05 = Moench05Transform() +matterhorn02 = Matterhorn02Transform() \ No newline at end of file diff --git a/python/examples/play.py b/python/examples/play.py index 91e744c..08703c2 100644 --- a/python/examples/play.py +++ b/python/examples/play.py @@ -9,6 +9,50 @@ from aare import transform print('transform imported') from pathlib import Path +import json + +def decode(frames, rawdata): + # rawdata = np.fromfile(f, dtype = np.uint16) + counters = int((np.shape(rawdata)[0]/frames-56)/(48*48)) + print('Counters:', counters) + rawdata = rawdata.reshape(frames,-1)[:,56:] + rawdata = rawdata.reshape(frames,576*counters,4) #Data come in "blocks" of 4 pixels/receiver + tr1 = rawdata[:,0:576*counters:2] #Transceiver1 + tr1=tr1.reshape((frames,48*counters,24)) + + tr2 = rawdata[:,1:576*counters:2] #Transceiver2 + tr2=tr2.reshape((frames,48*counters,24)) + + data = np.append(tr1,tr2,axis=2) + return data + +def get_Mh02_frames(fname): + # this function gives you the data from a file that is not a scan + # it returns a (frames,48*counters,48) + + jsonf = open(fname) + jsonpar = json.load(jsonf) + jsonf.close() + + frames=jsonpar["Frames in File"] + print('Frames:', frames) + + rawf = fname.replace('master','d0_f0') + rawf = rawf.replace('.json','.raw') + + with open(rawf, 'rb') as f: + rawdata = np.fromfile(f, dtype = np.uint16) + data = decode(frames, rawdata) + print('Data:', np.shape(data)) + + return data + + +#target format +# [frame, counter, row, col] +# plt.imshow(data[0,0]) + + # p = Path('/Users/erik/data/aare_test_data/jungfrau/jungfrau_single_master_0.json') # f = aare.File(p) @@ -18,27 +62,75 @@ from pathlib import Path # im = ax.imshow(frame, cmap='viridis') -fpath = Path('/Users/erik/data/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json') -# fpath = Path('/Users/erik/data/Moench05/moench05_multifile_master_0.json') +# fpath = Path('/Users/erik/data/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json') +# # fpath = Path('/Users/erik/data/Moench05/moench05_multifile_master_0.json') -# f = aare.CtbRawFile(fpath, transform = transform.moench05) -# with CtbRawFile(fpath, transform = transform.moench05) as f: -# for header, image in f: -# print(f'Frame number: {header["frameNumber"]}') +# # f = aare.CtbRawFile(fpath, transform = transform.moench05) +# # with CtbRawFile(fpath, transform = transform.moench05) as f: +# # for header, image in f: +# # print(f'Frame number: {header["frameNumber"]}') -# m = aare.RawMasterFile(fpath) -f = aare.File(fpath) +# # m = aare.RawMasterFile(fpath) +# f = aare.File(fpath) -cf = aare.ClusterFinder((400,400),(3,3)) +# cf = aare.ClusterFinder((400,400),(3,3)) -for i in range(100): - cf.push_pedestal_frame(f.read_frame()) +# for i in range(100): +# cf.push_pedestal_frame(f.read_frame()) -f.seek(0) -pd = f.read_n(100).mean(axis=0) +# f.seek(0) +# pd = f.read_n(100).mean(axis=0) + +# clusters = cf.find_clusters_without_threshold(f.read_frame()) + + + +base = Path('/Users/erik/data/matterhorn/raw') +fpath = Path(base / 'scan_15keV_vrf700_vrsh700_th0_master_0.json') +f = aare.CtbRawFile(fpath, transform=transform.matterhorn02) +f.seek(100) +header1, image1 = f.read_frame() + +fpath = Path(base / 'scan_all15keV_vrf500_vrsh700_th0_master_0.json') + +f = aare.CtbRawFile(fpath, transform=transform.matterhorn02) +f.seek(100) +header4, image4 = f.read_frame() + +# n_counters = image.shape[1] / 48**2 / 2 + +# for i in range(100): +# header, image = f.read_frame() +# print(header['frameNumber']) + + + + + +#Data come in "blocks" of 4 pixels/receiver +data = get_Mh02_frames(fpath.as_posix()) + +# rawi = np.zeros(48*48*4+56, dtype = np.uint16) +# for i,v in enumerate(rawi[56:]): +# rawi[i+56] = i + +# raw = image.view(np.uint16) + +# pixel_map = decode(1, rawi) +# # img = np.take(raw, pixel_map) + +# pm = np.zeros((4, 48,48), dtype = np.int64) +# for counter in range(4): +# for row in range(48): +# for col in range(48): +# pm[counter, row, col] = row*48 + col+counter*48*48 + + +f2 = aare.CtbRawFile(fpath, transform=transform.matterhorn02) +header, data = f2.read() +plt.plot(data[:,0,20,20]) -clusters = cf.find_clusters_without_threshold(f.read_frame()) \ No newline at end of file diff --git a/python/src/file.hpp b/python/src/file.hpp index c40e011..7f9b2a3 100644 --- a/python/src/file.hpp +++ b/python/src/file.hpp @@ -48,6 +48,7 @@ void define_file_io_bindings(py::module &m) { }) .def("seek", &CtbRawFile::seek) .def("tell", &CtbRawFile::tell) + .def("master", &CtbRawFile::master) .def_property_readonly("image_size_in_bytes", &CtbRawFile::image_size_in_bytes) .def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file); @@ -167,7 +168,8 @@ void define_file_io_bindings(py::module &m) { .def_property_readonly("bitdepth", &RawMasterFile::bitdepth) .def_property_readonly("analog_samples", &RawMasterFile::analog_samples) .def_property_readonly("digital_samples", - &RawMasterFile::digital_samples); + &RawMasterFile::digital_samples) + .def_property_readonly("transceiver_samples", &RawMasterFile::transceiver_samples); // py::class_(m, "ClusterHeader") // .def(py::init<>()) diff --git a/python/src/pixel_map.hpp b/python/src/pixel_map.hpp index 5791024..3a8e366 100644 --- a/python/src/pixel_map.hpp +++ b/python/src/pixel_map.hpp @@ -14,11 +14,19 @@ using namespace::aare; void define_pixel_map_bindings(py::module &m) { m.def("GenerateMoench03PixelMap", []() { - auto ptr = new NDArray(GenerateMoench03PixelMap()); + auto ptr = new NDArray(GenerateMoench03PixelMap()); return return_image_data(ptr); }) .def("GenerateMoench05PixelMap", []() { - auto ptr = new NDArray(GenerateMoench05PixelMap()); + auto ptr = new NDArray(GenerateMoench05PixelMap()); + return return_image_data(ptr); + }) + .def("GenerateMH02SingleCounterPixelMap", []() { + auto ptr = new NDArray(GenerateMH02SingleCounterPixelMap()); + return return_image_data(ptr); + }) + .def("GenerateMH02FourCounterPixelMap", []() { + auto ptr = new NDArray(GenerateMH02FourCounterPixelMap()); return return_image_data(ptr); }); diff --git a/src/CtbRawFile.cpp b/src/CtbRawFile.cpp index 94cf77c..1f3a507 100644 --- a/src/CtbRawFile.cpp +++ b/src/CtbRawFile.cpp @@ -48,6 +48,8 @@ size_t CtbRawFile::image_size_in_bytes() const { return m_master.image_size_in_b size_t CtbRawFile::frames_in_file() const { return m_master.frames_in_file(); } +RawMasterFile CtbRawFile::master() const { return m_master; } + void CtbRawFile::find_subfiles() { // we can semi safely assume that there is only one module for CTB while (std::filesystem::exists(m_master.data_fname(0, m_num_subfiles))) diff --git a/src/PixelMap.cpp b/src/PixelMap.cpp index dda6ec6..b8788c8 100644 --- a/src/PixelMap.cpp +++ b/src/PixelMap.cpp @@ -3,7 +3,7 @@ #include namespace aare { -NDArray GenerateMoench03PixelMap() { +NDArray GenerateMoench03PixelMap() { std::array const adc_nr = {300, 325, 350, 375, 300, 325, 350, 375, 200, 225, 250, 275, 200, 225, 250, 275, 100, 125, 150, 175, 100, 125, 150, 175, @@ -11,7 +11,7 @@ NDArray GenerateMoench03PixelMap() { int const sc_width = 25; int const nadc = 32; int const pixels_per_sc = 5000; - NDArray order_map({400, 400}); + NDArray order_map({400, 400}); int pixel = 0; for (int i = 0; i != pixels_per_sc; ++i) { @@ -30,9 +30,9 @@ NDArray GenerateMoench03PixelMap() { return order_map; } -NDArray GenerateMoench05PixelMap() { +NDArray GenerateMoench05PixelMap() { std::array adc_numbers = {9, 13, 1}; - NDArray order_map({160, 150}); + NDArray order_map({160, 150}); int n_pixel = 0; for (int row = 0; row < 160; row++) { for (int i_col = 0; i_col < 50; i_col++) { @@ -52,4 +52,26 @@ NDArray GenerateMoench05PixelMap() { return order_map; } +NDArrayGenerateMH02SingleCounterPixelMap(){ + NDArray order_map({48, 48}); + for(int row = 0; row < 48; row++){ + for(int col = 0; col < 48; col++){ + order_map(row, col) = row*48 + col; + } + } + return order_map; +} + +NDArray GenerateMH02FourCounterPixelMap(){ + NDArray order_map({4, 48, 48}); + for (int counter=0; counter<4; counter++){ + for(int row = 0; row < 48; row++){ + for(int col = 0; col < 48; col++){ + order_map(counter, row, col) = counter*48*48 + row*48 + col; + } + } + } + return order_map; +} + } // namespace aare \ No newline at end of file diff --git a/src/RawMasterFile.cpp b/src/RawMasterFile.cpp index ba55ae2..e6a878e 100644 --- a/src/RawMasterFile.cpp +++ b/src/RawMasterFile.cpp @@ -109,6 +109,10 @@ std::optional RawMasterFile::digital_samples() const { return m_digital_samples; } +std::optional RawMasterFile::transceiver_samples() const { + return m_transceiver_samples; +} + void RawMasterFile::parse_json(const std::filesystem::path &fpath) { std::ifstream ifs(fpath); json j; @@ -190,6 +194,15 @@ void RawMasterFile::parse_json(const std::filesystem::path &fpath) { // keep the optional empty } + try{ + m_transceiver_flag = j.at("Transceiver Flag"); + if(m_transceiver_flag){ + m_transceiver_samples = j.at("Transceiver Samples"); + } + }catch (const json::out_of_range &e) { + // keep the optional empty + } + // Update detector type for Moench // TODO! How does this work with old .raw master files? #ifdef AARE_VERBOSE