beam stop: a ring is only flat once the polarization is divided out

The shadow test compares a pixel with the median of its ring, which assumes the background is flat
around a ring with nothing in the beam. It is not: a polarized source suppresses the background in
its own plane by 1 - sin^2(2 theta), a factor of three at 2 theta = 55 degrees and four at 70. That
is several times the dip the test is looking for, so on a short-distance geometry the two horizontal
lobes of every outer ring read as shadow. Measured over the corpus it cost one dataset 5.1 % of its
detector, and another 11.2 %, with nothing visible under either mask.

The mean projection is therefore divided by the Kahn factor before the ring comparison, and the
Poisson deficit multiplies it back in so the significance is still the significance of the counts
that were recorded. The factor is the geometry's own CalcAzIntPolarizationCorr, evaluated about the
centre the caller named rather than the one in the file: the azimuth is the whole point of this
correction, and the geometry is what knows where the polarization plane lies once the detector is
tilted, the stored image quarter-turned or the detector rotated in its own plane. Six corpus
datasets are quarter-turned, and a flat-detector formula on the stored image's own axes gets them
90 degrees out of phase - measured, it takes one of them from 16.5 % masked to 22.8 % where the
geometry takes it to 4.4 %. Polarization is also the only correction a ring carries that varies
along it; solid angle, detector and air absorption are functions of 2 theta and the ring median
absorbs them.

Measured, mask fraction of the detector: a clean 110 mm sweep 0.82 -> 0.51 %, a clean 200 mm 18 Mpx
sweep 0.94 -> 0.80 %, a 150 mm sweep whose header centre is 351 px out 5.05 -> 0.74 %, and the
sweep with a third of the detector behind a pin 32.22 -> 32.83 %. Clean detectors lose mask, a real
obstruction gains it. Across 151 corpus datasets nothing gains more than a tenth of a percent from
the correction and 26 lose some, 6 of them by more than 3 percentage points.

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 dd95704d7b
commit 99910e187e
6 files changed
+128 -15

No files matched your search

+59
View File
@@ -6,6 +6,7 @@
#include <algorithm>
#include <cmath>
#include <cstring>
#include <optional>
#include <vector>
#include "../common/DetectorSetup.h"
@@ -279,3 +280,61 @@ TEST_CASE("ShadowFinder_TheRingsFollowTheCentreTheCallerNames", "[ShadowFinder]"
CHECK(masked_at_measured < 2 * PATCH_PX);
CHECK(masked_at_file > 3 * PATCH_PX);
}
// The background is not flat around a ring: a polarized source suppresses it in its own plane, by
// a factor that reaches four at the 2 theta a short detector distance puts in a corner. That is
// several times the dip this class looks for, so with the modulation left in, half of every outer
// ring reads as shadow with nothing in the beam. The scene here is exactly that and nothing else -
// a background carrying the Kahn factor of a horizontally polarized source, no hardware anywhere -
// and it must come back empty when the experiment states its polarization, and does not when it
// says nothing, which is what the correction is for.
TEST_CASE("ShadowFinder_APolarizedBackgroundIsNotAShadow", "[ShadowFinder]") {
// Short enough that this small detector reaches 2 theta = 70 degrees in its corner, where
// the source suppresses its own plane to a fifth. A real geometry gets there with a short
// crystal-to-detector distance and a large detector.
constexpr float DISTANCE_MM = 5.0f;
constexpr float POLARIZATION = 0.99f;
// Counts per pixel per frame across the azimuth the source does not suppress. Below
// MIN_REFLECTION, so no pixel of this scene is exempt as a recorded reflection.
constexpr int LEVEL = 24;
DiffractionExperiment reference = TestExperiment();
const float pixel_mm = reference.GetPixelSize_mm();
std::vector<int32_t> frame(static_cast<size_t>(W) * H);
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
const double dx = x - C, dy = y - C;
const double rho = std::hypot(dx, dy) * pixel_mm;
const double cos_2theta_sq = DISTANCE_MM * DISTANCE_MM
/ (DISTANCE_MM * DISTANCE_MM + rho * rho);
const double rr = dx * dx + dy * dy;
const double cos_2phi = rr > 0.0 ? (dx * dx - dy * dy) / rr : 0.0;
const double factor = 0.5 * (1.0 + cos_2theta_sq
- POLARIZATION * cos_2phi * (1.0 - cos_2theta_sq));
// Normalised per ring by the azimuth the source does not suppress, so what is left
// varies around a ring and not with radius - the ring median absorbs the rest.
const double at_peak = 0.5 * (1.0 + cos_2theta_sq
+ POLARIZATION * (1.0 - cos_2theta_sq));
frame[I(x, y)] = static_cast<int32_t>(std::lround(LEVEL * factor / at_peak));
}
auto mask_for = [&](const std::optional<float> &polarization) {
DiffractionExperiment x = TestExperiment();
x.DetectorDistance_mm(DISTANCE_MM).PolarizationFactor(polarization);
const PixelMask pixel_mask(x);
ShadowFinder finder(x, pixel_mask);
std::vector<uint8_t> buffer;
for (int f = 0; f < NFRAMES; f++) {
DataMessage msg{};
msg.image = CompressedImage(frame, W, H);
finder.AddImage(msg, buffer);
}
const auto mask = finder.GetMask();
return std::count(mask.begin(), mask.end(), 1u);
};
CHECK(mask_for(POLARIZATION) == 0);
CHECK(mask_for(std::nullopt) > static_cast<long>(W) * H / 20);
}