58#include "TRestRawMultiCoBoAsAdToSignalProcess.h"
60#include "TRestDataBase.h"
66#include "TTimeStamp.h"
70TRestRawMultiCoBoAsAdToSignalProcess::TRestRawMultiCoBoAsAdToSignalProcess() {
Initialize(); }
72TRestRawMultiCoBoAsAdToSignalProcess::TRestRawMultiCoBoAsAdToSignalProcess(
const char* configFilename) {
76TRestRawMultiCoBoAsAdToSignalProcess::~TRestRawMultiCoBoAsAdToSignalProcess() {
86Bool_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,
120vector<int> fileerrors;
132 fRunOrigin =
fRunInfo->GetRunNumber();
135 if (
fRunInfo->GetStartTimestamp() != 0) {
142Bool_t TRestRawMultiCoBoAsAdToSignalProcess::AddInputFile(
const string& file) {
143 if (file.find(
".graw") == string::npos) {
146 if (TRestRawToSignalProcess::AddInputFile(file)) {
147 CoBoHeaderFrame hdrtmp;
149 fileerrors.push_back(0);
152 if (fread(
fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) {
153 fclose(fInputFiles[i]);
154 fInputFiles[i] =
nullptr;
158 totalbytesRead += 256;
160 cout <<
"error when reading frame header in file " << i <<
" \"" << fInputFileNames[i] <<
"\""
162 cout <<
"event id " <<
fCurrentEvent + 1 <<
". The file will be closed" << endl;
166 fclose(fInputFiles[i]);
167 fInputFiles[i] =
nullptr;
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);
202 if ((Double_t)tSt == 0) tSt = data->timeStamp;
204 for (
int m = 0; m < 272; m++) {
205 if (data->chHit[m]) {
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 <<
", "
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);
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;
255bool 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;
265 totalbytesRead += 256;
267 cout <<
"error when reading frame header in file " << i <<
" \"" << fInputFileNames[i] <<
"\""
269 cout <<
"event id " <<
fCurrentEvent + 1 <<
". The file will be closed" << endl;
273 fclose(fInputFiles[i]);
274 fInputFiles[i] =
nullptr;
284 for (
unsigned int i = 1; i <
fHeaderFrame.size(); i++) {
290 for (
unsigned int i = 0; i <
fHeaderFrame.size(); i++) {
291 if (fInputFiles[i] ==
nullptr) {
302 cout <<
"TRestRawMultiCoBoAsAdToSignalProcess: retrieving frame header in "
304 << i <<
" (" << fInputFileNames[i] <<
")" << endl;
311 if (
fHeaderFrame[i].frameHeader[0] == 0x08 && type == 1)
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;
322 totalbytesRead += 278528;
325 fclose(fInputFiles[i]);
326 fInputFiles[i] =
nullptr;
332 if (fread(
fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) {
333 fclose(fInputFiles[i]);
334 fInputFiles[i] =
nullptr;
338 totalbytesRead += 256;
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 totalbytesRead += 256;
358 RESTWarning <<
"Successfully found next header (EventId : "
365 fSignalEvent->SetOK(
false);
370 fclose(fInputFiles[i]);
371 fInputFiles[i] =
nullptr;
381bool 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) {
495bool 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)) {
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));
540 dataf.asadId = asadid;
541 dataf.evId = eventid;
542 dataf.timeStamp = eveTimeStamp;
547bool 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;
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));
602 dataf.asadId = asadid;
603 dataf.evId = eventid;
604 dataf.timeStamp = eveTimeStamp;
609Bool_t TRestRawMultiCoBoAsAdToSignalProcess::EndReading() {
610 for (
auto& m : fDataFrame) {
611 m.second.finished =
true;
620 for (
int n = 0; n < nFiles; n++) {
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) {
TRestRun * fRunInfo
< Pointer to TRestRun object where to find metadata.
A base class for any REST event.
UChar_t frameDataF[278528]
///for partial readout data frame
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void Initialize() override
Making default settings.
int fCurrentEvent
///reserves a header frame for each file
TTimeStamp fStartTimeStamp
///for full readout data frame
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)
std::vector< CoBoHeaderFrame > fHeaderFrame
///asadId, dataframe
Double_t GetMaxValue()
Returns the maximum value found in the data points. It includes baseline correction.
void Initialize()
Initialization of TRestRawSignal members.
void SetSignalID(Int_t sID)
It sets the id number of the signal.
Double_t GetData(Int_t n) const
It returns the data value of point n including baseline correction.
void AddPoint(Short_t)
Adds a new point to the end of the signal data array.
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.