58 #include "TRestRawMultiCoBoAsAdToSignalProcess.h"
60 #include "TRestDataBase.h"
66 #include "TTimeStamp.h"
70 TRestRawMultiCoBoAsAdToSignalProcess::TRestRawMultiCoBoAsAdToSignalProcess() { Initialize(); }
72 TRestRawMultiCoBoAsAdToSignalProcess::TRestRawMultiCoBoAsAdToSignalProcess(
const char* configFilename) {
76 TRestRawMultiCoBoAsAdToSignalProcess::~TRestRawMultiCoBoAsAdToSignalProcess() {
83 SetSectionName(this->ClassName());
86 Bool_t TRestRawMultiCoBoAsAdToSignalProcess::InitializeStartTimeStampFromFilename(TString fName) {
89 int year, month, day, hour, minute, second, millisecond;
91 const Ssiz_t fnOffset = fName.Index(
".graw");
93 if (fName.Length() != fnOffset + 5 || fnOffset < 28) {
94 cout <<
"Input binary file name type unknown!" << endl;
98 if (fName[fnOffset - 24] !=
'-' || fName[fnOffset - 21] !=
'-' || fName[fnOffset - 18] !=
'T' ||
99 fName[fnOffset - 15] !=
':' || fName[fnOffset - 12] !=
':' || fName[fnOffset - 9] !=
'.' ||
100 fName[fnOffset - 5] !=
'_') {
101 cout <<
"Input binary file name unknown!" << endl;
105 year = (int)(fName[fnOffset - 28] - 48) * 1000 + (int)(fName[fnOffset - 27] - 48) * 100 +
106 (int)(fName[fnOffset - 26] - 48) * 10 + (int)(fName[fnOffset - 25] - 48) * 1;
107 month = (int)(fName[fnOffset - 23] - 48) * 10 + (int)(fName[fnOffset - 22] - 48) * 1;
108 day = (int)(fName[fnOffset - 20] - 48) * 10 + (int)(fName[fnOffset - 19] - 48) * 1;
109 hour = (int)(fName[fnOffset - 17] - 48) * 10 + (int)(fName[fnOffset - 16] - 48) * 1;
110 minute = (int)(fName[fnOffset - 14] - 48) * 10 + (int)(fName[fnOffset - 13] - 48) * 1;
111 second = (int)(fName[fnOffset - 11] - 48) * 10 + (int)(fName[fnOffset - 10] - 48) * 1;
112 millisecond = (int)(fName[fnOffset - 8] - 48) * 100 + (int)(fName[fnOffset - 7] - 48) * 10 +
113 (int)(fName[fnOffset - 6] - 48) * 1;
115 fStartTimeStamp.Set(year, month, day, hour, minute, second, millisecond * 1000000, kTRUE,
120 vector<int> fileerrors;
132 fRunOrigin = fRunInfo->GetRunNumber();
135 if (fRunInfo->GetStartTimestamp() != 0) {
136 fStartTimeStamp = TTimeStamp(fRunInfo->GetStartTimestamp());
139 totalBytesReaded = 0;
142 Bool_t TRestRawMultiCoBoAsAdToSignalProcess::AddInputFile(
const string& file) {
143 if (file.find(
".graw") == string::npos) {
146 if (TRestRawToSignalProcess::AddInputFile(file)) {
147 CoBoHeaderFrame hdrtmp;
148 fHeaderFrame.push_back(hdrtmp);
149 fileerrors.push_back(0);
151 int i = fHeaderFrame.size() - 1;
152 if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) {
153 fclose(fInputFiles[i]);
154 fInputFiles[i] =
nullptr;
155 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
158 totalBytesReaded += 256;
159 if (!ReadFrameHeader(fHeaderFrame[i])) {
160 cout <<
"error when reading frame header in file " << i <<
" \"" << fInputFileNames[i] <<
"\""
162 cout <<
"event id " << fCurrentEvent + 1 <<
". The file will be closed" << endl;
163 fHeaderFrame[i].Show();
166 fclose(fInputFiles[i]);
167 fInputFiles[i] =
nullptr;
168 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
184 fSignalEvent->SetOK(
false);
191 cout <<
"TRestRawMultiCoBoAsAdToSignalProcess: Generating event with ID: " << fCurrentEvent << endl;
196 map<int, CoBoDataFrame>::iterator it;
197 it = fDataFrame.begin();
199 while (it != fDataFrame.end()) {
200 CoBoDataFrame* data = &(it->second);
201 if (data->evId == fCurrentEvent) {
202 if ((Double_t)tSt == 0) tSt = data->timeStamp;
204 for (
int m = 0; m < 272; m++) {
205 if (data->chHit[m]) {
207 signal.SetSignalID(m + data->asadId * 272);
209 for (
int j = 0; j < 512; j++) signal.AddPoint((Short_t)data->data[m][j]);
211 fSignalEvent->AddSignal(signal);
214 cout <<
"AgetId, chnId, first value, max value: " << m / 68 <<
", " << m % 68 <<
", "
215 << signal.GetData(0) <<
", " << signal.GetMaxValue() << endl;
220 for (
int m = 0; m < 272; m++) data->chHit[m] = kFALSE;
226 cout <<
"TRestRawMultiCoBoAsAdToSignalProcess: event time is : " << tSt << endl;
227 cout <<
"TRestRawMultiCoBoAsAdToSignalProcess: " << fSignalEvent->GetNumberOfSignals()
228 <<
" signals added" << endl;
229 cout <<
"------------------------------------" << endl;
231 fSignalEvent->SetTimeStamp(tSt);
232 fSignalEvent->SetID(fCurrentEvent);
233 fSignalEvent->SetRunOrigin(0);
234 fSignalEvent->SetSubRunOrigin(0);
243 for (
unsigned int i = 0; i < fileerrors.size(); i++) {
244 if (fileerrors[i] > 0) {
245 RESTWarning <<
"Found " << fileerrors[i] <<
" error frame headers in file " << i << RESTendl;
246 RESTWarning <<
"\"" << fInputFileNames[i] <<
"\"" << RESTendl;
255 bool TRestRawMultiCoBoAsAdToSignalProcess::FillBuffer() {
257 for (
unsigned int i = 0; i < fInputFiles.size(); i++) {
258 if (fInputFiles[i] && ftell(fInputFiles[i]) == 0) {
259 if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) {
260 fclose(fInputFiles[i]);
261 fInputFiles[i] =
nullptr;
262 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
265 totalBytesReaded += 256;
266 if (!ReadFrameHeader(fHeaderFrame[i])) {
267 cout <<
"error when reading frame header in file " << i <<
" \"" << fInputFileNames[i] <<
"\""
269 cout <<
"event id " << fCurrentEvent + 1 <<
". The file will be closed" << endl;
270 fHeaderFrame[i].Show();
273 fclose(fInputFiles[i]);
274 fInputFiles[i] =
nullptr;
275 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
283 unsigned int evt = fHeaderFrame[0].eventIdx;
284 for (
unsigned int i = 1; i < fHeaderFrame.size(); i++) {
285 if (fHeaderFrame[i].eventIdx < evt) evt = fHeaderFrame[i].eventIdx;
290 for (
unsigned int i = 0; i < fHeaderFrame.size(); i++) {
291 if (fInputFiles[i] ==
nullptr) {
300 while (fCurrentEvent >= 0 && fHeaderFrame[i].eventIdx == (
unsigned int)fCurrentEvent) {
302 cout <<
"TRestRawMultiCoBoAsAdToSignalProcess: retrieving frame header in "
304 << i <<
" (" << fInputFileNames[i] <<
")" << endl;
306 fHeaderFrame[i].Show();
310 unsigned int type = fHeaderFrame[i].frameType;
311 if (fHeaderFrame[i].frameHeader[0] == 0x08 && type == 1)
313 ReadFrameDataP(fInputFiles[i], fHeaderFrame[i]);
314 }
else if (fHeaderFrame[i].frameHeader[0] == 0x08 && type == 2)
316 if (fread(frameDataF, 2048, 136, fInputFiles[i]) != 136 || feof(fInputFiles[i])) {
317 fclose(fInputFiles[i]);
318 fInputFiles[i] =
nullptr;
319 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
322 totalBytesReaded += 278528;
323 ReadFrameDataF(fHeaderFrame[i]);
325 fclose(fInputFiles[i]);
326 fInputFiles[i] =
nullptr;
327 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
332 if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) {
333 fclose(fInputFiles[i]);
334 fInputFiles[i] =
nullptr;
335 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
338 totalBytesReaded += 256;
339 if (!ReadFrameHeader(fHeaderFrame[i])) {
340 RESTWarning <<
"Event " << fCurrentEvent <<
" : error when reading next frame header"
342 RESTWarning <<
"in file " << i <<
" \"" << fInputFileNames[i] <<
"\"" << RESTendl;
344 RESTWarning <<
"trying to skip this event and find next header..." << RESTendl;
349 for (
int k = 0; k < 1088; k++)
351 if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 ||
352 feof(fInputFiles[i])) {
355 totalBytesReaded += 256;
356 if (ReadFrameHeader(fHeaderFrame[i])) {
358 RESTWarning <<
"Successfully found next header (EventId : "
359 << fHeaderFrame[i].eventIdx <<
")" << RESTendl;
361 fHeaderFrame[i].Show();
365 fSignalEvent->SetOK(
false);
370 fclose(fInputFiles[i]);
371 fInputFiles[i] =
nullptr;
372 fHeaderFrame[i].eventIdx = (
unsigned int)4294967295;
381 bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameHeader(CoBoHeaderFrame& HdrFrame) {
382 UChar_t* Header = &(HdrFrame.frameHeader[0]);
385 (
unsigned int)Header[1] * 0x10000 + (
unsigned int)Header[2] * 0x100 + (
unsigned int)Header[3];
386 HdrFrame.frameSize *= 256;
388 HdrFrame.frameType = (
unsigned int)Header[5] * 0x100 + (
unsigned int)Header[6];
389 HdrFrame.revision = (
unsigned int)Header[7];
390 HdrFrame.headerSize = (
unsigned int)Header[8] * 0x100 + (
unsigned int)Header[9];
391 HdrFrame.itemSize = (
unsigned int)Header[10] * 0x100 + (
unsigned int)Header[11];
392 HdrFrame.nItems = (
unsigned int)Header[12] * 0x1000000 + (
unsigned int)Header[13] * 0x10000 +
393 (
unsigned int)Header[14] * 0x100 + (
unsigned int)Header[15];
394 HdrFrame.eventTime = (Long64_t)Header[16] * 0x10000000000 + (Long64_t)Header[17] * 0x100000000 +
395 (Long64_t)Header[18] * 0x1000000 + (Long64_t)Header[19] * 0x10000 +
396 (Long64_t)Header[20] * 0x100 + (Long64_t)Header[21];
397 HdrFrame.eventTime *= 10;
398 HdrFrame.eventIdx = (
unsigned int)Header[22] * 0x1000000 + (
unsigned int)Header[23] * 0x10000 +
399 (
unsigned int)Header[24] * 0x100 + (
unsigned int)Header[25];
401 HdrFrame.asadIdx = (
unsigned int)Header[27];
402 HdrFrame.readOffset = (
unsigned int)Header[28] * 0x100 + (
unsigned int)Header[29];
403 HdrFrame.status = (
unsigned int)Header[30];
417 if (HdrFrame.frameType == 1) {
418 if (HdrFrame.itemSize != 4) {
419 RESTWarning <<
"unsupported item size!" << RESTendl;
423 }
else if (HdrFrame.frameType == 2) {
424 if (HdrFrame.itemSize != 2) {
425 RESTWarning <<
"unsupported item size!" << RESTendl;
428 if (HdrFrame.nItems != 139264) {
429 RESTWarning <<
"unsupported nItems!" << RESTendl;
433 RESTWarning <<
"unknown frame type" << RESTendl;
438 if (HdrFrame.revision != 5) {
439 RESTWarning <<
"unsupported revision!" << RESTendl;
444 if (HdrFrame.headerSize != 1) {
445 RESTWarning <<
"unsupported frameHeader size!" << RESTendl;
450 if (HdrFrame.readOffset != 0) {
451 RESTWarning <<
"unsupported readOffset!" << RESTendl;
455 if (HdrFrame.status) {
456 RESTWarning <<
"bad frame!" << RESTendl;
460 if (HdrFrame.nItems * HdrFrame.itemSize + 256 != HdrFrame.frameSize) {
461 RESTWarning <<
"Event " << fCurrentEvent <<
" : item number and frame size unmatch!" << RESTendl;
495 bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(FILE* f, CoBoHeaderFrame& hdr) {
497 unsigned int agetIdx, chanIdx, buckIdx, sample, chTmp;
499 unsigned int asadid = hdr.asadIdx;
500 unsigned int size = hdr.frameSize;
502 unsigned int eventid = hdr.eventIdx;
503 Long64_t time = hdr.eventTime;
504 TTimeStamp eveTimeStamp;
505 CoBoDataFrame& dataf = fDataFrame[asadid];
509 unsigned int NBuckTotal = (size - 256) / 4;
510 for (i = 0; i < NBuckTotal; i++) {
511 if ((fread(frameDataP, 4, 1, f)) != 1 || feof(f)) {
516 totalBytesReaded += 4;
520 agetIdx = (frameDataP[0] >> 6);
521 chanIdx = ((
unsigned int)(frameDataP[0] & 0x3f) * 2 + (frameDataP[1] >> 7));
522 chTmp = agetIdx * 68 + chanIdx;
523 buckIdx = ((
unsigned int)(frameDataP[1] & 0x7f) * 4 + (frameDataP[2] >> 6));
524 sample = ((
unsigned int)(frameDataP[2] & 0x0f) * 0x100 + frameDataP[3]);
527 cout <<
"channel id error! value: " << chTmp << endl;
531 dataf.chHit[chTmp] = kTRUE;
532 dataf.data[chTmp][buckIdx] = sample;
536 eveTimeStamp.SetNanoSec(time % ((Long64_t)1e9));
537 eveTimeStamp.SetSec(time / ((Long64_t)1e9));
538 eveTimeStamp.Add(fStartTimeStamp);
540 dataf.asadId = asadid;
541 dataf.evId = eventid;
542 dataf.timeStamp = eveTimeStamp;
547 bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataF(CoBoHeaderFrame& hdr) {
550 unsigned int agetIdx, chanIdx, chanIdx0, chanIdx1, chanIdx2, chanIdx3, sample, chTmp;
552 unsigned int asadid = hdr.asadIdx;
553 unsigned int eventid = hdr.eventIdx;
554 Long64_t time = hdr.eventTime;
555 TTimeStamp eveTimeStamp;
556 CoBoDataFrame& dataf = fDataFrame[asadid];
559 for (i = 0; i < 512; i++) {
564 for (j = 0; j < 272; j++) {
569 tmpP = (i * 272 + j) * 2;
570 agetIdx = (frameDataF[tmpP] >> 6);
571 sample = ((
unsigned int)(frameDataF[tmpP] & 0x0f) * 0x100 + frameDataF[tmpP + 1]);
576 }
else if (agetIdx == 1) {
579 }
else if (agetIdx == 2) {
592 chTmp = agetIdx * 68 + chanIdx;
593 dataf.chHit[chTmp] = kTRUE;
594 dataf.data[chTmp][i] = sample;
598 eveTimeStamp.SetNanoSec(time % ((Long64_t)1e9));
599 eveTimeStamp.SetSec(time / ((Long64_t)1e9));
600 eveTimeStamp.Add(fStartTimeStamp);
602 dataf.asadId = asadid;
603 dataf.evId = eventid;
604 dataf.timeStamp = eveTimeStamp;
609 Bool_t TRestRawMultiCoBoAsAdToSignalProcess::EndReading() {
610 for (
auto& m : fDataFrame) {
611 m.second.finished =
true;
620 for (
int n = 0; n < nFiles; n++) {
622 fDataFrame[fHeaderFrame[n].asadIdx].finished =
623 (fDataFrame[fHeaderFrame[n].asadIdx].finished &&
624 (fHeaderFrame[n].eventIdx == (
unsigned int)4294967295));
634 for (
auto m : fDataFrame) {
637 if (m.second.asadId == -1)
continue;
639 if (m.second.finished ==
true) {
644 for (
const auto& file : fInputFiles) {
645 if (file !=
nullptr) {
A base class for any REST event.
virtual void Initialize()=0
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void Initialize() override
Making default settings.
void EndProcess() override
To be executed at the end of the run (outside event loop)
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
void Initialize() override
Making default settings.
REST_Verbose_Level
Enumerate of verbose level, containing five levels.
@ REST_Extreme
show everything
@ REST_Info
+show most of the information for each steps
@ REST_Debug
+show the defined debug messages
@ REST_Silent
show minimum information of the software, as well as error messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.