diff --git a/image_analysis/geom_refinement/PostRefine.cpp b/image_analysis/geom_refinement/PostRefine.cpp index a4cd73d87..8bda19687 100644 --- a/image_analysis/geom_refinement/PostRefine.cpp +++ b/image_analysis/geom_refinement/PostRefine.cpp @@ -487,10 +487,19 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, }); constexpr size_t MAX_OBS = 20000; if (n_obs > MAX_OBS) { - std::nth_element(obs.get(), obs.get() + MAX_OBS, obs.get() + n_obs, - [](const Partial *a, const Partial *b) { - return a->I / std::max(1e-9, static_cast(a->sigma)) - > b->I / std::max(1e-9, static_cast(b->sigma)); }); + // By I/sigma, with the ratio worked out once per observation beside it rather than at + // every comparison through the pointer: the same comparisons, so the same selection in + // the same order. + struct Ranked { double i_over_sigma; const Partial *p; }; + std::vector ranked(n_obs); + ParallelChunks(static_cast(n_obs), nthreads, [&](int lo, int hi) { + for (int i = lo; i < hi; ++i) + ranked[i] = {obs[i]->I / std::max(1e-9, static_cast(obs[i]->sigma)), obs[i]}; + }); + std::nth_element(ranked.begin(), ranked.begin() + MAX_OBS, ranked.end(), + [](const Ranked &a, const Ranked &b) { return a.i_over_sigma > b.i_over_sigma; }); + for (size_t i = 0; i < MAX_OBS; ++i) + obs[i] = ranked[i].p; n_obs = MAX_OBS; } result.obs_used = static_cast(n_obs);