PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
EventStore.h
Go to the documentation of this file.
1#ifndef PODIO_EVENTSTORE_H
2#define PODIO_EVENTSTORE_H
3
4#include <iostream>
5#include <memory>
6#include <set>
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11// podio specific includes
17
18/**
19This is an *example* event store
20
21The event store holds the object collections.
22
23It is used to create new collections, and to access existing ones.
24When accessing a collection that is not yet in the event store,
25the event store makes use of a Reader to read the collection.
26
27**/
28
29namespace podio {
30
31class CollectionBase;
32class IReader;
33
34typedef std::map<int, GenericParameters> RunMDMap;
35typedef std::map<int, GenericParameters> ColMDMap;
36
38public:
39 /// Make non-copyable
40 EventStore(const EventStore&) = delete;
41 EventStore& operator=(const EventStore&) = delete;
42
43 /// Collection entry. Each collection is identified by a name
44 typedef std::pair<std::string, CollectionBase*> CollPair;
45 typedef std::vector<CollPair> CollContainer;
46
47 EventStore();
49
50 /// create a new collection
51 template <typename T>
52 T& create(const std::string& name);
53
54 /// register an existing collection
55 void registerCollection(const std::string& name, podio::CollectionBase* coll);
56
57 /// access a collection by name. returns true if successful
58 template <typename T>
59 bool get(const std::string& name, const T*& collection);
60
61 /// fast access to cached collections
62 CollectionBase* getFast(int id) const {
63 return (m_cachedCollections.size() > (unsigned)id ? m_cachedCollections[id] : nullptr);
64 }
65
66 /// access a collection by ID. returns true if successful
67 bool get(int id, CollectionBase*& coll) const final;
68
69 /// access a collection by name
70 /// returns a collection w/ setting isValid to true if successful
71 template <typename T>
72 const T& get(const std::string& name);
73
74 /// empties collections.
75 void clearCollections();
76
77 /// clears itself; deletes collections (use at end of event processing)
78 void clear();
79
80 /// Clears only the cache containers (use at end of event if ownership of read objects is transferred)
81 void clearCaches();
82
83 /// set the reader
84 void setReader(IReader* reader);
85
87 return m_table.get();
88 }
89
90 virtual bool isValid() const final;
91
92 /// return the event meta data for the current event
93 GenericParameters& getEventMetaData() override;
94
95 /// return the run meta data for the given runID
96 GenericParameters& getRunMetaData(int runID) override;
97
98 /// return the collection meta data for the given colID
99 GenericParameters& getCollectionMetaData(int colID) override;
100
101 RunMDMap* getRunMetaDataMap() {
102 return &m_runMDMap;
103 }
105 return &m_colMDMap;
106 }
108 return &m_evtMD;
109 }
110
111private:
112 /// get the collection of given name; returns true if successfull
113 bool doGet(const std::string& name, CollectionBase*& collection, bool setReferences = true) const;
114 /// check if a collection of given name already exists
115 bool collectionRegistered(const std::string& name) const;
116 void setCollectionIDTable(std::shared_ptr<CollectionIDTable> table) {
117 m_table = std::move(table);
118 }
119
120 // members
121 mutable std::set<int> m_retrievedIDs{};
122 mutable CollContainer m_collections{};
123 mutable std::vector<CollectionBase*> m_cachedCollections{};
124 IReader* m_reader{nullptr};
125 std::shared_ptr<CollectionIDTable> m_table;
126
127 GenericParameters m_evtMD{};
128 RunMDMap m_runMDMap{};
129 ColMDMap m_colMDMap{};
130};
131
132template <typename T>
133T& EventStore::create(const std::string& name) {
134 static_assert(std::is_base_of<podio::CollectionBase, T>::value,
135 "DataStore only accepts types inheriting from CollectionBase");
136 // TODO: add check for existence
137 T* coll = new T();
138 registerCollection(name, coll);
139 return *coll;
140}
141
142template <typename T>
143bool EventStore::get(const std::string& name, const T*& collection) {
144 // static_assert(std::is_base_of<podio::CollectionBase,T>::value,
145 // "DataStore only contains types inheriting from CollectionBase");
146 CollectionBase* tmp{nullptr};
147 doGet(name, tmp);
148 collection = static_cast<T*>(tmp);
149 if (collection != nullptr) {
150 return true;
151 }
152 return false;
153}
154
155template <typename T>
156const T& EventStore::get(const std::string& name) {
157 const T* tmp(0);
158 auto success = this->get(name, tmp);
159 if (!success) {
160 throw std::runtime_error("No collection \'" + name + "\' is present in the EventStore");
161 }
162 return *tmp;
163}
164
165} // namespace podio
166#endif
#define DEPR_EVTSTORE
Definition: Deprecated.h:4
void registerCollection(const std::string &name, podio::CollectionBase *coll)
register an existing collection
Definition: EventStore.cc:48
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
Definition: EventStore.h:143
CollectionBase * getFast(int id) const
fast access to cached collections
Definition: EventStore.h:62
EventStore(const EventStore &)=delete
Make non-copyable.
ColMDMap * getColMetaDataMap()
Definition: EventStore.h:104
std::pair< std::string, CollectionBase * > CollPair
Collection entry. Each collection is identified by a name.
Definition: EventStore.h:44
EventStore & operator=(const EventStore &)=delete
std::vector< CollPair > CollContainer
Definition: EventStore.h:45
T & create(const std::string &name)
create a new collection
Definition: EventStore.h:133
CollectionIDTable * getCollectionIDTable() const
Definition: EventStore.h:86
GenericParameters * eventMetaDataPtr()
Definition: EventStore.h:107
std::map< int, GenericParameters > ColMDMap
Definition: EventStore.h:35
std::map< int, GenericParameters > RunMDMap
Definition: EventStore.h:34