Files
Jungfraujoch/image_analysis/RadialIntegrationMappingFPGA.cpp

79 lines
3.1 KiB
C++

// Copyright (2019-2023) Paul Scherrer Institute
#include <cmath>
#include "RadialIntegrationMappingFPGA.h"
#include "../common/RawToConvertedGeometry.h"
#include "../common/DiffractionGeometry.h"
RadialIntegrationMappingFPGA::RadialIntegrationMappingFPGA(const DiffractionExperiment &experiment, uint16_t data_stream) :
low_q(experiment.GetLowQForRadialInt_recipA()),
high_q(experiment.GetHighQForRadialInt_recipA()),
q_spacing(experiment.GetQSpacingForRadialInt_recipA()),
pixel_to_bin(experiment.GetModulesNum(data_stream) * RAW_MODULE_SIZE),
max_bin_number(0)
{
if (q_spacing < 0.0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Q-spacing has to be >= 0.0");
if (low_q >= high_q)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Low-Q must be smaller than high-Q");
if (low_q <= 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Low-Q must be > 0");
if (std::ceil((high_q-low_q) / q_spacing ) >= FPGA_INTEGRATION_BIN_COUNT)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Cannot accommodate more than " + std::to_string(FPGA_INTEGRATION_BIN_COUNT) + " rings");
for (int m = 0; m < experiment.GetModulesNum(data_stream); m++) {
for (int pxl = 0; pxl < RAW_MODULE_SIZE; pxl++) {
auto [x,y] = RawToConvertedCoordinate(experiment, m + experiment.GetFirstModuleOfDataStream(data_stream), pxl);
double pixel_q = 2 * M_PI / PxlToRes(experiment, x, y);
if ((pixel_q < low_q) || (pixel_q >= high_q))
pixel_to_bin[m * RAW_MODULE_SIZE + pxl] = UINT16_MAX;
else
pixel_to_bin[m * RAW_MODULE_SIZE + pxl] = std::floor((pixel_q - low_q) / q_spacing);
}
}
// In principle max bin number is equal to std::floor((high_q - low_q) / q_spacing), but it might be lower than this
// depending on detector distance and beam center position
for (const auto &i: pixel_to_bin) {
if ((i != UINT16_MAX) && (i > max_bin_number))
max_bin_number = i;
}
bin_to_q.resize(max_bin_number + 1);
for (int i = 0; i < max_bin_number + 1; i++)
bin_to_q[i] = static_cast<float>(q_spacing * (i + 0.5) + low_q);
solid_angle_corr.resize(max_bin_number + 1);
for (int i = 0; i < max_bin_number + 1; i++)
solid_angle_corr[i] = 1.0f / CalcRadIntSolidAngleCorr(experiment, bin_to_q[i]);
}
uint16_t RadialIntegrationMappingFPGA::GetBinNumber() const {
return max_bin_number + 1;
}
const std::vector<uint16_t> &RadialIntegrationMappingFPGA::GetPixelToBinMapping() const {
return pixel_to_bin;
}
const std::vector<float> &RadialIntegrationMappingFPGA::GetBinToQ() const {
return bin_to_q;
}
const std::vector<float> &RadialIntegrationMappingFPGA::GetSolidAngleCorr() const {
return solid_angle_corr;
}
double RadialIntegrationMappingFPGA::QToBin(double q) const {
return std::min(static_cast<double>(max_bin_number), std::max(0.0, (q - low_q) / q_spacing));
}