calibration: take the detector distance from the rings, not from the header

A powder calibration is run because nobody is sure the header is right, and the
header's distance was the one number the fit could not survive being wrong
about. The ring search is local - each ring is looked for inside a window a few
pixels of radius wide - so a distance more than a percent or two out puts every
ring outside its own window, and the fit then converges on whatever background
fluctuation each window contains. It does not fail: a 110 mm exposure told the
detector was at 150 mm reported 149.8 mm, with 146 ring points and exit 0. Only
its residual said anything, 5.3 px against 0.4 px, and nothing read it.

Measure the distance from the rings instead. The peaks of the azimuthally
averaged profile give ring RADII, and a radius does not depend on the assumed
distance at all - bin i holds the pixels at one particular radius whatever q
that radius was called - so the radii are a property of the image. Against the
calibrant's d-spacings, r = D tan(2 asin(lambda/2d)) then has one unknown. It is
scanned rather than solved because the pairing of observed rings to d-spacings
is unknown too, and the winning basin is solved in closed form. Nothing here
reads the header distance except to bin the profile; it needs only the
wavelength, the pixel size and the detector's extent.

A powder pattern has genuine distance aliases, so one answer is not enough. A
cubic primitive standard puts its rings at radii proportional to sqrt(N), and
scaling the distance by sqrt(2) maps ring N onto ring 2N - most of the comb
still lands on peaks. Measured: the 110 mm exposure with a 115 mm header scored
its best at 156.5 mm, which is 110*sqrt(2). No adjustment of the score removes an
alias the lattice really has, so the scan hands back the few best distances and
each is fitted, the header among them as one hypothesis of several. The residual
then separates them - 0.4 px against 5.2 px on that case - subject to an attempt
explaining a comparable share of the pattern first, because a start so wrong
that one ring point survives leaves a residual of exactly zero.

Each attempt re-extracts at the geometry it converged to and fits again. The
seed is measured from blended peaks and is good to about a per cent, close
enough to converge from but far enough to sit every search window a few pixels
off its ring, and an off-centre window takes its background off the ring's own
flank. Nothing is re-read from disk, so the loop is free.

Measured on the LaB6 distance series. A 110 mm dataset now recovers 110.03-110.17
mm from any header between 25 and 1200 mm, against +-2 mm before. All five
datasets recover their own distance from a fixed wrong 250 mm header. With
correct headers, four of the five are bit-identical to before and the 500 mm one
moves by a single ring point - the two-ring fit whose tilt is 0.1 sigma anyway.
Run time is unchanged at 0.62 s.

The residual is larger on a run whose header was wrong (1.1 px against 0.4 px on
the 110 mm case), because the profile was still binned at the wrong distance and
its radial sampling is correspondingly coarse. The geometry is right; only the
scatter about it is inflated. Re-running with the recovered distance recovers
the residual too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NfuDvf5ipV3Hi8TiCUKD27
This commit is contained in:
2026-08-31 16:48:32 +02:00
co-authored by Claude Opus 5
parent d275bbcae7
commit a5f416fcdc
9 changed files with 629 additions and 10 deletions
@@ -60,13 +60,113 @@ CalibrationResult CalibrateFromProfile(const std::vector<float> &profile,
const DiffractionGeometry &geom,
const std::vector<float> &calibrant_ring_q,
bool refine_tilt) {
const auto points = RingsFromAzimuthalProfile(profile, mapping, geom, calibrant_ring_q);
if (points.empty())
// Where to start. The ring search below is local - each ring is looked for inside a window a few
// pixels of radius wide - so a header distance more than a percent or two out puts every ring
// outside its own window, and what the fit then converges on is noise. Ask the rings what the
// distance is rather than believing the header (see PowderAutoSeed.h), and because a powder pattern
// has genuine distance aliases, ask for several answers and fit them all.
const auto observed = RingRadiiFromProfile(profile, mapping, geom);
const auto [radius_min, radius_max] = ProfileRadiusRange_pxl(mapping, geom);
const auto candidates = CandidateDistancesFromPowderRings(observed, calibrant_ring_q, geom,
radius_min, radius_max);
// Where each ring sits in THIS profile, for a detector at `distance`. The profile was binned at the
// header's distance and cannot be re-binned without re-reading every image, so a corrected distance
// does not move the rings within it, only where they have to be looked for.
const auto search_list_for = [&](float distance) {
std::vector<float> out;
out.reserve(calibrant_ring_q.size());
for (const float q : calibrant_ring_q)
out.push_back(ProfileQForRing(q, distance, geom.GetDetectorDistance_mm(),
geom.GetWavelength_A(), geom.GetPixelSize_mm()));
return out;
};
// One starting distance, fitted to convergence. A seed only has to land in the fit's basin, not on
// the answer: it is measured from blended peaks in the azimuthally-averaged profile and is good to
// about a per cent, which is close enough to converge from but far enough to sit every search window
// a few pixels off its ring - and an off-centre window takes its background off the ring's own
// flank, which costs both points and residual. So re-extract at the geometry the fit converged to
// and fit again. Nothing is re-read from disk, so the whole loop is free.
struct Attempt {
DiffractionGeometry geometry;
std::vector<RingOptimizerInput> points;
RingFitUncertainty uncertainty;
double rms_radial_pxl = 0.0;
};
const auto fit_from = [&](float distance, bool seeded) -> std::optional<Attempt> {
DiffractionGeometry current = geom;
current.DetectorDistance_mm(distance);
Attempt attempt;
constexpr int MAX_PASSES = 3;
for (int pass = 0; pass < MAX_PASSES; ++pass) {
const std::vector<float> search =
(pass == 0 && !seeded) ? std::vector<float>{}
: search_list_for(current.GetDetectorDistance_mm());
auto pass_points = RingsFromAzimuthalProfile(profile, mapping, geom, calibrant_ring_q,
0.06f, 3.0f, search);
if (pass_points.empty())
break;
RingFitUncertainty pass_unc;
const auto pass_fitted = RingOptimizer(current, refine_tilt).Run(pass_points, &pass_unc);
const float moved = std::abs(pass_fitted.GetDetectorDistance_mm()
- current.GetDetectorDistance_mm());
attempt.points = std::move(pass_points);
attempt.uncertainty = pass_unc;
attempt.geometry = pass_fitted;
current = pass_fitted;
// A tenth of a micron of distance moves the outermost ring by far less than a thousandth of
// a pixel, so there is nothing left for another pass to find.
if (moved < 1e-4f)
break;
}
if (attempt.points.empty())
return std::nullopt;
attempt.rms_radial_pxl = Summarize(attempt.geometry, attempt.points,
attempt.uncertainty).rms_radial_pxl;
return attempt;
};
// Every candidate, and the header alongside them - the header is a hypothesis like any other here,
// neither trusted nor discarded.
std::vector<std::pair<Attempt, float>> attempts; // attempt, and the seed it came from (0 = header)
for (size_t i = 0; i <= candidates.size(); ++i) {
const bool seeded = i < candidates.size();
const float distance = seeded ? candidates[i] : geom.GetDetectorDistance_mm();
if (auto attempt = fit_from(distance, seeded))
attempts.emplace_back(std::move(*attempt), seeded ? distance : 0.0f);
}
// Residual alone cannot rank these: a starting distance so wrong that only one ring point survives
// leaves a residual of exactly zero, and would win every time. How many ring measurements an
// attempt explains is evidence in its own right, and the first thing to compare - an attempt that
// accounts for half as much of the pattern is not in the running whatever it does with what is
// left. Among those that explain a comparable amount, the residual decides, and decides clearly,
// because only the true distance makes every ring fit at once - on the aliased LaB6 case the two
// candidates differ by 0.4 px against 5.2 px, which is not a close call.
size_t most_points = 0;
for (const auto &[attempt, seed] : attempts)
most_points = std::max(most_points, attempt.points.size());
std::optional<Attempt> best;
float best_seed = 0.0f;
for (auto &[attempt, seed] : attempts) {
if (attempt.points.size() * 2 < most_points)
continue;
if (!best || attempt.rms_radial_pxl < best->rms_radial_pxl) {
best = std::move(attempt);
best_seed = seed;
}
}
if (!best)
throw JFJochException(JFJochExceptionCategory::CalibrationError,
"No powder ring found in the summed azimuthal profile");
RingFitUncertainty unc;
const auto fitted = RingOptimizer(geom, refine_tilt).Run(points, &unc);
return Summarize(fitted, points, unc);
auto result = Summarize(best->geometry, best->points, best->uncertainty);
result.seed_distance_mm = best_seed;
result.header_distance_mm = geom.GetDetectorDistance_mm();
return result;
}
CalibrationResult CalibrateFromSpots(const std::vector<SpotToSave> &spots,