// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #pragma once // What the FPGA card writes, precalculated on the CPU for the tests on MockAcquisitionDevice. It does not matter how // it is calculated here: that the mock gives the host what the card gives is checked against the HLS simulation in // MockAcquisitionDeviceTest.cpp. #include "CheckImageOutput.h" #include "../acquisition_device/AcquisitionDeviceGroup.h" #include "../acquisition_device/MockAcquisitionDevice.h" #include "../common/PixelMask.h" #include "../image_analysis/spot_finding/DetModuleSpotFinder_cpu.h" #include "../receiver/JFJochReceiverTest.h" // The card output of one image, one entry per module (absolute module number) using CardImage = std::vector>; inline std::unique_ptr OutputWithPixels(const uint16_t *pixels) { auto ret = std::make_unique(); memcpy(ret->pixels, pixels, RAW_MODULE_SIZE * sizeof(uint16_t)); return ret; } template void StorePixels(DeviceOutput &output, const std::vector &values) { auto pixels = reinterpret_cast(output.pixels); for (int i = 0; i < RAW_MODULE_SIZE; i++) pixels[i] = static_cast(values[i]); } // The values in the pixel format the card writes for the experiment inline void StorePixels(DeviceOutput &output, const DiffractionExperiment &x, const std::vector &values) { switch (x.GetByteDepthFPGA()) { case 1: x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); break; case 2: x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); break; default: x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); break; } } // Error and saturated pixels counted, the others summed, masked pixels left out; and the same for each ROI of // the experiment's ROI map (one bit per ROI) inline void PixelStatistics(DeviceOutput &output, const DiffractionExperiment &x, const PixelMask &mask, const std::vector &experiment_roi_map, size_t module, const std::vector &values, int64_t err, int64_t sat) { std::vector roi_map(RAW_MODULE_SIZE); ConvertedToRawGeometry(x, module, roi_map.data(), experiment_roi_map.data()); const uint32_t *module_mask = mask.GetMaskRaw().data() + module * RAW_MODULE_SIZE; auto &stats = output.module_statistics; stats.max_value = INT24_MIN; stats.min_value = INT24_MAX; for (int r = 0; r < FPGA_ROI_COUNT; r++) output.roi_counts[r].max_value = INT24_MIN; for (int i = 0; i < RAW_MODULE_SIZE; i++) { const int64_t v = values[i]; if (module_mask[i] != 0) { stats.masked_pixels++; continue; } if (v == err) stats.err_pixels++; else if (v == sat) stats.saturated_pixels++; else { stats.pixel_sum += v; stats.max_value = std::max(stats.max_value, v); stats.min_value = std::min(stats.min_value, v); } // roi_calc.cpp: the coordinates include the multipixels const int64_t line = i / RAW_MODULE_COLS + (i / RAW_MODULE_COLS / 256) * 2; const int64_t col = i % RAW_MODULE_COLS + (i % RAW_MODULE_COLS / 256) * 2; for (int r = 0; r < FPGA_ROI_COUNT; r++) { if (((roi_map[i] >> r) & 1) == 0 || v == err) continue; ROICount &roi = output.roi_counts[r]; if (v != sat) { roi.sum += v; roi.sum2 += v * v; roi.good_pixels++; roi.sum_x_weighted += v * col; roi.sum_y_weighted += v * line; } roi.max_value = std::max(roi.max_value, v); } } } // The strong pixels of the card's spot finder, by its CPU version, on the pixels in the output inline void FindStrongPixels(DeviceOutput &output, const DiffractionExperiment &x, const SpotFindingSettings &settings, size_t module) { std::vector d_map(RAW_MODULE_SIZE), mean(RAW_MODULE_SIZE), stddev(RAW_MODULE_SIZE); std::vector valid_count(RAW_MODULE_SIZE), strong_pixel(RAW_MODULE_SIZE); x.CalcSpotFinderResolutionMap(d_map.data(), module); switch (x.GetByteDepthFPGA()) { case 1: FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), strong_pixel.data()); break; case 2: FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), strong_pixel.data()); break; default: FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), strong_pixel.data()); break; } } // JUNGFRAU conversion of a raw image (all modules) as CheckImageOutput.h computes it; with host summation the card // converts single frames to 16 bit. With conversion, error and saturated pixels are counted from the conversion into // signed 32 bit, as the card counts them before the pixel is written in the image's format, where unsigned they have // one value; without, the card reads UINT16_MAX from the detector as saturated. inline CardImage ConvertedImage(const DiffractionExperiment &x, const JFCalibration &calib, const PixelMask &mask, const std::vector &raw, size_t storage_cell = 0) { DiffractionExperiment card_x(x); if (x.IsCPUSummation()) card_x.Summation(1).BitDepthImage(16); DiffractionExperiment signed_x(card_x); signed_x.PixelSigned(true).BitDepthImage(32); const auto ref = ConversionRef(card_x, calib, raw.data(), x.GetModulesNum(), storage_cell); const auto ref_signed = ConversionRef(signed_x, calib, raw.data(), x.GetModulesNum(), storage_cell); const auto roi_map = x.ExportROIMap(); CardImage ret; std::vector values(RAW_MODULE_SIZE), stats_values(RAW_MODULE_SIZE); for (int m = 0; m < x.GetModulesNum(); m++) { for (size_t i = 0; i < RAW_MODULE_SIZE; i++) { const size_t pxl = m * RAW_MODULE_SIZE + i; values[i] = std::llround(ref[pxl]); if (x.IsJungfrauConvPhotonCnt()) stats_values[i] = std::llround(ref_signed[pxl]); else stats_values[i] = (raw[pxl] == UINT16_MAX) ? INT32_MAX : values[i]; } auto output = std::make_unique(); StorePixels(*output, card_x, values); PixelStatistics(*output, x, mask, roi_map, m, stats_values, INT32_MIN, INT32_MAX); ret.push_back(std::move(output)); } return ret; } // The card passing a signed 16-bit image (raw geometry, all modules) through, INT16_MIN marking an error and // INT16_MAX a saturated pixel, with the strong pixels of its spot finder inline CardImage Int16Image(const DiffractionExperiment &x, const PixelMask &mask, const std::vector &image, const SpotFindingSettings &settings) { const int64_t err = (x.GetByteDepthFPGA() == 4) ? INT32_MIN : INT16_MIN; const int64_t sat = (x.GetByteDepthFPGA() == 4) ? INT32_MAX : INT16_MAX; const auto roi_map = x.ExportROIMap(); CardImage ret; std::vector values(RAW_MODULE_SIZE); for (int m = 0; m < x.GetModulesNum(); m++) { for (int i = 0; i < RAW_MODULE_SIZE; i++) { const int16_t v = image[m * RAW_MODULE_SIZE + i]; values[i] = (v == INT16_MIN) ? err : ((v == INT16_MAX) ? sat : v); } auto output = std::make_unique(); StorePixels(*output, x, values); PixelStatistics(*output, x, mask, roi_map, m, values, err, sat); FindStrongPixels(*output, x, settings, m); ret.push_back(std::move(output)); } return ret; } // Frame F of the mock of data stream d writes images[F % images.size()] inline void SetOutputs(MockAcquisitionDevice &mock, const DiffractionExperiment &x, uint16_t data_stream, const std::vector &images) { for (size_t i = 0; i < images.size(); i++) for (int m = 0; m < x.GetModulesNum(data_stream); m++) mock.SetOutput(i, m, *images[i][x.GetFirstModuleOfDataStream(data_stream) + m]); } // One mock per data stream inline AcquisitionDeviceGroup MockDevices(const DiffractionExperiment &x, const std::vector &images) { AcquisitionDeviceGroup ret; for (int d = 0; d < x.GetDataStreamsNum(); d++) { auto mock = std::make_unique(d, 64); SetOutputs(*mock, x, d, images); ret.Add(std::move(mock)); } return ret; } // The mocks for JFJochReceiverTest(): the conversion of its raw image with its calibration, the pixel mask applied // if the experiment asks for it, one image per storage cell inline AcquisitionDeviceGroup ConvertingMocks(const DiffractionExperiment &x, const PixelMask &mask, std::vector raw) { if (x.IsApplyPixelMask()) { for (int i = 0; i < raw.size(); i++) if (mask.GetMaskRaw().at(i) != 0) raw[i] = UINT16_MAX; } const JFCalibration calib = GeneratePedestalCalibration(x); std::vector images; for (int s = 0; s < x.GetStorageCellNumber(); s++) images.push_back(ConvertedImage(x, calib, mask, raw, s)); return MockDevices(x, images); } // The default raw image of JFJochReceiverTest() inline AcquisitionDeviceGroup ConvertingMocks(const DiffractionExperiment &x, const PixelMask &mask) { std::vector raw(RAW_MODULE_SIZE * x.GetModulesNum()); for (int i = 0; i < raw.size(); i++) raw[i] = i % 65536; return ConvertingMocks(x, mask, raw); }