7#include "podio/podioVersion.h"
17 m_file(new TFile(filename.c_str(),
"RECREATE",
"data file")),
18 m_datatree(new TTree(
"events",
"Events tree")),
19 m_metadatatree(new TTree(
"metadata",
"Metadata tree")),
20 m_runMDtree(new TTree(
"run_metadata",
"Run metadata tree")),
21 m_evtMDtree(new TTree(
"evt_metadata",
"Event metadata tree")),
22 m_colMDtree(new TTree(
"col_metadata",
"Collection metadata tree")) {
24 m_evtMDtree->Branch(
"evtMD",
"GenericParameters", m_store->
eventMetaDataPtr());
32 std::vector<StoreCollection> collections;
33 collections.reserve(m_collectionsToWrite.size());
34 for (
const auto& name : m_collectionsToWrite) {
36 m_store->
get(name, coll);
38 collections.back().second->prepareForWrite();
42 createBranches(collections);
45 setBranches(collections);
52void ROOTWriter::createBranches(
const std::vector<StoreCollection>& collections) {
53 for (
auto& [name, coll] : collections) {
55 const auto collBuffers = coll->getBuffers();
56 if (collBuffers.data) {
59 auto collClassName =
"vector<" + coll->getDataTypeName() +
">";
61 branches.
data = m_datatree->Branch(name.c_str(), collClassName.c_str(), collBuffers.data);
65 if (
auto refColls = collBuffers.references) {
67 for (
auto& c : (*refColls)) {
69 branches.
refs.push_back(m_datatree->Branch(brName.c_str(), c.get()));
75 if (
auto vminfo = collBuffers.vectorMembers) {
77 for (
auto& [type, vec] : (*vminfo)) {
78 const auto typeName =
"vector<" + type +
">";
80 branches.
vecs.push_back(m_datatree->Branch(brName.c_str(), typeName.c_str(), vec));
85 m_collectionBranches.push_back(branches);
89void ROOTWriter::setBranches(
const std::vector<StoreCollection>& collections) {
90 size_t iCollection = 0;
91 for (
auto& coll : collections) {
92 const auto& branches = m_collectionBranches[iCollection];
103 m_metadatatree->Branch(
"CollectionIDs", collIDTable);
106 std::vector<root_utils::CollectionInfoT> collectionInfo;
107 collectionInfo.reserve(m_collectionsToWrite.size());
108 for (
const auto& name : m_collectionsToWrite) {
109 const auto collID = collIDTable->collectionID(name);
112 m_store->
get(name, coll);
113 const auto collType = coll->getTypeName();
115 collectionInfo.emplace_back(collID, std::move(collType), coll->isSubsetCollection());
118 m_metadatatree->Branch(
"CollectionTypeInfo", &collectionInfo);
121 m_metadatatree->Branch(
"PodioVersion", &podioVersion);
123 m_metadatatree->Fill();
125 m_colMDtree->Branch(
"colMD",
"std::map<int,podio::GenericParameters>", m_store->
getColMetaDataMap());
127 m_runMDtree->Branch(
"runMD",
"std::map<int,podio::GenericParameters>", m_store->
getRunMetaDataMap());
136 if (!m_store->
get(name, tmp_coll)) {
137 std::cerr <<
"no such collection to write, throw exception." << std::endl;
141 m_collectionsToWrite.push_back(name);
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
ColMDMap * getColMetaDataMap()
CollectionIDTable * getCollectionIDTable() const
GenericParameters * eventMetaDataPtr()
RunMDMap * getRunMetaDataMap()
ROOTWriter(const std::string &filename, EventStore *store)
bool registerForWrite(const std::string &name)
std::string refBranch(const std::string &name, size_t index)
std::string vecBranch(const std::string &name, size_t index)
void setCollectionAddresses(const BufferT &collBuffers, const CollectionBranches &branches)
std::vector< TBranch * > vecs
std::vector< TBranch * > refs