PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
SIOBlock.h
Go to the documentation of this file.
1#ifndef PODIO_SIOBLOCK_H
2#define PODIO_SIOBLOCK_H
3
6#include <podio/EventStore.h>
8#include <podio/podioVersion.h>
10
11#include <sio/block.h>
12#include <sio/io_device.h>
13#include <sio/version.h>
14
15#include <map>
16#include <memory>
17#include <string>
18#include <string_view>
19#include <tuple>
20#include <vector>
21
22namespace podio {
23
24template <typename devT, typename PODData>
25void handlePODDataSIO(devT& device, PODData* data, size_t size) {
26 unsigned count = size * sizeof(PODData);
27 char* dataPtr = reinterpret_cast<char*>(data);
28 device.data(dataPtr, count);
29}
30
31/// Write anything that iterates like an std::map
32template <typename MapLikeT>
33void writeMapLike(sio::write_device& device, const MapLikeT& map) {
34 device.data((int)map.size());
35 for (const auto& [key, value] : map) {
36 device.data(key);
37 device.data(value);
38 }
39}
40
41/// Read anything that iterates like an std::map
42template <typename MapLikeT>
43void readMapLike(sio::read_device& device, MapLikeT& map) {
44 int size;
45 device.data(size);
46 while (size--) {
48 device.data(key);
50 device.data(value);
51 if constexpr (podio::detail::isVector<MapLikeT>) {
52 map.emplace_back(std::move(key), std::move(value));
53 } else {
54 map.emplace(std::move(key), std::move(value));
55 }
56 }
57}
58
59/// Base class for sio::block handlers used with PODIO
60class SIOBlock : public sio::block {
61
62public:
63 SIOBlock(const std::string& nam, sio::version_type vers) : sio::block(nam, vers) {
64 }
65 SIOBlock() = delete;
66 SIOBlock(const SIOBlock&) = delete;
67 SIOBlock& operator=(const SIOBlock&) = delete;
68
71 }
72
74 return m_buffers;
75 }
76
77 std::string name() {
78 return sio::block::name();
79 }
80
83 m_buffers = col->getBuffers();
84 }
85
86 virtual SIOBlock* create(const std::string& name) const = 0;
87
88 // create a new collection for this block
89 virtual void createBuffers(const bool subsetCollection = false) = 0;
90
91protected:
92 bool m_subsetColl{false};
94};
95
96/**
97 * A dedicated block for handling the I/O of the CollectionIDTable
98 */
99class SIOCollectionIDTableBlock : public sio::block {
100public:
101 SIOCollectionIDTableBlock() : sio::block("CollectionIDs", sio::version::encode_version(0, 4)) {
102 }
103
105
106 SIOCollectionIDTableBlock(std::vector<std::string>&& names, std::vector<int>&& ids, std::vector<std::string>&& types,
107 std::vector<short>&& isSubsetColl) :
108 sio::block("CollectionIDs", sio::version::encode_version(0, 3)),
109 _names(std::move(names)),
110 _ids(std::move(ids)),
111 _types(std::move(types)),
112 _isSubsetColl(std::move(isSubsetColl)) {
113 }
114
117
118 void read(sio::read_device& device, sio::version_type version) override;
119 void write(sio::write_device& device) override;
120
122 return new podio::CollectionIDTable(std::move(_ids), std::move(_names));
123 }
124 const std::vector<std::string>& getTypeNames() const {
125 return _types;
126 }
127 const std::vector<short>& getSubsetCollectionBits() const {
128 return _isSubsetColl;
129 }
130
131private:
132 std::vector<std::string> _names{};
133 std::vector<int> _ids{};
134 std::vector<std::string> _types{};
135 std::vector<short> _isSubsetColl{};
136};
137
138struct SIOVersionBlock : public sio::block {
139 SIOVersionBlock() : sio::block("podio_version", sio::version::encode_version(1, 0)) {
140 }
141
143 sio::block("podio_version", sio::version::encode_version(1, 0)), version(v) {
144 }
145
146 void write(sio::write_device& device) override {
147 device.data(version);
148 }
149
150 void read(sio::read_device& device, sio::version_type) override {
151 device.data(version);
152 }
153
155};
156
157/**
158 * A block for handling the EventMeta data
159 */
160class SIOEventMetaDataBlock : public sio::block {
161public:
162 SIOEventMetaDataBlock() : sio::block("EventMetaData", sio::version::encode_version(0, 2)) {
163 }
164
167
168 void read(sio::read_device& device, sio::version_type version) override;
169 void write(sio::write_device& device) override;
170
172};
173
174/**
175 * A block to serialize anything that behaves similar in iterating as a
176 * map<KeyT, ValueT>, e.g. vector<tuple<KeyT, ValueT>>, which is what is used
177 * internally to represent the data to be written.
178 */
179template <typename KeyT, typename ValueT>
180struct SIOMapBlock : public sio::block {
181 SIOMapBlock() : sio::block("SIOMapBlock", sio::version::encode_version(0, 1)) {
182 }
183 SIOMapBlock(std::vector<std::tuple<KeyT, ValueT>>&& data) :
184 sio::block("SIOMapBlock", sio::version::encode_version(0, 1)), mapData(std::move(data)) {
185 }
186
187 SIOMapBlock(const SIOMapBlock&) = delete;
189
190 void read(sio::read_device& device, sio::version_type) override {
191 readMapLike(device, mapData);
192 }
193 void write(sio::write_device& device) override {
194 writeMapLike(device, mapData);
195 }
196
197 std::vector<std::tuple<KeyT, ValueT>> mapData{};
198};
199
200/**
201 * A block for handling the run and collection meta data
202 */
203class SIONumberedMetaDataBlock : public sio::block {
204public:
205 SIONumberedMetaDataBlock(const std::string& name) : sio::block(name, sio::version::encode_version(0, 2)) {
206 }
207
210
211 void read(sio::read_device& device, sio::version_type version) override;
212 void write(sio::write_device& device) override;
213
214 std::map<int, GenericParameters>* data{nullptr};
215};
216
217/// factory for creating sio::blocks for a given type of EDM-collection
219private:
220 SIOBlockFactory() = default;
221
222 typedef std::map<std::string, SIOBlock*> BlockMap;
223 BlockMap _map{};
224
225public:
226 void registerBlockForCollection(const std::string& type, SIOBlock* b) {
227 _map[type] = b;
228 }
229
230 std::shared_ptr<SIOBlock> createBlock(const podio::CollectionBase* col, const std::string& name) const;
231
232 // return a block with a new collection (used for reading )
233 std::shared_ptr<SIOBlock> createBlock(const std::string& typeStr, const std::string& name,
234 const bool isRefColl = false) const;
235
237 static SIOBlockFactory me;
238 return me;
239 }
240};
241
243private:
245
246 /// Status code for loading shared SIOBlocks libraries
247 enum class LoadStatus : short { Success = 0, AlreadyLoaded = 1, Error = 2 };
248
249 /**
250 * Load a library with the given name via dlopen
251 */
252 LoadStatus loadLib(const std::string& libname);
253
254 /**
255 * Get all files that are found on LD_LIBRARY_PATH and that have "SioBlocks"
256 * in their name together with the directory they are in
257 */
258 static std::vector<std::tuple<std::string, std::string>> getLibNames();
259
260 std::map<std::string, void*> _loadedLibs{};
261
262public:
265 return instance;
266 }
267};
268
269namespace sio_helpers {
270 /// marker for showing that a TOC has been stored in the file
271 static constexpr uint32_t SIOTocMarker = 0xc001fea7;
272 /// the number of bits necessary to store the SIOTocMarker and the actual
273 /// position of the start of the SIOFileTOCRecord
274 static constexpr int SIOTocInfoSize = sizeof(uint64_t); // i.e. usually 8
275 /// The name of the TOCRecord
276 static constexpr const char* SIOTocRecordName = "podio_SIO_TOC_Record";
277
278 /// The name of the record containing the EDM definitions in json format
279 static constexpr const char* SIOEDMDefinitionName = "podio_SIO_EDMDefinitions";
280
281 // should hopefully be enough for all practical purposes
282 using position_type = uint32_t;
283} // namespace sio_helpers
284
286public:
288 void addRecord(const std::string& name, PositionType startPos);
289
290 size_t getNRecords(const std::string& name) const;
291
292 /** Get the position of the iEntry-th record with the given name. If no entry
293 * with the given name is recorded, return 0. Note there is no internal check
294 * on whether the given name actually has iEntry records. Use getNRecords to
295 * check for that if necessary.
296 */
297 PositionType getPosition(const std::string& name, unsigned iEntry = 0) const;
298
299 /** Get all the record names that are stored in this TOC record
300 */
301 std::vector<std::string_view> getRecordNames() const;
302
303private:
305
306 using RecordListType = std::pair<std::string, std::vector<PositionType>>;
307 using MapType = std::vector<RecordListType>;
308
309 MapType m_recordMap{};
310};
311
312struct SIOFileTOCRecordBlock : public sio::block {
313 SIOFileTOCRecordBlock() : sio::block(sio_helpers::SIOTocRecordName, sio::version::encode_version(0, 1)) {
314 }
315
317 sio::block(sio_helpers::SIOTocRecordName, sio::version::encode_version(0, 1)), record(r) {
318 }
319
322
323 void read(sio::read_device& device, sio::version_type version) override;
324 void write(sio::write_device& device) override;
325
327};
328
329} // namespace podio
330#endif
virtual bool isSubsetCollection() const =0
check if this collection is a subset collection
virtual podio::CollectionWriteBuffers getBuffers()=0
Get the collection buffers for this collection.
factory for creating sio::blocks for a given type of EDM-collection
Definition: SIOBlock.h:218
void registerBlockForCollection(const std::string &type, SIOBlock *b)
Definition: SIOBlock.h:226
static SIOBlockFactory & instance()
Definition: SIOBlock.h:236
std::shared_ptr< SIOBlock > createBlock(const podio::CollectionBase *col, const std::string &name) const
Definition: SIOBlock.cc:110
static SIOBlockLibraryLoader & instance()
Definition: SIOBlock.h:263
Base class for sio::block handlers used with PODIO.
Definition: SIOBlock.h:60
podio::CollectionBase * getCollection()
Definition: SIOBlock.h:69
std::string name()
Definition: SIOBlock.h:77
virtual SIOBlock * create(const std::string &name) const =0
virtual void createBuffers(const bool subsetCollection=false)=0
SIOBlock()=delete
podio::CollectionReadBuffers getBuffers() const
Definition: SIOBlock.h:73
SIOBlock(const std::string &nam, sio::version_type vers)
Definition: SIOBlock.h:63
bool m_subsetColl
Definition: SIOBlock.h:92
SIOBlock(const SIOBlock &)=delete
podio::CollectionReadBuffers m_buffers
Definition: SIOBlock.h:93
SIOBlock & operator=(const SIOBlock &)=delete
void setCollection(podio::CollectionBase *col)
Definition: SIOBlock.h:81
SIOCollectionIDTableBlock(const SIOCollectionIDTableBlock &)=delete
SIOCollectionIDTableBlock & operator=(const SIOCollectionIDTableBlock &)=delete
const std::vector< std::string > & getTypeNames() const
Definition: SIOBlock.h:124
void write(sio::write_device &device) override
Definition: SIOBlock.cc:44
SIOCollectionIDTableBlock(std::vector< std::string > &&names, std::vector< int > &&ids, std::vector< std::string > &&types, std::vector< short > &&isSubsetColl)
Definition: SIOBlock.h:106
const std::vector< short > & getSubsetCollectionBits() const
Definition: SIOBlock.h:127
podio::CollectionIDTable * getTable()
Definition: SIOBlock.h:121
void read(sio::read_device &device, sio::version_type version) override
Definition: SIOBlock.cc:35
SIOEventMetaDataBlock(const SIOEventMetaDataBlock &)=delete
SIOEventMetaDataBlock & operator=(const SIOEventMetaDataBlock &)=delete
void write(sio::write_device &device) override
Definition: SIOBlock.cc:72
void read(sio::read_device &device, sio::version_type version) override
Definition: SIOBlock.cc:68
podio::GenericParameters * metadata
Definition: SIOBlock.h:171
PositionType getPosition(const std::string &name, unsigned iEntry=0) const
Definition: SIOBlock.cc:204
size_t getNRecords(const std::string &name) const
Definition: SIOBlock.cc:195
sio_helpers::position_type PositionType
Definition: SIOBlock.h:287
std::vector< std::string_view > getRecordNames() const
Definition: SIOBlock.cc:216
void addRecord(const std::string &name, PositionType startPos)
Definition: SIOBlock.cc:184
std::map< int, GenericParameters > * data
Definition: SIOBlock.h:214
SIONumberedMetaDataBlock(const SIONumberedMetaDataBlock &)=delete
void write(sio::write_device &device) override
Definition: SIOBlock.cc:89
SIONumberedMetaDataBlock & operator=(const SIONumberedMetaDataBlock &)=delete
void read(sio::read_device &device, sio::version_type version) override
Definition: SIOBlock.cc:76
SIONumberedMetaDataBlock(const std::string &name)
Definition: SIOBlock.h:205
typename MapLikeTypeHelper< T >::mapped_type GetMappedType
Definition: TypeHelpers.h:159
typename MapLikeTypeHelper< T >::key_type GetKeyType
Definition: TypeHelpers.h:156
uint32_t position_type
Definition: SIOBlock.h:282
void readMapLike(sio::read_device &device, MapLikeT &map)
Read anything that iterates like an std::map.
Definition: SIOBlock.h:43
void handlePODDataSIO(devT &device, PODData *data, size_t size)
Definition: SIOBlock.h:25
void writeMapLike(sio::write_device &device, const MapLikeT &map)
Write anything that iterates like an std::map.
Definition: SIOBlock.h:33
SIOFileTOCRecordBlock(const SIOFileTOCRecordBlock &)=delete
void read(sio::read_device &device, sio::version_type version) override
Definition: SIOBlock.cc:226
void write(sio::write_device &device) override
Definition: SIOBlock.cc:239
SIOFileTOCRecordBlock(SIOFileTOCRecord *r)
Definition: SIOBlock.h:316
SIOFileTOCRecord * record
Definition: SIOBlock.h:326
SIOFileTOCRecordBlock & operator=(const SIOFileTOCRecordBlock &)=delete
void write(sio::write_device &device) override
Definition: SIOBlock.h:193
SIOMapBlock & operator=(const SIOMapBlock &)=delete
SIOMapBlock(const SIOMapBlock &)=delete
void read(sio::read_device &device, sio::version_type) override
Definition: SIOBlock.h:190
SIOMapBlock(std::vector< std::tuple< KeyT, ValueT > > &&data)
Definition: SIOBlock.h:183
std::vector< std::tuple< KeyT, ValueT > > mapData
Definition: SIOBlock.h:197
SIOVersionBlock(podio::version::Version v)
Definition: SIOBlock.h:142
void read(sio::read_device &device, sio::version_type) override
Definition: SIOBlock.h:150
void write(sio::write_device &device) override
Definition: SIOBlock.h:146
podio::version::Version version
Definition: SIOBlock.h:154