Files
Jungfraujoch/tests/CalibrationTest.cpp
leonarski_fandClaude Opus 5 a72ba82f48
Build Packages / Unit tests (push) Successful in 1h23m35s
Build Packages / build:viewer-tgz:cpu (push) Successful in 11m23s
Build Packages / build:viewer-tgz:cuda (push) Successful in 14m56s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 20m33s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 18m7s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 19m59s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 15m33s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 19m50s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 19m7s
Build Packages / build:rpm (rocky8) (push) Successful in 20m39s
Build Packages / build:rpm (rocky9) (push) Successful in 18m55s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 20m18s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 15m27s
Build Packages / DIALS test (push) Successful in 13m49s
Build Packages / XDS test (durin plugin) (push) Successful in 8m43s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 8m52s
Build Packages / XDS test (neggia plugin) (push) Successful in 6m59s
Build Packages / Generate python client (push) Successful in 12s
Build Packages / Build documentation (push) Successful in 42s
Build Packages / Create release (push) Skipped
Build Packages / build:windows:nocuda (push) Successful in 20m47s
Build Packages / build:windows:cuda (push) Successful in 23m0s
Powder calibration: write rot2/rot3 in pyFAI's frame, not ours
The .poni file carried rot2 and rot3 with our sign, which is not pyFAI's. pyFAI
has the slow axis increasing bottom to top; the MX convention runs top to bottom,
so the two frames differ by a reflection in y. Conjugating a rotation by a
reflection gives R(n, theta) -> R(Mn, -theta), so for rot2 (about x) and rot3
(about the beam) the sense reverses, while for rot1 the axis IS y and the axis and
the sense reverse together and cancel. Negate the first two, leave rot1 alone.

Poni1/Poni2 are unaffected: they are distances from pixel (0, 0) along each axis,
which the direction the axis runs in does not change.

Caught by integrating a LaB6 image in pyFAI with the file we had just written.
Unflipped, the rings come out BROADER than they do with no tilt at all - peak
height 42 against 30, mean ring-position error 0.0045 1/A against 0.0027 - which
is the signature of a tilt applied the wrong way. Flipped, they sharpen to 132 and
0.0005, and flipping rot1 as well makes it far worse (peak 5), so the asymmetry is
real and not a fitting artefact.

The unit test pinned the old signs, so it passed throughout. It now pins the
verified ones and says why, since the stored values and the written ones
disagreeing looks like a bug unless the reason is written down.

Only the exported file was wrong. Nothing internal changes: the fitted geometry
and everything downstream of it in Jungfraujoch were always self-consistent.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 12:04:41 +02:00

88 lines
4.2 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 <cstdio>
#include <fstream>
#include <map>
#include <sstream>
#include "../common/Definitions.h"
#include "../common/JFJochMath.h"
#include "../image_analysis/geom_refinement/Calibrants.h"
#include "../rugnux/RugnuxCalibration.h"
TEST_CASE("Calibrants_LookupIsCaseInsensitive", "[DetGeomCalib]") {
CHECK(CalibrantRings("LaB6") == CalibrantRings("lab6"));
CHECK(CalibrantRings("AgBh") == CalibrantRings("agbh"));
CHECK(CalibrantRings("nonsense").empty());
}
// The innermost ring of a cubic standard is its (100), so the first q is 2*pi/a. This is what fixes
// the distance in GuessInitialGeometry, so a wrong table would put every calibration off by that scale.
TEST_CASE("Calibrants_CubicStandardsHaveTheirLatticeConstant", "[DetGeomCalib]") {
const std::map<std::string, double> a_A = {{"lab6", LAB6_CELL_A}, {"ceo2", 5.4115}, {"si", 5.43102}};
for (const auto &[name, a] : a_A) {
const auto q = CalibrantRings(name);
REQUIRE(!q.empty());
CHECK(q.front() == Catch::Approx(2.0 * PI / a).epsilon(1e-5));
}
}
// Ice is the reason the calibrant abstraction is a ring list and not a UnitCell: its entries are
// measured ring positions, and enumerating hkl from the hexagonal cell would add rings that are
// systematically absent in P6_3/mmc.
TEST_CASE("Calibrants_IceIsTheMeasuredRingList", "[DetGeomCalib]") {
const auto q = CalibrantRings("ice");
REQUIRE(q.size() == ICE_RING_RES_A.size());
CHECK(std::is_sorted(q.begin(), q.end()));
CHECK(q.front() == Catch::Approx(2.0 * PI / ICE_RING_RES_A[0]).epsilon(1e-5)); // 3.895 A, the widest
}
// pyFAI's Poni1 is the SLOW axis (rows, our y) and Poni2 the FAST axis (columns, our x), both in
// metres. Transposing them produces a file that is silently wrong, so pin the mapping with a geometry
// whose two axes differ.
TEST_CASE("Calibration_PoniFileAxisConvention", "[DetGeomCalib]") {
DiffractionExperiment x(DetJF4M());
x.BeamX_pxl(1000.0f).BeamY_pxl(1275.0f).DetectorDistance_mm(150.0f);
DiffractionGeometry geom = x.GetDiffractionGeometry();
geom.PoniRot1_rad(0.01f).PoniRot2_rad(-0.02f);
const std::string path = "poni_test.poni";
WritePoniFile(path, x, geom);
std::map<std::string, std::string> keys;
std::ifstream f(path);
std::string line;
while (std::getline(f, line)) {
const auto colon = line.find(':');
if (line.empty() || line[0] == '#' || colon == std::string::npos)
continue;
keys[line.substr(0, colon)] = line.substr(colon + 2);
}
f.close();
std::remove(path.c_str());
const double pixel_m = geom.GetPixelSize_mm() * 1e-3;
CHECK(keys["poni_version"] == "2");
CHECK(std::stod(keys["Poni1"]) == Catch::Approx(1275.0 * pixel_m)); // slow axis = y
CHECK(std::stod(keys["Poni2"]) == Catch::Approx(1000.0 * pixel_m)); // fast axis = x
CHECK(std::stod(keys["Distance"]) == Catch::Approx(0.150));
// rot2 and rot3 are NEGATED into pyFAI's frame and rot1 is not: pyFAI's slow axis runs bottom to
// top where the MX convention runs top to bottom, so the frames differ by a reflection in y. That
// reverses the sense of a rotation about x or about the beam, while for a rotation about y itself
// the axis reverses too and the two cancel. Cross-checked against pyFAI on a real LaB6 image - the
// unflipped file integrates rings broader than a zero-tilt one. Do not "fix" these signs to match
// the stored values without repeating that check.
CHECK(std::stod(keys["Rot1"]) == Catch::Approx(0.01));
CHECK(std::stod(keys["Rot2"]) == Catch::Approx(0.02));
CHECK(std::stod(keys["Rot3"]) == Catch::Approx(0.0));
CHECK(std::stod(keys["Wavelength"]) == Catch::Approx(geom.GetWavelength_A() * 1e-10));
// max_shape is [rows, cols] - the same slow-then-fast order as Poni1/Poni2.
const std::string shape = "[" + std::to_string(x.GetYPixelsNumConv()) + ", "
+ std::to_string(x.GetXPixelsNumConv()) + "]";
CHECK(keys["Detector_config"].find(shape) != std::string::npos);
}