diff --git a/docs/CPU_DATA_ANALYSIS_IMAGE.md b/docs/CPU_DATA_ANALYSIS_IMAGE.md index 45423a635..d90ea2f17 100644 --- a/docs/CPU_DATA_ANALYSIS_IMAGE.md +++ b/docs/CPU_DATA_ANALYSIS_IMAGE.md @@ -336,7 +336,8 @@ and the stop is much wider than the box — and the comparison is against the me ring, taken over the pixels not already excluded and iterated a few times so the shadow stays out of the baseline it is measured against. A pixel is shadow when the ratio falls below 0.50 **and** the deficit is significant against its own Poisson scatter, $\sqrt{2\,(E-N+N\ln(N/E))}$ over the pooled -counts. A ratio says nothing when the background behind it is a handful of photons, and it is that +counts, taken in photons (on a detector read in ADU the counts are divided by its gain, as the protected +reflection count of 25 photons is multiplied by it). A ratio says nothing when the background behind it is a handful of photons, and it is that significance that makes the comparison scale-free rather than tuned to one exposure: rebuilt from six frames of a low-background sweep, the same ratio without it masks three quarters of the detector. The index of dispersion does *not* do this job — it is 1.0 inside the shadow and 1.0 outside it, a diff --git a/image_analysis/beam_stop/SHADOW_FINDER.md b/image_analysis/beam_stop/SHADOW_FINDER.md index fd451c6e2..2f15a418d 100644 --- a/image_analysis/beam_stop/SHADOW_FINDER.md +++ b/image_analysis/beam_stop/SHADOW_FINDER.md @@ -137,7 +137,7 @@ just safe headroom. | `penumbra_ratio` | 0.75 | soft boundary grows out to this ratio … | | `penumbra_max_px` | 30 | … but no further than this from the core | | `bridge_px` | 6 | bridge module gaps / breaks the arm crosses | -| `min_reflection` | 25 | max-projection value at/above which a pixel is a protected reflection | +| `min_reflection` | 25 | max-projection value, in photons (times the gain on a detector read in ADU), at/above which a pixel is a protected reflection | ## Complexity & memory diff --git a/image_analysis/beam_stop/ShadowFinder.cpp b/image_analysis/beam_stop/ShadowFinder.cpp index 171a569ba..1081eb1bd 100644 --- a/image_analysis/beam_stop/ShadowFinder.cpp +++ b/image_analysis/beam_stop/ShadowFinder.cpp @@ -489,6 +489,7 @@ ShadowFinder::ShadowFinder(const DiffractionExperiment &experiment, const PixelM beam_y(experiment.GetBeamY_pxl()), geometry(experiment.GetDiffractionGeometry()), polarization(experiment.GetPolarizationFactor()), + gain(experiment.GetBraggIntegrationSettings().GetPixelGain()), pixel_mask(mask.GetMask(experiment)), modules(experiment.GetModules()) { if (pixel_mask.size() != static_cast(width) * height) @@ -757,6 +758,7 @@ std::vector ShadowFinder::GetMask(size_t nthreads) const { setup.pixel_size_mm = geometry.GetPixelSize_mm(); setup.distance_mm = geometry.GetDetectorDistance_mm(); ScopedGpuPin pin(gpu->GetDevice()); + setup.gain = gain; setup.has_polarization = polarization.has_value(); if (polarization) { setup.polarization = PlaneApplied(*polarization, @@ -970,11 +972,11 @@ std::vector ShadowFinder::MaskOn(const Projection &p, const std::vecto low[i] = 0; if (!valid[i]) continue; - // The deficit is significant or not in the counts that were recorded, so the pooled + // The deficit is significant or not in the photons that were recorded, so the pooled // background and what the ring expects of it are both put back on the detector's own - // scale before they are compared - the same factor on each, so it is the units of the - // comparison that change and not its answer. - const double counted = static_cast(frames) * pooled_count[i] * pol[i]; + // scale, and from its units into photons, before they are compared - the same factor on + // each, so it is the units of the comparison that change and not its answer. + const double counted = static_cast(frames) * pooled_count[i] * pol[i] / gain; deficit[i] = static_cast(poisson_deficit_sigma(pooled[i] * counted, baseline[radius[i]] * counted)); low[i] = ratio[i] < SHADOW_RATIO && deficit[i] > MIN_DEFICIT_SIGMA; @@ -1014,10 +1016,11 @@ std::vector ShadowFinder::MaskOn(const Projection &p, const std::vecto // Recorded reflections. A small cluster is required so a single-frame zinger does not count. + const double min_reflection = static_cast(MIN_REFLECTION) * gain; Plane lit(n_pixels); ParallelChunks(n_pixels, nthreads, [&](int lo, int hi) { for (int i = lo; i < hi; i++) - lit[i] = (valid_count[i] > 0) && (max_value[i] >= MIN_REFLECTION); + lit[i] = (valid_count[i] > 0) && (static_cast(max_value[i]) >= min_reflection); }); Plane reflection(n_pixels); ParallelChunks(H, nthreads, [&](int ylo, int yhi) { @@ -1143,7 +1146,7 @@ std::vector ShadowFinder::MaskOn(const Projection &p, const std::vecto dim[i] = deficit[i] > MIN_DEFICIT_SIGMA; continue; } - const double counted = static_cast(frames) * pooled_count[i] * pol[i]; + const double counted = static_cast(frames) * pooled_count[i] * pol[i] / gain; dim[i] = ratio[i] < PENUMBRA_RATIO * model && poisson_deficit_sigma(pooled[i] * counted, baseline[radius[i]] * model * counted) > MIN_DEFICIT_SIGMA; } diff --git a/image_analysis/beam_stop/ShadowFinder.h b/image_analysis/beam_stop/ShadowFinder.h index d87e59bf5..168c7087b 100644 --- a/image_analysis/beam_stop/ShadowFinder.h +++ b/image_analysis/beam_stop/ShadowFinder.h @@ -63,6 +63,10 @@ class ShadowFinder { const std::optional polarization; // unset leaves the background as it was measured // What the last GetMask divided by: the stated degree, in the plane the background shows. mutable std::optional applied_polarization; + // Detector units per photon (BraggIntegrationSettings::PixelGain; 1 for counted photons). The + // shadow is a ratio and does not see it, but the two tests made in counts do: how significant a + // deficit is, and the count a pixel must reach to have recorded a reflection. + const float gain; std::vector pixel_mask; // pixels already masked carry no background to test // The panels the image is made of. With any, the mask is made on the detector plane (GetMask). diff --git a/image_analysis/beam_stop/ShadowFinderInternal.h b/image_analysis/beam_stop/ShadowFinderInternal.h index af89c80bd..47036e335 100644 --- a/image_analysis/beam_stop/ShadowFinderInternal.h +++ b/image_analysis/beam_stop/ShadowFinderInternal.h @@ -64,13 +64,15 @@ inline constexpr int PENUMBRA_MAX_PX = 30; // the arm search (see bridge_gaps). inline constexpr int BRIDGE_PX = 6; -// A pixel whose maximum reaches this recorded a real reflection and is never masked - a -// beam stop cannot block a reflection that was measured. +// A pixel whose maximum reaches this many photons recorded a real reflection and is never masked - a +// beam stop cannot block a reflection that was measured. On a detector read in ADU it is taken at the +// gain. inline constexpr int64_t MIN_REFLECTION = 25; // How far below the background it is compared against a pixel must sit before the dip is -// believed, in standard deviations of the counts that back it. The counts are photons, so their -// scatter is Poisson and the deficit is measured against it rather than against a fixed number: +// believed, in standard deviations of the photons that back it (counts divided by the gain on a +// detector read in ADU), so their scatter is Poisson and the deficit is measured against it rather +// than against a fixed number: // on a well-exposed sweep a third of the background missing is overwhelming, and on a handful of // low-background frames the same third is noise. Without this a six-frame pre-scan of a // low-background sweep masks three quarters of the detector. diff --git a/image_analysis/beam_stop/ShadowMaskGPU.cu b/image_analysis/beam_stop/ShadowMaskGPU.cu index b57efc671..b61d6f7b5 100644 --- a/image_analysis/beam_stop/ShadowMaskGPU.cu +++ b/image_analysis/beam_stop/ShadowMaskGPU.cu @@ -197,7 +197,8 @@ __global__ void baseline_kernel(int rings, const uint64_t *__restrict__ keys, co baseline[r] = b; } -__global__ void low_kernel(size_t n, uint32_t frames, const char *__restrict__ valid, const float *__restrict__ pooled, +__global__ void low_kernel(size_t n, uint32_t frames, float gain, const char *__restrict__ valid, + const float *__restrict__ pooled, const int32_t *__restrict__ pooled_count, const float *__restrict__ pol, const int *__restrict__ radius, const float *__restrict__ baseline, float *__restrict__ ratio, float *__restrict__ deficit, char *__restrict__ low) { @@ -209,7 +210,7 @@ __global__ void low_kernel(size_t n, uint32_t frames, const char *__restrict__ v float df = 0.0f; char l = 0; if (valid[i]) { - const double counted = static_cast(frames) * pooled_count[i] * pol[i]; + const double counted = static_cast(frames) * pooled_count[i] * pol[i] / gain; df = static_cast(poisson_deficit_sigma(pooled[i] * counted, base * counted)); l = rt < SHADOW_RATIO && df > MIN_DEFICIT_SIGMA; } @@ -283,11 +284,11 @@ __global__ void region_kernel(size_t n, const int *__restrict__ root, const int region[i] = r; } -__global__ void lit_kernel(size_t n, const uint32_t *__restrict__ valid_count, const int64_t *__restrict__ max_value, - char *__restrict__ lit) { +__global__ void lit_kernel(size_t n, double min_reflection, const uint32_t *__restrict__ valid_count, + const int64_t *__restrict__ max_value, char *__restrict__ lit) { const size_t i = blockIdx.x * static_cast(blockDim.x) + threadIdx.x; if (i >= n) return; - lit[i] = (valid_count[i] > 0) && (max_value[i] >= MIN_REFLECTION); + lit[i] = (valid_count[i] > 0) && (static_cast(max_value[i]) >= min_reflection); } __global__ void reflection_kernel(int W, int H, const char *__restrict__ lit, char *__restrict__ reflection) { @@ -402,7 +403,7 @@ __global__ void dim_kernel(ShadowMaskSetup s, uint32_t frames, const char *__res if (model >= 1.0f) { d = deficit[i] > MIN_DEFICIT_SIGMA; } else { - const double counted = static_cast(frames) * pooled_count[i] * pol[i]; + const double counted = static_cast(frames) * pooled_count[i] * pol[i] / s.gain; d = ratio[i] < PENUMBRA_RATIO * model && poisson_deficit_sigma(pooled[i] * counted, baseline[radius[i]] * model * counted) > MIN_DEFICIT_SIGMA; } @@ -564,7 +565,7 @@ std::vector ShadowMaskOnDevice(const ShadowMaskSetup &setup, const std const int blocked_out_to = BlockedOutTo(host_baseline, ring_pixels); // Low pixels, and the regions of them large enough to be hardware. - low_kernel<<>>(n, frames, valid, pooled, pooled_count, pol, radius, baseline, + low_kernel<<>>(n, frames, setup.gain, valid, pooled, pooled_count, pol, radius, baseline, ratio, deficit, low); e.Check("low"); e.Dilate(low, a, c, BRIDGE_PX); @@ -575,7 +576,8 @@ std::vector ShadowMaskOnDevice(const ShadowMaskSetup &setup, const std e.Check("region"); // Recorded reflections; `b` holds them until they are given back at the end. - lit_kernel<<>>(n, valid_count, max_value, a); + lit_kernel<<>>(n, static_cast(MIN_REFLECTION) * setup.gain, valid_count, + max_value, a); reflection_kernel<<>>(W, H, a, b); e.Check("reflections"); diff --git a/image_analysis/beam_stop/ShadowMaskGPU.h b/image_analysis/beam_stop/ShadowMaskGPU.h index 16b641cbf..c3b8d9f00 100644 --- a/image_analysis/beam_stop/ShadowMaskGPU.h +++ b/image_analysis/beam_stop/ShadowMaskGPU.h @@ -21,6 +21,7 @@ struct ShadowMaskSetup { float distance_mm = 0.0f; bool has_polarization = false; float polarization = 0.0f; + float gain = 1.0f; // detector units per photon (ShadowFinder::gain) }; // ShadowFinder::GetMask on the device, from the projection ShadowAccumulatorGPU holds there. Step for diff --git a/tests/ShadowFinderTest.cpp b/tests/ShadowFinderTest.cpp index 4a4b9384a..32fff47f7 100644 --- a/tests/ShadowFinderTest.cpp +++ b/tests/ShadowFinderTest.cpp @@ -112,6 +112,37 @@ TEST_CASE("ShadowFinder_FindsAnInjectedBeamStop", "[ShadowFinder]") { CHECK(std::count(mask.begin(), mask.end(), 1u) == 2612); } +// A detector read in ADU is the photon scene times its gain, and with the gain given the mask is the +// photon scene's. The stop here lets three photons a frame through: at nine ADU a photon that is 27 +// counts, which a photon threshold would take for a recorded reflection and give the whole stop back. +TEST_CASE("ShadowFinder_ADetectorReadInADUGivesThePhotonMask", "[ShadowFinder]") { + constexpr int32_t gain = 9; + const auto mask_of = [](int32_t scale, float pixel_gain) { + DiffractionExperiment x = TestExperiment(); + BraggIntegrationSettings bis = x.GetBraggIntegrationSettings(); + bis.PixelGain(pixel_gain); + x.ImportBraggIntegrationSettings(bis); + const PixelMask pixel_mask(x); + ShadowFinder finder(x, pixel_mask); + std::vector> frames; + std::vector buffer; + for (int f = 0; f < NFRAMES; f++) { + std::vector frame = Scene(/*cross=*/false, /*reflection=*/false); + for (auto &v : frame) + v = scale * (v == 0 ? 3 : 12); + frames.push_back(std::move(frame)); + DataMessage msg{}; + msg.image = CompressedImage(frames.back(), W, H); + finder.AddImage(msg, buffer); + } + return finder.GetMask(); + }; + const auto photons = mask_of(1, 1.0f); + REQUIRE(photons[I(C, C)] == 1); + CHECK(mask_of(gain, static_cast(gain)) == photons); + CHECK(mask_of(gain, 1.0f)[I(C, C)] == 0); +} + // A holder arm that lets part of the beam through, on a background bright enough that every pixel // reaches the reflection guard's count, crossing a module gap wider than the bridge. Each of the // three hid the arm on its own: the partly transmitting stretch never reaches SHADOW_RATIO, the gap @@ -680,6 +711,23 @@ TEST_CASE("ShadowFinder_DeviceMaskMatchesHost", "[ShadowFinder]") { CHECK(std::count(r.host_mask.begin(), r.host_mask.end(), ShadowFinder::TRANSMITTING) > 0); CHECK(r.device_mask == r.host_mask); } + + SECTION("a stop that lets a little through, read in ADU at the gain") { + constexpr int32_t gain = 9; + DiffractionExperiment x = TestExperiment(); + BraggIntegrationSettings bis = x.GetBraggIntegrationSettings(); + bis.PixelGain(static_cast(gain)); + x.ImportBraggIntegrationSettings(bis); + std::vector> frames; + for (int f = 0; f < NFRAMES; f++) { + frames.push_back(Scene(false, false)); + for (auto &v : frames.back()) + v = gain * (v == 0 ? 3 : 12); + } + const auto r = MaskBothWays(x, frames); + CHECK(r.host_mask[I(C, C)] == 1); + CHECK(r.device_mask == r.host_mask); + } } // The pre-scan keeps one accumulator per card and folds them together when the mask is read. Two