Sweep ends, overloads and ice flags: one walk over the reflections instead of two

Before the merge, the integrated reflections were walked twice - once per frame for what the
blank sweep ends are decided on, once more for the overload counts and the ice flags - and
each walk reads the whole reflection set from memory. The frame's sweep-end tally is now a
function of its own (TallySweepEnd), and the merge step makes it in the same per-frame walk
that counts the overloads and flags the ice, then adds the counts up over the frames still in,
so the trimmed and left-out frames count nothing, as before. Per frame the sums are the same
sums in the same order; the totals are integers. TrimBlankSweepEnds keeps its one-argument
form for the callers that walk the frames for nothing else.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
leonarski_fandClaude Fable 5.1 committed 2026-10-26 20:58:10 +01:00
1 parent 093ac1612c
commit dfb1536fe2
3 files changed
+70 -35

No files matched your search

@@ -408,23 +408,38 @@ RotationScaleMerge::RotationScaleMerge(const DiffractionExperiment &experiment,
// the scaling and the sweep ledger to judge as they judge any other. Only from the ends inward: a frame
// inside the sweep without beam is news, and is left to the ledger too. A frame with nothing integrated
// on it (missing from disk, not processed) is walked past and not counted.
std::pair<int, int> TrimBlankSweepEnds(std::vector<IntegrationOutcome> &outcomes, size_t nthreads) {
namespace {
constexpr double BLANK_TAIL_SIGMA = 3.0;
}
SweepEndTally TallySweepEnd(const IntegrationOutcome &outcome) {
SweepEndTally t;
for (const auto &r : outcome.reflections) {
if (std::isfinite(r.I) && std::isfinite(r.sigma) && r.sigma > 0.0f) {
t.upper += r.I > BLANK_TAIL_SIGMA * r.sigma;
t.lower += r.I < -BLANK_TAIL_SIGMA * r.sigma;
}
if (std::isfinite(r.bkg)) { t.bkg_sum += r.bkg; ++t.bkg_n; }
}
return t;
}
std::pair<int, int> TrimBlankSweepEnds(std::vector<IntegrationOutcome> &outcomes, size_t nthreads) {
std::vector<SweepEndTally> tally(outcomes.size());
ParallelFor(static_cast<int>(outcomes.size()), nthreads, [&](int f) { tally[f] = TallySweepEnd(outcomes[f]); });
return TrimBlankSweepEnds(outcomes, tally);
}
std::pair<int, int> TrimBlankSweepEnds(std::vector<IntegrationOutcome> &outcomes,
const std::vector<SweepEndTally> &tally) {
const int n = static_cast<int>(outcomes.size());
std::vector<int64_t> upper(n, 0), lower(n, 0);
std::vector<double> bkg(n, 0.0);
ParallelFor(n, nthreads, [&](int f) {
double bkg_sum = 0.0;
int64_t bkg_n = 0;
for (const auto &r : outcomes[f].reflections) {
if (std::isfinite(r.I) && std::isfinite(r.sigma) && r.sigma > 0.0f) {
upper[f] += r.I > BLANK_TAIL_SIGMA * r.sigma;
lower[f] += r.I < -BLANK_TAIL_SIGMA * r.sigma;
}
if (std::isfinite(r.bkg)) { bkg_sum += r.bkg; ++bkg_n; }
}
if (bkg_n > 0) bkg[f] = bkg_sum / bkg_n;
});
for (int f = 0; f < n; ++f) {
upper[f] = tally[f].upper;
lower[f] = tally[f].lower;
if (tally[f].bkg_n > 0) bkg[f] = tally[f].bkg_sum / tally[f].bkg_n;
}
std::vector<double> excess, tails, background;
for (int f = 0; f < n; ++f)
if (!outcomes[f].reflections.empty()) {