PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ROOTFrameReader.h
Go to the documentation of this file.
1#ifndef PODIO_ROOTFRAMEREADER_H
2#define PODIO_ROOTFRAMEREADER_H
3
6#include "podio/podioVersion.h"
8
9#include "TChain.h"
10
11#include <iostream>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <tuple>
16#include <utility>
17#include <vector>
18
19// forward declarations
20class TClass;
21// class TChain;
22class TFile;
23class TTree;
24
25namespace podio {
26
27namespace detail {
28 // Information about the data vector as wall as the collection class type
29 // and the index in the collection branches cache vector
30 using CollectionInfo = std::tuple<const TClass*, const TClass*, size_t>;
31
32} // namespace detail
33
34class EventStore;
35class CollectionBase;
39
40/**
41 * This class has the function to read available data from disk
42 * and to prepare collections and buffers.
43 **/
45
46public:
47 ROOTFrameReader() = default;
48 ~ROOTFrameReader() = default;
49
50 // non-copyable
53
54 void openFile(const std::string& filename);
55
56 void openFiles(const std::vector<std::string>& filenames);
57
58 /**
59 * Read the next data entry from which a Frame can be constructed for the
60 * given name. In case there are no more entries left for this name or in
61 * case there is no data for this name, this returns a nullptr.
62 */
63 std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string& name);
64
65 /**
66 * Read the specified data entry from which a Frame can be constructed for
67 * the given name. In case the entry does not exist for this name or in case
68 * there is no data for this name, this returns a nullptr.
69 */
70 std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string& name, const unsigned entry);
71
72 /// Returns number of entries for the given name
73 unsigned getEntries(const std::string& name) const;
74
75 /// Get the build version of podio that has been used to write the current file
77 return m_fileVersion;
78 }
79
80 /// Get the names of all the availalable Frame categories in the current file(s)
81 std::vector<std::string_view> getAvailableCategories() const;
82
83 /// Get the datamodel definition for the given name
84 const std::string_view getDatamodelDefinition(const std::string& name) const {
85 return m_datamodelHolder.getDatamodelDefinition(name);
86 }
87
88 /// Get all names of the datamodels that ara available from this reader
89 std::vector<std::string> getAvailableDatamodels() const {
90 return m_datamodelHolder.getAvailableDatamodels();
91 }
92
93private:
94 /**
95 * Helper struct to group together all the necessary state to read / process a
96 * given category. A "category" in this case describes all frames with the
97 * same name which are constrained by the ROOT file structure that we use to
98 * have the same contents. It encapsulates all state that is necessary for
99 * reading from a TTree / TChain (i.e. collection infos, branches, ...)
100 */
101 struct CategoryInfo {
102 /// constructor from chain for more convenient map insertion
103 CategoryInfo(std::unique_ptr<TChain>&& c) : chain(std::move(c)) {
104 }
105 std::unique_ptr<TChain> chain{nullptr}; ///< The TChain with the data
106 unsigned entry{0}; ///< The next entry to read
107 std::vector<std::pair<std::string, detail::CollectionInfo>> storedClasses{}; ///< The stored collections in this
108 ///< category
109 std::vector<root_utils::CollectionBranches> branches{}; ///< The branches for this category
110 std::shared_ptr<CollectionIDTable> table{nullptr}; ///< The collection ID table for this category
111 };
112
113 /**
114 * Initialze the passed CategoryInfo by setting up the necessary branches,
115 * collection infos and all necessary meta data to be able to read entries
116 * with this name
117 */
118 void initCategory(CategoryInfo& catInfo, const std::string& name);
119
120 /**
121 * Get the category information for the given name. In case there is no TTree
122 * with contents for the given name this will return a CategoryInfo with an
123 * uninitialized chain (nullptr) member
124 */
125 CategoryInfo& getCategoryInfo(const std::string& name);
126
127 GenericParameters readEventMetaData(CategoryInfo& catInfo);
128
129 /**
130 * Read the data entry specified in the passed CategoryInfo, and increase the
131 * counter aferwards. In case the requested entry is larger than the
132 * available number of entries, return a nullptr.
133 */
134 std::unique_ptr<podio::ROOTFrameData> readEntry(ROOTFrameReader::CategoryInfo& catInfo);
135
136 /**
137 * Get / read the buffers at index iColl in the passed category information
138 */
139 podio::CollectionReadBuffers getCollectionBuffers(CategoryInfo& catInfo, size_t iColl);
140
141 std::unique_ptr<TChain> m_metaChain{nullptr}; ///< The metadata tree
142 std::unordered_map<std::string, CategoryInfo> m_categories{}; ///< All categories
143 std::vector<std::string> m_availCategories{}; ///< All available categories from this file
144
145 podio::version::Version m_fileVersion{0, 0, 0};
146 DatamodelDefinitionHolder m_datamodelHolder{};
147};
148
149} // namespace podio
150
151#endif // PODIO_ROOTFRAMEREADER_H
std::vector< std::string > getAvailableDatamodels() const
const std::string_view getDatamodelDefinition(const std::string &name) const
void openFile(const std::string &filename)
const std::string_view getDatamodelDefinition(const std::string &name) const
Get the datamodel definition for the given name.
std::unique_ptr< podio::ROOTFrameData > readNextEntry(const std::string &name)
void openFiles(const std::vector< std::string > &filenames)
podio::version::Version currentFileVersion() const
Get the build version of podio that has been used to write the current file.
std::vector< std::string_view > getAvailableCategories() const
Get the names of all the availalable Frame categories in the current file(s)
unsigned getEntries(const std::string &name) const
Returns number of entries for the given name.
std::vector< std::string > getAvailableDatamodels() const
Get all names of the datamodels that ara available from this reader.
ROOTFrameReader(const ROOTFrameReader &)=delete
ROOTFrameReader & operator=(const ROOTFrameReader &)=delete
std::unique_ptr< podio::ROOTFrameData > readEntry(const std::string &name, const unsigned entry)
std::tuple< const TClass *, const TClass *, size_t > CollectionInfo