From d37fc21e320a8b3488118c55a0443bc2a29878af Mon Sep 17 00:00:00 2001 From: lrlunin Date: Mon, 11 Nov 2024 19:20:42 +0100 Subject: [PATCH] create h5 1d int array writing method, save frame indexes, test coverage --- .../src/backend/CPUComputationBackend.cpp | 37 +++++++++++++------ tango-moenchzmq/src/backend/FileWriter.hpp | 4 ++ tango-moenchzmq/src/backend/HDFWriter.cpp | 15 ++++++++ tango-moenchzmq/src/backend/HDFWriter.hpp | 2 + tango-moenchzmq/test/hdfwriter_test.cpp | 30 +++++++++++++++ 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/tango-moenchzmq/src/backend/CPUComputationBackend.cpp b/tango-moenchzmq/src/backend/CPUComputationBackend.cpp index 8f9bdf4..5d93715 100644 --- a/tango-moenchzmq/src/backend/CPUComputationBackend.cpp +++ b/tango-moenchzmq/src/backend/CPUComputationBackend.cpp @@ -54,9 +54,10 @@ void CPUComputationBackend::allocateIndividualStorage() { */ deleteIndividualStorage(); frameindex_storage_ptr = new int[individual_frame_buffer_capacity]; - // fill with zeros, because we will take the max value later + // fill with -1 to take the max value later + // those frames which are not arrived will be -1 std::fill(frameindex_storage_ptr, - frameindex_storage_ptr + individual_frame_buffer_capacity, 0); + frameindex_storage_ptr + individual_frame_buffer_capacity, -1); individual_analog_storage_ptr = new float[individual_frame_buffer_capacity * consts::LENGTH]; // need to consider the case if after the acquistion the @@ -110,24 +111,36 @@ void CPUComputationBackend::resetPedestalAndRMS() { void CPUComputationBackend::dumpAccumulators() { fileWriter->openFile(); + // for the case if none frame was processed there will be empty sum frames fileWriter->writeFrame("images_sum", "analog_sum", analog_sum); fileWriter->writeFrame("images_sum", "counting_sum", counting_sum); if (saveIndividualFrames) { - // no need to check if max_frame_index < individual_frame_buffer_capacity - // since it was check in the processFrame() + /*** + * no need to check if max_frame_index < individual_frame_buffer_capacity + * since it was check in the processFrame() + * + * if there was none frame -> the max_frame_index will be = -1 + */ int max_frame_index = *std::max_element( frameindex_storage_ptr, frameindex_storage_ptr + individual_frame_buffer_capacity); - // for the frameindex of (0, ..., max_frame_index) there are - // max_frame_index+1 frames + /*** + * for the frameindex of (0, ..., max_frame_index) there are + * max_frame_index+1 frames. if the max_frame_index is -1 (no frames at + * all) then the max_stack_length will be 0 + */ int max_stack_length = max_frame_index + 1; - fileWriter->writeFrameStack("individual_frames", "analog", - individual_analog_storage_ptr, - max_stack_length); - if (saveRawFrames) { - fileWriter->writeFrameStack("individual_frames", "raw", - individual_raw_storage_ptr, + if (max_stack_length) { + fileWriter->write1DArray("individual_frames", "frameindex", + frameindex_storage_ptr, max_stack_length); + fileWriter->writeFrameStack("individual_frames", "analog", + individual_analog_storage_ptr, max_stack_length); + if (saveRawFrames) { + fileWriter->writeFrameStack("individual_frames", "raw", + individual_raw_storage_ptr, + max_stack_length); + } } #ifdef SINGLE_FRAMES_DEBUG fileWriter->writeFrameStack("individual_frames", "pedestal", diff --git a/tango-moenchzmq/src/backend/FileWriter.hpp b/tango-moenchzmq/src/backend/FileWriter.hpp index 675bb0c..dacec9f 100644 --- a/tango-moenchzmq/src/backend/FileWriter.hpp +++ b/tango-moenchzmq/src/backend/FileWriter.hpp @@ -90,4 +90,8 @@ class FileWriter { unsigned short *frame_stack_ptr, size_t frame_stack_length) = 0; + virtual void write1DArray(const std::string group_name, + const std::string array_name, int *array, + size_t array_length) + = 0; }; diff --git a/tango-moenchzmq/src/backend/HDFWriter.cpp b/tango-moenchzmq/src/backend/HDFWriter.cpp index 743e54a..e7502d9 100644 --- a/tango-moenchzmq/src/backend/HDFWriter.cpp +++ b/tango-moenchzmq/src/backend/HDFWriter.cpp @@ -142,3 +142,18 @@ void HDFWriter::writeFrameStack(const std::string group_name, image_stack_dataspace); dataset.write(frame_stack_ptr, image_datatype, image_stack_dataspace); }; + +void HDFWriter::write1DArray(const std::string group_name, + const std::string array_name, int *array_ptr, + size_t array_length) { + const H5::DataType array_datatype(H5::PredType::NATIVE_INT); + const hsize_t array_dimension[1] = { array_length }; + const H5::DataSpace array_dataspace(1, array_dimension); + + if (!current_file.exists(group_name)) + current_file.createGroup(group_name); + std::string array_path = fmt::format("{}/{}", group_name, array_name); + H5::DataSet dataset = current_file.createDataSet(array_path, array_datatype, + array_dataspace); + dataset.write(array_ptr, array_datatype, array_dataspace); +}; diff --git a/tango-moenchzmq/src/backend/HDFWriter.hpp b/tango-moenchzmq/src/backend/HDFWriter.hpp index cc8c813..ab2e0bb 100644 --- a/tango-moenchzmq/src/backend/HDFWriter.hpp +++ b/tango-moenchzmq/src/backend/HDFWriter.hpp @@ -61,4 +61,6 @@ class HDFWriter : public FileWriter { const std::string frame_stack_name, unsigned short *frame_stack_ptr, size_t frame_stack_length) override; + void write1DArray(const std::string group_name, const std::string array_name, + int *array_ptr, size_t array_length) override; }; diff --git a/tango-moenchzmq/test/hdfwriter_test.cpp b/tango-moenchzmq/test/hdfwriter_test.cpp index 90099ed..67ec8dc 100644 --- a/tango-moenchzmq/test/hdfwriter_test.cpp +++ b/tango-moenchzmq/test/hdfwriter_test.cpp @@ -198,3 +198,33 @@ TEST_F(HDFWriterTest, FileWriteUnsignedShortFrameStack) { } delete[] data; } + +TEST_F(HDFWriterTest, FileWrite1DIntArray) { + const size_t array_length = 256; + int *array = new int[array_length]; + for (size_t x = 0; x < array_length; x++) { + array[x] = std::rand(); + } + + // write frame to file + file_writer_ptr->openFile(); + file_writer_ptr->write1DArray("group", "int_array", array, array_length); + file_writer_ptr->closeFile(); + + // read frame from file and compare values + H5::H5File file("/tmp/" + folder_name + "/" + + fmt::format("{:%Y%m%d}_run_{:06d}.h5", now, + file_writer_ptr->file_index), + H5F_ACC_RDONLY); + H5::DataSet dataset = file.openDataSet("group/int_array"); + H5::DataSpace dataspace = dataset.getSpace(); + hsize_t dims_out[1]; + dataspace.getSimpleExtentDims(dims_out, NULL); + EXPECT_EQ(dims_out[0], array_length); + int *data = new int[array_length]; + dataset.read(data, H5::PredType::NATIVE_INT); + for (size_t x = 0; x < array_length; x++) { + EXPECT_EQ(data[x], array[x]); + } + delete[] data; +}