diff --git a/include/podio/SIOFrameReader.h b/include/podio/SIOFrameReader.h index 9902373db..eafc383a9 100644 --- a/include/podio/SIOFrameReader.h +++ b/include/podio/SIOFrameReader.h @@ -1,90 +1,10 @@ #ifndef PODIO_SIOFRAMEREADER_H #define PODIO_SIOFRAMEREADER_H -#include "podio/SIOBlock.h" -#include "podio/SIOFrameData.h" -#include "podio/podioVersion.h" -#include "podio/utilities/DatamodelRegistryIOHelpers.h" - -#include - -#include -#include -#include -#include +#include "podio/SIOReader.h" namespace podio { - -class CollectionIDTable; - -class SIOFrameReader { - -public: - SIOFrameReader(); - ~SIOFrameReader() = default; - - // non copyable - SIOFrameReader(const SIOFrameReader&) = delete; - SIOFrameReader& operator=(const SIOFrameReader&) = delete; - - /** - * Read the next data entry from which a Frame can be constructed for the - * given name. In case there are no more entries left for this name or in - * case there is no data for this name, this returns a nullptr. - */ - std::unique_ptr readNextEntry(const std::string& name); - - /** - * Read the specified data entry from which a Frame can be constructed for - * the given name. In case the entry does not exist for this name or in - * case there is no data for this name, this returns a nullptr. - */ - std::unique_ptr readEntry(const std::string& name, const unsigned entry); - - /// Returns number of entries for the given name - unsigned getEntries(const std::string& name) const; - - void openFile(const std::string& filename); - - /// Get the build version of podio that has been used to write the current file - podio::version::Version currentFileVersion() const { - return m_fileVersion; - } - - /// Get the names of all the available Frame categories in the current file(s) - std::vector getAvailableCategories() const; - - /// Get the datamodel definition for the given name - const std::string_view getDatamodelDefinition(const std::string& name) const { - return m_datamodelHolder.getDatamodelDefinition(name); - } - - /// Get all names of the datamodels that ara available from this reader - std::vector getAvailableDatamodels() const { - return m_datamodelHolder.getAvailableDatamodels(); - } - -private: - void readPodioHeader(); - - /// read the TOC record - bool readFileTOCRecord(); - - void readEDMDefinitions(); - - sio::ifstream m_stream{}; ///< The stream from which we read - - /// Count how many times each an entry of this name has been read already - std::unordered_map m_nameCtr{}; - - /// Table of content record where starting points of named entries can be read from - SIOFileTOCRecord m_tocRecord{}; - /// The podio version that has been used to write the file - podio::version::Version m_fileVersion{0}; - - DatamodelDefinitionHolder m_datamodelHolder{}; -}; - -} // namespace podio +using SIOFrameReader [[deprecated("Will be removed in v1.0 switch podio::SIOReader")]] = podio::SIOReader; +} #endif // PODIO_SIOFRAMEREADER_H diff --git a/include/podio/SIOFrameWriter.h b/include/podio/SIOFrameWriter.h index c111261ca..85b4da8d2 100644 --- a/include/podio/SIOFrameWriter.h +++ b/include/podio/SIOFrameWriter.h @@ -1,44 +1,10 @@ #ifndef PODIO_SIOFRAMEWRITER_H #define PODIO_SIOFRAMEWRITER_H -#include "podio/SIOBlock.h" -#include "podio/utilities/DatamodelRegistryIOHelpers.h" - -#include - -#include -#include -#include +#include "podio/SIOWriter.h" namespace podio { - -class Frame; - -class SIOFrameWriter { -public: - SIOFrameWriter(const std::string& filename); - ~SIOFrameWriter(); - - SIOFrameWriter(const SIOFrameWriter&) = delete; - SIOFrameWriter& operator=(const SIOFrameWriter&) = delete; - - /** Write the given Frame with the given category - */ - void writeFrame(const podio::Frame& frame, const std::string& category); - - /** Write the given Frame with the given category only storing the collections - * that are desired via collsToWrite - */ - void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector& collsToWrite); - - void finish(); - -private: - sio::ofstream m_stream{}; ///< The output file stream - SIOFileTOCRecord m_tocRecord{}; ///< The "table of contents" of the written file - DatamodelDefinitionCollector m_datamodelCollector{}; - bool m_finished{false}; ///< Has finish been called already? -}; -} // namespace podio +using SIOFrameWriter [[deprecated("Will be removed in v1.0 switch podio::SIOWriter")]] = podio::SIOWriter; +} #endif // PODIO_SIOFRAMEWRITER_H diff --git a/include/podio/SIOReader.h b/include/podio/SIOReader.h new file mode 100644 index 000000000..5b16fbe79 --- /dev/null +++ b/include/podio/SIOReader.h @@ -0,0 +1,90 @@ +#ifndef PODIO_SIOREADER_H +#define PODIO_SIOREADER_H + +#include "podio/SIOBlock.h" +#include "podio/SIOFrameData.h" +#include "podio/podioVersion.h" +#include "podio/utilities/DatamodelRegistryIOHelpers.h" + +#include + +#include +#include +#include +#include + +namespace podio { + +class CollectionIDTable; + +class SIOReader { + +public: + SIOReader(); + ~SIOReader() = default; + + // non copyable + SIOReader(const SIOReader&) = delete; + SIOReader& operator=(const SIOReader&) = delete; + + /** + * Read the next data entry from which a Frame can be constructed for the + * given name. In case there are no more entries left for this name or in + * case there is no data for this name, this returns a nullptr. + */ + std::unique_ptr readNextEntry(const std::string& name); + + /** + * Read the specified data entry from which a Frame can be constructed for + * the given name. In case the entry does not exist for this name or in + * case there is no data for this name, this returns a nullptr. + */ + std::unique_ptr readEntry(const std::string& name, const unsigned entry); + + /// Returns number of entries for the given name + unsigned getEntries(const std::string& name) const; + + void openFile(const std::string& filename); + + /// Get the build version of podio that has been used to write the current file + podio::version::Version currentFileVersion() const { + return m_fileVersion; + } + + /// Get the names of all the available Frame categories in the current file(s) + std::vector getAvailableCategories() const; + + /// Get the datamodel definition for the given name + const std::string_view getDatamodelDefinition(const std::string& name) const { + return m_datamodelHolder.getDatamodelDefinition(name); + } + + /// Get all names of the datamodels that ara available from this reader + std::vector getAvailableDatamodels() const { + return m_datamodelHolder.getAvailableDatamodels(); + } + +private: + void readPodioHeader(); + + /// read the TOC record + bool readFileTOCRecord(); + + void readEDMDefinitions(); + + sio::ifstream m_stream{}; ///< The stream from which we read + + /// Count how many times each an entry of this name has been read already + std::unordered_map m_nameCtr{}; + + /// Table of content record where starting points of named entries can be read from + SIOFileTOCRecord m_tocRecord{}; + /// The podio version that has been used to write the file + podio::version::Version m_fileVersion{0}; + + DatamodelDefinitionHolder m_datamodelHolder{}; +}; + +} // namespace podio + +#endif // PODIO_SIOREADER_H diff --git a/include/podio/SIOWriter.h b/include/podio/SIOWriter.h new file mode 100644 index 000000000..2cd02118b --- /dev/null +++ b/include/podio/SIOWriter.h @@ -0,0 +1,44 @@ +#ifndef PODIO_SIOWRITER_H +#define PODIO_SIOWRITER_H + +#include "podio/SIOBlock.h" +#include "podio/utilities/DatamodelRegistryIOHelpers.h" + +#include + +#include +#include +#include + +namespace podio { + +class Frame; + +class SIOWriter { +public: + SIOWriter(const std::string& filename); + ~SIOWriter(); + + SIOWriter(const SIOWriter&) = delete; + SIOWriter& operator=(const SIOWriter&) = delete; + + /** Write the given Frame with the given category + */ + void writeFrame(const podio::Frame& frame, const std::string& category); + + /** Write the given Frame with the given category only storing the collections + * that are desired via collsToWrite + */ + void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector& collsToWrite); + + void finish(); + +private: + sio::ofstream m_stream{}; ///< The output file stream + SIOFileTOCRecord m_tocRecord{}; ///< The "table of contents" of the written file + DatamodelDefinitionCollector m_datamodelCollector{}; + bool m_finished{false}; ///< Has finish been called already? +}; +} // namespace podio + +#endif // PODIO_SIOWRITER_H diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index f876b0475..51c303f2a 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -22,7 +22,7 @@ def __init__(self, filename): Args: filename (str): File to open and read data from """ - self._reader = podio.SIOFrameReader() + self._reader = podio.SIOReader() self._reader.openFile(filename) super().__init__() @@ -57,5 +57,5 @@ def __init__(self, filename): Args: filename (str): The name of the output file """ - self._writer = podio.SIOFrameWriter(filename) + self._writer = podio.SIOWriter(filename) super().__init__() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4f2416cc..c7ac8f748 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -117,17 +117,17 @@ if(ENABLE_SIO) SET(sio_sources SIOBlockUserData.cc SIOBlock.cc - SIOFrameWriter.cc - SIOFrameReader.cc + SIOWriter.cc + SIOReader.cc SIOFrameData.cc sioUtils.h SIOLegacyReader.cc ) SET(sio_headers - ${PROJECT_SOURCE_DIR}/include/podio/SIOFrameReader.h + ${PROJECT_SOURCE_DIR}/include/podio/SIOReader.h ${PROJECT_SOURCE_DIR}/include/podio/SIOLegacyReader.h - ${PROJECT_SOURCE_DIR}/include/podio/SIOFrameWriter.h + ${PROJECT_SOURCE_DIR}/include/podio/SIOWriter.h ) PODIO_ADD_LIB_AND_DICT(podioSioIO "${sio_headers}" "${sio_sources}" sio_selection.xml) diff --git a/src/SIOFrameReader.cc b/src/SIOReader.cc similarity index 86% rename from src/SIOFrameReader.cc rename to src/SIOReader.cc index 60427adf5..de3d1e0e7 100644 --- a/src/SIOFrameReader.cc +++ b/src/SIOReader.cc @@ -1,4 +1,4 @@ -#include "podio/SIOFrameReader.h" +#include "podio/SIOReader.h" #include "podio/SIOBlock.h" #include "sioUtils.h" @@ -11,11 +11,11 @@ namespace podio { -SIOFrameReader::SIOFrameReader() { +SIOReader::SIOReader() { auto& libLoader [[maybe_unused]] = SIOBlockLibraryLoader::instance(); } -void SIOFrameReader::openFile(const std::string& filename) { +void SIOReader::openFile(const std::string& filename) { m_stream.open(filename, std::ios::binary); if (!m_stream.is_open()) { throw std::runtime_error("File " + filename + " couldn't be opened"); @@ -27,7 +27,7 @@ void SIOFrameReader::openFile(const std::string& filename) { readEDMDefinitions(); // Potentially could do this lazily } -std::unique_ptr SIOFrameReader::readNextEntry(const std::string& name) { +std::unique_ptr SIOReader::readNextEntry(const std::string& name) { // Skip to where the next record of this name starts in the file, based on // how many times we have already read this name // @@ -48,14 +48,14 @@ std::unique_ptr SIOFrameReader::readNextEntry(const std::string& n tableInfo._uncompressed_length); } -std::unique_ptr SIOFrameReader::readEntry(const std::string& name, const unsigned entry) { +std::unique_ptr SIOReader::readEntry(const std::string& name, const unsigned entry) { // NOTE: Will create or overwrite the entry counter // All checks are done in the following function m_nameCtr[name] = entry; return readNextEntry(name); } -std::vector SIOFrameReader::getAvailableCategories() const { +std::vector SIOReader::getAvailableCategories() const { // Filter the available records from the TOC to remove records that are // stored, but use reserved record names for podio meta data auto recordNames = m_tocRecord.getRecordNames(); @@ -65,11 +65,11 @@ std::vector SIOFrameReader::getAvailableCategories() const { return recordNames; } -unsigned SIOFrameReader::getEntries(const std::string& name) const { +unsigned SIOReader::getEntries(const std::string& name) const { return m_tocRecord.getNRecords(name); } -bool SIOFrameReader::readFileTOCRecord() { +bool SIOReader::readFileTOCRecord() { // Check if there is a dedicated marker at the end of the file that tells us // where the TOC actually starts m_stream.seekg(-sio_helpers::SIOTocInfoSize, std::ios_base::end); @@ -99,7 +99,7 @@ bool SIOFrameReader::readFileTOCRecord() { return false; } -void SIOFrameReader::readPodioHeader() { +void SIOReader::readPodioHeader() { const auto& [buffer, _] = sio_utils::readRecord(m_stream, false, sizeof(podio::version::Version)); sio::block_list blocks; @@ -109,7 +109,7 @@ void SIOFrameReader::readPodioHeader() { m_fileVersion = static_cast(blocks[0].get())->version; } -void SIOFrameReader::readEDMDefinitions() { +void SIOReader::readEDMDefinitions() { const auto recordPos = m_tocRecord.getPosition(sio_helpers::SIOEDMDefinitionName); if (recordPos == 0) { // No EDM definitions found diff --git a/src/SIOFrameWriter.cc b/src/SIOWriter.cc similarity index 86% rename from src/SIOFrameWriter.cc rename to src/SIOWriter.cc index dcc1e4c22..008e94a97 100644 --- a/src/SIOFrameWriter.cc +++ b/src/SIOWriter.cc @@ -1,9 +1,9 @@ -#include "podio/SIOFrameWriter.h" #include "podio/CollectionBase.h" #include "podio/CollectionIDTable.h" #include "podio/Frame.h" #include "podio/GenericParameters.h" #include "podio/SIOBlock.h" +#include "podio/SIOWriter.h" #include "sioUtils.h" @@ -12,7 +12,7 @@ namespace podio { -SIOFrameWriter::SIOFrameWriter(const std::string& filename) { +SIOWriter::SIOWriter(const std::string& filename) { m_stream.open(filename, std::ios::binary); if (!m_stream.is_open()) { SIO_THROW(sio::error_code::not_open, "Couldn't open output stream '" + filename + "'"); @@ -26,18 +26,18 @@ SIOFrameWriter::SIOFrameWriter(const std::string& filename) { sio_utils::writeRecord(blocks, "podio_header_info", m_stream, sizeof(podio::version::Version), false); } -SIOFrameWriter::~SIOFrameWriter() { +SIOWriter::~SIOWriter() { if (!m_finished) { finish(); } } -void SIOFrameWriter::writeFrame(const podio::Frame& frame, const std::string& category) { +void SIOWriter::writeFrame(const podio::Frame& frame, const std::string& category) { writeFrame(frame, category, frame.getAvailableCollections()); } -void SIOFrameWriter::writeFrame(const podio::Frame& frame, const std::string& category, - const std::vector& collsToWrite) { +void SIOWriter::writeFrame(const podio::Frame& frame, const std::string& category, + const std::vector& collsToWrite) { std::vector collections; collections.reserve(collsToWrite.size()); for (const auto& name : collsToWrite) { @@ -56,7 +56,7 @@ void SIOFrameWriter::writeFrame(const podio::Frame& frame, const std::string& ca sio_utils::writeRecord(blocks, category, m_stream); } -void SIOFrameWriter::finish() { +void SIOWriter::finish() { auto edmDefMap = std::make_shared>( m_datamodelCollector.getDatamodelDefinitionsToWrite()); diff --git a/src/sio_selection.xml b/src/sio_selection.xml index b0101fd3e..a7b88c789 100644 --- a/src/sio_selection.xml +++ b/src/sio_selection.xml @@ -1,7 +1,7 @@ - + - +