PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ROOTLegacyReader.h
Go to the documentation of this file.
1#ifndef PODIO_ROOTLEGACYREADER_H
2#define PODIO_ROOTLEGACYREADER_H
3
6#include "podio/podioVersion.h"
7
8#include "TChain.h"
9
10#include <iostream>
11#include <memory>
12#include <string>
13#include <tuple>
14#include <utility>
15#include <vector>
16
17// forward declarations
18class TClass;
19// class TChain;
20class TFile;
21class TTree;
22
23namespace podio {
24
25namespace detail {
26 // Information about the data vector as wall as the collection class type
27 // and the index in the collection branches cache vector
28 using CollectionInfo = std::tuple<const TClass*, const TClass*, size_t>;
29
30} // namespace detail
31
32class EventStore;
33class CollectionBase;
34class CollectionIDTable;
35class GenericParameters;
36struct CollectionReadBuffers;
37
38/**
39 * A root reader for reading legacy podio root files that have been written
40 * using the legacy, non Frame based I/O model. This reader grants Frame based
41 * access to those files, by mimicking the Frame I/O functionality and the
42 * interfaces of those readers.
43 *
44 * NOTE: Since there was only one category ("events") for those legacy podio
45 * files this reader will really only work if you try to read that category, and
46 * will simply return no data if you try to read anything else.
47 */
49
50public:
51 ROOTLegacyReader() = default;
52 ~ROOTLegacyReader() = default;
53
54 // non-copyable
57
58 void openFile(const std::string& filename);
59
60 void openFiles(const std::vector<std::string>& filenames);
61
62 /**
63 * Read the next data entry from which a Frame can be constructed. In case
64 * there are no more entries left, this returns a nullptr.
65 *
66 * NOTE: the category name has to be "events" in this case, as only that
67 * category is available for legacy files.
68 */
69 std::unique_ptr<podio::ROOTFrameData> readNextEntry(const std::string&);
70
71 /**
72 * Read the specified data entry from which a Frame can be constructed In case
73 * the entry does not exist, this returns a nullptr.
74 *
75 * NOTE: the category name has to be "events" in this case, as only that
76 * category is available for legacy files.
77 */
78 std::unique_ptr<podio::ROOTFrameData> readEntry(const std::string&, const unsigned entry);
79
80 /// Returns number of entries for a given category
81 unsigned getEntries(const std::string&) const;
82
83 /// Get the build version of podio that has been used to write the current file
85 return m_fileVersion;
86 }
87
88 /// Get the names of all the availalable Frame categories in the current file(s)
89 std::vector<std::string_view> getAvailableCategories() const;
90
91private:
92 std::pair<TTree*, unsigned> getLocalTreeAndEntry(const std::string& treename);
93
94 void createCollectionBranches(const std::vector<std::tuple<int, std::string, bool>>& collInfo);
95
96 podio::GenericParameters readEventMetaData();
97
98 podio::CollectionReadBuffers getCollectionBuffers(const std::pair<std::string, detail::CollectionInfo>& collInfo);
99
100 std::unique_ptr<podio::ROOTFrameData> readEntry();
101
102 // cache the necessary information to more quickly construct and read each
103 // collection after it has been read the very first time
104 std::vector<std::pair<std::string, detail::CollectionInfo>> m_storedClasses{};
105
106 std::shared_ptr<CollectionIDTable> m_table{nullptr};
107 std::unique_ptr<TChain> m_chain{nullptr};
108 unsigned m_eventNumber{0};
109
110 // Similar to writing we cache the branches that belong to each collection
111 // in order to not having to look them up every event. However, for the
112 // reader we cannot guarantee a fixed order of collections as they are read
113 // on demand. Hence, we give each collection an index the first time it is
114 // read and we start caching the branches.
115 std::vector<root_utils::CollectionBranches> m_collectionBranches{};
116
117 podio::version::Version m_fileVersion{0, 0, 0};
118
119 /// The **only** category name that is available from legacy files
120 constexpr static auto m_categoryName = "events";
121};
122
123} // namespace podio
124
125#endif // PODIO_ROOTLEGACYREADER_H
ROOTLegacyReader & operator=(const ROOTLegacyReader &)=delete
ROOTLegacyReader(const ROOTLegacyReader &)=delete
std::unique_ptr< podio::ROOTFrameData > readEntry(const std::string &, const unsigned entry)
void openFile(const std::string &filename)
void openFiles(const std::vector< std::string > &filenames)
std::unique_ptr< podio::ROOTFrameData > readNextEntry(const std::string &)
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 &) const
Returns number of entries for a given category.
podio::version::Version currentFileVersion() const
Get the build version of podio that has been used to write the current file.
std::tuple< const TClass *, const TClass *, size_t > CollectionInfo