From 49da039ff9ae6004bc288ca833a7c2b68b52c763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Thu, 31 Oct 2024 15:35:43 +0100 Subject: [PATCH] working on 05 --- include/aare/PixelMap.hpp | 1 + include/aare/RawFile.hpp | 12 +--- python/aare/__init__.py | 2 +- python/examples/play.py | 8 ++- python/src/pixel_map.hpp | 4 ++ src/PixelMap.cpp | 30 ++++++++- src/RawFile.cpp | 138 +++++--------------------------------- 7 files changed, 60 insertions(+), 135 deletions(-) diff --git a/include/aare/PixelMap.hpp b/include/aare/PixelMap.hpp index 2967ef1..4646476 100644 --- a/include/aare/PixelMap.hpp +++ b/include/aare/PixelMap.hpp @@ -6,5 +6,6 @@ namespace aare { NDArray GenerateMoench03PixelMap(); +NDArray GenerateMoench05PixelMap(); } // namespace aare \ No newline at end of file diff --git a/include/aare/RawFile.hpp b/include/aare/RawFile.hpp index 8dcf826..cd87f2a 100644 --- a/include/aare/RawFile.hpp +++ b/include/aare/RawFile.hpp @@ -40,6 +40,7 @@ class RawFile : public FileInterface { //Stuff that we might need with Ctb files uint32_t m_analog_samples{}; uint32_t m_digital_samples{}; + uint32_t m_adc_mask{}; public: @@ -49,14 +50,10 @@ class RawFile : public FileInterface { * @param mode file mode (r, w) * @param cfg file configuration */ - explicit RawFile(const std::filesystem::path &fname, const std::string &mode = "r", + RawFile(const std::filesystem::path &fname, const std::string &mode = "r", const FileConfig &config = FileConfig{}); - /** - * @brief write function is not implemented for RawFile - * @param frame frame to write - */ - void write(Frame &frame, sls_detector_header header); + Frame read_frame() override { return get_frame(this->current_frame++); }; Frame read_frame(size_t frame_number) override{ seek(frame_number); @@ -141,7 +138,6 @@ class RawFile : public FileInterface { xy geometry() { return m_geometry; } private: - void write_master_file(); /** * @brief read the frame at the given frame index into the image buffer * @param frame_number frame number to read @@ -192,8 +188,6 @@ class RawFile : public FileInterface { * @brief open the subfiles */ void open_subfiles(); - void parse_config(const FileConfig &config); - }; diff --git a/python/aare/__init__.py b/python/aare/__init__.py index ffdacb9..2e63912 100644 --- a/python/aare/__init__.py +++ b/python/aare/__init__.py @@ -1,4 +1,4 @@ # Make the compiled classes that live in _aare available from aare. from ._aare import File from ._aare import VarClusterFinder -from ._aare import GenerateMoench03PixelMap \ No newline at end of file +from ._aare import GenerateMoench03PixelMap, GenerateMoench05PixelMap \ No newline at end of file diff --git a/python/examples/play.py b/python/examples/play.py index 733aa43..bff4960 100644 --- a/python/examples/play.py +++ b/python/examples/play.py @@ -14,8 +14,12 @@ 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/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json') +fpath = Path('/Users/erik/data/Moench05/moench05_master_0.json') f = aare.File(fpath) +f.seek(437) frame = f.read_frame() -plt.imshow(frame) \ No newline at end of file +m = aare.GenerateMoench05PixelMap() +img = np.take(frame, m.astype(np.int64)) + diff --git a/python/src/pixel_map.hpp b/python/src/pixel_map.hpp index 31b8f31..5791024 100644 --- a/python/src/pixel_map.hpp +++ b/python/src/pixel_map.hpp @@ -16,6 +16,10 @@ void define_pixel_map_bindings(py::module &m) { m.def("GenerateMoench03PixelMap", []() { auto ptr = new NDArray(GenerateMoench03PixelMap()); return return_image_data(ptr); + }) + .def("GenerateMoench05PixelMap", []() { + auto ptr = new NDArray(GenerateMoench05PixelMap()); + return return_image_data(ptr); }); } \ No newline at end of file diff --git a/src/PixelMap.cpp b/src/PixelMap.cpp index e0d82bc..dda6ec6 100644 --- a/src/PixelMap.cpp +++ b/src/PixelMap.cpp @@ -4,9 +4,10 @@ namespace aare { 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, 0, 25, 50, 75, 0, 25, 50, 75}; + 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, + 0, 25, 50, 75, 0, 25, 50, 75}; int const sc_width = 25; int const nadc = 32; int const pixels_per_sc = 5000; @@ -28,4 +29,27 @@ NDArray GenerateMoench03PixelMap() { } return order_map; } + +NDArray GenerateMoench05PixelMap() { + std::array adc_numbers = {9, 13, 1}; + 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++) { + n_pixel = row * 50 + i_col; + for (int i_sc = 0; i_sc < 3; i_sc++) { + int col = 50 * i_sc + i_col; + int adc_nr = adc_numbers[i_sc]; + int i_analog = n_pixel * 32 + adc_nr; + + + // analog_frame[row * 150 + col] = analog_data[i_analog] & 0x3FFF; + order_map(row, col) = i_analog; + + } + } + } + return order_map; +} + } // namespace aare \ No newline at end of file diff --git a/src/RawFile.cpp b/src/RawFile.cpp index 030bf48..eab6e72 100644 --- a/src/RawFile.cpp +++ b/src/RawFile.cpp @@ -13,7 +13,7 @@ namespace aare { RawFile::RawFile(const std::filesystem::path &fname, const std::string &mode, const FileConfig &config) { m_mode = mode; m_fname = fname; - if (mode == "r" || mode == "r+") { + if (mode == "r") { if (config != FileConfig()) { // aare::logger::warn( // "In read mode it is not necessary to provide a config, the provided config will be ignored"); @@ -23,97 +23,11 @@ RawFile::RawFile(const std::filesystem::path &fname, const std::string &mode, co find_number_of_subfiles(); find_geometry(); open_subfiles(); - - } else if (mode == "w" || mode == "w+") { - - if (std::filesystem::exists(fname)) { - // handle mode w as w+ (no overrwriting) - throw std::runtime_error(LOCATION + "File already exists"); - } - - parse_config(config); - parse_fname(); - write_master_file(); - n_subfiles = 1; - n_subfile_parts = 1; - subfile_cols = m_cols; - subfile_rows = m_rows; - open_subfiles(); - } else { - throw std::runtime_error(LOCATION + "Unsupported mode"); + throw std::runtime_error(LOCATION + "Unsupported mode. Can only read RawFiles."); } } -void RawFile::parse_config(const FileConfig &config) { - m_bitdepth = config.dtype.bitdepth(); - m_total_frames = config.total_frames; - m_rows = config.rows; - m_cols = config.cols; - m_type = config.detector_type; - max_frames_per_file = config.max_frames_per_file; - m_geometry = config.geometry; - version = config.version; - subfile_rows = config.geometry.row; - subfile_cols = config.geometry.col; - - if (m_geometry != aare::xy{1, 1}) { - throw std::runtime_error(LOCATION + "Only geometry {1,1} files are supported for writing"); - } -} -void RawFile::write_master_file() { - if (m_ext != ".json") { - throw std::runtime_error(LOCATION + "only json master files are supported for writing"); - } - std::ofstream ofs(master_fname(), std::ios::binary); - std::string ss; - ss.reserve(1024); - ss += "{\n\t"; - aare::write_str(ss, "Version", version); - ss += "\n\t"; - aare::write_digit(ss, "Total Frames", m_total_frames); - ss += "\n\t"; - aare::write_str(ss, "Detector Type", ToString(m_type)); - ss += "\n\t"; - aare::write_str(ss, "Geometry", m_geometry.to_string()); - ss += "\n\t"; - - uint64_t img_size = (m_cols * m_rows) / (static_cast(m_geometry.col * m_geometry.row)); - img_size *= m_bitdepth; - aare::write_digit(ss, "Image Size in bytes", img_size); - ss += "\n\t"; - aare::write_digit(ss, "Max Frames Per File", max_frames_per_file); - ss += "\n\t"; - aare::write_digit(ss, "Dynamic Range", m_bitdepth); - ss += "\n\t"; - const aare::xy pixels = {static_cast(m_rows / m_geometry.row), - static_cast(m_cols / m_geometry.col)}; - aare::write_str(ss, "Pixels", pixels.to_string()); - ss += "\n\t"; - aare::write_digit(ss, "Number of rows", m_rows); - ss += "\n\t"; - const std::string tmp = "{\n" - " \"Frame Number\": \"8 bytes\",\n" - " \"Exposure Length\": \"4 bytes\",\n" - " \"Packet Number\": \"4 bytes\",\n" - " \"Bunch Id\": \"8 bytes\",\n" - " \"Timestamp\": \"8 bytes\",\n" - " \"Module Id\": \"2 bytes\",\n" - " \"Row\": \"2 bytes\",\n" - " \"Column\": \"2 bytes\",\n" - " \"Reserved\": \"2 bytes\",\n" - " \"Debug\": \"4 bytes\",\n" - " \"RoundRNumber\": \"2 bytes\",\n" - " \"DetType\": \"1 byte\",\n" - " \"Version\": \"1 byte\",\n" - " \"Packet Mask\": \"64 bytes\"\n" - " }"; - - ss += "\"Frame Header Format\":" + tmp + "\n"; - ss += "}"; - ofs << ss; - ofs.close(); -} void RawFile::open_subfiles() { if (m_mode == "r") @@ -230,6 +144,15 @@ void RawFile::parse_json_metadata() { }catch (const json::out_of_range &e) { m_analog_samples = 0; } + try{ + std::string adc_mask = j.at("ADC Mask"); + m_adc_mask = std::stoul(adc_mask, nullptr, 16); + // fmt::print("ADC Mask: {}, n_set: {}\n", m_adc_mask, __builtin_popcount(m_adc_mask)); + }catch (const json::out_of_range &e) { + m_adc_mask = 0; + } + + try { m_digital_samples = j.at("Digital Samples"); }catch (const json::out_of_range &e) { @@ -244,6 +167,12 @@ void RawFile::parse_json_metadata() { m_type = DetectorType::Moench03_old; } + //Here we know we have a ChipTestBoard file update the geometry? + //TODO! Carry on information about digtial, and transceivers + if (m_type == DetectorType::ChipTestBoard) { + subfile_rows = 1; + subfile_cols = m_analog_samples*__builtin_popcount(m_adc_mask); + } @@ -393,32 +322,9 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) { } delete[] part_buffer; } - - //TODO! deal with ROI! - - // if (m_type == DetectorType::Moench03_old) { - // auto *data = reinterpret_cast(frame_buffer); - // for (size_t i = 0; i < m_rows * m_cols; i++) { - // data[i] = pixel_map[data[i]]; - // } - // } - } -void RawFile::write(Frame &frame, sls_detector_header header) { - if (m_mode == "r") { - throw std::runtime_error(LOCATION + "File is open in read mode"); - } - size_t const subfile_id = this->current_frame / this->max_frames_per_file; - for (size_t part_idx = 0; part_idx != this->n_subfile_parts; ++part_idx) { - - this->subfiles[subfile_id][part_idx]->write_part(frame.data(), header, - this->current_frame % this->max_frames_per_file); - } - this->current_frame++; -} - std::vector RawFile::read_n(size_t n_frames) { // TODO: implement this in a more efficient way std::vector frames; @@ -450,15 +356,7 @@ size_t RawFile::frame_number(size_t frame_index) { RawFile::~RawFile() noexcept { - // update master file - if (m_mode == "w" || m_mode == "w+" || m_mode == "r+") { - try { - write_master_file(); - } catch (...) { - // aare::logger::warn(LOCATION + "Could not update master file"); - } - } - + //TODO! Fix this, for file closing for (auto &vec : subfiles) { for (auto *subfile : vec) { delete subfile;