Belle II Software development
RootParameterTracker.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10// in fw:
11#include <tracking/trackFindingVXD/analyzingTools/AnalizerTCInfo.h>
12#include <tracking/trackFindingVXD/analyzingTools/AnalyzingAlgorithmFactory.h>
13#include <tracking/trackFindingVXD/analyzingTools/KeyValBox.h>
14// #include <tracking/trackFindingVXD/analyzingTools/algorithms/AnalyzingAlgorithmClusterBased.h> // TODO
15#include <framework/logging/Logger.h>
16
17// root:
18#include <TTree.h>
19#include <TFile.h>
20#include <Math/Vector3D.h>
21
22// stl:
23#include <string>
24#include <vector>
25#include <utility> // for pair
26
27namespace Belle2 {
50 protected:
51
53 template<class ValueType> using StringKeyBox = KeyValBox<std::string, ValueType>;
54
55
58
59
68 template<class DataType> using AlgoDataBox =
70
71
78
79
82
83
86
87
90
91
93 TFile* m_file;
94
95
97 TTree* prepareTTree(std::string tcTypeName)
98 {
99 if (m_file == nullptr) {
100 B2FATAL("RootParameterTracker::prepareTTree(), Root file not initialized yet! Please call RootParameterTracker::initialize(...) first!");
101 }
102 m_file->cd();
103 // m_file->ls();
104
106 TTree** tree4tcType = m_treeBox.find(tcTypeName);
107 if (tree4tcType == nullptr) {
108 m_treeBox.push_back({
109 tcTypeName,
110 new TTree(tcTypeName.c_str(), (std::string("collects data collected to tcType ") + tcTypeName).c_str())
111 }
112 );
113 tree4tcType = m_treeBox.find(tcTypeName);
114 B2WARNING("RootParameterTracker::prepareTTree: new tree for tcType " << tcTypeName << " created, m_treeBox has now " <<
115 m_treeBox.size() << " entries");
116 return *tree4tcType;
117 }
118
119 B2WARNING("RootParameterTracker::prepareTTree: ttree for tcType " << tcTypeName << " is reused, m_treeBox has " << m_treeBox.size()
120 << " entries");
121 return *tree4tcType;
122 }
123
124
126 void collectData4DoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
127
128
130 void collectData4IntAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
131
132
134 void collectData4VecDoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
135
136
137 public:
138
141
142
147 void initialize(std::string fileName, std::string fileTreatment)
148 {
149 if (fileTreatment != std::string("RECREATE") and fileTreatment != std::string("UPDATE")) {
150 B2FATAL("RootParameterTracker::initialize(), specified fileTreatment is " << fileTreatment <<
151 ", which is invalid, please read the documentation!");
152 }
153
154 if (m_file != nullptr) {
155 B2FATAL("RootParameterTracker::initialize(), there was a file already linked to this ParameterTracker, which is invalid, please read the documentation!");
156 }
157 B2DEBUG(20, "RootParameterTracker::initialize(), given parameters are fileName/fileTreatment: " << fileName << "/" <<
158 fileTreatment);
159 m_file = new TFile(fileName.c_str(), fileTreatment.c_str()); // alternative: UPDATE
160 m_file->ls();
161 }
162
163
167 void addParameters4DoubleAlgorithms(std::string tcTypeName, std::string algorithmName);
168
169
173 void addParameters4IntAlgorithms(std::string tcTypeName, std::string algorithmName);
174
175
179 void addParameters4VecDoubleAlgorithms(std::string tcTypeName, std::string algorithmName);
180
181
183 void collectData(const std::vector<AnalizerTCInfo>& tcVector)
184 {
192 B2DEBUG(21, "RootParameterTracker::collectData(), size of given tcVector is: " << tcVector.size());
193
194 for (const AnalizerTCInfo& tc : tcVector) {
195 std::string tcTypeName = TCType::getTypeName(tc.getType());
196 B2DEBUG(22, "RootParameterTracker::collectData(), executing TC of type: " << tcTypeName);
197
198 collectData4DoubleAlgorithms(tcTypeName, tc);
199 collectData4IntAlgorithms(tcTypeName, tc);
200 collectData4VecDoubleAlgorithms(tcTypeName, tc);
201 } // looping over TCs
202 }
203
204
206 void fillRoot()
207 {
208 B2DEBUG(21, "RootParameterTracker::fillRoot(), Executing " << m_treeBox.size() << " ttrees.");
209
210 m_file->cd(); //important! without this the famework root I/O (SimpleOutput etc) could mix with the root I/O of this module
211// m_file->ls();
212
213 for (auto& boxEntry : m_treeBox) {
214 int nBytesWritten = boxEntry.second->Fill();
215 B2DEBUG(20, "RootParameterTracker::fillRoot() ttree " << boxEntry.first << " got " << nBytesWritten << " Bytes written");
216 // boxEntry.second->Print();
217 }
218
219 for (auto& algoData2tcType : m_algoDataDouble) {
220 for (auto& algoPack : algoData2tcType.second) {
221 algoPack.second.second->clear();
222 }
223 }
224
225 for (auto& algoData2tcType : m_algoDataInt) {
226 for (auto& algoPack : algoData2tcType.second) {
227 algoPack.second.second->clear();
228 }
229 }
230
231 for (auto& algoData2tcType : m_algoDataVecDouble) {
232 for (auto& algoPack : algoData2tcType.second) {
233 algoPack.second.second->clear();
234 }
235 }
236 }
237
238
241 {
242 B2DEBUG(20, "RootParameterTracker::terminate(), Writing results to root-file and clean up heap.");
243 if (m_file == nullptr) {
244 B2WARNING("RootParameterTracker::terminate(): no rootFile found! skipping writing data into root file!");
245 return;
246 }
247
248 m_file->cd(); //important! without this the famework root I/O (SimpleOutput etc) could mix with the root I/O of this module
249 m_file->ls();
250
251 for (auto& boxEntry : m_treeBox) {
252 boxEntry.second->Write();
253
254 if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 5, PACKAGENAME()) == true) {
255 B2DEBUG(20, "RootParameterTracker::terminate(), TTree " << boxEntry.first << " was written:");
256 // boxEntry.second->Print();
257 }
258 }
259 m_file->Close();
260
261 for (auto& algoData2tcType : m_algoDataDouble) {
262 for (auto& algoPack : algoData2tcType.second) {
263 delete algoPack.second.first; // algorithm
264 delete algoPack.second.second; // vector(data) to algorithm
265 }
266 }
267
268 for (auto& algoData2tcType : m_algoDataInt) {
269 for (auto& algoPack : algoData2tcType.second) {
270 delete algoPack.second.first; // algorithm
271 delete algoPack.second.second; // vector(data) to algorithm
272 }
273 }
274
275 for (auto& algoData2tcType : m_algoDataVecDouble) {
276 for (auto& algoPack : algoData2tcType.second) {
277 delete algoPack.second.first; // algorithm
278 delete algoPack.second.second; // vector(data) to algorithm
279 }
280 }
281 }
282
283 };
285}
simple class storing infos relevant for a TC for analyzing it.
Base class for storing an algorithm determining the data one wants to have.
Minimal container storing a pair of < KeyType, ValueType>
Definition: KeyValBox.h:24
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:26
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:31
Production notes for RootParameterTracker:
AlgoDataBox< int > m_algoDataInt
contains all algorithms and its data storing one int per TC
void addParameters4DoubleAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one double per TC:
StringKeyBox< TTree * > m_treeBox
contains all the trees for rootFile-filling later-on.
void addParameters4VecDoubleAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one vector of double per TC:
void terminate()
final cleanup and closing rootFile
RootParameterTracker()
constructor setting standard values
void fillRoot()
fills tree/branches with their stuff, clear intermediate results afterwards
void initialize(std::string fileName, std::string fileTreatment)
creates rootFile, first parameter is fileName, second one specifies how the file shall be treated.
void collectData4DoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
void collectData4IntAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
void addParameters4IntAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one int per TC:
TFile * m_file
stores pointer to file
TTree * prepareTTree(std::string tcTypeName)
checks if ttree for given tcTypeName exists and creates it if not.
void collectData4VecDoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
void collectData(const std::vector< AnalizerTCInfo > &tcVector)
take vector and fill for each tcType stored in rootParameterTracker
AlgoDataBox< std::vector< double > > m_algoDataVecDouble
contains all algorithms and its data storing one vector of double per TC
AlgoDataBox< double > m_algoDataDouble
contains all algorithms and its data storing one double per TC
static std::string getTypeName(TCType::Type type)
for given TCType the corresponding string-name will be returned.
Definition: TCType.h:62
Abstract base class for different kinds of events.