Weak/jet serial stills are often geometry-limited: a few-px beam error or a mm-scale detector distance error (unreachable per-image) fails many frames, but is well-determined jointly from the strong frames. Mirror the rotation two-pass: an index-only first pass over a spread sample collects each indexed frame's spots + assigned HKL + orientation; the strongest ~N (default 200) feed one Ceres bundle adjustment; the refined geometry is applied and the main pass re-indexes + integrates + merges every frame from scratch. GeometryRefiner (reusing the extracted XtalResidual - the RecipToDetector geometry residual pulled out of XtalOptimizer, behaviour-preserving): one problem with SHARED beam(2)/distance(1)/cell-length(3) blocks + a PER-FRAME orientation(3) block, robust Cauchy loss, a cell-length regularizer anchoring the known cell to break the low-resolution distance<->cell-scale degeneracy, DENSE_SCHUR eliminating the per-frame orientations, and a 3-round HKL-reassignment / tolerance-tightening loop. Tilt is not refined (gauge-coupled, zero gain). Opt-in via --refine-geometry[=N]; stills only (rotation untouched). Validated: KR2 7.58% -> 21.85% (matches CrystFEL's 21.5%; a real ~1.4mm distance error + ~3px beam), OCP 2.92% -> 4.19% (~3px beam). OFF runs are bit-identical to baseline (XtalResidual extraction non-regressing; rotation lyso_ref de-novo ISa 17.3 unchanged). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
60 lines
3.0 KiB
C++
60 lines
3.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 <vector>
|
|
|
|
#include "../../common/CrystalLattice.h"
|
|
#include "../../common/DiffractionGeometry.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
|
|
};
|
|
|
|
struct GeomRefineFrame {
|
|
CrystalLattice lattice; // per-frame indexed lattice (real space), for the orientation seed
|
|
std::vector<GeomRefineSpot> spots; // indexed spots on this frame
|
|
};
|
|
|
|
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;
|
|
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;
|
|
};
|
|
|
|
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)
|
|
};
|
|
|
|
// 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);
|