diff --git a/.clang-format b/.clang-format index 2a4b7953..d6ec39a3 100644 --- a/.clang-format +++ b/.clang-format @@ -2,6 +2,6 @@ BasedOnStyle: LLVM IndentWidth: 4 UseTab: Never -ColumnLimit: 80 +ColumnLimit: 120 AlignConsecutiveAssignments: false AlignConsecutiveMacros: true \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 10e21c5c..930311e7 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -12,7 +12,8 @@ "cppStandard": "c++17", "intelliSenseMode": "linux-gcc-x64", "configurationProvider": "ms-vscode.cmake-tools", - "compilerArgs": [] + "compilerArgs": [], + "compileCommands": "/home/l_bechir/github/aare/build/compile_commands.json" } ], "version": 4 diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..ba0f5fea --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,55 @@ +{ + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/aare", + "args": [], + "stopAtEntry": true, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + }, + { + "name": "C/C++: g++ build and debug active file", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ], + "preLaunchTask": "C/C++: g++ build active file", + "miDebuggerPath": "/usr/bin/gdb" + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index c31a2abf..3e8d822e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -70,7 +70,22 @@ "stop_token": "cpp", "thread": "cpp", "valarray": "cpp", - "variant": "cpp" + "variant": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "format": "cpp", + "__bit_reference": "cpp", + "__config": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__node_handle": "cpp", + "__split_buffer": "cpp", + "__threading_support": "cpp", + "__verbose_abort": "cpp", + "execution": "cpp", + "ios": "cpp", + "locale": "cpp" }, "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..eb42ea1a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,25 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": "build", + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index eb20097c..f3c82b31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ find_package(fmt 6 REQUIRED) set(CMAKE_BUILD_TYPE "Debug") if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(OPTIMIZATION_FLAGS "-Og -ggdb3") + set(OPTIMIZATION_FLAGS "-Og -ggdb3 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC") else() set(OPTIMIZATION_FLAGS "-O3") endif() diff --git a/data/m3_d0_f0_0.raw b/data/m3_d0_f0_0.raw new file mode 100644 index 00000000..ab85ccf2 Binary files /dev/null and b/data/m3_d0_f0_0.raw differ diff --git a/data/m3_master_0.json b/data/m3_master_0.json new file mode 100644 index 00000000..f6b4f316 --- /dev/null +++ b/data/m3_master_0.json @@ -0,0 +1,55 @@ +{ + "Version": 7.1, + "Timestamp": "Wed Sep 21 13:48:10 2022", + "Detector Type": "Mythen3", + "Timing Mode": "auto", + "Geometry": { + "x": 1, + "y": 1 + }, + "Image Size in bytes": 15360, + "Pixels": { + "x": 3840, + "y": 1 + }, + "Max Frames Per File": 10000, + "Frame Discard Policy": "nodiscard", + "Frame Padding": 1, + "Scan Parameters": "[disabled]", + "Total Frames": 1, + "Receiver Roi": { + "xmin": 4294967295, + "xmax": 4294967295, + "ymin": 4294967295, + "ymax": 4294967295 + }, + "Dynamic Range": 32, + "Ten Giga": 1, + "Period": "2ms", + "Counter Mask": "0x7", + "Exptime1": "0.1s", + "Exptime2": "0.1s", + "Exptime3": "0.1s", + "GateDelay1": "0ns", + "GateDelay2": "0ns", + "GateDelay3": "0ns", + "Gates": 1, + "Threshold Energies": "[-1, -1, -1]", + "Frames in File": 1, + "Frame Header Format": { + "Frame Number": "8 bytes", + "SubFrame Number/ExpLength": "4 bytes", + "Packet Number": "4 bytes", + "Bunch ID": "8 bytes", + "Timestamp": "8 bytes", + "Module Id": "2 bytes", + "Row": "2 bytes", + "Column": "2 bytes", + "Reserved": "2 bytes", + "Debug": "4 bytes", + "Round Robin Number": "2 bytes", + "Detector Type": "1 byte", + "Header Version": "1 byte", + "Packets Caught Mask": "64 bytes" + } +} \ No newline at end of file diff --git a/src/file_io/CMakeLists.txt b/src/file_io/CMakeLists.txt index f4029196..f990c9b9 100644 --- a/src/file_io/CMakeLists.txt +++ b/src/file_io/CMakeLists.txt @@ -4,6 +4,7 @@ target_include_directories(aare PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") set(FILE_IO_SOURCES "file/File.cpp" "file/JsonFile.cpp" + "file/SubFile.cpp" "file_factory/FileFactory.cpp" "file_factory/JsonFileFactory.cpp" "file_factory/RawFileFactory.cpp" diff --git a/src/file_io/file/File.hpp b/src/file_io/file/File.hpp index ed101a59..269e3941 100644 --- a/src/file_io/file/File.hpp +++ b/src/file_io/file/File.hpp @@ -1,15 +1,33 @@ #pragma once -#include #include "defs.hpp" +#include #include +#include "SubFile.hpp" + +struct RawFileConfig { + int module_gap_row{}; + int module_gap_col{}; + + bool operator==(const RawFileConfig &other) const { + if (module_gap_col != other.module_gap_col) + return false; + if (module_gap_row != other.module_gap_row) + return false; + return true; + } +}; + + -class File -{ -public: -std::filesystem::path fname; +class File { + private: + using config = RawFileConfig; + public: + std::vector subfiles; + std::filesystem::path fname; std::filesystem::path base_path; - std::string base_name,ext; + std::string base_name, ext; int findex, n_subfiles; size_t total_frames{}; @@ -17,27 +35,22 @@ std::filesystem::path fname; DetectorType type; TimingMode timing_mode; int subfile_rows, subfile_cols; - bool quad {false}; - - - + bool quad{false}; ssize_t rows{}; ssize_t cols{}; uint8_t bitdepth{}; std::vector positions; + config cfg{0,0}; // File(); // ~File(); - - - - inline size_t bytes_per_frame() const{ - return rows*cols*bitdepth/8; - } - inline size_t pixels() const{ - return rows*cols; + inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; } + inline size_t pixels() const { return rows * cols; } + inline void set_config(int row,int col){ + cfg.module_gap_row = row; + cfg.module_gap_col = col; } // TODO! Deal with fast quad and missing files @@ -49,13 +62,13 @@ std::filesystem::path fname; n_subfiles = n_mod; } - inline std::filesystem::path master_fname() { - return base_path / fmt::format("{}_master_{}{}", base_name, findex, ext); + return base_path / + fmt::format("{}_master_{}{}", base_name, findex, ext); } inline std::filesystem::path data_fname(int mod_id, int file_id) { - return base_path / fmt::format("{}_d{}_f{}_{}.raw", base_name, mod_id, file_id, findex); + return base_path / fmt::format("{}_d{}_f{}_{}.raw", base_name, mod_id, + file_id, findex); } // size_t total_frames(); - }; diff --git a/src/file_io/file/SubFile.cpp b/src/file_io/file/SubFile.cpp new file mode 100644 index 00000000..2e4dd703 --- /dev/null +++ b/src/file_io/file/SubFile.cpp @@ -0,0 +1,71 @@ +#include "SubFile.hpp" +#include +// #include + +/** + * SubFile methods + * + * + */ +template +SubFile::SubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols) : rows(rows), cols(cols) { + fp = fopen(fname.c_str(), "rb"); + if (fp == nullptr) { + throw std::runtime_error("Could not open file " + fname.string()); + } + std::cout<<"File opened"< +NormalSubFile::NormalSubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols) + : SubFile(fname, rows, cols){}; + +template size_t NormalSubFile::read_impl(std::byte *buffer) { + return fread(reinterpret_cast(buffer), sizeof(DataType) * this->rows * this->cols, 1, this->fp); +}; + + + +/** + * ReorderM03SubFile methods +*/ +template +ReorderM03SubFile::ReorderM03SubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols) + : SubFile(fname, rows, cols){}; + +template size_t ReorderM03SubFile::read_impl(std::byte *buffer) { + std::vector tmp(this->pixels_per_frame()); + size_t rc = fread(reinterpret_cast(&tmp[0]), this->bytes_per_frame(), 1, this->fp); + + int adc_nr[32] = {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 sc_width = 25; + int nadc = 32; + int pixels_per_sc = 5000; + + auto dst = reinterpret_cast(buffer); + int pixel = 0; + for (int i = 0; i != pixels_per_sc; ++i) { + for (int i_adc = 0; i_adc != nadc; ++i_adc) { + int col = adc_nr[i_adc] + (i % sc_width); + int row; + if ((i_adc / 4) % 2 == 0) + row = 199 - int(i / sc_width); + else + row = 200 + int(i / sc_width); + + dst[col + row * 400] = tmp[pixel]; + pixel++; + } + } + return rc; +}; + +template class NormalSubFile; +template class NormalSubFile; +template class ReorderM03SubFile; diff --git a/src/file_io/file/SubFile.hpp b/src/file_io/file/SubFile.hpp new file mode 100644 index 00000000..4d7980a6 --- /dev/null +++ b/src/file_io/file/SubFile.hpp @@ -0,0 +1,47 @@ +#pragma once +#include "defs.hpp" +#include +#include +#include + +template class SubFile { + protected: + FILE *fp = nullptr; + + public: + SubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols); + inline size_t bytes_per_frame() const { return sizeof(DataType) * rows * cols; } + + inline size_t pixels_per_frame() const { return rows * cols; } + + + ssize_t rows{}; + ssize_t cols{}; + ssize_t n_frames{}; + int sub_file_index_{}; + size_t read_impl(std::byte *buffer){}; + +}; + +template class NormalSubFile : public SubFile { + public: + NormalSubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols); + size_t read_impl(std::byte *buffer); +}; + +// template +// class FlipSubFile : public SubFile{ +// size_t read_impl() override; +// }; + +template class ReorderM03SubFile : public SubFile { + public: + ReorderM03SubFile(std::filesystem::path fname, ssize_t rows, ssize_t cols); + size_t read_impl(std::byte *buffer); +}; + +using JungfrauSubFile = NormalSubFile; +using Moench03SubFile = ReorderM03SubFile; +using Mythen3SubFile = NormalSubFile; + +using SubFileVariants = std::variant; diff --git a/src/file_io/file_factory/FileFactory.cpp b/src/file_io/file_factory/FileFactory.cpp index 601108e7..46e69c48 100644 --- a/src/file_io/file_factory/FileFactory.cpp +++ b/src/file_io/file_factory/FileFactory.cpp @@ -72,5 +72,5 @@ void FileFactory::find_geometry(File& file) { file.rows = r * file.subfile_rows; file.cols = c * file.subfile_cols; - file.rows += (r - 1) * cfg.module_gap_row; + file.rows += (r - 1) * file.cfg.module_gap_row; } \ No newline at end of file diff --git a/src/file_io/file_factory/FileFactory.hpp b/src/file_io/file_factory/FileFactory.hpp index 6bf251cc..7182fb07 100644 --- a/src/file_io/file_factory/FileFactory.hpp +++ b/src/file_io/file_factory/FileFactory.hpp @@ -14,7 +14,7 @@ class FileFactory{ virtual void parse_metadata(File&){}; - void find_geometry(File&){}; + void find_geometry(File&); void parse_fname(File&); template Header read_header(const std::filesystem::path &fname); diff --git a/src/file_io/file_factory/JsonFileFactory.cpp b/src/file_io/file_factory/JsonFileFactory.cpp index 9b46c990..f3a909a8 100644 --- a/src/file_io/file_factory/JsonFileFactory.cpp +++ b/src/file_io/file_factory/JsonFileFactory.cpp @@ -1,33 +1,32 @@ #include "JsonFileFactory.hpp" #include "JsonFile.hpp" +#include "defs.hpp" #include "helpers.hpp" -#include #include -#include "defs.hpp" +#include #include +#include "SubFile.hpp" using json = nlohmann::json; - - - -JsonFileFactory::JsonFileFactory(std::filesystem::path fpath){ - if(not is_master_file(fpath)) +JsonFileFactory::JsonFileFactory(std::filesystem::path fpath) { + if (not is_master_file(fpath)) throw std::runtime_error("Json file is not a master file"); this->fpath = fpath; } -void JsonFileFactory::parse_metadata(File& file){ - std::cout<<"Parsing metadata"<> j; double v = j["Version"]; file.version = fmt::format("{:.1f}", v); - std::string tmp; - j["Detector Type"].get_to(tmp); - file.type = StringTo(tmp); - file.timing_mode = StringTo(j["Timing Mode"].get()); + // std::string tmp; + // j["Detector Type"].get_to(tmp); + file.type = StringTo(j["Detector Type"].get()); + file.timing_mode = + StringTo(j["Timing Mode"].get()); file.total_frames = j["Frames in File"]; file.subfile_cols = j["Pixels"]["x"]; file.subfile_rows = j["Pixels"]["y"]; @@ -40,24 +39,30 @@ void JsonFileFactory::parse_metadata(File& file){ if (file.type == DetectorType::Eiger) { file.quad = (j["Quad"] == 1); } +} - - - +void JsonFileFactory::open_subfiles(File &file) { + for (int i = 0; i != file.n_subfiles; ++i) { + if (file.type == DetectorType::Jungfrau) + file.subfiles.push_back(JungfrauSubFile(file.data_fname(i, 0), file.subfile_rows, file.subfile_cols)); + else if (file.type == DetectorType::Mythen3) + file.subfiles.push_back(Mythen3SubFile(file.data_fname(i, 0), file.subfile_rows, file.subfile_cols)); + else if (file.type == DetectorType::Moench) + file.subfiles.push_back(Moench03SubFile(file.data_fname(i, 0), file.subfile_rows, file.subfile_cols)); + else + throw std::runtime_error("File not supported"); + } } -File JsonFileFactory::loadFile(){ - std::cout<<"Loading json file"<parse_fname(file); this->parse_metadata(file); file.find_number_of_subfiles(); this->find_geometry(file); - - - - + this->open_subfiles(file); return file; } \ No newline at end of file diff --git a/src/file_io/file_factory/JsonFileFactory.hpp b/src/file_io/file_factory/JsonFileFactory.hpp index 42ef35d2..659594a3 100644 --- a/src/file_io/file_factory/JsonFileFactory.hpp +++ b/src/file_io/file_factory/JsonFileFactory.hpp @@ -8,6 +8,7 @@ class JsonFileFactory: public FileFactory File loadFile() override; void parse_metadata(File&) override; JsonFileFactory(std::filesystem::path fpath); + void open_subfiles(File&); }; diff --git a/src/main.cpp b/src/main.cpp index e90319fa..a1d3820c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ int main() { - FileFactory fileFactory=FileFactory::getFactory(std::filesystem::current_path()/"test.json"); - + FileFactory fileFactory=FileFactory::getFactory(std::filesystem::path("/home/l_bechir/github/aare")/"data"/"m3_master_0.json"); + std::cout<<"filefactory is of instance: "<