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