From cb95938bc700da5f754c8ac5865b6913b54f2197 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:09:27 +0100 Subject: [PATCH] Tests: receiver, state machine and writer tests on MockAcquisitionDevice; FPGAIntegrationTest removed The host-side tests run on the mock instead of the HLS simulation: JFJochReceiverIntegrationTest, JFJochReceiverProcessingTest (the GPU-stills cases too, in raw mode the card writes the generator's frame), JFJochStateMachineTest, StreamWriterTest. tests/MockCardOutput.h gives the mock what the card would write for a test's image - its conversion by the host's floating-point reference, pixel statistics, ROI counts and the strong pixels of the CPU spot finder - so the checks of the receiver's images and plots stay as they were. Two cases are new: a receiver on two cards at once (not possible with the HLS simulation) and a cancelled pedestal run. FPGAIntegrationTest.cpp (60 cases) tested the HLS device itself - conversion, summation, spot finder, integration, ROI, ADU histogram, pixel counts, in every mode - and is removed, not ported. Its host-side checks (FPGAAcquisitionDevice and AcquisitionCounters: the internal generator, data streams, missing modules, delays, lost frames, the last frame, bunch ID, the generator's memory limit) are FPGAAcquisitionDeviceTest.cpp on the mock, plus a check that the generator's frames are what the mock writes. From ce8918062 and 710edd61e (rc186), resolved against rc188: JFJochReceiverTest() without quick_integrate, the FPGA receiver that indexes and does not integrate, StillsGPUAnalysisFPGA. JFJochReceiverFPGA_RugnuxOnStaysPerImage is not taken (rc188 keeps live stills on the FPGA receiver). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- receiver/JFJochReceiverTest.cpp | 2 +- receiver/JFJochReceiverTest.h | 3 + tests/CMakeLists.txt | 3 +- tests/FPGAAcquisitionDeviceTest.cpp | 216 ++ tests/FPGAIntegrationTest.cpp | 2846 ----------------------- tests/JFJochReceiverIntegrationTest.cpp | 304 +-- tests/JFJochReceiverProcessingTest.cpp | 414 ++-- tests/JFJochStateMachineTest.cpp | 12 +- tests/MockCardOutput.h | 220 ++ tests/StreamWriterTest.cpp | 9 +- 10 files changed, 788 insertions(+), 3241 deletions(-) create mode 100644 tests/FPGAAcquisitionDeviceTest.cpp delete mode 100644 tests/FPGAIntegrationTest.cpp create mode 100644 tests/MockCardOutput.h diff --git a/receiver/JFJochReceiverTest.cpp b/receiver/JFJochReceiverTest.cpp index 6227a38c6..afbe755ba 100644 --- a/receiver/JFJochReceiverTest.cpp +++ b/receiver/JFJochReceiverTest.cpp @@ -8,7 +8,7 @@ #define STORAGE_CELL_FOR_TEST 11 -static JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x) { +JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x) { JFCalibration ret(x); std::vector pedestal_g0( RAW_MODULE_SIZE), pedestal_g1(RAW_MODULE_SIZE), pedestal_g2(RAW_MODULE_SIZE); diff --git a/receiver/JFJochReceiverTest.h b/receiver/JFJochReceiverTest.h index ce3dd5554..3606de6df 100644 --- a/receiver/JFJochReceiverTest.h +++ b/receiver/JFJochReceiverTest.h @@ -5,6 +5,9 @@ #pragma once +// The calibration JFJochReceiverTest() converts with +JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x); + bool JFJochReceiverTest(JFJochReceiverOutput &output, Logger &logger, AcquisitionDeviceGroup &aq_devices, const DiffractionExperiment &x, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 26ae59b55..55aa3f969 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,8 +69,9 @@ ADD_EXECUTABLE(jfjoch_test ../common/RawToConvertedGeometry.h ../common/DiffractionExperiment.h CheckImageOutput.h - FPGAIntegrationTest.cpp + MockCardOutput.h MockAcquisitionDeviceTest.cpp + FPGAAcquisitionDeviceTest.cpp StrongPixelSetTest.cpp AdaptiveThresholdTest.cpp AdaptiveSpotFinderCPUTest.cpp diff --git a/tests/FPGAAcquisitionDeviceTest.cpp b/tests/FPGAAcquisitionDeviceTest.cpp new file mode 100644 index 000000000..d3d8df1bb --- /dev/null +++ b/tests/FPGAAcquisitionDeviceTest.cpp @@ -0,0 +1,216 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +// The host side of the card - FPGAAcquisitionDevice and its AcquisitionCounters - on the mock of the card: what +// arrived, where it went, delays, the end of the data collection. The card itself is compared with the mock in +// MockAcquisitionDeviceTest.cpp. + +#include + +#include "../acquisition_device/MockAcquisitionDevice.h" + +TEST_CASE("FPGAAcquisitionDevice_InternalPacketGenerator", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); + + MockAcquisitionDevice test(0, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); + for (int image = 0; image < 4; image++) { + for (int m = 0; m < nmodules; m++) { + const auto &stats = test.GetDeviceOutput(image, m)->module_statistics; + CHECK(stats.frame_number == image); + CHECK(stats.module_number == m); + CHECK(stats.timestamp == INT_PKT_GEN_EXPTTIME * image); + CHECK(stats.pulse_id == INT_PKT_GEN_BUNCHID + image); + CHECK(stats.exptime == INT_PKT_GEN_EXPTTIME); + CHECK(stats.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); + } + } + REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); +} + +// Of four data streams, the card of the last one generates only its own module +TEST_CASE("FPGAAcquisitionDevice_InternalPacketGenerator_DataStream", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.DataStreams(4); + x.Raw(); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); + + MockAcquisitionDevice test(3, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 128 * 4 * JUNGFRAU_PACKET_SIZE_BYTES); + for (int image = 0; image < 4; image++) + REQUIRE(test.Counters().IsFullModuleCollected(image, 0)); +} + +// Module 0 runs on, module 1 stops after 3 frames, modules 2 and 3 never come: the slowest frame moves on once the +// fastest module is far enough ahead +TEST_CASE("FPGAAcquisitionDevice_MissingModules", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + const uint64_t nframes = AcquisitionCounters::ThresholdFramesLost + 5; + + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(nframes).NumTriggers(1); + + MockAcquisitionDevice test(0, nframes + 2); + test.CreateJFPackets(x, 1, nframes, 0, nullptr); + test.CreateJFPackets(x, 1, 3, 1, nullptr); + test.CreateFinalPacket(x); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.Counters().GetSlowestFrameNumber() == 5 - 1); + REQUIRE(test.Counters().GetBufferHandle(0, 0) != AcquisitionCounters::HandleNotFound); + REQUIRE(test.Counters().GetBufferHandle(0, 1) == AcquisitionCounters::HandleNotFound); + REQUIRE(test.Counters().GetBufferHandle(0, 2) == AcquisitionCounters::HandleNotFound); + REQUIRE(test.GetBytesReceived() == nframes * 128 * JUNGFRAU_PACKET_SIZE_BYTES); +} + +TEST_CASE("FPGAAcquisitionDevice_Delay", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + for (int f = 1; f <= 3; f++) + for (int m = 0; m < nmodules; m++) + test.CreateJFPacket(x, f, 0, m, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.Counters().CalculateDelay(0) == 2); + REQUIRE(test.Counters().CalculateDelay(0, 0) == 2); + REQUIRE(test.Counters().CalculateDelay(1) == 1); + REQUIRE(test.Counters().CalculateDelay(1, 0) == 1); + REQUIRE(test.Counters().CalculateDelay(2) == 0); + REQUIRE(test.Counters().CalculateDelay(2, 0) == 0); +} + +// A single packet of one module: that module of that frame is there, nothing else is +TEST_CASE("FPGAAcquisitionDevice_LostFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + test.CreateJFPacket(x, 1, 0, 0, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); + REQUIRE_NOTHROW(test.GetDeviceOutput(0, 0)); + REQUIRE(!test.Counters().IsFullModuleCollected(0, 0)); + REQUIRE_THROWS(test.GetDeviceOutput(0, 1)); + REQUIRE_THROWS(test.GetDeviceOutput(1, 0)); + REQUIRE_THROWS(test.GetDeviceOutput(2, 1)); +} + +TEST_CASE("FPGAAcquisitionDevice_OneFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1); + + MockAcquisitionDevice test(0, 64); + test.CreateJFPackets(x, 1, 1, 0, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + test.WaitForActionComplete(); + REQUIRE(test.Counters().IsAcquisitionFinished()); + + // address properly aligned + REQUIRE((uint64_t) test.GetDeviceOutput(0, 0)->pixels % 128 == 0); + + REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); + REQUIRE(test.Counters().GetCurrFrameNumber(0) == 0); + REQUIRE(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); +} + +// A frame far past the last one ends the data collection +TEST_CASE("FPGAAcquisitionDevice_DetectLastFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.PedestalG0Frames(0).NumTriggers(2).ImagesPerTrigger(5); + + MockAcquisitionDevice test(0, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE(!test.Counters().IsAcquisitionFinished()); + + test.CreateJFPacket(x, 15, 0, 0, nullptr); + test.WaitForActionComplete(); + REQUIRE(test.Counters().IsAcquisitionFinished()); +} + +TEST_CASE("FPGAAcquisitionDevice_BunchID", "[FPGAAcquisitionDevice]") { + const uint64_t bunchid = 0xAAAAAAAA87345600; + DiffractionExperiment x(DetJF(4)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + test.CreateXfelBunchIDPacket(bunchid, 0); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 0); + REQUIRE(test.GetDataCollectionStatus().current_pulseid == bunchid); +} + +TEST_CASE("FPGAAcquisitionDevice_InternalGeneratorMemory", "[FPGAAcquisitionDevice]") { + // 64 module frames in the card memory; the generator takes 63 at most + DiffractionExperiment x(DetJF(2)); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).NumTriggers(1); + MockAcquisitionDevice test(0, 64); + + x.InternalPacketGeneratorImages(31); + REQUIRE_NOTHROW(test.PrepareAction(x)); + + x.InternalPacketGeneratorImages(32); + REQUIRE_THROWS(test.PrepareAction(x)); + + std::vector frame(RAW_MODULE_SIZE); + REQUIRE_NOTHROW(test.SetInternalGeneratorFrame(frame.data(), 62)); + REQUIRE_THROWS(test.SetInternalGeneratorFrame(frame.data(), 63)); +} + +// The generator's frames are what the card writes when no output was set for a module +TEST_CASE("FPGAAcquisitionDevice_InternalGeneratorFrames", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 2; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.UseInternalPacketGenerator(true).InternalPacketGeneratorImages(2).ImagesPerTrigger(5).PedestalG0Frames(0); + + MockAcquisitionDevice test(0, 64); + std::vector frame(RAW_MODULE_SIZE); + for (int slot = 0; slot < 4; slot++) { + std::fill(frame.begin(), frame.end(), 100 + slot); + test.SetInternalGeneratorFrame(frame.data(), slot); + } + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + for (int image = 0; image < 5; image++) { + for (int m = 0; m < nmodules; m++) { + const auto pixels = reinterpret_cast(test.GetDeviceOutput(image, m)->pixels); + CHECK(pixels[0] == 100 + (image % 2) * nmodules + m); + CHECK(pixels[RAW_MODULE_SIZE - 1] == 100 + (image % 2) * nmodules + m); + } + } +} diff --git a/tests/FPGAIntegrationTest.cpp b/tests/FPGAIntegrationTest.cpp deleted file mode 100644 index 10f159c59..000000000 --- a/tests/FPGAIntegrationTest.cpp +++ /dev/null @@ -1,2846 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include - -#include "../jungfrau/JFPedestalCalc.h" -#include "../acquisition_device/HLSSimulatedDevice.h" -#include "../image_analysis/image_preprocessing/ImagePreprocessorCPU.h" -#ifdef JFJOCH_USE_CUDA -#include "../image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.h" -#include "../image_analysis/image_preprocessing/ImagePreprocessorGPU.h" -#endif -#include "CheckImageOutput.h" - -using namespace std::literals::chrono_literals; - -TEST_CASE("HLS_C_Simulation_internal_packet_generator", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == i % 65536); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_data_stream", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - x.DataStreams(4); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(3, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - test.SetInternalGeneratorFrame(ref_data.data(), 0); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, 0)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == i % 65536); - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_custom_frame", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 32; - const size_t nframes = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 65535); - - for (auto &i: test_frame) - i = dist(g1); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 128); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + RAW_MODULE_SIZE * m, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); - - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - REQUIRE(imageBuf[i] == test_frame[m * RAW_MODULE_SIZE + i]); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_check_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - std::vector raw_frames(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_raw0.bin", raw_frames.data(), RAW_MODULE_SIZE); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - test.CreateJFPackets(x, 1, 1, 0, raw_frames.data()); - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - uint64_t diffs = 0; - - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frames[j] != ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j]) { - diffs++; - std::cout << raw_frames[j] << " " << ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j] << std::endl; - } - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_missing_modules", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const uint64_t nframes = AcquisitionCounters::ThresholdFramesLost + 5; - - std::vector raw_frames(nframes * RAW_MODULE_SIZE, 0); - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(nframes).NumTriggers(1); - - HLSSimulatedDevice test(0, nframes + 2); - test.CreateJFPackets(x, 1, nframes, 0, raw_frames.data()); - test.CreateJFPackets(x, 1, 3, 1, raw_frames.data()); - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 5 - 1); - - REQUIRE(test.Counters().GetBufferHandle(0,0) != AcquisitionCounters::HandleNotFound); - REQUIRE(test.Counters().GetBufferHandle(0,1) == AcquisitionCounters::HandleNotFound); - REQUIRE(test.Counters().GetBufferHandle(0,2) == AcquisitionCounters::HandleNotFound); - - REQUIRE(test.GetBytesReceived() == nframes * 128 * JUNGFRAU_PACKET_SIZE_BYTES); -} - - -TEST_CASE("HLS_C_Simulation_check_cancel", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - REQUIRE_NOTHROW(test.StartAction(x)); - test.Cancel(); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - - REQUIRE(test.GetBytesReceived() == 0); -} - - -TEST_CASE("HLS_C_Simulation_check_cancel_conversion", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - REQUIRE_NOTHROW(test.StartAction(x)); - test.Cancel(); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 0); -} - -TEST_CASE("HLS_C_Simulation_check_delay", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 0, 1, data); - test.CreateJFPacket(x, 1, 0, 2, data); - test.CreateJFPacket(x, 1, 0, 3, data); - - test.CreateJFPacket(x, 2, 0, 0, data); - test.CreateJFPacket(x, 2, 0, 1, data); - test.CreateJFPacket(x, 2, 0, 2, data); - test.CreateJFPacket(x, 2, 0, 3, data); - - test.CreateJFPacket(x, 3, 0, 0, data); - test.CreateJFPacket(x, 3, 0, 1, data); - test.CreateJFPacket(x, 3, 0, 2, data); - test.CreateJFPacket(x, 3, 0, 3, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().CalculateDelay(0) == 2); - REQUIRE(test.Counters().CalculateDelay(0, 0) == 2); - REQUIRE(test.Counters().CalculateDelay(1) == 1); - REQUIRE(test.Counters().CalculateDelay(1, 0) == 1); - REQUIRE(test.Counters().CalculateDelay(2) == 0); - REQUIRE(test.Counters().CalculateDelay(2, 0) == 0); -} - -TEST_CASE("HLS_C_Simulation_check_lost_frame_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - for (int i = 0; i < 4096; i++) data[i] = i; - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE(test.GetDeviceOutput(0,0)->pixels[0] == 0); - REQUIRE(test.GetDeviceOutput(0,0)->pixels[1] == 1); - REQUIRE(test.GetDeviceOutput(0,0)->pixels[4095] == 4095); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); -} - -TEST_CASE("HLS_C_Simulation_check_lost_frame_conversion", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - for (int i = 0; i < 4096; i++) data[i] = i; - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(1,0)); - REQUIRE_THROWS(test.GetDeviceOutput(2,1)); -} - -TEST_CASE("HLS_C_Simulation_check_single_packet_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - uint16_t wrong[4096]; - - for (int i = 0; i < 4096; i++) { - data[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; - wrong[i] = UINT16_MAX; - } - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 64, 0, data); - test.CreateJFPacket(x, 1, 0, 2, data); - - test.CreateJFPacket(x, 1, 2, 3, data); - test.CreateJFPacket(x, 1, 3, 3, data); - - test.CreateJFPacket(x, 1, 1, 3, data); - test.CreateJFPacket(x, 1, 0, 3, data); - test.CreateJFPacket(x, 1, 64, 3, data); - - test.CreateJFPacket(x, 1, 5, 0, data); - test.CreateJFPacket(x, 1, 4, 0, data); - - test.CreateJFPacket(x, 1, 67, 1, data); - test.CreateJFPacket(x, 1, 66, 1, data); - test.CreateJFPacket(x, 1, 68, 1, data); - - test.CreateJFPacket(x, 3, 1, 0, data); - test.CreateJFPacket(x, 2, 1, 0, data); - - test.CreateJFPacket(x, 4, 1, 0, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 15 * JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 1 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 2 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 3 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 4 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 5 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 6 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 64 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - - REQUIRE(memcmp(test.GetDeviceOutput(0,1)->pixels + 67 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,1)->pixels + 65 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(test.GetDeviceOutput(0,0)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); -} - -TEST_CASE("HLS_C_Simulation_check_single_packet_conv", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - int16_t wrong[4096]; - - for (int i = 0; i < 4096; i++) { - data[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; - wrong[i] = INT16_MIN; - } - - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(14500), pedestal_g2(14500); - JFCalibration c_in(x); - c_in.Pedestal(0, 0) = pedestal_g0; - c_in.Pedestal(0, 1) = pedestal_g1; - c_in.Pedestal(0, 2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 64, 0, data); - - test.CreateJFPacket(x, 1, 67, 1, data); - test.CreateJFPacket(x, 1, 66, 1, data); - test.CreateJFPacket(x, 1, 68, 1, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 5 * JUNGFRAU_PACKET_SIZE_BYTES); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 0 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 1 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 2 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 3 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 6 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 64 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 65 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 66 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 67 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 68 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info("Error of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_HG0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % 0x4000; // only generate gain 0 - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 2.0, 1.0, 0.5, 0.1}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.UsingGainHG0(true); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - logger.Info("With HG0"); - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(x.IsUsingGainHG0()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } - logger.Info("Without HG0"); - x.UsingGainHG0(false); - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(!x.IsUsingGainHG0()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_fixedG1", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(1500), pedestal_g2(14500); - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 2.0, 1.0}; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.FixedGainG1(true); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy).GeometryTransformation(false); - REQUIRE(x.IsFixedGainG1()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_I32", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 3.0}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(true).BitDepthImage(32).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (int32_t *) test.GetDeviceOutput(0, 0)->pixels); - logger.Info("Error of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_sum4", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - size_t nsummation = 4; - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy).Summation(nsummation) - .GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - for (int i = 0; i < nsummation; i++) - test.CreateJFPackets(x, i + 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * nsummation * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (int32_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 2.0); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_U16", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(false).BitDepthImage(16).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (uint16_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_poisson_full_range", "[FPGA][Full]") { - // This test is not 100% representable! - // At the moment it is not possible to include "hls_math.h" in jfjoch_test - // So hls::sqrt is actually implemented as round(sqrt()) - // This can be only handled by HLS test bench - - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - - for (int sqrtmult: {1, 2,4,8}) { - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw().PixelSigned(true).BitDepthImage(16); - HLSSimulatedDevice test(0, 64); - - x.NumTriggers(1).ImagesPerTrigger(1).LossyCompressionPoisson(sqrtmult); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data_signed = (int16_t *) data.data(); - - uint64_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - uint64_t expected_value = 0; - if (data_signed[i] == INT16_MAX) - expected_value = INT16_MAX; - else if (data_signed[i] == INT16_MIN) - expected_value = INT16_MIN; - else if (data_signed[i] >= 0) - expected_value = std::lround(std::sqrt(data_signed[i]) * sqrtmult); - - if (expected_value != test.GetDeviceOutput(0, 0)->pixels[i]) - diff++; - - } - REQUIRE(diff == 0); - } -} - -TEST_CASE("HLS_C_Simulation_check_threshold_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - - for (int threshold: {1, 2, 4, 8}) { - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw().PixelSigned(true).BitDepthImage(16); - HLSSimulatedDevice test(0, 64); - - x.NumTriggers(1).ImagesPerTrigger(1).PixelValueLowThreshold(threshold); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data_signed = (int16_t *) data.data(); - - uint64_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - uint64_t expected_value = data_signed[i]; - if (data_signed[i] == INT16_MAX) - expected_value = INT16_MAX; - else if (data_signed[i] == INT16_MIN) - expected_value = INT16_MIN; - else if (data_signed[i] < threshold) - expected_value = 0; - - if (expected_value != test.GetDeviceOutput(0, 0)->pixels[i]) { - std::cout << data_signed[i] << " " << expected_value << " " << threshold << std::endl; - diff++; - } - - } - REQUIRE(diff == 0); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_poisson", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - x.LossyCompressionPoisson(2); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(false).BitDepthImage(16).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (uint16_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_no_conversion_U16", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(false).BitDepthImage(16); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto output = (uint16_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - CHECK(data[i] == output[i]); - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_no_conversion_U32", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(false).BitDepthImage(32); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto output = (uint32_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (data[i] == UINT16_MAX) { - CHECK(output[i] == UINT32_MAX); - } else if (data[i] != output[i]) err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_no_conversion_I32", "[FPGA][Full]") { - Logger logger("HLS_C_Simulation_no_conversion_I32"); - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(true).BitDepthImage(32); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data16_signed = (int16_t *) data.data(); - auto output = (int32_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (data16_signed[i] == INT16_MAX) { - CHECK(output[i] == INT32_MAX); - } else if (data16_signed[i] == INT16_MIN) { - CHECK(output[i] == INT32_MIN); - } else if (data16_signed[i] != output[i]) err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_convert_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector data(RAW_MODULE_SIZE); - JFModulePedestal pedestal_g0, pedestal_g1, pedestal_g2; - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - pedestal_g0.GetPedestal()[i] = 0 + (i / 65536) * 1000 + 100 * (i % 5); - pedestal_g1.GetPedestal()[i] = 14500 - (i / 65536) * 1000 + 100 * (i % 3); - pedestal_g2.GetPedestal()[i] = 14500 - + (i / 65536) * 1000; - } - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % RAW_MODULE_SIZE; - } - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy) - .GeometryTransformation(false); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFCalibration c(x); - for (int i = 0; i < x.GetModulesNum(); i++) { - c.Pedestal(i, 0) = pedestal_g0; - c.Pedestal(i, 1) = pedestal_g1; - c.Pedestal(i, 2) = pedestal_g2; - c.GainCalibration(i) = gain_from_file; - } - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c, data.data(), test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % 7; - - x.Raw(); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy) - .GeometryTransformation(false).PixelSigned(true).ApplyPixelMask(true); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - HLSSimulatedDevice test(0, 64); - - std::vector module_pixel_mask(RAW_MODULE_SIZE); - module_pixel_mask[456*49+6] = 1; - module_pixel_mask[0] = 128; - module_pixel_mask[RAW_MODULE_SIZE - 1] = 16384; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(data.data(), m); - - REQUIRE_NOTHROW(test.InitializePixelMask(module_pixel_mask.data(), 1)); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - CHECK(test.GetDeviceOutput(0, 0)->pixels[0] == 0); - CHECK(test.GetDeviceOutput(0, 0)->pixels[1] == 1); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+5] == 5); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+6] == 6); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+7] == 0); - CHECK(test.GetDeviceOutput(0, 0)->pixels[RAW_MODULE_SIZE - 1] == (RAW_MODULE_SIZE - 1) % 7); - - CHECK(test.GetDeviceOutput(0, 1)->pixels[0] == INT16_MIN); - CHECK(test.GetDeviceOutput(0, 1)->pixels[1] == 1); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+5] == 5); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+6] == INT16_MIN); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+7] == 0); - CHECK(test.GetDeviceOutput(0, 1)->pixels[RAW_MODULE_SIZE - 1] == INT16_MIN); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_convert_full_range_adu_histo", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules, 2, 8, 36, true)); - - std::vector data(RAW_MODULE_SIZE); - JFModulePedestal pedestal_g0, pedestal_g1, pedestal_g2; - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - pedestal_g0.GetPedestal()[i] = 0 + (i / 65536) * 1000 + 100 * (i % 5); - pedestal_g1.GetPedestal()[i] = 14500 - (i / 65536) * 1000 + 100 * (i % 3); - pedestal_g2.GetPedestal()[i] = 14500 - + (i / 65536) * 1000; - } - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % RAW_MODULE_SIZE; - } - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFCalibration c(x); - for (int i = 0; i < x.GetModulesNum(); i++) { - c.Pedestal(i, 0) = pedestal_g0; - c.Pedestal(i, 1) = pedestal_g1; - c.Pedestal(i, 2) = pedestal_g2; - c.GainCalibration(i) = gain_from_file; - } - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - uint32_t err = 0; - const uint32_t *adu_histogram = test.GetDeviceOutput(0, 0)->adu_histogram; - for (int i = 0; i < ADU_HISTO_BIN_COUNT; i++) { - if(adu_histogram[i] != RAW_MODULE_SIZE / ADU_HISTO_BIN_COUNT) - err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_check_2_trigger_convert", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector pedestal_g0(RAW_MODULE_SIZE), pedestal_g1(RAW_MODULE_SIZE), pedestal_g2(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_pedeG0.bin", pedestal_g0.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_pedeG1.bin", pedestal_g1.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_pedeG2.bin", pedestal_g2.data(), RAW_MODULE_SIZE); - - std::vector raw_frames(RAW_MODULE_SIZE); - std::vector conv_frames(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_raw0.bin", raw_frames.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_conv0.bin", conv_frames.data(), RAW_MODULE_SIZE); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1); - - HLSSimulatedDevice test(0, 64); - - JFCalibration c(x); - REQUIRE_NOTHROW(c.Pedestal(0, 0).LoadPedestal(pedestal_g0)); - REQUIRE_NOTHROW(c.Pedestal(0, 1).LoadPedestal(pedestal_g1)); - REQUIRE_NOTHROW(c.Pedestal(0, 2).LoadPedestal(pedestal_g2)); - REQUIRE_NOTHROW(c.GainCalibration(0) = GainCalibrationFromTestFile()); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - - // send some frames without trigger (to be ignored) - test.CreateJFPackets(x, 1, 1, 0, raw_frames.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - - // address properly aligned - REQUIRE((uint64_t) test.GetDeviceOutput(0,0)->pixels % 128 == 0); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - REQUIRE(test.Counters().GetCurrFrameNumber(0) == 0); - - - REQUIRE(test.GetBytesReceived()== 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = 0.0; - auto img = test.GetDeviceOutput(0, 0)->pixels; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if ((img[j] < 30000) && (img[j] > -30000)) { - float diff = (conv_frames[j] - (float) img[j]); - mean_error += diff * diff; - } - } - - mean_error = sqrt(mean_error/ (RAW_MODULE_SIZE)); - std::cout << "Mean error " << mean_error << std::endl; - REQUIRE(mean_error < 0.30); -} - - -TEST_CASE("HLS_C_Simulation_check_detect_last_frame", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).NumTriggers(2).ImagesPerTrigger(5); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 15, 0, 0, data); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - -} - -TEST_CASE("HLS_C_Simulation_check_wrong_packet_size", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[8192]; - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(5); - - HLSSimulatedDevice test(0, 64); - - // send some frames with wrong size or tuser=1 - test.CreateJFPacket(x, 1, 0, 0, data, 0); - test.CreateJFPacket(x, 1, 1, 0, data, -1); - test.CreateJFPacket(x, 1, 2, 0, data, 2); - test.CreateJFPacket(x, 1, 3, 0, data, 0); - test.CreateJFPacket(x, 1, 4, 0, data, -5); - test.CreateJFPacket(x, 1, 5, 0, data, 0, 1); - test.CreateJFPacket(x, 1, 6, 0, data, 0); - test.CreateJFPacket(x, 1, 7, 0, data, 0); - test.CreateJFPacket(x, 1, 8, 0, data, 7); - test.CreateJFPacket(x, 1, 9, 0, data, 100); - test.CreateJFPacket(x, 1, 10, 0, data, 2); - test.CreateJFPacket(x, 1, 11, 0, data, 0); - test.CreateJFPacket(x, 1, 12, 0, data, -80); - test.CreateJFPacket(x, 1, 13, 0, data, 100); - test.CreateJFPacket(x, 1, 118, 0, data, 0); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - - REQUIRE(test.GetBytesReceived() == 6 * JUNGFRAU_PACKET_SIZE_BYTES); -} - - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_15_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 2; - const uint16_t ntrigger = 2; - const uint16_t nstoragecells = 15; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 32); - - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_8_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 2; - const uint16_t ntrigger = 2; - const uint16_t nstoragecells = 8; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 64); - - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_16_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 2; - const uint16_t ntrigger = 4; - const uint16_t nstoragecells = 16; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 128); - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_storage_cell_convert_G1", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(16).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(16); - - HLSSimulatedDevice test(0, 64); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16384 | 10; - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < 16; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 1, i).GetPedestal()[j] = (17 - i) * 10; - c.Pedestal(1, 1, i).GetPedestal()[j] = i * 20; - } - } - - std::vector tmp(3 * RAW_MODULE_SIZE, -1); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - tmp[i] = DEFAULT_G0_FACTOR; - JFModuleGainCalibration gain(tmp); - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 32*128*JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < 16; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 17 - (i % 16) - 1); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 2 * (i % 16) - 1); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_integration", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - frame[i] = 32754; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(frame.data(), m); - - - std::vector integration_map(nmodules * RAW_MODULE_SIZE, 54); - for (int i = 0; i < RAW_MODULE_SIZE/2; i++) { - integration_map[2 * i] = 0; - integration_map[2 * i + 1] = FPGA_INTEGRATION_BIN_COUNT - 1; - } - std::vector weights(nmodules * RAW_MODULE_SIZE, 1.0); - integration_map[RAW_MODULE_SIZE - 1] = FPGA_INTEGRATION_BIN_COUNT; - test.InitializeIntegrationMap(x, integration_map, weights); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto integration_result = test.GetDeviceOutput(0, 0)->integration_result; - CHECK(integration_result[0].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE / 2))); - CHECK(integration_result[0].count == RAW_MODULE_SIZE / 2); - CHECK(integration_result[1].sum == 0); - CHECK(integration_result[1].count == 0); - CHECK(integration_result[FPGA_INTEGRATION_BIN_COUNT - 1].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE / 2 - 1))); - CHECK(integration_result[FPGA_INTEGRATION_BIN_COUNT - 1].count == RAW_MODULE_SIZE / 2 - 1); - - integration_result = test.GetDeviceOutput(0, 1)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); - - integration_result = test.GetDeviceOutput(0, 2)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754 * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); - - integration_result = test.GetDeviceOutput(0, 3)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754 * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_integration_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - - std::vector mask(RAW_MODULE_SIZE, 0); - mask[0] = 4; - - std::vector frame(RAW_MODULE_SIZE, 0); - frame[0] = 2; - frame[1] = 3; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializePixelMask(mask.data(), m); - } - - std::vector integration_map(nmodules * RAW_MODULE_SIZE, 1); - - std::vector weights(nmodules * RAW_MODULE_SIZE, 1.0); - integration_map[0] = 0; - integration_map[1] = 0; - - test.InitializeIntegrationMap(x, integration_map, weights); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto integration_result = test.GetDeviceOutput(0, 0)->integration_result; - CHECK(integration_result[0].sum == 3); - CHECK(integration_result[0].count == 1); - CHECK(integration_result[1].sum == 0); - CHECK(integration_result[1].count == RAW_MODULE_SIZE - 2); -} - -bool spot_finder_output_contains(const SpotFindingResult& output, uint32_t number) { - size_t byte = number / 8; - size_t bit = number % 8; - return ((output.strong_pixel[byte] & (1 << bit)) != 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_count_threshold", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - std::vector frame(RAW_MODULE_SIZE, 0); - frame [ 1*1024 + 1] = 11; - frame [123*1024 + 578] = 10; - frame [121*1024 + 800] = 12; - frame [ 89*1024 + 300] = 8; - frame [300*1024 + 0] = 9; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.5, - .low_resolution_limit = 3.1 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 3); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 1)); - CHECK (spot_finder_output_contains(spot_finder_result, 123*1024 + 578)); - CHECK (spot_finder_output_contains(spot_finder_result, 121*1024 + 800)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - x.ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - - std::vector mask(RAW_MODULE_SIZE, 0); - mask[325*1024 + 463] = 4; - - std::vector frame(RAW_MODULE_SIZE, 0); - // Module boundary - frame [ 0*1024 + 0] = 50; - frame [ 5*1024 + 0] = 50; - frame [ 0*1024 + 5] = 50; - frame [ 511*1024 + 5] = 50; - - // Chip boundary - frame [ 345*1024 + 1023] = 50; - frame [ 346*1024 + 255] = 50; - frame [ 347*1024 + 256] = 50; - frame [ 348*1024 + 511] = 50; - frame [ 349*1024 + 512] = 50; - frame [ 350*1024 + 767] = 50; - frame [ 351*1024 + 768] = 50; - frame [ 255*1024 + 234] = 50; - frame [ 256*1024 + 876] = 50; - - // "Normal" pixel - frame [ 320*1024 + 400] = 50; - - // Masked pixel - frame [ 325*1024 + 463] = 50; - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - test.InitializePixelMask(mask.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 45, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 1); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 45); - - CHECK (spot_finder_output_contains(spot_finder_result, 320*1024 + 400)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_overload", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [ 132*1024 + 133] = INT16_MAX; - frame [ 0*1024 + 500] = INT16_MIN; - frame [ 255*1024 + 234] = INT16_MAX; - frame [ 300*1024 + 234] = INT16_MAX; - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - d_map[ 132*1024 + 133] = 0.0; - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 5.0, - .photon_count_threshold = 45, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 1); - REQUIRE (spot_finder_result.snr_threshold == 5.0); - REQUIRE (spot_finder_result.count_threshold == 45); - - CHECK (spot_finder_output_contains(spot_finder_result, 300*1024 + 234)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_min_pix_per_spot", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1*1024 + 1] = 11; - frame [1*1024 + 2] = 10; - frame [5*1024 + 7] = 12; - frame [6*1024 + 7] = 12; - - frame [ 89*1024+154] = 12; - - frame [ 99*1024+200] = 12; - frame [100*1024+200] = 11; - frame [100*1024+201] = 10; - frame [101*1024+200] = 12; - frame [RAW_MODULE_SIZE - 1 - 1024] = 20; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - test.SetInternalGeneratorFrame(frame.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 2, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 8); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 1)); - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 2)); - CHECK (spot_finder_output_contains(spot_finder_result, 5*1024 + 7)); - CHECK (!spot_finder_output_contains(spot_finder_result, RAW_MODULE_SIZE-1-1024)); - CHECK (!spot_finder_output_contains(spot_finder_result, 89*1024+154)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_d_min_max", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 0.0); - d_map[1024+1] = 2.5; - d_map[1024+2] = 4.0; - d_map[1024+3] = 1.0; - d_map[1024+4] = 1.5; - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 11; - frame [1024+2] = 10; - frame [1024+3] = 12; - frame [1024+4] = 12; - - test.InitializeSpotFinderResolutionMap(d_map.data(), 0); - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 1, - .high_resolution_limit = 1.25, - .low_resolution_limit = 3.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 2); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1024+1)); - CHECK (spot_finder_output_contains(spot_finder_result, 1024+4)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_snr_threshold", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - std::vector frame(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - frame[i] = ((i / RAW_MODULE_COLS) + (i % RAW_MODULE_COLS)) % 2; - } - // Mean = 0.5 - // Std. dev. = 0.5 - // Threshold = 10 * std. dev. - 6 is minimum count - - frame [ 1*1024 + 1] = 8; - frame [123*1024 + 578] = 5; - frame [121*1024 + 800] = 4; - frame [ 89*1024 + 300] = 7; - frame [300*1024 + 5] = 3; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 10.0, - .photon_count_threshold = 0, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 3.1 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 2); - REQUIRE (spot_finder_result.snr_threshold == 10.0); - REQUIRE (spot_finder_result.count_threshold == 0); - - - REQUIRE (spot_finder_output_contains(spot_finder_result, 1*1024+1)); - REQUIRE (spot_finder_output_contains(spot_finder_result, 89*1024 + 300)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_pixel_threshold_summation", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(4) - .PixelValueLowThreshold(4).AutoSummation(true).PixelSigned(true) - .PixelValueHighThreshold(500); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 1; - frame [1024+2] = 3; - frame [1024+3] = 4; - frame [1024+4] = 5; - frame [1024+5] = 100; - frame [1024+6] = 501; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(frame.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 4 * 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(imageBuf[1024+1] == 0); - REQUIRE(imageBuf[1024+2] == 0); - REQUIRE(imageBuf[1024+3] == 4*4); - REQUIRE(imageBuf[1024+4] == 4*5); - REQUIRE(imageBuf[1024+5] == 4*100); - REQUIRE(imageBuf[1024+6] == INT16_MAX); - - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.pixel_sum == 4 * (4+5+100)); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.max_value == 4*100); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_summation_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(4) - .AutoSummation(true).ApplyPixelMask(true).BitDepthImage(16).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 1; - frame [1024+2] = 3; - frame [1024+3] = 4; - frame [1024+4] = 5; - frame [1024+5] = 100; - frame [1024+6] = -20; - - std::vector pixel_mask(RAW_MODULE_SIZE, 0); - pixel_mask[1024+2] = 1; - - std::vector roi_map(RAW_MODULE_SIZE, 0); - roi_map[1024+1] = 1; - roi_map[1024+2] = 2; - roi_map[1024+5] = 4; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializePixelMask(pixel_mask.data(), m); - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - test.InitializeROIMap(roi_map.data(), m); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 4 * 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(imageBuf[1024+1] == 4 * 1); - REQUIRE(imageBuf[1024+2] == INT16_MIN); - REQUIRE(imageBuf[1024+3] == 4*4); - REQUIRE(imageBuf[1024+4] == 4*5); - REQUIRE(imageBuf[1024+5] == 4*100); - REQUIRE(imageBuf[1024+6] == -4*20); - - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.pixel_sum == 4 * (1+4+5+100-20)); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.max_value == 4*100); - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.min_value == -4 * 20); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[0].good_pixels == 1); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[1].good_pixels == 0); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[2].good_pixels == 1); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[2].sum == 4*100); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[3].good_pixels == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - const size_t nframes = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, UINT16_MAX - 1); - - for (auto &i: test_frame) - i = dist(g1); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0).PixelSigned(false).BitDepthImage(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + m * RAW_MODULE_SIZE, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * JUNGFRAU_PACKET_SIZE_BYTES); - - auto test_frame_unsigned = (uint16_t *) test_frame.data(); - - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - CHECK(test.GetDeviceOutput(image, m)->module_statistics.frame_number == image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_count == 512); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - - auto imageBuf = (uint32_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - REQUIRE(imageBuf[i] == test_frame_unsigned[m * RAW_MODULE_SIZE + i]); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_summation", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - const size_t nframes = 16; - const size_t nsummation = 16; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, UINT16_MAX); - - for (auto &i: test_frame) - i = dist(g1); - - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0).Summation(nsummation) - .JungfrauConvPhotonCnt(false).PixelSigned(true).BitDepthImage(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + m * RAW_MODULE_SIZE, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * nsummation * JUNGFRAU_PACKET_SIZE_BYTES); - - auto test_frame_signed = (int16_t *) test_frame.data(); - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image * nsummation); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image * nsummation); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - - auto imageBuf = (int32_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (test_frame_signed[m * RAW_MODULE_SIZE + i] == INT16_MAX) - CHECK(imageBuf[i] == INT32_MAX); - else if (test_frame_signed[m * RAW_MODULE_SIZE + i] == INT16_MIN) - CHECK(imageBuf[i] == INT32_MIN); - else - REQUIRE(imageBuf[i] == test_frame_signed[m * RAW_MODULE_SIZE + i] * nsummation); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_count_sat_and_err_pixels", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector v(RAW_MODULE_SIZE, 0); - v[567] = INT16_MAX; - v[RAW_MODULE_COLS * 511 + 255] = INT16_MAX; - v[RAW_MODULE_COLS * 250 + 233] = INT16_MIN; - v[0] = INT16_MIN; - v[RAW_MODULE_SIZE-1] = INT16_MIN; - v[23] = 23; - v[24] = -24; - v[25] = 27; - v[1500] = -25; - v[1501] = 30; - v[1502] = INT16_MIN + 25; - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame((uint16_t *) v.data(), m); - - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - CHECK(test.GetDeviceOutput(image, m)->module_statistics.saturated_pixels == 2); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.err_pixels == 3); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.max_value == 30); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.min_value == INT16_MIN + 25); - } - } -} - -TEST_CASE("HLS_C_Simulation_pixel_count_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - x.ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - std::vector pixel_mask(RAW_MODULE_SIZE, 0); - pixel_mask[0] = 2; - pixel_mask[1] = 4; - pixel_mask[2] = 1<<30; - test.InitializePixelMask(pixel_mask.data(), 1); - - std::vector v(RAW_MODULE_SIZE, 0); - v[0] = INT16_MAX; - v[1] = INT16_MIN; - v[2] = 35; - v[3] = 234; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame((uint16_t *) v.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.saturated_pixels == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.err_pixels == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.max_value == 234); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.min_value == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.masked_pixels == 3); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.pixel_sum == 234); - CHECK(test.GetDeviceOutput(image, 1)->pixels[2] == 35); - - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.saturated_pixels == 1); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.err_pixels == 1); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.max_value == 234); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.min_value == 0); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.masked_pixels == 0); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.pixel_sum == 35 + 234); - } -} - -TEST_CASE("HLS_C_Simulation_count_pixel_sum", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector v_0(RAW_MODULE_SIZE, 123); - std::vector v_1(RAW_MODULE_SIZE, 34); - v_1[0] = INT16_MAX; - v_1[1] = INT16_MIN; - v_1[2] = 0; - v_1[RAW_MODULE_SIZE-1] = -1234; - std::vector v_2(RAW_MODULE_SIZE, -23); - - test.SetInternalGeneratorFrame((uint16_t *) v_0.data(), 0); - test.SetInternalGeneratorFrame((uint16_t *) v_1.data(), 1); - test.SetInternalGeneratorFrame((uint16_t *) v_2.data(), 2); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.pixel_sum == 123 * RAW_MODULE_SIZE); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.pixel_sum == 34 * (RAW_MODULE_SIZE - 4) - 1234); - CHECK(test.GetDeviceOutput(image, 2)->module_statistics.pixel_sum == -23 * RAW_MODULE_SIZE); - } -} - -TEST_CASE("HLS_C_Simulation_check_bunchid", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint64_t bunchid = 0xAAAAAAAA87345600; - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - test.CreateXfelBunchIDPacket(bunchid, 0); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 0); - REQUIRE(test.GetDataCollectionStatus().current_pulseid == bunchid); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i % (UINT16_MAX - 11); - - RawToEigerInput<4>(raw_frame_eiger.data(), raw_frame.data()); - const uint16_t nmodules = 1; - DetectorSetup detector(DetEIGER(nmodules)); - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, raw_frame_eiger.data() + 2048 * (2 * i)); - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, raw_frame_eiger.data() + 2048 * (1 + 2 * i)); - - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, raw_frame_eiger.data() + 2048 * (128 + 2 * i)); - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, raw_frame_eiger.data() + 2048 * (129 + 2 * i)); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - - REQUIRE(test.GetBytesReceived() == 256 * 4096); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger_8bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i; - - const uint16_t nmodules = 1; - - DetectorSetup detector(DetEIGER(nmodules)); - RawToEigerInput<8>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1).EigerBitDepth(8).BitDepthImage(8); - - REQUIRE(x.GetByteDepthImage() == 1); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i))); - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 1))); - - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 64))); - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 65))); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 128 * 4096); - - auto out = (uint8_t *) test.GetDeviceOutput(0, 0)->pixels; - - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[4] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[5] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[6] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[7] == UINT64_MAX); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != out[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i; - - const uint16_t nmodules = 1; - - DetectorSetup detector(DetEIGER(nmodules)); - RawToEigerInput<2>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1).EigerBitDepth(32).BitDepthImage(32); - - REQUIRE(x.GetByteDepthImage() == 4); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i))); - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 1))); - - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 256))); - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 257))); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 512 * 4096); - - auto out = (uint32_t *) test.GetDeviceOutput(0, 0)->pixels; - - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[4] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[5] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[6] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[7] == UINT64_MAX); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != out[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE), raw_frame_eiger(RAW_MODULE_SIZE); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 65535); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<4>(raw_frame_eiger.data(), raw_frame.data()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(raw_frame_eiger.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == raw_frame[i]); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger_8bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE*2), raw_frame_eiger(RAW_MODULE_SIZE*2); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 254); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<8>(raw_frame_eiger.data(), raw_frame.data()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - x.EigerBitDepth(8); - - REQUIRE(x.GetBitDepthReadout() == 8); - REQUIRE(x.GetByteDepthImage() == 2); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 2 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = test.GetDeviceOutput(image, m)->pixels; - size_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if(imageBuf[i] != raw_frame.data()[i]) diff++; - } - REQUIRE(diff == 0); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - std::vector raw_frame(RAW_MODULE_SIZE), raw_frame_eiger(RAW_MODULE_SIZE); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 4*UINT16_MAX); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<2>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - x.EigerBitDepth(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()), - 2 * m); - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()) + RAW_MODULE_SIZE, - 2 * m + 1); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 512 * nmodules * 2 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = reinterpret_cast(test.GetDeviceOutput(image, m)->pixels); - size_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if(imageBuf[i] != raw_frame.data()[i]) diff++; - } - REQUIRE(diff == 0); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_roi_calc", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const size_t nimages = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nimages).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - frame[i] = 135; - - frame[12+RAW_MODULE_COLS*3] = 83; - frame[13+RAW_MODULE_COLS*3] = 95; - frame[12+RAW_MODULE_COLS*4] = 13; - frame[13+RAW_MODULE_COLS*4] = 125; - - frame[812+RAW_MODULE_COLS*303] = 83; - frame[813+RAW_MODULE_COLS*303] = 95; - frame[812+RAW_MODULE_COLS*304] = 13; - frame[813+RAW_MODULE_COLS*304] = 125; - - std::vector roi_map(RAW_MODULE_SIZE, 0); - roi_map[12+RAW_MODULE_COLS*3] = 1; - roi_map[13+RAW_MODULE_COLS*3] = 1; - roi_map[12+RAW_MODULE_COLS*4] = 1; - roi_map[13+RAW_MODULE_COLS*4] = 1; - - roi_map[812+RAW_MODULE_COLS*303] = (1<<5); - roi_map[813+RAW_MODULE_COLS*303] = (1<<5); - roi_map[812+RAW_MODULE_COLS*304] = (1<<5); - roi_map[813+RAW_MODULE_COLS*304] = (1<<5); - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeROIMap(roi_map.data(), m); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nimages * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - for (int i = 0; i < x.GetImageNum(); i++) { - for (int m = 0; m < x.GetModulesNum(); m++) { - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum == 83 + 95 + 13 + 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum2 == 83 * 83 + 95 * 95 + 13 * 13 + 125 * 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum_x_weighted == Catch::Approx(83 * 12 + 95 * 13 + 13 * 12 + 125 * 13)); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum_y_weighted == Catch::Approx(83 * 3 + 95 * 3 + 13 * 4 + 125 * 4)); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].max_value == 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].good_pixels == 4); - - CHECK (test.GetDeviceOutput(i, m)->roi_counts[5].sum_x_weighted == Catch::Approx(83 * (812+6) + 95 * (813+6) + 13 * (812+6) + 125 * (813+6))); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[5].sum_y_weighted == Catch::Approx(83 * (303+2) + 95 * (303+2) + 13 * (304+2) + 125 * (304+2))); - - } - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_4_images", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const uint16_t nimages = 4; - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nimages).PedestalG0Frames(0).InternalPacketGeneratorImages(nimages); - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE*nimages*nmodules); - for (int i = 0; i < ref_data.size(); i++) - ref_data[i] = (7 * i + 5) % 65521; - - for (int m = 0; m < nmodules*nimages; m++) - test.SetInternalGeneratorFrame(ref_data.data() + RAW_MODULE_SIZE * m, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - std::cout << image << " " << m << std::endl; - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == ref_data[i + (image * nmodules + m) * RAW_MODULE_SIZE]); - } - } -} - -TEST_CASE("HLSSimulatedDevice_InternalGeneratorMemory", "[FPGA]") { - // 64 module frames in the card memory; the generator takes 63 at most - DiffractionExperiment x(DetJF(2)); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).NumTriggers(1); - HLSSimulatedDevice hls_device(0, 64); - - x.InternalPacketGeneratorImages(31); - REQUIRE_NOTHROW(hls_device.PrepareAction(x)); - - x.InternalPacketGeneratorImages(32); - REQUIRE_THROWS(hls_device.PrepareAction(x)); - - std::vector frame(RAW_MODULE_SIZE); - REQUIRE_NOTHROW(hls_device.SetInternalGeneratorFrame(frame.data(), 62)); - REQUIRE_THROWS(hls_device.SetInternalGeneratorFrame(frame.data(), 63)); -} - -// The claim behind ImageStatistics::pixel_sum: the preprocessors, reading the frame the card wrote, give the -// card's own sum over the same pixels. -namespace { - template - void CheckPixelSumParity(int64_t nsummation, CompressedImageMode mode) { - const uint16_t nmodules = 2; - - DiffractionExperiment x(DetJF(nmodules)); - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(nsummation) - .AutoSummation(true).ApplyPixelMask(true).BitDepthImage(8 * sizeof(T)).PixelSigned(true); - - std::mt19937 rng(5); - std::vector frame(RAW_MODULE_SIZE); - for (auto &v: frame) - v = static_cast(static_cast(rng() % 401) - 200); - frame[1024 + 1] = 2000; - frame[1024 + 2] = -1500; - frame[1024 + 3] = INT16_MAX; - frame[1024 + 4] = INT16_MIN; - frame[1024 + 5] = 1234; - - std::vector module_mask(RAW_MODULE_SIZE, 0); - module_mask[1024 + 5] = 1; - module_mask[1024 + 6] = 1; - - HLSSimulatedDevice test(0, 64); - std::vector user_mask; - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializePixelMask(module_mask.data(), m); - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - user_mask.insert(user_mask.end(), module_mask.begin(), module_mask.end()); - } - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - std::vector image(nmodules * RAW_MODULE_SIZE); - int64_t card_sum = 0; - for (int m = 0; m < nmodules; m++) { - const auto *output = test.GetDeviceOutput(0, m); - memcpy(image.data() + m * RAW_MODULE_SIZE, output->pixels, RAW_MODULE_SIZE * sizeof(T)); - card_sum += output->module_statistics.pixel_sum; - } - CHECK(card_sum != 0); - - // The modules one under the other, as a detector without a card delivers its image. - DiffractionExperiment x_image(DetDECTRIS(RAW_MODULE_COLS, nmodules * RAW_MODULE_LINES, "Test", {}) - .BitDepthImage(8 * sizeof(T))); - x_image.PixelSigned(true); - PixelMask mask(x_image); - mask.LoadUserMask(x_image, user_mask); - const auto input = reinterpret_cast(image.data()); - - ImagePreprocessorCPU cpu(x_image, mask); - ImagePreprocessorBuffer cpu_buffer(x_image.GetPixelsNum()); - CHECK(cpu.Analyze(cpu_buffer, input, mode).pixel_sum == card_sum); - -#ifdef JFJOCH_USE_CUDA - if (get_gpu_count() > 0) { - ImagePreprocessorGPU gpu(x_image, mask, std::make_shared(), /*copy_image_to_host=*/false); - ImagePreprocessorBufferGPU gpu_buffer(x_image.GetPixelsNum()); - CHECK(gpu.Analyze(gpu_buffer, input, mode).pixel_sum == card_sum); - } -#endif - } -} - -TEST_CASE("HLS_C_Simulation_pixel_sum_matches_preprocessor", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - CheckPixelSumParity(1, CompressedImageMode::Int16); - CheckPixelSumParity(4, CompressedImageMode::Int32); -} diff --git a/tests/JFJochReceiverIntegrationTest.cpp b/tests/JFJochReceiverIntegrationTest.cpp index 3b669762a..8aa91430c 100644 --- a/tests/JFJochReceiverIntegrationTest.cpp +++ b/tests/JFJochReceiverIntegrationTest.cpp @@ -4,7 +4,7 @@ #include #include "../receiver/JFJochReceiverTest.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "MockCardOutput.h" #include "../jungfrau/JFPedestalCalc.h" #include "../image_pusher/TestImagePusher.h" #include "../receiver/JFJochReceiverService.h" @@ -19,15 +19,11 @@ TEST_CASE("JFJochReceiverTest_Raw", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(100).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::NO_COMPRESSION); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } Logger logger("JFJochReceiverTest_Raw"); PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -41,6 +37,31 @@ TEST_CASE("JFJochReceiverTest_Raw", "[JFJochReceiver]") { REQUIRE(!output.status.cancelled); } +// Two cards running at the same time - not possible with the HLS simulation +TEST_CASE("JFJochReceiverTest_Raw_TwoDevices", "[JFJochReceiver]") { + DiffractionExperiment x(DetJF(4)); + const uint16_t nthreads = 4; + + x.Raw(); + x.DataStreams(2); + x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) + .ImagesPerTrigger(100).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::NO_COMPRESSION); + REQUIRE(x.GetDataStreamsNum() == 2); + + Logger logger("JFJochReceiverTest_Raw_TwoDevices"); + + PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); + + JFJochReceiverOutput output; + bool ret; + REQUIRE_NOTHROW(ret = JFJochReceiverTest(output, logger, aq_devices, x, mask, nthreads)); + REQUIRE(ret); + REQUIRE(output.efficiency == 1.0); + REQUIRE(output.status.images_sent == x.GetImageNum()); + REQUIRE(!output.status.cancelled); +} + TEST_CASE("JFJochReceiverTest_Conversion", "[JFJochReceiver]") { Logger logger("JFJochReceiverTest_Conversion"); @@ -51,15 +72,11 @@ TEST_CASE("JFJochReceiverTest_Conversion", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } REQUIRE(x.IsGeometryTransformed()); PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -94,12 +111,12 @@ TEST_CASE("JFJochReceiverTest_ForceCPUAzInt", "[JFJochReceiver]") { for (int i = 0; i < raw_image.size(); i++) raw_image[i] = 1000 + i % 1000; - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); - PixelMask mask(x); + std::vector images; + images.push_back(ConvertedImage(x, JFCalibration(x), mask, raw_image)); + AcquisitionDeviceGroup aq_devices = MockDevices(x, images); + TestImagePusher pusher(x.GetImageNum() - 1); JFJochReceiverService service(aq_devices, logger, pusher); service.NumThreads(nthreads); @@ -136,7 +153,7 @@ TEST_CASE("JFJochReceiverTest_AzIntBinsExceedFPGA", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); PixelMask mask(x); TestImagePusher pusher(0); @@ -157,13 +174,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_NoGeomTransform", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .GeometryTransformation(false); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -187,13 +200,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Poisson", "[JFJochReceiver]") { CompressionAlgorithm::BSHUF_ZSTD); x.LossyCompressionPoisson(1); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -217,13 +226,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Threshold", "[JFJochReceiver]") { CompressionAlgorithm::BSHUF_ZSTD); x.PixelValueLowThreshold(50); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -246,13 +251,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_ApplyMask", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.ApplyPixelMask(true).MaskChipEdges(true).MaskModuleEdges(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -276,13 +277,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Threshold_Summation", "[JFJochReceiver] CompressionAlgorithm::BSHUF_ZSTD); x.PixelValueLowThreshold(10); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -305,13 +302,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_FixedGainG1", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .FixedGainG1(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -336,13 +329,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_FixedGainG1_onlyG1", "[JFJochReceiver]" .FixedGainG1(true); std::vector input(RAW_MODULE_SIZE*2, 0x4000 + 3000); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask, input); JFJochReceiverOutput output; bool ret; @@ -366,13 +355,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_U16", "[JFJochReceiver]") { .BitDepthImage(16).PixelSigned(false); REQUIRE(!x.IsPixelSigned()); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -398,13 +383,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_U8", "[JFJochReceiver]") { REQUIRE(!x.IsPixelSigned()); REQUIRE(x.GetByteDepthImage() == 1); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -427,13 +408,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_I32", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .PixelSigned(true).BitDepthImage(32); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -455,13 +432,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Summation2", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD).Summation(2); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -484,13 +457,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Summation7_CPU", "[JFJochReceiver]") { .ImagesPerTrigger(8).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .Summation(7).CPUSummation(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -515,13 +484,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_StorageCell", "[JFJochReceiver]") { REQUIRE(x.GetImageNum() == 16); REQUIRE(x.GetStorageCellNumber() == 16); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -537,10 +502,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG1", "[JFJochReceiver]") { const uint16_t nthreads = 4; size_t nframes = 256; + // The card output repeats every distinct_frames frames + size_t distinct_frames = 32; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes); - - for (auto &i: pedestal_in) i = 16384 | 14000; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g1(1900); @@ -560,15 +525,17 @@ TEST_CASE("JFJochReceiverTest_PedestalG1", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - test->CreateJFPackets(x, 1, nframes, 0, pedestal_in.data()); + auto test = std::make_unique(i, 64); + for (int f = 0; f < distinct_frames; f++) + test->SetOutput(f, 0, *OutputWithPixels(pedestal_in.data() + f * RAW_MODULE_SIZE)); + test->CreateJFPackets(x, 1, nframes, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); Logger logger("JFJochReceiverTest_PedestalG1"); @@ -594,8 +561,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { const uint16_t nthreads = 4; size_t nframes = 150; + // The card output repeats every 2 * distinct_frames frames + size_t distinct_frames = 25; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes), pedestal_in2(RAW_MODULE_SIZE*nframes);; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames), pedestal_in2(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g2(1900); @@ -625,10 +594,14 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, nframes * 2); + auto test = std::make_unique(i, nframes * 2); + for (int j = 0; j < distinct_frames; j++) { + test->SetOutput(2 * j, 0, *OutputWithPixels(pedestal_in2.data() + j * RAW_MODULE_SIZE)); + test->SetOutput(2 * j + 1, 0, *OutputWithPixels(pedestal_in.data() + j * RAW_MODULE_SIZE)); + } for (int j = 0; j < nframes; j++) { - test->CreateJFPackets(x, 2 * j + 1, 1, 0, pedestal_in2.data() + j * RAW_MODULE_SIZE); - test->CreateJFPackets(x, 2 * j + 2, 1, 0, pedestal_in.data() + j * RAW_MODULE_SIZE); + test->CreateJFPackets(x, 2 * j + 1, 1, 0, nullptr); + test->CreateJFPackets(x, 2 * j + 2, 1, 0, nullptr); } test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -636,7 +609,7 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); Logger logger("JFJochReceiverTest_PedestalG2_storage_cell"); @@ -660,10 +633,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG0", "[JFJochReceiver]") { DiffractionExperiment x(DetJF(1)); const uint16_t nthreads = 4; size_t nframes = 500; + // The card output repeats every distinct_frames frames + size_t distinct_frames = 25; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes); - - for (auto &i: pedestal_in) i = 16384 | 14000; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g1(1900); @@ -683,15 +656,17 @@ TEST_CASE("JFJochReceiverTest_PedestalG0", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i,64); - test->CreateJFPackets(x, 1, nframes, 0, pedestal_in.data()); + auto test = std::make_unique(i, 64); + for (int f = 0; f < distinct_frames; f++) + test->SetOutput(f, 0, *OutputWithPixels(pedestal_in.data() + f * RAW_MODULE_SIZE)); + test->CreateJFPackets(x, 1, nframes, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); PixelMask mask(x); @@ -732,13 +707,12 @@ TEST_CASE("JFJochReceiverTest_PedestalG0_StorageCell", "[JFJochReceiver]") { .ImagesPerTrigger(0).IncidentEnergy_keV(12.4); AcquisitionDeviceGroup aq_devices; - auto test = std::make_unique(0, 64); - for (int i = 0; i < nframes; i++) { - test->CreateJFPackets(x, i * 4 + 1, 1, 0, pedestal_in_0.data()); - test->CreateJFPackets(x, i * 4 + 2, 1, 0, pedestal_in_1.data()); - test->CreateJFPackets(x, i * 4 + 3, 1, 0, pedestal_in_2.data()); - test->CreateJFPackets(x, i * 4 + 4, 1, 0, pedestal_in_3.data()); - } + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(pedestal_in_0.data())); + test->SetOutput(1, 0, *OutputWithPixels(pedestal_in_1.data())); + test->SetOutput(2, 0, *OutputWithPixels(pedestal_in_2.data())); + test->SetOutput(3, 0, *OutputWithPixels(pedestal_in_3.data())); + test->CreateJFPackets(x, 1, nframes * 4, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -790,13 +764,12 @@ TEST_CASE("JFJochReceiverTest_PedestalFixedG1_StorageCell", "[JFJochReceiver]") REQUIRE(x.GetStorageCellNumber() == 4); AcquisitionDeviceGroup aq_devices; - auto test = std::make_unique(0, 64); - for (int i = 0; i < nframes; i++) { - test->CreateJFPackets(x, i * 4 + 1, 1, 0, pedestal_in_0.data()); - test->CreateJFPackets(x, i * 4 + 2, 1, 0, pedestal_in_1.data()); - test->CreateJFPackets(x, i * 4 + 3, 1, 0, pedestal_in_2.data()); - test->CreateJFPackets(x, i * 4 + 4, 1, 0, pedestal_in_3.data()); - } + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(pedestal_in_0.data())); + test->SetOutput(1, 0, *OutputWithPixels(pedestal_in_1.data())); + test->SetOutput(2, 0, *OutputWithPixels(pedestal_in_2.data())); + test->SetOutput(3, 0, *OutputWithPixels(pedestal_in_3.data())); + test->CreateJFPackets(x, 1, nframes * 4, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -840,7 +813,7 @@ TEST_CASE("JFJochReceiverTest_PedestalG1_NoFrames", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, nframes + 8); + auto test = std::make_unique(i, nframes + 8); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } @@ -875,19 +848,28 @@ TEST_CASE("JFJochReceiverTest_PacketLost_Raw", "[JFJochReceiver]") { PixelMask pixel_mask(x); + // The card writes the missing packet as UINT16_MAX + std::vector frame_lost = frame_in; + for (int i = 127 * 4096; i < RAW_MODULE_SIZE; i++) + frame_lost[i] = UINT16_MAX; + AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, 64); - test->CreateJFPackets(x, 1, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 1, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 1, frame_in.data()); + auto test = std::make_unique(i, 64); + for (int image = 0; image < 4; image++) { + test->SetOutput(image, 0, *OutputWithPixels((image == 3) ? frame_lost.data() : frame_in.data())); + test->SetOutput(image, 1, *OutputWithPixels(frame_in.data())); + } + test->CreateJFPackets(x, 1, 1, 0, nullptr); + test->CreateJFPackets(x, 1, 1, 1, nullptr); + test->CreateJFPackets(x, 2, 1, 0, nullptr); + test->CreateJFPackets(x, 2, 1, 1, nullptr); + test->CreateJFPackets(x, 3, 1, 0, nullptr); + test->CreateJFPackets(x, 3, 1, 1, nullptr); for (int j = 0; j < 127; j++) - test->CreateJFPacket(x, 4, j, 0, frame_in.data() + (i * 128 + j) * 4096); - test->CreateJFPackets(x, 4, 1, 1, frame_in.data()); + test->CreateJFPacket(x, 4, j, 0, nullptr); + test->CreateJFPackets(x, 4, 1, 1, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -939,13 +921,15 @@ TEST_CASE("JFJochReceiverTest_Cancel", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, 64); - test->CreateJFPackets(x, 1, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 1, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 1, frame_in.data()); + auto test = std::make_unique(i, 64); + test->SetOutput(0, 0, *OutputWithPixels(frame_in.data())); + test->SetOutput(0, 1, *OutputWithPixels(frame_in.data())); + test->CreateJFPackets(x, 1, 1, 0, nullptr); + test->CreateJFPackets(x, 1, 1, 1, nullptr); + test->CreateJFPackets(x, 2, 1, 0, nullptr); + test->CreateJFPackets(x, 2, 1, 1, nullptr); + test->CreateJFPackets(x, 3, 1, 0, nullptr); + test->CreateJFPackets(x, 3, 1, 1, nullptr); aq_devices.Add(std::move(test)); } Logger logger("JFJochReceiverTest_Cancel"); @@ -958,13 +942,49 @@ TEST_CASE("JFJochReceiverTest_Cancel", "[JFJochReceiver]") { service.Start(x, pixel_mask, &calib); - std::this_thread::sleep_for(std::chrono::seconds(60)); + std::this_thread::sleep_for(std::chrono::seconds(1)); service.Cancel(false); auto receiver_out = service.Stop(); REQUIRE(receiver_out.status.cancelled); } +TEST_CASE("JFJochReceiverTest_Pedestal_Cancel", "[JFJochReceiver]") { + Logger logger("JFJochReceiverTest_Pedestal_Cancel"); + + DiffractionExperiment x(DetJF(1)); + x.Mode(DetectorMode::PedestalG0); + x.PedestalG0Frames(100).NumTriggers(1).UseInternalPacketGenerator(false).IncidentEnergy_keV(12.4); + + std::vector frame_in(RAW_MODULE_SIZE, 1000); + + // Only the first frames of the 100 arrive, and no final packet: the run waits until it is cancelled. + AcquisitionDeviceGroup aq_devices; + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(frame_in.data())); + test->CreateJFPackets(x, 1, 10, 0, nullptr); + aq_devices.Add(std::move(test)); + + PixelMask pixel_mask(x); + TestImagePusher pusher(0); + JFJochReceiverService service(aq_devices, logger, pusher); + service.NumThreads(4); + + service.Start(x, pixel_mask, nullptr); + std::this_thread::sleep_for(std::chrono::seconds(1)); + service.Cancel(false); + + auto output = service.Stop(); + REQUIRE(output.status.cancelled); + REQUIRE(output.efficiency < 1.0); + REQUIRE(output.received_packets.at(0) > 0); + REQUIRE(output.received_packets.at(0) < output.expected_packets.at(0)); + // Too few frames for a pedestal, so every pixel is reported as wrong + REQUIRE(output.pedestal_result.size() == 1); + REQUIRE(output.pedestal_result[0].CountMaskedPixels() == RAW_MODULE_SIZE); + REQUIRE(pusher.GetCounter() == 0); +} + TEST_CASE("JFJochReceiverTest_EIGER", "[JFJochReceiver]") { Logger logger("JFJochReceiverTest_EIGER"); @@ -975,13 +995,9 @@ TEST_CASE("JFJochReceiverTest_EIGER", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1003,13 +1019,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_8bit", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.EigerBitDepth(8); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1031,13 +1043,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_32bit", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.EigerBitDepth(32); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1059,13 +1067,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_conversion", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; diff --git a/tests/JFJochReceiverProcessingTest.cpp b/tests/JFJochReceiverProcessingTest.cpp index bc873d224..68e4c5a4a 100644 --- a/tests/JFJochReceiverProcessingTest.cpp +++ b/tests/JFJochReceiverProcessingTest.cpp @@ -6,7 +6,7 @@ #include #include "../receiver/JFJochReceiverTest.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "MockCardOutput.h" #include "../writer/HDF5Objects.h" #include "../receiver/JFJochReceiverService.h" #include "../preview/JFJochTIFF.h" @@ -50,13 +50,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index", "[JFJochReceiver]" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -72,13 +76,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index", "[JFJochReceiver]" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -142,7 +139,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_GPUStills", "[JFJoch // Setup acquisition device AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); + auto test = std::make_unique(0, 64); for (int m = 0; m < experiment.GetModulesNum(); m++) test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); @@ -225,7 +222,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_GPUStills_writer_down", "[JFJochReceiver]") ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); + auto test = std::make_unique(0, 64); for (int m = 0; m < experiment.GetModulesNum(); m++) test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); aq_devices.Add(std::move(test)); @@ -257,9 +254,7 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei // A cancel while the frames are held for the pre-pass ends the collection. Logger logger(Catch::getResultCapture().getCurrentTestName()); - // Few images: after the cancel, the simulated FPGA still processes, at CPU speed, the frames it has already - // taken in, which takes minutes for a long collection on a loaded machine. For the same reason, how long the - // stop takes is not checked. + // How long the stop takes is not checked. DiffractionExperiment experiment(DetJF4M()); experiment.ImagesPerTrigger(30).NumTriggers(1).UseInternalPacketGenerator(true) .FilePrefix("gpu_stills_cancel").JungfrauConvPhotonCnt(false) @@ -269,7 +264,7 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei PixelMask pixel_mask(experiment); AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; JFJochReceiverService service(aq_devices, logger, pusher); @@ -323,13 +318,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement", "[JFJoc ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -345,13 +344,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement", "[JFJoc // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -416,13 +408,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement_tetragona ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -438,13 +434,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement_tetragona // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -504,13 +493,18 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_integration", "[JFJoc ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + settings.quick_integration = true; // asked for, and still not done + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -526,14 +520,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_integration", "[JFJoc // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; - settings.quick_integration = true; // asked for, and still not done service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -603,13 +589,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_spots", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 20; // make sure there are zero spots possible + settings.photon_count_threshold = 10000; + settings.min_pix_per_spot = 5; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -625,13 +615,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_spots", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 20; // make sure there are zero spots possible - settings.photon_count_threshold = 10000; - settings.min_pix_per_spot = 5; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -684,13 +667,11 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_azint_2d", "[JFJochReceiver]") { ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, + DiffractionExperiment::DefaultDataProcessingSettings())); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -757,13 +738,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT", "[JFJochReceiv ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -779,13 +764,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT", "[JFJochReceiv // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -846,13 +824,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFTW", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -868,13 +850,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFTW", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -936,13 +911,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_reference", "[JF ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -958,13 +937,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_reference", "[JF // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1026,13 +998,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_wrong_reference" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1048,13 +1024,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_wrong_reference" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1120,13 +1089,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_grid", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1142,13 +1115,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_grid", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1224,13 +1190,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_32", "[JFJochReceive ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1246,13 +1216,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_32", "[JFJochReceive // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1313,12 +1276,15 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_min_pix_2", "[JFJoch ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 3; + settings.min_pix_per_spot = 2; + settings.max_pix_per_spot = 200; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); ZMQImagePuller puller(pusher.GetAddress()[0]); @@ -1329,11 +1295,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_min_pix_2", "[JFJoch service.NumThreads(nthreads); service.Indexing(experiment.GetIndexingSettings()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 3; - settings.min_pix_per_spot = 2; - settings.max_pix_per_spot = 200; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1449,13 +1410,11 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_ROI", "[JFJochReceiver]") { } } - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, + DiffractionExperiment::DefaultDataProcessingSettings())); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); ZMQImagePuller puller(pusher.GetAddress()[0]); @@ -1523,7 +1482,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQPreview", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; @@ -1587,7 +1546,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQMetadata", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; @@ -1647,7 +1606,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher; @@ -1681,7 +1640,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher_republish", "[JFJochReceiver]") PixelMask pixel_mask(experiment); AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher(repub_addr); @@ -1743,7 +1702,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher_Raw", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher; @@ -1793,13 +1752,13 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_no_processing", "[JFJochReceiver]") ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.enable = false; + settings.indexing = true; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); TCPImagePuller puller(pusher.GetAddress()[0]); @@ -1813,9 +1772,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_no_processing", "[JFJochReceiver]") // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.enable = false; - settings.indexing = true; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1870,13 +1826,17 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index", "[JFJochReceiver]" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -1891,13 +1851,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index", "[JFJochReceiver]" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1952,9 +1905,7 @@ TEST_CASE("JFJochIntegrationTest_TCP_calibration", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - - aq_devices.Add(std::move(test)); + aq_devices.Add(std::make_unique(0, 64)); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -2012,13 +1963,17 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index_single_file", "[JFJo ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -2033,13 +1988,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index_single_file", "[JFJo // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); diff --git a/tests/JFJochStateMachineTest.cpp b/tests/JFJochStateMachineTest.cpp index c501586c3..7f5c4d3a4 100644 --- a/tests/JFJochStateMachineTest.cpp +++ b/tests/JFJochStateMachineTest.cpp @@ -3,7 +3,7 @@ #include #include "../broker/JFJochStateMachine.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "../acquisition_device/MockAcquisitionDevice.h" #include "../receiver/JFJochReceiverService.h" #include "../broker/OpenAPIConvert.h" #include "../broker/ModulePlacementConvert.h" @@ -278,7 +278,7 @@ TEST_CASE("JFJochStateMachine_AsyncStartFailure") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); RefusingImagePusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -452,7 +452,7 @@ TEST_CASE("JFJochStateMachine_CancelledRunIsNotAnError") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); GatedPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -508,7 +508,7 @@ TEST_CASE("JFJochStateMachine_WriterFailureAtEndIsAnError") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); FinalizeFailingPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -563,7 +563,7 @@ TEST_CASE("JFJochStateMachine_PreflightRefusesBeforeStarting") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); PreflightRefusingPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -682,7 +682,7 @@ TEST_CASE("JFJochStateMachine_StartModulePlacement") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); StartMessageCapture pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); JFJochServices services(logger); diff --git a/tests/MockCardOutput.h b/tests/MockCardOutput.h new file mode 100644 index 000000000..217ee3b17 --- /dev/null +++ b/tests/MockCardOutput.h @@ -0,0 +1,220 @@ +// 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); +} diff --git a/tests/StreamWriterTest.cpp b/tests/StreamWriterTest.cpp index 1b767fb64..e797b158e 100644 --- a/tests/StreamWriterTest.cpp +++ b/tests/StreamWriterTest.cpp @@ -12,6 +12,7 @@ #include "../receiver/JFJochReceiverService.h" #include "../image_pusher/ZMQWriterNotificationPuller.h" #include "../image_puller/TestImagePuller.h" +#include "../acquisition_device/MockAcquisitionDevice.h" TEST_CASE("StreamWriterTest_ZMQ", "[StreamWriter]") { RegisterHDF5Filter(); @@ -27,7 +28,7 @@ TEST_CASE("StreamWriterTest_ZMQ", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); JFJochReceiverService fpga_receiver_service(aq_devices, logger, pusher); @@ -83,7 +84,7 @@ TEST_CASE("StreamWriterTest_ZMQ_Update", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -144,7 +145,7 @@ TEST_CASE("StreamWriterTest_ZMQ_Update_NoNotification", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -208,7 +209,7 @@ TEST_CASE("StreamWriterTest_ZMQ_MasterAfterAllDataFiles", "[StreamWriter]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*", "ipc://*"}); pusher.WriterNotificationSocket("ipc://*");