The ring calibration already here (AssignSpotsToRings + RingOptimizer, driven from the viewer's powder panel) is given a SPOT LIST from a single image. A powder ring is not a set of spots - it is a smooth arc - so a spot finder samples it wherever its threshold happens to bite, and one image carries only the counts that image collected. An azimuthally-binned profile summed over a run measures the same ring directly, at every azimuth, with the whole run behind it. RingsFromAzimuthalProfile turns such a profile into the (x, y, q_expected) triples RingOptimizer already consumes, so nothing downstream changes: for each calibrant ring and each azimuthal sector it fits the radial peak against a locally interpolated background, and maps the measured (q, phi) back through the current geometry to the pixel it came from. What this is for is the BEAM CENTRE. A powder ring is a conic centred on the beam, so a wrong centre makes its apparent radius oscillate once per turn and a detector tilt twice - and neither depends on the calibrant's d-spacings or on the detector distance. That matters, because the beam centre is otherwise the weakest parameter we have: fitted from Bragg spots it is gauge-coupled to the crystal orientation, which is why PostRefine has to restrain it toward the header and commit only a sub-1 % move, and why XtalOptimizer carries a soft prior noting the beam is "only LaB6-monitored to ~a few px". A ring does not know about the crystal. Two things the peak fit is careful about, both of which would otherwise show up as a spurious cos(phi) - i.e. as a beam-centre shift: - the sector's CENTRE is used, not its lower edge. GetBin() floors phi into the sector, so a bin stands for [j, j+1), and taking its edge rotates every ring point by half a sector. - a peak has to stand clear of the scatter of the background either side of it, or a sector with no ring in it contributes its largest noise excursion as though it were a measurement. Refuses a single-azimuthal-bin profile outright: that is a plain radial profile, the ring has been averaged over every direction, and there is nothing left to say where its centre is. Tested by round trip against the forward model, as the existing calibration tests are: synthesise the profile the azimuthal integration would build with the rings where a shifted geometry puts them but every pixel binned with the unshifted one, then extract and fit. A 6.0 / -4.0 px beam offset is recovered as 6.13 / -4.03 from 192 ring points. Only the beam centre is exercised here; the tilt path is covered by the existing DetGeomCalibTest round trips. This is the extraction only - nothing calls it yet, and the run-scoped accumulator it is meant to read (JFJochReceiverPlots::az_int_profile, already summed over a run and written to /entry/azint/dataset) is still integrated with one azimuthal bin by default. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
124 lines
6.0 KiB
C++
124 lines
6.0 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 "../image_analysis/geom_refinement/RingsFromProfile.h"
|
|
#include "../image_analysis/geom_refinement/AssignSpotsToRings.h"
|
|
#include "../common/Definitions.h"
|
|
#include "../common/JFJochMath.h"
|
|
|
|
namespace {
|
|
|
|
constexpr UnitCell LAB6{LAB6_CELL_A, LAB6_CELL_A, LAB6_CELL_A, 90.0f, 90.0f, 90.0f};
|
|
|
|
// A (q x azimuth) powder profile as the azimuthal integration would build it: the rings sit where
|
|
// geom_true puts them, but every pixel is binned with geom_assumed - which is the whole point, since a
|
|
// wrong assumed geometry is what makes a ring's apparent q wander with azimuth.
|
|
std::vector<float> SynthesiseProfile(const AzimuthalIntegrationMapping &mapping,
|
|
const DiffractionGeometry &geom_assumed,
|
|
const DiffractionGeometry &geom_true) {
|
|
const auto &settings = mapping.Settings();
|
|
const int32_t q_bins = mapping.GetQBinCount();
|
|
const int32_t azim_bins = mapping.GetAzimuthalBinCount();
|
|
std::vector<float> profile(static_cast<size_t>(q_bins) * azim_bins, 100.0f); // flat background
|
|
|
|
for (const float q_ring : CalculateXtalRings(LAB6)) {
|
|
const float d = static_cast<float>(2.0 * PI) / q_ring;
|
|
if (d <= geom_true.GetWavelength_A() / 2.0f)
|
|
continue;
|
|
for (int t = 0; t < 3600; ++t) {
|
|
const float phi_true = static_cast<float>(2.0 * PI * t / 3600.0);
|
|
const auto [px, py] = geom_true.ResPhiToPxl(d, phi_true);
|
|
if (!std::isfinite(px) || !std::isfinite(py))
|
|
continue;
|
|
const float q_obs = geom_assumed.PxlToQ(px, py);
|
|
float phi_deg = geom_assumed.Phi_rad(px, py) * 180.0f / static_cast<float>(PI);
|
|
if (phi_deg < 0.0f)
|
|
phi_deg += 360.0f;
|
|
const uint16_t bin = settings.GetBin(q_obs, phi_deg);
|
|
if (bin == UINT16_MAX)
|
|
continue;
|
|
// Lay a narrow peak over the neighbouring q bins of this azimuthal row.
|
|
const int q_bin = bin % q_bins, phi_bin = bin / q_bins;
|
|
for (int k = -3; k <= 3; ++k) {
|
|
const int b = q_bin + k;
|
|
if (b < 0 || b >= q_bins)
|
|
continue;
|
|
profile[static_cast<size_t>(phi_bin) * q_bins + b] +=
|
|
2000.0f * std::exp(-0.5f * static_cast<float>(k * k) / (1.2f * 1.2f));
|
|
}
|
|
}
|
|
}
|
|
return profile;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// The measurement this is for: a powder ring is a conic centred on the beam, so a wrong beam centre
|
|
// makes its apparent radius oscillate once per turn. Recovering the centre from that needs neither the
|
|
// calibrant's lattice constant nor the detector distance - only that the ring be round.
|
|
TEST_CASE("RingsFromProfile_RecoversBeamCenter", "[DetGeomCalib]") {
|
|
DiffractionExperiment x(DetJF4M());
|
|
x.QSpacingForAzimInt_recipA(0.004).QRangeForAzimInt_recipA(0.5, 4.0);
|
|
auto azint = x.GetAzimuthalIntegrationSettings();
|
|
azint.AzimuthalBinCount(32);
|
|
x.ImportAzimuthalIntegrationSettings(azint);
|
|
|
|
PixelMask pixel_mask(x);
|
|
AzimuthalIntegrationMapping mapping(x, pixel_mask);
|
|
|
|
const DiffractionGeometry geom_assumed = x.GetDiffractionGeometry();
|
|
DiffractionGeometry geom_true = geom_assumed;
|
|
geom_true.BeamX_pxl(geom_assumed.GetBeamX_pxl() + 6.0f)
|
|
.BeamY_pxl(geom_assumed.GetBeamY_pxl() - 4.0f);
|
|
|
|
const auto profile = SynthesiseProfile(mapping, geom_assumed, geom_true);
|
|
const auto rings = RingsFromAzimuthalProfile(profile, mapping, geom_assumed, LAB6);
|
|
|
|
// Several rings, sampled all the way round: without azimuthal coverage there is no centre to find.
|
|
REQUIRE(rings.size() > 64);
|
|
|
|
RingOptimizer optimizer(geom_assumed);
|
|
const auto fitted = optimizer.Run(rings);
|
|
|
|
CHECK(fitted.GetBeamX_pxl() == Catch::Approx(geom_true.GetBeamX_pxl()).margin(0.5));
|
|
CHECK(fitted.GetBeamY_pxl() == Catch::Approx(geom_true.GetBeamY_pxl()).margin(0.5));
|
|
// The starting point was wrong by 6 and 4 pixels, so a fit that did nothing would fail the above -
|
|
// but check explicitly that it moved toward the truth rather than merely landing near it.
|
|
CHECK(std::abs(fitted.GetBeamX_pxl() - geom_true.GetBeamX_pxl())
|
|
< std::abs(geom_assumed.GetBeamX_pxl() - geom_true.GetBeamX_pxl()));
|
|
}
|
|
|
|
// One azimuthal bin is a plain radial profile: the ring has been averaged over every direction, so
|
|
// nothing is left to say where its centre is. Refuse rather than return points that cannot constrain it.
|
|
TEST_CASE("RingsFromProfile_NeedsAzimuthalBins", "[DetGeomCalib]") {
|
|
DiffractionExperiment x(DetJF4M());
|
|
x.QSpacingForAzimInt_recipA(0.004).QRangeForAzimInt_recipA(0.5, 4.0);
|
|
|
|
PixelMask pixel_mask(x);
|
|
AzimuthalIntegrationMapping mapping(x, pixel_mask);
|
|
REQUIRE(mapping.GetAzimuthalBinCount() == 1);
|
|
|
|
const std::vector<float> profile(static_cast<size_t>(mapping.GetQBinCount()), 1000.0f);
|
|
CHECK(RingsFromAzimuthalProfile(profile, mapping, x.GetDiffractionGeometry(), LAB6).empty());
|
|
}
|
|
|
|
// A profile with no rings in it must yield no ring points: the peak has to stand clear of the scatter
|
|
// of the background either side of it, or every azimuthal sector would contribute its largest noise
|
|
// excursion as though it were a measurement.
|
|
TEST_CASE("RingsFromProfile_FlatProfileGivesNothing", "[DetGeomCalib]") {
|
|
DiffractionExperiment x(DetJF4M());
|
|
x.QSpacingForAzimInt_recipA(0.004).QRangeForAzimInt_recipA(0.5, 4.0);
|
|
auto azint = x.GetAzimuthalIntegrationSettings();
|
|
azint.AzimuthalBinCount(32);
|
|
x.ImportAzimuthalIntegrationSettings(azint);
|
|
|
|
PixelMask pixel_mask(x);
|
|
AzimuthalIntegrationMapping mapping(x, pixel_mask);
|
|
|
|
const std::vector<float> profile(
|
|
static_cast<size_t>(mapping.GetQBinCount()) * mapping.GetAzimuthalBinCount(), 100.0f);
|
|
CHECK(RingsFromAzimuthalProfile(profile, mapping, x.GetDiffractionGeometry(), LAB6).empty());
|
|
}
|