From 0aeb7edb9ac41707b2b2585f0670a8ce088a3474 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 27 Sep 2026 21:27:26 +0200 Subject: [PATCH] rugnux: re-run the supercell-guard and header-geometry passes at the fixed radius when starved The background-starvation check (the widened integration radius left too many reflections without a background ring) followed only the canonical pass and the geometry walk's write pass. The two later re-runs - the supercell guard's pass with pass-1's lattice forced and the header-geometry redo - ran at whatever radius was in hand, which is still the widened one whenever the canonical pass was not starved. A starved supercell-guard re-run marks itself superseded (quality_guard_pass1_ is still set), writes no report and no files, and nothing re-ran it: the run exited 0 with the files of the spurious-supercell pass it was meant to replace and a report from the re-run. A starved header-geometry redo was not superseded but shipped the starved intensities. The starvation check is now a lambda asked after each of the three; after the first re-run the radius is the fixed one and it cannot fire again. The supercell re-run keeps pass-1's lattice forced through its own starvation re-run. Both late passes set rerun_if_starved_, so a starved one stops after integration as the canonical pass does. Demonstrated on myob with temporary switches forcing the supercell guard and a starved forced pass (removed before commit): before, PASS 3 of 3 superseded and the files came from the supercell pass; after, PASS 4 of 4 at r1=4 with the forced lattice. Normal runs byte-identical (myob and a starved open-arm set: p.hkl p.mtz p_P1.mtz p_unmerged.mtz). No battery run of 4 checked ever fired the supercell guard. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C --- rugnux/Rugnux.cpp | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index 6fcb5a959..bc1c94b86 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -2901,8 +2901,14 @@ ProcessResult Rugnux::RunAllPasses(RugnuxObserver *observer) { // The pass that integrates at the widened radius is now the canonical one, so the measurement // and the remedy are both here: re-run it at the radius that keeps the data, the same remedy the // two guards below use and, like them, a pass only on a crystal that was going to be wrong. - if (!cancelled_ && bragg_adaptive_ && bkg_starved_fraction_ - && *bkg_starved_fraction_ > spot_width::BKG_STARVED_MAX_FRACTION) { + // Asked of every pass that can end the run - the two guards below re-run at whatever radius is + // in hand, and a starved refined pass has written nothing (see the superseded flag in + // RunPipeline) - so each is followed by it. After the first re-run the radius is the fixed one + // and this never fires again. + const auto rerun_if_starved = [&](ProcessResult &pass, const std::string decision) { + if (cancelled_ || !bragg_adaptive_ || !bkg_starved_fraction_ + || *bkg_starved_fraction_ <= spot_width::BKG_STARVED_MAX_FRACTION) + return; logger.Info("Two-pass: at the widened integration radius r1={:.1f} the neighbouring " "reflections left {:.2f}% of the predicted reflections without a background " "ring (bound {:.2f}%), so this pass is re-run at r1={:.1f} r2={:.1f} r3={:.2f}", @@ -2912,18 +2918,19 @@ ProcessResult Rugnux::RunAllPasses(RugnuxObserver *observer) { bragg_before_adaptive_->GetR3()); const double starved = *bkg_starved_fraction_; const float wide_r1 = bragg_adaptive_->GetR1(); - const int passes = pass2.pass_count + 1; + const int passes = pass.pass_count + 1; experiment_.ImportBraggIntegrationSettings(*bragg_before_adaptive_); bragg_adaptive_.reset(); - pass2 = RunPipeline(observer, /*write_output=*/true, /*geometry_prepass=*/false); - pass2.post_refine = pass1.post_refine; - pass2.pass_number = passes; - pass2.pass_count = passes; - pass2.pass_decision = fmt::format( - "post-refined geometry adopted, re-run at the fixed integration radius: at r1={:.1f} " - "neighbouring reflections left {:.2f}% of the predicted reflections without a " - "background ring", wide_r1, 100.0 * starved); - } + pass = RunPipeline(observer, /*write_output=*/true, /*geometry_prepass=*/false); + pass.post_refine = pass1.post_refine; + pass.pass_number = passes; + pass.pass_count = passes; + pass.pass_decision = fmt::format( + "{}, re-run at the fixed integration radius: at r1={:.1f} neighbouring reflections " + "left {:.2f}% of the predicted reflections without a background ring", decision, wide_r1, + 100.0 * starved); + }; + rerun_if_starved(pass2, "post-refined geometry adopted"); // Supercell-collapse guard for the second pass. The de-novo lattice search can be BISTABLE on a // periodically-modulated dataset: the tiny post-refine geometry move tips it out of the true-cell @@ -2959,7 +2966,9 @@ ProcessResult Rugnux::RunAllPasses(RugnuxObserver *observer) { if (prepass_rotation_scale_ && force_rotation_result_->axis) force_rotation_result_->axis = ScaleRotation(*force_rotation_result_->axis, *prepass_rotation_scale_); + rerun_if_starved_ = true; pass2 = RunPipeline(observer, /*write_output=*/true, /*geometry_prepass=*/false); + rerun_if_starved_ = false; pass2.post_refine = pass1.post_refine; pass2.pass_number = 3; pass2.pass_count = 3; @@ -2967,6 +2976,8 @@ ProcessResult Rugnux::RunAllPasses(RugnuxObserver *observer) { "post-refined geometry adopted, re-run with pass-1's lattice forced: the de-novo " "primitive cell volume {:.0f} A^3 was {:.2f}x pass-1's ({:.0f} A^3), a spurious " "supercell", v2, v2 / v1, v1); + // Still forced: the re-run at the fixed radius is the same pass. + rerun_if_starved(pass2, pass2.pass_decision); force_rotation_result_.reset(); } } @@ -3001,12 +3012,15 @@ ProcessResult Rugnux::RunAllPasses(RugnuxObserver *observer) { // This pass is the answer whatever it measures - the guard has had its one chance - // so it must not conclude from its own search merge that it is going to be re-run. quality_guard_pass1_.reset(); + rerun_if_starved_ = true; auto redo = RunPipeline(observer, /*write_output=*/true, /*geometry_prepass=*/false); + rerun_if_starved_ = false; redo.post_refine = pass1.post_refine; redo.pass_number = pass2.pass_count + 1; redo.pass_count = pass2.pass_count + 1; redo.pass_decision = fmt::format( "header geometry re-adopted: the post-refined pass was worse ({})", worse); + rerun_if_starved(redo, redo.pass_decision); return redo; } if (pass2.pass_decision.empty())