PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
sioUtils.h
Go to the documentation of this file.
1#ifndef PODIO_SIO_UTILS_H // NOLINT(llvm-header-guard): internal headers confuse clang-tidy
2#define PODIO_SIO_UTILS_H // NOLINT(llvm-header-guard): internal headers confuse clang-tidy
3
6#include "podio/SIOBlock.h"
7
8#include <sio/api.h>
9#include <sio/compression/zlib.h>
10#include <sio/definitions.h>
11
12#include <string_view>
13#include <utility>
14
15namespace podio {
16namespace sio_utils {
17 /// Read the record into a buffer and potentially uncompress it
18 inline std::pair<sio::buffer, sio::record_info> readRecord(sio::ifstream& stream, bool decompress = true,
19 std::size_t initBufferSize = sio::mbyte) {
20 sio::record_info recInfo;
21 sio::buffer infoBuffer{sio::max_record_info_len};
22 sio::buffer recBuffer{initBufferSize};
23 sio::api::read_record_info(stream, recInfo, infoBuffer);
24 sio::api::read_record_data(stream, recInfo, recBuffer);
25
26 if (decompress) {
27 sio::buffer uncBuffer{recInfo._uncompressed_length};
28 sio::zlib_compression compressor;
29 compressor.uncompress(recBuffer.span(), uncBuffer);
30 return std::make_pair(std::move(uncBuffer), recInfo);
31 }
32
33 return std::make_pair(std::move(recBuffer), recInfo);
34 }
35
36 using StoreCollection = std::pair<const std::string&, const podio::CollectionBase*>;
37
38 /// Create the collection ID block from the passed collections
39 inline std::shared_ptr<SIOCollectionIDTableBlock> createCollIDBlock(const std::vector<StoreCollection>& collections,
40 const podio::CollectionIDTable& collIdTable) {
41 // Need to make sure that the type names and subset collection bits are in
42 // the same order here!
43 std::vector<std::string> types;
44 types.reserve(collections.size());
45 std::vector<short> subsetColl;
46 subsetColl.reserve(collections.size());
47 std::vector<std::string> names;
48 names.reserve(collections.size());
49 std::vector<int> ids;
50 ids.reserve(collections.size());
51
52 for (const auto& [name, coll] : collections) {
53 names.emplace_back(name);
54 ids.emplace_back(collIdTable.collectionID(name));
55 types.emplace_back(coll->getValueTypeName());
56 subsetColl.emplace_back(coll->isSubsetCollection());
57 }
58
59 return std::make_shared<SIOCollectionIDTableBlock>(std::move(names), std::move(ids), std::move(types),
60 std::move(subsetColl));
61 }
62
63 /// Create all blocks to store the passed collections and parameters into a record
64 inline sio::block_list createBlocks(const std::vector<StoreCollection>& collections,
65 const podio::GenericParameters& parameters) {
66 sio::block_list blocks;
67 blocks.reserve(collections.size() + 1); // parameters + collections
68
69 auto paramBlock = std::make_shared<SIOEventMetaDataBlock>();
70 // TODO: get rid of const_cast
71 paramBlock->metadata = const_cast<podio::GenericParameters*>(&parameters);
72 blocks.emplace_back(std::move(paramBlock));
73
74 for (const auto& [name, col] : collections) {
75 blocks.emplace_back(podio::SIOBlockFactory::instance().createBlock(col, name));
76 }
77
78 return blocks;
79 }
80
81 /// Write the passed record and return where it starts in the file
82 inline sio::ifstream::pos_type writeRecord(const sio::block_list& blocks, const std::string& recordName,
83 sio::ofstream& stream, std::size_t initBufferSize = sio::mbyte,
84 bool compress = true) {
85 auto buffer = sio::buffer{initBufferSize};
86 auto recInfo = sio::api::write_record(recordName, buffer, blocks, 0);
87
88 if (compress) {
89 // use zlib to compress the record into another buffer
90 sio::zlib_compression compressor;
91 compressor.set_level(6); // Z_DEFAULT_COMPRESSION==6
92 auto comBuffer = sio::buffer{initBufferSize};
93 sio::api::compress_record(recInfo, buffer, comBuffer, compressor);
94
95 sio::api::write_record(stream, buffer.span(0, recInfo._header_length), comBuffer.span(), recInfo);
96 } else {
97 sio::api::write_record(stream, buffer.span(), recInfo);
98 }
99
100 return recInfo._file_start;
101 }
102
103} // namespace sio_utils
104} // namespace podio
105
106#endif
int collectionID(const std::string &name) const
return collection ID for given name
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
sio::block_list createBlocks(const std::vector< StoreCollection > &collections, const podio::GenericParameters &parameters)
Create all blocks to store the passed collections and parameters into a record.
Definition: sioUtils.h:64
sio::ifstream::pos_type writeRecord(const sio::block_list &blocks, const std::string &recordName, sio::ofstream &stream, std::size_t initBufferSize=sio::mbyte, bool compress=true)
Write the passed record and return where it starts in the file.
Definition: sioUtils.h:82
std::pair< const std::string &, const podio::CollectionBase * > StoreCollection
Definition: sioUtils.h:36
std::shared_ptr< SIOCollectionIDTableBlock > createCollIDBlock(const std::vector< StoreCollection > &collections, const podio::CollectionIDTable &collIdTable)
Create the collection ID block from the passed collections.
Definition: sioUtils.h:39
std::pair< sio::buffer, sio::record_info > readRecord(sio::ifstream &stream, bool decompress=true, std::size_t initBufferSize=sio::mbyte)
Read the record into a buffer and potentially uncompress it.
Definition: sioUtils.h:18