Files
Jungfraujoch/image_analysis/IntegrationStats.cpp
2025-06-11 19:53:33 +02:00

51 lines
1.5 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "IntegrationStats.h"
#include "Regression.h"
#include <cmath>
IntegrationStats::IntegrationStats(float d_max_A, float d_min_A, int32_t in_nshells) {
d_min = d_min_A;
d_max = d_max_A;
one_over_dmax = 1.0f / (d_max);
one_over_dmin = 1.0f / (d_min);
nshells = in_nshells;
I.resize(nshells);
I_sigma.resize(nshells);
count.resize(nshells);
}
void IntegrationStats::AddReflection(const Reflection &r) {
if (r.d == 0.0f)
return;
float one_over_d = 1.0f / (r.d);
int64_t shell = std::lround((one_over_d - one_over_dmax) / (one_over_dmin - one_over_dmax)
* static_cast<float>(nshells));
if (shell >= 0 && shell < nshells) {
I.at(shell) += r.I;
I_sigma.at(shell) += r.I / r.sigma;
count.at(shell) += 1;
}
}
float IntegrationStats::BFactor() {
std::vector<float> x;
std::vector<float> y;
for (int i = 0; i < nshells; i++) {
if (count[i] == 0 || I[i] < 0.0 || I_sigma[i] < 0.2)
continue;
float one_over_d = one_over_dmax
+ (static_cast<float>(i) + 0.5) * (one_over_dmin - one_over_dmax) / static_cast<float>(nshells);
x.push_back(one_over_d);
y.push_back(log(I[i] / count[i]));
}
if (!x.empty()) {
auto res = regression(x,y);
return -2.0f * res.slope;
} else
return 0.0;
}