PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ROOTWriter.cc
Go to the documentation of this file.
1#include "rootUtils.h"
2
3// podio specific includes
5#include "podio/EventStore.h"
6#include "podio/ROOTWriter.h"
7#include "podio/podioVersion.h"
8
9// ROOT specifc includes
10#include "TFile.h"
11#include "TTree.h"
12
13namespace podio {
14ROOTWriter::ROOTWriter(const std::string& filename, EventStore* store) :
15 m_filename(filename),
16 m_store(store),
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")) {
23
24 m_evtMDtree->Branch("evtMD", "GenericParameters", m_store->eventMetaDataPtr());
25}
26
28 delete m_file;
29}
30
32 std::vector<StoreCollection> collections;
33 collections.reserve(m_collectionsToWrite.size());
34 for (const auto& name : m_collectionsToWrite) {
35 const podio::CollectionBase* coll;
36 m_store->get(name, coll);
37 collections.emplace_back(name, const_cast<podio::CollectionBase*>(coll));
38 collections.back().second->prepareForWrite();
39 }
40
41 if (m_firstEvent) {
42 createBranches(collections);
43 m_firstEvent = false;
44 } else {
45 setBranches(collections);
46 }
47
48 m_datatree->Fill();
49 m_evtMDtree->Fill();
50}
51
52void ROOTWriter::createBranches(const std::vector<StoreCollection>& collections) {
53 for (auto& [name, coll] : collections) {
55 const auto collBuffers = coll->getBuffers();
56 if (collBuffers.data) {
57 // only create the data buffer branch if necessary
58
59 auto collClassName = "vector<" + coll->getDataTypeName() + ">";
60
61 branches.data = m_datatree->Branch(name.c_str(), collClassName.c_str(), collBuffers.data);
62 }
63
64 // reference collections
65 if (auto refColls = collBuffers.references) {
66 int i = 0;
67 for (auto& c : (*refColls)) {
68 const auto brName = root_utils::refBranch(name, i);
69 branches.refs.push_back(m_datatree->Branch(brName.c_str(), c.get()));
70 ++i;
71 }
72 }
73
74 // vector members
75 if (auto vminfo = collBuffers.vectorMembers) {
76 int i = 0;
77 for (auto& [type, vec] : (*vminfo)) {
78 const auto typeName = "vector<" + type + ">";
79 const auto brName = root_utils::vecBranch(name, i);
80 branches.vecs.push_back(m_datatree->Branch(brName.c_str(), typeName.c_str(), vec));
81 ++i;
82 }
83 }
84
85 m_collectionBranches.push_back(branches);
86 }
87}
88
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];
93 root_utils::setCollectionAddresses(coll.second->getBuffers(), branches);
94
95 iCollection++;
96 }
97}
98
100 // now we want to safe the metadata. This includes info about the
101 // collections
102 const auto collIDTable = m_store->getCollectionIDTable();
103 m_metadatatree->Branch("CollectionIDs", collIDTable);
104
105 // collectionID, collection type, subset collection
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);
110 const podio::CollectionBase* coll{nullptr};
111 // No check necessary, only registered collections possible
112 m_store->get(name, coll);
113 const auto collType = coll->getTypeName();
114 // const auto collType = "std::vector<" + coll->getDataTypeName() + ">";
115 collectionInfo.emplace_back(collID, std::move(collType), coll->isSubsetCollection());
116 }
117
118 m_metadatatree->Branch("CollectionTypeInfo", &collectionInfo);
119
120 podio::version::Version podioVersion = podio::version::build_version;
121 m_metadatatree->Branch("PodioVersion", &podioVersion);
122
123 m_metadatatree->Fill();
124
125 m_colMDtree->Branch("colMD", "std::map<int,podio::GenericParameters>", m_store->getColMetaDataMap());
126 m_colMDtree->Fill();
127 m_runMDtree->Branch("runMD", "std::map<int,podio::GenericParameters>", m_store->getRunMetaDataMap());
128 m_runMDtree->Fill();
129
130 m_file->Write();
131 m_file->Close();
132}
133
134bool ROOTWriter::registerForWrite(const std::string& name) {
135 const podio::CollectionBase* tmp_coll(nullptr);
136 if (!m_store->get(name, tmp_coll)) {
137 std::cerr << "no such collection to write, throw exception." << std::endl;
138 return false;
139 }
140
141 m_collectionsToWrite.push_back(name);
142 return true;
143}
144
145} // namespace podio
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
Definition: EventStore.h:143
ColMDMap * getColMetaDataMap()
Definition: EventStore.h:104
CollectionIDTable * getCollectionIDTable() const
Definition: EventStore.h:86
GenericParameters * eventMetaDataPtr()
Definition: EventStore.h:107
RunMDMap * getRunMetaDataMap()
Definition: EventStore.h:101
ROOTWriter(const std::string &filename, EventStore *store)
Definition: ROOTWriter.cc:14
bool registerForWrite(const std::string &name)
Definition: ROOTWriter.cc:134
std::string refBranch(const std::string &name, size_t index)
Definition: rootUtils.h:75
std::string vecBranch(const std::string &name, size_t index)
Definition: rootUtils.h:79
void setCollectionAddresses(const BufferT &collBuffers, const CollectionBranches &branches)
Definition: rootUtils.h:84