PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
EventStore.cc
Go to the documentation of this file.
1
2// podio specific includes
3#include "podio/EventStore.h"
5#include "podio/IReader.h"
6
7namespace podio {
8
10 m_cachedCollections.resize(128); // allow for a sufficiently large initial number of collections
11}
12
14 for (auto& coll : m_collections) {
15 delete coll.second;
16 }
17}
18
19bool EventStore::get(int id, CollectionBase*& collection) const {
20 // see if we have a cached collection
21 if ((collection = getFast(id)) != nullptr) {
22 return true;
23 }
24
25 auto val = m_retrievedIDs.insert(id);
26 bool success = false;
27 if (val.second == true) {
28 // collection not yet retrieved in recursive-call
29 auto name = m_table->name(id);
30 success = doGet(name, collection, true);
31 if (collection != nullptr) { // cache the collection for faster retreaval later
32 if (m_cachedCollections.size() < (unsigned)id + 1) {
33 m_cachedCollections.resize(id + 1);
34 }
35 m_cachedCollections[id] = collection;
36 }
37 } else {
38 // collection already requested in recursive call
39 // do not set the references to break collection dependency-cycle
40 auto name = m_table->name(id);
41 success = doGet(name, collection, false);
42 }
43 // fg: the set should only be cleared at the end of event (in clear() ) ...
44 // m_retrievedIDs.erase(id);
45 return success;
46}
47
48void EventStore::registerCollection(const std::string& name, podio::CollectionBase* coll) {
49 m_collections.push_back({name, coll});
50 auto id = m_table->add(name);
51 coll->setID(id);
52}
53
54bool EventStore::isValid() const {
55 return m_reader->isValid();
56}
57
58bool EventStore::doGet(const std::string& name, CollectionBase*& collection, bool setReferences) const {
59 auto result = std::find_if(begin(m_collections), end(m_collections),
60 [&name](const CollPair& item) -> bool { return name == item.first; });
61 if (result != end(m_collections)) {
62 auto tmp = result->second;
63 if (tmp != nullptr) {
64 collection = tmp;
65 return true;
66 }
67 } else if (m_reader != nullptr) {
68 auto tmp = m_reader->readCollection(name);
69 if (setReferences == true) {
70 if (tmp != nullptr) {
71 tmp->setReferences(this);
72 // check again whether collection exists
73 // it may have been created on-demand already
74 if (collectionRegistered(name) == false) {
75 m_collections.emplace_back(std::make_pair(name, tmp));
76 }
77 }
78 }
79 collection = tmp;
80 if (tmp != nullptr) {
81 return true;
82 }
83 } else {
84 return false;
85 }
86 return false;
87}
88
90
91 if (m_evtMD.empty() && m_reader != nullptr) {
92 GenericParameters* tmp = m_reader->readEventMetaData();
93 m_evtMD = std::move(*tmp);
94 delete tmp;
95 }
96 return m_evtMD;
97}
98
100
101 if (m_runMDMap.empty() && m_reader != nullptr) {
102 RunMDMap* tmp = m_reader->readRunMetaData();
103 m_runMDMap = std::move(*tmp);
104 delete tmp;
105 }
106 return m_runMDMap[runID];
107}
108
110
111 if (m_colMDMap.empty() && m_reader != nullptr) {
112 ColMDMap* tmp = m_reader->readCollectionMetaData();
113 m_colMDMap = std::move(*tmp);
114 delete tmp;
115 }
116 return m_colMDMap[colID];
117}
118
120 for (auto& coll : m_collections) {
121 coll.second->clear();
122 }
123 m_evtMD.clear();
124}
125
127 for (auto& coll : m_collections) {
128 coll.second->clear();
129 delete coll.second;
130 }
131
132 m_evtMD.clear();
133 clearCaches();
134}
135
137 m_collections.clear();
138 m_cachedCollections.clear();
139 m_cachedCollections.resize(128);
140 m_retrievedIDs.clear();
141}
142
143bool EventStore::collectionRegistered(const std::string& name) const {
144 auto result = std::find_if(begin(m_collections), end(m_collections),
145 [&name](const CollPair& item) -> bool { return name == item.first; });
146 return (result != end(m_collections));
147}
148
150 m_reader = reader;
151 setCollectionIDTable(reader->getCollectionIDTable());
152}
153
154} // namespace podio
virtual void setID(unsigned id)=0
set collection ID
virtual bool setReferences(const ICollectionProvider *collectionProvider)=0
initialize references after read
void registerCollection(const std::string &name, podio::CollectionBase *coll)
register an existing collection
Definition: EventStore.cc:48
virtual bool isValid() const final
Definition: EventStore.cc:54
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
Definition: EventStore.h:143
GenericParameters & getRunMetaData(int runID) override
return the run meta data for the given runID
Definition: EventStore.cc:99
void clearCaches()
Clears only the cache containers (use at end of event if ownership of read objects is transferred)
Definition: EventStore.cc:136
void clear()
clears itself; deletes collections (use at end of event processing)
Definition: EventStore.cc:126
CollectionBase * getFast(int id) const
fast access to cached collections
Definition: EventStore.h:62
GenericParameters & getEventMetaData() override
return the event meta data for the current event
Definition: EventStore.cc:89
std::pair< std::string, CollectionBase * > CollPair
Collection entry. Each collection is identified by a name.
Definition: EventStore.h:44
void clearCollections()
empties collections.
Definition: EventStore.cc:119
void setReader(IReader *reader)
set the reader
Definition: EventStore.cc:149
GenericParameters & getCollectionMetaData(int colID) override
return the collection meta data for the given colID
Definition: EventStore.cc:109
bool empty() const
Check if no parameter is stored (i.e. if all internal maps are empty)
void clear()
erase all elements
virtual CollectionBase * readCollection(const std::string &name)=0
virtual std::shared_ptr< CollectionIDTable > getCollectionIDTable()=0
Get CollectionIDTable of read-in data.
virtual bool isValid() const =0
Check if reader is valid.
virtual std::map< int, GenericParameters > * readRunMetaData()=0
virtual GenericParameters * readEventMetaData()=0
read event meta data from file
virtual std::map< int, GenericParameters > * readCollectionMetaData()=0
std::map< int, GenericParameters > ColMDMap
Definition: EventStore.h:35
std::map< int, GenericParameters > RunMDMap
Definition: EventStore.h:34