Files
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

71 lines
3.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>
namespace {
// Internal frame (x = column, y = row downward, z = beam) -> McStas, a half turn about z.
std::vector<double> ToMcStas(const Coord &v) {
return {0.0 - v.x, 0.0 - v.y, 0.0 + v.z};
}
// A translation of `length` metres along v (pixels, internal frame), as NXmx writes one: the length as
// the value, the direction as a unit vector - and a zero translation with a unit vector all the same.
void SaveTranslation(HDF5Group &group, const std::string &name, const RotMatrix &delta, double dx_px, double dy_px,
double pixel_size_m, const std::string &depends_on) {
const double len_px = std::hypot(dx_px, dy_px);
const Coord dir = len_px > 0 ? Coord(static_cast<float>(dx_px / len_px), static_cast<float>(dy_px / len_px), 0)
: Coord(0, 0, 1);
SaveScalar(group, name, len_px * pixel_size_m)->
Transformation("m", depends_on, "", "", "translation", ToMcStas(delta * dir), {0, 0, 0}, "");
}
} // namespace
void WriteNXmxModules(HDF5Object &file, const DetectorModules &modules, double pixel_size_m,
const DetectorOrientation &orientation, const std::string &depends_on) {
const RotMatrix delta = orientation.Matrix();
for (size_t n = 0; n < modules.size(); n++) {
const DetectorModule &m = modules[n];
const std::string path = "/entry/instrument/detector/module_" + std::to_string(n);
HDF5Group group(file, path);
group.NXClass("NXdetector_module");
group.SaveVector("data_origin", std::vector<int32_t>{static_cast<int32_t>(m.y0), static_cast<int32_t>(m.x0)});
group.SaveVector("data_size", std::vector<int32_t>{static_cast<int32_t>(m.height),
static_cast<int32_t>(m.width)});
SaveTranslation(group, "module_offset", delta, m.ox, m.oy, pixel_size_m, depends_on);
SaveTranslation(group, "fast_pixel_direction", delta, m.fx, m.fy, pixel_size_m, path + "/module_offset");
SaveTranslation(group, "slow_pixel_direction", delta, m.sx, m.sy, pixel_size_m, path + "/module_offset");
}
}
void WriteModuleGeometryFile(const std::string &path, const DetectorModules &modules,
const DiffractionGeometry &geom) {
HDF5File file(path);
HDF5Group(file, "/entry").NXClass("NXentry");
HDF5Group(file, "/entry/instrument").NXClass("NXinstrument");
HDF5Group(file, "/entry/instrument/detector").NXClass("NXdetector");
HDF5Group transformations(file, "/entry/instrument/detector/transformations");
transformations.NXClass("NXtransformations");
// Sample -> image pixel (0, 0), square to the beam: what the modules' offsets are measured from. The
// tilt, if any, is the master file's to state; this file carries the modules only.
const double pixel_m = geom.GetPixelSize_mm() / 1000.0;
const RotMatrix delta = geom.GetOrientation().Matrix();
const auto v = ToMcStas(delta * Coord(-geom.GetBeamX_pxl() * static_cast<float>(pixel_m),
-geom.GetBeamY_pxl() * static_cast<float>(pixel_m),
geom.GetDetectorDistance_mm() / 1000.0f));
const double len = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
SaveScalar(transformations, "translation", len)->
Transformation("m", ".", "detector", "detector_arm", "translation",
std::vector<double>{v[0] / len, v[1] / len, v[2] / len});
SaveScalar(file, "/entry/instrument/detector/depends_on",
std::string("/entry/instrument/detector/transformations/translation"));
WriteNXmxModules(file, modules, pixel_m, geom.GetOrientation(),
"/entry/instrument/detector/transformations/translation");
}