Skip to content

Commit

Permalink
create h5 1d int array writing method, save frame indexes, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlunin committed Nov 11, 2024
1 parent 0a21a5c commit d37fc21
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
37 changes: 25 additions & 12 deletions tango-moenchzmq/src/backend/CPUComputationBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions tango-moenchzmq/src/backend/FileWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
15 changes: 15 additions & 0 deletions tango-moenchzmq/src/backend/HDFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 2 additions & 0 deletions tango-moenchzmq/src/backend/HDFWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
30 changes: 30 additions & 0 deletions tango-moenchzmq/test/hdfwriter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d37fc21

Please sign in to comment.