GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
OakRidge.cc
Go to the documentation of this file.
1
12
13#include <fstream>
14#include <iostream>
15#include <sstream>
16#include <stdlib.h>
17#include <string.h>
18
19#include <TClass.h>
20#include <TKey.h>
21
22#include "OakRidge.hh"
24
25namespace GamR {
26namespace ORNL {
28{
29 this->iIH = 0;
30 this->iIB = 0;
31 this->iDG = 0;
32 this->iNP = 0;
33 this->iIPX = 0;
34 this->iIPY = 0;
35 this->iLXD = 0;
36 this->iLXG = 0;
37 this->iLYD = 0;
38 this->iLYG = 0;
39 this->iNUPM = 0;
40 this->iIAUX = 0;
41 this->iJAUX = 0;
42 this->fileName = "default-name";
43}
44
46{
47 this->iIH = 0;
48 this->iIB = 0;
49 this->iDG = 0;
50 this->iNP = 0;
51 this->iIPX = 0;
52 this->iIPY = 0;
53 this->iLXD = 0;
54 this->iLXG = 0;
55 this->iLYD = 0;
56 this->iLYG = 0;
57 this->iNUPM = 0;
58 this->iIAUX = 0;
59 this->iJAUX = 0;
60 this->fileName.clear();
61}
62
63BananaGate::BananaGate(const char *name, int n /*=0*/) : TCutG(name, n)
64{
65 this->iIH = 0;
66 this->iIB = 0;
67 this->iDG = 0;
68 this->iNP = 0;
69 this->iIPX = 0;
70 this->iIPY = 0;
71 this->iLXD = 0;
72 this->iLXG = 0;
73 this->iLYD = 0;
74 this->iLYG = 0;
75 this->iNUPM = 0;
76 this->iIAUX = 0;
77 this->iJAUX = 0;
78 this->fileName = "default-name";
79}
80
81BananaGate::BananaGate(const TCutG &cutg, TH2 &hist, int iBID, int iHID, const char *fileName) : TCutG(cutg)
82{
83 this->iIH = iHID;
84 this->iIB = iBID;
85 this->iDG = 0;
86 this->iNP = cutg.GetN();
87 this->iIPX = 0;
88 this->iIPY = 0;
89 this->iLXD = hist.GetNbinsX();
90 this->iLXG = hist.GetNbinsX();
91 this->iLYD = hist.GetNbinsY();
92 this->iLYG = hist.GetNbinsY();
93 this->iNUPM = 0;
94 this->iIAUX = 0;
95 this->iJAUX = 0;
96 this->fileName = fileName;
97}
98
99void BananaGate::SetFileName(const char *fileName)
100{
101 this->fileName = fileName;
102}
103
105{
106 /* returns string that is the relevant part of the *.ban file */
107 char cInpLine[81], cFileName[27], cHID[7], cBID[7], cDG[7], cNP[7];
108 std::sprintf(cFileName, "%-26s", (this->fileName).c_str());
109 std::sprintf(cHID, "%6d", this->iIH);
110 std::sprintf(cBID, "%6d", this->iIB);
111 std::sprintf(cDG, "%6d", this->iDG);
112 std::sprintf(cNP, "%6d", this->iNP);
113 std::sprintf(cInpLine, "INP %s%s%s%s%s%*c", cFileName, cHID, cBID, cDG, cNP, 26, ' ');
114
115 char cTitleLine[81];
116 std::sprintf(cTitleLine, "TIT %-76s", this->GetTitle());
117
118 char cGateLine[81];
119 char cIPX[7], cIPY[7], cLXD[7], cLXG[7], cLYD[7], cLYG[7];
120 char cNUPM[9], cIAUX[9], cJAUX[9];
121 std::sprintf(cIPX, "%6d", this->iIPX);
122 std::sprintf(cIPY, "%6d", this->iIPY);
123 std::sprintf(cLXD, "%6d", this->iLXD);
124 std::sprintf(cLXG, "%6d", this->iLXG);
125 std::sprintf(cLYD, "%6d", this->iLYD);
126 std::sprintf(cLYG, "%6d", this->iLYG);
127 std::sprintf(cNUPM, "%8d", this->iNUPM);
128 std::sprintf(cIAUX, "%8d", this->iIAUX);
129 std::sprintf(cJAUX, "%8d", this->iJAUX);
130 std::sprintf(cGateLine, "GATE %s%s%s%s%s%s%s%s%s%*c", cIPX, cIPY, cLXD, cLXG, cLYD, cLYG, cNUPM, cIAUX, cJAUX, 15,
131 ' ');
132
133 char cCXYLine[721] = "CXY ";
134 int nPoint = 0;
135 int nPoints = this->iNP;
136 /* 9x CXY lines */
137 for (int i = 0; i < 9; i++) {
138 if (i > 0)
139 std::sprintf(cCXYLine, "%sCXY ", cCXYLine);
140 int nLinePoints = 0;
141 while (nPoint < nPoints) { /* point loop */
142 double dx;
143 double dy;
144 this->GetPoint(nPoint, dx, dy);
145 int x = (int)dx;
146 int y = (int)dy;
147 if (x < 0)
148 x = 0;
149 if (y < 0)
150 y = 0;
151 /* check if we have room on this line */
152 if (nLinePoints < 7) {
153 /* append to this line */
154 std::sprintf(cCXYLine, "%s%5d%5d", cCXYLine, x, y);
155 /* go to next point */
156 nLinePoints = nLinePoints + 1;
157 nPoint = nPoint + 1;
158 } else { /* no room on the line */
159 std::sprintf(cCXYLine, "%s%*c", cCXYLine, 75 - nLinePoints * 10, ' ');
160 /* go to next line */
161 break;
162 }
163 }
164 if (nPoint == nPoints)
165 std::sprintf(cCXYLine, "%s%*c", cCXYLine, 75 - nLinePoints * 10, ' ');
166 }
167
168 char cBanLine[961];
169 std::sprintf(cBanLine, "%s%s%s%s", cInpLine, cTitleLine, cGateLine, cCXYLine);
170 std::string sBanLine(cBanLine);
171 return sBanLine;
172}
173
182int his2root(const char *cHisFileName)
183{
184 std::stringstream ss;
185 std::string line, sListFileName, sHisFileName(cHisFileName), sRootFileName;
186 sListFileName = sHisFileName.substr(0, sHisFileName.length() - 3);
187 sListFileName.append("list");
188 sRootFileName = sHisFileName.substr(0, sHisFileName.length() - 3);
189 sRootFileName.append("root");
190 std::ifstream listFile;
191 listFile.open(sListFileName);
192 std::string first;
193 int details = 0; // flag to mark if we're in the "detailed info" part of the
194 // list file or not
195 int nHists = 0; // counter for number of histograms processed
196
197 int HID; /*histogram ID*/
198 int dim; /* dimension of histogram: assuming 1 or 2 */
199 int hwpc; /* half words per channel */
200 // int len; /* length in characters of array */
201 // int compr; /* compression, not used */
202 int minX; /* minimum (X) value of array */
203 int maxX; /* max value (X) */
204 int minY; /* minimum (Y) value of array */
205 int maxY; /* max value (Y) */
206 std::string title; /* title of histogram */
207 uint32_t datum; /* 64 bit integer to store a single channel */
208
209 FILE *hisFile;
210 int read;
211 hisFile = std::fopen(cHisFileName, "rb");
212 TFile *outFile = new TFile(sRootFileName.c_str(), "recreate");
213
214 while (std::getline(listFile, line)) {
215 ss.clear();
216 ss.str(line);
217 ss >> first;
218 if (details == 1) { /* if past the list of IDs */
219 std::cout << "processing line: " << line << std::endl;
220 HID = stoi(line.substr(0, 5));
221 dim = stoi(line.substr(5, 5));
222 hwpc = stoi(line.substr(10, 4));
223 // compr = stoi(line.substr(23, 8));
224 minX = stoi(line.substr(31, 6));
225 maxX = stoi(line.substr(37, 6));
226 title = line.substr(54);
227
228 if (hwpc > 2) {
229 std::cout << "warning! more than 64 bit integer, expect bad behaviour" << std::endl;
230 }
231 if (dim == 2) { /* 2D histogram */
232 std::getline(listFile, line);
233 ss.clear();
234 ss.str(line);
235 minY = stoi(line.substr(31, 6));
236 maxY = stoi(line.substr(37, 6));
237
238 /* make the histogram */
239 TString sHistName;
240 sHistName.Form("ID%d", HID);
241 TH2D *hist = new TH2D(sHistName.Data(), title.c_str(), maxX - minX + 1, minX, maxX + 1, maxY - minY + 1,
242 minY, maxY + 1);
243
244 /* fill the histogram */
245 for (int iyChannel = 0; iyChannel <= maxY; iyChannel++) {
246 for (int ixChannel = 0; ixChannel <= maxX; ixChannel++) {
247 read = std::fread(&datum, (size_t)hwpc * 2, (size_t)1, hisFile);
248 if (read == 1) {
249 hist->SetBinContent(ixChannel, iyChannel, datum);
250 } else {
251 std::cout << "error occurred in reading data" << std::endl;
252 }
253 }
254 }
255
256 hist->Write();
257 } else { /* 1D histogram */
258 TString sHistName;
259 sHistName.Form("ID%d", HID);
260 TH1D *hist = new TH1D(sHistName.Data(), title.c_str(), maxX - minX + 1, minX, maxX + 1);
261
262 /* fill the histogram */
263 for (int ixChannel = 0; ixChannel <= maxX; ixChannel++) {
264 read = std::fread(&datum, (size_t)hwpc * 2, (size_t)1, hisFile);
265 if (read == 1) {
266 hist->SetBinContent(ixChannel, datum);
267 } else {
268 std::cout << "error occurred in reading data" << std::endl;
269 }
270 }
271
272 hist->Write();
273 }
274
275 nHists = nHists + 1;
276 }
277 if (first == "HID") {
278 details = 1;
279 }
280
281 } // *.list file loop
282 outFile->Close();
283 return nHists;
284} // his2root
285
293BananaGate *readBanana(const char *cBanFileName, int iID)
294{
295 std::stringstream ss;
296 std::string sBanString, line;
297 std::ifstream banFile;
298 banFile.open(cBanFileName);
299 std::getline(banFile, sBanString);
300
301 size_t pos = 0;
302 BananaGate *banGate;
303
304 pos = sBanString.find("INP ", pos);
305 line = sBanString.substr(0, pos);
306 /* check if ID exists */
307 if (line.find(std::to_string(iID)) > line.length()) {
308 std::cout << "ID not present in *.ban file" << std::endl;
309 return NULL;
310 }
311
312 pos = pos + 4;
313 /* find relevant part */
314 while (true) {
315 std::string junk;
316 /* next INP */
317 std::string sBananaGate = sBanString.substr(pos - 4, sBanString.find("INP ", pos + 4) - pos);
318
319 int iTIT = sBananaGate.find("TIT ");
320 int iGATE = sBananaGate.find("GATE ");
321 int iCXY = sBananaGate.find("CXY ");
322 std::string sINPLine = sBananaGate.substr(0, iTIT);
323 std::string sTITLine = sBananaGate.substr(iTIT, iGATE - iTIT);
324 std::string sGATELine = sBananaGate.substr(iGATE, iCXY - iGATE);
325 std::string sCXYLine = sBananaGate.substr(iCXY);
326
327 /*
328 std::cout << sINPLine << std::endl;
329 std::cout << sTITLine << std::endl;
330 std::cout << sGATELine << std::endl;
331 std::cout << sCXYLine << std::endl;
332 */
333
334 /* process first line */
335 std::stringstream ssINP(sINPLine);
336 std::string fileName;
337 int iIH, iIB, iDG, iNP;
338 ssINP >> junk;
339 ssINP >> fileName;
340 ssINP >> iIH;
341 ssINP >> iIB;
342 ssINP >> iDG;
343 ssINP >> iNP;
344
345 if (iIB == iID) {
346 /* found correct Banana gate, process */
347 TString sCutName;
348 sCutName.Form("bID%d", iID);
349 banGate = new BananaGate(sCutName.Data(), iNP);
350 banGate->SetName(sCutName.Data());
351 banGate->Set(iNP);
352
353 banGate->SetIH(iIH);
354 banGate->SetIB(iIB);
355 banGate->SetDG(iDG);
356 banGate->SetNP(iNP);
357 banGate->SetFileName(fileName);
358
359 /* second line */
360 std::string sTitle = sTITLine.substr(4);
361 // std::cout << sTitle << std::endl;
362 banGate->SetTitle(sTitle.c_str());
363
364 /* third line */
365 std::stringstream ssGATE(sGATELine);
366 int iIPX, iIPY, iLXD, iLXG, iLYD, iLYG, iNUPM, iIAUX, iJAUX;
367 ssGATE >> junk;
368 ssGATE >> iIPX;
369 ssGATE >> iIPY;
370 ssGATE >> iLXD;
371 ssGATE >> iLXG;
372 ssGATE >> iLYD;
373 ssGATE >> iLYG;
374 ssGATE >> iNUPM;
375 ssGATE >> iIAUX;
376 ssGATE >> iJAUX;
377
378 banGate->SetIPX(iIPX);
379 banGate->SetIPY(iIPY);
380 banGate->SetLXD(iLXD);
381 banGate->SetLXG(iLXG);
382 banGate->SetLYD(iLYD);
383 banGate->SetLYG(iLYG);
384 banGate->SetNUPM(iNUPM);
385 banGate->SetIAUX(iIAUX);
386 banGate->SetJAUX(iJAUX);
387
388 /* final line: coordinates */
389 std::stringstream ssCXY(sCXYLine);
390 int nPoint = 0;
391 junk.clear();
392 while (true) {
393 ssCXY >> junk;
394 if (junk == "CXY") {
395 continue;
396 } else {
397 int x;
398 int y;
399 x = std::atoi(junk.c_str());
400 ssCXY >> y;
401 banGate->SetPoint(nPoint, x, y);
402 nPoint = nPoint + 1;
403 }
404 if (nPoint == iNP)
405 break; // CXY loop
406 }
407 break; // INP loop
408 } // iIB == iID
409
410 pos = sBanString.find("INP", pos);
411 if (pos >= sBanString.length()) {
412 std::cout << "End of file reached" << std::endl;
413 return NULL;
414 } else {
415 pos = pos + 4; // move past INP characters
416 }
417
418 } // INP loop
419
420 return banGate;
421
422} // readBanana
423
430void readAllBananas(const char *cBanFileName)
431{
432 std::string sBanString;
433 std::string line;
434 std::ifstream banFile;
435 banFile.open(cBanFileName);
436 std::getline(banFile, sBanString);
437
438 BananaGate *banGate;
439
440 line = sBanString.substr(0, sBanString.find("INP"));
441 std::stringstream ss(line);
442
443 while (true) {
444 int iID;
445 ss >> iID;
446 if (iID == 0) {
447 break;
448 }
449 banGate = readBanana(cBanFileName, iID);
450 if (banGate) {
451 banGate->Write();
452 }
453 }
454}
455
462void writeBananas(const char *cBanFileName, std::vector<BananaGate> vBananas)
463{
464
465 /* write header of file */
466 char cHeader[401];
467 int n = 0;
468 for (auto &bananaGate : vBananas) {
469 n = n + 1;
470 /* append to cHeader */
471 if (n == 1) {
472 std::sprintf(cHeader, "%5d", bananaGate.GetIB());
473 } else {
474 std::sprintf(cHeader, "%s%5d", cHeader, bananaGate.GetIB());
475 }
476 }
477 for (int i = n; i < 80; i++) {
478 std::sprintf(cHeader, "%s%5d", cHeader, 0);
479 }
480
481 std::ofstream outFile;
482 outFile.open(cBanFileName);
483 outFile << cHeader;
484
485 for (auto &bananaGate : vBananas) {
486 /* append to file */
487 outFile << bananaGate.WriteBan().c_str();
488 }
489
490 outFile.close();
491}
492
499void writeAllBananas(const char *cBanFileName)
500{
501 TIter next(gDirectory->GetListOfKeys());
502 TKey *key;
503 std::vector<BananaGate> vBananas;
504 while ((key = (TKey *)next())) {
505 TClass *cl = gROOT->GetClass(key->GetClassName());
506 if (!cl->InheritsFrom("GamR::ORNL::BananaGate"))
507 continue;
508 vBananas.push_back(*(BananaGate *)key->ReadObj());
509 }
510 writeBananas(cBanFileName, vBananas);
511}
512
513} // namespace ORNL
514} // namespace GamR
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
ClassImp(GamR::ORNL::BananaGate)
void SetIPY(int iIPY)
Definition OakRidge.hh:50
void SetIB(int iIB)
Definition OakRidge.hh:46
void SetDG(int iDG)
Definition OakRidge.hh:47
void SetLYD(int iLYD)
Definition OakRidge.hh:53
void SetLXD(int iLXD)
Definition OakRidge.hh:51
void SetNP(int iNP)
Definition OakRidge.hh:48
void SetJAUX(int iJAUX)
Definition OakRidge.hh:57
void writeBananas(const char *cBanFileName, std::vector< BananaGate > vBananas)
Definition OakRidge.cc:462
void SetIH(int iIH)
Definition OakRidge.hh:45
void SetLYG(int iLYG)
Definition OakRidge.hh:54
void SetIAUX(int iIAUX)
Definition OakRidge.hh:56
void readAllBananas(const char *cBanFileName)
Definition OakRidge.cc:430
void SetIPX(int iIPX)
Definition OakRidge.hh:49
std::string WriteBan()
Definition OakRidge.cc:104
void SetFileName(const char *fileName)
Definition OakRidge.cc:99
void writeAllBananas(const char *cBanFileName)
Definition OakRidge.cc:499
BananaGate * readBanana(const char *cBanFileName, int iID)
Definition OakRidge.cc:293
void SetNUPM(int iNUPM)
Definition OakRidge.hh:55
void SetLXG(int iLXG)
Definition OakRidge.hh:52
int his2root(const char *cHisFileName)
Definition OakRidge.cc:182
Definition Gain.cc:19