PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
SIOFrameWriter.cc
Go to the documentation of this file.
4#include "podio/Frame.h"
6#include "podio/SIOBlock.h"
7
8#include "sioUtils.h"
9
10#include <memory>
11#include <string>
12
13namespace podio {
14
15SIOFrameWriter::SIOFrameWriter(const std::string& filename) {
16 m_stream.open(filename, std::ios::binary);
17 if (!m_stream.is_open()) {
18 SIO_THROW(sio::error_code::not_open, "Couldn't open output stream '" + filename + "'");
19 }
20
21 auto& libLoader [[maybe_unused]] = SIOBlockLibraryLoader::instance();
22
23 sio::block_list blocks;
24 blocks.emplace_back(std::make_shared<SIOVersionBlock>(podio::version::build_version));
25 // write the version uncompressed
26 sio_utils::writeRecord(blocks, "podio_header_info", m_stream, sizeof(podio::version::Version), false);
27}
28
29void SIOFrameWriter::writeFrame(const podio::Frame& frame, const std::string& category) {
30 writeFrame(frame, category, frame.getAvailableCollections());
31}
32
33void SIOFrameWriter::writeFrame(const podio::Frame& frame, const std::string& category,
34 const std::vector<std::string>& collsToWrite) {
35 std::vector<sio_utils::StoreCollection> collections;
36 collections.reserve(collsToWrite.size());
37 for (const auto& name : collsToWrite) {
38 collections.emplace_back(name, frame.getCollectionForWrite(name));
39 m_datamodelCollector.registerDatamodelDefinition(collections.back().second, name);
40 }
41
42 // Write necessary metadata and the actual data into two different records.
43 // Otherwise we cannot easily unpack the data record, because necessary
44 // information is contained within the record.
45 sio::block_list tableBlocks;
46 tableBlocks.emplace_back(sio_utils::createCollIDBlock(collections, frame.getCollectionIDTableForWrite()));
47 m_tocRecord.addRecord(category, sio_utils::writeRecord(tableBlocks, category + "_HEADER", m_stream));
48
49 const auto blocks = sio_utils::createBlocks(collections, frame.getParameters());
50 sio_utils::writeRecord(blocks, category, m_stream);
51}
52
54 auto edmDefMap = std::make_shared<podio::SIOMapBlock<std::string, std::string>>(
55 m_datamodelCollector.getDatamodelDefinitionsToWrite());
56
57 sio::block_list blocks;
58 blocks.push_back(edmDefMap);
59 m_tocRecord.addRecord(sio_helpers::SIOEDMDefinitionName, sio_utils::writeRecord(blocks, "EDMDefinitions", m_stream));
60
61 blocks.clear();
62 blocks.emplace_back(std::make_shared<SIOFileTOCRecordBlock>(&m_tocRecord));
63
64 auto tocStartPos = sio_utils::writeRecord(blocks, sio_helpers::SIOTocRecordName, m_stream);
65
66 // Now that we know the position of the TOC Record, put this information
67 // into a final marker that can be identified and interpreted when reading
68 // again
69 uint64_t finalWords = (((uint64_t)sio_helpers::SIOTocMarker) << 32) | ((uint64_t)tocStartPos & 0xffffffff);
70 m_stream.write(reinterpret_cast<char*>(&finalWords), sizeof(finalWords));
71
72 m_stream.close();
73}
74
75} // namespace podio
std::vector< std::tuple< std::string, std::string > > getDatamodelDefinitionsToWrite() const
Get all the names and JSON definitions that need to be written.
void registerDatamodelDefinition(const podio::CollectionBase *coll, const std::string &name)
podio::CollectionIDTable getCollectionIDTableForWrite() const
Definition: Frame.h:278
std::vector< std::string > getAvailableCollections() const
Definition: Frame.h:252
const podio::GenericParameters & getParameters() const
Definition: Frame.h:238
const podio::CollectionBase * getCollectionForWrite(const std::string &name) const
Definition: Frame.h:269
static SIOBlockLibraryLoader & instance()
Definition: SIOBlock.h:263
void addRecord(const std::string &name, PositionType startPos)
Definition: SIOBlock.cc:184
void writeFrame(const podio::Frame &frame, const std::string &category)
SIOFrameWriter(const std::string &filename)
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::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