diff --git a/CMakeLists.txt b/CMakeLists.txt index fc51c14..43dd767 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -344,6 +344,7 @@ endif() ###------------------------------------------------------------------------------------------ set(PUBLICHEADERS + include/aare/AngleCalibration.hpp include/aare/ArrayExpr.hpp include/aare/CalculateEta.hpp include/aare/Cluster.hpp @@ -433,6 +434,7 @@ endif() if(AARE_TESTS) set(TestSources ${CMAKE_CURRENT_SOURCE_DIR}/src/algorithm.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/AngleCalibration.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/defs.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/decode.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Dtype.test.cpp diff --git a/include/aare/AngleCalibration.hpp b/include/aare/AngleCalibration.hpp new file mode 100644 index 0000000..a8007b1 --- /dev/null +++ b/include/aare/AngleCalibration.hpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include + +// function check connected that reads from a file if modules are connected or +// not - which module though? + +// function read flatfield store in ff_corr probably correlation field + +// class variables: + +namespace aare { + +// TODO: can i have a static struct, constexpr? +struct MythenSpecifications { + + static constexpr int32_t max_modules = 48; + static constexpr int32_t channels_per_module = 1280; + static constexpr double pitch = + 0.05; // what is this pitch pitch is up e.g. rotation aroung y axis + static constexpr double ttmin = -180.0; // what is this the angle + static constexpr float ttmax = 180.0; + static constexpr float ttstep = + 0.0036; // probably here to calculate bin size +}; + +// number_of_activated_modules +// number_of_channles +// number_of_dimension +// is bad array keeping track of bad channels!! +class AngleCalibration { + + public: + AngleCalibration() { + centers.reserve(MythenSpecifications::max_modules); + conversions.reserve(MythenSpecifications::max_modules); + offsets.reserve(MythenSpecifications::max_modules); + } + + void read_initial_calibration_from_file(const std::string &filename); + + protected: + std::vector centers; + std::vector conversions; + std::vector offsets; +}; + +// read hdf5 files - > do they store the histogram? what angles do they store? + +// TODO what kind of file does it need to support? - probably code a csv parser +void AngleCalibration::read_initial_calibration_from_file( + const std::string &filename) { + + std::string line; + uint32_t module_number{}; + + try { + std::ifstream file(filename, std::ios_base::in); + if (!file.good()) { + throw std::logic_error("file does not exist"); + } + + std::stringstream file_buffer; + file_buffer << file.rdbuf(); + + while (file_buffer >> line) { + if (line == "module") { + file_buffer >> line; + module_number = std::stoi(line); + } + if (line == "center") { + file_buffer >> line; + centers.insert(centers.begin() + module_number, + std::stod(line)); + } + if (line == "conversion") { + file_buffer >> line; + conversions.insert(conversions.begin() + module_number, + std::stod(line)); + } + if (line == "offset") { + file_buffer >> line; + offsets.insert(offsets.begin() + module_number, + std::stod(line)); + } + } + + file.close(); + } catch (const std::exception &e) { + std::cerr << "Error: " << e.what() + << std::endl; // TODO: replace with log + } + + // angle_sign = signbit(conversion); + // signed_angles = std::abs(conversion); + // inverse_angles = 1./signed_angles + // store [centers, signed_conversions, offsets] + // dont know what conversion and offset is - dont know what calculations one + // does there the errors are actually not needed +} + +} // namespace aare \ No newline at end of file diff --git a/src/AngleCalibration.test.cpp b/src/AngleCalibration.test.cpp new file mode 100644 index 0000000..d103705 --- /dev/null +++ b/src/AngleCalibration.test.cpp @@ -0,0 +1,53 @@ +/************************************************ + * @file AngleCalibration.test.cpp + * @short test case for reading ascii file + ***********************************************/ + +#include "aare/AngleCalibration.hpp" + +#include + +#include +#include +#include + +class AngleCalibrationTestClass : public aare::AngleCalibration { + + public: + AngleCalibrationTestClass() = default; + ~AngleCalibrationTestClass() = default; + + std::vector get_centers() { return centers; } + + std::vector get_conversions() { return conversions; } + + std::vector get_offsets() { return offsets; } +}; + +TEST_CASE("read initial angle calibration file", + "[.anglecalibration][.fileread]") { + + AngleCalibrationTestClass anglecalibration; + + std::string filename = + "/home/mazzol_a/Documents/mythen3tools/beamline/" + "TDATA/Angcal_2E_Feb2023_P29.off"; // TODO change path upload data + + REQUIRE(std::filesystem::exists(filename)); + + anglecalibration.read_initial_calibration_from_file(filename); + + auto centers = anglecalibration.get_centers(); + auto conversions = anglecalibration.get_conversions(); + auto offsets = anglecalibration.get_offsets(); + + std::cout.precision(17); + + CHECK(centers.size() == 48); + CHECK(conversions.size() == 48); + CHECK(offsets.size() == 48); + + CHECK(centers[9] == Catch::Approx(660.342326)); + CHECK(offsets[47] == Catch::Approx(5.8053312)); + CHECK(conversions[27] == Catch::Approx(-0.6581179125e-4)); +} \ No newline at end of file