9#include "trg/cdc/HandleRoot.h"
11#include "HandleRoot.h"
15#include <TBranchObject.h>
29 std::map<std::string, TVectorD*>& eventMapTVectorD,
30 std::map<std::string, TClonesArray*>& trackMapTVectorD
33 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
36 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
42 std::map<std::string, TClonesArray*>& trackMapTVectorD
45 map<string, TVectorD*> eventMapTVectorD;
46 initializeEvent(eventMapTVectorD, trackMapTVectorD);
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
60 (*runTree) =
new TTree((prefix +
"runTree").c_str(),
"run");
62 for (map<string, double>::iterator it = constMapD.begin(); it != constMapD.end(); ++it) {
63 runMapTVectorD[prefix +
"Double" + (*it).first] =
new TVectorD(1, &(*it).second);
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]);
71 for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
72 (*runTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
79 (*eventTree) =
new TTree((prefix +
"eventTree").c_str(),
"event");
82 for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
83 eventMapTVectorD[prefix +
"EventDouble" + (*it).first] =
new TVectorD(1);
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);
91 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
92 (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
96 for (map<string, double>::iterator it = trackMapD.begin(); it != trackMapD.end(); ++it) {
97 trackMapTVectorD[prefix +
"TrackDouble" + (*it).first] =
new TClonesArray(
"TVectorD");
100 for (map<
string, vector<double> >::iterator it = trackMapV.begin(); it != trackMapV.end(); ++it) {
101 trackMapTVectorD[prefix +
"TrackVector" + (*it).first] =
new TClonesArray(
"TVectorD");
104 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
105 (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
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
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,
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
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);
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]);
143 trackMapD[
"iSave"]++;
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
152 for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
153 eventMapTVectorD[prefix +
"EventDouble" + (*it).first]->Use(1, &(*it).second);
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]);
162 void writeRoot(TFile* file)
171 std::map<std::string, TVectorD*>& runMapTVectorD,
172 std::map<std::string, TVectorD*>& eventMapTVectorD,
173 std::map<std::string, TClonesArray*>& trackMapTVectorD
176 for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
179 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
182 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
188 std::map<std::string, TVectorD*>& runMapTVectorD,
189 std::map<std::string, TClonesArray*>& trackMapTVectorD
192 std::map<std::string, TVectorD*> eventMapTVectorD;
193 terminateRoot(runMapTVectorD, eventMapTVectorD, trackMapTVectorD);
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
203 (*runTree) = (TTree*)file->Get((prefix +
"runTree").c_str());
204 (*eventTree) = (TTree*)file->Get((prefix +
"eventTree").c_str());
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]);
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();
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>") {
223 eventMapTVectorD[t_branchName] =
new TVectorD();
224 (*eventTree)->SetBranchAddress(t_branchName.c_str(), &eventMapTVectorD[t_branchName]);
226 cout <<
"[Warning] HandleRoot::initializeBranches => Type of branch " << t_branchName <<
" is unkown." << endl;
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
236 map<string, TVectorD*> eventMapTVectorD;
237 initializeBranches(prefix, file, runTree, eventTree,
238 runMapTVectorD, eventMapTVectorD, trackMapTVectorD
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
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];
260 cout <<
"[Error] HandleRoot::getRunValues => t_type: " << t_type <<
" is unknown." << endl;
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
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];
283 cout <<
"[Error] HandleRoot::getEventValues => t_type: " << t_type <<
" is unknown." << endl;
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
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];
305 cout <<
"[Error] HandleRoot::getTrackValues => t_type: " << t_type <<
" is unknown." << endl;
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)
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]);
321 size_t t_find = t_name.find(
"_");
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()));
327 if (!trackMapV.count(t_vectorName)) {
328 trackMapV[t_vectorName] = vector<double> (1);
331 int nIncrease = t_vectorIndex + 1 - trackMapV[t_vectorName].size();
333 for (
int iIncrease = 0; iIncrease < nIncrease; iIncrease++) trackMapV[t_vectorName].push_back(0);
336 trackMapV[t_vectorName][t_vectorIndex] = t_value;
339 trackMapD[t_name] = t_value;