Post-refinement scaled the pass-1 lattice by one scalar against the observed
rocking angles (step A), then read the detector distance off that scaled cell
(step B). The split was there because the positional residual is degenerate
with the cell scale - which is true of the positions ALONE, and is exactly
what the excitation residual already computed in the same struct breaks. So
the degeneracy that motivated the split was resolvable inside one problem all
along, and splitting it manufactured error twice over:
* Pass 1 frees the whole lattice against a frozen distance, so what it
absorbs of a header distance error is ANISOTROPIC (0.16 % on the crystal
measured), and no single scale can undo it.
* Whatever bias is left in that scale goes straight into the distance, since
the distance is only ever determined relative to the cell. Over six runs
spanning three wavelengths of one crystal, distance error (%) = 1.33 x
step-A bias (%/axis), r = 0.9947 - and the bias changed SIGN with the
wavelength (-0.067 % at 1.9 A, +0.049 % at 2.7 A, +0.19 % at 3.3 A),
reproducing the whole wavelength ordering of the distance error.
The two steps' own printed numbers say it, on 112 of the corpus's datasets.
Comparing each run's held-out POSITIONAL residual at the unrefined cell with
the same residual at step A's scaled cell and at step B's committed geometry:
step A leaves it WORSE than the unrefined cell on 110 of 112 (median 3.41x),
and step B - which does fit that residual - cannot get back: its committed
value is still worse than the unrefined cell on 110 of 112 (median 2.97x).
Step B was improving on a baseline step A had corrupted. This part of the
measurement is of the old code alone and owes nothing to the replacement.
Now one Ceres problem: free crystal orientation, the cell (every parameter
the crystal system leaves free), goniometer axis, detector distance and beam
centre; residuals are the positional detector<->reciprocal one at each
partial's observed spot and the distance-independent Ewald excitation one at
each rocking centroid. Same deterministic hkl-hash split-half gate as before,
with the move bounded: distance and every cell length within 1 %, beam within
the existing bound. Detector tilt stays fixed (gauge-coupled to the
orientation on one crystal). Committed, it leaves the held-out positional
residual BELOW the unrefined cell's on the same 110 of 112 (median 0.90x) -
though that residual is half its own objective, so read that line as a
sanity check, not as an independent arbiter. The rotation-scale fit that
followed step A now runs on the jointly committed lattice and axis; it is
unchanged otherwise and reproduces its previous k to five digits.
MEASURED. Three wavelengths of one rotation crystal, scored against an
independent determination of the cell (unconstrained joint fit to XDS spot
centroids) and against XDS's own refined cell:
set cell error vs XDS committed distance R_meas ISa
1.9 A -0.048 % -> -0.041 % 60.274 -> 60.279 mm .0725/.0725 31.2/31.1
2.7 A +0.057 % -> -0.058 % 60.360 -> 60.266 mm .0681/.0676 25.8/26.3
3.3 A +0.230 % -> -0.033 % 60.491 -> 60.267 mm .1193/.0835 9.2/14.9
The three committed distances now agree with each other to 0.013 mm and with
the external value (60.26 +- 0.04) instead of drifting 0.22 mm with the
wavelength. Anomalous peak heights at the known sulphur sites rise from
7.82/8.18 to 10.09/9.04 - the best previously reachable state was 10.08/9.10
and required knowing the answer.
The bistability documented on the long-wavelength set is gone. Sweeping the
header distance over 1.0 mm, the two-step commits 60.49-60.54 however it
starts (or refuses outright, losing the dataset: R_meas 0.276, ISa 2.9); the
joint fit commits 60.256-60.313, tracking the truth, and lands in the correct
cell basin every time. Same from two starting beam centres.
CORPUS. 115 datasets, paired against the tip, 113 processed in both arms:
* cell deviation from the reference: 58 better, 38 worse, 16 unchanged
(sign test p = 0.052); median 0.171 % -> 0.128 %. The asymmetry is in the
magnitudes: the largest improvement is 2.4 percentage points, the largest
regression 0.125. The 24.4-point figure at the head of that list is not a
cell change at all - it is a hexagonal lattice the base arm described in
its C-centred orthorhombic setting, which a sorted-axis comparison reports
as a large difference; the scorer's own cause for that dataset is an
under-called point group, and the primitive volume ratio passed in both
arms.
* POINT GROUP == reference 105 -> 106. Exact space group 82 -> 81: one
screw-axis call (P2 -> P2_1) on a crystal whose R_meas is above 139 %,
where the cell itself improved.
* V ratio in [0.97, 1.03]: 106 -> 106. Datasets processed: 113 -> 113.
* R_meas and ISa are confounded here: of the nine material regressions,
seven reached a FINER resolution limit, which raises R_meas and lowers
ISa mechanically. The two that did not are small (+0.55 pp R_meas, -0.9
ISa). One unconfounded rescue is large - R_meas 105 % -> 27 % at 0.72 ->
0.51 A on a small-molecule set whose uniform scale step A had refused.
Cost: 90-150 ms on runs of minutes. Bit-identical across repeat runs.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFEJG6WBQv8th4UJFNe53N
77 lines
4.8 KiB
C++
77 lines
4.8 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 <optional>
|
|
#include <vector>
|
|
|
|
#include "../../common/DiffractionGeometry.h"
|
|
#include "../../common/CrystalLattice.h"
|
|
#include "../../common/GoniometerAxis.h"
|
|
#include "../../common/UnitCell.h"
|
|
#include "../../common/Logger.h"
|
|
#include "../IntegrationOutcome.h"
|
|
#include "gemmi/symmetry.hpp"
|
|
|
|
// Post-integration geometry refinement for rotation data. Unlike the at-indexing XtalOptimizer, this runs
|
|
// AFTER integration/merge, where each reflection has an OBSERVED rocking centroid phi_obs (the intensity-
|
|
// weighted mean goniometer angle over the frames it spans) and an observed spot position. One JOINT fit
|
|
// refines the crystal (orientation, cell, rotation axis) and the detector (distance, beam centre) together
|
|
// against two residuals:
|
|
// * an Ewald excitation residual evaluated at phi_obs (distance-independent) -> pins the absolute cell
|
|
// scale that the positional residual leaves degenerate with the distance. Because phi_obs is the real
|
|
// rocking angle (not a frame centre) it is unbiased.
|
|
// * the positional detector<->reciprocal residual at each partial's observed spot -> pins the distance.
|
|
// Where there are more reflections than the fit's caps, the strongest by I/sigma are the ones kept;
|
|
// inside the fit every reflection carries the same weight.
|
|
|
|
struct PostRefineResult {
|
|
bool ok = false;
|
|
DiffractionGeometry geom; // refined (distance / beam left as configured)
|
|
UnitCell cell{}; // refined unit cell
|
|
int events_used = 0;
|
|
int obs_used = 0;
|
|
double distance_before_mm = 0.0, distance_after_mm = 0.0;
|
|
double beam_x_before_px = 0.0, beam_x_after_px = 0.0; // refined beam centre (GEOM mode)
|
|
double beam_y_before_px = 0.0, beam_y_after_px = 0.0;
|
|
// The joint fit is one decision - the crystal and the detector are refined together and committed
|
|
// together - so these two are always equal. Both are kept because the report names them separately.
|
|
bool cell_refined = false; // the joint fit passed cross-validation (crystal half)
|
|
bool detector_refined = false; // the joint fit passed cross-validation (detector half)
|
|
// GONIOMETER ROTATION SCALE: the factor by which the stage actually turned relative to the angle
|
|
// stored in the file (which is the COMMANDED value, hence a stage calibration error is invisible in
|
|
// the header). Fitted after the joint fit as a single free parameter, with the crystal and the axis
|
|
// direction held at their committed values. Always the fitted value; 1.0 = header and stage agree.
|
|
double rotation_scale = 1.0;
|
|
// Whether the fit passed every test needed to ACT on it: enough sweep and events, a significant and
|
|
// physically relevant size, and the same k from every fifth of the sweep. Only then is it applied.
|
|
bool rotation_scale_suspect = false;
|
|
};
|
|
|
|
struct PostRefineSettings {
|
|
gemmi::CrystalSystem crystal_system = gemmi::CrystalSystem::Triclinic;
|
|
bool refine_geometry = false; // XtalOptimizer-equivalent: the crystal (from phi_obs and the observed
|
|
// spot positions) and the detector distance + beam centre, in one
|
|
// cross-validated fit. The only supported refinement mode.
|
|
double excitation_weight = 1.0; // weight of the phi/excitation residual vs the positional one
|
|
int min_events = 50;
|
|
int num_threads = 1;
|
|
// An independent measurement of the beam centre in pixels, where the run has one (rugnux's pre-scan
|
|
// fit to the isotropy of the scattered background, which runs on every rotation run). The fit bounds
|
|
// how far it may move the beam from whichever of this and the nominal centre is NEARER; see
|
|
// BEAM_BOUND_PXL in PostRefine.cpp for why the nominal centre alone is not enough to bound it.
|
|
std::optional<std::array<double, 2>> measured_beam_px;
|
|
};
|
|
|
|
// nominal_geom / reference_latt: the current detector geometry and the phi=0 reference lattice (orientation
|
|
// + cell) from rotation indexing. outcomes: the per-image integrated reflections (observed_x/y, I, sigma,
|
|
// image_number). axis: the goniometer. Returns ok=false (geometry untouched) on failure.
|
|
PostRefineResult PostRefineRotationGeometry(const std::vector<IntegrationOutcome> &outcomes,
|
|
const GoniometerAxis &axis,
|
|
const DiffractionGeometry &nominal_geom,
|
|
const CrystalLattice &reference_latt,
|
|
const PostRefineSettings &settings,
|
|
Logger &logger);
|