Stills integration: background ring reach measured on empty apertures, radial curvature from the whole frame

Two changes to the stills integration background, both stills-only (rotation keeps its path bit for bit):

1. The r2..r3 background ring's outer radius is measured per run. The pre-scan lays apertures on a
   grid over its frames, away from every found spot, and for each candidate r3 (r2+2 .. 14) takes the
   mean square difference between the r1 disk mean and the clipped ring mean. With nothing in the
   aperture that is exactly the background error the ring makes (noise + bias); the run keeps the
   minimum. On a photon-counting pixel detector the noise wins and the shipped r3=14 is kept; on a
   binned CCD the background structure wins and the narrowest ring is taken. r1 stays 6: a smaller
   disk (4,6,14) costs 0.0013 dimple R_free on a JUNGFRAU set, so it does not pass the
   photon-counting gate. --adaptive-integration-radius now defaults on for stills too (=off restores
   the fixed 6/8/14). The pre-scan bandwidth estimate is not applied to stills.

2. The radial background-curvature correction on stills takes its curve from the adaptive spot
   finder's peak-excluded per-ring background (whole frame) instead of the predicted reflections'
   own ring pixels, and auto applies it to every stills image (no ice-score gate: a solvent/carrier
   hump curves the background as much as ice). New engine call RadialBackgroundFromRings; CPU, GPU
   and Metal read the handed-over curve. The GPU box sum now sizes the radial bin index separately
   from accumulating, so a handed-over curve is read at the right radius.

Measured (dimple R_free, deposited free set, battery arguments, all frames unless noted):
  pal86  (Rayonix 4x4, 156 um): 0.354 -> 0.218   (ring 10; at 4,6,10 0.213)
  lcls222 (Rayonix 4x4, 177 um): 0.205 -> 0.196  (ring 10)
  sacla236 (MPCCD): 0.2588 -> 0.2558             (ring 11)
  alvra213_int16 (JUNGFRAU): 0.1986 -> 0.1986    (ring 14, unchanged)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-21 18:10:08 +02:00
1 parent 987e812e23
commit e8145d8d2f
17 files changed
+558 -42

No files matched your search

@@ -218,6 +218,50 @@ void BraggIntegrationEngine::BuildRadialKernel(float grow) {
k_diff.push_back(static_cast<float>(hist_ann[k] / sa - hist_disk[k] / sd));
}
void BraggIntegrationEngine::RadialBackgroundFromRings(const std::vector<float> &ring_bkg, int32_t q_bins,
float low_q, float dq) {
radial_curve.clear();
if (q_bins <= 0 || !(dq > 0.0f))
return;
// The rings averaged over their azimuthal bins, where the profile has any.
std::vector<double> sum(q_bins, 0.0);
std::vector<int> cnt(q_bins, 0);
for (size_t i = 0; i < ring_bkg.size(); ++i)
if (std::isfinite(ring_bkg[i])) {
sum[i % q_bins] += ring_bkg[i];
++cnt[i % q_bins];
}
std::vector<double> q, b;
for (int i = 0; i < q_bins; ++i)
if (cnt[i] > 0) {
q.push_back(low_q + (i + 0.5) * dq);
b.push_back(sum[i] / cnt[i]);
}
if (q.size() < 2)
return;
// The same bins as the engines' own curve: one per pixel of distance from the beam, out to the far
// corner. Each is read off the rings by linear interpolation in q; beyond the first and the last
// ring the end value is held, which has no curvature to correct.
const int n = static_cast<int>(std::ceil(r_max)) + 2;
const double two_k = 4.0 * PI / geom.GetWavelength_A();
radial_curve.resize(n);
size_t j = 0;
for (int r = 0; r < n; ++r) {
const double qr = two_k * std::sin(0.5 * std::atan(r / F_px));
while (j + 1 < q.size() && q[j + 1] < qr)
++j;
double v;
if (qr <= q.front())
v = b.front();
else if (j + 1 >= q.size())
v = b.back();
else
v = b[j] + (b[j + 1] - b[j]) * (qr - q[j]) / (q[j + 1] - q[j]);
radial_curve[r] = static_cast<float>(v);
}
}
std::vector<BraggPanel> BraggIntegrationEngine::Panels(const std::vector<Reflection> &predicted,
size_t npredicted) const {
std::vector<BraggPanel> out(npredicted);