diff --git a/image_analysis/scale_merge/StillsPartialityRefine.cpp b/image_analysis/scale_merge/StillsPartialityRefine.cpp index 327daff84..35d55eb5f 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.cpp +++ b/image_analysis/scale_merge/StillsPartialityRefine.cpp @@ -53,6 +53,32 @@ namespace { double VecLen(double x, double y, double z) { return std::sqrt(x * x + y * y + z * z); } + // The rocking width sigma^2 at a reflection of resolution d* (and |q_z|, for the bandwidth term): the + // run's table where there is one - linear in d* between its shells, held beyond the outermost - else + // the three-term formula. + struct Width { + double gamma0, gamma_e, bw; + const std::vector *table_dstar = nullptr, *table_s = nullptr; + + double Sigma2(double dstar, double qz) const { + if (table_dstar && !table_dstar->empty()) { + const auto &x = *table_dstar; + const auto &y = *table_s; + double s = y.front(); + if (dstar >= x.back()) + s = y.back(); + else if (dstar > x.front()) { + const size_t i = std::upper_bound(x.begin(), x.end(), dstar) - x.begin(); + s = y[i - 1] + (dstar - x[i - 1]) / (x[i] - x[i - 1]) * (y[i] - y[i - 1]); + } + return s * s; + } + const double sig_ang = gamma_e * dstar; + const double sbw = bw * std::fabs(qz); + return gamma0 * gamma0 + sig_ang * sig_ang + sbw * sbw; + } + }; + // Analytic partiality for a reflection whose base reciprocal vector is q, tilted by (psi_x, psi_y). // Mirrors BraggPrediction: dist_ewald = |S| - 1/lambda with S = q_rot + S0, and // p = exp(-dist_ewald^2 / 2 sigma^2), sigma^2 = gamma0^2 + (gamma_e*d*)^2 + (bw*|q_z|)^2 - three @@ -63,7 +89,7 @@ namespace { double ComputeP(double qx, double qy, double qz, double psi_x, double psi_y, double s0x, double s0y, double s0z, double inv_lambda, - double gamma0, double gamma_e, double bw) { + const Width &width) { double aa[3] = {psi_x, psi_y, 0.0}; double q[3] = {qx, qy, qz}; double qr[3]; @@ -71,9 +97,7 @@ namespace { const double Sx = qr[0] + s0x, Sy = qr[1] + s0y, Sz = qr[2] + s0z; const double de = std::sqrt(Sx * Sx + Sy * Sy + Sz * Sz) - inv_lambda; const double dstar = std::sqrt(qr[0] * qr[0] + qr[1] * qr[1] + qr[2] * qr[2]); - const double sig_ang = gamma_e * dstar; - const double sbw = bw * std::fabs(qr[2]); - const double sig2 = gamma0 * gamma0 + sig_ang * sig_ang + sbw * sbw; + const double sig2 = width.Sigma2(dstar, qr[2]); if (!(sig2 > 0.0)) return 1.0; return std::exp(-0.5 * de * de / sig2); @@ -149,16 +173,14 @@ namespace { // ComputeP with the tilt given as its rotation matrix. struct TiltModel { double s0x, s0y, s0z, inv_lambda; - double gamma0, gamma_e, bw; + Width width; double P(const std::array &R, const FitObs &o) const { const double qx = R[0] * o.qx + R[1] * o.qy + R[2] * o.qz; const double qy = R[3] * o.qx + R[4] * o.qy + R[5] * o.qz; const double qz = R[6] * o.qx + R[7] * o.qy + R[8] * o.qz; const double de = VecLen(qx + s0x, qy + s0y, qz + s0z) - inv_lambda; - const double sig_ang = gamma_e * VecLen(qx, qy, qz); - const double sbw = bw * std::fabs(qz); - const double sig2 = gamma0 * gamma0 + sig_ang * sig_ang + sbw * sbw; + const double sig2 = width.Sigma2(VecLen(qx, qy, qz), qz); if (!(sig2 > 0.0)) return 1.0; return std::exp(-0.5 * de * de / sig2); @@ -346,12 +368,15 @@ double StillsPartialityRefine::RefineOne(IntegrationOutcome &outcome, gamma_e = std::max(run_width->gamma_e, 1e-9); bw = run_width->bandwidth_sigma; } + const Width width{.gamma0 = gamma0, .gamma_e = gamma_e, .bw = bw, + .table_dstar = run_width ? &run_width->table_dstar : nullptr, + .table_s = run_width ? &run_width->table_s : nullptr}; double psi[2] = {0.0, 0.0}; double G = 1.0; const TiltModel model{.s0x = S0.x, .s0y = S0.y, .s0z = S0.z, .inv_lambda = inv_lambda, - .gamma0 = gamma0, .gamma_e = gamma_e, .bw = bw}; + .width = width}; const bool refine_tilt = obs.size() >= settings_.min_reflections; const int inner = refine_tilt ? settings_.inner_iterations : 1; @@ -360,7 +385,7 @@ double StillsPartialityRefine::RefineOne(IntegrationOutcome &outcome, std::vector coeff(obs.size()); for (size_t j = 0; j < obs.size(); ++j) { const double p = ComputeP(obs[j].qx, obs[j].qy, obs[j].qz, psi[0], psi[1], - S0.x, S0.y, S0.z, inv_lambda, gamma0, gamma_e, bw); + S0.x, S0.y, S0.z, inv_lambda, width); coeff[j] = p * obs[j].inv_corr * obs[j].Iref; } G = SolveScaleIRLS(obs, coeff, settings_.robust_k); @@ -384,8 +409,7 @@ double StillsPartialityRefine::RefineOne(IntegrationOutcome &outcome, // (partiality * G), the same composition ScaleOnTheFly writes. for (auto &r: outcome.reflections) { const Coord q = base_q(r); - const double p = ComputeP(q.x, q.y, q.z, psi[0], psi[1], S0.x, S0.y, S0.z, inv_lambda, - gamma0, gamma_e, bw); + const double p = ComputeP(q.x, q.y, q.z, psi[0], psi[1], S0.x, S0.y, S0.z, inv_lambda, width); r.partiality = static_cast(p); const float corr = r.prescaling_corr * r.qe_corr * r.flight_corr; const double denom = p * G; @@ -529,23 +553,19 @@ std::optional StillsPartialityRefine::MeasureR // where that sum is measured to 10 % (sum I / sigma(sum I) >= 10): beyond the diffraction limit the // sum is noise and can even be negative, and says nothing about the width. constexpr double MIN_SHELL_SIGNAL = 10.0; - std::vector sx, sy; - for (size_t k = 0; k < N_SHELLS; ++k) { - const size_t lo = k * pts.size() / N_SHELLS, hi = (k + 1) * pts.size() / N_SHELLS; - double sumx = 0.0; - for (size_t i = lo; i < hi; ++i) - sumx += pts[i].x; - std::sort(pts.begin() + lo, pts.begin() + hi, - [](const Point &a, const Point &b) { return a.window < b.window; }); + // The width of the points [lo, hi) of pts (sorted by d*): the median of the window groups' widths. + auto shell_width = [&](size_t lo, size_t hi) -> std::optional { + std::vector shell(pts.begin() + lo, pts.begin() + hi); + std::sort(shell.begin(), shell.end(), [](const Point &a, const Point &b) { return a.window < b.window; }); std::vector widths; for (size_t g = 0; g < WINDOW_GROUPS; ++g) { - const size_t glo = lo + g * (hi - lo) / WINDOW_GROUPS, ghi = lo + (g + 1) * (hi - lo) / WINDOW_GROUPS; + const size_t glo = g * shell.size() / WINDOW_GROUPS, ghi = (g + 1) * shell.size() / WINDOW_GROUPS; double sw = 0.0, svar = 0.0, swde2 = 0.0, sde2 = 0.0; for (size_t i = glo; i < ghi; ++i) { - sw += pts[i].w; - svar += pts[i].var_w; - swde2 += pts[i].w * pts[i].de2; - sde2 += pts[i].de2; + sw += shell[i].w; + svar += shell[i].var_w; + swde2 += shell[i].w * shell[i].de2; + sde2 += shell[i].de2; } if (!(sw >= MIN_SHELL_SIGNAL * std::sqrt(svar)) || !(swde2 > 0.0)) continue; @@ -553,16 +573,84 @@ std::optional StillsPartialityRefine::MeasureR widths.push_back(*v); } if (widths.empty()) - continue; + return std::nullopt; std::sort(widths.begin(), widths.end()); const size_t m = widths.size(); + return m % 2 ? widths[m / 2] : 0.5 * (widths[m / 2 - 1] + widths[m / 2]); + }; + + // For the three-term formula (the prediction window): shells of equal count in d*^2. + std::vector sx, sy; + for (size_t k = 0; k < N_SHELLS; ++k) { + const size_t lo = k * pts.size() / N_SHELLS, hi = (k + 1) * pts.size() / N_SHELLS; + const auto y = shell_width(lo, hi); + if (!y) + continue; + double sumx = 0.0; + for (size_t i = lo; i < hi; ++i) + sumx += pts[i].x; const double x = sumx / static_cast(hi - lo); - const double y = m % 2 ? widths[m / 2] : 0.5 * (widths[m / 2 - 1] + widths[m / 2]); sx.push_back(x); - sy.push_back(y); - logger.Debug("Stills rocking width at d = {:.2f} A: {:.2e} 1/A ({:.3f} deg), {} of {} window groups", - 1.0 / std::sqrt(x), std::sqrt(y), std::sqrt(y / x) * kRadToDeg, m, WINDOW_GROUPS); + sy.push_back(*y); + logger.Debug("Stills rocking width at d = {:.2f} A: {:.2e} 1/A ({:.3f} deg)", + 1.0 / std::sqrt(x), std::sqrt(*y), std::sqrt(*y / x) * kRadToDeg); } + + // For the partiality: shells of equal width in d*, kept as a table. Equal-count shells crowd where + // the reflections are - on a run predicting ~1000 reflections a frame, all eight sat at 3.8-1.8 A, so + // the low-resolution width was the formula's extrapolation, and the three-term fit flipped between + // forms with the number of frames (to a pure bandwidth at 50k frames). Measured where it is used and + // interpolated, the width needs no form. A wider curve at finer resolution is the physics (every term + // grows with d*), so the table is made non-decreasing (pool adjacent violators, weighted by the + // shells' summed signal). + std::vector table_dstar, table_s, table_weight; + { + const double lo_d = std::sqrt(pts.front().x), hi_d = std::sqrt(pts.back().x); + size_t lo = 0; + for (size_t k = 0; k < N_SHELLS; ++k) { + const double edge = lo_d + (hi_d - lo_d) * static_cast(k + 1) / N_SHELLS; + size_t hi = lo; + while (hi < pts.size() && (k + 1 == N_SHELLS || std::sqrt(pts[hi].x) < edge)) + ++hi; + double sw = 0.0, svar = 0.0, sds = 0.0; + for (size_t i = lo; i < hi; ++i) { + sw += pts[i].w; + svar += pts[i].var_w; + sds += std::sqrt(pts[i].x); + } + if (hi - lo >= 400 && sw >= MIN_SHELL_SIGNAL * std::sqrt(svar)) + if (const auto y = shell_width(lo, hi)) { + table_dstar.push_back(sds / static_cast(hi - lo)); + table_s.push_back(std::sqrt(*y)); + table_weight.push_back(sw / std::sqrt(svar)); + } + lo = hi; + } + std::vector block_s, block_w; + std::vector block_n; + for (size_t i = 0; i < table_s.size(); ++i) { + block_s.push_back(table_s[i]); + block_w.push_back(table_weight[i]); + block_n.push_back(1); + while (block_s.size() > 1 && block_s[block_s.size() - 2] > block_s.back()) { + const size_t j = block_s.size() - 2; + block_s[j] = (block_s[j] * block_w[j] + block_s.back() * block_w.back()) / (block_w[j] + block_w.back()); + block_w[j] += block_w.back(); + block_n[j] += block_n.back(); + block_s.pop_back(); + block_w.pop_back(); + block_n.pop_back(); + } + } + table_s.clear(); + for (size_t j = 0; j < block_s.size(); ++j) + table_s.insert(table_s.end(), block_n[j], block_s[j]); + if (table_dstar.size() < 3) { + table_dstar.clear(); + table_s.clear(); + } + } + if (sx.size() < 3) return std::nullopt; @@ -606,9 +694,14 @@ std::optional StillsPartialityRefine::MeasureR // The d*^4 term is a bandwidth: its radial smear bw |q_z|, with |q_z| = lambda d*^2 / 2 on the sphere. const RunWidth width{.gamma0 = std::sqrt(best[0]), .gamma_e = std::sqrt(best[1]), .bandwidth_sigma = 2.0 * std::sqrt(best[2]) / experiment_.GetWavelength_A(), - .radial_scale = radial_scale}; + .radial_scale = radial_scale, .table_dstar = table_dstar, .table_s = table_s}; logger.Info("Stills rocking width measured: {:.2e} 1/A + {:.3f} deg angular + {:.4f} bandwidth (FWHM)", width.gamma0, width.gamma_e * kRadToDeg, width.bandwidth_sigma * 2.3548); + std::string table; + for (size_t i = 0; i < table_dstar.size(); ++i) + table += fmt::format(" {:.2f} A {:.2e}", 1.0 / table_dstar[i], table_s[i]); + logger.Info("Stills rocking width for the partiality, against resolution (1/A):{}", + table.empty() ? " none, the formula is used" : table); return width; } diff --git a/image_analysis/scale_merge/StillsPartialityRefine.h b/image_analysis/scale_merge/StillsPartialityRefine.h index 9d3344350..38c63f43c 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.h +++ b/image_analysis/scale_merge/StillsPartialityRefine.h @@ -49,9 +49,12 @@ public: // relative), measured from the intensity-weighted excitation errors with the reciprocal lattice scaled // by radial_scale (the curve's centre, MeasureRadialScale); empty when the run has too few reflections // to measure it. + // table_s is the width measured in shells of equal width in d* (at the shells' mean d*, non-decreasing), + // which the partiality uses where it is not empty; the formula is what the prediction window uses. struct RunWidth { double gamma0, gamma_e, bandwidth_sigma; double radial_scale = 1.0; + std::vector table_dstar, table_s; }; // Refine all crystals in place. Returns the mean |dpsi| applied (degrees), for diagnostics.