62#include "TRestRawUSTCToSignalProcess.h"
68#include "TTimeStamp.h"
72TRestRawUSTCToSignalProcess::TRestRawUSTCToSignalProcess() {
Initialize(); }
74TRestRawUSTCToSignalProcess::TRestRawUSTCToSignalProcess(
const char* configFilename) {
Initialize(); }
76TRestRawUSTCToSignalProcess::~TRestRawUSTCToSignalProcess() {
93#ifndef Incoherent_Event_Generation
95 if (nBufferedEvent < 1) nBufferedEvent = 1;
100 for (
int n = 0; n < nBufferedEvent + 1; n++) {
101 fEventBuffer.push_back(vector<USTCDataFrame>());
104 fRunOrigin =
fRunInfo->GetRunNumber();
110 if ((!GetNextFrame(frame)) || (!ReadFrameData(frame))) {
111 FixToNextFrame(fInputFiles[fCurrentFile]);
112 if ((!GetNextFrame(frame)) || (!ReadFrameData(frame))) {
113 RESTError <<
"TRestRawUSTCToSignalProcess: Failed to read the first data "
114 "frame in file, may be wrong "
121 fCurrentEvent = frame.evId;
124 if (fCurrentEvent != 0) {
125 RESTWarning <<
"TRestRawUSTCToSignalProcess : first event is not with id 0 !" <<
RESTendl;
126 RESTWarning <<
"The first Id is " << fCurrentEvent <<
". May be input file not the first file?"
137 fSignalEvent->SetOK(
false);
139 if (fEventBuffer[fCurrentBuffer].size() == 0) {
140 RESTDebug <<
"Blank event " << fCurrentEvent <<
" !" <<
RESTendl;
149 fSignalEvent->SetID(fCurrentEvent);
151 RESTDebug <<
"------------------------------------" <<
RESTendl;
152 RESTDebug <<
"Generating event with ID: " << fCurrentEvent <<
RESTendl;
155 USTCDataFrame* frame0 = &fEventBuffer[fCurrentBuffer][0];
157 Long64_t evtTime = frame0->eventTime;
158 tSt.SetNanoSec((fTimeOffset + evtTime) % ((Long64_t)1e9));
159 tSt.SetSec((fTimeOffset + evtTime) / ((Long64_t)1e9));
162 for (
unsigned int i = 0; i < fEventBuffer[fCurrentBuffer].size(); i++) {
163 USTCDataFrame* frame = &fEventBuffer[fCurrentBuffer][i];
164 if (frame->evId == fCurrentEvent && frame->eventTime == evtTime) {
167 for (
int j = 0; j < 512; j++) {
168 sgnl.
AddPoint((Short_t)frame->dataPoint[j]);
170 fSignalEvent->AddSignal(sgnl);
172 RESTDebug <<
"AsAdId, AgetId, chnId, max value: " << frame->boardId <<
", " << frame->chipId
176 RESTWarning <<
"TRestRawUSTCToSignalProcess : unmatched signal frame!" <<
RESTendl;
177 RESTWarning <<
"ID (supposed, received): " << fCurrentEvent <<
", " << frame->evId <<
RESTendl;
178 RESTWarning <<
"Time (supposed, received) : " << evtTime <<
", " << frame->eventTime <<
RESTendl;
180 fSignalEvent->SetOK(
false);
190 cout <<
"------------------------------------" << endl;
193 fSignalEvent->SetTimeStamp(tSt);
194 fSignalEvent->SetRunOrigin(fRunOrigin);
195 fSignalEvent->SetSubRunOrigin(fSubRunOrigin);
205 for (
unsigned int i = 0; i < errorevents.size(); i++) {
206 RESTWarning <<
"Event " << errorevents[i] <<
" contains error !" <<
RESTendl;
208 if (errorevents.size() > 0 && unknownerrors > 0) {
209 RESTWarning <<
"There are also " << unknownerrors <<
" errors from unknown events! " <<
RESTendl;
210 }
else if (unknownerrors > 0) {
211 RESTWarning <<
"There are " << unknownerrors <<
" errors from unknown events! " <<
RESTendl;
217bool TRestRawUSTCToSignalProcess::FillBuffer() {
218#ifdef Incoherent_Event_Generation
219 bool unknowncurrentevent =
false;
220 if (fEventBuffer[fCurrentBuffer].size() > 0) {
221 fCurrentEvent = fEventBuffer[fCurrentBuffer][0].evId;
223 unknowncurrentevent =
true;
228 while (fLastBufferedId < fCurrentEvent + ((
int)fEventBuffer.size() - 1) / 2)
231 bool errortag =
false;
232 bool breaktag =
false;
234 if (!GetNextFrame(frame)) {
237 if (!ReadFrameData(frame)) {
238 RESTWarning <<
"error reading frame data in file " << fCurrentFile <<
RESTendl;
239 FixToNextFrame(fInputFiles[fCurrentFile]);
241 ReadFrameData(frame);
244#ifdef Incoherent_Event_Generation
245 if (unknowncurrentevent) {
246 cout << frame.evId << endl;
247 fCurrentEvent = frame.evId;
248 unknowncurrentevent =
false;
251 if (frame.evId != fCurrentEvent) {
255 if (frame.evId >= fCurrentEvent + ((
int)fEventBuffer.size() - 1) / 2) {
260 if (!AddBuffer(frame)) {
265 if (frame.evId != -1) {
266 if (errorevents.size() == 0) {
267 errorevents.push_back(frame.evId);
269 for (
unsigned int i = 0; i < errorevents.size(); i++) {
270 if (errorevents[i] == frame.evId) {
272 }
else if (i == errorevents.size() - 1) {
273 errorevents.push_back(frame.evId);
285 fLastBufferedId = frame.evId;
289 for (
unsigned int i = 0; i < errorevents.size(); i++) {
290 if (errorevents[i] == fCurrentEvent)
return false;
295bool TRestRawUSTCToSignalProcess::OpenNextFile(USTCDataFrame& frame) {
296 if (fCurrentFile < (
int)fInputFiles.size() - 1)
299 return GetNextFrame(frame);
305bool TRestRawUSTCToSignalProcess::GetNextFrame(USTCDataFrame& frame) {
306 if (fInputFiles[fCurrentFile] ==
nullptr) {
307 return OpenNextFile(frame);
309#ifdef V4_Readout_Format
311 UChar_t Protocol[PROTOCOL_SIZE];
312 if (fread(Protocol, PROTOCOL_SIZE, 1, fInputFiles[fCurrentFile]) != 1 ||
313 feof(fInputFiles[fCurrentFile])) {
314 fclose(fInputFiles[fCurrentFile]);
315 fInputFiles[fCurrentFile] =
nullptr;
316 return OpenNextFile(frame);
318 totalbytesRead += PROTOCOL_SIZE;
320 if (!(Protocol[0] ^ 0xac) && !(Protocol[1] ^ 0x0f)) {
323 int flag = Protocol[2] >> 5;
326 memcpy(fEnding, Protocol, PROTOCOL_SIZE);
327 if (fread(fEnding + PROTOCOL_SIZE, ENDING_SIZE - PROTOCOL_SIZE, 1,
328 fInputFiles[fCurrentFile]) != 1 ||
329 feof(fInputFiles[fCurrentFile])) {
330 fclose(fInputFiles[fCurrentFile]);
331 fInputFiles[fCurrentFile] =
nullptr;
332 return OpenNextFile(frame);
334 totalbytesRead += ENDING_SIZE;
335 }
else if (flag & 0x2) {
337 memcpy(fHeader, Protocol, PROTOCOL_SIZE);
338 if (fread(fHeader + PROTOCOL_SIZE, HEADER_SIZE - PROTOCOL_SIZE, 1,
339 fInputFiles[fCurrentFile]) != 1 ||
340 feof(fInputFiles[fCurrentFile])) {
341 fclose(fInputFiles[fCurrentFile]);
342 fInputFiles[fCurrentFile] =
nullptr;
343 return OpenNextFile(frame);
345 totalbytesRead += HEADER_SIZE;
348 memcpy(frame.data, Protocol, PROTOCOL_SIZE);
349 if (fread(frame.data + PROTOCOL_SIZE, DATA_SIZE - PROTOCOL_SIZE, 1,
350 fInputFiles[fCurrentFile]) != 1 ||
351 feof(fInputFiles[fCurrentFile])) {
352 fclose(fInputFiles[fCurrentFile]);
353 fInputFiles[fCurrentFile] =
nullptr;
354 return OpenNextFile(frame);
356 totalbytesRead += DATA_SIZE;
364 if (fread(frame.data, DATA_SIZE, 1, fInputFiles[fCurrentFile]) != 1 || feof(fInputFiles[fCurrentFile])) {
365 fclose(fInputFiles[fCurrentFile]);
366 fInputFiles[fCurrentFile] =
nullptr;
367 return OpenNextFile(frame);
369 totalbytesRead += DATA_SIZE;
371 if (frame.data[0] * 0x100 + frame.data[1] != 0xEEEE) {
372 RESTarning <<
"wrong header!" <<
RESTendl;
381void TRestRawUSTCToSignalProcess::FixToNextFrame(FILE* f) {
382 if (f ==
nullptr)
return;
383 UChar_t buffer[PROTOCOL_SIZE];
386 if (fread(buffer, PROTOCOL_SIZE, 1, f) != 1 || feof(f)) {
390#ifdef V4_Readout_Format
391 if (!(buffer[0] ^ 0xac) && !(buffer[1] ^ 0x0f)) {
392 int flag = buffer[2] >> 5;
395 memcpy(fHeader, buffer, PROTOCOL_SIZE);
396 if (fread(fHeader + PROTOCOL_SIZE, HEADER_SIZE - PROTOCOL_SIZE, 1,
397 fInputFiles[fCurrentFile]) != 1 ||
398 feof(fInputFiles[fCurrentFile])) {
404 RESTWarning <<
"successfully switched to next frame ( + " << n <<
" byte)" <<
RESTendl;
410 if (!(buffer[0] ^ 0xff) && !(buffer[1] ^ 0xff) && !(buffer[2] ^ 0xff) && !(buffer[3] ^ 0xff)) {
411 RESTWarning <<
"successfully switched to next frame ( + " << n <<
" byte)" <<
RESTendl;
420bool TRestRawUSTCToSignalProcess::ReadFrameData(USTCDataFrame& frame) {
421#ifdef V3_Readout_Format_Long
427 frame.boardId = frame.data[2] & 0x0F;
428 frame.chipId = (frame.data[3] & 0xF0) / 16 - 10;
429 frame.readoutType = frame.data[3] & 0x0F;
430 Long64_t tmp = (Long64_t)frame.data[5] * 0x10000 + (Long64_t)frame.data[6] * 0x100 +
431 (Long64_t)frame.data[7];
432 frame.eventTime = tmp * 0x100000000 + (Long64_t)frame.data[8] * 0x1000000 +
433 (Long64_t)frame.data[9] * 0x10000 + (Long64_t)frame.data[10] * 0x100 +
434 (Long64_t)frame.data[11];
435 frame.channelId = frame.data[12] * 0x100 + frame.data[13];
436 frame.evId = (frame.data[16] & 0x7F) * 0x1000000 + frame.data[17] * 0x10000 + frame.data[18] * 0x100 +
440 frame.signalId = frame.boardId * 4 * 64 + frame.chipId * 64 + frame.channelId;
443#ifdef V3_Readout_Format_Short
447 frame.boardId = frame.data[2] & 0x0F;
448 frame.chipId = (frame.data[3] & 0xF0) / 16 - 10;
449 frame.readoutType = frame.data[3] & 0x0F;
450 Long64_t tmp = (Long64_t)frame.data[4] * 0x1000000 + (Long64_t)frame.data[5] * 0x10000 +
451 (Long64_t)frame.data[6] * 0x100 + (Long64_t)frame.data[7];
452 frame.eventTime = tmp;
453 frame.channelId = frame.data[8] * 0x100 + frame.data[9];
454 frame.evId = frame.data[10] * 256 + frame.data[11];
456 frame.signalId = frame.boardId * 4 * 64 + frame.chipId * 64 + frame.channelId;
460#ifdef V4_Readout_Format
470 frame.readoutType = fHeader[5] & 0xc0;
472 int t_high = fHeader[10] * 0x100 + fHeader[11];
473 int t_mid = fHeader[8] * 0x100 + fHeader[9];
474 int t_low = fHeader[6] * 0x100 + fHeader[7];
475 Long64_t tmp = (Long64_t)t_high * 0x100000000 + (Long64_t)t_mid * 0x10000 + (Long64_t)t_low;
476 frame.eventTime = tmp;
478 int id_high = fHeader[14] * 0x100 + fHeader[15];
479 int id_low = fHeader[12] * 0x100 + fHeader[13];
480 frame.evId = id_high * 0x10000 + id_low;
487 frame.boardId = (frame.data[4] & 0x3e) >> 1;
488 frame.chipId = (frame.data[4] & 0x01) * 2 + (frame.data[5] >> 7);
489 frame.channelId = frame.data[5] & 0x7f;
491 frame.signalId = frame.boardId * 4 * 68 + frame.chipId * 68 + frame.channelId;
493 fChannelOffset.insert(frame.boardId * 4 * 68 + frame.chipId * 68);
497 for (
int i = 0; i < 512; i++) {
498 int pos = i * 2 + DATA_OFFSET;
499 frame.dataPoint[i] = (int)((frame.data[pos] & 0x0F) * 0x100 + frame.data[pos + 1]);
517bool TRestRawUSTCToSignalProcess::AddBuffer(USTCDataFrame& frame) {
518#ifdef Incoherent_Event_Generation
519 if (frame.evId == fCurrentEvent) {
520 fEventBuffer[fCurrentBuffer].push_back(frame);
522 int pos = 1 + fCurrentBuffer;
523 if (pos >= fEventBuffer.size()) pos -= fEventBuffer.size();
524 fEventBuffer[pos].push_back(frame);
527 if (frame.evId >= fCurrentEvent + (
int)fEventBuffer.size()) {
528 RESTWarning <<
"too large event id for buffering!" <<
RESTendl;
529 RESTWarning <<
"this may due to the inconherence of event id. Increase the "
532 RESTWarning <<
"Current Event, Burrfering event : " << fCurrentEvent <<
", " << frame.evId
536 if (frame.evId < fCurrentEvent) {
537 RESTWarning <<
"skipping a signal from old event!" <<
RESTendl;
538 RESTWarning <<
"the cause may be that too much events are mixing. Increase the "
541 RESTWarning <<
"Current Event, Burrfering event : " << fCurrentEvent <<
", " << frame.evId
545 size_t pos = frame.evId - fCurrentEvent + fCurrentBuffer;
546 if (pos >= fEventBuffer.size()) pos -= fEventBuffer.size();
547 fEventBuffer[pos].push_back(frame);
553void TRestRawUSTCToSignalProcess::ClearBuffer() {
554 fEventBuffer[fCurrentBuffer].clear();
556 if (fCurrentBuffer >= (
int)fEventBuffer.size()) {
557 fCurrentBuffer -= fEventBuffer.size();
561Bool_t TRestRawUSTCToSignalProcess::EndReading() {
562 for (
const auto& file : fInputFiles) {
563 if (file !=
nullptr) {
568 for (
const auto& eventBuffer : fEventBuffer) {
569 if (!eventBuffer.empty()) {
TRestRun * fRunInfo
< Pointer to TRestRun object where to find metadata.
A base class for any REST event.
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.
void AddPoint(Short_t)
Adds a new point to the end of the signal data array.
void Initialize() override
Making default settings.
A process to read USTC electronic binary format files generated.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
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_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Int_t StringToInteger(std::string in)
Gets an integer from a string.