Skip to content

Commit

Permalink
Make it possible read "old" podio files (#232)
Browse files Browse the repository at this point in the history
* Make it possible read "old" podio files

* Make it possible to read old SIO files
  • Loading branch information
tmadlener authored Oct 12, 2021
1 parent caa125d commit 181ec3d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
4 changes: 2 additions & 2 deletions include/podio/SIOBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ namespace podio {
class SIOCollectionIDTableBlock : public sio::block {
public:
SIOCollectionIDTableBlock() :
sio::block("CollectionIDs", sio::version::encode_version(0, 1)) {}
sio::block("CollectionIDs", sio::version::encode_version(0, 2)) {}

SIOCollectionIDTableBlock(podio::EventStore* store) :
sio::block("CollectionIDs", sio::version::encode_version(0, 1)),
sio::block("CollectionIDs", sio::version::encode_version(0, 2)),
_store(store), _table(store->getCollectionIDTable()) {}

SIOCollectionIDTableBlock(const SIOCollectionIDTableBlock&) = delete;
Expand Down
23 changes: 16 additions & 7 deletions src/ROOTReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,21 @@ namespace podio {
m_table = new CollectionIDTable();
metadatatree->SetBranchAddress("CollectionIDs", &m_table);

auto collectionInfo = new std::vector<std::tuple<int, std::string, bool>>;
metadatatree->SetBranchAddress("CollectionTypeInfo", &collectionInfo);
metadatatree->GetEntry(0);

createCollectionBranches(*collectionInfo);
delete collectionInfo;
// Check if the CollectionTypeInfo branch is there and assume that the file
// has been written with with podio pre #197 (<0.13.1) if that is not the case
if (auto* collInfoBranch = root_utils::getBranch(metadatatree, "CollectionTypeInfo")) {
auto collectionInfo = new std::vector<root_utils::CollectionInfoT>;
collInfoBranch->SetAddress(&collectionInfo);
metadatatree->GetEntry(0);
createCollectionBranches(*collectionInfo);
delete collectionInfo;
} else {
std::cout << "PODIO: Reconstructing CollectionTypeInfo branch from other sources in file: \'"
<< m_chain->GetFile()->GetName() << "\'" << std::endl;
metadatatree->GetEntry(0);
const auto collectionInfo = root_utils::reconstructCollectionInfo(m_chain, *m_table);
createCollectionBranches(collectionInfo);
}
}

void ROOTReader::closeFile(){
Expand Down Expand Up @@ -196,7 +205,7 @@ namespace podio {
m_inputs.clear();
}

void ROOTReader::createCollectionBranches(const std::vector<std::tuple<int, std::string, bool>>& collInfo) {
void ROOTReader::createCollectionBranches(const std::vector<root_utils::CollectionInfoT>& collInfo) {
size_t collectionIndex{0};

for (const auto& [collID, collType, isSubsetColl] : collInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/ROOTWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void ROOTWriter::setBranches(const std::vector<StoreCollection>& collections) {
m_metadatatree->Branch("CollectionIDs", collIDTable);

// collectionID, collection type, subset collection
std::vector<std::tuple<int, std::string, bool>> collectionInfo;
std::vector<root_utils::CollectionInfoT> collectionInfo;
collectionInfo.reserve(m_collectionsToWrite.size());
for (const auto& name : m_collectionsToWrite) {
const auto collID = collIDTable->collectionID(name);
Expand Down
6 changes: 4 additions & 2 deletions src/SIOBlock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#endif

namespace podio {
void SIOCollectionIDTableBlock::read(sio::read_device& device, sio::version_type) {
void SIOCollectionIDTableBlock::read(sio::read_device& device, sio::version_type version) {
std::vector<std::string> names;
std::vector<int> ids;
device.data(names);
device.data(ids);
device.data(_types);
device.data(_isSubsetColl);
if (version >= sio::version::encode_version(0, 2)) {
device.data(_isSubsetColl);
}

_table = new CollectionIDTable(std::move(ids), std::move(names));
}
Expand Down
3 changes: 2 additions & 1 deletion src/SIOReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ namespace podio {
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_subsetCollectionBits[i]);
const bool subsetColl = m_subsetCollectionBits.size() && m_subsetCollectionBits[i];
auto blk = podio::SIOBlockFactory::instance().createBlock(m_typeNames[i], m_table->names()[i], subsetColl);
m_blocks.push_back(blk);
m_inputs.emplace_back(blk->getCollection(), m_table->names()[i]);
}
Expand Down
41 changes: 41 additions & 0 deletions src/rootUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

#include "podio/CollectionBase.h"
#include "podio/CollectionBranches.h"
#include "podio/CollectionIDTable.h"

#include "TBranch.h"
#include "TClass.h"

#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <string_view>

namespace podio::root_utils {
// Workaround slow branch retrieval for 6.22/06 performance degradation
Expand Down Expand Up @@ -47,6 +51,43 @@ inline void setCollectionAddresses(podio::CollectionBase* collection, const Coll
}
}

// A collection of additional information that describes the collection: the
// collectionID, the collection (data) type, and whether it is a subset
// collection
using CollectionInfoT = std::tuple<int, std::string, bool>;

/**
* reconstruct the collection info from information that is available from other
* trees in the file.
*
* NOTE: This function is only supposed to be called if there is no
* "CollectionTypeInfo" branch in the metadata tree, as it assumes that the file
* has been written with podio previous to #197 where there were no subset
* collections
*/
inline auto reconstructCollectionInfo(TTree* eventTree, podio::CollectionIDTable const& idTable) {
std::vector<CollectionInfoT> collInfo;

for (size_t iColl = 0; iColl < idTable.names().size(); ++iColl) {
const auto collID = idTable.ids()[iColl];
const auto& name = idTable.names()[iColl];

if (auto branch = getBranch(eventTree, name.c_str())) {
const std::string_view bufferClassName = branch->GetClassName();
// this comes with vector<...Data>, where we only care about the ...
std::string_view dataClass = bufferClassName;
dataClass.remove_suffix(5);
const auto collClass = std::string(dataClass.substr(7)) + "Collection";
// Assume that there are no subset collections in "old files"
collInfo.emplace_back(collID, std::move(collClass), false);
} else {
std::cerr << "Problems reconstructing collection info for collection: \'" << name << "\'\n";
}
}

return collInfo;
}

}

#endif

0 comments on commit 181ec3d

Please sign in to comment.