Build Packages / Create release (push) Successful in 21s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 9m40s
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 9m49s
Build Packages / build:viewer-tgz:cpu (push) Successful in 11m37s
Build Packages / build:viewer-tgz:cuda (push) Successful in 12m40s
Build Packages / build:windows:nocuda (push) Successful in 17m44s
Build Packages / build:windows:cuda (push) Successful in 20m13s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 14m41s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 15m5s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 14m35s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 15m53s
Build Packages / build:rugnux:windows (push) Successful in 11m29s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 18m51s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 18m43s
Build Packages / Generate python client (push) Successful in 51s
Build Packages / build:rpm (rocky8) (push) Successful in 18m51s
Build Packages / Build documentation (push) Successful in 1m21s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 18m38s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 18m24s
Build Packages / build:rpm (rocky9) (push) Successful in 19m19s
Build Packages / Unit tests (push) Successful in 1h37m15s
* Building Jungfraujoch no longer needs zlib or Eigen installed on the machine, and the dependencies the build fetches are pinned and updated to current releases. * rugnux: improvements in indexing, lattice selection and geometry post-refinement, which index crystals that previously returned no lattice and keep the better of the two geometries a run measures. * rugnux: improvements in beam-centre measurement, beam-stop detection and space-group determination. * rugnux: the unit cell reported with a determined space group now obeys that group - a cell whose symmetry was confirmed from the intensities is re-refined under it, and a cell the group cannot describe is reported with a warning rather than as it stands. * rugnux drops the stretches of a rotation sweep whose removal measurably improves the merged intensities and reports what became of every frame, and decides the resolution cut on the crystal's own diffraction rather than on its ice rings. * The rugnux results report is machine-readable - every line that is not `KEY= value` data starts with `#` - and states the build it was written by, its authorship and its terms of use (`REPORT_VERSION= 8`). * `jfjoch_viewer`: improvements in the file manager (CBF frames beside HDF5 datasets, a remembered root), the dataset plots, the inspector and the image statistics, plus a settable font size, a view of the rugnux results report, usable performance over a remote display (`ssh -X`) and a reset of all settings to defaults; the reciprocal-space window is removed. * Broker fixes around DECTRIS collections and dark-mask calibration: re-initialising after a run that never started no longer freezes the broker, a cancelled calibration is abandoned instead of reported as done, and a collection whose start message never arrives ends by itself. Reviewed-on: #79 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
132 lines
7.0 KiB
C++
132 lines
7.0 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <future>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
#include "../../common/CompressedImage.h"
|
|
#include "../../common/DiffractionExperiment.h"
|
|
#include "../../common/DiffractionGeometry.h"
|
|
#include "../../common/JFJochMessages.h"
|
|
#include "../../common/PixelMask.h"
|
|
#ifdef JFJOCH_USE_CUDA
|
|
#include "ShadowAccumulatorGPU.h"
|
|
#endif
|
|
|
|
// Finds the beam-stop shadow - the central disk and the holder arm - from a set of images,
|
|
// mirroring the accumulate-then-finalize shape of DarkMaskAnalysis: feed frames with
|
|
// AddImage(), then read the mask once with GetMask(). The mask is in converted geometry
|
|
// and is 1 where the beam stop shadows the detector.
|
|
//
|
|
// The shadow is a place where the background is missing, so it is found by comparing each
|
|
// pixel's mean against the typical background at the same radius - the median over its ring,
|
|
// taken over the pixels not already known to be shadowed. That comparison holds wherever the
|
|
// ring still has unshadowed pixels to measure. Where it does not - a ring lying wholly inside
|
|
// the stop - there is nothing to compare against, and such a ring is shadow in its entirety.
|
|
//
|
|
// The background belongs to the beam and the shadow to the stop, and the two are not concentric:
|
|
// the stop sits off the beam by a sizeable fraction of its own radius. Only the per-ring
|
|
// comparison is used, so nothing here assumes they share a centre.
|
|
//
|
|
// Frames are chosen by the caller; the detection needs enough of them that the background
|
|
// is counted rather than guessed (see MIN_EXPECTED_COUNTS in the .cpp).
|
|
// Thread-safe: workers call AddImage concurrently, each naming a shard of its own (see
|
|
// SetShardCount) - so no two threads touch the same accumulator and nothing is locked while
|
|
// an image is added. The shards are summed when the projection is read.
|
|
class ShadowFinder {
|
|
mutable std::mutex m;
|
|
|
|
const int width;
|
|
const int height;
|
|
float beam_x;
|
|
float beam_y;
|
|
|
|
// What the background owes to the source rather than to the hardware. The scattered background
|
|
// is not flat around a ring: a polarized source suppresses it in its own plane by a factor that
|
|
// reaches three at the 2 theta a short detector distance puts in a corner - several times the
|
|
// dip this class is looking for - so the comparison divides it out before it compares. Of the
|
|
// corrections a ring carries this is the only one that varies along it; solid angle, detector
|
|
// and air absorption are all functions of 2 theta alone and the ring's own median absorbs them.
|
|
// The geometry is kept whole rather than reduced to a distance and a pixel size because the
|
|
// azimuth is the whole point: detector tilt, a quarter-turned image and an in-plane rotation
|
|
// all move the polarization plane across the stored image, and the geometry already knows where
|
|
// it lies. Only the centre is replaced, by the one the caller measured.
|
|
const DiffractionGeometry geometry;
|
|
const std::optional<float> polarization; // unset leaves the background as it was measured
|
|
|
|
std::vector<uint32_t> pixel_mask; // pixels already masked carry no background to test
|
|
|
|
// Per-pixel projection over the frames added so far (converted geometry). One set per shard:
|
|
// the sums and counts are integers, so summing the shards is exact and the result does not
|
|
// depend on how the frames were spread over them.
|
|
struct Projection {
|
|
std::vector<int64_t> max_value;
|
|
std::vector<int64_t> sum_value;
|
|
std::vector<uint32_t> valid_count;
|
|
uint32_t frames = 0;
|
|
};
|
|
std::vector<Projection> shards;
|
|
|
|
#ifdef JFJOCH_USE_CUDA
|
|
// Present when a GPU is available. Frames it can decode are accumulated there instead of on the
|
|
// host - only the compressed chunk crosses PCIe - and its projection is folded in with the
|
|
// shards when the mask is read. Frames it cannot take (anything but bitshuffle+LZ4) still go to
|
|
// a host shard, so a run mixing compressions is handled without a second code path.
|
|
// Built on a thread of its own: it allocates and clears several hundred megabytes of device
|
|
// memory, and cudaMalloc synchronises the whole device, so doing it in the constructor would
|
|
// stall the caller before it has read its first frame. The first AddImage waits for it, by
|
|
// which time the reads have been running for a while.
|
|
mutable std::future<std::unique_ptr<ShadowAccumulatorGPU>> gpu_pending;
|
|
mutable std::unique_ptr<ShadowAccumulatorGPU> gpu;
|
|
mutable std::mutex gpu_mutex;
|
|
|
|
// The accumulator once its construction has finished, or null if there is none.
|
|
[[nodiscard]] ShadowAccumulatorGPU *Gpu() const;
|
|
#endif
|
|
|
|
template<class T> void Add(const T *ptr, Projection &p);
|
|
|
|
// Sum the shards into one projection. max_value is only taken from a shard that actually
|
|
// counted the pixel - a shard that never saw it holds 0, which would beat a genuinely
|
|
// negative maximum.
|
|
[[nodiscard]] Projection Reduce() const;
|
|
|
|
public:
|
|
ShadowFinder(const DiffractionExperiment &experiment, const PixelMask &mask);
|
|
|
|
// The centre the rings are drawn about. It starts as the file's, which is the only one there
|
|
// is when the finder is built; a caller that has measured one replaces it before reading the
|
|
// mask. A centre far from the truth draws the rings across the background's own radial
|
|
// fall-off instead of along it, and the comparison then describes the fall-off rather than the
|
|
// hardware. The projection is not centred on anything, so this may be set after the frames.
|
|
void BeamCenter(float x, float y);
|
|
|
|
// Give each worker a shard to accumulate into. Must be called before the first AddImage,
|
|
// and costs 20 bytes per pixel per shard.
|
|
void SetShardCount(size_t n);
|
|
|
|
// Accumulate one full converted-geometry image into shard `shard`. Gap / masked pixels
|
|
// (the pixel type's sentinel extreme) are skipped. `buffer` is scratch space for
|
|
// decompression, reused across the calls of one worker.
|
|
void AddImage(const DataMessage &data, std::vector<uint8_t> &buffer, size_t shard = 0);
|
|
|
|
// Compute the shadow mask (1 = shadow, 0 = keep), of the converted pixel count.
|
|
// Recomputed from the accumulators on each call - meant to be called once at the end.
|
|
// nthreads = 0 asks for all hardware threads. The per-pixel passes over a 16M-pixel detector
|
|
// dominate this, and they are all exactly parallel.
|
|
[[nodiscard]] std::vector<uint32_t> GetMask(size_t nthreads = 0) const;
|
|
|
|
// Mean counts per pixel over the frames added, NAN where nothing was counted. This is the
|
|
// projection GetMask() tests, so anything else that wants the background before indexing
|
|
// gets it without reading the frames a second time.
|
|
[[nodiscard]] std::vector<float> GetMeanProjection() const;
|
|
|
|
[[nodiscard]] uint32_t GetFrameCount() const;
|
|
};
|