stills: detector tilt refined in the geometry pre-pass, kept where held-out frames agree
The stills geometry bundle (RefineGlobalGeometry) can now refine the detector tilt together with the beam, the distance, the cell and the modules. With the tilt free the beam block is the direct beam, from which the PONI is worked out inside the residual: in PONI terms a tilt moves the whole pattern as a moved beam does (D tan(tilt)/pixel), more than the beam's 8 px box holds, while the direct beam does not move with it and the tilt is left with the keystone. The two tilt-like rows of ModuleGauge are gone: within a module a tilt is a gradient no rigid move makes, and the rows cost held-out accuracy. The shift, turn and outward gauges stay. The tilt is a hypothesis (DecideStillsTilt): the strong frames of the last geometry refinement are split in two halves, each fitted with the tilt held and refined (modules placed in both where the run placed them), each fit scored on the other half, and the tilt is kept where the refined fits predict the held-out spots better. No threshold. It is decided after the stills beam-centre trial and its re-refinement, on frames indexed at the settled centre: a centre error on frames indexed at it reads as a tilt. --rot1/--rot2 or --no-refine-tilt hold the tilt on stills. The stills beam-centre trial now compares direct beams. Its hypotheses (the file's centre, the background walk, the point-symmetry capture) are taken as the point the beam lands on and each trial puts the PONI where its distance and the run's tilt land the direct beam there; the walk, fitted through the geometry's tilt, returns a PONI and is converted. The pre-scan beam-centre check log compares the walk's direct beam, not its PONI, with the file's direct beam. Without a tilt nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
1 parent
f72950612a
commit
a39e18b80f
9 files changed
+319
-64
No files matched your search
@@ -139,12 +139,56 @@ void PlaceOnModule(const ModuleShift &m, const double move[3], double x, double
|
||||
py = m.y0 + s * ux + c * uy + move[1];
|
||||
}
|
||||
|
||||
// How a residual reads the beam block. Without the tilt refined it is the PONI, as everywhere else. With the
|
||||
// tilt refined it is the DIRECT BEAM, the point the undeflected beam lands on, and the PONI is worked out from
|
||||
// it: a tilt moves the whole pattern by D tan(tilt)/pixel, exactly as a moved PONI does, so in PONI terms
|
||||
// the two trade tens of pixels for each other (and the beam's box cannot hold the trade), while the direct
|
||||
// beam does not move with the tilt and the tilt is left with only what no beam can imitate - the keystone.
|
||||
// In XtalResidual's frame the direct beam is where M (u, v) = (-D tan rot1, -D tan rot2 / cos rot1), with
|
||||
// (u, v) its offset from the PONI in mm and M the image orientation (a mirror and quarter turns, so M^-1 = M^T).
|
||||
struct BeamBlock {
|
||||
bool direct = false;
|
||||
double m00 = 1, m01 = 0, m10 = 0, m11 = 1; // DetectorOrientation::Matrix(), as XtalResidual takes it
|
||||
double pixel = 1;
|
||||
|
||||
template <typename T>
|
||||
void Poni(const T *const beam, const T &distance_mm, const T *const detector_rot, T *poni) const {
|
||||
if (!direct) {
|
||||
poni[0] = beam[0];
|
||||
poni[1] = beam[1];
|
||||
return;
|
||||
}
|
||||
using std::cos;
|
||||
using std::tan;
|
||||
const T dx = -distance_mm * tan(detector_rot[0]);
|
||||
const T dy = -distance_mm * tan(detector_rot[1]) / cos(detector_rot[0]);
|
||||
poni[0] = beam[0] - (T(m00) * dx + T(m10) * dy) / T(pixel);
|
||||
poni[1] = beam[1] - (T(m01) * dx + T(m11) * dy) / T(pixel);
|
||||
}
|
||||
};
|
||||
|
||||
// XtalResidual with the beam block read through BeamBlock.
|
||||
struct BeamXtalResidual {
|
||||
XtalResidual residual;
|
||||
BeamBlock beam_block;
|
||||
|
||||
template <typename T>
|
||||
bool operator()(const T *const beam, const T *const distance_mm, const T *const detector_rot,
|
||||
const T *const rotation_axis, const T *const p0, const T *const p1, const T *const p2,
|
||||
T *out) const {
|
||||
T poni[2];
|
||||
beam_block.Poni(beam, distance_mm[0], detector_rot, poni);
|
||||
return residual(poni, distance_mm, detector_rot, rotation_axis, p0, p1, p2, out);
|
||||
}
|
||||
};
|
||||
|
||||
// XtalResidual for a spot on a module that is being placed: block `move` = (dx, dy, rot) of the module,
|
||||
// (ux, uy) the spot's offset from the module centre. Moving the spot by R(rot) u - u + (dx, dy) shifts its
|
||||
// offset from the beam exactly as moving the beam the other way does, so the beam handed to XtalResidual
|
||||
// carries the move and everything else is the residual of an unmoved spot.
|
||||
struct ModuleXtalResidual {
|
||||
XtalResidual residual;
|
||||
BeamBlock beam_block;
|
||||
double ux, uy;
|
||||
|
||||
template <typename T>
|
||||
@@ -153,9 +197,11 @@ struct ModuleXtalResidual {
|
||||
const T *const move, T *out) const {
|
||||
using std::cos;
|
||||
using std::sin;
|
||||
T poni[2];
|
||||
beam_block.Poni(beam, distance_mm[0], detector_rot, poni);
|
||||
const T c = cos(move[2]), s = sin(move[2]);
|
||||
const T moved_beam[2] = {beam[0] - ((c - T(1)) * T(ux) - s * T(uy) + move[0]),
|
||||
beam[1] - (s * T(ux) + (c - T(1)) * T(uy) + move[1])};
|
||||
const T moved_beam[2] = {poni[0] - ((c - T(1)) * T(ux) - s * T(uy) + move[0]),
|
||||
poni[1] - (s * T(ux) + (c - T(1)) * T(uy) + move[1])};
|
||||
return residual(moved_beam, distance_mm, detector_rot, rotation_axis, p0, p1, p2, out);
|
||||
}
|
||||
};
|
||||
@@ -167,36 +213,30 @@ struct ModuleXtalResidual {
|
||||
// weighted by the spots each module holds: a module the frames barely reach is placed poorly, and an
|
||||
// unweighted sum would hand its error to all the others.
|
||||
//
|
||||
// With the tilt refined, the two tilt-like patterns are held at zero as well: a detector tilted by a small
|
||||
// angle about y moves a point (x, y) (from the beam) by (x^2, x y) / D times the angle, one about x by
|
||||
// (x y, y^2) / D. Each is taken as the modules' moves projected on that field, in units of the lever.
|
||||
// The detector tilt is not among them. Within one module a tilt is a gradient (a scale and a shear across
|
||||
// it), which no rigid move of the module makes, and with the tilt refined the placement gains no new
|
||||
// direction it cannot tell apart; holding the modules' moves to no tilt-like pattern as well only cost
|
||||
// accuracy on frames the fit had not seen.
|
||||
struct ModuleGauge {
|
||||
std::vector<std::array<double, 2>> outward; // unit vector from the beam to each module centre
|
||||
std::vector<std::array<double, 2>> centre; // each module centre from the beam, over lever_px
|
||||
std::vector<double> share; // each module's fraction of the spots
|
||||
double lever_px; // turns are weighed at this distance from the centre
|
||||
double weight;
|
||||
bool tilt = false;
|
||||
|
||||
template <typename T>
|
||||
bool operator()(T const *const *move, T *out) const {
|
||||
T sx(0), sy(0), sr(0), so(0), t1(0), t2(0);
|
||||
T sx(0), sy(0), sr(0), so(0);
|
||||
for (size_t i = 0; i < outward.size(); ++i) {
|
||||
const T w(share[i]);
|
||||
sx += w * move[i][0];
|
||||
sy += w * move[i][1];
|
||||
sr += w * move[i][2] * T(lever_px);
|
||||
so += w * (move[i][0] * T(outward[i][0]) + move[i][1] * T(outward[i][1]));
|
||||
const double x = centre[i][0], y = centre[i][1];
|
||||
t1 += w * (move[i][0] * T(x * x) + move[i][1] * T(x * y));
|
||||
t2 += w * (move[i][0] * T(x * y) + move[i][1] * T(y * y));
|
||||
}
|
||||
out[0] = T(weight) * sx;
|
||||
out[1] = T(weight) * sy;
|
||||
out[2] = T(weight) * sr;
|
||||
out[3] = T(weight) * so;
|
||||
out[4] = tilt ? T(weight) * t1 : T(0);
|
||||
out[5] = tilt ? T(weight) * t2 : T(0);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -211,7 +251,8 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
try {
|
||||
const gemmi::CrystalSystem system = MapSystem(settings.crystal_system);
|
||||
|
||||
// Shared geometry parameter blocks (refined). Detector tilt and the rotation axis are constant.
|
||||
// Shared geometry parameter blocks (refined). The rotation axis is constant, and so is the detector tilt
|
||||
// unless settings.refine_tilt.
|
||||
double beam[2] = {nominal_geom.GetBeamX_pxl(), nominal_geom.GetBeamY_pxl()};
|
||||
double distance_mm = nominal_geom.GetDetectorDistance_mm();
|
||||
double detector_rot[2] = {nominal_geom.GetPoniRot1_rad(), nominal_geom.GetPoniRot2_rad()};
|
||||
@@ -223,6 +264,28 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
const double lambda = nominal_geom.GetWavelength_A();
|
||||
const double pixel = nominal_geom.GetPixelSize_mm();
|
||||
|
||||
// With the tilt refined, the beam block is the direct beam (BeamBlock), started and bounded where the
|
||||
// nominal geometry puts the direct beam.
|
||||
const bool refine_tilt = settings.refine_tilt && settings.refine_beam_distance;
|
||||
BeamBlock beam_block;
|
||||
if (refine_tilt) {
|
||||
const RotMatrix m = orientation.Matrix();
|
||||
beam_block = {true, m.Column(0).x, m.Column(1).x, m.Column(0).y, m.Column(1).y, pixel};
|
||||
double poni_at_zero[2];
|
||||
const double zero[2] = {0, 0};
|
||||
beam_block.Poni(zero, distance_mm, detector_rot, poni_at_zero);
|
||||
beam[0] -= poni_at_zero[0];
|
||||
beam[1] -= poni_at_zero[1];
|
||||
}
|
||||
const double beam0[2] = {beam[0], beam[1]};
|
||||
// The PONI the beam block stands for, at the current tilt and distance.
|
||||
auto poni = [&](double &x, double &y) {
|
||||
double p[2];
|
||||
beam_block.Poni(beam, distance_mm, detector_rot, p);
|
||||
x = p[0];
|
||||
y = p[1];
|
||||
};
|
||||
|
||||
// Shared cell blocks. Lengths refined (regularized); angles constant.
|
||||
double cell_len[3] = {input_cell.a, input_cell.b, input_cell.c};
|
||||
const double cell_len0[3] = {input_cell.a, input_cell.b, input_cell.c};
|
||||
@@ -304,8 +367,10 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
for (int round = 0; round < settings.rounds; ++round) {
|
||||
EffectiveCell(system, cell_len, cell_ang, eff_len, eff_alpha, eff_beta, eff_gamma);
|
||||
|
||||
double poni_x, poni_y;
|
||||
poni(poni_x, poni_y);
|
||||
DiffractionGeometry cur = nominal_geom;
|
||||
cur.BeamX_pxl(beam[0]).BeamY_pxl(beam[1]).DetectorDistance_mm(distance_mm)
|
||||
cur.BeamX_pxl(poni_x).BeamY_pxl(poni_y).DetectorDistance_mm(distance_mm)
|
||||
.PoniRot1_rad(static_cast<float>(detector_rot[0])).PoniRot2_rad(static_cast<float>(detector_rot[1]));
|
||||
|
||||
const double tol = round_tol(round);
|
||||
@@ -337,11 +402,18 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
new ceres::AutoDiffCostFunction<ModuleXtalResidual, 3, 2, 1, 2, 3, 3, 3, 3, 3>(
|
||||
new ModuleXtalResidual{XtalResidual(s.x, s.y, lambda, pixel, cos_rot3, sin_rot3,
|
||||
0.0, h, k, l, system, orientation),
|
||||
s.x - m.x0, s.y - m.y0}),
|
||||
beam_block, s.x - m.x0, s.y - m.y0}),
|
||||
new ceres::CauchyLoss(loss_scale),
|
||||
beam, &distance_mm, detector_rot, rot_vec, orient[i].data(), cell_len, cell_ang,
|
||||
move[s.module].data());
|
||||
} else
|
||||
} else if (refine_tilt)
|
||||
problem.AddResidualBlock(
|
||||
new ceres::AutoDiffCostFunction<BeamXtalResidual, 3, 2, 1, 2, 3, 3, 3, 3>(
|
||||
new BeamXtalResidual{XtalResidual(x, y, lambda, pixel, cos_rot3, sin_rot3, 0.0,
|
||||
h, k, l, system, orientation), beam_block}),
|
||||
new ceres::CauchyLoss(loss_scale),
|
||||
beam, &distance_mm, detector_rot, rot_vec, orient[i].data(), cell_len, cell_ang);
|
||||
else
|
||||
problem.AddResidualBlock(
|
||||
new ceres::AutoDiffCostFunction<XtalResidual, 3, 2, 1, 2, 3, 3, 3, 3>(
|
||||
new XtalResidual(x, y, lambda, pixel, cos_rot3, sin_rot3, 0.0,
|
||||
@@ -396,22 +468,18 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
const double oy = modules[m].y0 - nominal_geom.GetBeamY_pxl();
|
||||
const double r = std::max(1.0, std::hypot(ox, oy));
|
||||
gauge->outward.push_back({ox / r, oy / r});
|
||||
gauge->centre.push_back({ox, oy});
|
||||
gauge->share.push_back(module_spots[m] / total);
|
||||
lever += r;
|
||||
}
|
||||
gauge->lever_px = lever / static_cast<double>(moved.size());
|
||||
for (auto &c : gauge->centre)
|
||||
c = {c[0] / gauge->lever_px, c[1] / gauge->lever_px};
|
||||
gauge->weight = 10.0 * recip_per_px * sqrtN;
|
||||
gauge->tilt = settings.refine_tilt;
|
||||
auto *cost = new ceres::DynamicAutoDiffCostFunction<ModuleGauge, 4>(gauge);
|
||||
std::vector<double *> blocks;
|
||||
for (size_t m : moved) {
|
||||
cost->AddParameterBlock(3);
|
||||
blocks.push_back(move[m].data());
|
||||
}
|
||||
cost->SetNumResiduals(6);
|
||||
cost->SetNumResiduals(4);
|
||||
problem.AddResidualBlock(cost, nullptr, blocks);
|
||||
}
|
||||
for (size_t m : moved) {
|
||||
@@ -426,10 +494,10 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
|
||||
// Beam + distance bounds around the nominal value; cell lengths loosely bounded (the
|
||||
// regularizer does the anchoring).
|
||||
problem.SetParameterLowerBound(beam, 0, nominal_geom.GetBeamX_pxl() - settings.beam_range_px);
|
||||
problem.SetParameterUpperBound(beam, 0, nominal_geom.GetBeamX_pxl() + settings.beam_range_px);
|
||||
problem.SetParameterLowerBound(beam, 1, nominal_geom.GetBeamY_pxl() - settings.beam_range_px);
|
||||
problem.SetParameterUpperBound(beam, 1, nominal_geom.GetBeamY_pxl() + settings.beam_range_px);
|
||||
problem.SetParameterLowerBound(beam, 0, beam0[0] - settings.beam_range_px);
|
||||
problem.SetParameterUpperBound(beam, 0, beam0[0] + settings.beam_range_px);
|
||||
problem.SetParameterLowerBound(beam, 1, beam0[1] - settings.beam_range_px);
|
||||
problem.SetParameterUpperBound(beam, 1, beam0[1] + settings.beam_range_px);
|
||||
problem.SetParameterLowerBound(&distance_mm, 0,
|
||||
std::max(1.0, nominal_geom.GetDetectorDistance_mm() - settings.distance_range_mm));
|
||||
problem.SetParameterUpperBound(&distance_mm, 0,
|
||||
@@ -441,7 +509,7 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
|
||||
if (!settings.refine_distance)
|
||||
problem.SetParameterBlockConstant(&distance_mm);
|
||||
if (settings.refine_tilt && settings.refine_beam_distance) {
|
||||
if (refine_tilt) {
|
||||
const double tilt_range = 2.0 * PI / 180.0;
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
problem.SetParameterLowerBound(detector_rot, j, (j == 0 ? nominal_geom.GetPoniRot1_rad()
|
||||
@@ -478,8 +546,10 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
|
||||
// Final fit quality: median detector residual of the retained spots at the refined geometry.
|
||||
EffectiveCell(system, cell_len, cell_ang, eff_len, eff_alpha, eff_beta, eff_gamma);
|
||||
double poni_x, poni_y;
|
||||
poni(poni_x, poni_y);
|
||||
DiffractionGeometry cur = nominal_geom;
|
||||
cur.BeamX_pxl(beam[0]).BeamY_pxl(beam[1]).DetectorDistance_mm(distance_mm)
|
||||
cur.BeamX_pxl(poni_x).BeamY_pxl(poni_y).DetectorDistance_mm(distance_mm)
|
||||
.PoniRot1_rad(static_cast<float>(detector_rot[0])).PoniRot2_rad(static_cast<float>(detector_rot[1]));
|
||||
std::vector<double> residuals;
|
||||
constexpr double CAP_PX = 2.0;
|
||||
@@ -531,8 +601,8 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge
|
||||
}
|
||||
|
||||
result.ok = solve_usable && result.spots_used >= settings.min_spots_total && result.frames_used > 0;
|
||||
result.beam_x_px = beam[0];
|
||||
result.beam_y_px = beam[1];
|
||||
result.beam_x_px = poni_x;
|
||||
result.beam_y_px = poni_y;
|
||||
result.distance_mm = distance_mm;
|
||||
result.cell = BuildUnitCell(system, cell_len, cell_ang);
|
||||
result.rot1_rad = detector_rot[0];
|
||||
|
||||
Reference in new issue
Block a user