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
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-20 23:30:48 +02:00
1 parent 6557ba87f4
commit ebce906025
46 files changed
+3686 -936

No files matched your search

+60 -50
View File
@@ -4836,29 +4836,44 @@ TEST_CASE("JFJochReader_DECTRISMultichannel", "[HDF5][Full]") {
}
// LCLS-I XTC: datagrams of a 40-byte header and a tree of Xtcs; a camera image is a Camera::FrameV1
// leaf. The fixture writes a Configure and two L1Accepts. The first carries a second, smaller camera
// beside the diffraction detector, which the reader must pass over; the geometry comes from a
// one-panel DIALS experiment list beside the stream.
// leaf. The fixture writes a Configure carrying a Rayonix configuration, and L1Accepts with the
// electron beam's photon energy. The first event carries a second, smaller camera beside the
// diffraction detector, which the reader must pass over; one event's image is damaged, and the stream
// ends inside a last event, as a partly downloaded file does.
namespace {
void PutWords(std::string &s, std::initializer_list<uint32_t> words) {
for (uint32_t w: words)
s.append(reinterpret_cast<const char *>(&w), 4);
}
std::string XtcLeaf(uint32_t type, const std::string &payload) {
std::string XtcLeaf(uint32_t type, const std::string &payload, uint32_t source = 0, uint32_t damage = 0) {
std::string s;
PutWords(s, {0, 0, 0, type, static_cast<uint32_t>(20 + payload.size())});
PutWords(s, {damage, 0, source, type, static_cast<uint32_t>(20 + payload.size())});
return s + payload;
}
std::string XtcFrame(uint32_t width, uint32_t height, uint16_t base) {
std::string XtcFrame(uint32_t width, uint32_t height, uint16_t base, uint32_t source, uint32_t damage = 0) {
std::string p;
PutWords(p, {width, height, 16, 0});
PutWords(p, {width, height, 16, 3}); // an offset of 3 counts, taken off when read
for (uint32_t i = 0; i < width * height; i++) {
const uint16_t v = base + i;
const uint16_t v = base + i % 100;
p.append(reinterpret_cast<const char *>(&v), 2);
}
return XtcLeaf(2 | (1u << 16), p);
return XtcLeaf(2 | (1u << 16), p, source, damage);
}
std::string XtcEBeam(double photon_energy_eV) {
std::string p(4 + 20 * 8, '\0');
std::memcpy(p.data() + 4 + 17 * 8, &photon_energy_eV, 8);
return XtcLeaf(15 | (7u << 16), p);
}
std::string XtcRayonixConfig(const std::string &model, uint8_t binning) {
std::string p(60, '\0');
p[0] = static_cast<char>(binning);
p[1] = static_cast<char>(binning);
p.replace(20, model.size(), model);
return XtcLeaf(73 | (2u << 16), p);
}
std::string XtcDatagram(uint32_t transition, const std::string &payload) {
@@ -4869,55 +4884,50 @@ namespace {
}
}
TEST_CASE("JFJochXTCReader_Geometry", "[portable]") {
const uint32_t nx = 24, ny = 16;
TEST_CASE("JFJochXTCReader", "[portable]") {
// 1920 wide at 4x4 binning: a whole MX340-HS readout, so 340 mm / 1920 pixels.
const uint32_t nx = 1920, ny = 4;
std::filesystem::remove_all("xtc_test");
std::filesystem::create_directories("xtc_test");
{
std::ofstream f("xtc_test/run-s00-c00.xtc", std::ios::binary);
f << XtcDatagram(4, "");
f << XtcDatagram(12, XtcLeaf(1, XtcLeaf(13, std::string(32, '\0')) + XtcFrame(4, 4, 7))
+ XtcLeaf(1, XtcFrame(nx, ny, 100)));
f << XtcDatagram(12, XtcLeaf(1, XtcFrame(nx, ny, 200)));
}
{
// The beam meets the panel 12 pixels along fast and 8 along slow from the first pixel's
// corner, 150 mm away; 0.1 mm pixels.
std::ofstream f("xtc_test/geometry.expt");
f << R"({"beam": [{"direction": [0, 0, 1], "wavelength": 1.3, "polarization_normal": [0, 1, 0],
"polarization_fraction": 0.999}],
"detector": [{"panels": [{"fast_axis": [1, 0, 0], "slow_axis": [0, -1, 0],
"origin": [-1.2, 0.8, -150], "pixel_size": [0.1, 0.1],
"image_size": [24, 16]}]}]})";
f << XtcDatagram(4, XtcLeaf(1, XtcRayonixConfig("MX340-HS:125", 4)));
f << XtcDatagram(12, XtcLeaf(1, XtcLeaf(13, std::string(32, '\0')) + XtcFrame(4, 4, 7, 2) + XtcEBeam(9500))
+ XtcLeaf(1, XtcFrame(nx, ny, 100, 1)));
f << XtcDatagram(12, XtcLeaf(1, XtcFrame(nx, ny, 300, 1, 0x4000) + XtcEBeam(9520))); // damaged
f << XtcDatagram(12, XtcLeaf(1, XtcFrame(nx, ny, 200, 1) + XtcEBeam(9510)));
const std::string cut = XtcDatagram(12, XtcLeaf(1, XtcFrame(nx, ny, 400, 1)));
f << cut.substr(0, cut.size() / 2);
}
REQUIRE(JFJochXTCReader::CanRead("xtc_test/run-s00-c00.xtc"));
CHECK_FALSE(JFJochXTCReader::CanRead("xtc_test")); // a directory is not read
CHECK_FALSE(JFJochXTCReader::CanRead("xtc_test/geometry.expt"));
JFJochXTCReader reader;
REQUIRE_NOTHROW(reader.ReadFiles({"xtc_test/run-s00-c00.xtc"}));
CHECK(reader.GetNumberOfImages() == 2);
const auto x = reader.GetDataset()->experiment;
CHECK(x.GetXPixelsNum() == nx);
CHECK(x.GetYPixelsNum() == ny);
CHECK(x.GetDetectorDistance_mm() == Catch::Approx(150.0));
CHECK(x.GetPixelSize_mm() == Catch::Approx(0.1));
CHECK(x.GetWavelength_A() == Catch::Approx(1.3).epsilon(1e-4));
CHECK(x.GetBeamX_pxl() == Catch::Approx(12.0));
CHECK(x.GetBeamY_pxl() == Catch::Approx(8.0));
CHECK_FALSE(x.GetGoniometer().has_value()); // stills
REQUIRE(x.GetPolarizationFactor().has_value());
CHECK(x.GetPolarizationFactor().value() == Catch::Approx(0.998));
for (int i = 0; i < 2; i++) {
auto raw = reader.GetRawImage(i);
REQUIRE(raw);
CHECK(raw->image.GetWidth() == nx);
CHECK(raw->image.GetHeight() == ny);
const auto *pixels = reinterpret_cast<const int32_t *>(raw->image_buffer.data());
CHECK(pixels[0] == 100 * (i + 1));
CHECK(pixels[nx * ny - 1] == 100 * (i + 1) + nx * ny - 1);
{
// The XTC's own beam records and Rayonix configuration.
JFJochXTCReader reader;
REQUIRE_NOTHROW(reader.ReadFiles({"xtc_test/run-s00-c00.xtc"}));
CHECK(reader.GetNumberOfImages() == 2);
CHECK(reader.HasWavelength());
CHECK(reader.HasPixelSize());
const auto x = reader.GetDataset()->experiment;
CHECK(x.GetXPixelsNum() == nx);
CHECK(x.GetPixelSize_mm() == Catch::Approx(340.0 / 1920.0));
// The damaged image's beam record still counts: three records, 9500, 9520 and 9510 eV.
CHECK(x.GetIncidentEnergy_keV() == Catch::Approx(9.510).epsilon(1e-5));
CHECK(x.GetBeamX_pxl() == Catch::Approx(nx / 2.0));
for (int i = 0; i < 2; i++) {
auto raw = reader.GetRawImage(i);
REQUIRE(raw);
const auto *pixels = reinterpret_cast<const int32_t *>(raw->image_buffer.data());
CHECK(pixels[0] == 100 * (i + 1) - 3);
CHECK(pixels[99] == 100 * (i + 1) + 99 - 3);
}
reader.ApplyGain(2.0f);
CHECK(reader.CountsInPhotons());
auto raw = reader.GetRawImage(0);
CHECK(reinterpret_cast<const int32_t *>(raw->image_buffer.data())[1] == 49); // (100 + 1 - 3) / 2
}
reader.Close();
std::filesystem::remove_all("xtc_test");
}