diff --git a/common/DetectorModules.cpp b/common/DetectorModules.cpp index 7b313ed65..449b965f9 100644 --- a/common/DetectorModules.cpp +++ b/common/DetectorModules.cpp @@ -4,6 +4,7 @@ #include "DetectorModules.h" #include "DetectorGeometry.h" #include "Definitions.h" +#include "PixelMask.h" #include #include @@ -193,3 +194,39 @@ DetectorModules ModulesAsImaged(const DetectorGeometry &geom) { } return DetectorModules(out); } + +DetectorModules ModulesFromGapMask(const std::vector &mask, int64_t width, int64_t height) { + if (width <= 0 || height <= 0 || mask.size() != static_cast(width * height)) + return {}; + const auto clear = [&](int64_t x, int64_t y) { + return x >= 0 && y >= 0 && x < width && y < height + && (mask[y * width + x] & (1u << PixelMask::ModuleGapPixelBit)) == 0; + }; + std::vector taken(mask.size(), 0); + std::vector out; + for (int64_t y = 0; y < height; y++) + for (int64_t x = 0; x < width; x++) { + if (!clear(x, y) || taken[y * width + x]) + continue; + // The first clear pixel of a region not yet taken is a module's corner: the module runs to the + // gap along the row and down the column, and has to be whole and bounded by gap all round. + int64_t w = 0, h = 0; + while (clear(x + w, y)) w++; + while (clear(x, y + h)) h++; + if (!((w == CONVERTED_MODULE_COLS && h == CONVERTED_MODULE_LINES) + || (w == CONVERTED_MODULE_LINES && h == CONVERTED_MODULE_COLS))) + return {}; + for (int64_t yy = y - 1; yy <= y + h; yy++) + for (int64_t xx = x - 1; xx <= x + w; xx++) { + const bool inside = xx >= x && xx < x + w && yy >= y && yy < y + h; + if (clear(xx, yy) != inside) + return {}; + if (inside) + taken[yy * width + xx] = 1; + } + out.push_back(DetectorModule::AsImaged(x, y, w, h)); + } + if (out.size() < 2) + return {}; + return DetectorModules(out); +} diff --git a/common/DetectorModules.h b/common/DetectorModules.h index d38649d4f..4f979b952 100644 --- a/common/DetectorModules.h +++ b/common/DetectorModules.h @@ -121,3 +121,9 @@ public: // CONVERTED_MODULE_COLS x CONVERTED_MODULE_LINES turned the way the geometry turns it, placed as imaged. // Empty for a detector of one module. DetectorModules ModulesAsImaged(const DetectorGeometry &geom); + +// The modules of a JUNGFRAU image where only its pixel mask says where they are: every region of pixels +// clear of the module-gap bit (PixelMask::ModuleGapPixelBit) has to be one whole module, +// CONVERTED_MODULE_COLS x CONVERTED_MODULE_LINES either way round, and is taken as placed as imaged. +// Empty where any region is not, or where there are fewer than two. The mask is row-major, width x height. +DetectorModules ModulesFromGapMask(const std::vector &mask, int64_t width, int64_t height); diff --git a/common/DiffractionExperiment.cpp b/common/DiffractionExperiment.cpp index 488a8f1fb..d8bb68ff4 100644 --- a/common/DiffractionExperiment.cpp +++ b/common/DiffractionExperiment.cpp @@ -653,6 +653,10 @@ SpotFindingSettings DiffractionExperiment::DefaultDataProcessingSettings() { void DiffractionExperiment::FillMessage(StartMessage &message) const { message.images_per_file = GetImagesPerFile(); message.detector_modules = GetModules().Get(); + // A JUNGFRAU image assembled here is made of modules this experiment places itself: they go into + // the file one by one, so that what reads it back knows them. + if (message.detector_modules.empty() && GetDetectorType() == DetectorType::JUNGFRAU && IsGeometryTransformed()) + message.detector_modules = ModulesAsImaged(detector.GetGeometry()).Get(); message.beam_center_x = GetBeamX_pxl(); message.beam_center_y = GetBeamY_pxl(); const auto direct_beam = GetDiffractionGeometry().GetDirectBeam_pxl(); diff --git a/image_analysis/IndexAndRefine.cpp b/image_analysis/IndexAndRefine.cpp index eb04815f4..40d436577 100644 --- a/image_analysis/IndexAndRefine.cpp +++ b/image_analysis/IndexAndRefine.cpp @@ -642,6 +642,7 @@ void IndexAndRefine::QuickPredictAndIntegrate(DataMessage &msg, settings_prediction.window_ang = stills_window_->ang; settings_prediction.window_bw = stills_window_->bw; settings_prediction.window_q_scale = stills_window_->q_scale; + settings_prediction.window_radial = stills_window_->radial; } // Online is bounded by what the image-buffer slot can carry, offline by the prediction limit; both diff --git a/image_analysis/IndexAndRefine.h b/image_analysis/IndexAndRefine.h index 5eaae7f43..05c9a039a 100644 --- a/image_analysis/IndexAndRefine.h +++ b/image_analysis/IndexAndRefine.h @@ -97,6 +97,7 @@ struct StillsWindow { float ang = 0.0f; float bw = 0.0f; float q_scale = 1.0f; // centred on the excitation error of the reciprocal lattice scaled by this + float radial = 0.0f; // where an off-sphere reflection is placed (BraggPredictionSettings::window_radial) }; class IndexAndRefine { diff --git a/image_analysis/WriteReflections.cpp b/image_analysis/WriteReflections.cpp index 92055a8dd..8bb1b8789 100644 --- a/image_analysis/WriteReflections.cpp +++ b/image_analysis/WriteReflections.cpp @@ -8,7 +8,6 @@ #include "scale_merge/TwinningAnalysis.h" #include "bragg_integration/SystematicAbsence.h" #include "../common/ParallelFor.h" -#include "../common/PanelBoxes.h" #include "../writer/WriteRugnuxFiles.h" #include @@ -1400,12 +1399,11 @@ MtzTable ObservationMtzTable(const std::vector &outcomes, for (const auto &l : labels) mtz.columns.push_back({l, (l == "H" || l == "K" || l == "L") ? 'H' : (l == "FLAGS" || l == "USED" ? 'I' : 'R'), 0, {}}); - const auto panels = PanelBoxes(experiment.GetModules(), experiment.GetXPixelsNum(), experiment.GetYPixelsNum()); + // Predictions are on the detector plane, so the panel is the one whose placed footprint holds the + // point (-1 for a detector without panels, -2 in a gap or off every panel). const auto panel_of = [&](float x, float y) { - for (const auto &b : panels) - if (x >= b.x0 && x < b.x1 && y >= b.y0 && y < b.y1) - return static_cast(b.panel); - return -2.0f; + const auto p = experiment.GetModules().PlaneToPanel(x, y); + return p ? static_cast(p->panel) : -2.0f; }; // The run's rocking width at d*, as StillsPartialityRefine evaluates it: its table, linear in d* and held // beyond the end shells, else the three-term formula. diff --git a/image_analysis/bragg_prediction/BraggPrediction.cpp b/image_analysis/bragg_prediction/BraggPrediction.cpp index 20dd30fb0..14b1cd5be 100644 --- a/image_analysis/bragg_prediction/BraggPrediction.cpp +++ b/image_analysis/bragg_prediction/BraggPrediction.cpp @@ -167,6 +167,14 @@ int BraggPrediction::Calc(const DiffractionExperiment &experiment, const Crystal } if (window_dist <= radial_cutoff) { + if (settings.window_radial > 0.0f) { + const float az = std::fabs(recip_z); + const float f = dist_ewald_sphere * std::fmin(settings.window_radial * az / (radial_cutoff * radial_cutoff), + 1.0f / az); + S_x += f * S0.x; + S_y += f * S0.y; + S_z += f * S0.z; + } const float s0_p0 = S0.x * recip_x + S0.y * recip_y + S0.z * recip_z; const float val = s0_sq * recip_sq - s0_p0 * s0_p0; diff --git a/image_analysis/bragg_prediction/BraggPrediction.h b/image_analysis/bragg_prediction/BraggPrediction.h index f93ab8ef6..f0e82033e 100644 --- a/image_analysis/bragg_prediction/BraggPrediction.h +++ b/image_analysis/bragg_prediction/BraggPrediction.h @@ -45,6 +45,12 @@ struct BraggPredictionSettings { // The window is centred where the rocking curve is: on the excitation error of the reciprocal lattice // scaled by this (StillsPartialityRefine::MeasureRadialScale). dist_ewald stays the lattice's own. float window_q_scale = 1.0f; + // Stills, with the window: a reflection off the Ewald sphere is placed where the radial part of its + // smeared node diffracts (StillsPartialityRefine::MeasureRadialSpread) - its S moved along S0 by + // dk / k = window_radial |q_z| eps / W^2, W the window at the reflection, but never past the change that + // puts the node on the sphere (eps / |q_z|). window_radial = s^2 times the window's width in sigmas + // squared, s the run's relative radial spread. 0 leaves the positions as they were. + float window_radial = 0.0f; }; class BraggPrediction { diff --git a/image_analysis/bragg_prediction/BraggPredictionGPU.cu b/image_analysis/bragg_prediction/BraggPredictionGPU.cu index b6b1954c8..274ce58b3 100644 --- a/image_analysis/bragg_prediction/BraggPredictionGPU.cu +++ b/image_analysis/bragg_prediction/BraggPredictionGPU.cu @@ -123,6 +123,14 @@ namespace { window_dist = fabsf(sqrtf(Scx * Scx + Scy * Scy + Scz * Scz) - C.one_over_wavelength); } if (window_dist > radial_cutoff) return false; + if (C.window_radial > 0.0f) { // see the CPU path + const float az = fabsf(recip_z); + const float f = dist_ewald * fminf(C.window_radial * az / (radial_cutoff * radial_cutoff), 1.0f / az); + Sx += f * C.S0.x; + Sy += f * C.S0.y; + Sz += f * C.S0.z; + S_len = sqrtf(Sx * Sx + Sy * Sy + Sz * Sz); + } float Srx = C.rot[0] * Sx + C.rot[1] * Sy + C.rot[2] * Sz; float Sry = C.rot[3] * Sx + C.rot[4] * Sy + C.rot[5] * Sz; float Srz = C.rot[6] * Sx + C.rot[7] * Sy + C.rot[8] * Sz; @@ -253,6 +261,7 @@ int BraggPredictionGPU::Calc(const DiffractionExperiment &experiment, hK.window_ang = settings.window_ang; hK.window_bw = settings.window_bw; hK.window_q_scale = settings.window_q_scale; + hK.window_radial = settings.window_radial; cudaMemcpyAsync(dK, &hK, sizeof(KernelConsts), cudaMemcpyHostToDevice, stream); cudaMemsetAsync(d_count, 0, sizeof(int), stream); diff --git a/image_analysis/bragg_prediction/BraggPredictionGPU.h b/image_analysis/bragg_prediction/BraggPredictionGPU.h index 427b8d043..86d570d7a 100644 --- a/image_analysis/bragg_prediction/BraggPredictionGPU.h +++ b/image_analysis/bragg_prediction/BraggPredictionGPU.h @@ -18,6 +18,7 @@ struct KernelConsts { float one_over_dmax_sq; float ewald_cutoff; float window_c0, window_ang, window_bw, window_q_scale; // the stills window curve; all zero: ewald_cutoff (see BraggPredictionSettings) + float window_radial; // where an off-sphere reflection is placed (see BraggPredictionSettings) float bandwidth_sigma; // relative Δλ/λ (sigma); 0 = monochromatic // Angle-dependent sensor efficiency, reduced to the two per-dataset numbers the kernel needs: // the optical thickness at normal incidence and the absorbed fraction there. qe_a0 = 0 leaves diff --git a/image_analysis/scale_merge/StillsPartialityRefine.cpp b/image_analysis/scale_merge/StillsPartialityRefine.cpp index 1e0911824..fd879d092 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.cpp +++ b/image_analysis/scale_merge/StillsPartialityRefine.cpp @@ -526,6 +526,67 @@ double StillsPartialityRefine::MeasureRadialScale(const std::vector &outcomes, + const RunWidth &width) const { + // Strong spots only: their centroids are good to a fraction of a pixel. + constexpr double MIN_I_OVER_SIGMA = 10.0; + constexpr size_t MIN_SPOTS = 200; + constexpr float STEP = 1e-3f; // of the wavenumber, for the direction a spot moves in + // Per spot: m = how far (px, on the plane) the model moves it per unit s^2, and d = observed - predicted. + struct Spot { double mx, my, dx, dy; }; + std::vector spots; + for (const auto &o: outcomes) { + if (!o.image_scale_g || !(*o.image_scale_g > 0.0)) + continue; + const Coord S0 = o.geom.GetScatteringVector(); + for (const auto &r: o.reflections) { + if (r.on_ice_ring || !AcceptReflection(r, d_min_limit_, d_max_limit_) || !(r.d > 0.0f) + || !(r.I > MIN_I_OVER_SIGMA * r.sigma) || !std::isfinite(r.observed_x) || !std::isfinite(r.observed_y) + || !std::isfinite(r.dist_ewald)) + continue; + const Coord q = o.latt.Astar() * static_cast(r.h) + o.latt.Bstar() * static_cast(r.k) + + o.latt.Cstar() * static_cast(r.l); + const double dstar = 1.0 / r.d, qz = std::fabs(q.z); + const double a = width.gamma_e * dstar, b = width.bandwidth_sigma * qz; + const double sigma2 = width.gamma0 * width.gamma0 + a * a + b * b; + if (!(sigma2 > 0.0)) + continue; + const auto p0 = o.geom.RecipToDetector(q); + const auto p1 = o.geom.RecipToDetector(q + S0 * STEP); + const double f = qz * r.dist_ewald / sigma2 / STEP; + Spot s{f * (p1.first - p0.first), f * (p1.second - p0.second), + r.observed_x - r.predicted_x, r.observed_y - r.predicted_y}; + if (std::isfinite(s.mx) && std::isfinite(s.my)) + spots.push_back(s); + } + } + if (spots.size() < MIN_SPOTS) + return 0.0; + // Least squares, reweighted against the spots the model does not describe (overlaps, split spots): + // Cauchy weights at three times the typical residual. + double s2 = 0.0; + std::vector r2(spots.size()); + for (int iter = 0; iter < 5; ++iter) { + for (size_t i = 0; i < spots.size(); ++i) { + const double ex = spots[i].dx - s2 * spots[i].mx, ey = spots[i].dy - s2 * spots[i].my; + r2[i] = ex * ex + ey * ey; + } + std::vector sorted = r2; + std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); + const double scale2 = 9.0 * sorted[sorted.size() / 2] / 1.386; // median r^2 = 1.386 sigma^2 in 2D + double num = 0.0, den = 0.0; + for (size_t i = 0; i < spots.size(); ++i) { + const double w = 1.0 / (1.0 + r2[i] / scale2); + num += w * (spots[i].mx * spots[i].dx + spots[i].my * spots[i].dy); + den += w * (spots[i].mx * spots[i].mx + spots[i].my * spots[i].my); + } + if (!(den > 0.0)) + return 0.0; + s2 = num / den; + } + return std::max(0.0, s2); +} + std::optional StillsPartialityRefine::MeasureRunWidth( const std::vector &outcomes, const std::optional &reference_cell, double radial_scale, const RunWidth *window_width) const { diff --git a/image_analysis/scale_merge/StillsPartialityRefine.h b/image_analysis/scale_merge/StillsPartialityRefine.h index 91b721c04..8cc299158 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.h +++ b/image_analysis/scale_merge/StillsPartialityRefine.h @@ -85,6 +85,16 @@ public: double MeasureRadialScale(const std::vector &outcomes, const std::optional &reference_cell, double radial_scale) const; + // How far the run's spots move radially with their excitation error, as the relative spread s of the + // radial scale of the reciprocal lattice (a photon-energy spread, or a cell spread) that would move them + // so. A node off the Ewald sphere is recorded where the part of its smeared node that IS on the sphere + // diffracts: along the radial share of its rocking width, that is where the photon energy that puts the + // node on the sphere sends it. With the rocking width sigma (width's three-term formula) made of a radial + // part s |q_z| and the rest, the expected move is that of a change of the beam's wavenumber by + // dk / k = s^2 |q_z| eps / sigma^2 - fitted by least squares to where the strong spots were observed + // against where they were predicted. Returns s^2; 0 when the run has too few strong spots. + double MeasureRadialSpread(const std::vector &outcomes, const RunWidth &width) const; + private: const DiffractionExperiment experiment_; const Settings settings_{}; diff --git a/reader/HDF5MetadataSource.cpp b/reader/HDF5MetadataSource.cpp index d53bee837..0d6fa3b6d 100644 --- a/reader/HDF5MetadataSource.cpp +++ b/reader/HDF5MetadataSource.cpp @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only +#include +#include #include #include #include @@ -1148,6 +1150,14 @@ HDF5MetadataSource::OpenResult HDF5MetadataSource::Open(const std::string &filen } if (mask_tmp.empty()) mask_tmp = std::vector(image_size_x * image_size_y); + // A JUNGFRAU file that does not state its modules one by one still marks the gaps between + // them in its mask: each region clear of the gap bit is one module, placed as imaged. Only for + // JUNGFRAU - a DECTRIS mask uses the same bit for its gaps, and its modules are not read here. + std::string detector_upper = detector_name; + std::transform(detector_upper.begin(), detector_upper.end(), detector_upper.begin(), + [](unsigned char c) { return static_cast(std::toupper(c)); }); + if (dataset->experiment.GetModules().empty() && detector_upper.find("JUNGFRAU") != std::string::npos) + dataset->experiment.Modules(ModulesFromGapMask(mask_tmp, image_size_x, image_size_y)); dataset->pixel_mask = std::make_shared(mask_tmp); } diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index cf9722375..10fd7e204 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -378,6 +378,12 @@ Rugnux::Rugnux(JFJochReader &reader, DiffractionExperiment experiment, : fmt::format("{:.0f}% of the machine's {:.1f} GB", 100.0 * HOST_MEMORY_BUDGET_FRACTION, static_cast(machine) / 1e9)); + // Modules where the image already has them (a JUNGFRAU file's own, or rebuilt from its mask) tell + // the stills module placement what may move; a rotation run has no such step and processes the + // image as one panel, as it always has. + if (experiment_.IsRotationIndexing() && !experiment_.GetModules().Moved()) + experiment_.Modules({}); + // Every decision on a rotation run - per-frame scaling, the correction surfaces, the outlier test, // the error model, the CC1/2 half-sets and the resolution cut, the frame ledger, the space-group // search - is taken with Friedel mates merged, -A or not. Splitting them halves the multiplicity diff --git a/rugnux/RugnuxImagePass.cpp b/rugnux/RugnuxImagePass.cpp index f9509795e..7e1886f85 100644 --- a/rugnux/RugnuxImagePass.cpp +++ b/rugnux/RugnuxImagePass.cpp @@ -449,6 +449,7 @@ void Rugnux::ImagePass(PipelineLocals &p) { for (int i = 0; i < 3; ++i) radial_scale = refine.MeasureRadialScale(outcomes, stills_user_cell_, radial_scale); stills_run_width_ = refine.MeasureRunWidth(outcomes, stills_user_cell_, radial_scale); + const double radial_spread2 = stills_run_width_ ? refine.MeasureRadialSpread(outcomes, *stills_run_width_) : 0.0; for (const int o: window_frames) outcomes[o] = IntegrationOutcome{}; if (stills_run_width_) { @@ -456,9 +457,12 @@ void Rugnux::ImagePass(PipelineLocals &p) { indexer->SetStillsWindow(StillsWindow{.c0 = k * static_cast(stills_run_width_->gamma0), .ang = k * static_cast(stills_run_width_->gamma_e), .bw = k * static_cast(stills_run_width_->bandwidth_sigma), - .q_scale = static_cast(stills_run_width_->radial_scale)}); + .q_scale = static_cast(stills_run_width_->radial_scale), + .radial = k * k * static_cast(radial_spread2)}); Note(fmt::format("prediction window {:.1f} x the rocking width measured on {} frames, centred at " "radial scale {:+.2e}", config_.stills_window_sigmas, window_frames.size(), radial_scale - 1.0)); + Note(fmt::format("spots placed with their excitation error as by a radial spread of {:.1e}", + std::sqrt(radial_spread2))); } } for (int i = 0; i < image_workers; ++i) diff --git a/tests/CalcBraggPredictionTest.cpp b/tests/CalcBraggPredictionTest.cpp index 9c071c358..831a7cb9c 100644 --- a/tests/CalcBraggPredictionTest.cpp +++ b/tests/CalcBraggPredictionTest.cpp @@ -451,6 +451,49 @@ TEST_CASE("BraggPrediction_WindowFollowsResolution", "[portable]") { CHECK(got_scaled != got); } +TEST_CASE("BraggPrediction_WindowRadialPlacement", "[portable]") { + // With window_radial a reflection off the Ewald sphere is placed as by the wavenumber change + // dk / k = window_radial |q_z| eps / W^2, capped where it puts the node on the sphere: an outside node + // (eps > 0) moves toward the beam, an inside one away, by the amount the formula says. + DiffractionExperiment experiment(DetJF4M()); + experiment.DetectorDistance_mm(100.0).BeamX_pxl(1500.0).BeamY_pxl(1000.0).IncidentEnergy_keV(12.0); + const CrystalLattice lattice(Coord{30, 10, 0}, Coord{-15, 45, 0}, Coord{0, 0, 150}); + BraggPredictionSettings plain{.high_res_A = 1.8, .max_h = 40, .max_k = 40, .max_l = 90}; + plain.window_c0 = 1e-4f; + plain.window_ang = 0.06f * static_cast(PI) / 180.0f; + BraggPredictionSettings moved = plain; + moved.window_radial = 4.0f * 2e-3f * 2e-3f; + + BraggPrediction pred_plain, pred_moved; + const int n = pred_plain.Calc(experiment, lattice, plain); + REQUIRE(n > 100); + REQUIRE(pred_moved.Calc(experiment, lattice, moved) == n); + + const auto geom = experiment.GetDiffractionGeometry(); + const Coord S0 = geom.GetScatteringVector(); + int inward = 0, outward = 0; + for (int i = 0; i < n; ++i) { + const auto &a = pred_plain.GetReflections()[i], &b = pred_moved.GetReflections()[i]; + REQUIRE(a.h == b.h); + REQUIRE(a.k == b.k); + REQUIRE(a.l == b.l); + CHECK(b.dist_ewald == a.dist_ewald); + const auto q = a.h * lattice.Astar() + a.k * lattice.Bstar() + a.l * lattice.Cstar(); + const float W2 = plain.window_c0 * plain.window_c0 + plain.window_ang * plain.window_ang * (q * q); + const float az = std::fabs(q.z); + const float f = a.dist_ewald * std::fmin(moved.window_radial * az / W2, 1.0f / az); + const auto [ex, ey] = geom.RecipToDetector(q + S0 * f); + CHECK(b.predicted_x == Catch::Approx(ex).margin(1e-3)); + CHECK(b.predicted_y == Catch::Approx(ey).margin(1e-3)); + const float ra = std::hypot(a.predicted_x - 1500.0f, a.predicted_y - 1000.0f); + const float rb = std::hypot(b.predicted_x - 1500.0f, b.predicted_y - 1000.0f); + if (a.dist_ewald > 0 && rb < ra) ++inward; + if (a.dist_ewald < 0 && rb > ra) ++outward; + } + CHECK(inward > 10); + CHECK(outward > 10); +} + TEST_CASE("BraggPrediction_GrowsPastStartingCapacity", "[portable]") { // A frame predicting more than the buffer starts with must come back whole - the same reflections a // buffer large enough from the outset returns - and not as whichever the h/k/l walk reached first. diff --git a/tests/DetectorModulesTest.cpp b/tests/DetectorModulesTest.cpp index 9a3478a29..bcb823c72 100644 --- a/tests/DetectorModulesTest.cpp +++ b/tests/DetectorModulesTest.cpp @@ -9,6 +9,7 @@ #include "../common/DetectorModules.h" #include "../common/DiffractionExperiment.h" #include "../common/JFJochMath.h" +#include "../common/PixelMask.h" #include "../reader/NXmxModules.h" #include "../writer/NXmxModules.h" @@ -110,3 +111,30 @@ TEST_CASE("DetectorModules_NXmxRoundTrip", "[DetectorModules]") { CHECK(b.sy == Catch::Approx(a.sy).margin(1e-6)); } } + +// A JUNGFRAU file that marks only the gaps between its modules in the mask: the modules come back from +// the mask, one per gap-bounded region; a region that is not one whole module gives none at all. +TEST_CASE("DetectorModules_FromGapMask", "[DetectorModules]") { + const DetectorGeometryModular geom(4, 2, 8, 36, false); + const DetectorModules expected = ModulesAsImaged(geom); + const int64_t width = geom.GetWidth(true), height = geom.GetHeight(true); + std::vector mask(width * height, 1u << PixelMask::ModuleGapPixelBit); + expected.ForEachPixel(width, height, [&](int64_t, size_t i, double, double) { mask[i] = 0; }); + mask[5] |= 1u << PixelMask::ErrorPixelBit; // other bits do not matter + + const DetectorModules modules = ModulesFromGapMask(mask, width, height); + REQUIRE(modules.size() == expected.size()); + for (size_t i = 0; i < modules.size(); i++) { + CHECK(modules[i].x0 == expected[i].x0); + CHECK(modules[i].y0 == expected[i].y0); + CHECK(modules[i].width == expected[i].width); + CHECK(modules[i].height == expected[i].height); + } + + // A gap pixel inside a module: no longer whole modules. + auto holed = mask; + holed[100 * width + 100] |= 1u << PixelMask::ModuleGapPixelBit; + CHECK(ModulesFromGapMask(holed, width, height).empty()); + // No gaps at all: one region the size of the image, not a module. + CHECK(ModulesFromGapMask(std::vector(width * height, 0), width, height).empty()); +} diff --git a/tests/JFJochReaderTest.cpp b/tests/JFJochReaderTest.cpp index fa3117bc4..02364ce78 100644 --- a/tests/JFJochReaderTest.cpp +++ b/tests/JFJochReaderTest.cpp @@ -5168,3 +5168,54 @@ TEST_CASE("JFJochReader_PanelId", "[HDF5][PanelId]") { REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); } + +// A JUNGFRAU image assembled by the experiment is written with its modules one by one (module_0, ...), +// and the reader gives them back where the assembly put them; a DECTRIS detector is written without. +TEST_CASE("JFJochReader_JungfrauModules", "[HDF5][Full]") { + RegisterHDF5Filter(); + for (const bool jungfrau: {true, false}) { + DiffractionExperiment x(jungfrau ? DetJF(4) : DetDECTRIS(2068, 1064, "Detector", {})); + x.FilePrefix("test_modules").ImagesPerTrigger(1).OverwriteExistingFiles(true); + x.BeamX_pxl(100).BeamY_pxl(200).DetectorDistance_mm(150) + .IncidentEnergy_keV(WVL_1A_IN_KEV).PixelSigned(false).BitDepthImage(16); + if (jungfrau) + x.FrameTime(std::chrono::microseconds(500), std::chrono::microseconds(10)); + const DetectorModules expected = jungfrau ? ModulesAsImaged(x.GetDetectorSetup().GetGeometry()) + : DetectorModules(); + CHECK(expected.size() == (jungfrau ? 4 : 0)); + ScanResultGenerator generator(x); + std::vector image(x.GetPixelsNum(), 0); + { + StartMessage start_message; + x.FillMessage(start_message); + FileWriter file_set(start_message); + DataMessage message{}; + message.image = CompressedImage(image, x.GetXPixelsNum(), x.GetYPixelsNum()); + message.number = 0; + REQUIRE_NOTHROW(file_set.WriteHDF5(message)); + REQUIRE_NOTHROW(generator.Add(message)); + EndMessage end_message; + end_message.max_image_number = 1; + generator.FillEndMessage(end_message); + file_set.WriteHDF5(end_message); + file_set.Finalize(); + } + { + JFJochHDF5Reader reader; + reader.ReadFile("test_modules_master.h5"); + const DetectorModules &back = reader.GetDataset()->experiment.GetModules(); + REQUIRE(back.size() == expected.size()); + for (size_t i = 0; i < back.size(); i++) { + CHECK(back[i].x0 == expected[i].x0); + CHECK(back[i].y0 == expected[i].y0); + CHECK(back[i].width == expected[i].width); + CHECK(back[i].height == expected[i].height); + CHECK(back[i].ox == Catch::Approx(expected[i].ox).margin(1e-3)); + CHECK(back[i].oy == Catch::Approx(expected[i].oy).margin(1e-3)); + } + CHECK_FALSE(back.Moved()); + } + remove("test_modules_master.h5"); + } + REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); +}