2 #include "trg/cdc/HandleRoot.h"
4 #include "HandleRoot.h"
8 #include <TBranchObject.h>
19 namespace HandleRoot {
22 std::map<std::string, TVectorD*>& eventMapTVectorD,
23 std::map<std::string, TClonesArray*>& trackMapTVectorD
26 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
29 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
35 std::map<std::string, TClonesArray*>& trackMapTVectorD
38 map<string, TVectorD*> eventMapTVectorD;
39 initializeEvent(eventMapTVectorD, trackMapTVectorD);
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
53 (*runTree) =
new TTree((prefix +
"runTree").c_str(),
"run");
55 for (map<string, double>::iterator it = constMapD.begin(); it != constMapD.end(); ++it) {
56 runMapTVectorD[prefix +
"Double" + (*it).first] =
new TVectorD(1, &(*it).second);
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]);
64 for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
65 (*runTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
72 (*eventTree) =
new TTree((prefix +
"eventTree").c_str(),
"event");
75 for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
76 eventMapTVectorD[prefix +
"EventDouble" + (*it).first] =
new TVectorD(1);
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);
84 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
85 (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
89 for (map<string, double>::iterator it = trackMapD.begin(); it != trackMapD.end(); ++it) {
90 trackMapTVectorD[prefix +
"TrackDouble" + (*it).first] =
new TClonesArray(
"TVectorD");
93 for (map<
string, vector<double> >::iterator it = trackMapV.begin(); it != trackMapV.end(); ++it) {
94 trackMapTVectorD[prefix +
"TrackVector" + (*it).first] =
new TClonesArray(
"TVectorD");
97 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
98 (*eventTree)->Branch(it->first.c_str(), &it->second, 32000, 0);
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
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,
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
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);
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]);
136 trackMapD[
"iSave"]++;
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
145 for (map<string, double>::iterator it = eventMapD.begin(); it != eventMapD.end(); ++it) {
146 eventMapTVectorD[prefix +
"EventDouble" + (*it).first]->Use(1, &(*it).second);
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]);
155 void writeRoot(TFile* file)
164 std::map<std::string, TVectorD*>& runMapTVectorD,
165 std::map<std::string, TVectorD*>& eventMapTVectorD,
166 std::map<std::string, TClonesArray*>& trackMapTVectorD
169 for (map<string, TVectorD*>::iterator it = runMapTVectorD.begin(); it != runMapTVectorD.end(); ++it) {
172 for (map<string, TVectorD*>::iterator it = eventMapTVectorD.begin(); it != eventMapTVectorD.end(); ++it) {
175 for (map<string, TClonesArray*>::iterator it = trackMapTVectorD.begin(); it != trackMapTVectorD.end(); ++it) {
181 std::map<std::string, TVectorD*>& runMapTVectorD,
182 std::map<std::string, TClonesArray*>& trackMapTVectorD
185 std::map<std::string, TVectorD*> eventMapTVectorD;
186 terminateRoot(runMapTVectorD, eventMapTVectorD, trackMapTVectorD);
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
196 (*runTree) = (TTree*)file->Get((prefix +
"runTree").c_str());
197 (*eventTree) = (TTree*)file->Get((prefix +
"eventTree").c_str());
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]);
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();
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>") {
216 eventMapTVectorD[t_branchName] =
new TVectorD();
217 (*eventTree)->SetBranchAddress(t_branchName.c_str(), &eventMapTVectorD[t_branchName]);
219 cout <<
"[Warning] HandleRoot::initializeBranches => Type of branch " << t_branchName <<
" is unkown." << endl;
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
229 map<string, TVectorD*> eventMapTVectorD;
230 initializeBranches(prefix, file, runTree, eventTree,
231 runMapTVectorD, eventMapTVectorD, trackMapTVectorD
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
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];
253 cout <<
"[Error] HandleRoot::getRunValues => t_type: " << t_type <<
" is unknown." << endl;
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
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];
276 cout <<
"[Error] HandleRoot::getEventValues => t_type: " << t_type <<
" is unknown." << endl;
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
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];
298 cout <<
"[Error] HandleRoot::getTrackValues => t_type: " << t_type <<
" is unknown." << endl;
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)
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]);
314 size_t t_find = t_name.find(
"_");
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()));
320 if (trackMapV.find(t_vectorName) == trackMapV.end()) {
321 trackMapV[t_vectorName] = vector<double> (1);
324 int nIncrease = t_vectorIndex + 1 - trackMapV[t_vectorName].size();
326 for (
int iIncrease = 0; iIncrease < nIncrease; iIncrease++) trackMapV[t_vectorName].push_back(0);
329 trackMapV[t_vectorName][t_vectorIndex] = t_value;
332 trackMapD[t_name] = t_value;