Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef PODIO_RNTUPLEREADER_H
0002 #define PODIO_RNTUPLEREADER_H
0003 
0004 #include "podio/ROOTFrameData.h"
0005 #include "podio/SchemaEvolution.h"
0006 #include "podio/podioVersion.h"
0007 #include "podio/utilities/DatamodelRegistryIOHelpers.h"
0008 
0009 #include <string>
0010 #include <string_view>
0011 #include <unordered_map>
0012 #include <vector>
0013 
0014 #include <ROOT/RNTuple.hxx>
0015 #include <RVersion.h>
0016 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 31, 0)
0017   #include <ROOT/RNTupleReader.hxx>
0018 #endif
0019 
0020 namespace podio {
0021 
0022 /**
0023 This class has the function to read available data from disk
0024 and to prepare collections and buffers.
0025 **/
0026 /// The RNTupleReader can be used to read files that have been written with the
0027 /// RNTuple backend.
0028 ///
0029 /// The RNTupleReader provides the data as ROOTFrameData from which a podio::Frame
0030 /// can be constructed. It can be used to read files written by the RNTupleWriter.
0031 class RNTupleReader {
0032 
0033 public:
0034   /// Create a RNTupleReader
0035   RNTupleReader() = default;
0036   /// Destructor
0037   ~RNTupleReader() = default;
0038   /// The RNTupleReader is not copy-able
0039   RNTupleReader(const RNTupleReader&) = delete;
0040   /// The RNTupleReader is not copy-able
0041   RNTupleReader& operator=(const RNTupleReader&) = delete;
0042 
0043   /// Open a single file for reading.
0044   ///
0045   /// @param filename The name of the input file
0046   void openFile(const std::string& filename);
0047 
0048   /// Open multiple files for reading and then treat them as if they are one file
0049   ///
0050   /// @note All of the files are assumed to have the same structure. Specifically
0051   /// this means:
0052   /// - The same categories are available from all files
0053   /// - The collections that are contained in the individual categories are the
0054   ///   same across all files
0055   /// - This usually boils down to "the files have been written with the same
0056   ///   "settings", e.g. they are outputs of a batched process.
0057   ///
0058   /// @param filenames The filenames of all input files that should be read
0059   void openFiles(const std::vector<std::string>& filenames);
0060 
0061   /// Read the next data entry for a given category.
0062   ///
0063   /// @param name The category name for which to read the next entry
0064   ///
0065   /// @returns FrameData from which a podio::Frame can be constructed if the
0066   ///          category exists and if there are still entries left to read.
0067   ///          Otherwise a nullptr
0068   std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string& name);
0069 
0070   /// Read the desired data entry for a given category.
0071   ///
0072   /// @param name  The category name for which to read the next entry
0073   /// @param entry The entry number to read
0074   ///
0075   /// @returns FrameData from which a podio::Frame can be constructed if the
0076   ///          category and the desired entry exist. Otherwise a nullptr
0077   std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry);
0078 
0079   /// Get the names of all the available Frame categories in the current file(s).
0080   ///
0081   /// @returns The names of the available categores from the file
0082   std::vector<std::string_view> getAvailableCategories() const;
0083 
0084   /// Get the number of entries for the given name
0085   ///
0086   /// @param name The name of the category
0087   ///
0088   /// @returns The number of entries that are available for the category
0089   unsigned getEntries(const std::string& name);
0090 
0091   /// Get the build version of podio that has been used to write the current
0092   /// file
0093   ///
0094   /// @returns The podio build version
0095   podio::version::Version currentFileVersion() const {
0096     return m_fileVersion;
0097   }
0098 
0099   /// Get the (build) version of a datamodel that has been used to write the
0100   /// current file
0101   ///
0102   /// @param name The name of the datamodel
0103   ///
0104   /// @returns The (build) version of the datamodel if available or an empty
0105   ///          optional
0106   std::optional<podio::version::Version> currentFileVersion(const std::string& name) const {
0107     return m_datamodelHolder.getDatamodelVersion(name);
0108   }
0109 
0110   /// Get the datamodel definition for the given name
0111   ///
0112   /// @param name The name of the datamodel
0113   ///
0114   /// @returns The high level definition of the datamodel in JSON format
0115   const std::string_view getDatamodelDefinition(const std::string& name) const {
0116     return m_datamodelHolder.getDatamodelDefinition(name);
0117   }
0118 
0119   /// Get all names of the datamodels that are available from this reader
0120   ///
0121   /// @returns The names of the datamodels
0122   std::vector<std::string> getAvailableDatamodels() const {
0123     return m_datamodelHolder.getAvailableDatamodels();
0124   }
0125 
0126 private:
0127   /**
0128    * Initialize the given category by filling the maps with metadata information
0129    * that will be used later
0130    */
0131   bool initCategory(const std::string& category);
0132 
0133   /**
0134    * Read and reconstruct the generic parameters of the Frame
0135    */
0136   GenericParameters readEventMetaData(const std::string& name, unsigned entNum);
0137 
0138   template <typename T>
0139   void readParams(const std::string& name, unsigned entNum, GenericParameters& params);
0140 
0141   std::unique_ptr<ROOT::Experimental::RNTupleReader> m_metadata{};
0142 
0143   podio::version::Version m_fileVersion{};
0144   DatamodelDefinitionHolder m_datamodelHolder{};
0145 
0146   std::unordered_map<std::string, std::vector<std::unique_ptr<ROOT::Experimental::RNTupleReader>>> m_readers{};
0147   std::unordered_map<std::string, std::unique_ptr<ROOT::Experimental::RNTupleReader>> m_metadata_readers{};
0148   std::vector<std::string> m_filenames{};
0149 
0150   std::unordered_map<std::string, int> m_entries{};
0151   std::unordered_map<std::string, unsigned> m_totalEntries{};
0152 
0153   struct CollectionInfo {
0154     std::vector<unsigned int> id{};
0155     std::vector<std::string> name{};
0156     std::vector<std::string> type{};
0157     std::vector<short> isSubsetCollection{};
0158     std::vector<SchemaVersionT> schemaVersion{};
0159   };
0160 
0161   std::unordered_map<std::string, CollectionInfo> m_collectionInfo{};
0162 
0163   std::vector<std::string> m_availableCategories{};
0164 
0165   std::unordered_map<std::string, std::shared_ptr<podio::CollectionIDTable>> m_idTables{};
0166 };
0167 
0168 } // namespace podio
0169 
0170 #endif