Files
Jungfraujoch/image_analysis/beam_stop/ShadowMaskGPU.h
T
leonarski_f e866262ed6 ShadowFinder: the deficit and the reflection guard in photons on a detector read in ADU
The shadow is a ratio and does not depend on the units, but two tests in the shadow finder are made
in counts: the Poisson significance of a pixel's deficit against its ring, and the maximum a pixel
must reach (25) to have recorded a reflection that is never masked. On a frame analysed in ADU at a
gain g > 1 the first was overstated by sqrt(g) and the second fired on a few photons - on a stop
that lets a little through, the whole stop was given back as "reflections". Both are now taken in
photons: the counts behind the deficit divided by the gain, the reflection count multiplied by it.
CPU and CUDA alike (ShadowMaskSetup carries the gain). g = 1 (every photon-counting input) is
bit-identical by construction.

Tests: ShadowFinder_ADetectorReadInADUGivesThePhotonMask (the photon scene times 9 at gain 9 gives
the photon mask; at gain 1 the stop is given back), and a device-vs-host section on the same scene.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
(cherry picked from commit 148177e3e54c9ac3a129c6ff3cd4fa24e6d34b07)
2026-10-23 21:21:46 +02:00

41 lines
2.1 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// Included only under JFJOCH_USE_CUDA.
#include <cstdint>
#include <vector>
#include <cuda_runtime.h>
// What the mask is drawn about: the detector, the beam centre, and the geometry the rings and the
// polarization factor are read off (ShadowFinder keeps it as a DiffractionGeometry; the device takes
// it as numbers).
struct ShadowMaskSetup {
int width = 0, height = 0;
float beam_x = 0.0f, beam_y = 0.0f;
float det_matrix[9] = {}; // row major
float pixel_size_mm = 0.0f;
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
// step the host's algorithm - the same pooling, ring medians, components, morphology and arm search -
// and the same answer wherever the arithmetic is exact: every integer, comparison, sort and component
// is. What is not is the floating point the two compilers evaluate differently - the polarization
// factor's trigonometry, the Poisson test's logarithm and the azimuth of the arm search - so a pixel
// within a rounding of one of those thresholds can come out the other way.
std::vector<uint32_t> ShadowMaskOnDevice(const ShadowMaskSetup &setup, const std::vector<uint32_t> &pixel_mask,
const int64_t *max_value, const int64_t *sum_value,
const uint32_t *valid_count, uint32_t frames, cudaStream_t stream);
// The mean projection the host's GetMeanProjection makes, computed where the sums are: the same
// division, so the same bits, and a quarter of the bytes to bring back.
std::vector<float> MeanProjectionOnDevice(const std::vector<uint32_t> &pixel_mask, const int64_t *sum_value,
const uint32_t *valid_count, size_t npixels, cudaStream_t stream);