diff --git a/docs/RUGNUX.md b/docs/RUGNUX.md index 8b3198a35..2b8c7f7e0 100644 --- a/docs/RUGNUX.md +++ b/docs/RUGNUX.md @@ -52,7 +52,7 @@ that, or to lift the per-image loop's default ceiling of 16 workers per GPU. Nothing more is needed to pick the workflow: a dataset carrying a **goniometer axis** is processed as a rotation sweep, one without as **independent stills**, and scaling and merging run by default -in both. A rotation run that merges — the default — leaves eight files next to each other: +in both. A rotation run that merges — the default — leaves nine files next to each other: ``` myrun.mtz merged intensities + French-Wilson amplitudes, for CCP4 / phenix @@ -63,6 +63,7 @@ myrun_unmerged.mtz every observation before scaling, for pointless / aimless / myrun_report.txt what the run determined: cell, space group, statistics, warnings myrun_plot.txt one row per image, for plotting how the crystal behaved over the sweep myrun_detector.jpg the detector with the pixel mask and the beam-stop shadow drawn on it +myrun_XDS.INP the geometry the run refined, as an XDS input - a starting point for XDS myrun_log.txt the full timestamped log of the run (debug level) ``` diff --git a/docs/RUGNUX_REPORT.md b/docs/RUGNUX_REPORT.md index d992bf483..13c2dad66 100644 --- a/docs/RUGNUX_REPORT.md +++ b/docs/RUGNUX_REPORT.md @@ -150,6 +150,9 @@ BEAM_CENTRE_SHIFT= 7.15 BEAM_CENTRE_TOLERANCE= 3.49 ``` +**`XDS_INPUT=`** in section 1 names `_XDS.INP`, the same geometry written as an XDS input +(rotation runs; see the tutorial's list of output files). + **Which pass.** A rotation run integrates twice — once at the geometry in the input file (the first half of the sweep but at least 120°, `--prepass-fraction`), then the whole sweep again at the post-refined geometry — and can integrate a third time if a guard rejects the second pass. There is **one** report, for the pass that became the canonical output, and `PASS=` / diff --git a/docs/RUGNUX_TUTORIAL.md b/docs/RUGNUX_TUTORIAL.md index 213fe8db5..8f68b2acd 100644 --- a/docs/RUGNUX_TUTORIAL.md +++ b/docs/RUGNUX_TUTORIAL.md @@ -331,6 +331,20 @@ Absorption is corrected only by the empirical surface a rotation run fits and ke is missing. A shadow drawn where the background is unbroken, or a dark patch the run left uncoloured, is visible at a glance; read it against the `Beam stop shadow: N pixels ...` line in the log. +- `_XDS.INP` — **the geometry the run ended on, as an XDS input**: on a rotation run the + post-refined beam centre (`ORGX`/`ORGY`, the foot of the perpendicular, numbered from 1 as XDS + numbers pixels), distance, detector axes (`DIRECTION_OF_DETECTOR_X-AXIS`/`Y-AXIS`, which carry the + tilt), wavelength, rotation axis (the one the run integrated at, after any sign rescue, negated: XDS turns + the crystal the other way about the axis it is given), oscillation + (scaled where the run adopted a goniometer rotation scale), starting angle and data range, plus the + space group and cell (left commented out where the group is not in the reference setting of its + number, which is the only setting a number names), `FRIEDEL'S_LAW` per `-A` and the merged + resolution range. For HDF5 the template is the master file and the `LIB=` line for the reader + plugin is left for the user to point at durin or neggia; the file's own pixel mask reaches XDS + through the plugin, the beam-stop shadow rugnux detected does not (XDS finds its own in `DEFPIX`). + The conventions are XDS's own (W. Kabsch, "XDS" (2010), Acta Cryst. D66, 125-132 + [doi:10.1107/S0907444909047337](https://doi.org/10.1107/S0907444909047337)). It is a starting + point - XDS refines the geometry again - and the report names it as **`XDS_INPUT=`** in section 1. Merged statistics (⟨I/σ⟩, CC1/2, completeness, …), the error model and timing are printed to the console. By default the written resolution is trimmed automatically where CC1/2 falls off diff --git a/rugnux/CMakeLists.txt b/rugnux/CMakeLists.txt index 6b437d83b..5db9f5764 100644 --- a/rugnux/CMakeLists.txt +++ b/rugnux/CMakeLists.txt @@ -23,6 +23,8 @@ ADD_LIBRARY(JFJochRugnux STATIC DiagnosticOutput.h BrokerFeed.cpp BrokerFeed.h + XDSInput.cpp + XDSInput.h ) TARGET_LINK_LIBRARIES(JFJochRugnux JFJochReader JFJochImageAnalysis JFJochWriter JFJochModelWriter JFJochPreview gemmi fftw3f diff --git a/rugnux/ResultReport.cpp b/rugnux/ResultReport.cpp index eca796bb3..8cfc7353d 100644 --- a/rugnux/ResultReport.cpp +++ b/rugnux/ResultReport.cpp @@ -548,6 +548,9 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, } if (result.spot_resolution_estimate_A.has_value()) Add(s, KeyReal("SPOT_RESOLUTION_ESTIMATE", *result.spot_resolution_estimate_A, "{:.2f}")); + // The same geometry once more, as an XDS input (rugnux/XDSInput.h). + if (!result.xds_input_path.empty()) + Add(s, KeyText("XDS_INPUT", result.xds_input_path)); if (result.pass_count > 1) { Add(s, KeyText("PASS", fmt::format("{} of {}", result.pass_number, result.pass_count))); Add(s, KeyText("PASS_DECISION", result.pass_decision, true)); diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index bfa4dd921..33dd36c9f 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -1075,6 +1075,8 @@ ProcessResult Rugnux::RunPipeline(RugnuxObserver *observer, bool write_output, b result.used_direct_beam_x_pxl = direct.first; result.used_direct_beam_y_pxl = direct.second; } + if (const auto g = experiment_.GetGoniometer()) + result.used_rotation_axis = g->GetAxis(); if (census_future.valid()) { TimingMark mark(logger, "leftover-lattice census wait"); result.leftover_lattices = census_future.get(); diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index fe821b0ce..cbe0bafbd 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -301,6 +301,9 @@ struct ProcessResult { // rotations, which is a geometry no pass ever ran at. std::array used_detector_tilt_deg{}; double used_direct_beam_x_pxl = 0.0, used_direct_beam_y_pxl = 0.0; + // The rotation axis the same pass integrated at - the sign rescue can adopt the opposite of the + // file's (see LogXDSGeometry). Empty on stills. + std::optional used_rotation_axis; std::optional consensus_cell; // The centring `consensus_cell` is written in; empty when nothing named one (the stills path // averages per-image cells and has no lattice search behind them). A conventional cell says @@ -326,6 +329,8 @@ struct ProcessResult { std::optional lattice_angle_departure_deg; MeanProcessingTime mean_processing_time{}; std::optional written_master_path; + // _XDS.INP, where the CLI wrote the run's geometry as an XDS input (rugnux/XDSInput.h). + std::string xds_input_path; std::string merge_statistics_text; // populated when scaling/merging ran // Structured merge statistics (per-shell + overall), populated when scaling/merging ran. ISa is diff --git a/rugnux/XDSInput.cpp b/rugnux/XDSInput.cpp new file mode 100644 index 000000000..e8361c8f8 --- /dev/null +++ b/rugnux/XDSInput.cpp @@ -0,0 +1,167 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#include +#include +#include +#include +#include + +#include + +#include "../common/GitInfo.h" +#include "../common/JFJochMath.h" +#include "XDSInput.h" + +std::string RenderXDSInput(const std::string &input_file, + const DiffractionExperiment &experiment, + const ProcessResult &result, + int64_t first_image, int64_t end_image) { + // The geometry the result was integrated at, on a copy of the file's experiment. + DiffractionExperiment x = experiment; + if (std::isfinite(result.used_beam_x_pxl) && std::isfinite(result.used_distance_mm)) { + constexpr double RAD = PI / 180.0; + const auto &t = result.used_detector_tilt_deg; + x.BeamX_pxl(result.used_beam_x_pxl).BeamY_pxl(result.used_beam_y_pxl) + .DetectorDistance_mm(result.used_distance_mm) + .PoniRot1_rad(static_cast(t[0] * RAD)).PoniRot2_rad(static_cast(t[1] * RAD)) + .PoniRot3_rad(static_cast(t[2] * RAD)); + } + const auto geom = x.GetDiffractionGeometry(); + const float beam_x = geom.GetBeamX_pxl(); + const float beam_y = geom.GetBeamY_pxl(); + const float pixel_mm = geom.GetPixelSize_mm(); + + // Following the XDS conventions, Kabsch (2010) Acta Cryst. D66, 125-132: XDS places a pixel at + // x_lab(i,j) = (i-ORGX)*QX*X_axis + (j-ORGY)*QY*Y_axis + DETECTOR_DISTANCE*(X_axis x Y_axis) + // in a laboratory frame that is ours (x along a column step, y along a row step, z along the + // beam), with pixels numbered from 1 and ORGX/ORGY the foot of the perpendicular from the crystal - + // our PONI (see LogXDSGeometry in Rugnux.cpp and docs/DETECTOR_GEOMETRY.md). So the tilt goes out + // as the two detector axes, taken as differences of LabCoord. + const Coord origin = geom.LabCoord(beam_x, beam_y); + const Coord x_axis = ((geom.LabCoord(beam_x + 1.0f, beam_y) - origin) / pixel_mm).Normalize(); + const Coord y_axis = ((geom.LabCoord(beam_x, beam_y + 1.0f) - origin) / pixel_mm).Normalize(); + + const bool hdf5 = input_file.ends_with(".h5") || input_file.ends_with(".nxs"); + const bool cbf = input_file.ends_with(".cbf") || input_file.ends_with(".cbf.gz"); + std::string input_path = input_file; + try { + input_path = std::filesystem::absolute(input_file).string(); + } catch (const std::exception &) { + } + + // A CBF sweep is one file per frame: XDS takes a template with ? for the frame number - the last run + // of digits in the name - and numbers the frames by it, so the data range starts at the given file's. + std::string template_path = input_path; + int64_t first_frame = 1; + // The HDF5 plugins take the master file's name with "master" as ??????, as XDS has it for + // an EIGER data set. + if (hdf5 && input_path.ends_with("_master.h5")) + template_path.replace(template_path.size() - 9, 6, "??????"); + if (cbf) { + const size_t name = template_path.find_last_of('/') + 1; + const size_t end = template_path.find_last_of("0123456789"); + if (end != std::string::npos && end >= name) { + size_t begin = end; + while (begin > name && std::isdigit(static_cast(template_path[begin - 1]))) + --begin; + first_frame = std::stoll(template_path.substr(begin, end - begin + 1)); + template_path.replace(begin, end - begin + 1, std::string(end - begin + 1, '?')); + } + } + + std::string s; + s += fmt::format("! XDS.INP written by rugnux {} from {}\n", jfjoch_version(), input_path); + s += "! The detector geometry is the one rugnux integrated at - on a rotation run the post-refined\n" + "! one - not the file's; XDS refines it again from here. Check before use: this file is a\n" + "! starting point, not a validated XDS setup.\n"; + s += "JOB= XYCORR INIT COLSPOT IDXREF DEFPIX INTEGRATE CORRECT\n\n"; + + if (hdf5) + s += "! HDF5 is read through a plugin: uncomment and point LIB= at durin-plugin.so (or the\n" + "! Dectris neggia plugin), which opens the _master.h5 the template names with ??????.\n" + "! The plugin applies the file's own pixel mask.\n" + "!LIB= /path/to/durin-plugin.so\n"; + else if (!cbf) + s += "! rugnux read this input itself; XDS may need the frames converted, and the template\n" + "! below written with ?????? in place of the frame number.\n"; + s += fmt::format("NAME_TEMPLATE_OF_DATA_FRAMES= {}\n", template_path); + s += fmt::format("DATA_RANGE= {} {}\n", first_frame + first_image, first_frame + end_image - 1); + s += fmt::format("SPOT_RANGE= {} {}\n\n", first_frame + first_image, first_frame + end_image - 1); + + s += fmt::format("DETECTOR= {} MINIMUM_VALID_PIXEL_VALUE= 0 OVERLOAD= {}\n", + cbf ? "PILATUS" : "EIGER", SaturationValueFromLimit(experiment.GetSaturationLimit())); + s += fmt::format("NX= {} NY= {} QX= {:.6f} QY= {:.6f}\n", + experiment.GetXPixelsNumConv(), experiment.GetYPixelsNumConv(), pixel_mm, pixel_mm); + const float thickness_um = experiment.GetDetectorSetup().GetSensorThickness_um(); + if (thickness_um > 0.0f) + s += fmt::format("SENSOR_THICKNESS= {:.3f}\n", thickness_um / 1000.0f); + s += fmt::format("ORGX= {:.2f} ORGY= {:.2f}\n", beam_x + 1.0f, beam_y + 1.0f); + s += fmt::format("DETECTOR_DISTANCE= {:.3f}\n", geom.GetDetectorDistance_mm()); + s += fmt::format("DIRECTION_OF_DETECTOR_X-AXIS= {:.6f} {:.6f} {:.6f}\n", x_axis.x, x_axis.y, x_axis.z); + s += fmt::format("DIRECTION_OF_DETECTOR_Y-AXIS= {:.6f} {:.6f} {:.6f}\n", y_axis.x, y_axis.y, y_axis.z); + s += "INCIDENT_BEAM_DIRECTION= 0 0 1\n"; + s += fmt::format("X-RAY_WAVELENGTH= {:.6f}\n", geom.GetWavelength_A()); + s += "! Polarization is not in the file; a typical synchrotron:\n" + "!FRACTION_OF_POLARIZATION= 0.99 POLARIZATION_PLANE_NORMAL= 0 1 0\n\n"; + + if (const auto gonio = experiment.GetGoniometer()) { + // Negated: rugnux turns the crystal by -phi about its axis (IndexAndRefine takes the lattice + // to an image with GetTransformationAngle(-angle)), XDS by a right-handed +phi about + // ROTATION_AXIS. Checked with IDXREF on the export: 99.5% of the spots index with the + // negated axis, 37% (the chance floor of a mis-signed spindle) with rugnux's own. + const Coord axis = -result.used_rotation_axis.value_or(gonio->GetAxis()); + // The oscillation the run integrated at: the file's, scaled where the run adopted a goniometer + // rotation scale (as OSCILLATION_RANGE in the report). + const double scale = result.post_refine && result.post_refine->rotation_scale_suspect + ? result.post_refine->rotation_scale : 1.0; + // + 0.0f: no "-0.000000" from a negated zero. + s += fmt::format("ROTATION_AXIS= {:.6f} {:.6f} {:.6f}\n", axis.x + 0.0f, axis.y + 0.0f, axis.z + 0.0f); + s += fmt::format("OSCILLATION_RANGE= {:.6f}\n", gonio->GetIncrement_deg() * scale); + s += fmt::format("STARTING_FRAME= {}\n", first_frame); + s += fmt::format("STARTING_ANGLE= {:.4f}\n\n", gonio->GetStart_deg()); + } + + if (result.space_group && result.consensus_cell) { + const auto &c = *result.consensus_cell; + // A number names only the reference setting; a cell in another one would be read wrongly. + const char *prefix = result.space_group->is_reference_setting() ? "" : "!"; + if (*prefix) + s += fmt::format("! The run's space group {} is not in the reference setting of its number;\n" + "! XDS would read the cell below in the wrong setting, so both are left out.\n", + result.space_group->xhm()); + s += fmt::format("{}SPACE_GROUP_NUMBER= {}\n", prefix, result.space_group->number); + s += fmt::format("{}UNIT_CELL_CONSTANTS= {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f}\n", prefix, + c.a, c.b, c.c, c.alpha, c.beta, c.gamma); + } + s += fmt::format("FRIEDEL'S_LAW= {}\n", experiment.GetScalingSettings().GetMergeFriedel() ? "TRUE" : "FALSE"); + if (result.has_merge_statistics) + s += fmt::format("INCLUDE_RESOLUTION_RANGE= {:.3f} {:.3f}\n", + result.merge_statistics.overall.d_max, result.merge_statistics.overall.d_min); + s += "\n! rugnux also masked the beam-stop shadow it detected (_detector.jpg); XDS finds its\n" + "! own in DEFPIX, or add UNTRUSTED_RECTANGLE= / UNTRUSTED_QUADRILATERAL= lines here.\n"; + return s; +} + +std::string WriteXDSInput(const std::string &output_prefix, + const std::string &input_file, + const DiffractionExperiment &experiment, + const ProcessResult &result, + int64_t first_image, int64_t end_image, + Logger &logger) { + if (output_prefix.empty() || !experiment.GetGoniometer()) + return {}; + const std::string filename = output_prefix + "_XDS.INP"; + try { + std::ofstream f(filename); + if (!f) + throw std::runtime_error("cannot open for writing"); + f << RenderXDSInput(input_file, experiment, result, first_image, end_image); + if (!f) + throw std::runtime_error("write failed"); + } catch (const std::exception &e) { + logger.Warning("Could not write the XDS input {}: {}", filename, e.what()); + return {}; + } + return filename; +} diff --git a/rugnux/XDSInput.h b/rugnux/XDSInput.h new file mode 100644 index 000000000..393cfd7ef --- /dev/null +++ b/rugnux/XDSInput.h @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +#include +#include + +#include "../common/DiffractionExperiment.h" +#include "../common/Logger.h" +#include "Rugnux.h" + +// _XDS.INP: the geometry the run integrated at - on a rotation run the post-refined one - as an +// XDS input, so the same data can be processed by XDS from where rugnux ended rather than from the +// header. `experiment` is the one the run was given (the file's), `result` carries what the run +// refined; the images are the file's 0-based [first_image, end_image). +std::string RenderXDSInput(const std::string &input_file, + const DiffractionExperiment &experiment, + const ProcessResult &result, + int64_t first_image, int64_t end_image); + +// Writes _XDS.INP and returns its name; returns an empty string, writing nothing, on a +// run with no output prefix or no rotation, and logs and swallows a failure to write. +std::string WriteXDSInput(const std::string &output_prefix, + const std::string &input_file, + const DiffractionExperiment &experiment, + const ProcessResult &result, + int64_t first_image, int64_t end_image, + Logger &logger); diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index 9e74ef787..4e9d30bc5 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -29,6 +29,7 @@ #include "../reader/JFJochSMVReader.h" #include "../reader/JFJochStartMessageReader.h" #include "BrokerFeed.h" +#include "XDSInput.h" #include "../writer/HDF5NXmx.h" #include #include @@ -3063,6 +3064,10 @@ static int RunRugnux(int argc, char **argv) { provenance.wall_time_s = std::chrono::duration( std::chrono::steady_clock::now() - invocation_start).count(); TimingMark report_mark(logger, "results report"); + // The geometry the run ended on, as an XDS input beside the report, which names it. + if (rotation_indexing) + result.xds_input_path = WriteXDSInput(output_prefix, input_file, experiment, result, start_image, + end_image, logger); const ReportDocument doc = WriteResultReport(output_prefix, input_file, experiment, result, logger, provenance); report_mark.Stop(); diff --git a/tests/ResultReportTest.cpp b/tests/ResultReportTest.cpp index cba64eef1..64d68342b 100644 --- a/tests/ResultReportTest.cpp +++ b/tests/ResultReportTest.cpp @@ -7,6 +7,7 @@ #include "../image_analysis/scale_merge/Merge.h" #include "../reader/JFJochHDF5Reader.h" #include "../rugnux/ResultReport.h" +#include "../rugnux/XDSInput.h" #include "../common/CUDAWrapper.h" #include "../writer/FileWriter.h" @@ -1263,3 +1264,36 @@ TEST_CASE("ResultReport_OverloadsAndOverlaps", "[Diagnostics]") { CHECK(text.find("Attenuate the beam") != std::string::npos); CHECK(text.find("Move the detector back") != std::string::npos); } + +TEST_CASE("XDSInput_Render", "[Diagnostics]") { + DiffractionExperiment x(DetJF(1)); + x.BeamX_pxl(500.0f).BeamY_pxl(400.0f).DetectorDistance_mm(100.0f); + x.Goniometer(GoniometerAxis("omega", 10.0f, 0.1f, Coord(1, 0, 0), {})); + ProcessResult result; + // The refined geometry, not the file's, goes out - 1-based, untilted here. + result.used_beam_x_pxl = 510.0f; + result.used_beam_y_pxl = 395.5f; + result.used_distance_mm = 120.0f; + result.used_rotation_axis = Coord(-1, 0, 0); + + const auto text = RenderXDSInput("/data/x_master.h5", x, result, 0, 900); + CHECK(text.find("\nORGX= 511.00 ORGY= 396.50\n") != std::string::npos); + CHECK(text.find("\nDETECTOR_DISTANCE= 120.000\n") != std::string::npos); + CHECK(text.find("\nDIRECTION_OF_DETECTOR_X-AXIS= 1.000000 0.000000 0.000000\n") != std::string::npos); + // XDS turns the crystal the other way about the axis it is given. + CHECK(text.find("\nROTATION_AXIS= 1.000000 0.000000 0.000000\n") != std::string::npos); + CHECK(text.find("\nNAME_TEMPLATE_OF_DATA_FRAMES= /data/x_??????.h5\n") != std::string::npos); + CHECK(text.find("\nDATA_RANGE= 1 900\n") != std::string::npos); + CHECK(text.find("\nSTARTING_ANGLE= 10.0000\n") != std::string::npos); + CHECK(text.find("\nOSCILLATION_RANGE= 0.100000\n") != std::string::npos); +} + +TEST_CASE("XDSInput_CBFTemplate", "[Diagnostics]") { + DiffractionExperiment x(DetJF(1)); + x.Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, Coord(1, 0, 0), {})); + const auto text = RenderXDSInput("/data/run_2_0101.cbf", x, ProcessResult{}, 0, 50); + CHECK(text.find("\nNAME_TEMPLATE_OF_DATA_FRAMES= /data/run_2_????.cbf\n") != std::string::npos); + CHECK(text.find("\nDATA_RANGE= 101 150\n") != std::string::npos); + CHECK(text.find("\nSTARTING_FRAME= 101\n") != std::string::npos); + CHECK(text.find("\nDETECTOR= PILATUS") != std::string::npos); +}