Skip to content

Commit

Permalink
Make SIOFrameData work properly with dropped collections (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener authored Apr 12, 2023
1 parent d71e4c0 commit f4c9219
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/SIOFrameData.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ std::optional<podio::CollectionReadBuffers> SIOFrameData::getCollectionBuffers(c
// collection indices start at 1!
const auto index = std::distance(std::begin(names), nameIt) + 1;

m_availableBlocks[index] = 1;
// Mark this block as consumed
m_availableBlocks[index] = 0;
return {dynamic_cast<podio::SIOBlock*>(m_blocks[index].get())->getBuffers()};
}

Expand All @@ -35,9 +36,13 @@ std::unique_ptr<podio::GenericParameters> SIOFrameData::getParameters() {
std::vector<std::string> SIOFrameData::getAvailableCollections() {
unpackBuffers();
std::vector<std::string> collections;
for (size_t i = 0; i < m_blocks.size(); ++i) {
for (size_t i = 1; i < m_blocks.size(); ++i) {
if (m_availableBlocks[i]) {
collections.push_back(m_idTable.name(i));
// We have to get the collID of this collection in the idTable as there is
// no guarantee that it coincides with the index in the blocks.
// Additionally, collection indices start at 1
const auto collID = m_idTable.ids()[i - 1];
collections.push_back(m_idTable.name(collID));
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/read_frame.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "podio/ROOTFrameReader.h"

#include "read_frame.h"
#include "read_frame_auxiliary.h"

#include "podio/ROOTFrameReader.h"

int main() {
return read_frames<podio::ROOTFrameReader>("example_frame.root");
return read_frames<podio::ROOTFrameReader>("example_frame.root") +
test_frame_aux_info<podio::ROOTFrameReader>("example_frame.root");
}
65 changes: 65 additions & 0 deletions tests/read_frame_auxiliary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef PODIO_TESTS_READ_FRAME_AUXILIARY_H // NOLINT(llvm-header-guard): folder structure not suitable
#define PODIO_TESTS_READ_FRAME_AUXILIARY_H // NOLINT(llvm-header-guard): folder structure not suitable

#include "write_frame.h"

#include "podio/Frame.h"

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

bool present(const std::string& elem, const std::vector<std::string>& vec) {
return std::find(vec.begin(), vec.end(), elem) != vec.end();
}

int testGetAvailableCollections(const podio::Frame& frame, const std::vector<std::string>& expected) {
const auto& collNames = frame.getAvailableCollections();
int result = 0;
for (const auto& name : expected) {
if (!present(name, collNames)) {
std::cerr << "Cannot find expected collection " << name << " in collections of Frame" << std::endl;
result = 1;
}
}

// Get a few collections and make sure that the resutls are unchanged (apart
// from ordering)
frame.get("hitRefs");
frame.get("mcparticles");

const auto& newCollNames = frame.getAvailableCollections();
for (const auto& name : newCollNames) {
if (!present(name, collNames)) {
std::cerr << "getAvailableCollections returns different collections after getting collections" << std::endl;
return 1;
}
}

return result;
}

/**
* Test function for testing some auxiliary functionality of the Frame.
* Encapsulates everything, such that a corresponding main function boils down
* to including the reader to test and defining a main that invokes and returns
* this function.
*
* @param fileName the name of the file to read from
* @tparam ReaderT a Frame based I/O capable reader
* @return 0 if all checks pass, non-zero otherwise
* */
template <typename ReaderT>
int test_frame_aux_info(const std::string& fileName) {
auto reader = ReaderT{};
reader.openFile(fileName);

// Test on the first event only here. Additionally, also only testing the
// "events" category, since that is the one where not all collections are
// written
auto event = podio::Frame(reader.readEntry("events", 0));

return testGetAvailableCollections(event, collsToWrite);
}

#endif // PODIO_TESTS_READ_FRAME_AUXILIARY_H
8 changes: 5 additions & 3 deletions tests/read_frame_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "podio/SIOFrameReader.h"

#include "read_frame.h"
#include "read_frame_auxiliary.h"

#include "podio/SIOFrameReader.h"

int main() {
return read_frames<podio::SIOFrameReader>("example_frame.sio");
return read_frames<podio::SIOFrameReader>("example_frame.sio") +
test_frame_aux_info<podio::SIOFrameReader>("example_frame.sio");
}

0 comments on commit f4c9219

Please sign in to comment.