// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "RingBackground.h" #include #include RingBackground::RingBackground(const std::vector &ring_bkg, const std::vector &q_centre) { const size_t q_bins = q_centre.size(); if (q_bins == 0) return; std::vector sum(q_bins, 0.0); std::vector cnt(q_bins, 0); for (size_t i = 0; i < ring_bkg.size(); ++i) if (std::isfinite(ring_bkg[i])) { sum[i % q_bins] += ring_bkg[i]; ++cnt[i % q_bins]; } for (size_t i = 0; i < q_bins; ++i) if (cnt[i] > 0) { q.push_back(q_centre[i]); b.push_back(sum[i] / cnt[i]); } } double RingBackground::At(double q_recipA) const { if (q_recipA <= q.front()) return b.front(); if (q_recipA >= q.back()) return b.back(); const size_t j = std::upper_bound(q.begin(), q.end(), q_recipA) - q.begin() - 1; return b[j] + (b[j + 1] - b[j]) * (q_recipA - q[j]) / (q[j + 1] - q[j]); }