diff --git a/include/podio/CollectionIDTable.h b/include/podio/CollectionIDTable.h index 39b947c2e..0c2dd1ec9 100644 --- a/include/podio/CollectionIDTable.h +++ b/include/podio/CollectionIDTable.h @@ -34,6 +34,9 @@ class CollectionIDTable { /// Check if collection name is known bool present(const std::string& name) const; + /// Check if collection ID is known + bool present(uint32_t collectionID) const; + /// return registered names const std::vector& names() const { return m_names; diff --git a/include/podio/Frame.h b/include/podio/Frame.h index 7b6f39c7a..6bf6542e7 100644 --- a/include/podio/Frame.h +++ b/include/podio/Frame.h @@ -400,6 +400,9 @@ podio::CollectionBase* Frame::FrameModel::doGet(const std::string& n template bool Frame::FrameModel::get(uint32_t collectionID, CollectionBase*& collection) const { + if (!m_idTable.present(collectionID)) { + return false; + } const auto& name = m_idTable.name(collectionID); const auto& [_, inserted] = m_retrievedIDs.insert(collectionID); diff --git a/src/CollectionIDTable.cc b/src/CollectionIDTable.cc index fa97a4bbb..6c69ae417 100644 --- a/src/CollectionIDTable.cc +++ b/src/CollectionIDTable.cc @@ -1,10 +1,11 @@ // podio specific includes #include "podio/CollectionIDTable.h" -#include -#include #include "MurmurHash3.h" +#include +#include + namespace podio { CollectionIDTable::CollectionIDTable() : m_mutex(std::make_unique()) { @@ -46,6 +47,11 @@ bool CollectionIDTable::present(const std::string& name) const { return result != end(m_names); } +bool CollectionIDTable::present(uint32_t collectionID) const { + std::lock_guard lock{*m_mutex}; + return std::find(m_collectionIDs.begin(), m_collectionIDs.end(), collectionID) != m_collectionIDs.end(); +} + uint32_t CollectionIDTable::add(const std::string& name) { std::lock_guard lock(*m_mutex); const auto result = std::find(begin(m_names), end(m_names), name);