// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only // Summation split between the card and the host, on the mock of the card: the card sums the largest divisor of the // summation up to 256, the host the card's images of one image. The pixels of both receivers are checked against // a sum done here. #include #include #include #include "../frame_serialize/CBORStream2Deserializer.h" #include "../image_pusher/ImagePusher.h" #include "../receiver/FPGAImagePuller.h" #include "../receiver/FrameTransformation.h" #include "../receiver/JFJochReceiverService.h" #include "MockCardOutput.h" namespace { struct SumReceived { DataMessage message; std::vector pixels; }; class SumPusher : public ImagePusher { public: std::mutex m; std::map images; void StartDataCollection(StartMessage &) override {} bool EndDataCollection(const EndMessage &) override { return true; } bool SendCalibration(const CompressedImage &) override { return true; } bool SendImage(const uint8_t *image_data, size_t image_size, int64_t image_number) override { auto d = CBORStream2Deserialize(image_data, image_size); SumReceived r; r.message = d->data_message.value(); r.message.image.GetUncompressed(r.pixels); r.message.image = CompressedImage(); std::unique_lock ul(m); images.emplace(image_number, std::move(r)); return true; } std::string PrintSetup() const override { return "summation"; } ImagePusherType GetType() const override { return ImagePusherType::Test; } }; DiffractionExperiment SumExperiment(int64_t summation, int64_t images) { DiffractionExperiment x(DetJF(2)); x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true).InternalPacketGeneratorImages(1) .ImagesPerTrigger(images).ImagesPerFile(10).IncidentEnergy_keV(12.4) .DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(600); x.Conversion(); x.Summation(summation).Compression(CompressionAlgorithm::BSHUF_LZ4); return x; } constexpr size_t ErrPixel = 100, SatPixel = 200, LargePixel = 300; constexpr int64_t CardImageCount = 5; // What the card writes for its images, in its pixel format: a pattern, one pixel in error in the second image, one // saturated in the third, and one large enough that three of them saturate in 32 bits std::vector CardSums(const DiffractionExperiment &x) { const bool wide = x.GetByteDepthFPGA() == 4; std::vector ret; std::vector values(RAW_MODULE_SIZE); for (int64_t f = 0; f < CardImageCount; f++) { CardImage image; for (int m = 0; m < x.GetModulesNum(); m++) { for (size_t i = 0; i < RAW_MODULE_SIZE; i++) values[i] = (f * 7 + m * 3 + static_cast(i)) % 1000; values[ErrPixel] = (f == 1) ? (wide ? INT32_MIN : INT16_MIN) : 5; values[SatPixel] = (f == 2) ? (wide ? INT32_MAX : INT16_MAX) : 5; values[LargePixel] = wide ? INT32_MAX / 3 + 1 : 30000; auto output = std::make_unique(); StorePixels(*output, x, values); output->module_statistics.pixel_sum = 1000 * f + m; image.push_back(std::move(output)); } ret.push_back(std::move(image)); } return ret; } template int32_t Pixel(const DeviceOutput &output, size_t i) { return reinterpret_cast(output.pixels)[i]; } // The image summed here from the card's images, by ModuleSummation's rule: an error in any is an error, then a // saturated one or a sum reaching INT32_MAX is saturated. Assembled by FrameTransformation, as the receivers do. std::vector Reference(const DiffractionExperiment &x, const std::vector &cards, int64_t image) { const bool wide = x.GetByteDepthFPGA() == 4; const int64_t err_src = wide ? INT32_MIN : INT16_MIN; const int64_t sat_src = wide ? INT32_MAX : INT16_MAX; const int64_t h = x.GetHostSummation(); FrameTransformation transformation(x); for (int m = 0; m < x.GetModulesNum(); m++) { auto output = std::make_unique(); memset(output.get(), 0, sizeof(DeviceOutput)); output->module_statistics.module_number = m; auto sum = reinterpret_cast(output->pixels); for (size_t i = 0; i < RAW_MODULE_SIZE; i++) { int64_t acc = 0; for (int64_t k = 0; k < h; k++) { const auto &card = *cards[(image * h + k) % CardImageCount][m]; const int64_t v = wide ? Pixel(card, i) : Pixel(card, i); if (v == err_src || acc == INT32_MIN) acc = INT32_MIN; else if (v == sat_src || acc == INT32_MAX || acc + v >= INT32_MAX) acc = INT32_MAX; else acc += v; } sum[i] = static_cast(acc); } transformation.ProcessModule(output.get(), 0); } auto p = static_cast(transformation.GetImage()); return {p, p + x.GetPixelsNum() * x.GetByteDepthImage()}; } std::map Run(const DiffractionExperiment &x, bool puller) { auto devices = MockDevices(x, CardSums(x)); Logger logger("FPGAImagePullerSummation"); SumPusher pusher; JFJochReceiverService service(devices, logger, pusher, 256); SpotFindingSettings spots = DiffractionExperiment::DefaultDataProcessingSettings(); spots.indexing = false; service.NumThreads(4).SetSpotFindingSettings(spots); service.FPGAImagePullerSettings(puller); PixelMask mask(x); const JFCalibration calib = GeneratePedestalCalibration(x); service.Start(x, mask, &calib); const auto out = service.Stop(); CHECK(out.efficiency == 1.0f); CHECK(out.received_packets.empty() == puller); // only the FPGA receiver fills these return std::move(pusher.images); } void CheckSummation(int64_t summation, int64_t card, int64_t host) { const auto x = SumExperiment(summation, 2); REQUIRE(x.GetFPGASummation() == card); REQUIRE(x.GetHostSummation() == host); REQUIRE(x.GetByteDepthImage() == 4); const auto cards = CardSums(x); for (const bool puller: {false, true}) { INFO("puller " << puller); const auto images = Run(x, puller); REQUIRE(images.size() == 2); for (const auto &[n, r]: images) { INFO("image " << n); CHECK(r.message.packets_received == r.message.packets_expected); CHECK(r.message.packets_expected == 512 * summation * x.GetModulesNum()); CHECK(r.pixels == Reference(x, cards, n)); // From the card's module statistics: only the FPGA receiver keeps them if (!puller) { int64_t pixel_sum = 0; for (int64_t k = 0; k < host; k++) for (int m = 0; m < x.GetModulesNum(); m++) pixel_sum += 1000 * ((n * host + k) % CardImageCount) + m; CHECK(r.message.pixel_sum == pixel_sum); } } } } } TEST_CASE("FPGAImagePuller_Summation256_CardOnly", "[FPGAImagePuller]") { CheckSummation(256, 256, 1); } TEST_CASE("FPGAImagePuller_Summation1000_Hybrid", "[FPGAImagePuller]") { CheckSummation(1000, 250, 4); } TEST_CASE("FPGAImagePuller_Summation257_HostOnly", "[FPGAImagePuller]") { CheckSummation(257, 1, 257); }