diff --git a/inc/TRestRawMultiCoBoAsAdToSignalProcess.h b/inc/TRestRawMultiCoBoAsAdToSignalProcess.h index bc675409..8fdd8b3c 100644 --- a/inc/TRestRawMultiCoBoAsAdToSignalProcess.h +++ b/inc/TRestRawMultiCoBoAsAdToSignalProcess.h @@ -126,8 +126,9 @@ class TRestRawMultiCoBoAsAdToSignalProcess : public TRestRawToSignalProcess { bool ReadFrameHeader(CoBoHeaderFrame& Frame); - bool ReadFrameDataP(FILE* f, CoBoHeaderFrame& hdr); + bool ReadFrameDataP(int fileid, CoBoHeaderFrame& hdr); bool ReadFrameDataF(CoBoHeaderFrame& hdr); + void CloseFile(int fileid); Bool_t EndReading(); diff --git a/inc/TRestRawSignal.h b/inc/TRestRawSignal.h index 05f4d36a..69229a5d 100644 --- a/inc/TRestRawSignal.h +++ b/inc/TRestRawSignal.h @@ -190,7 +190,7 @@ class TRestRawSignal : public TObject { Int_t GetMinPeakBin(); - Bool_t IsACDSaturation(int Nflat = 3, int OverThres = 3600); + Bool_t IsADCSaturation(int Nflat = 3); void GetDifferentialSignal(TRestRawSignal* diffSgnl, Int_t smearPoints); diff --git a/inc/TRestRawToSignalProcess.h b/inc/TRestRawToSignalProcess.h index 9280ab4e..715ba8d4 100644 --- a/inc/TRestRawToSignalProcess.h +++ b/inc/TRestRawToSignalProcess.h @@ -38,6 +38,7 @@ class TRestRawToSignalProcess : public TRestEventProcess { Double_t tStart; Long64_t totalBytesReaded; Long64_t totalBytes; + Int_t fMaxWaitTimeEOF; // wait for xx seconds at eof before really closing the binary file TRestRawSignalEvent* fSignalEvent = 0; //! #ifndef __CINT__ @@ -70,6 +71,7 @@ class TRestRawToSignalProcess : public TRestEventProcess { void SetRunOrigin(Int_t run_origin) { fRunOrigin = run_origin; } void SetSubRunOrigin(Int_t sub_run_origin) { fSubRunOrigin = sub_run_origin; } + bool FRead(void* ptr, size_t size, size_t n, FILE* file); void LoadConfig(std::string cfgFilename, std::string name = ""); diff --git a/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx b/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx index 5af3af61..4d4290e4 100644 --- a/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx +++ b/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx @@ -55,8 +55,6 @@ ///
/// -// int counter = 0; - #include "TRestRawMultiCoBoAsAdToSignalProcess.h" #include "TRestDataBase.h" @@ -153,13 +151,10 @@ Bool_t TRestRawMultiCoBoAsAdToSignalProcess::AddInputFile(string file) { fileerrors.push_back(0); int i = fHeaderFrame.size() - 1; - if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + if (!FRead(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i])) { + CloseFile(i); return kFALSE; } - totalBytesReaded += 256; if (!ReadFrameHeader(fHeaderFrame[i])) { cout << "error when reading frame header in file " << i << " \"" << fInputFileNames[i] << "\"" << endl; @@ -167,9 +162,7 @@ Bool_t TRestRawMultiCoBoAsAdToSignalProcess::AddInputFile(string file) { fHeaderFrame[i].Show(); cout << endl; GetChar(); - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + CloseFile(i); return false; } @@ -182,7 +175,7 @@ TRestEvent* TRestRawMultiCoBoAsAdToSignalProcess::ProcessEvent(TRestEvent* evInp fSignalEvent->Initialize(); if (EndReading()) { - return NULL; + return nullptr; } if (!fillbuffer()) { fSignalEvent->SetOK(false); @@ -238,7 +231,7 @@ TRestEvent* TRestRawMultiCoBoAsAdToSignalProcess::ProcessEvent(TRestEvent* evInp fSignalEvent->SetSubRunOrigin(0); // cout << fSignalEvent->GetNumberOfSignals() << endl; - // if( fSignalEvent->GetNumberOfSignals( ) == 0 ) return NULL; + // if( fSignalEvent->GetNumberOfSignals( ) == 0 ) return nullptr; return fSignalEvent; } @@ -259,14 +252,11 @@ void TRestRawMultiCoBoAsAdToSignalProcess::EndProcess() { bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { // if the file is opened but not read, read header frame for (int i = 0; i < fInputFiles.size(); i++) { - if (fInputFiles[i] != NULL && ftell(fInputFiles[i]) == 0) { - if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + if (fInputFiles[i] != nullptr && ftell(fInputFiles[i]) == 0) { + if (!FRead(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i])) { + CloseFile(i); return kFALSE; } - totalBytesReaded += 256; if (!ReadFrameHeader(fHeaderFrame[i])) { cout << "error when reading frame header in file " << i << " \"" << fInputFileNames[i] << "\"" << endl; @@ -274,9 +264,7 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { fHeaderFrame[i].Show(); cout << endl; GetChar(); - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + CloseFile(i); return false; } } @@ -292,7 +280,7 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { // loop for each file for (int i = 0; i < fHeaderFrame.size(); i++) { - if (fInputFiles[i] == NULL) { + if (fInputFiles[i] == nullptr) { continue; } @@ -313,32 +301,24 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { unsigned int type = fHeaderFrame[i].frameType; if (fHeaderFrame[i].frameHeader[0] == 0x08 && type == 1) // partial readout { - ReadFrameDataP(fInputFiles[i], fHeaderFrame[i]); + ReadFrameDataP(i, fHeaderFrame[i]); } else if (fHeaderFrame[i].frameHeader[0] == 0x08 && type == 2) // full readout { - if (fread(frameDataF, 2048, 136, fInputFiles[i]) != 136 || feof(fInputFiles[i])) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + if (!FRead(frameDataF, 2048, 136, fInputFiles[i])) { + CloseFile(i); break; } - totalBytesReaded += 278528; ReadFrameDataF(fHeaderFrame[i]); } else { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + CloseFile(i); return false; } // reading next header - if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || feof(fInputFiles[i])) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; // maximum of unsigned int + if (!FRead(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i])) { + CloseFile(i); break; } - totalBytesReaded += 256; if (!ReadFrameHeader(fHeaderFrame[i])) { warning << "Event " << fCurrentEvent << " : error when reading next frame header" << endl; warning << "in file " << i << " \"" << fInputFileNames[i] << "\"" << endl; @@ -350,11 +330,9 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { fVerboseLevel = REST_Silent; for (int k = 0; k < 1088; k++) // fullreadoutsize(278528)/headersize(256)=1088 { - if (fread(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i]) != 1 || - feof(fInputFiles[i])) { + if (!FRead(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i])) { break; } - totalBytesReaded += 256; if (ReadFrameHeader(fHeaderFrame[i])) { fVerboseLevel = tmp; warning << "Successfully found next header (EventId : " << fHeaderFrame[i].eventIdx @@ -368,9 +346,7 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::fillbuffer() { } } if (!found) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; // maximum of unsigned int + CloseFile(i); } } } @@ -493,7 +469,7 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameHeader(CoBoHeaderFrame& HdrF return true; } -bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(FILE* f, CoBoHeaderFrame& hdr) { +bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(int fileid, CoBoHeaderFrame& hdr) { unsigned int i; int j; unsigned int agetIdx, chanIdx, buckIdx, sample, chTmp; @@ -510,12 +486,10 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(FILE* f, CoBoHeaderFra if (size > 256) { unsigned int NBuckTotal = (size - 256) / 4; for (i = 0; i < NBuckTotal; i++) { - if ((fread(frameDataP, 4, 1, f)) != 1 || feof(f)) { - fclose(f); - f = NULL; + if (!FRead(frameDataP, 4, 1, fInputFiles[fileid])) { + CloseFile(fileid); return kFALSE; } - totalBytesReaded += 4; // total: 4bytes, 32 bits // 11 111111|1 1111111|11 11 1111|11111111 // agetIdx chanIdx buckIdx unused samplepoint @@ -546,6 +520,14 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(FILE* f, CoBoHeaderFra return true; } +void TRestRawMultiCoBoAsAdToSignalProcess::CloseFile(int i) { + if(fInputFiles[i] != nullptr) { + fclose(fInputFiles[i]); + fInputFiles[i] = nullptr; + fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + } +} + bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataF(CoBoHeaderFrame& hdr) { int i; int j; @@ -644,7 +626,7 @@ Bool_t TRestRawMultiCoBoAsAdToSignalProcess::EndReading() { } for (int n = 0; n < nFiles; n++) { - if (fInputFiles[n] != NULL) { + if (fInputFiles[n] != nullptr) { return false; } } diff --git a/src/TRestRawSignal.cxx b/src/TRestRawSignal.cxx index 2554105a..86a2192f 100644 --- a/src/TRestRawSignal.cxx +++ b/src/TRestRawSignal.cxx @@ -528,17 +528,21 @@ Int_t TRestRawSignal::GetMinPeakBin() { /////////////////////////////////////////////// /// \brief It returns whether the signal has ADC saturation /// -Bool_t TRestRawSignal::IsACDSaturation(int Nflat, int OverThres) { +Bool_t TRestRawSignal::IsADCSaturation(int Nflat) { + if (Nflat <= 0) return false; // GetMaxPeakBin() will always find the first max peak bin if mulitple // bins are in same max value. int index = GetMaxPeakBin(); + Short_t value = fSignalData[index]; - bool sat = true; + bool sat = false; if (index + Nflat <= fSignalData.size()) { for (int i = index; i < index + Nflat; i++) { - if (fSignalData[index + i] > OverThres && fSignalData[index] == fSignalData[index + i]) { - } else { - sat = false; + if (fSignalData[i] != value) { + break; + } + if (i == index + Nflat - 1) { + sat = true; } } } diff --git a/src/TRestRawSignalAnalysisProcess.cxx b/src/TRestRawSignalAnalysisProcess.cxx index 8f6f41c8..fe55cc9c 100644 --- a/src/TRestRawSignalAnalysisProcess.cxx +++ b/src/TRestRawSignalAnalysisProcess.cxx @@ -396,7 +396,7 @@ TRestEvent* TRestRawSignalAnalysisProcess::ProcessEvent(TRestEvent* evInput) { risetime[sgnl->GetID()] = sgnl->GetRiseTime(); peak_time[sgnl->GetID()] = sgnl->GetMaxPeakBin(); npointsot[sgnl->GetID()] = sgnl->GetPointsOverThreshold().size(); - if (sgnl->IsACDSaturation()) saturatedchnId.push_back(sgnl->GetID()); + if (sgnl->IsADCSaturation()) saturatedchnId.push_back(sgnl->GetID()); } SetObservableValue("pointsoverthres_map", npointsot); diff --git a/src/TRestRawToSignalProcess.cxx b/src/TRestRawToSignalProcess.cxx index b7c97467..80461823 100644 --- a/src/TRestRawToSignalProcess.cxx +++ b/src/TRestRawToSignalProcess.cxx @@ -109,6 +109,7 @@ void TRestRawToSignalProcess::InitFromConfigFile() { fElectronicsType = GetParameter("electronics"); fShowSamples = StringToInteger(GetParameter("showSamples", "10")); fMinPoints = StringToInteger(GetParameter("minPoints", "512")); + fMaxWaitTimeEOF = StringToInteger(GetParameter("maxWaitTimeEOF", "0")); PrintMetadata(); @@ -240,3 +241,34 @@ Bool_t TRestRawToSignalProcess::GoToNextFile() { } return false; } + +// custom fread method which has retry times for the file to be written when reading +bool TRestRawToSignalProcess::FRead(void* ptr, size_t size, size_t n, FILE* file) { + if (file == nullptr || ptr == nullptr) return false; + if (size == 0 || n == 0) return false; + int nwaits = 0; + size_t chunksReaded = 0; + size_t chunksRemaining = n; + while (1) { + int pos = ftell(file); + int reads = fread((char*)ptr + chunksReaded * size, size, chunksRemaining, file); + totalBytesReaded += reads * size; + if (reads != chunksRemaining || feof(file)) { + if (reads == 0) { + nwaits++; + } else { + // In case it reads something partially + nwaits = 1; + chunksReaded += reads; + chunksRemaining -= reads; + } + if (nwaits > fMaxWaitTimeEOF) return false; + + std::this_thread::sleep_for(std::chrono::seconds(1)); + fseek(file, ftell(file), 0); + } else { + return true; + } + } + return false; +}