Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:10

0001 #ifndef PODIO_WRITER_H
0002 #define PODIO_WRITER_H
0003 
0004 #include "podio/Frame.h"
0005 
0006 namespace podio {
0007 
0008 /// Generic (type erased) writer class that can handle different I/O backends
0009 /// (almost) transparently
0010 ///
0011 /// Offers some more high level functionality compared to the lower level
0012 /// backend specific writers that this class wraps. In addition, it provides
0013 /// convenience methods to deal specifically with the "events" frame category.
0014 ///
0015 /// @note Since this simply wraps lower level writers, some of the limitations
0016 /// of the wrapped writers will still apply, e.g. if used for writing ROOT files
0017 /// frames of a given category will have to have the same contents.
0018 ///
0019 /// @note The recommended way to construct is to use the makeWriter() function
0020 /// since that handles the instantiation of the correct low level writers
0021 class Writer {
0022 private:
0023   struct WriterConcept {
0024     virtual ~WriterConcept() = default;
0025 
0026     virtual void writeFrame(const podio::Frame& frame, const std::string& category,
0027                             const std::vector<std::string>& collections) = 0;
0028     virtual void finish() = 0;
0029   };
0030 
0031 private:
0032   template <typename T>
0033   struct WriterModel final : WriterConcept {
0034     WriterModel(std::unique_ptr<T> writer) : m_writer(std::move(writer)) {
0035     }
0036     WriterModel(const WriterModel&) = delete;
0037     WriterModel& operator=(const WriterModel&) = delete;
0038     WriterModel(WriterModel&&) = default;
0039     WriterModel& operator=(WriterModel&&) = default;
0040 
0041     ~WriterModel() = default;
0042 
0043     void writeFrame(const podio::Frame& frame, const std::string& category,
0044                     const std::vector<std::string>& collections) override {
0045       return m_writer->writeFrame(frame, category, collections);
0046     }
0047     void finish() override {
0048       return m_writer->finish();
0049     }
0050     std::unique_ptr<T> m_writer{nullptr};
0051   };
0052 
0053   std::unique_ptr<WriterConcept> m_self{nullptr};
0054 
0055 public:
0056   /// Create a Writer from a lower level writer
0057   ///
0058   /// @tparam T the type of the low level writer (will be deduced)
0059   /// @param writer A low level writer that does the actual work
0060   template <typename T>
0061   Writer(std::unique_ptr<T> writer) : m_self(std::make_unique<WriterModel<T>>(std::move(writer))) {
0062   }
0063 
0064   Writer(const Writer&) = delete;
0065   Writer& operator=(const Writer&) = delete;
0066   Writer(Writer&&) = default;
0067   Writer& operator=(Writer&&) = default;
0068 
0069   /// Destructor
0070   ///
0071   /// This also takes care of writing all the necessary metadata to read files
0072   /// back again.
0073   ~Writer() = default;
0074 
0075   /// Store the given frame with the given category
0076   ///
0077   /// This stores all available categories from the passed frame
0078   ///
0079   /// @param frame    The frame to write
0080   /// @param category The category name under which this frame should be stored
0081   void writeFrame(const podio::Frame& frame, const std::string& category) {
0082     return m_self->writeFrame(frame, category, frame.getAvailableCollections());
0083   }
0084 
0085   /// Store the given Frame with the given category.
0086   ///
0087   /// This stores only the desired collections and not the complete frame.
0088   ///
0089   /// @param frame        The Frame to store
0090   /// @param category     The category name under which this Frame should be
0091   ///                     stored
0092   /// @param collections  The collection names that should be written
0093   void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collections) {
0094     return m_self->writeFrame(frame, category, collections);
0095   }
0096 
0097   /// Store the given frame under the "events" category
0098   ///
0099   /// This stores all available categories from the passed frame
0100   ///
0101   /// @param frame    The frame to write
0102   void writeEvent(const podio::Frame& frame) {
0103     writeFrame(frame, podio::Category::Event, frame.getAvailableCollections());
0104   }
0105 
0106   /// Store the given Frame under the "events" category
0107   ///
0108   /// This stores only the desired collections and not the complete frame.
0109   ///
0110   /// @param frame        The Frame to store
0111   /// @param collections  The collection names that should be written
0112   void writeEvent(const podio::Frame& frame, const std::vector<std::string>& collections) {
0113     writeFrame(frame, podio::Category::Event, collections);
0114   }
0115 
0116   /// Write the current file, including all the necessary metadata to read it
0117   /// again.
0118   ///
0119   /// @note The destructor will also call this, so letting a Writer go out of
0120   /// scope is also a viable way to write a readable file
0121   void finish() {
0122     return m_self->finish();
0123   }
0124 };
0125 
0126 /// Create a Writer that is able to write files for the desired backend
0127 ///
0128 /// Will look at the desired filename as well as the type argument to decide on
0129 /// the backend.
0130 ///
0131 /// @param filename The filename of the output file that will be created.
0132 /// @param type     The (optional) type argument to switch between RNTuple and TTree
0133 ///                 based backend in case the suffix is ".root". Will be ignored
0134 ///                 in case the suffix is ".sio"
0135 ///
0136 /// @returns A fully initialized Writer for the I/O backend that has been
0137 ///         determined
0138 ///
0139 /// @throws std::runtime_error In case the suffix can not be associated to an
0140 ///         I/O backend or if support for the desired I/O backend has not been built
0141 Writer makeWriter(const std::string& filename, const std::string& type = "default");
0142 
0143 } // namespace podio
0144 
0145 #endif // PODIO_WRITER_H