Files
Jungfraujoch/tests/DetectorSetupTest.cpp
T
jungfrauandClaude Opus 5 a97d763098
Build Packages / build:viewer-tgz:cpu (push) Successful in 20m56s
Build Packages / build:viewer-tgz:cuda (push) Successful in 24m22s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 25m52s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 26m12s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 31m13s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 31m27s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 32m3s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 20m9s
Build Packages / XDS test (durin plugin) (push) Successful in 11m44s
Build Packages / build:rpm (rocky9) (push) Successful in 22m43s
Build Packages / Generate python client (push) Successful in 35s
Build Packages / Build documentation (push) Successful in 1m25s
Build Packages / Create release (push) Skipped
Build Packages / build:rpm (rocky8) (push) Successful in 27m26s
Build Packages / DIALS test (push) Successful in 21m34s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 26m50s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 21m57s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 12m11s
Build Packages / XDS test (neggia plugin) (push) Successful in 10m6s
Build Packages / Unit tests (push) Successful in 1h56m8s
Build Packages / build:windows:nocuda (push) Canceled after 0s
Build Packages / build:windows:cuda (push) Canceled after 0s
Do not assume a DECTRIS detector's bit depths before asking it
DetectorSetup gave a DECTRIS detector bit_depth_image = 16 and bit_depth_readout = 16
the moment it was constructed. Both mean "the detector told us", and at construction
nothing has asked it: the SIMPLON client learns them at configure time, and the lite
receiver reads them off the start message. Until then they were an assumption wearing
the clothes of a measurement.

The assumption was load-bearing in the wrong direction. GetByteDepthImage() gives the
detector's image depth absolute priority - correctly, since the DECTRIS path forwards
images verbatim and the depth has to be the one the pixels actually have - so a value
that was always set meant everything below it was unreachable on DECTRIS:

  * image_format_settings.bit_depth_image, which the API documents as "bit depth of
    resulting image ... if not provided value is adjusted automatically", was silently
    discarded. It works on JUNGFRAU, where the detector's value is not set, and did
    nothing at all on an EIGER. No error, no warning.
  * so was the promotion to four bytes that summation needs.

Both are now unset for DECTRIS. JUNGFRAU keeps its readout depth of 16, because there
that is a property of the hardware rather than something configured.

Where nothing supplies a depth the answer does not change: GetByteDepthImage() falls
through to the same two bytes it produced before, and the fallback no longer throws
merely because the readout depth is not known yet - GetBitDepthReadoutIfKnown() reports
it as unknown instead of inventing one. The start message carries it as an optional
already, and the receiver already guards on that, so "not known" travels end to end
rather than being papered over. GetBitDepthReadout() still throws for the callers that
genuinely require a value; all of them are on the FPGA path, where it is always set.

Also fixes the jfjoch_test build, which the previous commit broke: SpotExtractorGPU's
Extract() takes a PixelView now, and the parity test still passed a raw pointer. That
should have been caught before it was pushed.

Tests: DetectorSetup (with a new case pinning the contract this restores), plus
DiffractionExperiment, CBOR, writer, HDF5, preprocessing, azimuthal integration and the
GPU spot-finding / integration / decoder suites. rugnux unchanged end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 17:48:19 -04:00

229 lines
10 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/DetectorSetup.h"
#include "../common/NetworkAddressConvert.h"
#include "../common/DiffractionExperiment.h"
TEST_CASE("DetectorSetup_MismatchInSize") {
REQUIRE_THROWS(
DetectorSetup(DetectorGeometryModular(8), DetectorType::JUNGFRAU, "JF", {"mx1", "mx2", "mx3", "mx4"}));
REQUIRE_THROWS(
DetectorSetup(DetectorGeometryModular(2), DetectorType::JUNGFRAU, "JF", {"mx1", "mx2", "mx3", "mx4"}));
REQUIRE_NOTHROW(
DetectorSetup(DetectorGeometryModular(4), DetectorType::JUNGFRAU, "JF", {"mx1", "mx2", "mx3", "mx4"}));
}
TEST_CASE("DetectorSetup_MismatchInSize_EIGER") {
REQUIRE_THROWS(DetectorSetup(DetectorGeometryModular(4), DetectorType::EIGER, "JF", {"mx1", "mx2", "mx3", "mx4"}));
REQUIRE_NOTHROW(DetectorSetup(DetectorGeometryModular(2), DetectorType::EIGER, "JF", {"mx1", "mx2", "mx3", "mx4"}));
}
TEST_CASE("DetectorSetup_MismatchInSize_DECTRIS") {
REQUIRE_NOTHROW(DetectorSetup(DetectorGeometryFixed(123, 112), DetectorType::DECTRIS, "JF", {"mx1"}));
REQUIRE_NOTHROW(DetectorSetup(DetectorGeometryFixed(123, 112), DetectorType::DECTRIS, "JF", {}));
REQUIRE_THROWS(DetectorSetup(DetectorGeometryFixed(123, 112), DetectorType::DECTRIS, "JF", {"mx1", "mx2"}));
}
TEST_CASE("DetectorSetup_MismatchInGeometry") {
REQUIRE_THROWS(DetectorSetup(DetectorGeometryFixed(123, 112), DetectorType::EIGER, "JF"));
REQUIRE_THROWS(DetectorSetup(DetectorGeometryFixed(123, 112), DetectorType::JUNGFRAU, "JF"));
REQUIRE_THROWS(DetectorSetup(DetectorGeometryModular(2), DetectorType::DECTRIS, "JF"));
}
TEST_CASE("DetectorSetup_ReadoutDepth") {
auto setup = DetDECTRIS(123,123, "zzz", "a");
REQUIRE_NOTHROW(setup.BitDepthReadout(16));
REQUIRE(setup.GetBitDepthReadout() == 16);
REQUIRE_NOTHROW(setup.BitDepthReadout(12));
REQUIRE(setup.GetBitDepthReadout() == 12);
REQUIRE_NOTHROW(setup.BitDepthReadout(32));
REQUIRE(setup.GetBitDepthReadout() == 32);
REQUIRE_NOTHROW(setup.BitDepthReadout(8));
REQUIRE(setup.GetBitDepthReadout() == 8);
REQUIRE_THROWS(setup.BitDepthReadout(0));
REQUIRE_THROWS(setup.BitDepthReadout(15));
REQUIRE_THROWS(setup.BitDepthReadout(-1));
auto setup2 = DetJF(1);
REQUIRE(setup2.GetBitDepthReadout() == 16);
REQUIRE_THROWS(setup2.BitDepthReadout(32));
auto setup3 = DetEIGER(1);
REQUIRE(!setup3.GetBitDepthReadout());
REQUIRE_THROWS(setup3.BitDepthReadout(32));
}
TEST_CASE("DetectorSetup_ImageDepth") {
auto setup = DetDECTRIS(123,123, "zzz", "a");
REQUIRE_NOTHROW(setup.BitDepthImage(16));
REQUIRE(setup.GetBitDepthImage() == 16);
REQUIRE_THROWS(setup.BitDepthImage(12));
REQUIRE_NOTHROW(setup.BitDepthImage(32));
REQUIRE(setup.GetBitDepthImage() == 32);
REQUIRE_NOTHROW(setup.BitDepthImage(8));
REQUIRE(setup.GetBitDepthImage() == 8);
REQUIRE_THROWS(setup.BitDepthImage(0));
REQUIRE_THROWS(setup.BitDepthImage(15));
REQUIRE_THROWS(setup.BitDepthImage(-1));
auto setup2 = DetJF(1);
REQUIRE(!setup2.GetBitDepthImage());
REQUIRE_THROWS(setup2.BitDepthImage(32));
auto setup3 = DetEIGER(1);
REQUIRE(!setup3.GetBitDepthImage());
REQUIRE_THROWS(setup3.BitDepthImage(32));
}
TEST_CASE("DetectorSetup_ProtoBuf") {
DetectorSetup setup = DetJF(DetectorGeometryModular(4), "JF", {"mx1","mx2","mx3","mx4"});
REQUIRE(setup.GetDescription() == "JF");
REQUIRE(setup.GetDetectorModuleHostname().size() == 4);
REQUIRE(setup.GetDetectorModuleHostname()[3] == "mx4");
REQUIRE(setup.GetPixelSize_mm() == Catch::Approx(0.075));
REQUIRE(setup.GetModulesNum() == 4);
REQUIRE(setup.GetGeometry().GetModulesNum() == 4);
}
TEST_CASE("DetectorSetup_ProtoBuf_FullSpeed") {
DetectorSetup setup = DetJF(DetectorGeometryModular(4), "JF", {"mx1","mx2","mx3","mx4"});
REQUIRE(setup.GetUDPInterfaceCount() == 2);
REQUIRE_NOTHROW(setup.UDPInterfaceCount(1));
REQUIRE_THROWS(setup.UDPInterfaceCount(0));
REQUIRE_THROWS(setup.UDPInterfaceCount(5));
REQUIRE_THROWS(setup.UDPInterfaceCount(-56));
REQUIRE(setup.GetUDPInterfaceCount() == 1);
}
TEST_CASE("DetectorSetup_IPv4Base_2Interfaces") {
DetectorSetup detector = DetJF(DetectorGeometryModular(2));
detector.UDPInterfaceCount(2);
REQUIRE_NOTHROW(detector.BaseIPv4Addr("64.1.124.1"));
REQUIRE(detector.GetSrcIPv4Addr(0) == IPv4AddressFromStr("64.1.124.1"));
REQUIRE(detector.GetSrcIPv4Addr(1) == IPv4AddressFromStr("64.1.124.2"));
REQUIRE(detector.GetSrcIPv4Addr(3) == IPv4AddressFromStr("64.1.124.4"));
REQUIRE_THROWS(detector.GetSrcIPv4Addr(4));
}
TEST_CASE("DetectorSetup_IPv4Base_1Interface") {
DetectorSetup detector = DetJF(DetectorGeometryModular(4));
detector.UDPInterfaceCount(1);
REQUIRE_NOTHROW(detector.BaseIPv4Addr("64.1.124.1"));
REQUIRE(detector.GetSrcIPv4Addr(0) == IPv4AddressFromStr("64.1.124.1"));
REQUIRE(detector.GetSrcIPv4Addr(1) == IPv4AddressFromStr("64.1.124.2"));
REQUIRE(detector.GetSrcIPv4Addr(3) == IPv4AddressFromStr("64.1.124.4"));
REQUIRE_THROWS(detector.GetSrcIPv4Addr(4));
}
TEST_CASE("DetectorSetup_LoadTrimFile") {
DetectorSetup setup(DetectorGeometryModular(4), DetectorType::EIGER, "E", {"mx1","mx2","mx3","mx4","mx5","mx6","mx7","mx8"});
REQUIRE_NOTHROW(setup.SetTrimFiles({"a", "b", "c", "d", "e", "f", "g", "h"}));
REQUIRE(setup.GetTrimFileDirectory().empty());
REQUIRE(setup.GetTrimFileNames().size() == 8);
REQUIRE_NOTHROW(setup.SetTrimFiles({"../../tests"})); // must be directory
REQUIRE(setup.GetTrimFileDirectory() == "../../tests");
REQUIRE(setup.GetTrimFileNames().empty());
REQUIRE_THROWS(setup.SetTrimFiles({"Ridiculous_file_name"}));
}
TEST_CASE("DetectorSetup_LoadGainFile") {
DetectorSetup setup(DetectorGeometryModular(4), DetectorType::JUNGFRAU, "JF", {"mx1","mx2","mx3","mx4"});
REQUIRE_THROWS(setup.LoadGain({}));
REQUIRE(setup.GetGainCalibration().empty());
REQUIRE_THROWS(setup.LoadGain({
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin"
}));
REQUIRE_THROWS(setup.LoadGain({
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin"
}));
REQUIRE_NOTHROW(setup.LoadGain({
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin"
}));
REQUIRE(setup.GetGainCalibration().size() == 4);
}
TEST_CASE("DetectorSetup_LoadGainFile_CopyConstructor") {
DetectorSetup setup(DetectorGeometryModular(4), DetectorType::JUNGFRAU, "JF", {"mx1","mx2","mx3","mx4"});
REQUIRE_NOTHROW(setup.LoadGain({
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin",
"../../tests/test_data/gainMaps_M049.bin"
}));
REQUIRE(setup.GetGainCalibration().size() == 4);
// Copy constructor works
DetectorSetup setup2(setup);
REQUIRE(setup2.GetGainCalibration().size() == 4);
REQUIRE(setup2.GetGainCalibration()[0].GetG0Mean() == setup.GetGainCalibration()[0].GetG0Mean());
REQUIRE(setup2.GetGainCalibration()[3].GetG1Mean() == setup.GetGainCalibration()[3].GetG1Mean());
}
TEST_CASE("DetectorSetup_MaxFrameTime") {
DetectorSetup s1(DetectorGeometryModular(8), DetectorType::JUNGFRAU);
s1.UDPInterfaceCount(1);
REQUIRE(s1.GetMinFrameTime() == MIN_FRAME_TIME_JUNGFRAU_HALF_SPEED);
s1.UDPInterfaceCount(2);
REQUIRE(s1.GetMinFrameTime() == MIN_FRAME_TIME_JUNGFRAU_FULL_SPEED);
DetectorSetup s2(DetectorGeometryModular(8), DetectorType::EIGER);
REQUIRE(s2.GetMinFrameTime() == MIN_FRAME_TIME_EIGER);
}
// A DECTRIS detector starts with no image depth of its own, so the depth comes from what the run is
// actually configured to do. It used to default to 16, and because GetByteDepthImage() gives the
// detector's value absolute priority that silently shadowed everything below it - the image-format
// setting the API offers among them.
TEST_CASE("DetectorSetup_ImageDepthDefaultsToUnset") {
auto setup = DetDECTRIS(123, 123, "zzz", "a");
REQUIRE(!setup.GetBitDepthImage());
REQUIRE(!setup.GetBitDepthReadout());
// Nothing set anywhere: the fallback still lands on the readout depth, as before.
DiffractionExperiment x(setup);
REQUIRE(x.GetByteDepthImage() == 2);
// The API's image-format setting is now reached instead of being discarded.
x.ImportImageFormatSettings(ImageFormatSettings().BitDepthImage(32));
REQUIRE(x.GetByteDepthImage() == 4);
// An unknown readout depth is not an error while the image depth is being decided, and it is
// reported as "not known" rather than invented.
REQUIRE_THROWS(x.GetBitDepthReadout());
REQUIRE(!x.GetBitDepthReadoutIfKnown());
setup.BitDepthReadout(32);
REQUIRE(DiffractionExperiment(setup).GetByteDepthImage() == 4);
// And a depth the detector itself reports still wins over both.
setup.BitDepthImage(8);
DiffractionExperiment y(setup);
y.ImportImageFormatSettings(ImageFormatSettings().BitDepthImage(32));
REQUIRE(y.GetByteDepthImage() == 1);
}