Files
Jungfraujoch/image_analysis/MXAnalysisWithoutFPGA.h
T
leonarski_f e97b902603 Beam stop: a reflection whose signal disk reaches the shadow is not integrated; no-data pixels are dead, not shadow
The profile fit keeps a signal disk that lost pixels to the mask and fits the pixels that remain. That
holds for a gap or a dead pixel, but not for the beam-stop shadow: the mask is drawn where the background
is dimmed, and the Bragg half-shadow does not end there, so the rest of such a disk is attenuated too.
Measured on summed rocking events against their symmetry mates (stratified by resolution and zeta, the
detector edge excluded), observations whose disk touches the shadow read low by 12-60 % on 5uth, 7qij,
5nw5, 3r6o, 3meb (CCD and PILATUS alike) and level on the in-house photon-counting sweeps; observations
with a clean disk are unbiased except on 5nw5, whose half-shadow reaches ~20 px past the mask. The test
sits in MXAnalysisWithoutFPGA::IntegrateOnImage, so rotation, stills, the _process.h5 and --mode scale see
the same reflections; with no shadow in the mask (the broker) it does nothing.

The shadow finder also reports pixels holding a detector's no-data mark (PILATUS CBF module gaps at -1):
they are the darkest pixels there are. The pre-scan now masks those as dead (bit 10) instead of beam-stop
shadow (bit 9), so that the shadow test does not drop every reflection clipped by a module gap and the
reported shadow share is the shadow's. A mean of zero is not taken as dead: a deep shadow on a still
counts nothing in the projected frames.

CPU subset battery vs rc180 (REFMAC 0-cycle R-free): 3meb -0.0004, 5uth -0.0004, 6h2p -0.0003,
6oel -0.0002, 3r6o +0.0001, 7qij/7rji 0, 5nw5 +0.002 (its lowest-shell CC1/2 0.78 -> 0.89, ISa 8.4 -> 9.3);
in-house refres ISa up 0.1-0.3 on lyso/thau/insu/cytc/myob, half_image refres R_meas 75 -> 64 %, no set
lost quality. Stills (lcls242_s/t, lcls222, euxfel83, sacla236): R-free within +-0.002, the noise of a
perturbed run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
(cherry picked from commit a7610d847694b5540dacc9ef609344ea7c7ae8fd)
2026-10-25 07:20:21 +01:00

140 lines
7.7 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <mutex>
#include "../common/JFJochMessages.h"
#include "../common/DiffractionExperiment.h"
#include "../common/AzimuthalIntegrationMapping.h"
#include "../common/PixelMask.h"
#include "../common/AzimuthalIntegrationProfile.h"
#include "bragg_prediction/BraggPrediction.h"
#include "bragg_integration/BraggIntegrationEngine.h"
#include "bragg_integration/BraggIntegrationEngineCPU.h"
#include "spot_finding/ImageSpotFinder.h"
#include "spot_finding/AdaptiveSpotFinderCPU.h"
#include "indexing/IndexerThreadPool.h"
#include "azint/AzIntEngine.h"
#include "roi/ROIIntegration.h"
#include "IndexAndRefine.h"
#include "image_preprocessing/ImagePreprocessor.h"
#include "image_preprocessing/ImagePreprocessorBuffer.h"
#include "image_preprocessing/ImagePreprocessorCPU.h"
class CudaStream;
class AdaptiveSpotFinderGPU;
// Leave out the reflections whose signal disk - radius r1 about the predicted position, in image
// coordinates - reaches a pixel of the beam-stop shadow (PixelMask::BeamStopPixelBit of `mask`, a
// width x height image).
void DropShadowClipped(std::vector<Reflection> &reflections, const std::vector<uint32_t> &mask,
int width, int height, float r1);
// MXAnalysisWithoutFPGA is not thread safe - it has to owned by a single thread
class MXAnalysisWithoutFPGA {
const DiffractionExperiment &experiment;
const AzimuthalIntegrationMapping &integration;
std::vector<uint8_t> decompression_buffer;
std::unique_ptr<ImagePreprocessor> preprocessor;
// The preprocessor, where it is the CPU one.
ImagePreprocessorCPU *preprocessor_cpu = nullptr;
size_t npixels;
size_t xpixels;
// Built on first use: the fused adaptive finder produces the azimuthal profile as a by-product,
// so on the rugnux path this engine is constructed and then never run.
std::unique_ptr<AzIntEngine> azint;
AzIntEngine &AzInt();
std::unique_ptr<ROIIntegration> roi;
// Built on first use. Which finder an image takes arrives with its SpotFindingSettings, and
// with adaptive detection on - the default everywhere but the broker - this one is never asked
// for; on the GPU it is ~14 MB and 15 device allocations per worker.
std::unique_ptr<ImageSpotFinder> spotFinder;
ImageSpotFinder &FixedThresholdFinder();
// Self-calibrating finder, used when spot settings request adaptive detection. Kept alongside the
// default finder because the choice arrives with the per-image settings, not at construction. It is
// an AdaptiveSpotFinderCPU by default; on the GPU path, when the fused engine is enabled (rugnux
// offline only), it is instead an AdaptiveSpotFinderGPU that also computes the azimuthal profile,
// aliased through fused_adaptive so Analyze() can take that profile and skip the separate azint pass.
std::unique_ptr<ImageSpotFinder> adaptiveSpotFinder;
AdaptiveSpotFinderGPU *fused_adaptive = nullptr;
// The CPU finder, where it gives the profile too (the azimuthal integration being on the CPU).
AdaptiveSpotFinderCPU *fused_adaptive_cpu = nullptr;
const bool enable_fused_adaptive_gpu;
IndexAndRefine &indexer;
std::unique_ptr<BraggPrediction> prediction;
std::unique_ptr<BraggIntegrationEngine> bragg_engine;
// The engine run on reflections predicted on the detector plane: each read where its module put it on
// the image (DetectorModules), handed back on the plane.
std::vector<Reflection> IntegrateOnImage(const std::vector<Reflection> &predicted, size_t npredicted,
int64_t image_number);
// What the supercell probe's integrations added to bragg_engine's counts (see BraggCounts).
BraggIntegrationCounts probe_counts;
// Counts the neighbour crowding of the integrated reflections at another stencil (CountCrowdingAt),
// on the images for which count_crowding is on.
std::unique_ptr<BraggIntegrationEngineCPU> crowding_engine;
bool count_crowding = false;
std::unique_ptr<ImagePreprocessorBuffer> preprocessor_buffer;
const PixelMask &mask;
// Decompress the image into decompression_buffer (or read it straight from the message, when it is
// not compressed) and return where it landed.
const uint8_t *Decompress(const CompressedImage &image);
// The CPU preprocessing. A bitshuffled image is decoded a block (~32 KB) at a time and each block
// is preprocessed while it is in cache, then - with ring_pass - put through the adaptive finder's
// plain ring pass as well, so the image is never written out whole before it is preprocessed and
// the preprocessed pixels are read back from cache, not from memory. The blocks come in pixel
// order, so every sum is taken in the same order as by separate passes.
ImageStatistics PreprocessCPU(const CompressedImage &image, bool ring_pass);
// Pixels outside the resolution limits, bit-packed. Built by the integration mapping, which is
// shared by every worker's engine and hands out the same mask to all of them.
std::shared_ptr<const std::vector<uint32_t>> mask_resolution;
// The limits mask_resolution was built for. Kept as the OPTIONAL the caller passed, so an unset
// high-resolution limit compares equal to itself and the mask is not rebuilt on every image.
std::optional<float> mask_high_res;
std::optional<float> mask_low_res;
void UpdateMaskResolution(const SpotFindingSettings& settings);
#ifdef JFJOCH_USE_CUDA
std::shared_ptr<CudaStream> stream; // kept so RebuildROI() can recreate the GPU ROI engine
#endif
public:
// enable_fused_adaptive_gpu turns on the fused GPU azint+adaptive spot finder (only takes effect on
// the GPU path with adaptive detection). The rugnux offline path and the interactive viewer enable
// it by default, as does the online receiver. It only changes performance - the fused engine
// reproduces the CPU finder's spots. Note it also decides whether the preprocessed image is copied
// back to the host each frame: that copy exists only for a CPU engine to read, and with the flag on
// no CPU engine is built, so the copy is skipped.
MXAnalysisWithoutFPGA(const DiffractionExperiment &experiment, const AzimuthalIntegrationMapping &integration,
const PixelMask &mask, IndexAndRefine &indexer, bool enable_fused_adaptive_gpu = false);
void Analyze(DataMessage &output, AzimuthalIntegrationProfile &profile, const SpotFindingSettings &spot_finding_settings);
// Surgical ROI-only paths used when a full re-analysis is not wanted: rebuild the
// ROI engine after the ROI set changes, recompute ROIs after preprocessing a new
// image (reanalyze off), or just rerun ROIs on the current preprocessed image (an
// interactive ROI move). A full Analyze() already computes ROIs, so needs nothing.
void RebuildROI();
void AnalyzeROIOnly(DataMessage &output);
void RunROIOnly(DataMessage &output);
// What this worker's Bragg integrator counted (BraggIntegrationCounts). Each worker builds its own
// analysis, so a caller that wants the run's totals sums this over the workers it started.
[[nodiscard]] BraggIntegrationCounts BraggCounts() const;
// Also count, on the images Analyze is given while CountCrowding(true), how crowded the background
// rings of the integrated reflections would be at `settings` rather than the experiment's own
// (BraggIntegrationEngineCPU::CountNeighbourCrowding), read back with CrowdingCounts.
void CountCrowdingAt(const BraggIntegrationSettings &settings);
void CountCrowding(bool on) { count_crowding = on; }
[[nodiscard]] BraggIntegrationCounts CrowdingCounts() const;
};