One gain mechanism: frames analysed in their own units, the gain applied where a photon count is assumed

The gain of an input in detector units (SMV, marCCD, XTC, the XFEL formats) is measured once, before
anything reads the frames (RugnuxMeasureGain: photon-peak spacing and background variance per unit
mean), or given with --gain, and handed to the integration and the spot finding by one function
(ApplyRugnuxPixelGain). The rotation pre-scan no longer measures it, and the stills reader no longer
converts frames of a detector whose photons are not resolved: SACLA MPCCD, Rayonix and CCD frames are
integrated in ADU at the gain, rotation and stills alike.

Where a photon count was assumed, it is now taken at the gain (bit-identical at gain 1):
- integration: the background clip's one-photon floor, the sigma floor (one photon), the profile fit's
  background floor (0.01 photon), and a still's overload kept as a lower bound, which was left in ADU
  while every other reflection left the integrator in photons - the cause of the earlier loss of the
  engine route on clipped CCD stills (merged low resolution inflated by the gain, the rocking-width
  fit's bandwidth collapsing); CPU and CUDA, Metal through the shared CPU code;
- the adaptive spot threshold (Poisson arm and its one-photon floor), CPU and GPU;
- the pre-scan spot-width measurement (strong-spot SNR, the ring clip) behind the stills bandwidth and
  the rotation signal radius;
- the hot/dead pixel tests, CPU and GPU; the empty-aperture ring clip.
BackgroundPixelGain is gone: one pixel_gain.

A detector whose background resolves single photons (photon peaks: JUNGFRAU in keV, AGIPD) is a photon
counter and its frames are still counted in whole photons as read: analysed in keV at the gain instead,
a photon-counting still set lost 0.008 R-free (fewer spots, a noisier beam-centre trial) - the rounding
is the detector's own discrimination. --gain sets the value, the background still says which kind.

Validation, against rc178: photon-counting and g<=1 rotation p.hkl md5-identical (lyso, thau, cytc,
myob, insu, a small-molecule set, 5reo, 6cee, 6i3j, 2xfw, 6s1u, 8v4j); CCD g>1 at or within noise of rc178 (5uth
R_meas 0.113 -> 0.108). Stills R-free (0-cycle REFMAC): MPCCD sets 0.184 -> 0.185 and 0.199 -> 0.196,
Rayonix identical, photon-resolving sets back on their rc178 numbers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
(cherry picked from commit da8dbfc8147354b63e375f2d9521dffb80938b6d)
This commit is contained in:
leonarski_f committed 2026-10-23 17:56:51 +02:00
1 parent 15793a6bc2
commit 711f902aa7
30 files changed
+341 -267

No files matched your search

@@ -354,10 +354,12 @@ bool BraggIntegrationEngineCPU::ClipsRing(const BraggBoxSums &s) const {
int32_t BraggIntegrationEngineCPU::ClipThreshold(const BraggBoxSums &s) const {
// Reject pixels above mean + n*sqrt(gain*mean) to strip a neighbour core or a zinger that biases
// the mean. The gain is 1 for counted photons, so this is then exactly the Poisson clip. A pixel
// value is an integer, so it is at or below the threshold exactly when it is at or below its floor.
// the mean, the mean taken at no less than one photon. The gain is 1 for counted photons, so this is
// then exactly the Poisson clip. A pixel value is an integer, so it is at or below the threshold
// exactly when it is at or below its floor.
const double bkg = static_cast<double>(s.bkg_sum) / s.n_bkg;
const double thr = bkg + bkg_clip_nsigma * std::sqrt(static_cast<double>(bkg_pixel_gain) * std::max(bkg, 1.0));
const double g = pixel_gain;
const double thr = bkg + bkg_clip_nsigma * std::sqrt(g * std::max(bkg, g));
return static_cast<int32_t>(std::clamp(std::floor(thr), static_cast<double>(INT32_MIN),
static_cast<double>(INT32_MAX)));
}
@@ -440,7 +442,7 @@ void BraggIntegrationEngineCPU::FinishPassA(PassA &a, size_t i, const Reflection
out.n_disk = s.n_disk;
out.n_own = s.n_own;
const double var_bkg_term = static_cast<double>(s.n_inner_valid) * s.n_inner_valid * out.bkg_var;
out.sigma = 1.0;
out.sigma = g; // a photon
if (s.I_sum > 0) {
out.sigma = std::max(out.sigma, std::sqrt(g * (static_cast<double>(s.I_sum) + var_bkg_term)));
// The centroid of the SIGNAL, not of the disk. Weighting by the raw counts weights by
@@ -825,7 +827,7 @@ std::vector<Reflection> BraggIntegrationEngineCPU::ProfilePasses(const Sampler &
fs.inv_2s2t = 0.5f / static_cast<float>(s2t);
}
fs.bkg = static_cast<float>(rh.bkg);
fs.B = static_cast<float>(std::max(rh.bkg, PIXEL_VARIANCE_FLOOR));
fs.B = static_cast<float>(std::max(rh.bkg, pixel_gain * PIXEL_VARIANCE_FLOOR));
fs.v_min = static_cast<float>(WEIGHT_VARIANCE_MIN_FRACTION) * fs.B;
fs.I0 = static_cast<float>(rh.I);
}