PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
SIOFrameData.cc
Go to the documentation of this file.
2#include "podio/SIOBlock.h"
3
4#include <sio/compression/zlib.h>
5
6#include <algorithm>
7#include <iterator>
8
9namespace podio {
10std::optional<podio::CollectionReadBuffers> SIOFrameData::getCollectionBuffers(const std::string& name) {
11 unpackBuffers();
12
13 if (m_idTable.present(name)) {
14 // The collections that we read are not necessarily in the same order as
15 // they are in the collection id table. Hence, we cannot simply use the
16 // collection ID to index into the blocks
17 const auto& names = m_idTable.names();
18 const auto nameIt = std::find(std::begin(names), std::end(names), name);
19 // collection indices start at 1!
20 const auto index = std::distance(std::begin(names), nameIt) + 1;
21
22 m_availableBlocks[index] = 1;
23 return {dynamic_cast<podio::SIOBlock*>(m_blocks[index].get())->getBuffers()};
24 }
25
26 return std::nullopt;
27}
28
29std::unique_ptr<podio::GenericParameters> SIOFrameData::getParameters() {
30 unpackBuffers();
31 m_availableBlocks[0] = 0;
32 return std::make_unique<podio::GenericParameters>(std::move(m_parameters));
33}
34
35std::vector<std::string> SIOFrameData::getAvailableCollections() {
36 unpackBuffers();
37 std::vector<std::string> collections;
38 for (size_t i = 0; i < m_blocks.size(); ++i) {
39 if (m_availableBlocks[i]) {
40 collections.push_back(m_idTable.name(i));
41 }
42 }
43
44 return collections;
45}
46
47void SIOFrameData::unpackBuffers() {
48 // Only do the unpacking once. Use the block as proxy for deciding whether
49 // we have already unpacked things, since that is the main thing we do in
50 // here: create blocks and read the data into them
51 if (!m_blocks.empty()) {
52 return;
53 }
54
55 if (m_idTable.empty()) {
56 readIdTable();
57 }
58
59 createBlocks();
60
61 sio::zlib_compression compressor;
62 sio::buffer uncBuffer{m_dataSize};
63 compressor.uncompress(m_recBuffer.span(), uncBuffer);
64 sio::api::read_blocks(uncBuffer.span(), m_blocks);
65}
66
67void SIOFrameData::createBlocks() {
68 m_blocks.reserve(m_typeNames.size() + 1);
69 // First block during writing is parameters / metadata, then collections
70 auto parameters = std::make_shared<podio::SIOEventMetaDataBlock>();
71 parameters->metadata = &m_parameters;
72 m_blocks.push_back(parameters);
73
74 for (size_t i = 0; i < m_typeNames.size(); ++i) {
75 const bool subsetColl = !m_subsetCollectionBits.empty() && m_subsetCollectionBits[i];
76 auto blk = podio::SIOBlockFactory::instance().createBlock(m_typeNames[i], m_idTable.names()[i], subsetColl);
77 m_blocks.push_back(blk);
78 }
79
80 m_availableBlocks.resize(m_blocks.size(), 1);
81}
82
83void SIOFrameData::readIdTable() {
84 sio::buffer uncBuffer{m_tableSize};
85 sio::zlib_compression compressor;
86 compressor.uncompress(m_tableBuffer.span(), uncBuffer);
87
88 sio::block_list blocks;
89 blocks.emplace_back(std::make_shared<SIOCollectionIDTableBlock>());
90 sio::api::read_blocks(uncBuffer.span(), blocks);
91
92 auto* idTableBlock = static_cast<SIOCollectionIDTableBlock*>(blocks[0].get());
93 m_idTable = std::move(*idTableBlock->getTable());
94 m_typeNames = idTableBlock->getTypeNames();
95 m_subsetCollectionBits = idTableBlock->getSubsetCollectionBits();
96}
97
98} // namespace podio
bool present(const std::string &name) const
Check if collection name is known.
bool empty() const
Does this table hold any information?
const std::vector< std::string > & names() const
return registered names
const std::string name(int collectionID) const
return name for given collection ID
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
Base class for sio::block handlers used with PODIO.
Definition: SIOBlock.h:60
std::unique_ptr< podio::GenericParameters > getParameters()
Definition: SIOFrameData.cc:29
std::vector< std::string > getAvailableCollections()
Definition: SIOFrameData.cc:35
std::optional< podio::CollectionReadBuffers > getCollectionBuffers(const std::string &name)
Definition: SIOFrameData.cc:10