Files
Jungfraujoch/tests/JFJochReceiverRugnuxTest.cpp
leonarski_fandClaude Opus 5.5 8f3ba8d389 rugnux live: a cancelled rotation run reports CANCELLED, counted in images received
A cancelled collection showed verdict FAILED, "No crystal lattice was found
in these frames" and "cancelled after 0 images", with step titles carrying
the planned image counts. The live status now has verdict CANCELLED, says
how many images came before the cancel, and a step title's ", N images" is
capped at the images received once the run is cut by a cancel. The report
of a rugnux CLI run is unchanged. The frontend shows CANCELLED as info,
not as an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
2026-11-08 11:19:08 +01:00

842 lines
34 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include <mutex>
#include "../acquisition_device/MockAcquisitionDevice.h"
#include "../compression/JFJochCompressor.h"
#include "../image_analysis/structure_refinement/RugnuxModel.h"
#include "../image_puller/TestImagePuller.h"
#include "../image_puller/ZMQImagePuller.h"
#include "../image_pusher/ImagePusher.h"
#include "../image_pusher/ZMQStream2Pusher.h"
#include "../receiver/JFJochReceiverDarkMask.h"
#include "../receiver/JFJochReceiverLite.h"
#include "../receiver/JFJochReceiverRugnuxRotation.h"
#include "../receiver/JFJochReceiverRugnuxStills.h"
#include "../receiver/JFJochReceiverService.h"
#include "../writer/HDF5Objects.h"
#include "../writer/StreamWriter.h"
namespace {
// What the writer would be given, in order.
class RecordingPusher : public ImagePusher {
mutable std::mutex m;
std::vector<std::string> events;
std::optional<StartMessage> start;
std::optional<EndMessage> end;
std::vector<int64_t> images;
public:
void StartDataCollection(StartMessage &message) override {
std::unique_lock ul(m);
events.emplace_back("start");
start = message;
}
bool EndDataCollection(const EndMessage &message) override {
std::unique_lock ul(m);
events.emplace_back("end");
end = message;
return true;
}
bool SendImage(const uint8_t *, size_t, int64_t number) override {
std::unique_lock ul(m);
if (events.empty() || events.back() != "image")
events.emplace_back("image");
images.push_back(number);
return true;
}
bool SendCalibration(const CompressedImage &) override {
std::unique_lock ul(m);
events.emplace_back("calibration");
return true;
}
std::string PrintSetup() const override { return "recording"; }
ImagePusherType GetType() const override { return ImagePusherType::Test; }
std::vector<std::string> Events() const { std::unique_lock ul(m); return events; }
std::vector<int64_t> Images() const { std::unique_lock ul(m); return images; }
std::optional<StartMessage> Start() const { std::unique_lock ul(m); return start; }
std::optional<EndMessage> End() const { std::unique_lock ul(m); return end; }
};
DiffractionExperiment StillsExperiment(int64_t images) {
DiffractionExperiment x(DetDECTRIS(2068, 2164, "Test", {}));
// As JFJochReceiverLite's test: the frames are signed int16, and the stream's start message says so.
x.ImagesPerTrigger(images).NumTriggers(1).ImagesPerFile(2).FilePrefix("rugnux_stills").JungfrauConvPhotonCnt(false)
.PixelSigned(true).DetectorDistance_mm(75).BeamY_pxl(1136).BeamX_pxl(1090).IncidentEnergy_keV(12.4)
.SetUnitCell(UnitCell{.a = 36.9, .b = 78.95, .c = 78.95, .alpha = 90, .beta = 90, .gamma = 90});
return x;
}
// The frames of the test data, as a stream delivers them: compressed, one DataMessage each, no more.
class Stream {
std::vector<std::vector<uint8_t>> frames;
size_t width, height;
public:
const std::shared_ptr<TestImagePuller> puller = std::make_shared<TestImagePuller>();
// With same_frame >= 0, every frame is that frame of the test data.
Stream(const DiffractionExperiment &x, int64_t nframes, bool blank = false, int64_t same_frame = -1)
: width(x.GetXPixelsNum()), height(x.GetYPixelsNum()) {
HDF5ReadOnlyFile data("../../tests/test_data/compression_benchmark.h5");
HDF5DataSet dataset(data, "/entry/data/data");
HDF5DataSpace file_space(dataset);
std::vector<int16_t> image(file_space.GetDimensions()[1] * file_space.GetDimensions()[2]);
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
for (int64_t i = 0; i < nframes; i++) {
const int64_t frame = (same_frame >= 0) ? same_frame : i;
const std::vector<hsize_t> start = {static_cast<hsize_t>(frame % file_space.GetDimensions()[0]), 0, 0};
const std::vector<hsize_t> size = {1, file_space.GetDimensions()[1], file_space.GetDimensions()[2]};
if (blank)
std::fill(image.begin(), image.end(), 0);
else
dataset.ReadVector(image, start, size);
frames.push_back(compressor.Compress(image));
}
StartMessage start_msg;
x.FillMessage(start_msg);
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(start_msg)});
}
void Frames(int64_t first, int64_t count) {
for (int64_t i = first; i < first + count; i++) {
DataMessage message;
message.number = i;
message.image = CompressedImage(frames[i].data(), frames[i].size(), width, height,
CompressedImageMode::Int16, CompressionAlgorithm::BSHUF_LZ4);
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(message)});
}
}
void End() {
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(EndMessage{})});
}
};
void WaitForStatus(JFJochReceiverService &service, const std::function<bool(const RugnuxLiveSnapshot &)> &what) {
const auto give_up = std::chrono::steady_clock::now() + std::chrono::minutes(5);
while (!what(service.GetRugnuxStatus())) {
REQUIRE(std::chrono::steady_clock::now() < give_up);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
bool HasNote(const RugnuxLiveSnapshot &s, const std::string &text) {
for (const auto &step: s.steps)
for (const auto &note: step.notes)
if (note.find(text) != std::string::npos)
return true;
return false;
}
}
TEST_CASE("JFJochReceiverRugnux_Stills", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 12;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, images);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
// Integration in live stills is rugnux's own, whatever the broker's spot-finding settings say.
SpotFindingSettings spot_finding;
spot_finding.quick_integration = false;
service.SetSpotFindingSettings(spot_finding);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.efficiency == 1.0f);
CHECK(!out.status.cancelled);
CHECK(out.status.images_collected == images);
CHECK(out.status.images_sent == images);
REQUIRE(out.status.indexing_rate);
CHECK(out.status.indexing_rate.value() > 0.5f);
CHECK(out.scan_result.images.size() == images);
// The writer sees the start message before the frames, the end message last.
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
auto sent = pusher.Images();
std::sort(sent.begin(), sent.end());
REQUIRE(sent.size() == images);
CHECK(sent.front() == 0);
CHECK(sent.back() == images - 1);
// The merged data travel in the end message, with the geometry the frames were analysed at.
const auto end = pusher.End();
REQUIRE(end);
CHECK(end->merged.has_value());
CHECK(end->unit_cell.has_value());
CHECK(end->refined_beam_center_x.has_value());
CHECK(end->refined_detector_distance_mm.has_value());
REQUIRE(pusher.Start());
CHECK(pusher.Start()->beam_center_x == Catch::Approx(end->refined_beam_center_x.value()));
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
CHECK(live.cut.empty());
CHECK(HasNote(live, "Merge of 50% of the images (provisional)"));
CHECK(HasNote(live, "Merge of 75% of the images (provisional)"));
CHECK(HasNote(live, "Merge of the collection"));
REQUIRE(live.stills);
CHECK(live.stills->prepass == RugnuxLivePrePass::Done);
CHECK(live.stills->images_analysed == images);
CHECK(live.stills->images_indexed > images / 2);
REQUIRE(live.last_image_to_written_s);
CHECK(!live.space_group.empty());
REQUIRE(!service.GetProgress().has_value());
}
TEST_CASE("JFJochReceiverRugnux_Stills_CancelDuringPrePass", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 8;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
int64_t given = 0;
SECTION("while it holds frames") {
given = 3;
stream.Frames(0, given);
service.Start(x, mask, nullptr, stream.puller);
WaitForStatus(service, [&](const RugnuxLiveSnapshot &s) {
return s.stills && s.stills->images_held == given;
});
}
SECTION("while it runs") {
given = images;
stream.Frames(0, given);
service.Start(x, mask, nullptr, stream.puller);
WaitForStatus(service, [&](const RugnuxLiveSnapshot &s) {
return s.stills && s.stills->prepass == RugnuxLivePrePass::Running;
});
}
service.Cancel(false);
const auto out = service.Stop();
// The frames that came are still analysed and written, and the collection ends properly.
CHECK(out.status.cancelled);
CHECK(out.status.images_sent == given);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
REQUIRE(live.stills);
CHECK(live.stills->images_analysed == given);
REQUIRE(!service.GetProgress().has_value());
}
TEST_CASE("JFJochReceiverRugnux_Stills_CancelDuringRun", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 40;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
// Twelve slots: the pre-pass ends on six frames, and the analysis starts with most of the run to come.
const size_t buffer_MiB = 12 * x.GetImageBufferLocationSize() / (1024 * 1024) + 1;
const int64_t given = 8;
stream.Frames(0, given);
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher, buffer_MiB);
service.NumThreads(2);
service.Indexing(x.GetIndexingSettings());
// A model only so that there is a model step for the cancel to cut.
service.RugnuxLive(true, std::make_shared<RugnuxModel>(), 4, 60);
service.Start(x, mask, nullptr, stream.puller);
WaitForStatus(service, [](const RugnuxLiveSnapshot &s) {
return s.stills && s.stills->prepass == RugnuxLivePrePass::Done && s.stills->images_analysed > 0;
});
service.Cancel(false);
const auto out = service.Stop();
CHECK(out.status.cancelled);
CHECK(out.status.images_sent == given);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
CHECK(live.cut == "cancel");
REQUIRE(!service.GetProgress().has_value());
}
TEST_CASE("JFJochReceiverRugnux_Stills_ShortRun", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
DiffractionExperiment x = StillsExperiment(8);
PixelMask mask(x);
Stream stream(x, 3);
stream.Frames(0, 3);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
// The pre-pass runs on what came.
CHECK(out.efficiency == Catch::Approx(3.0 / 8.0));
CHECK(!out.status.cancelled);
CHECK(out.status.images_sent == 3);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
REQUIRE(live.stills);
CHECK(live.stills->images_analysed == 3);
}
TEST_CASE("JFJochReceiverRugnux_Stills_FramesBeyondTheCollection", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
DiffractionExperiment x = StillsExperiment(3);
PixelMask mask(x);
Stream stream(x, 5);
stream.Frames(0, 5);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.status.images_collected == 3);
CHECK(out.status.images_sent == 3);
}
TEST_CASE("JFJochReceiverRugnux_Stills_NoFrames", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
DiffractionExperiment x = StillsExperiment(8);
PixelMask mask(x);
Stream stream(x, 0);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.status.images_sent == 0);
CHECK(pusher.Events() == std::vector<std::string>{"start", "end"});
CHECK(service.GetRugnuxStatus().state == RugnuxLiveState::Done);
}
TEST_CASE("JFJochReceiverRugnux_Stills_NoIndexableFrames", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 6;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images, true);
stream.Frames(0, images);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
// Written, with no merge, at the geometry in the header.
CHECK(!out.status.cancelled);
CHECK(out.status.images_sent == images);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
REQUIRE(pusher.End());
CHECK(!pusher.End()->merged.has_value());
CHECK(pusher.End()->refined_beam_center_x == Catch::Approx(x.GetBeamX_pxl()));
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
CHECK(HasNote(live, "could not refine the geometry"));
REQUIRE(live.stills);
CHECK(live.stills->images_analysed == images);
CHECK(live.stills->images_indexed == 0);
}
TEST_CASE("JFJochReceiverRugnux_Stills_WriterDown", "[JFJochReceiverRugnux]") {
// The start message goes out after the pre-pass: where it fails, the collection ends with the error.
class NoWriter : public RecordingPusher {
public:
bool down = true;
void StartDataCollection(StartMessage &message) override {
if (down)
throw JFJochException(JFJochExceptionCategory::ZeroMQ, "no writer");
RecordingPusher::StartDataCollection(message);
}
};
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 4;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, images);
stream.End();
NoWriter pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
REQUIRE_THROWS_AS(service.Stop(), JFJochException);
CHECK(pusher.Events().empty());
CHECK(service.GetRugnuxStatus().state == RugnuxLiveState::Failed);
// And the next one runs.
pusher.down = false;
Stream next(x, images);
next.Frames(0, images);
next.End();
service.Start(x, mask, nullptr, next.puller);
CHECK(service.Stop().status.images_sent == images);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
}
TEST_CASE("JFJochReceiverRugnux_Stills_ZMQWriter", "[JFJochReceiverRugnux]") {
// As the removed GPU-stills test of the FPGA receiver: five copies of one frame, through ZeroMQ to the writer.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
x.FilePrefix("rugnux_stills_zmq").SetFileWriterFormat(FileWriterFormat::NXmxVDS).OverwriteExistingFiles(true);
PixelMask mask(x);
Stream stream(x, images, false, 4);
stream.Frames(0, images);
stream.End();
ZMQStream2Pusher pusher({"ipc://*"});
pusher.WriterNotificationSocket("ipc://*");
ZMQImagePuller puller(pusher.GetAddress()[0]);
StreamWriter writer(logger, puller);
auto writer_future = std::async(std::launch::async, &StreamWriter::Run, &writer);
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
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(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.efficiency == 1.0f);
REQUIRE(out.status.indexing_rate);
CHECK(out.status.indexing_rate.value() == 1.0f);
CHECK(out.status.images_sent == images);
CHECK(!out.status.cancelled);
// Done once everything is written, with the latency from the last image to then.
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
REQUIRE(live.last_image_to_written_s);
REQUIRE(live.last_image_s);
CHECK(live.elapsed_s >= live.last_image_s.value() + live.last_image_to_written_s.value() - 1e-6);
REQUIRE(!service.GetProgress().has_value());
REQUIRE_NOTHROW(writer_future.get());
}
TEST_CASE("JFJochReceiverRugnux_Stills_ZMQWriterDown", "[JFJochReceiverRugnux]") {
// With no writer listening the collection ends with the error, and the next one runs.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
x.FilePrefix("rugnux_stills_zmq_writer_down").SetFileWriterFormat(FileWriterFormat::NXmxVDS)
.OverwriteExistingFiles(true);
PixelMask mask(x);
ZMQStream2Pusher pusher({"ipc://*"});
pusher.WriterNotificationSocket("ipc://*");
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
Stream stream(x, images, false, 4);
stream.Frames(0, images);
stream.End();
service.Start(x, mask, nullptr, stream.puller);
REQUIRE_THROWS_AS(service.Stop(), JFJochException);
REQUIRE(!service.GetProgress().has_value());
ZMQImagePuller puller(pusher.GetAddress()[0]);
StreamWriter writer(logger, puller);
auto writer_future = std::async(std::launch::async, &StreamWriter::Run, &writer);
Stream next(x, images, false, 4);
next.Frames(0, images);
next.End();
service.Start(x, mask, nullptr, next.puller);
const auto out = service.Stop();
CHECK(out.status.images_sent == images);
CHECK(!out.status.cancelled);
REQUIRE_NOTHROW(writer_future.get());
}
TEST_CASE("JFJochReceiverRugnux_Stills_BufferFullDuringPrePass", "[JFJochReceiverRugnux]") {
// Forty frames, all there before the pre-pass ends, and twelve slots in the image buffer.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 40;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, images);
stream.End();
const size_t buffer_MiB = 12 * x.GetImageBufferLocationSize() / (1024 * 1024) + 1;
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher, buffer_MiB);
service.NumThreads(2);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
// No deadlock, and the end message goes out.
CHECK(!out.status.cancelled);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
// A frame that finds no free slot is not written, and the collection says so.
CHECK(out.status.images_collected == images);
CHECK(out.efficiency == 1.0f);
CHECK(out.writer_queue_full_warning == (out.status.images_sent < images));
CHECK(out.scan_result.images.size() == out.status.images_sent);
REQUIRE(pusher.End());
CHECK(pusher.End()->images_collected_count == images);
CHECK(pusher.End()->images_sent_to_write_count == out.status.images_sent);
CHECK(pusher.Images().size() == out.status.images_sent);
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Done);
REQUIRE(live.stills);
CHECK(live.stills->images_analysed == out.status.images_sent);
}
TEST_CASE("JFJochReceiverRugnux_Rotation", "[JFJochReceiverRugnux]") {
// A scan stays with rugnux's own run: the start message goes out at once, and there are no stills results.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
x.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, {1, 0, 0}, {}));
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, images);
stream.End();
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.status.images_sent == images);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
const auto live = service.GetRugnuxStatus();
CHECK(!live.stills);
CHECK(live.images_received == images);
CHECK(live.state != RugnuxLiveState::Idle);
CHECK(live.state != RugnuxLiveState::Running);
}
TEST_CASE("JFJochReceiverRugnux_Rotation_DoesNotFit", "[JFJochReceiverRugnux]") {
// A scan the image buffer cannot hold is analysed image by image, and the live status says why rugnux did not run.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
x.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, {1, 0, 0}, {}));
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, images);
stream.End();
// Three slots
const size_t buffer_MiB = (3 * x.GetImageBufferLocationSize()) / (1024 * 1024) + 1;
REQUIRE(buffer_MiB * 1024 * 1024 / x.GetImageBufferLocationSize() < images);
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher, buffer_MiB);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, stream.puller);
const auto out = service.Stop();
CHECK(out.status.images_collected == images);
CHECK(out.scan_result.images.size() == images);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
CHECK(out.analysis_warning.find("image buffer") != std::string::npos);
const auto live = service.GetRugnuxStatus();
CHECK(live.state == RugnuxLiveState::Skipped);
CHECK(live.message.find("image buffer") != std::string::npos);
}
TEST_CASE("JFJochReceiverRugnux_Rotation_Cancel", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
x.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, {1, 0, 0}, {}));
PixelMask mask(x);
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.Indexing(x.GetIndexingSettings());
service.RugnuxLive(true, nullptr, 4, 60);
SECTION("Before the start message") {
service.Start(x, mask, nullptr, std::make_shared<TestImagePuller>());
service.Cancel(false);
const auto out = service.Stop();
CHECK(out.status.cancelled);
CHECK(pusher.Events().empty());
}
SECTION("Detector finished, no start message") {
service.Start(x, mask, nullptr, std::make_shared<TestImagePuller>());
service.Cancel(true);
const auto out = service.Stop();
CHECK(out.status.cancelled);
CHECK(pusher.Events().empty());
}
SECTION("During the collection") {
Stream stream(x, images);
stream.Frames(0, 2);
service.Start(x, mask, nullptr, stream.puller);
WaitForStatus(service, [](const RugnuxLiveSnapshot &s) { return s.images_received == 2; });
service.Cancel(false);
const auto out = service.Stop();
CHECK(out.status.cancelled);
CHECK(out.status.images_sent == 2);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
const auto live = service.GetRugnuxStatus();
CHECK(live.cut == "cancel");
CHECK(live.state != RugnuxLiveState::Running);
// A cancelled collection, not a failed analysis, and counted in the images that came
CHECK(live.verdict == "CANCELLED");
CHECK(live.verdict_text.find("after 2 images") != std::string::npos);
for (const auto &step: live.steps) {
const size_t pos = step.title.rfind(", ");
if (pos != std::string::npos && step.title.ends_with(" images"))
CHECK(std::stoll(step.title.substr(pos + 2)) <= 2);
}
}
SECTION("A silent cancel lets the collection finish") {
Stream stream(x, images);
stream.Frames(0, 2);
service.Start(x, mask, nullptr, stream.puller);
WaitForStatus(service, [](const RugnuxLiveSnapshot &s) { return s.images_received == 2; });
service.Cancel(true);
stream.Frames(2, images - 2);
stream.End();
const auto out = service.Stop();
CHECK(!out.status.cancelled);
CHECK(out.status.images_sent == images);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
CHECK(service.GetRugnuxStatus().cut.empty());
}
}
// A leaf joins its threads in its own destructor, with no one having asked for the end of the collection.
TEST_CASE("JFJochReceiverRugnux_DestroyedMidRun", "[JFJochReceiverRugnux]") {
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
const int64_t images = 5;
DiffractionExperiment x = StillsExperiment(images);
PixelMask mask(x);
Stream stream(x, images);
stream.Frames(0, 2);
RecordingPusher pusher;
JFJochReceiverCurrentStatus status;
JFJochReceiverPlots plots;
RugnuxLiveStatus live;
ImageBuffer buffer(512 * 1024 * 1024);
SECTION("JFJochReceiverLite") {
JFJochReceiverLite receiver(x, mask, *stream.puller, pusher, logger, 4, DiffractionExperiment::DefaultDataProcessingSettings(),
status, plots, buffer);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
SECTION("JFJochReceiverDarkMask") {
DiffractionExperiment dark = x;
dark.Mode(DetectorMode::DarkMask);
JFJochReceiverDarkMask receiver(dark, mask, *stream.puller, pusher, logger, 4, DiffractionExperiment::DefaultDataProcessingSettings(),
status, plots, buffer);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
SECTION("JFJochReceiverRugnuxRotation") {
DiffractionExperiment rotation = x;
rotation.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, {1, 0, 0}, {}));
JFJochReceiverRugnuxRotation receiver(rotation, mask, *stream.puller, pusher, logger, 4, 4, nullptr, 60, live,
DiffractionExperiment::DefaultDataProcessingSettings(), status, plots, buffer);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
SECTION("JFJochReceiverRugnuxStills") {
JFJochReceiverRugnuxStills receiver(x, mask, *stream.puller, pusher, logger, 4, nullptr, 60, live,
DiffractionExperiment::DefaultDataProcessingSettings(), status, plots, buffer);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
SUCCEED("the destructor joined the threads");
}
TEST_CASE("JFJochReceiverFPGA_RugnuxOnStaysPerImage", "[JFJochReceiverRugnux]") {
// Live stills are this receiver's alone until a puller feeds it: the card's run is as with rugnux off.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
DiffractionExperiment x(DetJF(1));
x.ImagesPerTrigger(2).NumTriggers(1).UseInternalPacketGenerator(true).FilePrefix("rugnux_on_fpga")
.DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(250).IncidentEnergy_keV(12.4);
PixelMask mask(x);
AcquisitionDeviceGroup aq_devices;
aq_devices.Add(std::make_unique<MockAcquisitionDevice>(0, 64));
RecordingPusher pusher;
JFJochReceiverService service(aq_devices, logger, pusher);
service.NumThreads(2);
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr);
const auto out = service.Stop();
CHECK(out.status.images_sent == 2);
CHECK(pusher.Events() == std::vector<std::string>{"start", "image", "end"});
CHECK(service.GetRugnuxStatus().state == RugnuxLiveState::Idle);
}
TEST_CASE("JFJochReceiverRugnux_DarkMaskStaysOut", "[JFJochReceiverRugnux]") {
// The mask sequence of a later POST /pedestal, with rugnux on: the state machine's experiment still has the
// last /start's goniometer, or none.
Logger logger(Catch::getResultCapture().getCurrentTestName());
RegisterHDF5Filter();
DiffractionExperiment x(DetDECTRIS(2068, 2164, "Test", {}));
SECTION("after a serial collection") {}
SECTION("after a rotation collection") {
x.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, {1, 0, 0}, {}));
}
DarkMaskSettings mask_settings;
mask_settings.NumberOfFrames(10).MaxCounts(1).MaxFramesWithCounts(5);
x.Mode(DetectorMode::DarkMask).ImportDarkMaskSettings(mask_settings);
PixelMask mask(x);
auto puller = std::make_shared<TestImagePuller>();
StartMessage start_msg;
x.FillMessage(start_msg);
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(start_msg)});
std::vector<uint16_t> image(x.GetPixelsNum(), 0);
image[2] = 2;
for (int i = 0; i < x.GetFrameNum(); i++) {
DataMessage message;
message.image = CompressedImage(image, x.GetXPixelsNum(), x.GetYPixelsNum());
message.number = i;
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(message)});
}
puller->Put(ImagePullerOutput{.cbor = std::make_shared<CBORStream2DeserializerOutput>(EndMessage{})});
RecordingPusher pusher;
AcquisitionDeviceGroup group;
JFJochReceiverService service(group, logger, pusher);
service.NumThreads(4);
service.RugnuxLive(true, nullptr, 4, 60);
service.Start(x, mask, nullptr, puller);
const auto out = service.Stop();
REQUIRE(out.dark_mask_result.size() == x.GetPixelsNum());
CHECK(out.dark_mask_result[2] != 0);
CHECK(out.dark_mask_result[0] == 0);
CHECK(out.status.images_sent == 0);
CHECK(!service.GetRugnuxStatus().stills);
}