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
95 lines
4.1 KiB
C++
95 lines
4.1 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <catch2/catch_all.hpp>
|
|
#include <cmath>
|
|
#include <random>
|
|
|
|
#include "../common/JFJochMath.h"
|
|
#include "../image_analysis/geom_refinement/GeometryRefiner.h"
|
|
#include "../image_analysis/bragg_prediction/BraggPrediction.h"
|
|
|
|
// A detector of four modules (the quadrants around the beam), each displaced by a known rigid move: the
|
|
// bundle adjustment with module placement must find the moves, and the fit without it must be worse.
|
|
TEST_CASE("RefineGlobalGeometry_ModulePlacement") {
|
|
DiffractionExperiment exp;
|
|
exp.IncidentEnergy_keV(WVL_1A_IN_KEV).BeamX_pxl(1000).BeamY_pxl(1000).DetectorDistance_mm(150);
|
|
const DiffractionGeometry geom = exp.GetDiffractionGeometry();
|
|
|
|
// Moves that hold none of what the gauge takes away: zero in sum, in turn and outwards.
|
|
std::vector<ModuleShift> truth(4);
|
|
const double centre[4][2] = {{500, 500}, {1500, 500}, {500, 1500}, {1500, 1500}};
|
|
const double move[4][3] = {{2.0, -2.0, 0.2}, {2.0, 2.0, -0.2}, {-2.0, -2.0, 0.1}, {-2.0, 2.0, -0.1}};
|
|
for (int m = 0; m < 4; m++) {
|
|
truth[m].x0 = centre[m][0];
|
|
truth[m].y0 = centre[m][1];
|
|
truth[m].dx = move[m][0];
|
|
truth[m].dy = move[m][1];
|
|
truth[m].rot_rad = move[m][2] * PI / 180.0;
|
|
}
|
|
auto module_of = [](double x, double y) { return (x < 1000 ? 0 : 1) + (y < 1000 ? 0 : 2); };
|
|
|
|
const CrystalLattice latt(50, 60, 70, 90, 90, 90);
|
|
BraggPredictionSettings settings{.high_res_A = 2.0, .ewald_dist_cutoff = 0.001};
|
|
std::mt19937 rng(7);
|
|
std::uniform_real_distribution<float> u(-1.0f, 1.0f);
|
|
|
|
std::vector<GeomRefineFrame> frames;
|
|
for (int f = 0; f < 60; f++) {
|
|
const Coord axis = Coord(u(rng), u(rng), u(rng)).Normalize();
|
|
const CrystalLattice rotated = latt.Multiply(RotMatrix(static_cast<float>(PI) * u(rng), axis));
|
|
BraggPrediction prediction;
|
|
const int n = prediction.Calc(exp, rotated, settings);
|
|
GeomRefineFrame frame;
|
|
frame.lattice = rotated;
|
|
frame.ordinal = f;
|
|
for (int i = 0; i < n; i++) {
|
|
const auto &r = prediction.GetReflections().at(i);
|
|
const int m = module_of(r.predicted_x, r.predicted_y);
|
|
// where the image shows a spot whose true place is the prediction: the inverse of the move
|
|
const auto &t = truth[m];
|
|
const double c = std::cos(t.rot_rad), s = std::sin(t.rot_rad);
|
|
const double vx = r.predicted_x - t.x0 - t.dx, vy = r.predicted_y - t.y0 - t.dy;
|
|
GeomRefineSpot spot;
|
|
spot.x = static_cast<float>(t.x0 + c * vx + s * vy);
|
|
spot.y = static_cast<float>(t.y0 - s * vx + c * vy);
|
|
spot.h = r.h;
|
|
spot.k = r.k;
|
|
spot.l = r.l;
|
|
spot.module = static_cast<int16_t>(m);
|
|
frame.spots.push_back(spot);
|
|
}
|
|
if (frame.spots.size() >= 20)
|
|
frames.push_back(frame);
|
|
}
|
|
REQUIRE(frames.size() > 30);
|
|
|
|
GeometryRefinerSettings st;
|
|
st.crystal_system = gemmi::CrystalSystem::Orthorhombic;
|
|
st.num_threads = 2;
|
|
for (const auto &t: truth) {
|
|
ModuleShift start;
|
|
start.x0 = t.x0;
|
|
start.y0 = t.y0;
|
|
st.modules.push_back(start);
|
|
}
|
|
|
|
const auto held = RefineGlobalGeometry(geom, latt.GetUnitCell(), frames, st);
|
|
REQUIRE(held.ok);
|
|
|
|
st.refine_modules = true;
|
|
const auto placed = RefineGlobalGeometry(geom, latt.GetUnitCell(), frames, st);
|
|
REQUIRE(placed.ok);
|
|
CHECK(placed.median_residual_px < 0.1);
|
|
CHECK(held.median_residual_px > 5 * placed.median_residual_px);
|
|
|
|
for (int m = 0; m < 4; m++) {
|
|
CHECK(placed.modules[m].dx == Catch::Approx(truth[m].dx).margin(0.1));
|
|
CHECK(placed.modules[m].dy == Catch::Approx(truth[m].dy).margin(0.1));
|
|
CHECK(placed.modules[m].rot_rad * 180.0 / PI == Catch::Approx(move[m][2]).margin(0.01));
|
|
}
|
|
CHECK(placed.beam_x_px == Catch::Approx(1000).margin(0.1));
|
|
CHECK(placed.beam_y_px == Catch::Approx(1000).margin(0.1));
|
|
CHECK(placed.distance_mm == Catch::Approx(150).margin(0.05));
|
|
}
|