PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
UserDataCollection.h
Go to the documentation of this file.
1#ifndef PODIO_USERDATACOLLECTION_H
2#define PODIO_USERDATACOLLECTION_H
3
8
9#include <map>
10#include <string>
11#include <typeindex>
12#include <utility>
13#include <vector>
14
15#define PODIO_ADD_USER_TYPE(type) \
16 template <> \
17 constexpr const char* userDataTypeName<type>() { \
18 return #type; \
19 }
20
21namespace podio {
22
23/** tuple of basic types supported in user vector
24 */
26 std::tuple<float, double, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t>;
27
28/**
29 * Alias template to be used to enable template specializations only for the types listed in the
30 * SupportedUserDataTypes list
31 */
32template <typename T>
33using EnableIfSupportedUserType = std::enable_if_t<detail::isInTuple<T, SupportedUserDataTypes>>;
34
35/** helper template to provide readable type names for basic types with macro PODIO_ADD_USER_TYPE(type)
36 */
37template <typename BasicType, typename = EnableIfSupportedUserType<BasicType>>
38constexpr const char* userDataTypeName();
39
42
48PODIO_ADD_USER_TYPE(uint16_t)
49PODIO_ADD_USER_TYPE(uint32_t)
50PODIO_ADD_USER_TYPE(uint64_t)
51
52/** Collection of basic types for additional user data not defined in the EDM.
53 * The data is stored in an std::vector<basic_type>. Supported are all basic types supported in
54 * PODIO, i.e. float, double and 8-64 bit fixed size signed and unsigned integers - @see SupportedUserDataTypes.
55 * @author F.Gaede, DESY
56 * @date Sep 2021
57 */
58template <typename BasicType, typename = EnableIfSupportedUserType<BasicType>>
60
61private:
62 std::vector<BasicType> _vec{};
63 // Pointer to the actual storage, necessary for I/O. In order to have
64 // simpler move-semantics this will be set and properly initialized on
65 // demand during the call to getBuffers
66 std::vector<BasicType>* _vecPtr{nullptr};
67 int m_collectionID{0};
68 CollRefCollection m_refCollections{};
69 VectorMembersInfo m_vecmem_info{};
70
71public:
72 UserDataCollection() = default;
73 /// Constructor from an existing vector (wich will be moved from!)
74 UserDataCollection(std::vector<BasicType>&& vec) : _vec(std::move(vec)) {
75 }
81
82 /// prepare buffers for serialization
83 void prepareForWrite() const override {
84 }
85
86 /// re-create collection from buffers after read
87 void prepareAfterRead() override {
88 }
89
90 /// initialize references after read
91 bool setReferences(const ICollectionProvider*) override {
92 return true;
93 }
94
95 /// set collection ID
96 void setID(unsigned id) override {
97 m_collectionID = id;
98 }
99
100 /// get collection ID
101 unsigned getID() const override {
102 return m_collectionID;
103 }
104
105 /// Get the collection buffers for this collection
107 _vecPtr = &_vec; // Set the pointer to the correct internal vector
108 return {&_vecPtr, &m_refCollections, &m_vecmem_info};
109 }
110
112 return {nullptr, nullptr, nullptr,
113 [](podio::CollectionReadBuffers buffers, bool) {
114 return std::make_unique<UserDataCollection<BasicType>>(std::move(*buffers.dataAsVector<BasicType>()));
115 },
116 [](podio::CollectionReadBuffers& buffers) {
117 buffers.data = podio::CollectionWriteBuffers::asVector<BasicType>(buffers.data);
118 }};
119 }
120
121 /// check for validity of the container after read
122 bool isValid() const override {
123 return true;
124 }
125
126 /// number of elements in the collection
127 size_t size() const override {
128 return _vec.size();
129 }
130
131 /// fully qualified type name
132 std::string getTypeName() const override {
133 return std::string("podio::UserDataCollection<") + userDataTypeName<BasicType>() + ">";
134 }
135
136 /// fully qualified type name of elements - with namespace
137 std::string getValueTypeName() const override {
138 return userDataTypeName<BasicType>();
139 }
140
141 /// fully qualified type name of stored POD elements - with namespace
142 std::string getDataTypeName() const override {
143 return userDataTypeName<BasicType>();
144 }
145
146 /// clear the collection and all internal states
147 void clear() override {
148 _vec.clear();
149 };
150
151 /// check if this collection is a subset collection - no subset possible
152 bool isSubsetCollection() const override {
153 return false;
154 }
155
156 /// declare this collection to be a subset collectionv - no effect
157 void setSubsetCollection(bool) override {
158 }
159
160 /// Print this collection to the passed stream
161 void print(std::ostream& os = std::cout, bool flush = true) const override {
162 os << "[";
163 if (!_vec.empty()) {
164 os << _vec[0];
165 for (size_t i = 0; i < _vec.size(); ++i) {
166 os << ", " << _vec[i];
167 }
168 }
169 os << "]";
170
171 if (flush) {
172 os.flush(); // Necessary for python
173 }
174 }
175
176 size_t getDatamodelRegistryIndex() const override {
178 }
179
180 // ----- some wrapers for std::vector and access to the complete std::vector (if really needed)
181
182 typename std::vector<BasicType>::iterator begin() {
183 return _vec.begin();
184 }
185 typename std::vector<BasicType>::iterator end() {
186 return _vec.end();
187 }
188 typename std::vector<BasicType>::const_iterator begin() const {
189 return _vec.begin();
190 }
191 typename std::vector<BasicType>::const_iterator end() const {
192 return _vec.end();
193 }
194
195 typename std::vector<BasicType>::reference operator[](size_t idx) {
196 return _vec[idx];
197 }
198 typename std::vector<BasicType>::const_reference operator[](size_t idx) const {
199 return _vec[idx];
200 }
201
202 void resize(size_t count) {
203 _vec.resize(count);
204 }
205 void push_back(const BasicType& value) {
206 _vec.push_back(value);
207 }
208
209 /// access to the actual data vector
210 typename std::vector<BasicType>& vec() {
211 return _vec;
212 }
213
214 /// const access to the actual data vector
215 const typename std::vector<BasicType>& vec() const {
216 return _vec;
217 }
218};
219
220// don't make this macro public as it should only be used internally here...
221#undef PODIO_ADD_USER_TYPE
222
223template <typename BasicType, typename = EnableIfSupportedUserType<BasicType>>
224std::ostream& operator<<(std::ostream& o, const podio::UserDataCollection<BasicType>& coll) {
225 coll.print(o);
226 return o;
227}
228
229} // namespace podio
230
231#endif
#define PODIO_ADD_USER_TYPE(type)
static constexpr size_t NoDefinitionNecessary
void setSubsetCollection(bool) override
declare this collection to be a subset collectionv - no effect
std::string getTypeName() const override
fully qualified type name
void push_back(const BasicType &value)
std::vector< BasicType > & vec()
access to the actual data vector
void prepareAfterRead() override
re-create collection from buffers after read
UserDataCollection & operator=(const UserDataCollection &)=delete
UserDataCollection(std::vector< BasicType > &&vec)
Constructor from an existing vector (wich will be moved from!)
std::string getDataTypeName() const override
fully qualified type name of stored POD elements - with namespace
std::vector< BasicType >::const_iterator begin() const
bool setReferences(const ICollectionProvider *) override
initialize references after read
bool isValid() const override
check for validity of the container after read
podio::CollectionWriteBuffers getBuffers() override
Get the collection buffers for this collection.
const std::vector< BasicType > & vec() const
const access to the actual data vector
std::vector< BasicType >::const_iterator end() const
bool isSubsetCollection() const override
check if this collection is a subset collection - no subset possible
void setID(unsigned id) override
set collection ID
UserDataCollection(UserDataCollection &&)=default
std::string getValueTypeName() const override
fully qualified type name of elements - with namespace
unsigned getID() const override
get collection ID
std::vector< BasicType >::iterator end()
podio::CollectionReadBuffers createBuffers() final
Create (empty) collection buffers from which a collection can be constructed.
std::vector< BasicType >::reference operator[](size_t idx)
size_t size() const override
number of elements in the collection
void prepareForWrite() const override
prepare buffers for serialization
UserDataCollection & operator=(UserDataCollection &&)=default
void print(std::ostream &os=std::cout, bool flush=true) const override
Print this collection to the passed stream.
std::vector< BasicType >::iterator begin()
void clear() override
clear the collection and all internal states
std::vector< BasicType >::const_reference operator[](size_t idx) const
size_t getDatamodelRegistryIndex() const override
Get the index in the DatatypeRegistry of the EDM this collection belongs to.
UserDataCollection(const UserDataCollection &)=delete
std::tuple< float, double, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t > SupportedUserDataTypes
std::vector< std::pair< std::string, void * > > VectorMembersInfo
std::ostream & operator<<(std::ostream &o, const podio::UserDataCollection< BasicType > &coll)
std::enable_if_t< detail::isInTuple< T, SupportedUserDataTypes > > EnableIfSupportedUserType
constexpr const char * userDataTypeName()
std::vector< UVecPtr< podio::ObjectID > > CollRefCollection
std::vector< DataT > * dataAsVector()