Belle II Software  release-05-01-25
HandleRoot.cc
1 #ifndef __EXTERNAL__
2 #include "trg/cdc/HandleRoot.h"
3 #else
4 #include "HandleRoot.h"
5 #endif
6 #include <utility>
7 #include <iostream>
8 #include <TBranchObject.h>
9 #include <tuple>
10 
11 using std::cout;
12 using std::endl;
13 using std::vector;
14 using std::map;
15 using std::string;
16 using std::to_string;
17 using std::get;
18 
19 namespace HandleRoot {
20 
21  void initializeEvent(
22  std::map<std::string, TVectorD*>& eventMapTVectorD,
23  std::map<std::string, TClonesArray*>& trackMapTVectorD
24  )
25  {
26  for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
27  it->second->Clear();
28  }
29  for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
30  it->second->Clear();
31  }
32  }
33 
34  void initializeEvent(
35  std::map<std::string, TClonesArray*>& trackMapTVectorD
36  )
37  {
38  map<string, TVectorD*> eventMapTVectorD;
39  initializeEvent(eventMapTVectorD, trackMapTVectorD);
40  }
41 
42  void initializeRoot(std::string prefix, TTree** runTree, TTree** eventTree,
43  std::map<std::string, TVectorD*>& runMapTVectorD,
44  std::map<std::string, TVectorD*>& eventMapTVectorD,
45  std::map<std::string, TClonesArray*>& trackMapTVectorD,
46  std::map<std::string, double>& constMapD, std::map<std::string, std::vector<double> >& constMapV,
47  std::map<std::string, double>& eventMapD, std::map<std::string, std::vector<double> >& eventMapV,
48  std::map<std::string, double>& trackMapD, std::map<std::string, std::vector<double> >& trackMapV
49  )
50  {
51 
52  // Add Run tree
53  (*runTree) = new TTree((prefix + "runTree").c_str(), "run");
54  // Make storage and save for saving m_mConstD.
55  for (map<string, double>::iterator it = constMapD.begin(); it != constMapD.end(); ++it) {
56  runMapTVectorD[prefix + "Double" + (*it).first] = new TVectorD(1, &(*it).second);
57  }
58  // Make storage and save for saving m_mConstV
59  for (map<string, vector<double> >::iterator it = constMapV.begin(); it != constMapV.end(); ++it) {
60  unsigned t_vectorSize = it->second.size();
61  runMapTVectorD[prefix + "Vector" + (*it).first] = new TVectorD(t_vectorSize, &((*it).second)[0]);
62  }
63  // Adding branch
64  for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
65  (*runTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
66  }
67  // Fill tree
68  (*runTree)->Fill();
69 
70 
71  // Make event tree
72  (*eventTree) = new TTree((prefix + "eventTree").c_str(), "event");
73  // Events
74  // Make storage for saving m_mEventD
75  for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
76  eventMapTVectorD[prefix + "EventDouble" + (*it).first] = new TVectorD(1);
77  }
78  // Make storage for saving m_mEventV
79  for (map<string, vector<double> >::iterator it = eventMapV.begin(); it != eventMapV.end(); ++it) {
80  unsigned t_vectorSize = it->second.size();
81  eventMapTVectorD[prefix + "EventVector" + (*it).first] = new TVectorD(t_vectorSize);
82  }
83  // Adding branch
84  for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
85  (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
86  }
87  // Tracks
88  // Make storage for saving m_mDouble
89  for (map<string, double>::iterator it = trackMapD.begin(); it != trackMapD.end(); ++it) {
90  trackMapTVectorD[prefix + "TrackDouble" + (*it).first] = new TClonesArray("TVectorD");
91  }
92  // Make storage for saving m_mVector
93  for (map<string, vector<double> >::iterator it = trackMapV.begin(); it != trackMapV.end(); ++it) {
94  trackMapTVectorD[prefix + "TrackVector" + (*it).first] = new TClonesArray("TVectorD");
95  }
96  // Adding branch
97  for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
98  (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
99  }
100 
101  }
102 
103  void initializeRoot(std::string prefix, TTree** runTree, TTree** eventTree,
104  std::map<std::string, TVectorD*>& runMapTVectorD,
105  std::map<std::string, TClonesArray*>& trackMapTVectorD,
106  std::map<std::string, double>& constMapD, std::map<std::string, std::vector<double> >& constMapV,
107  std::map<std::string, double>& trackMapD, std::map<std::string, std::vector<double> >& trackMapV
108  )
109  {
110  map<string, TVectorD*> eventMapTVectorD;
111  map<string, double> eventMapD;
112  map<string, vector<double> > eventMapV;
113  initializeRoot(prefix, runTree, eventTree,
114  runMapTVectorD, eventMapTVectorD, trackMapTVectorD,
115  constMapD, constMapV,
116  eventMapD, eventMapV,
117  trackMapD, trackMapV
118  );
119  }
120 
121  void saveTrackValues(std::string prefix,
122  std::map<std::string, TClonesArray*>& trackMapTVectorD,
123  std::map<std::string, double>& trackMapD, std::map<std::string, std::vector<double> >& trackMapV
124  )
125  {
126  // Save m_mDouble
127  for (map<string, double >::iterator it = trackMapD.begin(); it != trackMapD.end(); ++it) {
128  new((*trackMapTVectorD[prefix + "TrackDouble" + (*it).first])[trackMapD["iSave"]]) TVectorD(1, &(*it).second);
129  }
130  // Save m_mVector
131  for (map<string, vector<double> >::iterator it = trackMapV.begin(); it != trackMapV.end(); ++it) {
132  unsigned t_vectorSize = it->second.size();
133  new((*trackMapTVectorD[prefix + "TrackVector" + (*it).first])[trackMapD["iSave"]]) TVectorD(t_vectorSize, &((*it).second)[0]);
134  }
135 
136  trackMapD["iSave"]++;
137  }
138 
139  void saveEventValues(std::string prefix,
140  std::map<std::string, TVectorD*>& eventMapTVectorD,
141  std::map<std::string, double>& eventMapD, std::map<std::string, std::vector<double> >& eventMapV
142  )
143  {
144  // Save m_mEventD
145  for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
146  eventMapTVectorD[prefix + "EventDouble" + (*it).first]->Use(1, &(*it).second);
147  }
148  // Save m_mEventV
149  for (map<string, vector<double> >::iterator it = eventMapV.begin(); it != eventMapV.end(); ++it) {
150  unsigned t_vectorSize = it->second.size();
151  if (t_vectorSize != 0) eventMapTVectorD[prefix + "EventVector" + (*it).first]->Use(t_vectorSize, &((*it).second)[0]);
152  }
153  }
154 
155  void writeRoot(TFile* file)
156  {
157  if (file) {
158  file->Write();
159  file->Close();
160  }
161  }
162 
163  void terminateRoot(
164  std::map<std::string, TVectorD*>& runMapTVectorD,
165  std::map<std::string, TVectorD*>& eventMapTVectorD,
166  std::map<std::string, TClonesArray*>& trackMapTVectorD
167  )
168  {
169  for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
170  delete it->second;
171  }
172  for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
173  delete it->second;
174  }
175  for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
176  delete it->second;
177  }
178  }
179 
180  void terminateRoot(
181  std::map<std::string, TVectorD*>& runMapTVectorD,
182  std::map<std::string, TClonesArray*>& trackMapTVectorD
183  )
184  {
185  std::map<std::string, TVectorD*> eventMapTVectorD;
186  terminateRoot(runMapTVectorD, eventMapTVectorD, trackMapTVectorD);
187  }
188 
189  void initializeBranches(std::string& prefix, TFile* file, TTree** runTree, TTree** eventTree,
190  std::map<std::string, TVectorD*>& runMapTVectorD,
191  std::map<std::string, TVectorD*>& eventMapTVectorD,
192  std::map<std::string, TClonesArray*>& trackMapTVectorD
193  )
194  {
195  // Get Trees
196  (*runTree) = (TTree*)file->Get((prefix + "runTree").c_str());
197  (*eventTree) = (TTree*)file->Get((prefix + "eventTree").c_str());
198  // Set memory for constant information
199  TObjArray* constBranchList = (*runTree)->GetListOfBranches();
200  for (int iBranch = 0; iBranch < constBranchList->GetEntries(); iBranch++) {
201  string t_branchName = constBranchList->At(iBranch)->GetName();
202  runMapTVectorD[t_branchName] = new TVectorD();
203  (*runTree)->SetBranchAddress(t_branchName.c_str(), &runMapTVectorD[t_branchName]);
204  }
205  // Set memory for event and track information
206  TObjArray* trackBranchList = (*eventTree)->GetListOfBranches();
207  for (int iBranch = 0; iBranch < trackBranchList->GetEntries(); iBranch++) {
208  string t_branchName = trackBranchList->At(iBranch)->GetName();
209  string t_className = ((TBranchObject*)trackBranchList->At(iBranch))->GetClassName();
210  // Track
211  if (t_className == "TClonesArray") {
212  trackMapTVectorD[t_branchName] = new TClonesArray("TVectorD");
213  (*eventTree)->SetBranchAddress(t_branchName.c_str(), &trackMapTVectorD[t_branchName]);
214  } else if (t_className == "TVectorT<double>") {
215  // Event
216  eventMapTVectorD[t_branchName] = new TVectorD();
217  (*eventTree)->SetBranchAddress(t_branchName.c_str(), &eventMapTVectorD[t_branchName]);
218  } else {
219  cout << "[Warning] HandleRoot::initializeBranches => Type of branch " << t_branchName << " is unkown." << endl;
220  }
221  }
222  }
223 
224  void initializeBranches(std::string& prefix, TFile* file, TTree** runTree, TTree** eventTree,
225  std::map<std::string, TVectorD*>& runMapTVectorD,
226  std::map<std::string, TClonesArray*>& trackMapTVectorD
227  )
228  {
229  map<string, TVectorD*> eventMapTVectorD;
230  initializeBranches(prefix, file, runTree, eventTree,
231  runMapTVectorD, eventMapTVectorD, trackMapTVectorD
232  );
233  }
234 
235  void getRunValues(std::string prefix,
236  std::map<std::string, TVectorD*>& runMapTVectorD,
237  std::map<std::string, double>& constMapD, std::map<std::string, std::vector<double> >& constMapV
238  )
239  {
240  // Get all constant information
241  for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
242  string t_name = it->first.substr(prefix.size() + 6);
243  string t_type = it->first.substr(prefix.size(), 6);
244  if (t_type == "Double") {
245  constMapD[t_name] = (*it->second)[0];
246  } else if (t_type == "Vector") {
247  unsigned nElements = it->second->GetNrows();
248  constMapV[t_name] = vector<double> (nElements);
249  for (unsigned i = 0; i < nElements; i++) {
250  constMapV[t_name][i] = (*it->second)[i];
251  }
252  } else {
253  cout << "[Error] HandleRoot::getRunValues => t_type: " << t_type << " is unknown." << endl;
254  }
255  }
256  }
257 
258  void getEventValues(std::string& prefix,
259  std::map<std::string, TVectorD*>& eventMapTVectorD,
260  std::map<std::string, double>& eventMapD, std::map<std::string, std::vector<double> >& eventMapV
261  )
262  {
263  // Get all event information
264  for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
265  string t_name = it->first.substr((prefix + "Event").size() + 6);
266  string t_type = it->first.substr((prefix + "Event").size(), 6);
267  if (t_type == "Double") {
268  eventMapD[t_name] = (*it->second)[0];
269  } else if (t_type == "Vector") {
270  unsigned nElements = it->second->GetNrows();
271  eventMapV[t_name] = vector<double> (nElements);
272  for (unsigned i = 0; i < nElements; i++) {
273  eventMapV[t_name][i] = (*it->second)[i];
274  }
275  } else {
276  cout << "[Error] HandleRoot::getEventValues => t_type: " << t_type << " is unknown." << endl;
277  }
278  }
279  }
280 
281  void getTrackValues(std::string& prefix, int iTrack,
282  std::map<std::string, TClonesArray*>& trackMapTVectorD,
283  std::map<std::string, double>& trackMapD, std::map<std::string, std::vector<double> >& trackMapV
284  )
285  {
286  for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
287  string t_name = it->first.substr((prefix + "Track").size() + 6);
288  string t_type = it->first.substr((prefix + "Track").size(), 6);
289  if (t_type == "Double") {
290  trackMapD[t_name] = (*(TVectorD*)it->second->At(iTrack))[0];
291  } else if (t_type == "Vector") {
292  unsigned nElements = ((TVectorD*)it->second->At(iTrack))->GetNrows();
293  trackMapV[t_name] = vector<double> (nElements);
294  for (unsigned i = 0; i < nElements; i++) {
295  trackMapV[t_name][i] = (*(TVectorD*)it->second->At(iTrack))[i];
296  }
297  } else {
298  cout << "[Error] HandleRoot::getTrackValues => t_type: " << t_type << " is unknown." << endl;
299  }
300  }
301  }
302 
303  void convertSignalValuesToMaps(std::vector<std::tuple<std::string, double, int, double, double, int> > const& inValues,
304  std::map<std::string, double>& trackMapD, std::map<std::string, std::vector<double> >& trackMapV)
305  {
306  for (unsigned iValue = 0; iValue < inValues.size(); iValue++) {
307  string const& t_name = get<0>(inValues[iValue]);
308  double const& t_value = get<1>(inValues[iValue]);
309  //int const & t_bitWidth = get<2>(inValues[iValue]);
310  //double const & t_min = get<3>(inValues[iValue]);
311  //double const & t_max = get<4>(inValues[iValue]);
312  //int const & t_clock = get<5>(inValues[iValue]);
313  // Divide vectors and doubles by name.
314  size_t t_find = t_name.find("_");
315  // Vector
316  if (t_find != string::npos) {
317  string t_vectorName = t_name.substr(0, t_find);
318  int t_vectorIndex = stoi(t_name.substr(t_find + 1, t_name.size()));
319  // Create vector if not in map.
320  if (trackMapV.find(t_vectorName) == trackMapV.end()) {
321  trackMapV[t_vectorName] = vector<double> (1);
322  }
323  // Increase vector size if vector size is too small
324  int nIncrease = t_vectorIndex + 1 - trackMapV[t_vectorName].size();
325  if (nIncrease > 0) {
326  for (int iIncrease = 0; iIncrease < nIncrease; iIncrease++) trackMapV[t_vectorName].push_back(0);
327  }
328  // Fill to map
329  trackMapV[t_vectorName][t_vectorIndex] = t_value;
330  } else {
331  // Double
332  trackMapD[t_name] = t_value;
333  }
334  }
335  }
336 
337 } // namespace HandleRoot