From 251ddf86b7d99b168272976cee66c07d0a5c1e73 Mon Sep 17 00:00:00 2001 From: nkx <1160274182@qq.com> Date: Tue, 29 Jun 2021 15:14:32 +0800 Subject: [PATCH 1/6] TRestRawMultiCoBoAsAdToSignalProcess: real time reading for the binary files --- inc/TRestRawMultiCoBoAsAdToSignalProcess.h | 3 +- inc/TRestRawToSignalProcess.h | 2 + src/TRestRawMultiCoBoAsAdToSignalProcess.cxx | 66 +++++++------------- src/TRestRawToSignalProcess.cxx | 19 ++++++ 4 files changed, 46 insertions(+), 44 deletions(-) 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/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..5f716a60 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; } @@ -260,13 +253,10 @@ 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 (!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; } } @@ -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,12 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(FILE* f, CoBoHeaderFra return true; } +void TRestRawMultiCoBoAsAdToSignalProcess::CloseFile(int i) { + fclose(fInputFiles[i]); + fInputFiles[i] = NULL; + fHeaderFrame[i].eventIdx = (unsigned int)4294967295; +} + bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataF(CoBoHeaderFrame& hdr) { int i; int j; diff --git a/src/TRestRawToSignalProcess.cxx b/src/TRestRawToSignalProcess.cxx index b7c97467..47e8e844 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", "1")); PrintMetadata(); @@ -240,3 +241,21 @@ 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) { + int nwaits = 0; + while (1) { + int reads = fread(ptr, size, n, file); + totalBytesReaded += reads * size; + if (reads != n || feof(file)) { + nwaits++; + if (nwaits > fMaxWaitTimeEOF) return false; + sleep(1); + fseek(file, ftell(file), 0); + } else { + return true; + } + } + return false; +} \ No newline at end of file From 92bf7d9a6ac045280915723f8d67e7131d6fcdaa Mon Sep 17 00:00:00 2001 From: nkx Date: Tue, 29 Jun 2021 19:54:01 +0800 Subject: [PATCH 2/6] TRestRawToSignalProcess::FRead() waits for partial reading --- src/TRestRawToSignalProcess.cxx | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/TRestRawToSignalProcess.cxx b/src/TRestRawToSignalProcess.cxx index 47e8e844..f5315cfe 100644 --- a/src/TRestRawToSignalProcess.cxx +++ b/src/TRestRawToSignalProcess.cxx @@ -244,13 +244,26 @@ Bool_t TRestRawToSignalProcess::GoToNextFile() { // 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 reads = fread(ptr, size, n, file); + int pos = ftell(file); + int reads = fread((char*)ptr + chunksReaded * size, size, chunksRemaining, file); totalBytesReaded += reads * size; - if (reads != n || feof(file)) { - nwaits++; + if (reads != chunksRemaining || feof(file)) { + if (reads == 0) { + nwaits++; + } else { + // In case it reads something partially + nwaits = 0; + chunksReaded += reads; + chunksRemaining -= reads; + } if (nwaits > fMaxWaitTimeEOF) return false; + sleep(1); fseek(file, ftell(file), 0); } else { @@ -258,4 +271,4 @@ bool TRestRawToSignalProcess::FRead(void* ptr, size_t size, size_t n, FILE* file } } return false; -} \ No newline at end of file +} From 0a015ac48f8318b8339e8fe0877e2499123fc332 Mon Sep 17 00:00:00 2001 From: nkx Date: Tue, 29 Jun 2021 19:58:01 +0800 Subject: [PATCH 3/6] TRestRawMultiCoBoAsAdToSignalProcess::CloseFile(): more stable --- src/TRestRawMultiCoBoAsAdToSignalProcess.cxx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx b/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx index 5f716a60..4d4290e4 100644 --- a/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx +++ b/src/TRestRawMultiCoBoAsAdToSignalProcess.cxx @@ -175,7 +175,7 @@ TRestEvent* TRestRawMultiCoBoAsAdToSignalProcess::ProcessEvent(TRestEvent* evInp fSignalEvent->Initialize(); if (EndReading()) { - return NULL; + return nullptr; } if (!fillbuffer()) { fSignalEvent->SetOK(false); @@ -231,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; } @@ -252,7 +252,7 @@ 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 (fInputFiles[i] != nullptr && ftell(fInputFiles[i]) == 0) { if (!FRead(fHeaderFrame[i].frameHeader, 256, 1, fInputFiles[i])) { CloseFile(i); return kFALSE; @@ -280,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; } @@ -521,9 +521,11 @@ bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataP(int fileid, CoBoHeader } void TRestRawMultiCoBoAsAdToSignalProcess::CloseFile(int i) { - fclose(fInputFiles[i]); - fInputFiles[i] = NULL; - fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + if(fInputFiles[i] != nullptr) { + fclose(fInputFiles[i]); + fInputFiles[i] = nullptr; + fHeaderFrame[i].eventIdx = (unsigned int)4294967295; + } } bool TRestRawMultiCoBoAsAdToSignalProcess::ReadFrameDataF(CoBoHeaderFrame& hdr) { @@ -624,7 +626,7 @@ Bool_t TRestRawMultiCoBoAsAdToSignalProcess::EndReading() { } for (int n = 0; n < nFiles; n++) { - if (fInputFiles[n] != NULL) { + if (fInputFiles[n] != nullptr) { return false; } } From 80ff35e094d370fb08e44c2245471889cfba56d0 Mon Sep 17 00:00:00 2001 From: nkx Date: Wed, 30 Jun 2021 15:20:54 +0800 Subject: [PATCH 4/6] TRestRawToSignalProcess: update sleep method --- src/TRestRawToSignalProcess.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TRestRawToSignalProcess.cxx b/src/TRestRawToSignalProcess.cxx index f5315cfe..c50cdbb5 100644 --- a/src/TRestRawToSignalProcess.cxx +++ b/src/TRestRawToSignalProcess.cxx @@ -264,7 +264,7 @@ bool TRestRawToSignalProcess::FRead(void* ptr, size_t size, size_t n, FILE* file } if (nwaits > fMaxWaitTimeEOF) return false; - sleep(1); + std::this_thread::sleep_for(std::chrono::seconds(1)); fseek(file, ftell(file), 0); } else { return true; From 1c299a6182920a1a75100e2eaa4ed61ac37a23f0 Mon Sep 17 00:00:00 2001 From: nkx Date: Wed, 30 Jun 2021 15:35:42 +0800 Subject: [PATCH 5/6] TRestRawToSignalProcess: default waiting time at eof is now 0 to return previous behavier(no-waiting) --- src/TRestRawToSignalProcess.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TRestRawToSignalProcess.cxx b/src/TRestRawToSignalProcess.cxx index c50cdbb5..80461823 100644 --- a/src/TRestRawToSignalProcess.cxx +++ b/src/TRestRawToSignalProcess.cxx @@ -109,7 +109,7 @@ void TRestRawToSignalProcess::InitFromConfigFile() { fElectronicsType = GetParameter("electronics"); fShowSamples = StringToInteger(GetParameter("showSamples", "10")); fMinPoints = StringToInteger(GetParameter("minPoints", "512")); - fMaxWaitTimeEOF = StringToInteger(GetParameter("maxWaitTimeEOF", "1")); + fMaxWaitTimeEOF = StringToInteger(GetParameter("maxWaitTimeEOF", "0")); PrintMetadata(); @@ -258,7 +258,7 @@ bool TRestRawToSignalProcess::FRead(void* ptr, size_t size, size_t n, FILE* file nwaits++; } else { // In case it reads something partially - nwaits = 0; + nwaits = 1; chunksReaded += reads; chunksRemaining -= reads; } From dfb0bb3f4810b633ff2733ce60d1888bb7e5b7fc Mon Sep 17 00:00:00 2001 From: nkx <1160274182@qq.com> Date: Sun, 12 Sep 2021 21:24:50 +0800 Subject: [PATCH 6/6] fixed TRestRawSignal::IsADCSaturation --- inc/TRestRawSignal.h | 2 +- src/TRestRawSignal.cxx | 14 +++++++++----- src/TRestRawSignalAnalysisProcess.cxx | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) 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/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);