EDM4eic
EIC data model
All Classes Namespaces Files Functions Variables Typedefs Friends Pages
TensorCollection.h
Go to the documentation of this file.
1// AUTOMATICALLY GENERATED FILE - DO NOT EDIT
2
3#ifndef EDM4EIC_TensorCollection_H
4#define EDM4EIC_TensorCollection_H
5
6// datamodel specific includes
7#include "edm4eic/Tensor.h"
9#include "edm4eic/TensorObj.h"
11
12// podio specific includes
13#include "podio/ICollectionProvider.h"
14#include "podio/CollectionBase.h"
15
16#if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
17#include "nlohmann/json_fwd.hpp"
18#endif
19
20#include <string_view>
21#include <vector>
22#include <algorithm>
23#include <ostream>
24#include <mutex>
25#include <memory>
26#include <cstddef>
27
28namespace podio {
29 struct RelationNames;
30}
31
32namespace edm4eic {
33
34
35
37public:
38 TensorCollectionIterator(size_t index, const TensorObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<TensorObj>{nullptr}), m_collection(collection) {}
39
42
43 bool operator!=(const TensorCollectionIterator& x) const {
44 return m_index != x.m_index; // TODO: may not be complete
45 }
46
47 bool operator==(const TensorCollectionIterator& x) const {
48 return m_index == x.m_index; // TODO: may not be complete
49 }
50
54
55private:
56 size_t m_index;
57 Tensor m_object;
58 const TensorObjPointerContainer* m_collection;
59};
60
61
63public:
64 TensorMutableCollectionIterator(size_t index, const TensorObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<TensorObj>{nullptr}), m_collection(collection) {}
65
68
70 return m_index != x.m_index; // TODO: may not be complete
71 }
72
74 return m_index == x.m_index; // TODO: may not be complete
75 }
76
80
81private:
82 size_t m_index;
83 MutableTensor m_object;
84 const TensorObjPointerContainer* m_collection;
85};
86
87
88/**
89A Collection is identified by an ID.
90*/
91class TensorCollection : public podio::CollectionBase {
92public:
96 using difference_type = ptrdiff_t;
97 using size_type = size_t;
98
100 TensorCollection(TensorCollectionData&& data, bool isSubsetColl);
101 // This is a move-only type
106
107// TensorCollection(TensorVector* data, uint32_t collectionID);
108 ~TensorCollection() override;
109
110 constexpr static auto typeName = "edm4eic::TensorCollection";
111 constexpr static auto valueTypeName = "edm4eic::Tensor";
112 constexpr static auto dataTypeName = "edm4eic::TensorData";
113
114 void clear() final;
115
116 /// Print this collection to the passed stream
117 void print(std::ostream& os=std::cout, bool flush=true) const final;
118
119 /// operator to allow pointer like calling of members a la LCIO
120 TensorCollection* operator->() { return static_cast<TensorCollection*>(this); }
121
122 /// Append a new object to the collection, and return this object.
124
125 /// Append a new object to the collection, and return this object.
126 /// Initialized with the parameters given
127 template<typename... Args>
128 MutableTensor create(Args&&... args);
129
130 /// number of elements in the collection
131 std::size_t size() const final;
132
133 /// maximal number of elements in the collection
134 std::size_t max_size() const final;
135
136 /// Is the collection empty
137 bool empty() const final;
138
139 /// fully qualified type name
140 const std::string_view getTypeName() const final { return typeName; }
141 /// fully qualified type name of elements - with namespace
142 const std::string_view getValueTypeName() const final { return valueTypeName; }
143 /// fully qualified type name of stored POD elements - with namespace
144 const std::string_view getDataTypeName() const final { return dataTypeName; }
145 /// schema version
146 podio::SchemaVersionT getSchemaVersion() const final;
147
148 bool isSubsetCollection() const final {
149 return m_isSubsetColl;
150 }
151
152 void setSubsetCollection(bool setSubset=true) final;
153
154 /// Returns the const object of given index
155 Tensor operator[](std::size_t index) const;
156 /// Returns the object of a given index
157 MutableTensor operator[](std::size_t index);
158 /// Returns the const object of given index
159 Tensor at(std::size_t index) const;
160 /// Returns the object of given index
161 MutableTensor at(std::size_t index);
162
163
164 /// Append object to the collection
165 void push_back(const MutableTensor& object);
166 /// Append an object to the (subset) collection
167 void push_back(const Tensor& object);
168
169 void prepareForWrite() const final;
170 void prepareAfterRead() final;
171 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
172
173 /// Get the collection buffers for this collection
174 podio::CollectionWriteBuffers getBuffers() final;
175
176 void setID(uint32_t ID) final {
177 m_collectionID = ID;
178 if (!m_isSubsetColl) {
179 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
180 [ID] (TensorObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; }
181 );
182 }
183 m_isValid = true;
184 }
185
186 uint32_t getID() const final {
187 return m_collectionID;
188 }
189
190 bool isValid() const final {
191 return m_isValid;
192 }
193
194 size_t getDatamodelRegistryIndex() const final;
195
196 // support for the iterator protocol
197 iterator begin() {
198 return iterator(0, &m_storage.entries);
199 }
201 return const_iterator(0, &m_storage.entries);
202 }
204 return begin();
205 }
207 return iterator(m_storage.entries.size(), &m_storage.entries);
208 }
210 return const_iterator(m_storage.entries.size(), &m_storage.entries);
211 }
213 return end();
214 }
215
216 std::vector<std::int32_t> elementType(const size_t nElem = 0) const;
217
218private:
219 // For setReferences, we need to give our own CollectionData access to our
220 // private entries. Otherwise we would need to expose a public member function
221 // that gives access to the Obj* which is definitely not what we want
223
224 bool m_isValid{false};
225 mutable bool m_isPrepared{false};
226 bool m_isSubsetColl{false};
227 uint32_t m_collectionID{0};
228 mutable std::unique_ptr<std::mutex> m_storageMtx{nullptr};
229 mutable TensorCollectionData m_storage{};
230};
231
232std::ostream& operator<<(std::ostream& o, const TensorCollection& v);
233
234template<typename... Args>
235MutableTensor TensorCollection::create(Args&&... args) {
236 if (m_isSubsetColl) {
237 throw std::logic_error("Cannot create new elements on a subset collection");
238 }
239 const int size = m_storage.entries.size();
240 auto obj = new TensorObj({size, m_collectionID}, {std::forward<Args>(args)...});
241 m_storage.entries.push_back(obj);
242
243 // Need to initialize the relation vectors manually for the {ObjectID, TensorData} constructor
244 obj->m_shape = new std::vector<std::int64_t>();
245 obj->m_floatData = new std::vector<float>();
246 obj->m_int64Data = new std::vector<std::int64_t>();
247 m_storage.createRelations(obj);
248 return MutableTensor(podio::utils::MaybeSharedPtr(obj));
249}
250
251#if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
252void to_json(nlohmann::json& j, const TensorCollection& collection);
253#endif
254
255} // namespace edm4eic
256
257
258#endif
Definition MutableTensor.h:35
Definition TensorCollectionData.h:30
TensorObjPointerContainer entries
Definition TensorCollectionData.h:35
Definition TensorCollection.h:91
TensorCollection & operator=(TensorCollection &&)=default
static constexpr auto typeName
Definition TensorCollection.h:110
~TensorCollection() override
Definition TensorCollection.cc:29
TensorCollection()
Definition TensorCollection.cc:23
bool isSubsetCollection() const final
Definition TensorCollection.h:148
static constexpr auto dataTypeName
Definition TensorCollection.h:112
void prepareAfterRead() final
Definition TensorCollection.cc:111
ptrdiff_t difference_type
Definition TensorCollection.h:96
const_iterator cbegin() const
Definition TensorCollection.h:203
uint32_t getID() const final
Definition TensorCollection.h:186
MutableTensor create()
Append a new object to the collection, and return this object.
Definition TensorCollection.cc:73
const std::string_view getTypeName() const final
fully qualified type name
Definition TensorCollection.h:140
const_iterator end() const
Definition TensorCollection.h:209
const std::string_view getValueTypeName() const final
fully qualified type name of elements - with namespace
Definition TensorCollection.h:142
void setID(uint32_t ID) final
Definition TensorCollection.h:176
void clear() final
Definition TensorCollection.cc:85
iterator end()
Definition TensorCollection.h:206
void push_back(const MutableTensor &object)
Append object to the collection.
Definition TensorCollection.cc:130
TensorCollection(TensorCollection &&)=default
std::size_t size() const final
number of elements in the collection
Definition TensorCollection.cc:50
const_iterator cend() const
Definition TensorCollection.h:212
TensorCollection & operator=(const TensorCollection &)=delete
podio::CollectionWriteBuffers getBuffers() final
Get the collection buffers for this collection.
Definition TensorCollection.cc:163
bool isValid() const final
Definition TensorCollection.h:190
static constexpr auto valueTypeName
Definition TensorCollection.h:111
podio::SchemaVersionT getSchemaVersion() const final
schema version
Definition TensorCollection.cc:182
bool setReferences(const podio::ICollectionProvider *collectionProvider) final
Definition TensorCollection.cc:126
TensorCollection(const TensorCollection &)=delete
void prepareForWrite() const final
Definition TensorCollection.cc:90
const std::string_view getDataTypeName() const final
fully qualified type name of stored POD elements - with namespace
Definition TensorCollection.h:144
size_t size_type
Definition TensorCollection.h:97
const_iterator begin() const
Definition TensorCollection.h:200
void setSubsetCollection(bool setSubset=true) final
Definition TensorCollection.cc:62
Tensor at(std::size_t index) const
Returns the const object of given index.
Definition TensorCollection.cc:38
void print(std::ostream &os=std::cout, bool flush=true) const final
Print this collection to the passed stream.
Definition TensorCollection.cc:361
bool empty() const final
Is the collection empty.
Definition TensorCollection.cc:58
std::size_t max_size() const final
maximal number of elements in the collection
Definition TensorCollection.cc:54
Definition TensorCollection.h:36
Tensor * operator->()
Definition TensorCollection.cc:299
bool operator!=(const TensorCollectionIterator &x) const
Definition TensorCollection.h:43
TensorCollectionIterator(const TensorCollectionIterator &)=delete
TensorCollectionIterator(size_t index, const TensorObjPointerContainer *collection)
Definition TensorCollection.h:38
TensorCollectionIterator & operator++()
Definition TensorCollection.cc:304
bool operator==(const TensorCollectionIterator &x) const
Definition TensorCollection.h:47
Tensor operator*()
Definition TensorCollection.cc:294
TensorCollectionIterator & operator=(const TensorCollectionIterator &)=delete
Definition Tensor.h:43
Definition TensorCollection.h:62
TensorMutableCollectionIterator & operator++()
Definition TensorCollection.cc:321
TensorMutableCollectionIterator & operator=(const TensorMutableCollectionIterator &)=delete
TensorMutableCollectionIterator(size_t index, const TensorObjPointerContainer *collection)
Definition TensorCollection.h:64
bool operator!=(const TensorMutableCollectionIterator &x) const
Definition TensorCollection.h:69
MutableTensor operator*()
Definition TensorCollection.cc:311
TensorMutableCollectionIterator(const TensorMutableCollectionIterator &)=delete
MutableTensor * operator->()
Definition TensorCollection.cc:316
bool operator==(const TensorMutableCollectionIterator &x) const
Definition TensorCollection.h:73
Definition TensorObj.h:19
std::vector< std::int64_t > * m_shape
Definition TensorObj.h:35
Definition CalorimeterHit.cc:17
std::deque< TensorObj * > TensorObjPointerContainer
Definition TensorCollectionData.h:22
std::ostream & operator<<(std::ostream &o, const CalorimeterHit &value)
Definition CalorimeterHit.cc:103
Definition CalorimeterHit.h:31