PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ROOTReader.h
Go to the documentation of this file.
1#ifndef PODIO_ROOTREADER_H
2#define PODIO_ROOTREADER_H
3
5#include "podio/IReader.h"
6
7#include <algorithm>
8#include <iostream>
9#include <map>
10#include <string>
11#include <tuple>
12#include <utility>
13#include <vector>
14
15// forward declarations
16class TClass;
17class TFile;
18class TTree;
19class TChain;
20
21namespace podio {
22
23class EventStore;
24class CollectionBase;
25class Registry;
26class CollectionIDTable;
27class GenericParameters;
28
29/**
30This class has the function to read available data from disk
31and to prepare collections and buffers.
32**/
33class ROOTReader : public IReader {
34 friend EventStore;
35
36public:
37 ROOTReader() = default;
38 // todo: see https://github.com/AIDASoft/podio/issues/290
39 ~ROOTReader(); // NOLINT(modernize-use-equals-default)
40
41 // non-copyable
42 ROOTReader(const ROOTReader&) = delete;
43 ROOTReader& operator=(const ROOTReader&) = delete;
44
45 void openFile(const std::string& filename) override;
46 void openFiles(const std::vector<std::string>& filenames);
47 void closeFile() override;
48 void closeFiles();
49
50 /// Read all collections requested
51 void readEvent() override;
52
53 /// Read CollectionIDTable from ROOT file
54 std::shared_ptr<CollectionIDTable> getCollectionIDTable() override {
55 return m_table;
56 }
57
58 /// Returns number of entries in the TTree
59 unsigned getEntries() const override;
60
61 /// Preparing to read next event
62 void endOfEvent() override;
63
64 /// Preparing to read a given event
65 void goToEvent(unsigned evnum) override;
66
68 return m_fileVersion;
69 }
70
71 /// Check if TFile is valid
72 bool isValid() const override;
73
74private:
75 /// Implementation for collection reading
76 CollectionBase* readCollection(const std::string& name) override;
77
78 /// read event meta data for current event
79 GenericParameters* readEventMetaData() override;
80
81 /// read the collection meta data
82 std::map<int, GenericParameters>* readCollectionMetaData() override;
83
84 /// read the run meta data
85 std::map<int, GenericParameters>* readRunMetaData() override;
86
87private:
88 void createCollectionBranches(const std::vector<std::tuple<int, std::string, bool>>& collInfo);
89
90 std::pair<TTree*, unsigned> getLocalTreeAndEntry(const std::string& treename);
91 // Information about the data vector as wall as the collection class type
92 // and the index in the collection branches cache vector
93 using CollectionInfo = std::tuple<const TClass*, const TClass*, size_t>;
94
95 CollectionBase* getCollection(const std::pair<std::string, CollectionInfo>& collInfo);
96 CollectionBase* readCollectionData(const root_utils::CollectionBranches& branches, CollectionBase* collection,
97 Long64_t entry, const std::string& name);
98
99 // cache collections that have been read already in a given event
100 typedef std::pair<CollectionBase*, std::string> Input;
101 std::vector<Input> m_inputs{};
102
103 // cache the necessary information to more quickly construct and read each
104 // collection after it has been read the very first time
105 std::map<std::string, CollectionInfo> m_storedClasses{};
106
107 std::shared_ptr<CollectionIDTable> m_table{nullptr};
108 TChain* m_chain{nullptr};
109 unsigned m_eventNumber{0};
110
111 // Similar to writing we cache the branches that belong to each collection
112 // in order to not having to look them up every event. However, for the
113 // reader we cannot guarantee a fixed order of collections as they are read
114 // on demand. Hence, we give each collection an index the first time it is
115 // read and we start caching the branches.
116 size_t m_collectionIndex = 0;
117 std::vector<root_utils::CollectionBranches> m_collectionBranches{};
118
119 podio::version::Version m_fileVersion{0, 0, 0};
120};
121
122} // namespace podio
123
124#endif
void openFile(const std::string &filename) override
Definition: ROOTReader.cc:140
ROOTReader()=default
void goToEvent(unsigned evnum) override
Preparing to read a given event.
Definition: ROOTReader.cc:221
podio::version::Version currentFileVersion() const override
Get the podio version with which the current file has been written.
Definition: ROOTReader.h:67
unsigned getEntries() const override
Returns number of entries in the TTree.
Definition: ROOTReader.cc:217
void readEvent() override
Read all collections requested.
Definition: ROOTReader.cc:196
ROOTReader(const ROOTReader &)=delete
ROOTReader & operator=(const ROOTReader &)=delete
std::shared_ptr< CollectionIDTable > getCollectionIDTable() override
Read CollectionIDTable from ROOT file.
Definition: ROOTReader.h:54
void openFiles(const std::vector< std::string > &filenames)
Definition: ROOTReader.cc:144
bool isValid() const override
Check if TFile is valid.
Definition: ROOTReader.cc:208
void closeFile() override
Definition: ROOTReader.cc:188
void endOfEvent() override
Preparing to read next event.
Definition: ROOTReader.cc:212
void readCollection()