Skip to content

Commit

Permalink
Add a reader and writer interface, rename the RNTuple reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Dec 3, 2023
1 parent 26d51c9 commit 0e7e02a
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 83 deletions.
8 changes: 4 additions & 4 deletions include/podio/GenericParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ using version_type = uint32_t; // from sio/definitions
} // namespace sio

namespace podio {
class ROOTNTupleReader;
class ROOTNTupleWriter;
class ROOTRNTupleReader;
class ROOTRNTupleWriter;
} // namespace podio

#define DEPR_NON_TEMPLATE \
Expand Down Expand Up @@ -150,8 +150,8 @@ class GenericParameters {

friend void writeGenericParameters(sio::write_device& device, const GenericParameters& parameters);
friend void readGenericParameters(sio::read_device& device, GenericParameters& parameters, sio::version_type version);
friend ROOTNTupleReader;
friend ROOTNTupleWriter;
friend ROOTRNTupleReader;
friend ROOTRNTupleWriter;

/// Get a reference to the internal map for a given type
template <typename T>
Expand Down
85 changes: 85 additions & 0 deletions include/podio/IROOTFrameReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef PODIO_FRAMEREADER_H
#define PODIO_FRAMEREADER_H

#include "podio/ROOTFrameData.h"
#include "podio/podioVersion.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <iostream>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace podio {

/**
* This class has the function to read available data from disk
* and to prepare collections and buffers.
**/
class IROOTFrameReader {

public:
IROOTFrameReader() = default;
virtual ~IROOTFrameReader() = default;

IROOTFrameReader(const IROOTFrameReader&) = delete;
IROOTFrameReader& operator=(const IROOTFrameReader&) = delete;

/**
* Open a single file for reading.
*
* @param filename The name of the input file
*/
virtual void openFile(const std::string& filename) = 0;

/**
* Open multiple files for reading and then treat them as if they are one file
*
* NOTE: All of the files are assumed to have the same structure. Specifically
* this means:
* - The same categories are available from all files
* - The collections that are contained in the individual categories are the
* same across all files
*
* This usually boils down to "the files have been written with the same
* settings", e.g. they are outputs of a batched process.
*
* @param filenames The filenames of all input files that should be read
*/
virtual void openFiles(const std::vector<std::string>& filenames) = 0;

/**
* 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.
*/
virtual std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string& name) = 0;

/**
* 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.
*/
virtual std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry) = 0;

/// Returns number of entries for the given name
virtual unsigned getEntries(const std::string& name) = 0;

/// Get the build version of podio that has been used to write the current file
virtual podio::version::Version currentFileVersion() const = 0;

/// Get the names of all the availalable Frame categories in the current file(s)
virtual std::vector<std::string_view> getAvailableCategories() const = 0;

/// Get the datamodel definition for the given name
virtual const std::string_view getDatamodelDefinition(const std::string& name) const = 0;

/// Get all names of the datamodels that ara available from this reader
virtual std::vector<std::string> getAvailableDatamodels() const = 0;
};

} // namespace podio

#endif // PODIO_FRAMEREADER_H
66 changes: 66 additions & 0 deletions include/podio/IROOTFrameWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef PODIO_IROOTFRAMEWRITER_H
#define PODIO_IROOTFRAMEWRITER_H

#include "podio/CollectionBranches.h"
#include "podio/CollectionIDTable.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <memory>
#include <string>
#include <tuple>
#include <vector>

namespace podio {
class Frame;
class CollectionBase;
class GenericParameters;

class IROOTFrameWriter {
public:
IROOTFrameWriter() = default;
virtual ~IROOTFrameWriter() = default;

IROOTFrameWriter(const IROOTFrameWriter&) = delete;
IROOTFrameWriter& operator=(const IROOTFrameWriter&) = delete;

/** Store the given frame with the given category. Store all available
* collections from the Frame.
*
* NOTE: The contents of the first Frame that is written in this way
* determines the contents that will be written for all subsequent Frames.
*/
virtual void writeFrame(const podio::Frame& frame, const std::string& category) = 0;

/** Store the given Frame with the given category. Store only the
* collections that are passed.
*
* NOTE: The contents of the first Frame that is written in this way
* determines the contents that will be written for all subsequent Frames.
*/
virtual void writeFrame(const podio::Frame& frame, const std::string& category,
const std::vector<std::string>& collsToWrite) = 0;

/** Write the current file, including all the necessary metadata to read it again.
*/
virtual void finish() = 0;

/** Check whether the collsToWrite are consistent with the state of the passed
* category.
*
* Return two vectors of collection names. The first one contains all the
* names that were missing from the collsToWrite but were present in the
* category. The second one contains the names that are present in the
* collsToWrite only. If both vectors are empty the category and the passed
* collsToWrite are consistent.
*
* NOTE: This will only be a meaningful check if the first Frame of the passed
* category has already been written. Also, this check is rather expensive as
* it has to effectively do two set differences.
*/
std::tuple<std::vector<std::string>, std::vector<std::string>>
checkConsistency(const std::vector<std::string>& collsToWrite, const std::string& category) const;
};

} // namespace podio

#endif // PODIO_IROOTFRAMEWRITER_H
21 changes: 11 additions & 10 deletions include/podio/ROOTFrameReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PODIO_ROOTFRAMEREADER_H

#include "podio/CollectionBranches.h"
#include "podio/IROOTFrameReader.h"
#include "podio/ROOTFrameData.h"
#include "podio/podioVersion.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"
Expand Down Expand Up @@ -42,7 +43,7 @@ struct CollectionReadBuffers;
* This class has the function to read available data from disk
* and to prepare collections and buffers.
**/
class ROOTFrameReader {
class ROOTFrameReader : public IROOTFrameReader {

public:
ROOTFrameReader() = default;
Expand All @@ -57,7 +58,7 @@ class ROOTFrameReader {
*
* @param filename The name of the input file
*/
void openFile(const std::string& filename);
void openFile(const std::string& filename) override;

/**
* Open multiple files for reading and then treat them as if they are one file
Expand All @@ -73,40 +74,40 @@ class ROOTFrameReader {
*
* @param filenames The filenames of all input files that should be read
*/
void openFiles(const std::vector<std::string>& filenames);
void openFiles(const std::vector<std::string>& filenames) override;

/**
* 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<podio::ROOTFrameData> readNextEntry(const std::string& name);
std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string& name) override;

/**
* 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<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry);
std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry) override;

/// Returns number of entries for the given name
unsigned getEntries(const std::string& name) const;
unsigned getEntries(const std::string& name) override;

/// Get the build version of podio that has been used to write the current file
podio::version::Version currentFileVersion() const {
podio::version::Version currentFileVersion() const override {
return m_fileVersion;
}

/// Get the names of all the availalable Frame categories in the current file(s)
std::vector<std::string_view> getAvailableCategories() const;
std::vector<std::string_view> getAvailableCategories() const override;

/// Get the datamodel definition for the given name
const std::string_view getDatamodelDefinition(const std::string& name) const {
const std::string_view getDatamodelDefinition(const std::string& name) const override {
return m_datamodelHolder.getDatamodelDefinition(name);
}

/// Get all names of the datamodels that ara available from this reader
std::vector<std::string> getAvailableDatamodels() const {
std::vector<std::string> getAvailableDatamodels() const override {
return m_datamodelHolder.getAvailableDatamodels();
}

Expand Down
10 changes: 6 additions & 4 deletions include/podio/ROOTFrameWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "podio/CollectionBranches.h"
#include "podio/CollectionIDTable.h"
#include "podio/IROOTFrameWriter.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include "TFile.h"
Expand All @@ -21,7 +22,7 @@ class Frame;
class CollectionBase;
class GenericParameters;

class ROOTFrameWriter {
class ROOTFrameWriter : public IROOTFrameWriter {
public:
ROOTFrameWriter(const std::string& filename);
~ROOTFrameWriter();
Expand All @@ -35,19 +36,20 @@ class ROOTFrameWriter {
* NOTE: The contents of the first Frame that is written in this way
* determines the contents that will be written for all subsequent Frames.
*/
void writeFrame(const podio::Frame& frame, const std::string& category);
void writeFrame(const podio::Frame& frame, const std::string& category) override;

/** Store the given Frame with the given category. Store only the
* collections that are passed.
*
* NOTE: The contents of the first Frame that is written in this way
* determines the contents that will be written for all subsequent Frames.
*/
void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collsToWrite);
void writeFrame(const podio::Frame& frame, const std::string& category,
const std::vector<std::string>& collsToWrite) override;

/** Write the current file, including all the necessary metadata to read it again.
*/
void finish();
void finish() override;

/** Check whether the collsToWrite are consistent with the state of the passed
* category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "podio/CollectionBranches.h"
#include "podio/ICollectionProvider.h"
#include "podio/IROOTFrameReader.h"
#include "podio/ROOTFrameData.h"
#include "podio/SchemaEvolution.h"
#include "podio/podioVersion.h"
Expand All @@ -22,55 +23,53 @@ namespace podio {
This class has the function to read available data from disk
and to prepare collections and buffers.
**/
class ROOTNTupleReader {
class ROOTRNTupleReader : public IROOTFrameReader {

public:
ROOTNTupleReader() = default;
~ROOTNTupleReader() = default;
ROOTRNTupleReader() = default;
~ROOTRNTupleReader() = default;

ROOTNTupleReader(const ROOTNTupleReader&) = delete;
ROOTNTupleReader& operator=(const ROOTNTupleReader&) = delete;
ROOTRNTupleReader(const ROOTRNTupleReader&) = delete;
ROOTRNTupleReader& operator=(const ROOTRNTupleReader&) = delete;

void openFile(const std::string& filename);
void openFiles(const std::vector<std::string>& filename);
void openFile(const std::string& filename) override;
void openFiles(const std::vector<std::string>& filename) override;

/**
* 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<podio::ROOTFrameData> readNextEntry(const std::string& name);
std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string& name) override;

/**
* 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<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry);

/// Get the names of all the available Frame categories in the current file(s)
std::vector<std::string_view> getAvailableCategories() const;
std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry) override;

/// Returns number of entries for the given name
unsigned getEntries(const std::string& name);
unsigned getEntries(const std::string& name) override;

/// Get the build version of podio that has been used to write the current file
podio::version::Version currentFileVersion() const {
podio::version::Version currentFileVersion() const override {
return m_fileVersion;
}

/// Get the names of all the availalable Frame categories in the current file(s)
std::vector<std::string_view> getAvailableCategories() const override;

/// Get the datamodel definition for the given name
const std::string_view getDatamodelDefinition(const std::string& name) const {
const std::string_view getDatamodelDefinition(const std::string& name) const override {
return m_datamodelHolder.getDatamodelDefinition(name);
}

/// Get all names of the datamodels that ara available from this reader
std::vector<std::string> getAvailableDatamodels() const {
std::vector<std::string> getAvailableDatamodels() const override {
return m_datamodelHolder.getAvailableDatamodels();
}

void closeFile();

private:
/**
* Initialize the given category by filling the maps with metadata information
Expand Down
Loading

0 comments on commit 0e7e02a

Please sign in to comment.