Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:25:09

0001 #ifndef PODIO_ROOTLEGACYREADER_H
0002 #define PODIO_ROOTLEGACYREADER_H
0003 
0004 #include "podio/ROOTFrameData.h"
0005 #include "podio/podioVersion.h"
0006 #include "podio/utilities/RootHelpers.h"
0007 
0008 #include "TChain.h"
0009 
0010 #include <memory>
0011 #include <string>
0012 #include <tuple>
0013 #include <utility>
0014 #include <vector>
0015 
0016 // forward declarations
0017 class TClass;
0018 // class TChain;
0019 class TFile;
0020 class TTree;
0021 
0022 namespace podio {
0023 
0024 namespace detail {
0025   // Information about the collection class type, whether it is a subset, the
0026   // schema version on file and the index in the collection branches cache
0027   // vector
0028   using CollectionInfo = std::tuple<std::string, bool, SchemaVersionT, size_t>;
0029 } // namespace detail
0030 
0031 class CollectionBase;
0032 class CollectionIDTable;
0033 class GenericParameters;
0034 struct CollectionReadBuffers;
0035 
0036 /// A root reader for reading legacy podio root files that have been written
0037 /// using the legacy, non Frame based I/O model. This reader grants Frame based
0038 /// access to those files, by mimicking the Frame I/O functionality and the
0039 /// interfaces of those readers.
0040 ///
0041 /// @note Since there was only one category ("events") for those legacy podio
0042 /// files this reader will really only work if you try to read that category, and
0043 /// will simply return no data if you try to read anything else.
0044 class ROOTLegacyReader {
0045 
0046 public:
0047   /// Create a SIOLegacyReader
0048   ROOTLegacyReader() = default;
0049   /// Destructor
0050   ~ROOTLegacyReader() = default;
0051 
0052   /// The SIOLegacyReader is not copy-able
0053   ROOTLegacyReader(const ROOTLegacyReader&) = delete;
0054   /// The SIOLegacyReader is not copy-able
0055   ROOTLegacyReader& operator=(const ROOTLegacyReader&) = delete;
0056 
0057   /// Open a single file for reading.
0058   ///
0059   /// @param filename The name of the input file
0060   void openFile(const std::string& filename);
0061 
0062   /// Open multiple files for reading and then treat them as if they are one file
0063   ///
0064   /// @note All of the files are assumed to have the same structure. Specifically
0065   /// this means:
0066   /// - The collections that are contained in the individual event are always the
0067   ///   same
0068   ///
0069   /// This usually boils down to "the files have been written with the same
0070   /// "settings", e.g. they are outputs of a batched process.
0071   ///
0072   /// @param filenames The filenames of all input files that should be read
0073   void openFiles(const std::vector<std::string>& filenames);
0074 
0075   /// Read the next data entry from which a Frame can be constructed.
0076   ///
0077   /// @note the category name has to be "events" in this case, as only that
0078   /// category is available for legacy files. Also the collections to read
0079   /// argument will be ignored.
0080   ///
0081   /// @returns FrameData from which a podio::Frame can be constructed if there
0082   ///          are still entries left to read. Otherwise a nullptr
0083   std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string&, const std::vector<std::string>& = {});
0084 
0085   /// Read the desired data entry from which a Frame can be constructed.
0086   ///
0087   /// @note the category name has to be "events" in this case, as only that
0088   /// category is available for legacy files. Also the collections to read
0089   /// argument will be ignored.
0090   ///
0091   /// @returns FrameData from which a podio::Frame can be constructed if the
0092   ///          desired entry exists. Otherwise a nullptr
0093   std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string&, const unsigned entry,
0094                                                   const std::vector<std::string>& = {});
0095 
0096   /// Get the number of entries for the given name
0097   ///
0098   /// @param name The name of the category
0099   ///
0100   /// @returns The number of entries that are available for the category
0101   unsigned getEntries(const std::string& name) const;
0102 
0103   /// Get the build version of podio that has been used to write the current
0104   /// file
0105   ///
0106   /// @returns The podio build version
0107   podio::version::Version currentFileVersion() const {
0108     return m_fileVersion;
0109   }
0110 
0111   /// Get the names of all the available Frame categories in the current file(s).
0112   ///
0113   /// @returns The names of the available categories from the file
0114   std::vector<std::string_view> getAvailableCategories() const;
0115 
0116 private:
0117   std::pair<TTree*, unsigned> getLocalTreeAndEntry(const std::string& treename);
0118 
0119   void createCollectionBranches(const std::vector<root_utils::CollectionWriteInfo>& collInfo);
0120 
0121   podio::GenericParameters readEventMetaData();
0122 
0123   podio::CollectionReadBuffers getCollectionBuffers(const std::pair<std::string, detail::CollectionInfo>& collInfo);
0124 
0125   std::unique_ptr<podio::ROOTFrameData> readEntry();
0126 
0127   // cache the necessary information to more quickly construct and read each
0128   // collection after it has been read the very first time
0129   std::vector<std::pair<std::string, detail::CollectionInfo>> m_storedClasses{};
0130 
0131   std::shared_ptr<CollectionIDTable> m_table{nullptr};
0132   std::unique_ptr<TChain> m_chain{nullptr};
0133   unsigned m_eventNumber{0};
0134 
0135   // Similar to writing we cache the branches that belong to each collection
0136   // in order to not having to look them up every event. However, for the
0137   // reader we cannot guarantee a fixed order of collections as they are read
0138   // on demand. Hence, we give each collection an index the first time it is
0139   // read and we start caching the branches.
0140   std::vector<root_utils::CollectionBranches> m_collectionBranches{};
0141 
0142   podio::version::Version m_fileVersion{0, 0, 0};
0143 
0144   /// The **only** category name that is available from legacy files
0145   constexpr static auto m_categoryName = "events";
0146 };
0147 
0148 } // namespace podio
0149 
0150 #endif // PODIO_ROOTLEGACYREADER_H