1#ifndef PODIO_SIO_UTILS_H
2#define PODIO_SIO_UTILS_H
9#include <sio/compression/zlib.h>
10#include <sio/definitions.h>
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);
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);
33 return std::make_pair(std::move(recBuffer), recInfo);
36 using StoreCollection = std::pair<const std::string&, const podio::CollectionBase*>;
39 inline std::shared_ptr<SIOCollectionIDTableBlock>
createCollIDBlock(
const std::vector<StoreCollection>& collections,
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());
50 ids.reserve(collections.size());
52 for (
const auto& [name, coll] : collections) {
53 names.emplace_back(name);
55 types.emplace_back(coll->getValueTypeName());
56 subsetColl.emplace_back(coll->isSubsetCollection());
59 return std::make_shared<SIOCollectionIDTableBlock>(std::move(names), std::move(ids), std::move(types),
60 std::move(subsetColl));
64 inline sio::block_list
createBlocks(
const std::vector<StoreCollection>& collections,
66 sio::block_list blocks;
67 blocks.reserve(collections.size() + 1);
69 auto paramBlock = std::make_shared<SIOEventMetaDataBlock>();
72 blocks.emplace_back(std::move(paramBlock));
74 for (
const auto& [name, col] : collections) {
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);
90 sio::zlib_compression compressor;
91 compressor.set_level(6);
92 auto comBuffer = sio::buffer{initBufferSize};
93 sio::api::compress_record(recInfo, buffer, comBuffer, compressor);
95 sio::api::write_record(stream, buffer.span(0, recInfo._header_length), comBuffer.span(), recInfo);
97 sio::api::write_record(stream, buffer.span(), recInfo);
100 return recInfo._file_start;
int collectionID(const std::string &name) const
return collection ID for given name
static SIOBlockFactory & instance()
std::shared_ptr< SIOBlock > createBlock(const podio::CollectionBase *col, const std::string &name) const
sio::block_list createBlocks(const std::vector< StoreCollection > &collections, const podio::GenericParameters ¶meters)
Create all blocks to store the passed collections and parameters into a record.
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.
std::pair< const std::string &, const podio::CollectionBase * > StoreCollection
std::shared_ptr< SIOCollectionIDTableBlock > createCollIDBlock(const std::vector< StoreCollection > &collections, const podio::CollectionIDTable &collIdTable)
Create the collection ID block from the passed collections.
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.