Build Packages / Create release (push) Successful in 7s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m56s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m10s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m56s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 20m59s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 21m25s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 21m17s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 14m14s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 25m48s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 15m52s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m33s
Build Packages / Generate python client (push) Successful in 29s
Build Packages / Build documentation (push) Successful in 1m19s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m49s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 25m31s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 25m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 26m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 22m19s
Build Packages / Unit tests (push) Failing after 2h28m24s
Build Packages / Validation Battery (push) Successful in 40m33s
* Rugnux: Processes a collection live while jfjoch_broker writes it (`rugnux <master.h5> <broker URL>`). * Rugnux: Processing starts on the first images and confirms its choices once all are in, so analysis can begin during collection. * Rugnux: Faster processing, especially on servers with several GPUs and CPU sockets and on computers without a GPU (`--no-gpu`, macOS). * Rugnux: Uses much less GPU memory, so large data sets stay on the GPU of smaller cards, and a data set too large for the card is processed on the CPU instead. * Rugnux: On macOS, Bragg integration runs on the Apple GPU. * Rugnux: Better handling of twinning and pseudo-symmetry, with the twin hypothesis written as `<prefix>_twin_<SG>.mtz`, and a wrong `--model` no longer confirms the data or decides the hand. * Rugnux: `--model` accepts predicted (AlphaFold) models and falls back to molecular replacement when the model is not placed as a rigid body (experimental `--mr` forces it), settling space group, hand and twinning and writing a different answer as `<prefix>_mr.mtz`. * Rugnux: Molecular replacement is faster and settles the space group in more cases with several copies in the asymmetric unit. * Rugnux: Deposition-ready mmCIF, a richer report, and numbered steps on the terminal with the full log in `<prefix>_log.txt`; `_P1.mtz`, `--estimate-beam-center` and `--beam-center-search` are removed. * Rugnux: The beam-stop shadow is found correctly on tilted detectors. * Rugnux: Stills files that record a stationary goniometer axis get the default stills geometry refinement. * jfjoch_viewer: "Analyze dataset" uses the same defaults as a bare `rugnux <file>`, and shows the results of processed CBF, marCCD and SMV sweeps. * jfjoch_viewer: The Files panel refreshes on F5 and every few seconds, and the plot type and other selections stay put during live collection. * jfjoch_writer: Memory stays bounded and old data files are closed efficiently on long runs, any failure ends the run in an error state the broker is told about, and the master file appears only after its data files; the ZeroMQ pusher logs when a stalled writer makes it drop images. * Fixes: The TCP image pusher no longer blocks the broker's receiver, GPU out-of-memory failures stop with a clear message, and the XDS plugin masks module and chip edges. --------- Co-authored-by: Filip Leonarski <jungfrau@filips-mac-mini.home> Reviewed-on: #85 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
124 lines
6.6 KiB
C++
124 lines
6.6 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
// What ShadowFinder::GetMask shares with its device twin (ShadowMaskGPU): the constants it tests
|
|
// against, where a pixel sits on its ring, and the two small fits made over whole rings and sectors,
|
|
// which stay on the host on both paths.
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
#ifdef __CUDACC__
|
|
#define SHADOW_FINDER_HD __host__ __device__ inline
|
|
#else
|
|
#define SHADOW_FINDER_HD inline
|
|
#endif
|
|
|
|
namespace shadow_finder {
|
|
|
|
// Where a pixel sits on the ring it is compared along. A ring is a cone of constant 2theta about the
|
|
// beam. On an untilted detector that is a circle about the beam centre, but on a tilted one - a
|
|
// detector on a swung 2theta arm - it is a circle about no pixel at all, and a circle drawn about the
|
|
// centre runs across the background's own fall-off: 5 degrees of tilt at 50 mm put a quarter of a
|
|
// detector in the shadow. So a pixel's ring is its 2theta, in pixels: `radius` is distance *
|
|
// tan(2theta), the radius it would have on an untilted detector at the same distance, and (lx, ly) is
|
|
// its offset from the beam in that frame, which gives its azimuth. A tilt also makes a pixel's solid
|
|
// angle vary along the ring, by tens of per cent at that same tilt; `obliquity` is the pixel's solid
|
|
// angle over the one the untilted detector has at the same 2theta, (distance / lz)^3, and the
|
|
// background is divided by it as by the polarization factor.
|
|
// (dx, dy) is the pixel's offset from the geometry's beam centre, `det_matrix` the detector matrix
|
|
// (row major) and `distance_px` the detector distance in pixels. Untilted, (lx, ly) is (dx, dy) and
|
|
// the obliquity is 1, exactly.
|
|
struct RingPosition {
|
|
float lx, ly, radius, obliquity;
|
|
};
|
|
|
|
SHADOW_FINDER_HD RingPosition ring_position(const float *det_matrix, float distance_px, float dx, float dy) {
|
|
const float *m = det_matrix;
|
|
const float lx = m[0] * dx + m[1] * dy + m[2] * distance_px;
|
|
const float ly = m[3] * dx + m[4] * dy + m[5] * distance_px;
|
|
const float lz = m[6] * dx + m[7] * dy + m[8] * distance_px;
|
|
const float stretch = distance_px / lz;
|
|
return {lx, ly, sqrtf(lx * lx + ly * ly) * stretch, stretch * stretch * stretch};
|
|
}
|
|
|
|
// The values of ShadowFinder::SHADOW and ShadowFinder::TRANSMITTING, for the device code, which does
|
|
// not include ShadowFinder.h.
|
|
inline constexpr uint32_t MASK_SHADOW = 1;
|
|
inline constexpr uint32_t MASK_TRANSMITTING = 2;
|
|
|
|
// A pixel is shadow when its background is below this fraction of the background it is
|
|
// compared against.
|
|
inline constexpr float SHADOW_RATIO = 0.50f;
|
|
|
|
// The boundary grows outward into partially shadowed pixels down to this fraction, but no
|
|
// further than PENUMBRA_MAX_PX from the core. A pin or a loop casts a wide half-shadow, so the
|
|
// reach is a good deal more than the beam stop's own edge needs.
|
|
inline constexpr float PENUMBRA_RATIO = 0.75f;
|
|
inline constexpr int PENUMBRA_MAX_PX = 30;
|
|
|
|
// Bridge small breaks along the holder arm. A module gap wider than this is bridged separately for
|
|
// 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.
|
|
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:
|
|
// 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.
|
|
inline constexpr double MIN_DEFICIT_SIGMA = 6.0;
|
|
|
|
// Smallest region the per-pixel test may return. A shadow is cast by something physical and is
|
|
// correspondingly large; an isolated patch this small is the background wandering, not hardware.
|
|
// This is what keeps the test specific now that a shadow no longer has to touch the direct beam.
|
|
inline constexpr int MIN_SHADOW_PIXELS = 2000;
|
|
|
|
// ... and of those, how many must be deep (below SHADOW_RATIO) for a region of merely DIM pixels -
|
|
// hardware that lets part of the beam through - to count. A thin holder arm is dim along most of
|
|
// its length and deep in places; the background drifting over a detector's edge is dim everywhere
|
|
// and deep nowhere.
|
|
inline constexpr int MIN_CORE_PIXELS = 200;
|
|
|
|
// The arm search compares a pixel with its ring as the ring actually varies around the beam. A ring
|
|
// of background is not flat once divided by the polarization factor when that factor is not the
|
|
// beam's: the remainder is a second harmonic in azimuth, cos 2phi, which reaches tens of percent at
|
|
// high angle. It is measured over radial bands of HARMONIC_BAND_PX, from the median of each of
|
|
// HARMONIC_SECTORS sectors that holds at least MIN_SECTOR_PIXELS pixels.
|
|
inline constexpr int HARMONIC_BAND_PX = 64;
|
|
inline constexpr int HARMONIC_SECTORS = 24;
|
|
inline constexpr int MIN_SECTOR_PIXELS = 200;
|
|
|
|
// Side of the box the background is pooled over before testing. Its area is how many pixels back
|
|
// a ring's countability test, which decides where an azimuthal comparison is possible at all.
|
|
inline constexpr int POOL_PX = 5;
|
|
inline constexpr double MEAN_POOLED_PIXELS = POOL_PX * POOL_PX;
|
|
|
|
// A ring with fewer valid pixels than this says nothing about whether it was counted.
|
|
inline constexpr int MIN_RING_PIXELS = 32;
|
|
|
|
// A ring lies wholly inside the stop when its background is below this fraction of the background
|
|
// further out. This asks about a whole ring rather than about a pixel, so it keeps a threshold of
|
|
// its own and does not follow SHADOW_RATIO.
|
|
inline constexpr float BLOCKED_RING_RATIO = 0.35f;
|
|
|
|
// The rings that lie wholly inside the stop: walking outward, every judgeable ring (at least
|
|
// MIN_RING_PIXELS pixels) before the first whose baseline reaches BLOCKED_RING_RATIO of the typical
|
|
// background. -1 when there is none. See GetMask.
|
|
int BlockedOutTo(const std::vector<float> &baseline, const std::vector<int> &ring_pixels);
|
|
|
|
// The second harmonic in azimuth of each radial band, relative to its level, fitted to the medians of
|
|
// its HARMONIC_SECTORS sectors (sector_median[band * HARMONIC_SECTORS + k], negative where the sector
|
|
// has too few pixels). See GetMask.
|
|
void HarmonicFit(const std::vector<double> §or_median, int n_bands,
|
|
std::vector<float> &harm_c, std::vector<float> &harm_s);
|
|
|
|
} // namespace shadow_finder
|