Files
leonarski_fandClaude Opus 5.5 532f795348
Build Packages / build:portable:windows-x86_64:cuda (push) Failing after 24m10s
Build Packages / build:portable:linux-x86_64:cuda (push) Failing after 12m10s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 26m58s
Build Packages / Create release (push) Successful in 7s
Build Packages / build:portable:macos-arm64:nocuda (push) Failing after 2m53s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 24m49s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 30m24s
Build Packages / Build documentation (push) Successful in 44s
Build Packages / build:portable:linux-x86_64:nocuda (push) Failing after 9m10s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 10m45s
Build Packages / build:portable:windows-x86_64:nocuda (push) Failing after 22m23s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 29m4s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 30m18s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m38s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 28m38s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 30m36s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 32m54s
Build Packages / Generate python client (push) Successful in 13s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 32m18s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 35m18s
Build Packages / Unit tests (push) Successful in 2h25m46s
Build Packages / Validation Battery (push) Successful in 1h10m47s
module_placement in custom_geometry's words: x0/y0/fast_axis/slow_axis after mirror_y
dataset_settings.module_placement (detector_module_placement) now names a module as custom_geometry's
detector_module does - x0, y0 (image position of the readout pixel 0) and fast_axis, slow_axis
(detector_module_direction) - but in the stored image, after mirror_y; x0_plane, y0_plane and
fast_step, slow_step give where pixel 0 lies on the plane and the plane step per readout pixel. Both
schema descriptions state the frame and the mirror (y0 = H - 1 - y0, Yp <-> Yn).

DetectorModule carries the readout axes in the image (pixel 0 follows from the box): from the
detector geometry after mirror_y (ModulesAsImaged), from how the XFEL reader turns a panel, and
Xp/Yp for a panel read along its own array. They travel in the CBOR detector_modules
(fast_axis/slow_axis) and in the NXmx module_NN groups (readout_fast_axis/readout_slow_axis, beside
the NXmx fields), so a stored file names its modules as the broker does. ModulesPlaced matches the
four keys exactly; a module not of the detector, placed twice or left out is an error (400 at /start).
The rugnux log names modules the same way.

New test ModulePlacement_TurnedMirroredModules (Yp/Xn module, mirror_y on) and PanelLayout_Placement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJKPUFLoeAchjzv3x4
2026-11-03 11:39:36 +01:00

81 lines
4.7 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();
// A NXmx reader lists the groups by name (dxtbx, through h5py), and its panel n is the n-th of them:
// the index is zero-padded to the width of the last one (module_00 ... module_15), so that panel n is
// module n - and the index /entry/MX/peakPanel gives.
const size_t digits = std::to_string(modules.size() - 1).size();
for (size_t n = 0; n < modules.size(); n++) {
const DetectorModule &m = modules[n];
std::string index = std::to_string(n);
index.insert(0, digits - index.size(), '0');
const std::string path = "/entry/instrument/detector/module_" + index;
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");
// Not NXmx: which way the module's readout axes run in the stored image (jfjoch_api.yaml
// detector_module_direction), so that its readout pixel 0 and axes name it in a module placement.
group.SaveScalar("readout_fast_axis", DirectionName(m.fast));
group.SaveScalar("readout_slow_axis", DirectionName(m.slow));
}
}
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");
}