beam stop: the rings are drawn about the centre the data measures, not the one the file claims

Every pixel is compared against the ring it sits on, and the rings were drawn about the
beam centre in the file. Displacing that centre costs nothing for tens of pixels and a
great deal beyond: on clean sweeps 50 px leaves the mask where it was, 150 px returns a
tenth of the detector as shadow with nothing blocking it. Header centres are wrong by that
much - one sweep in the corpus states one 351 px from where the data put it, on a run that
otherwise indexes every frame and merges at CC1/2 0.999 - so a shadow search that trusts
the header is a live hazard now that an empty mask is no longer the fail-safe it used to be.

The centre is therefore fitted from the projection BEFORE the mask is read, and named to
the finder with BeamCenter(). It costs no frame of its own: the fit reads the same
per-pixel mean the mask is computed from. Measured on the phase boundary, the pre-scan
grows by 0.4-0.6 s on a 2.5 Mpx detector and by 3-4 s on an 18 Mpx one.

The other half of the circle is that this fit is itself biased by a shadow it has not yet
masked - on a sweep with a third of the detector behind a pin it put the centre 5 px from
the truth and called it 0.40 px, ten of its own sigmas out. It does not have to be unbiased
here. The ring comparison does not notice tens of pixels and the bias is a few, and the
centre the run REPORTS and consumes is not this one: it is the fit that already ran after
the mask was loaded, unchanged, with the shadow out of the way. The order is fit, mask,
fit, and only the second answer leaves the function.

Two rounds are enough, measured rather than assumed: on four clean sweeps the fit before
the mask and the fit after it agree to 0.03 px, so a third round would draw the same rings.
The centre estimator is not made robust to a shadow either - it already robustifies across
azimuthal sectors, three IRLS rounds on the per-sector shifts, and that is what returned
5 px at 0.40 px, because a third of the azimuth is a second population and not an outlier.
And no guard refuses to mask when the fitted centre disagrees with the header: a header
351 px out is precisely the case this has to survive.

Mask, before -> after (pixels, and per cent of the detector):

  clean, 2.5 Mpx            20851 (0.82%) ->  20670 (0.82%)
  clean, 2.5 Mpx            33308 (1.32%) ->  32730 (1.29%)
  clean, 18 Mpx, low bkg   173804 (0.96%) -> 170031 (0.94%)
  clean, 18 Mpx            250196 (1.38%) -> 249915 (1.38%)
  a third behind a pin     818184 (32.32%) -> 815699 (32.22%)
  header 351 px out         54183 (0.87%) -> 314592 (5.05%)

The clean sweeps do not move; on all four the measured centre is 1-9 px from the file's and
the mask shrinks by under 3 %. The pinned sweep does not move either - its header centre
happens to be right, so the rings were already where they belong - and its merge is
unchanged to the third decimal.

The last row goes the other way and is not the ordering: with the rings finally about the
beam, this geometry reaches 2 theta = 68 deg, where the polarization of the source modulates
the background around a ring by a factor of three. That is four times the threshold this
test cuts at, so the horizontal lobes read as shadow. The commit that follows removes that
confound; with it the same row reads 39 k px (0.63 %), which is the holder arm and nothing
else. Even uncorrected the run's answer is unchanged - same space group, same cell to the
third decimal, 12658 unique reflections either way, R_meas 9.06 -> 9.10 %, and 1 % of the
multiplicity lost.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5 committed 2026-09-13 07:36:57 +02:00
1 parent d412306c77
commit dd95704d7b
5 files changed
+108 -19

No files matched your search

+49
View File
@@ -230,3 +230,52 @@ TEST_CASE("ShadowFinder_FindsNothingOnAnUnobstructedScene", "[ShadowFinder]") {
const auto mask = finder.GetMask();
CHECK(std::count(mask.begin(), mask.end(), 1u) == 0);
}
// The rings are drawn about a beam centre, and a centre in a file can be a long way from the truth.
// On a background that falls with radius, rings drawn about the wrong point cut across that
// fall-off, and pixels that are simply further out than the ring's median read as shadow: a large
// part of the detector comes back masked with nothing blocking it. The caller therefore names the
// centre it has measured, and that is the one the comparison has to use.
TEST_CASE("ShadowFinder_TheRingsFollowTheCentreTheCallerNames", "[ShadowFinder]") {
// Far enough out that the ring about the file's centre spans a wide range of true radii.
constexpr int OFFSET = 100;
// Opaque, well clear of the beam, and larger than the smallest region the test may return.
constexpr int PATCH_X0 = 175, PATCH_X1 = 245, PATCH_Y0 = 30, PATCH_Y1 = 90;
constexpr int PATCH_PX = (PATCH_X1 - PATCH_X0 + 1) * (PATCH_Y1 - PATCH_Y0 + 1);
DiffractionExperiment x = TestExperiment();
x.BeamX_pxl(static_cast<float>(C + OFFSET)); // what the file claims
const PixelMask pixel_mask(x);
ShadowFinder finder(x, pixel_mask);
// A background falling with the true radius, kept under MIN_REFLECTION so no pixel is exempt.
std::vector<std::vector<int32_t>> frames;
std::vector<uint8_t> buffer;
for (int f = 0; f < NFRAMES; f++) {
frames.emplace_back(static_cast<size_t>(W) * H, 0);
for (int y = 0; y < H; y++)
for (int xi = 0; xi < W; xi++) {
const float r = std::hypot(static_cast<float>(xi - C), static_cast<float>(y - C));
const bool blocked = xi >= PATCH_X0 && xi <= PATCH_X1 && y >= PATCH_Y0 && y <= PATCH_Y1;
frames.back()[I(xi, y)] = blocked ? 0 : std::lround(24.0f * std::exp(-r / 80.0f));
}
DataMessage msg{};
msg.image = CompressedImage(frames.back(), W, H);
finder.AddImage(msg, buffer);
}
const auto at_file = finder.GetMask();
finder.BeamCenter(static_cast<float>(C), static_cast<float>(C));
const auto at_measured = finder.GetMask();
// The patch is shadow either way - it is opaque, and no centre makes it look lit.
CHECK(at_file[I((PATCH_X0 + PATCH_X1) / 2, (PATCH_Y0 + PATCH_Y1) / 2)] == 1);
CHECK(at_measured[I((PATCH_X0 + PATCH_X1) / 2, (PATCH_Y0 + PATCH_Y1) / 2)] == 1);
// About the true centre the rings are flat and only the patch and its penumbra come back;
// about the file's they cut across the fall-off and a large part of the detector does.
const auto masked_at_file = std::count(at_file.begin(), at_file.end(), 1u);
const auto masked_at_measured = std::count(at_measured.begin(), at_measured.end(), 1u);
CHECK(masked_at_measured < 2 * PATCH_PX);
CHECK(masked_at_file > 3 * PATCH_PX);
}