Files
Jungfraujoch/reader/JFJochMultiReader.cpp
leonarski_f 4809c1622c
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m43s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 28m46s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 32m5s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 39m44s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m7s
Build Packages / Create release (push) Successful in 8s
Build Packages / Build documentation (push) Successful in 49s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m30s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m29s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m51s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m7s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 29m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m6s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m44s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 33m22s
Build Packages / Generate python client (push) Successful in 47s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 34m6s
Build Packages / Unit tests (push) Successful in 1h2m47s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 35m59s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 34m8s
Build Packages / Validation Battery (push) Successful in 38m31s
v1.0.0-rc.187 (#98)
* Rugnux: Refinement several times faster, one protocol on the CPU and the GPU, parts of it on the Apple GPU; its random draws are the same on every platform.
* Rugnux: Refinement follows common practice: the free set decides only the restraint weights and the displacement model (isotropic, TLS or anisotropic, by Hamilton's test), everything else is decided on the working set; lower R-free for placed and predicted models.
* Rugnux: Restraint dictionaries via `--restraints` (or among the inputs) and more built-in ligands; a model whose space group is a subgroup of the data's is refined in its own group; reflections plus a model select `--mode refine` on their own.
* Rugnux: `-C` fixes the crystal system on the axes as typed; `-S` alone fixes the space group only, the cell is still found and reduced; a space group or cell stored in the input file is ignored, with a line in the log.
* Rugnux: More reliable multi-sweep merging; `--mode scale` merges several stills runs as one; `_plot.txt` and the report name each input; a second crystal form is reported for stills given a cell.
* jfjoch_broker (experimental): Live Rugnux on FPGA detectors (JUNGFRAU) takes the same path as a DECTRIS stream, rotation and serial alike, model step included, with analysis threads per GPU; frames waiting for the analysis can be viewed; no host stage drops a frame - a source faster than the analysis is held at the card.
* jfjoch_broker: Summation of more than 256 frames per image is split between the card and the host (`summation_mode` is then `hybrid`), and live Rugnux runs on such collections; pedestal runs take a receiver of their own.
* jfjoch_broker (experimental): `stills_downsample` analyses 1 of every N serial images (chosen by a hash; the first 2000 all; the others written without analysis; reset to 1 by `/initialize`); `max_spot_count` left out means 250 for per-image indexing and up to 1000 for live stills; the live status gives the merge before the model work, the fitted resolution and the anomalous peak height of each unexplained blob.
* jfjoch_broker: Without a `pcie` entry the broker runs on a mock of the card instead of the HLS simulation; its spot, ROI and azimuthal results are zero then.
* Web frontend: Redesigned Rugnux live page; Vite 8, TypeScript 6, Mol* 5.13 and dependency security updates.
* Tools: `jfjoch_fpga_stills_test` measures live stills on the cards with a stored dataset; `jfjoch_fpga_test` loses the HLS-simulation option.
* Other: FFTW no longer fetched or linked (the CPU FFT indexer keeps the name `FFTW`); PCIe driver now GPL-2.0-or-later and third-party notices complete; several crash fixes.

Reviewed-on: #98
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-19 20:49:28 +01:00

168 lines
7.7 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "JFJochMultiReader.h"
#include <algorithm>
#include <cmath>
#include "../common/JFJochException.h"
namespace {
// How far two inputs may state the geometry apart and still be one run: what two collections of one
// setup differ by in their headers, well under what a stills geometry refinement moves.
constexpr double WAVELENGTH_TOLERANCE = 1e-3; // relative
constexpr double DISTANCE_TOLERANCE_MM = 0.1;
constexpr double BEAM_TOLERANCE_PXL = 1.0;
void CheckSameGeometry(const DiffractionExperiment &a, const DiffractionExperiment &b, size_t input) {
auto refuse = [input](const std::string &what) {
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Input " + std::to_string(input + 1) + " is not at the geometry of input 1: "
+ what + " differs. Inputs read as one run must share one detector, "
"wavelength, distance and beam centre; process them separately");
};
if (a.GetXPixelsNum() != b.GetXPixelsNum() || a.GetYPixelsNum() != b.GetYPixelsNum()
|| std::fabs(a.GetPixelSize_mm() - b.GetPixelSize_mm()) > 1e-6)
refuse("the detector");
if (std::fabs(a.GetWavelength_A() - b.GetWavelength_A()) > WAVELENGTH_TOLERANCE * a.GetWavelength_A())
refuse("the wavelength");
if (std::fabs(a.GetDetectorDistance_mm() - b.GetDetectorDistance_mm()) > DISTANCE_TOLERANCE_MM)
refuse("the detector distance");
if (std::fabs(a.GetBeamX_pxl() - b.GetBeamX_pxl()) > BEAM_TOLERANCE_PXL
|| std::fabs(a.GetBeamY_pxl() - b.GetBeamY_pxl()) > BEAM_TOLERANCE_PXL)
refuse("the beam centre");
}
}
JFJochMultiReader::JFJochMultiReader(std::vector<std::unique_ptr<JFJochReader>> in, bool as_stills)
: inputs(std::move(in)) {
if (inputs.empty())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "No input to read");
const auto first = inputs[0]->GetDataset();
if (!first)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Input 1 holds no dataset");
auto dataset = std::make_shared<JFJochReaderDataset>(*first);
std::vector<uint32_t> mask = first->pixel_mask->GetMask();
dataset->efficiency.clear();
dataset->xfel_pulse_id.clear();
dataset->xfel_event_code.clear();
// The XFEL IDs are kept only where every input has one for each of its images.
bool xfel_ids = true;
for (size_t k = 0; k < inputs.size(); k++) {
const auto d = inputs[k]->GetDataset();
if (!d)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Input " + std::to_string(k + 1) + " holds no dataset");
// Rotation sweeps are not concatenated: their frames are one crystal turned, numbered by angle -
// unless they are to be read as stills.
if (const auto g = d->experiment.GetGoniometer(); !as_stills && g && g->GetIncrement_deg() != 0.0f)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Input " + std::to_string(k + 1) + " is a rotation sweep; only stills "
"collections are read together");
CheckSameGeometry(first->experiment, d->experiment, k);
const auto &m = d->pixel_mask->GetMask();
if (m.size() != mask.size())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Input " + std::to_string(k + 1) + " has a pixel mask of another size");
// A pixel is out where any input has it out.
for (size_t i = 0; i < mask.size(); i++)
mask[i] |= m[i];
const auto n = inputs[k]->GetNumberOfImages();
first_image.push_back(static_cast<int64_t>(number_of_images));
// An input stating no efficiency has every frame at 1, as the image pass reads a missing one.
auto efficiency = d->efficiency;
efficiency.resize(n, 1.0f);
dataset->efficiency.insert(dataset->efficiency.end(), efficiency.begin(), efficiency.end());
xfel_ids = xfel_ids && d->xfel_pulse_id.size() == n && d->xfel_event_code.size() == n;
if (xfel_ids) {
dataset->xfel_pulse_id.insert(dataset->xfel_pulse_id.end(), d->xfel_pulse_id.begin(), d->xfel_pulse_id.end());
dataset->xfel_event_code.insert(dataset->xfel_event_code.end(), d->xfel_event_code.begin(), d->xfel_event_code.end());
}
number_of_images += n;
}
// Per-image results a previous run stored in the first input's file are not the whole run's.
if (!xfel_ids) {
dataset->xfel_pulse_id.clear();
dataset->xfel_event_code.clear();
}
dataset->spot_count.clear();
dataset->spot_count_indexed.clear();
dataset->spot_count_low_res.clear();
dataset->spot_count_ice_rings.clear();
dataset->spot_count_ice_control.clear();
dataset->indexing_result.clear();
dataset->merge_result.clear();
dataset->indexing_lattice_count.clear();
dataset->bkg_estimate.clear();
dataset->pixel_sum.clear();
dataset->ice_ring_score.clear();
dataset->resolution_estimate.clear();
dataset->image_scale_factor.clear();
dataset->image_scale_cc.clear();
dataset->pixel_mask = std::make_shared<const PixelMask>(mask);
if (as_stills)
dataset->experiment.Goniometer(std::nullopt);
// Several collections are not one grid: the first input's grid would place the whole run's image
// numbers on its own raster and throw past its last element. The run keeps no grid map.
dataset->experiment.GridScan(std::nullopt);
SetStartMessage(dataset);
}
std::pair<size_t, int64_t> JFJochMultiReader::Locate(int64_t image_number) const {
if (image_number < 0 || image_number >= static_cast<int64_t>(number_of_images))
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Image number out of range");
const auto it = std::upper_bound(first_image.begin(), first_image.end(), image_number);
const auto k = static_cast<size_t>(it - first_image.begin()) - 1;
return {k, image_number - first_image[k]};
}
bool JFJochMultiReader::LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset, DataMessage &message,
std::vector<uint8_t> &buffer, int64_t image_number, bool update_dataset) {
const auto [k, local] = Locate(image_number);
// The input decodes the frame against its own dataset; the whole run's stays as it is.
auto input_dataset = std::const_pointer_cast<JFJochReaderDataset>(inputs[k]->GetDataset());
if (!inputs[k]->LoadImage_i(input_dataset, message, buffer, local, false))
return false;
message.number = image_number;
return true;
}
uint64_t JFJochMultiReader::GetNumberOfImages() const {
return number_of_images;
}
void JFJochMultiReader::Close() {
for (auto &i: inputs)
i->Close();
}
bool JFJochMultiReader::ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) {
const auto [k, local] = Locate(image_number);
return inputs[k]->ReadRawImage(local, image);
}
std::vector<SpotToSave> JFJochMultiReader::ReadSpots(int64_t image) const {
const auto [k, local] = Locate(image);
return inputs[k]->ReadSpots(local);
}
size_t JFJochMultiReader::GetNumberOfInputs() const {
return inputs.size();
}
int64_t JFJochMultiReader::FirstImage(size_t input) const {
return first_image.at(input);
}
uint64_t JFJochMultiReader::ImagesInInput(size_t input) const {
return inputs.at(input)->GetNumberOfImages();
}