Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/podio/RNTupleReader.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #ifndef PODIO_RNTUPLEREADER_H
0002 #define PODIO_RNTUPLEREADER_H
0003 
0004 #include "podio/ROOTFrameData.h"
0005 #include "podio/utilities/ReaderCommon.h"
0006 #include "podio/utilities/RootHelpers.h"
0007 
0008 #include <string>
0009 #include <string_view>
0010 #include <unordered_map>
0011 #include <vector>
0012 
0013 #include <ROOT/RNTuple.hxx>
0014 #include <ROOT/RNTupleReader.hxx>
0015 #include <RVersion.h>
0016 
0017 namespace podio {
0018 
0019 /// Introduce a new namespace instead of potentially opening and polluting the
0020 /// ROOT namespace
0021 namespace root_compat {
0022 #if ROOT_VERSION_CODE < ROOT_VERSION(6, 35, 0)
0023   using RNTupleReader = ROOT::Experimental::RNTupleReader;
0024 #else
0025   using RNTupleReader = ROOT::RNTupleReader;
0026 #endif
0027 } // namespace root_compat
0028 
0029 /// The RNTupleReader can be used to read files that have been written with the
0030 /// RNTuple backend.
0031 ///
0032 /// The RNTupleReader provides the data as ROOTFrameData from which a podio::Frame
0033 /// can be constructed. It can be used to read files written by the RNTupleWriter.
0034 class RNTupleReader : public ReaderCommon {
0035 
0036 public:
0037   RNTupleReader() = default;
0038   ~RNTupleReader() = default;
0039 
0040   RNTupleReader(const RNTupleReader&) = delete;
0041   RNTupleReader& operator=(const RNTupleReader&) = delete;
0042   RNTupleReader(RNTupleReader&&) = default;
0043   RNTupleReader& operator=(RNTupleReader&&) = default;
0044 
0045   /// Open a single file for reading.
0046   ///
0047   /// @param filename The name of the input file
0048   void openFile(const std::string& filename);
0049 
0050   /// Open multiple files for reading and then treat them as if they are one file
0051   ///
0052   /// @note All of the files are assumed to have the same structure. Specifically
0053   /// this means:
0054   /// - The same categories are available from all files
0055   /// - The collections that are contained in the individual categories are the
0056   ///   same across all files
0057   /// - This usually boils down to "the files have been written with the same
0058   ///   "settings", e.g. they are outputs of a batched process.
0059   ///
0060   /// @param filenames The filenames of all input files that should be read
0061   void openFiles(const std::vector<std::string>& filenames);
0062 
0063   /// Read the next data entry for a given category.
0064   ///
0065   /// @param name The category name for which to read the next entry
0066   /// @param collsToRead (optional) the collection names that should be read. If
0067   ///             not provided (or empty) all collections will be read
0068   ///
0069   /// @returns FrameData from which a podio::Frame can be constructed if the
0070   ///          category exists and if there are still entries left to read.
0071   ///          Otherwise a nullptr
0072   ///
0073   /// @throws std::invalid_argument in case collsToRead contains collection
0074   /// names that are not available
0075   std::unique_ptr<podio::ROOTFrameData> readNextEntry(std::string_view name,
0076                                                       const std::vector<std::string>& collsToRead = {});
0077 
0078   /// Read the desired data entry for a given category.
0079   ///
0080   /// @param name  The category name for which to read the next entry
0081   /// @param entry The entry number to read
0082   /// @param collsToRead (optional) the collection names that should be read. If
0083   ///             not provided (or empty) all collections will be read
0084   ///
0085   /// @returns FrameData from which a podio::Frame can be constructed if the
0086   ///          category and the desired entry exist. Otherwise a nullptr
0087   ///
0088   /// @throws std::invalid_argument in case collsToRead contains collection
0089   /// names that are not available
0090   std::unique_ptr<podio::ROOTFrameData> readEntry(std::string_view name, const unsigned entry,
0091                                                   const std::vector<std::string>& collsToRead = {});
0092 
0093   /// Get the number of entries for the given name
0094   ///
0095   /// @param name The name of the category
0096   ///
0097   /// @returns The number of entries that are available for the category
0098   unsigned getEntries(std::string_view name) const;
0099 
0100 private:
0101   /**
0102    * Initialize the given category by filling the maps with metadata information
0103    * that will be used later
0104    */
0105   bool initCategory(std::string_view category);
0106 
0107   /**
0108    * Read and reconstruct the generic parameters of the Frame
0109    */
0110   GenericParameters readEventMetaData(root_compat::RNTupleReader* reader, const unsigned localEntry);
0111 
0112   std::unique_ptr<root_compat::RNTupleReader> m_metadata{};
0113 
0114   std::unordered_map<std::string_view, std::vector<std::unique_ptr<root_compat::RNTupleReader>>> m_readers{};
0115   std::unordered_map<std::string, std::unique_ptr<root_compat::RNTupleReader>> m_metadata_readers{};
0116   std::vector<std::string> m_filenames{};
0117 
0118   std::unordered_map<std::string_view, unsigned> m_entries{};
0119   // Map category to a vector that contains at how many entries each reader starts
0120   // For example, if we have 3 readers and the first one has 10 entries, the second one 20 and the third one 30
0121   // then the vector will be {0, 10, 30}
0122   // 60 is not needed because anything after 30 will be in the last reader
0123   std::unordered_map<std::string_view, std::vector<unsigned>> m_readerEntries{};
0124   std::unordered_map<std::string_view, unsigned> m_totalEntries{};
0125 
0126   /// Map each category to the collections that have been written and are available
0127   std::unordered_map<std::string_view, std::vector<podio::root_utils::CollectionWriteInfo>> m_collectionInfo{};
0128 
0129   std::unordered_map<std::string_view, std::shared_ptr<podio::CollectionIDTable>> m_idTables{};
0130 };
0131 
0132 } // namespace podio
0133 
0134 #endif