Files
Jungfraujoch/tests/DetectorModulesTest.cpp
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

113 lines
4.6 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 <filesystem>
#include "../common/DetectorGeometryModular.h"
#include "../common/DetectorModules.h"
#include "../common/DiffractionExperiment.h"
#include "../common/JFJochMath.h"
#include "../reader/NXmxModules.h"
#include "../writer/NXmxModules.h"
// Two JUNGFRAU modules side by side, 8 pixels apart: each a 1030 x 514 box of the assembled image, placed
// as imaged, so every pixel maps onto itself.
TEST_CASE("DetectorModules_JungfrauAsImaged", "[DetectorModules]") {
const DetectorGeometryModular geom(2, 2, 8, 36, false);
const DetectorModules modules = ModulesAsImaged(geom);
REQUIRE(modules.size() == 2);
CHECK(modules[0].x0 == 0);
CHECK(modules[1].x0 == 1038);
for (const auto &m: modules.Get()) {
CHECK(m.width == 1030);
CHECK(m.height == 514);
}
CHECK_FALSE(modules.Moved());
CHECK(modules.ModuleOfImagePixel(1033, 100) == -1); // in the gap
CHECK(modules.ModuleOfImagePixel(1040, 100) == 1);
const auto [px, py] = modules.ImageToPlane(1040.25, 100.5);
CHECK(px == Catch::Approx(1040.25));
CHECK(py == Catch::Approx(100.5));
// Not modular: no modules.
CHECK(ModulesAsImaged(DetectorGeometryModular(1)).empty());
}
// A module moved by a rigid step: the image -> plane map moves its pixels, and plane -> image undoes it.
TEST_CASE("DetectorModules_Apply", "[DetectorModules]") {
DetectorModules modules({DetectorModule::AsImaged(0, 0, 100, 50), DetectorModule::AsImaged(108, 0, 100, 50)});
const auto centres = modules.Centres();
std::vector<ModuleShift> step = centres;
step[1].dx = 1.5;
step[1].dy = -0.25;
step[1].rot_rad = 0.2 * PI / 180.0;
modules.Apply(step);
CHECK(modules.Moved());
// Module 0 is where it was.
const auto [ax, ay] = modules.ImageToPlane(10, 10);
CHECK(ax == Catch::Approx(10));
CHECK(ay == Catch::Approx(10));
// Module 1's centre moved by the shift, a corner by the shift and the turn.
const auto [cx, cy] = modules[1].PlaneCentre();
CHECK(cx == Catch::Approx(centres[1].x0 + 1.5));
CHECK(cy == Catch::Approx(centres[1].y0 - 0.25));
const double c = std::cos(step[1].rot_rad), s = std::sin(step[1].rot_rad);
const double ux = 108 - centres[1].x0, uy = 0 - centres[1].y0;
const auto [qx, qy] = modules.ImageToPlane(108, 0);
CHECK(qx == Catch::Approx(centres[1].x0 + c * ux - s * uy + 1.5));
CHECK(qy == Catch::Approx(centres[1].y0 + s * ux + c * uy - 0.25));
// And back: a plane point inside module 1's footprint goes to the image pixel that put it there.
const auto [ix, iy] = modules.PlaneToImage(qx, qy);
CHECK(ix == Catch::Approx(108).margin(1e-9));
CHECK(iy == Catch::Approx(0).margin(1e-9));
const auto [jx, jy] = modules.PlaneToImage(modules.ImageToPlane(150.3, 20.7).first,
modules.ImageToPlane(150.3, 20.7).second);
CHECK(jx == Catch::Approx(150.3));
CHECK(jy == Catch::Approx(20.7));
}
// Written as NXdetector_module groups and read back, the modules come back as they were - here two
// JUNGFRAU modules, one of them moved off the image's grid, on a detector stored mirrored.
TEST_CASE("DetectorModules_NXmxRoundTrip", "[DetectorModules]") {
DetectorModules modules = ModulesAsImaged(DetectorGeometryModular(2, 2, 8, 36, false));
auto step = modules.Centres();
step[0].dx = -0.7;
step[0].dy = 0.35;
step[0].rot_rad = -0.05 * PI / 180.0;
modules.Apply(step);
DiffractionGeometry geom;
geom.BeamX_pxl(1100).BeamY_pxl(250).DetectorDistance_mm(120).PixelSize_mm(0.075)
.Orientation(DetectorOrientation(true, 1));
const std::string path = "detector_modules_roundtrip.h5";
WriteModuleGeometryFile(path, modules, geom);
DetectorModules back;
{
HDF5ReadOnlyFile file(path);
back = ReadNXmxModules(file, 0.075e-3, geom.GetOrientation());
}
std::filesystem::remove(path);
REQUIRE(back.size() == 2);
for (size_t i = 0; i < 2; i++) {
const auto &a = modules[i], &b = back[i];
CHECK(b.x0 == a.x0);
CHECK(b.y0 == a.y0);
CHECK(b.width == a.width);
CHECK(b.height == a.height);
CHECK(b.ox == Catch::Approx(a.ox).margin(1e-3));
CHECK(b.oy == Catch::Approx(a.oy).margin(1e-3));
CHECK(b.fx == Catch::Approx(a.fx).margin(1e-6));
CHECK(b.fy == Catch::Approx(a.fy).margin(1e-6));
CHECK(b.sx == Catch::Approx(a.sx).margin(1e-6));
CHECK(b.sy == Catch::Approx(a.sy).margin(1e-6));
}
}