Skip to content

Commit

Permalink
Make SIOReader follow the same interface as ROOTReader
Browse files Browse the repository at this point in the history
Now no longer owns the blocks it reads, but instead creates them for
every event and then passes on the ownership to the EventStore
  • Loading branch information
tmadlener committed Sep 16, 2020
1 parent ce4fc3b commit 5c0b5bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
5 changes: 4 additions & 1 deletion include/podio/SIOReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace podio {
CollectionBase* readCollection(const std::string& name) override final;

/// read event meta data for current event
GenericParameters* readEventMetaData() override final { return m_eventMetaData->metadata; }
GenericParameters* readEventMetaData() override final;

/// read the collection meta data
std::map<int,GenericParameters>* readCollectionMetaData() override final;
Expand All @@ -64,11 +64,14 @@ namespace podio {
private:
void readCollectionIDTable();
void readMetaDataRecord(std::shared_ptr<SIONumberedMetaDataBlock> mdBlock);
void createBlocks();

typedef std::pair<CollectionBase*, std::string> Input;
std::vector<Input> m_inputs;
CollectionIDTable* m_table{nullptr}; // will be owned by the EventStore
int m_eventNumber{0};
int m_lastEventRead{-1};
std::vector<std::string> m_typeNames;

std::shared_ptr<SIOEventMetaDataBlock> m_eventMetaData{};
std::shared_ptr<SIONumberedMetaDataBlock> m_runMetaData{};
Expand Down
51 changes: 34 additions & 17 deletions src/SIOReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ namespace podio {
m_runMetaData(std::make_shared<SIONumberedMetaDataBlock>("RunMetaData")),
m_collectionMetaData(std::make_shared<SIONumberedMetaDataBlock>("CollectionMetaData"))
{
// make sure that the first block is EventMetaData as it is also the first
// during wrting
m_blocks.push_back(m_eventMetaData);
auto& libLoader = SIOBlockLibraryLoader::instance();
}

CollectionBase* SIOReader::readCollection(const std::string& name) {
if (m_lastEventRead != m_eventNumber) {
readEvent();
}

auto p = std::find_if(begin(m_inputs), end(m_inputs),
[&name](const SIOReader::Input& t){ return t.second == name;});

Expand Down Expand Up @@ -56,6 +57,12 @@ namespace podio {
return m_runMetaData->data;
}

podio::GenericParameters* SIOReader::readEventMetaData() {
if (m_lastEventRead != m_eventNumber) {
readEvent();
}
return m_eventMetaData->metadata;
}

void SIOReader::openFile(const std::string& filename){
m_stream.open( filename , std::ios::binary ) ;
Expand All @@ -68,7 +75,9 @@ namespace podio {
}

void SIOReader::readEvent(){
m_eventMetaData->metadata = new GenericParameters(); // will be managed by EventStore (?)
// recreate the blocks, since the contents are owned and managed by the
// EventStore
createBlocks();

// skip possible intermediate records that are not event data
sio::api::go_to_record(m_stream, "event_record");
Expand All @@ -80,12 +89,13 @@ namespace podio {
m_unc_buffer.resize( rec_info._uncompressed_length ) ;
sio::zlib_compression compressor ;
compressor.uncompress( m_rec_buffer.span(), m_unc_buffer ) ;

sio::api::read_blocks( m_unc_buffer.span(), m_blocks ) ;

for (auto& [collection, name] : m_inputs) {
collection->setID(m_table->collectionID(name));
}

m_lastEventRead = m_eventNumber;
}

bool SIOReader::isValid() const {
Expand All @@ -98,10 +108,24 @@ namespace podio {
// TODO: who deletes the buffers?
}

void SIOReader::endOfEvent() {
++m_eventNumber;
// m_inputs.clear();
}
void SIOReader::endOfEvent() {
++m_eventNumber;
m_blocks.clear();
m_inputs.clear();
}

void SIOReader::createBlocks() {
// make sure that the first block is EventMetaData as it is also the first
// during wrting
m_eventMetaData->metadata = new GenericParameters(); // will be managed by EventStore (?)
m_blocks.push_back(m_eventMetaData);

for (size_t i = 0; i < m_typeNames.size(); ++i) {
auto blk = podio::SIOBlockFactory::instance().createBlock(m_typeNames[i], m_table->names()[i]);
m_blocks.push_back(blk);
m_inputs.emplace_back(blk->getCollection(), m_table->names()[i]);
}
}

void SIOReader::readCollectionIDTable() {
sio::record_info rec_info;
Expand All @@ -118,14 +142,7 @@ namespace podio {

auto* idTableBlock = static_cast<SIOCollectionIDTableBlock*>(blocks[0].get());
m_table = idTableBlock->getTable();
const auto& typeNames = idTableBlock->getTypeNames();

for (size_t i = 0; i < typeNames.size(); ++i) {
auto blk = podio::SIOBlockFactory::instance().createBlock(typeNames[i], m_table->names()[i]);
m_blocks.push_back(blk);
m_inputs.emplace_back(std::make_pair(blk->getCollection(), m_table->names()[i]));
}

m_typeNames = idTableBlock->getTypeNames();
}

void SIOReader::readMetaDataRecord(std::shared_ptr<SIONumberedMetaDataBlock> mdBlock) {
Expand Down
9 changes: 1 addition & 8 deletions tests/read_sio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ int main(){
if(i%1000==0)
std::cout<<"reading event "<<i<<std::endl;

reader.readEvent() ;

processEvent(store, true, i);

// only clear collections for re-use
store.clearCollections();
store.clearCaches(); // Would be done in EventStore::clear
// store.clear(); // However, this would delete the collections as well ...
store.clear();
reader.endOfEvent();

}
}
catch( sio::exception &e ) {
Expand Down

0 comments on commit 5c0b5bd

Please sign in to comment.