Powder calibration: write Poni1/Poni2 in pyFAI's frame, not ours

The same frame mismatch as the rot2/rot3 fix, in the other two fields. Our pixel
coordinates are pixel-centred - 948.0 is the CENTRE of pixel 948 - while pyFAI
measures from the edge of the sensor and puts the centre of pixel i at (i + 0.5) *
pixel size. Poni1/Poni2 went out as beam * pixel size, so anything reading the
file placed the pattern half a pixel (37.5 um at 75 um pixels) off ours. The
previous commit's "Poni1/Poni2 need no such change" was right about the axis
directions and wrong about the origin.

The proof was already in the tree. The pyFAI reference values in
DiffractionGeometryTest were computed for a .poni with Poni2: 0.150 and a 75 um
pixel, which the tests translate to beam_x = 2000 - but pyFAI's numbers are
reproduced only at 1999.5. At 2000 every one of them is out by 2.6e-3 nm^-1, which
the 1e-2 tolerance hid. The tests now use the beam centre those headers actually
mean, and agree with pyFAI to 1e-6 - float precision - across untilted q, azimuth,
rot1, rot1+rot2, rot3, rot1+rot2+rot3 and the solid-angle correction. Tolerances
drop to 1e-4 (1e-5 for solid angle): ~100x the observed float noise, and 26x
tighter than the half pixel they were blind to.

The viewer's calibration window printed "PONI x = ... mm" from the un-offset value
beside the path of the file it disagreed with; it now matches the file.

Also moves the viewer's beam-centre cross half a pixel down and right, where the
spot, prediction, top-pixel and saturation markers already are. Our coordinates
are pixel-centred and the Qt scene's are pixel-cornered, so the map between them
is +0.5, and DrawBeamCenter was the one overlay missing it.

The convention itself is now written down in docs/DETECTOR_GEOMETRY.md, with the
conversions to XDS ORGX/ORGY and to the edge-of-sensor programs, this being the
second bug to come out of it.

Only exported and displayed values change; the fitted geometry, spot positions and
integration were always self-consistent. A .poni written by an earlier build is
half a pixel off.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-08 03:55:42 +02:00
co-authored by Claude Opus 5
parent a72ba82f48
commit a8d289e7cf
8 changed files with 85 additions and 36 deletions
+31 -26
View File
@@ -264,17 +264,22 @@ Wavelength: 1e-10
*/
// PyFAI uses nm^-1 for Q?
// The beam centre is Poni/pixel_size - 0.5 in every PONI test here: our coordinates are pixel-centred
// (0.0 is the centre of the first pixel) while pyFAI measures from the edge of the sensor and puts the
// centre of pixel i at (i + 0.5) * pixel size - see docs/DETECTOR_GEOMETRY.md. So 0.150 m / 75 um gives
// 1999.5, not 2000. With the half pixel the reference values below are reproduced to float precision;
// without it every one of them is out by 2.6e-3 nm^-1, which the old 1e-2 tolerance hid.
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV);
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV);
DiffractionGeometry geom = x.GetDiffractionGeometry();
float diff_800_400 = fabs(geom.PxlToQ( 800,400)*10.0 - 6.295358803860941);
float diff_400_800 = fabs(geom.PxlToQ( 400,800)*10.0 - 7.554628215027982);
float diff_1300_2000 = fabs(geom.PxlToQ( 1300,2000)*10.0 - 5.73479724964891);
REQUIRE(diff_800_400 < 0.01);
REQUIRE(diff_400_800 < 0.01);
REQUIRE(diff_1300_2000 < 0.01);
REQUIRE(diff_800_400 < 1e-4);
REQUIRE(diff_400_800 < 1e-4);
REQUIRE(diff_1300_2000 < 1e-4);
}
TEST_CASE("DiffractionGeometry_PONI_phi","") {
@@ -293,7 +298,7 @@ Wavelength: 1e-10
// PyFAI uses nm^-1 for Q?
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV);
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV);
DiffractionGeometry geom = x.GetDiffractionGeometry();
float phi_2000_0 = fabs(geom.Phi_rad(2000,0) - 2 * M_PI + 1.5702959937284997);
@@ -301,10 +306,10 @@ Wavelength: 1e-10
float phi_0_1000 = fabs(geom.Phi_rad(0,1000) - 3.1413425992666903);
float phi_2000_1300 = fabs(geom.Phi_rad(1300,2000) - 2.1809518509415025);
CHECK(phi_2000_0 < 0.001);
CHECK(phi_2000_2000 < 0.001);
CHECK(phi_0_1000 < 0.001);
CHECK(phi_2000_1300 < 0.001);
CHECK(phi_2000_0 < 1e-4);
CHECK(phi_2000_2000 < 1e-4);
CHECK(phi_0_1000 < 1e-4);
CHECK(phi_2000_1300 < 1e-4);
}
@@ -324,7 +329,7 @@ Wavelength: 1e-10
// PyFAI uses nm^-1 for Q?
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV)
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV)
.PoniRot3_rad(0.5);
DiffractionGeometry geom = x.GetDiffractionGeometry();
REQUIRE(geom.GetPoniRot3_rad() == Catch::Approx(0.5f));
@@ -332,8 +337,8 @@ Wavelength: 1e-10
float phi_800_400 = fabs(geom.Phi_rad(800,400) - 3.105073518019684);
float phi_2000_1300 = fabs(geom.Phi_rad(1300,2000) - 1.6809518509415027);
CHECK(phi_800_400 < 0.001);
CHECK(phi_2000_1300 < 0.001);
CHECK(phi_800_400 < 1e-4);
CHECK(phi_2000_1300 < 1e-4);
}
@@ -353,7 +358,7 @@ Wavelength: 1e-10
// PyFAI uses nm^-1 for Q?
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV)
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV)
.PoniRot1_rad(0.2).PoniRot2_rad(-0.1).PoniRot3_rad(0.5);
DiffractionGeometry geom = x.GetDiffractionGeometry();
@@ -364,8 +369,8 @@ Wavelength: 1e-10
float phi_800_400 = fabs(geom.Phi_rad(800,400) - 2 * M_PI + 1.4175001633470816);
float phi_2000_1300 = fabs(geom.Phi_rad(1300,2000) - 2 * M_PI + 0.6630282166663707);
CHECK(phi_800_400 < 0.001);
CHECK(phi_2000_1300 < 0.001);
CHECK(phi_800_400 < 1e-4);
CHECK(phi_2000_1300 < 1e-4);
}
TEST_CASE("DiffractionGeometry_PONI_rot1","") {
@@ -384,7 +389,7 @@ Wavelength: 1e-10
// PyFAI uses nm^-1 for Q?
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV);
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV);
DiffractionGeometry geom = x.GetDiffractionGeometry();
geom.PoniRot1_rad(0.2);
@@ -392,9 +397,9 @@ Wavelength: 1e-10
float diff_400_800 = fabs(geom.PxlToQ( 400,800)*10.0 - 5.148411999405654);
float diff_1300_2000 = fabs(geom.PxlToQ( 1300,2000)*10.0 - 10.37635963741911);
CHECK(diff_800_400 < 0.01);
CHECK(diff_400_800 < 0.01);
CHECK(diff_1300_2000 < 0.01);
CHECK(diff_800_400 < 1e-4);
CHECK(diff_400_800 < 1e-4);
CHECK(diff_1300_2000 < 1e-4);
}
@@ -414,7 +419,7 @@ Wavelength: 1e-10
// PyFAI uses nm^-1 for Q?
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(1000).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV);
x.DetectorDistance_mm(1000).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV);
DiffractionGeometry geom = x.GetDiffractionGeometry();
geom.PoniRot1_rad(0.2).PoniRot2_rad(-0.1);
@@ -422,9 +427,9 @@ Wavelength: 1e-10
float diff_400_800 = fabs(geom.PxlToQ( 400,800)*10.0 - 8.805012278158177);
float diff_1300_2000 = fabs(geom.PxlToQ( 1300,2000)*10.0 - 9.363455481328781);
CHECK(diff_800_400 < 0.01);
CHECK(diff_400_800 < 0.01);
CHECK(diff_1300_2000 < 0.01);
CHECK(diff_800_400 < 1e-4);
CHECK(diff_400_800 < 1e-4);
CHECK(diff_1300_2000 < 1e-4);
}
TEST_CASE("DiffractionGeometry_PyFAI_Solid_angle","") {
@@ -445,14 +450,14 @@ Wavelength: 1e-10
// so it is independent of the poni rotation (tilt). CalcAzIntSolidAngleCorr matches this;
// the invariance is checked in DiffractionGeometry_SolidAngleCorrection_TiltInvariant.
DiffractionExperiment x(DetJF4M());
x.DetectorDistance_mm(200).BeamX_pxl(2000).BeamY_pxl(1000).IncidentEnergy_keV(WVL_1A_IN_KEV);
x.DetectorDistance_mm(200).BeamX_pxl(1999.5).BeamY_pxl(999.5).IncidentEnergy_keV(WVL_1A_IN_KEV);
DiffractionGeometry geom = x.GetDiffractionGeometry();
float diff_100_100 = fabs(geom.CalcAzIntSolidAngleCorr( 100,100) - 0.4844596502755233);
CHECK(diff_100_100 < 0.0002);
CHECK(diff_100_100 < 1e-5);
float diff_400_800 = fabs(geom.CalcAzIntSolidAngleCorr( 400,800)- 0.6267921080721112);
CHECK(diff_400_800 < 0.0002);
CHECK(diff_400_800 < 1e-5);
}
TEST_CASE("ResPhiToPxl") {