Files
Jungfraujoch/image_analysis/geom_refinement/GeometryRefiner.h
T
leonarski_fandClaude Opus 5.5 ebce906025 Serial XFEL stills inputs: readers from the files themselves, module geometry and its refinement
Combines the stills reader and module work (stills-readers, stills-modgeom, stills-euxfel) onto
rc178 with the owner's decisions for this release:

- Inputs are files, never a directory; several files of one format are one dataset (the multi-input
  CLI of the previous commit), so every reader takes an explicit list: JFJochXFELReader::ReadFiles,
  ReadCXI, ReadPanelTable and JFJochXTCReader::ReadFiles take std::vector<std::string>; ListFiles and
  xtc::CollectStreams are gone, CanRead* accept a regular file only.
- One HDF5 frame reader (JFJochCrystFELReader -> JFJochXFELReader) parameterised by what the file holds:
  per-shot groups or a 3D stack, a /metadata panel table (posx/posy/angle, pixel size, nominal camera
  length, pixel mask: as SACLA writes), a Cheetah CXI single panel (square frame; a tiled slab is
  refused with a message). At most 8 files open; every file checked against the first. Signed 32-bit
  decode, per-frame 3D masks, panels at their own coffset (from the EuXFEL work).
- Frames stored one chunk each are read the way the NXmx reader reads its images: only the chunk's
  place is asked of HDF5 under its lock (H5Dget_chunk_info_by_coord), the bytes are read through
  HDF5ImageSource::RawFile (positional, no lock) and decoded outside it - deflate(+shuffle) by zlib,
  bitshuffle/LZ4 (and zstd, plain LZ4) by CompressedImage's decoders. Bitshuffle/LZ4 used to go through
  HDF5's filter under the global lock, one frame at a time.
- LCLS-I XTC (Rayonix): wavelength from the beam record, pixel size from the Rayonix model and
  binning, only --detector-distance needed; damaged/compressed frames skipped, a truncated last event
  dropped; std::ifstream, no POSIX calls: builds with MSVC. No DIALS .expt support at all.
- CrystFEL .geom: module geometry only (panel ranges, fs/ss, corners, pixel size, coffset, data path)
  and optionally the camera length; everything else is ignored with a note in the log.
- --mask-good / --mask-bad <file>:<dataset>: a mask array in another HDF5 file, in the frames' stored
  layout (what a .geom's mask_file used to give).
- Gain measured on the background for panel-table and CXI input that states none (--gain overrides).
- Detector modules from explicit geometry only (DetectorModules: the reader's panels, an NXmx file with
  one NXdetector_module per module, or --module-geometry); never from the image. Spots and predictions
  are mapped through the modules, the image is not re-assembled.
- Module refinement in the stills bundle adjustment (Ceres; per-module shift and in-plane turn, gauge
  on mean shift/turn/scale and on the two tilt-like patterns when the tilt is refined; adopted only
  where it predicts held-out frames better). Applied for a foreign multi-panel detector (panel table,
  .geom panels); for a JUNGFRAU measured and reported only. Written to <prefix>_modules.h5 (NXmx
  NXdetector_module groups), read back with --module-geometry.
- A panel-table file's camera length is nominal: with a reference cell it is refined from the cell.
- Broker and writer output unchanged: masters keep the single NXdetector_module group, the start
  message carries no module list.

Tests: [CrystFELGeometry], JFJochXFELReader_*, JFJochXTCReader, [JFJochMultiReader], [CellPopulation],
SaturationCeiling_*, [DetectorModules], RefineGlobalGeometry_ModulePlacement, [reindex], Sublattice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
2026-10-20 23:30:48 +02:00

115 lines
6.7 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <array>
#include <cstdint>
#include <vector>
#include "../../common/CrystalLattice.h"
#include "../../common/DiffractionGeometry.h"
#include "../../common/ModuleShift.h"
#include "../../common/UnitCell.h"
#include "gemmi/symmetry.hpp"
// Offline global (multi-frame) geometry refinement for serial stills. Pass 1 indexes many frames
// independently; this joint bundle adjustment then determines the ONE shared detector geometry (beam
// centre, distance, cell scale) that per-image refinement cannot reach from a single sparse still, so
// re-indexing with it converts more frames. Mirrors the validated Python prototype: one Ceres problem
// with shared beam/distance/cell blocks and a per-frame orientation block, robust loss, and a cell
// regularizer that anchors the (known) cell to break the distance<->cell-scale degeneracy at low
// resolution. Detector tilt (rot1/rot2) is NOT refined (gauge-coupled with the beam; zero indexing gain).
struct GeomRefineSpot {
float x = 0, y = 0; // observed (raw) spot position, pixels
int32_t h = 0, k = 0, l = 0; // integer Miller index assigned in pass 1
int16_t module = -1; // detector module the spot lies on (GeometryRefinerSettings::modules); -1: none
};
struct GeomRefineFrame {
CrystalLattice lattice; // per-frame indexed lattice (real space), for the orientation seed
std::vector<GeomRefineSpot> spots; // indexed spots on this frame
std::vector<GeomRefineSpot> unindexed; // the frame's other spots (no index); see ScanModuleShifts
int32_t ordinal = 0; // image this came from; ties are broken on it, so the
// selected bundle does not depend on collection order
};
struct GeometryRefinerSettings {
gemmi::CrystalSystem crystal_system = gemmi::CrystalSystem::Triclinic;
// Cell-scale regularizer strength (anchors the cell lengths to the input cell so the otherwise
// degenerate distance is determined). Large => cell effectively fixed (the validated safe regime).
double cell_reg_coeff = 30.0;
// With the distance held, the cell is no longer degenerate with it and is refined from the spots.
bool refine_distance = true;
int rounds = 3; // hkl-reassignment / tolerance-tightening rounds
double beam_range_px = 8.0; // beam bound around the nominal value
double distance_range_mm = 8.0; // distance bound around the nominal value
int min_spots_total = 100; // refuse to refine below this many indexed spots
int num_threads = 1;
// false: beam, distance and cell are held where nominal_geom and input_cell put them and only the
// crystal orientations follow - the fit quality of a geometry decided elsewhere, on frames it was
// not fitted to.
bool refine_beam_distance = true;
// The detector's modules, with where each is placed to start from; empty: the image is one rigid
// detector. With refine_modules, every module that has at least min_module_spots spots on its frames'
// lattices where it starts is moved as a rigid block (shift and turn in the detector plane); the others
// go back to where they were given (no move), whatever their start.
std::vector<ModuleShift> modules;
bool refine_modules = false;
int min_module_spots = 500;
// Refine the detector tilt (PONI rot1, rot2) with the beam and distance. A tilt moves the modules far
// from the beam more than those near it, in a pattern module moves could otherwise take up: with it
// refined, the modules are also held to no tilt-like pattern of their own (ModuleGauge).
bool refine_tilt = false;
};
struct GeometryRefinerResult {
bool ok = false;
double beam_x_px = 0, beam_y_px = 0; // refined beam centre (== direct beam, tilt not refined)
double distance_mm = 0;
UnitCell cell{}; // refined cell
int frames_used = 0;
int spots_used = 0;
double median_residual_px = 0; // final per-spot detector residual (fit quality)
// The same over EVERY spot of the frames, each spot's residual to its nearest predicted reflection
// capped at 2 px: unlike the median of the spots that fit, it does not improve by fitting fewer of them,
// so two geometries can be compared on the same spots.
double capped_rms_px = 0;
std::vector<ModuleShift> modules; // the modules' placement at the end (settings.modules if not refined)
double rot1_rad = 0, rot2_rad = 0; // detector tilt at the end (the nominal one where not refined)
// Per module (as settings.modules): rms of every spot's residual capped at 2 px, and the mean
// resolution of its spots - what a module's fit rests on.
std::vector<double> module_capped_rms_px;
std::vector<double> module_mean_d_A;
// Per frame, per spot (as frames[i].spots): where the refined geometry predicts the spot's reflection on
// the detector, as the spot would be found (before its module's move); NaN where it predicts none.
std::vector<std::vector<std::array<float, 2>>> predicted_px;
};
// nominal_geom supplies the starting beam/distance/tilt/wavelength/pixel size; input_cell is anchored by
// the regularizer. Returns ok=false (and leaves the caller's geometry untouched) on any failure.
GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_geom,
const UnitCell &input_cell,
const std::vector<GeomRefineFrame> &frames,
const GeometryRefinerSettings &settings);
// A coarse search for where each module is, ahead of RefineGlobalGeometry: a module misplaced by more than a
// fraction of the spot spacing has its spots left unindexed (or indexed wrongly), so the refinement has
// nothing to pull it with. Each module is shifted over a grid of +-range_px, and at each shift every spot on
// it (indexed or not) is scored against its frame's lattice as indexed: the shift that puts the most spots on
// the lattices wins. The frames' lattices come from the other modules, so this works while most of the
// detector is roughly right. Turns are not searched.
struct ModuleScan {
double dx = 0, dy = 0; // the best shift found
int on_lattice_at_zero = 0;
int on_lattice_at_best = 0;
};
std::vector<ModuleScan> ScanModuleShifts(const DiffractionGeometry &geom,
const std::vector<GeomRefineFrame> &frames,
const std::vector<ModuleShift> &modules,
double range_px);