Belle II Software  release-05-02-19
RootParameterTracker.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 // in fw:
13 #include <tracking/trackFindingVXD/analyzingTools/AnalizerTCInfo.h>
14 #include <tracking/trackFindingVXD/analyzingTools/AnalyzingAlgorithmFactory.h>
15 #include <tracking/trackFindingVXD/analyzingTools/KeyValBox.h>
16 // #include <tracking/trackFindingVXD/analyzingTools/algorithms/AnalyzingAlgorithmClusterBased.h> // TODO
17 #include <framework/logging/Logger.h>
18 
19 // root:
20 #include <TTree.h>
21 #include <TFile.h>
22 #include <TVector3.h>
23 
24 // stl:
25 #include <string>
26 #include <vector>
27 #include <utility> // for pair
28 
29 namespace Belle2 {
51  class RootParameterTracker {
52  protected:
53 
55  template<class ValueType> using StringKeyBox = KeyValBox<std::string, ValueType>;
56 
57 
60 
61 
70  template<class DataType> using AlgoDataBox =
72 
73 
80 
81 
84 
85 
88 
89 
92 
93 
95  TFile* m_file;
96 
97 
99  TTree* prepareTTree(std::string tcTypeName)
100  {
101  if (m_file == NULL) {
102  B2FATAL("RootParameterTracker::prepareTTree(), Root file not initialized yet! Please call RootParameterTracker::initialize(...) first!");
103  }
104  m_file->cd();
105  // m_file->ls();
106 
108  TTree** tree4tcType = m_treeBox.find(tcTypeName);
109  if (tree4tcType == NULL) {
110  m_treeBox.push_back({
111  tcTypeName,
112  new TTree(tcTypeName.c_str(), (std::string("collects data collected to tcType ") + tcTypeName).c_str())
113  }
114  );
115  tree4tcType = m_treeBox.find(tcTypeName);
116  B2WARNING("RootParameterTracker::prepareTTree: new tree for tcType " << tcTypeName << " created, m_treeBox has now " <<
117  m_treeBox.size() << " entries");
118  return *tree4tcType;
119  }
120 
121  B2WARNING("RootParameterTracker::prepareTTree: ttree for tcType " << tcTypeName << " is reused, m_treeBox has " << m_treeBox.size()
122  << " entries");
123  return *tree4tcType;
124  }
125 
126 
128  void collectData4DoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
129 
130 
132  void collectData4IntAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
133 
134 
136  void collectData4VecDoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo& aTC);
137 
138 
139  public:
140 
142  RootParameterTracker() : m_file(NULL) {}
143 
144 
149  void initialize(std::string fileName, std::string fileTreatment)
150  {
151  if (fileTreatment != std::string("RECREATE") and fileTreatment != std::string("UPDATE")) {
152  B2FATAL("RootParameterTracker::initialize(), specified fileTreatment is " << fileTreatment <<
153  ", which is invalid, please read the documentation!");
154  }
155 
156  if (m_file != NULL) {
157  B2FATAL("RootParameterTracker::initialize(), there was a file already linked to this ParameterTracker, which is invalid, please read the documentation!");
158  }
159  B2DEBUG(5, "RootParameterTracker::initialize(), given parameters are fileName/fileTreatment: " << fileName << "/" <<
160  fileTreatment);
161  m_file = new TFile(fileName.c_str(), fileTreatment.c_str()); // alternative: UPDATE
162  m_file->ls();
163  }
164 
165 
169  void addParameters4DoubleAlgorithms(std::string tcTypeName, std::string algorithmName);
170 
171 
175  void addParameters4IntAlgorithms(std::string tcTypeName, std::string algorithmName);
176 
177 
181  void addParameters4VecDoubleAlgorithms(std::string tcTypeName, std::string algorithmName);
182 
183 
185  void collectData(const std::vector<AnalizerTCInfo>& tcVector)
186  {
194  B2DEBUG(15, "RootParameterTracker::collectData(), size of given tcVector is: " << tcVector.size());
195 
196  for (const AnalizerTCInfo& tc : tcVector) {
197  std::string tcTypeName = TCType::getTypeName(tc.getType());
198  B2DEBUG(50, "RootParameterTracker::collectData(), executing TC of type: " << tcTypeName);
199 
200  collectData4DoubleAlgorithms(tcTypeName, tc);
201  collectData4IntAlgorithms(tcTypeName, tc);
202  collectData4VecDoubleAlgorithms(tcTypeName, tc);
203  } // looping over TCs
204  }
205 
206 
208  void fillRoot()
209  {
210  B2DEBUG(15, "RootParameterTracker::fillRoot(), Executing " << m_treeBox.size() << " ttrees.");
211 
212  m_file->cd(); //important! without this the famework root I/O (SimpleOutput etc) could mix with the root I/O of this module
213 // m_file->ls();
214 
215  for (auto& boxEntry : m_treeBox) {
216  int nBytesWritten = boxEntry.second->Fill();
217  B2DEBUG(20, "RootParameterTracker::fillRoot() ttree " << boxEntry.first << " got " << nBytesWritten << " Bytes written");
218  // boxEntry.second->Print();
219  }
220 
221  for (auto& algoData2tcType : m_algoDataDouble) {
222  for (auto& algoPack : algoData2tcType.second) {
223  algoPack.second.second->clear();
224  }
225  }
226 
227  for (auto& algoData2tcType : m_algoDataInt) {
228  for (auto& algoPack : algoData2tcType.second) {
229  algoPack.second.second->clear();
230  }
231  }
232 
233  for (auto& algoData2tcType : m_algoDataVecDouble) {
234  for (auto& algoPack : algoData2tcType.second) {
235  algoPack.second.second->clear();
236  }
237  }
238  }
239 
240 
242  void terminate()
243  {
244  B2DEBUG(2, "RootParameterTracker::terminate(), Writing results to root-file and clean up heap.");
245  if (m_file == NULL) {
246  B2WARNING("RootParameterTracker::terminate(): no rootFile found! skipping writing data into root file!");
247  return;
248  }
249 
250  m_file->cd(); //important! without this the famework root I/O (SimpleOutput etc) could mix with the root I/O of this module
251  m_file->ls();
252 
253  for (auto& boxEntry : m_treeBox) {
254  boxEntry.second->Write();
255 
256  if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 5, PACKAGENAME()) == true) {
257  B2DEBUG(5, "RootParameterTracker::terminate(), TTree " << boxEntry.first << " was written:");
258  // boxEntry.second->Print();
259  }
260  }
261  m_file->Close();
262 
263  for (auto& algoData2tcType : m_algoDataDouble) {
264  for (auto& algoPack : algoData2tcType.second) {
265  delete algoPack.second.first; // algorithm
266  delete algoPack.second.second; // vector(data) to algorithm
267  }
268  }
269 
270  for (auto& algoData2tcType : m_algoDataInt) {
271  for (auto& algoPack : algoData2tcType.second) {
272  delete algoPack.second.first; // algorithm
273  delete algoPack.second.second; // vector(data) to algorithm
274  }
275  }
276 
277  for (auto& algoData2tcType : m_algoDataVecDouble) {
278  for (auto& algoPack : algoData2tcType.second) {
279  delete algoPack.second.first; // algorithm
280  delete algoPack.second.second; // vector(data) to algorithm
281  }
282  }
283  }
284 
285  };
287 }
Belle2::RootParameterTracker::collectData4DoubleAlgorithms
void collectData4DoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
Definition: RootParameterTracker.cc:17
Belle2::TCType::getTypeName
static std::string getTypeName(TCType::Type type)
for given TCType the corresponding string-name will be returned.
Definition: TCType.h:72
Belle2::RootParameterTracker::fillRoot
void fillRoot()
fills tree/branches with their stuff, clear intermediate results afterwards
Definition: RootParameterTracker.h:216
Belle2::KeyValBox
Minimal container storing a pair of < KeyType, ValueType>
Definition: KeyValBox.h:34
Belle2::RootParameterTracker::StringKeyBox
KeyValBox< std::string, ValueType > StringKeyBox
short-cut since strings are always used as keys here:
Definition: RootParameterTracker.h:63
Belle2::RootParameterTracker::collectData4IntAlgorithms
void collectData4IntAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
Definition: RootParameterTracker.cc:60
Belle2::RootParameterTracker::collectData4VecDoubleAlgorithms
void collectData4VecDoubleAlgorithms(std::string tcTypeName, const AnalizerTCInfo &aTC)
takes aTC with tcTypeName and applies the algorithms for it
Definition: RootParameterTracker.cc:103
Belle2::RootParameterTracker::prepareTTree
TTree * prepareTTree(std::string tcTypeName)
checks if ttree for given tcTypeName exists and creates it if not.
Definition: RootParameterTracker.h:107
Belle2::RootParameterTracker::m_file
TFile * m_file
stores pointer to file
Definition: RootParameterTracker.h:103
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RootParameterTracker::collectData
void collectData(const std::vector< AnalizerTCInfo > &tcVector)
take vector and fill for each tcType stored in rootParameterTracker
Definition: RootParameterTracker.h:193
Belle2::RootParameterTracker::addParameters4IntAlgorithms
void addParameters4IntAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one int per TC:
Definition: RootParameterTracker.cc:237
Belle2::RootParameterTracker::m_algoDataDouble
AlgoDataBox< double > m_algoDataDouble
contains all algorithms and its data storing one double per TC
Definition: RootParameterTracker.h:91
Belle2::RootParameterTracker::addParameters4VecDoubleAlgorithms
void addParameters4VecDoubleAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one vector of double per TC:
Definition: RootParameterTracker.cc:294
Belle2::RootParameterTracker::terminate
void terminate()
final cleanup and closing rootFile
Definition: RootParameterTracker.h:250
Belle2::LogSystem::Instance
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:33
Belle2::RootParameterTracker::m_algoDataInt
AlgoDataBox< int > m_algoDataInt
contains all algorithms and its data storing one int per TC
Definition: RootParameterTracker.h:95
Belle2::RootParameterTracker::addParameters4DoubleAlgorithms
void addParameters4DoubleAlgorithms(std::string tcTypeName, std::string algorithmName)
relevant for all algorithms storing one double per TC:
Definition: RootParameterTracker.cc:158
Belle2::LogConfig::c_Debug
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:36
Belle2::RootParameterTracker::m_treeBox
StringKeyBox< TTree * > m_treeBox
contains all the trees for rootFile-filling later-on.
Definition: RootParameterTracker.h:87
Belle2::RootParameterTracker::RootParameterTracker
RootParameterTracker()
constructor setting standard values
Definition: RootParameterTracker.h:150
Belle2::AnalyzingAlgorithmBase
Base class for storing an algorithm determining the data one wants to have.
Definition: AnalyzingAlgorithmBase.h:41
Belle2::RootParameterTracker::initialize
void initialize(std::string fileName, std::string fileTreatment)
creates rootFile, first parameter is fileName, second one specifies how the file shall be treated.
Definition: RootParameterTracker.h:157
Belle2::RootParameterTracker::m_algoDataVecDouble
AlgoDataBox< std::vector< double > > m_algoDataVecDouble
contains all algorithms and its data storing one vector of double per TC
Definition: RootParameterTracker.h:99