Files
Jungfraujoch/reader/NXmxModules.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

69 lines
2.9 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "NXmxModules.h"
#include <cmath>
#include "../common/JFJochException.h"
namespace {
// A translation dataset as a vector in pixels of the internal frame (x = column, y = row, z = beam).
Coord ReadTranslation_px(HDF5Object &file, const std::string &path, double pixel_size_m, const RotMatrix &delta) {
HDF5DataSet ds(file, path);
std::vector<double> value;
ds.ReadVector(value);
const auto vec = ds.ReadAttrVec("vector");
if (value.empty() || vec.size() != 3)
throw JFJochException(JFJochExceptionCategory::HDF5, path + ": not a translation");
double scale = 1.0;
if (ds.AttrExists("units")) {
const std::string units = ds.ReadAttrStr("units");
if (units == "mm")
scale = 1e-3;
else if (units == "um" || units == "microns")
scale = 1e-6;
}
const double len_px = value[0] * scale / pixel_size_m;
// McStas -> internal is a half turn about z; the discrete image orientation is undone after it.
const Coord mcstas(static_cast<float>(-vec[0]), static_cast<float>(-vec[1]), static_cast<float>(vec[2]));
return delta.transpose() * (static_cast<float>(len_px) * mcstas);
}
} // namespace
DetectorModules ReadNXmxModules(HDF5Object &file, double pixel_size_m, const DetectorOrientation &orientation) {
const RotMatrix delta = orientation.Matrix();
std::vector<DetectorModule> out;
for (size_t n = 0;; n++) {
const std::string base = "/entry/instrument/detector/module_" + std::to_string(n) + "/";
if (!file.Exists(base + "data_origin"))
break;
const auto origin = file.ReadVector<int64_t>(base + "data_origin");
const auto size = file.ReadVector<int64_t>(base + "data_size");
if (origin.size() != 2 || size.size() != 2)
throw JFJochException(JFJochExceptionCategory::HDF5, base + ": only two-dimensional modules are read");
const Coord o = ReadTranslation_px(file, base + "module_offset", pixel_size_m, delta);
const Coord f = ReadTranslation_px(file, base + "fast_pixel_direction", pixel_size_m, delta);
const Coord s = ReadTranslation_px(file, base + "slow_pixel_direction", pixel_size_m, delta);
if (std::fabs(o.z) > 1e-3 || std::fabs(f.z) > 1e-6 || std::fabs(s.z) > 1e-6)
throw JFJochException(JFJochExceptionCategory::HDF5, base + ": a module out of the detector plane is not read");
DetectorModule m;
m.y0 = origin[0];
m.x0 = origin[1];
m.height = size[0];
m.width = size[1];
m.ox = o.x;
m.oy = o.y;
m.fx = f.x;
m.fy = f.y;
m.sx = s.x;
m.sy = s.y;
out.push_back(m);
}
if (out.size() < 2)
return {};
return DetectorModules(out);
}