diff --git a/common/DiffractionExperiment.cpp b/common/DiffractionExperiment.cpp index 639d3399d..6576c5e0e 100644 --- a/common/DiffractionExperiment.cpp +++ b/common/DiffractionExperiment.cpp @@ -761,6 +761,10 @@ void DiffractionExperiment::FillMessage(StartMessage &message) const { message.write_master_file = dataset.IsWriteNXmxHDF5Master(); message.overwrite = file_writer.IsOverwriteExistingFiles(); message.file_format = file_writer.GetFileFormat(); + // The receiver records the pulse ID and event code of every image of a pulsed source; the master then + // carries them too (/entry/xfel), not only the data files. + if (IsPulsedSource()) + message.xfel_pulse_id = true; message.ring_current_mA = dataset.GetRingCurrent_mA(); message.sample_temperature_K = dataset.GetSampleTemperature_K(); message.fluorescence_spectrum = dataset.GetFluorescenceSpectrum(); diff --git a/docs/HDF5.md b/docs/HDF5.md index dc19c8fcb..3e2c00ffe 100644 --- a/docs/HDF5.md +++ b/docs/HDF5.md @@ -113,7 +113,8 @@ dataset and writes its results to a master file named **`_process.h5`**. **integrated** format, but instead of copying the images its `/entry/data/data` is a *virtual dataset that links back to the original image files* (`hdf5_source_data` → `NXmx::LinkToData_ProcessingVDS`). The result is a compact, self-describing companion file that -holds *all* the derived analysis (everything in §4) plus a virtual view +holds *all* the derived analysis (everything in §4, including the XFEL pulse IDs of §4.8 when the +input has them) plus a virtual view of the raw images — without duplicating terabytes of data. This is a particularly FAIR-friendly artefact: it can be shared or archived alongside (or instead @@ -566,7 +567,16 @@ A convenience NXcollection in the data file (note: distinct from the standard ### 4.8 `/entry/xfel` — pulsed-source metadata -`[n_images]` vectors `pulseID` and `eventCode`, written for pulsed sources (e.g. SwissFEL). +`[n_images]` vectors `pulseID` (uint64) and `eventCode` (uint32), written for pulsed sources (e.g. +SwissFEL): the XFEL pulse ID and the event code of each image, as the detector's timing receiver +recorded them. A pulse ID of `UINT64_MAX` marks an image whose modules disagreed on it. They are written to +the data files and, for a VDS master, mapped into the master as well. + +In a `_process.h5`, `/entry/xfel/pulseID` and `/entry/xfel/eventCode` hold the IDs of the images +processed, in the file's own image order: entry `i` belongs to the image whose analysis is at index `i` +(`/entry/MX/...[i]`, `/entry/reflections/image_`), the source image `/entry/detector/number[i]`. With +a subset or a stride (`-s`/`-e`/`--stride`) only the processed images are there; several inputs read as +one keep them only where every input has them. ### 4.9 Other collections diff --git a/reader/HDF5MetadataSource.cpp b/reader/HDF5MetadataSource.cpp index b7811f7cf..4d3dd97fc 100644 --- a/reader/HDF5MetadataSource.cpp +++ b/reader/HDF5MetadataSource.cpp @@ -572,6 +572,9 @@ HDF5MetadataSource::OpenResult HDF5MetadataSource::Open(const std::string &filen else dataset->efficiency = std::vector(number_of_images, 1.0); + dataset->xfel_pulse_id = master_file->ReadOptVector("/entry/xfel/pulseID"); + dataset->xfel_event_code = master_file->ReadOptVector("/entry/xfel/eventCode"); + if (master_file->Exists("/entry/roi")) dataset->roi = master_file->FindLeafs("/entry/roi"); @@ -735,6 +738,11 @@ HDF5MetadataSource::OpenResult HDF5MetadataSource::Open(const std::string &filen number_of_images, fimages); } + ReadVector(dataset->xfel_pulse_id, data_file, "/entry/xfel/pulseID", + number_of_images, fimages); + ReadVector(dataset->xfel_event_code, data_file, "/entry/xfel/eventCode", + number_of_images, fimages); + if (data_file.Exists("/entry/MX")) { if (data_file.Exists("/entry/MX/peakCountUnfiltered")) ReadVector(dataset->spot_count, @@ -1468,6 +1476,10 @@ void HDF5MetadataSource::FillPerImage(DataMessage &message, int64_t requested_im message.indexing_lattice_count = dataset->indexing_lattice_count[image_number]; if (dataset->bkg_estimate.size() > image_number) message.bkg_estimate = dataset->bkg_estimate[image_number]; + if (dataset->xfel_pulse_id.size() > image_number) + message.xfel_pulse_id = dataset->xfel_pulse_id[image_number]; + if (dataset->xfel_event_code.size() > image_number) + message.xfel_event_code = dataset->xfel_event_code[image_number]; // NaN is how the file stores a frame with no value; the optional must come back absent, not // carrying a NaN, because absence is the CANNOT-SAY trigger state and a value is not. if (dataset->spindle_blind_fraction.size() > image_number diff --git a/reader/JFJochMultiReader.cpp b/reader/JFJochMultiReader.cpp index 1f4a53945..ba91a4f6a 100644 --- a/reader/JFJochMultiReader.cpp +++ b/reader/JFJochMultiReader.cpp @@ -47,6 +47,10 @@ JFJochMultiReader::JFJochMultiReader(std::vector> auto dataset = std::make_shared(*first); std::vector 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(); @@ -74,10 +78,19 @@ JFJochMultiReader::JFJochMultiReader(std::vector> 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(); diff --git a/reader/JFJochReaderDataset.h b/reader/JFJochReaderDataset.h index b255e0ece..5c1cc154e 100644 --- a/reader/JFJochReaderDataset.h +++ b/reader/JFJochReaderDataset.h @@ -64,6 +64,9 @@ struct JFJochReaderDataset { std::vector indexing_result; std::vector indexing_lattice_count; std::vector bkg_estimate; + // The XFEL pulse ID and event code of each image (/entry/xfel), where the file records them. + std::vector xfel_pulse_id; + std::vector xfel_event_code; // NaN entries are frames with no value (CANNOT SAY), not zeros. std::vector spindle_blind_fraction; std::vector ice_ring_score; diff --git a/rugnux/RugnuxImagePass.cpp b/rugnux/RugnuxImagePass.cpp index af8a6d07f..d55c2ccee 100644 --- a/rugnux/RugnuxImagePass.cpp +++ b/rugnux/RugnuxImagePass.cpp @@ -220,6 +220,10 @@ void Rugnux::ImagePass(PipelineLocals &p) { msg.original_number = image_idx; if (dataset->efficiency.size() > image_idx) msg.image_collection_efficiency = dataset->efficiency[image_idx]; + if (dataset->xfel_pulse_id.size() > image_idx) + msg.xfel_pulse_id = dataset->xfel_pulse_id[image_idx]; + if (dataset->xfel_event_code.size() > image_idx) + msg.xfel_event_code = dataset->xfel_event_code[image_idx]; total_uncompressed_bytes += msg.image.GetUncompressedSize(); const auto t0 = std::chrono::steady_clock::now(); @@ -329,6 +333,10 @@ void Rugnux::ImagePass(PipelineLocals &p) { msg.original_number = image_idx; if (dataset->efficiency.size() > image_idx) msg.image_collection_efficiency = dataset->efficiency[image_idx]; + if (dataset->xfel_pulse_id.size() > image_idx) + msg.xfel_pulse_id = dataset->xfel_pulse_id[image_idx]; + if (dataset->xfel_event_code.size() > image_idx) + msg.xfel_event_code = dataset->xfel_event_code[image_idx]; total_uncompressed_bytes += msg.image.GetUncompressedSize(); const bool count_crowding = crowding_at && ordinal % crowding_stride == 0; diff --git a/tests/RugnuxTest.cpp b/tests/RugnuxTest.cpp index 2b812e40b..4fc9b27e8 100644 --- a/tests/RugnuxTest.cpp +++ b/tests/RugnuxTest.cpp @@ -23,7 +23,8 @@ namespace { // Write a small VDS dataset of `n` flat images and return nothing (prefix_master.h5 + // prefix_data_000001.h5 land in the test working directory). - void WriteTestDataset(const std::string &prefix, int n, bool rotation = false) { + // `pulsed`: an XFEL run, image i with pulse ID 1000 + 7 i and event code i % 4. + void WriteTestDataset(const std::string &prefix, int n, bool rotation = false, bool pulsed = false) { RegisterHDF5Filter(); DiffractionExperiment x(DetJF(1)); @@ -34,6 +35,9 @@ namespace { x.Compression(CompressionAlgorithm::NO_COMPRESSION); x.BeamX_pxl(512).BeamY_pxl(256).DetectorDistance_mm(150).IncidentEnergy_keV(WVL_1A_IN_KEV) .FrameTime(std::chrono::microseconds(500), std::chrono::microseconds(10)); + if (pulsed) + x.PulsedSource(true).NumTriggers(n).ImagesPerTrigger(1); + REQUIRE(x.GetImageNum() == n); std::vector image(x.GetPixelsNum(), 5); StartMessage start_message; @@ -44,6 +48,10 @@ namespace { DataMessage message{}; message.image = CompressedImage(image, x.GetXPixelsNum(), x.GetYPixelsNum()); message.number = i; + if (pulsed) { + message.xfel_pulse_id = 1000 + 7 * i; + message.xfel_event_code = i % 4; + } REQUIRE_NOTHROW(file_set.WriteHDF5(message)); generator.Add(message); } @@ -95,6 +103,57 @@ TEST_CASE("Rugnux_AzInt", "[HDF5][Full]") { REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); } +TEST_CASE("Rugnux_ProcessFileKeepsXfelIds", "[HDF5][Full]") { + // The pulse ID and event code of each processed image go into the _process.h5 at the image's place in + // it, the place its reflections are stored at: here images 1, 4 and 7 of ten (-s 1 -e 9, stride 3). + WriteTestDataset("process_xfel_in", 10, false, true); + { + // The master of a pulsed-source run carries them too, not only its data file. + HDF5ReadOnlyFile file("process_xfel_in_master.h5"); + CHECK(file.ReadOptVector("/entry/xfel/pulseID").size() == 10); + } + + JFJochHDF5Reader reader; + REQUIRE_NOTHROW(reader.ReadFile("process_xfel_in_master.h5")); + auto dataset = reader.GetDataset(); + REQUIRE(dataset); + REQUIRE(dataset->xfel_pulse_id.size() == 10); + CHECK(dataset->xfel_pulse_id[9] == 1063); + + ProcessConfig config; + config.mode = ProcessMode::AzimuthalIntegration; + config.nthreads = 2; + config.start_image = 1; + config.end_image = 9; + config.stride = 3; + ProcessFileSink sink("process_xfel_out"); + config.image_sink = &sink; + + Rugnux process(reader, dataset->experiment, *dataset->pixel_mask, config); + ProcessResult result; + REQUIRE_NOTHROW(result = process.Run()); + CHECK(result.images_processed == 3); + + { + HDF5ReadOnlyFile file("process_xfel_out_process.h5"); + CHECK(file.ReadOptVector("/entry/xfel/pulseID") == std::vector{1007, 1028, 1049}); + CHECK(file.ReadOptVector("/entry/xfel/eventCode") == std::vector{1, 0, 3}); + CHECK(file.ReadOptVector("/entry/detector/number") == std::vector{1, 4, 7}); + } + { + // ... and read back with the process file. + JFJochHDF5Reader out; + REQUIRE_NOTHROW(out.ReadFile("process_xfel_out_process.h5")); + CHECK(out.GetDataset()->xfel_pulse_id == std::vector{1007, 1028, 1049}); + } + + reader.Close(); + remove("process_xfel_in_master.h5"); + remove("process_xfel_in_data_000001.h5"); + remove("process_xfel_out_process.h5"); + REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); +} + TEST_CASE("Rugnux_ProcessFileWithoutImages", "[HDF5][Full]") { // A reader with no single HDF5 file to link to (here several inputs read as one) gets a _process.h5 // with the per-image analysis and no /entry/data/data, which reads back with its images' metadata