Files
Jungfraujoch/image_analysis/geom_refinement/RingsFromProfile.cpp
T
leonarski_fandClaude Opus 5 5b5bed4f66 Powder calibration: read the rings off an azimuthal profile, not off a spot list
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>
2026-08-06 21:30:24 +02:00

126 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 <algorithm>
#include <cmath>
#include "RingsFromProfile.h"
#include "AssignSpotsToRings.h" // CalculateXtalRings
#include "../../common/JFJochMath.h"
namespace {
// Peak position of one ring in one azimuthal sector, in q, or NaN if there is no peak worth using.
//
// The window is narrow and centred on where the ring is expected, so the background under it is close
// to a straight line: take it from the two bins at each end and interpolate. The position itself is the
// intensity-weighted centroid of everything above half the peak height, which is insensitive to the
// exact half-maximum crossing and needs no line-shape assumption - a powder ring is not Gaussian, it is
// the instrumental profile convolved with whatever strain and size broadening the standard has.
float SectorPeakQ(const std::vector<float> &profile, int32_t q_bins, int phi_bin,
int lo_bin, int hi_bin, float low_q, float q_spacing, float min_peak_over_noise) {
const size_t row = static_cast<size_t>(phi_bin) * static_cast<size_t>(q_bins);
const auto value = [&](int i) { return profile[row + static_cast<size_t>(i)]; };
const auto q_of = [&](int i) { return low_q + (static_cast<float>(i) + 0.5f) * q_spacing; };
const float bkg_lo = 0.5f * (value(lo_bin) + value(lo_bin + 1));
const float bkg_hi = 0.5f * (value(hi_bin) + value(hi_bin - 1));
const auto bkg_at = [&](int i) {
const float t = static_cast<float>(i - lo_bin) / static_cast<float>(hi_bin - lo_bin);
return bkg_lo + t * (bkg_hi - bkg_lo);
};
int peak = -1;
float peak_height = 0.0f;
for (int i = lo_bin + 2; i <= hi_bin - 2; ++i) {
const float h = value(i) - bkg_at(i);
if (h > peak_height) { peak_height = h; peak = i; }
}
if (peak < 0)
return NAN;
// Scatter of the background shoulders, as the noise this peak has to stand clear of. A sector with
// no ring in it has a "peak" that is just the largest background fluctuation, and this is what
// rejects it - the alternative, an absolute intensity cut, would need a value per detector and beam.
float s = 0.0f;
int n = 0;
for (int i : {lo_bin, lo_bin + 1, hi_bin - 1, hi_bin}) {
const float r = value(i) - bkg_at(i);
s += r * r;
++n;
}
const float noise = std::sqrt(s / static_cast<float>(n));
if (!(peak_height > min_peak_over_noise * noise))
return NAN;
const float half = 0.5f * peak_height;
double sum_wq = 0.0, sum_w = 0.0;
for (int i = peak; i >= lo_bin && value(i) - bkg_at(i) >= half; --i) {
const double w = value(i) - bkg_at(i);
sum_wq += w * q_of(i);
sum_w += w;
}
for (int i = peak + 1; i <= hi_bin && value(i) - bkg_at(i) >= half; ++i) {
const double w = value(i) - bkg_at(i);
sum_wq += w * q_of(i);
sum_w += w;
}
if (!(sum_w > 0.0))
return NAN;
return static_cast<float>(sum_wq / sum_w);
}
} // namespace
std::vector<RingOptimizerInput> RingsFromAzimuthalProfile(const std::vector<float> &profile,
const AzimuthalIntegrationMapping &mapping,
const DiffractionGeometry &geom,
const UnitCell &calibrant,
float q_window_recipA,
float min_peak_over_noise) {
std::vector<RingOptimizerInput> out;
const int32_t q_bins = mapping.GetQBinCount();
const int32_t azim_bins = mapping.GetAzimuthalBinCount();
// One azimuthal bin is a plain radial profile: the ring is averaged over every direction at once, so
// nothing remains to say where its centre is. This needs the run to have been integrated with
// azimuthal bins (jfjoch_broker azim_int_settings.azimuthal_bins, rugnux --azim-phi-bins).
if (azim_bins < 4 || q_bins < 8
|| profile.size() != static_cast<size_t>(q_bins) * static_cast<size_t>(azim_bins))
return out;
const auto &settings = mapping.Settings();
const float low_q = settings.GetLowQ_recipA();
const float q_spacing = settings.GetQSpacing_recipA();
const float high_q = low_q + static_cast<float>(q_bins) * q_spacing;
const int window_bins = std::max(3, static_cast<int>(std::lround(q_window_recipA / q_spacing)));
for (const float q_ring : CalculateXtalRings(calibrant)) {
if (!(q_ring - q_window_recipA > low_q) || !(q_ring + q_window_recipA < high_q))
continue;
const int centre_bin = static_cast<int>((q_ring - low_q) / q_spacing);
const int lo_bin = std::max(0, centre_bin - window_bins);
const int hi_bin = std::min(q_bins - 1, centre_bin + window_bins);
if (hi_bin - lo_bin < 6)
continue;
for (int phi_bin = 0; phi_bin < azim_bins; ++phi_bin) {
const float q_obs = SectorPeakQ(profile, q_bins, phi_bin, lo_bin, hi_bin,
low_q, q_spacing, min_peak_over_noise);
if (!std::isfinite(q_obs))
continue;
// The sector's CENTRE, not its lower edge: GetBin() floors phi into the sector, so a bin
// stands for [j, j+1) and taking its edge would rotate every ring point by half a sector -
// which is exactly the cos(phi) signal the beam centre is read from.
const float phi_rad = static_cast<float>((static_cast<double>(phi_bin) + 0.5)
* 2.0 * PI / static_cast<double>(azim_bins));
const auto [x, y] = geom.ResPhiToPxl(static_cast<float>(2.0 * PI) / q_obs, phi_rad);
if (!std::isfinite(x) || !std::isfinite(y))
continue;
out.push_back({x, y, q_ring});
}
}
return out;
}