Back to home page

EIC code displayed by LXR

 
 

    


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

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.
0079   ///
0080   /// @returns FrameData from which a podio::Frame can be constructed if there
0081   ///          are still entries left to read. Otherwise a nullptr
0082   std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string&);
0083 
0084   /// Read the desired data entry from which a Frame can be constructed.
0085   ///
0086   /// @note the category name has to be "events" in this case, as only that
0087   /// category is available for legacy files.
0088   ///
0089   /// @returns FrameData from which a podio::Frame can be constructed if the
0090   ///          desired entry exists. Otherwise a nullptr
0091   std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string&, const unsigned entry);
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(const std::string& name) const;
0099 
0100   /// Get the build version of podio that has been used to write the current
0101   /// file
0102   ///
0103   /// @returns The podio build version
0104   podio::version::Version currentFileVersion() const {
0105     return m_fileVersion;
0106   }
0107 
0108   /// Get the names of all the available Frame categories in the current file(s).
0109   ///
0110   /// @returns The names of the available categories from the file
0111   std::vector<std::string_view> getAvailableCategories() const;
0112 
0113 private:
0114   std::pair<TTree*, unsigned> getLocalTreeAndEntry(const std::string& treename);
0115 
0116   void createCollectionBranches(const std::vector<root_utils::CollectionWriteInfoT>& collInfo);
0117 
0118   podio::GenericParameters readEventMetaData();
0119 
0120   podio::CollectionReadBuffers getCollectionBuffers(const std::pair<std::string, detail::CollectionInfo>& collInfo);
0121 
0122   std::unique_ptr<podio::ROOTFrameData> readEntry();
0123 
0124   // cache the necessary information to more quickly construct and read each
0125   // collection after it has been read the very first time
0126   std::vector<std::pair<std::string, detail::CollectionInfo>> m_storedClasses{};
0127 
0128   std::shared_ptr<CollectionIDTable> m_table{nullptr};
0129   std::unique_ptr<TChain> m_chain{nullptr};
0130   unsigned m_eventNumber{0};
0131 
0132   // Similar to writing we cache the branches that belong to each collection
0133   // in order to not having to look them up every event. However, for the
0134   // reader we cannot guarantee a fixed order of collections as they are read
0135   // on demand. Hence, we give each collection an index the first time it is
0136   // read and we start caching the branches.
0137   std::vector<root_utils::CollectionBranches> m_collectionBranches{};
0138 
0139   podio::version::Version m_fileVersion{0, 0, 0};
0140 
0141   /// The **only** category name that is available from legacy files
0142   constexpr static auto m_categoryName = "events";
0143 };
0144 
0145 } // namespace podio
0146 
0147 #endif // PODIO_ROOTLEGACYREADER_H