Skip to content

Commit

Permalink
Add basic checks for roundtripping
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jan 16, 2025
1 parent 1f1f4b3 commit 73121eb
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/root_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(root_dependent_tests
read_and_write_frame_root.cpp
write_interface_root.cpp
read_interface_root.cpp
selected_colls_roundtrip_root.cpp
)
if(ENABLE_RNTUPLE)
set(root_dependent_tests
Expand All @@ -17,6 +18,7 @@ if(ENABLE_RNTUPLE)
read_python_frame_rntuple.cpp
write_interface_rntuple.cpp
read_interface_rntuple.cpp
selected_colls_roundtrip_rntuple.cpp
)
endif()
if(ENABLE_DATASOURCE)
Expand All @@ -39,13 +41,20 @@ set_tests_properties(
read_frame_root
read_frame_root_multiple
read_and_write_frame_root
selected_colls_roundtrip_root

PROPERTIES
DEPENDS write_frame_root
)

if(ENABLE_RNTUPLE)
set_property(TEST read_rntuple PROPERTY DEPENDS write_rntuple)
set_tests_properties(
read_rntuple
selected_colls_roundtrip_rntuple

PROPERTIES
DEPENDS write_rntuple
)
set_property(TEST read_interface_rntuple PROPERTY DEPENDS write_interface_rntuple)
endif()

Expand Down
9 changes: 9 additions & 0 deletions tests/root_io/selected_colls_roundtrip_rntuple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "selected_colls_roundtrip.h"

#include "podio/RNTupleReader.h"
#include "podio/RNTupleWriter.h"

int main() {
return do_roundtrip<podio::RNTupleReader, podio::RNTupleWriter>("example_rntuple.root",
"selected_example_rntuple.root");
}
8 changes: 8 additions & 0 deletions tests/root_io/selected_colls_roundtrip_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "selected_colls_roundtrip.h"

#include "podio/ROOTReader.h"
#include "podio/ROOTWriter.h"

int main() {
return do_roundtrip<podio::ROOTReader, podio::ROOTWriter>("example_frame.root", "selected_example_frame.root");
}
101 changes: 101 additions & 0 deletions tests/selected_colls_roundtrip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <podio/Frame.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

/// Roundtrip test for ensuring that files that have been written with Frames
/// that have been read from file selecting only a few collections to be read
/// are still properly usable when read again.
///
/// The flow is
///
/// - Open the original file (using one that has been produced by other I/O
/// tests)
/// - Read a Frame selecting only a few of the available collections
/// - Write this frame into the outputFile without doing any further checks
/// (that is done by other tests)
/// - Read back that file and check that it can still be used
/// - Reading full frames
/// - and frames with a selection of collections
///
/// do_roundrip ties everything together and is the only function that needs to
/// be called by backend specific tests

/// The collection names that will be written into the new file
const std::vector<std::string> collectionSelection = {"mcparticles", "links", "userInts", "hits"};

/// The collections to select in the second round
const std::vector<std::string> roundTripSelection = {"hits", "userInts"};

/// Write a new file containing only a few selected collections and only one
/// event
template <typename ReaderT, typename WriterT>
void writeSelectedFile(const std::string& originalFile, const std::string& outputFile) {
auto reader = ReaderT();
reader.openFile(originalFile);
const auto event = podio::Frame(reader.readEntry("events", 0, collectionSelection));

auto writer = WriterT(outputFile);
writer.writeFrame(event, "events");
}

bool compareUnordered(std::vector<std::string> lhs, std::vector<std::string> rhs) {
std::ranges::sort(lhs);
std::ranges::sort(rhs);
return std::ranges::equal(lhs, rhs);
}

std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& vec) {
os << "[";
std::string delim = "";
for (const auto& v : vec) {
os << std::exchange(delim, ", ") << v;
}
return os << "]";
}

/// Read the file that has been created and check all available collections
template <typename ReaderT>
int readSelectedFileFull(const std::string& filename) {
auto reader = ReaderT();
reader.openFile(filename);

const auto event = podio::Frame(reader.readEntry("events", 0));

if (!compareUnordered(event.getAvailableCollections(), collectionSelection)) {
std::cerr
<< "Collection names that are available from the selected collections file are not as expected (expected: "
<< collectionSelection << ", actual " << event.getAvailableCollections() << ")" << std::endl;
return 1;
}

return 0;
}

/// Read the file that has been created and check whether selecting on that file
/// again also works
template <typename ReaderT>
int readSelectedFilePartial(const std::string& filename) {
auto reader = ReaderT();
reader.openFile(filename);

const auto event = podio::Frame(reader.readEntry("events", 0, roundTripSelection));

if (!compareUnordered(event.getAvailableCollections(), roundTripSelection)) {
std::cerr
<< "Collection names that are available from the selected collections file are not as expected (expected: "
<< roundTripSelection << ", actual " << event.getAvailableCollections() << ")" << std::endl;
return 1;
}

return 0;
}

template <typename ReaderT, typename WriterT>
int do_roundtrip(const std::string& originalFile, const std::string& outputFile) {
writeSelectedFile<ReaderT, WriterT>(originalFile, outputFile);

return readSelectedFileFull<ReaderT>(outputFile) + readSelectedFilePartial<ReaderT>(outputFile);
}
2 changes: 2 additions & 0 deletions tests/sio_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(sio_dependent_tests
read_python_frame_sio.cpp
write_interface_sio.cpp
read_interface_sio.cpp
selected_colls_roundtrip_sio.cpp
)
set(sio_libs podio::podioSioIO podio::podioIO)
foreach( sourcefile ${sio_dependent_tests} )
Expand All @@ -14,6 +15,7 @@ endforeach()
set_tests_properties(
read_frame_sio
read_and_write_frame_sio
selected_colls_roundtrip_sio

PROPERTIES
DEPENDS
Expand Down
8 changes: 8 additions & 0 deletions tests/sio_io/selected_colls_roundtrip_sio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "selected_colls_roundtrip.h"

#include "podio/SIOReader.h"
#include "podio/SIOWriter.h"

int main() {
return do_roundtrip<podio::SIOReader, podio::SIOWriter>("example_frame.sio", "selected_example_frame.sio");
}

0 comments on commit 73121eb

Please sign in to comment.