diff --git a/FSUDAQ.cpp b/FSUDAQ.cpp index 4c34c8f..bcdd1b6 100644 --- a/FSUDAQ.cpp +++ b/FSUDAQ.cpp @@ -36,7 +36,6 @@ FSUDAQ::FSUDAQ(QWidget *parent) : QMainWindow(parent){ scope = nullptr; digiSettings = nullptr; singleHistograms = nullptr; - histThread = nullptr; onlineAnalyzer = nullptr; runTimer = new QTimer(); breakAutoRepeat = true; @@ -323,17 +322,6 @@ FSUDAQ::~FSUDAQ(){ if( scope ) delete scope; - if( histThread ){ - - if( !histThread->isStopped() ){ - histThread->Stop(); - histThread->quit(); - histThread->wait(); - } - - delete histThread; - } - if( singleHistograms ) delete singleHistograms; if( onlineAnalyzer ) delete onlineAnalyzer; @@ -743,12 +731,6 @@ void FSUDAQ::OpenDigitizers(){ } singleHistograms = new SingleSpectra(digi, nDigi, rawDataPath); - histThread = new TimingThread(this); - histThread->SetWaitTimeinSec(singleHistograms->GetMaxFillTime()/1000.); - connect(histThread, &TimingThread::timeUp, this, [=](){ - if( singleHistograms == nullptr && !singleHistograms->IsFillHistograms()) return; - singleHistograms->FillHistograms(); - }); LogMsg("====== " + QString("Done. Opened %1 digitizer(s).").arg(nDigi) + " ====="); @@ -784,22 +766,12 @@ void FSUDAQ::CloseDigitizers(){ scalarThread->exit(); CleanUpScalar(); - if( histThread){ - histThread->Stop(); - histThread->quit(); - histThread->wait(); - - delete histThread; - histThread = nullptr; - } - if( onlineAnalyzer ){ onlineAnalyzer->close(); delete onlineAnalyzer; onlineAnalyzer = nullptr; } - if( singleHistograms ){ singleHistograms->close(); delete singleHistograms; @@ -1054,6 +1026,9 @@ void FSUDAQ::OpenScalar(){ void FSUDAQ::UpdateScalar(){ DebugPrint("%s", "FSUDAQ"); + + // printf("================== FSUDAQ::%s\n", __func__); + if( digi == nullptr ) return; if( scalar == nullptr ) return; //if( !scalar->isVisible() ) return; @@ -1066,6 +1041,7 @@ void FSUDAQ::UpdateScalar(){ uint64_t totalFileSize = 0; for( unsigned int iDigi = 0; iDigi < nDigi; iDigi++){ + // printf("======== digi-%d\n", iDigi); if( digi[iDigi]->IsBoardDisabled() ) continue; uint32_t acqStatus = digi[iDigi]->GetACQStatusFromMemory(); @@ -1107,6 +1083,7 @@ void FSUDAQ::UpdateScalar(){ } // digiMTX[iDigi].unlock(); + // printf("============= end of FSUDAQ::%s\n", __func__); } @@ -1181,7 +1158,7 @@ void FSUDAQ::StartACQ(){ } lbScalarACQStatus->setText("ACQ On"); - if( singleHistograms != nullptr ) histThread->start(); + if( singleHistograms != nullptr ) singleHistograms->startWork(); bnStartACQ->setEnabled(false); bnStartACQ->setStyleSheet(""); @@ -1256,13 +1233,9 @@ void FSUDAQ::StopACQ(){ } if( onlineAnalyzer ) onlineAnalyzer->StopThread(); + if( singleHistograms ) singleHistograms->stopWork(); + - if( singleHistograms && histThread->isRunning()){ - histThread->Stop(); - histThread->quit(); - histThread->wait(); - singleHistograms->ClearInternalDataCount(); - } lbScalarACQStatus->setText("ACQ Off"); bnStartACQ->setEnabled(true); @@ -1755,19 +1728,14 @@ void FSUDAQ::OpenScope(){ } if( digiSettings ) digiSettings->setEnabled(!onOff); - - if( singleHistograms ){ - if( onOff) { - histThread->start(); + if( singleHistograms ) { + if( onOff ) { + singleHistograms->startWork(); }else{ - if( histThread->isRunning()){ - histThread->Stop(); - histThread->quit(); - histThread->wait(); - singleHistograms->ClearInternalDataCount(); - } + singleHistograms->stopWork(); } } + }); connect(scope, &Scope::UpdateScaler, this, &FSUDAQ::UpdateScalar); diff --git a/FSUDAQ.h b/FSUDAQ.h index 751dd93..90423ec 100644 --- a/FSUDAQ.h +++ b/FSUDAQ.h @@ -194,7 +194,7 @@ private: //@----- SingleSpectra SingleSpectra * singleHistograms; - TimingThread * histThread; + // TimingThread * histThread; //@----- Analyzer Analyzer * onlineAnalyzer; diff --git a/SingleSpectra.cpp b/SingleSpectra.cpp index 346af9f..a6de8ee 100644 --- a/SingleSpectra.cpp +++ b/SingleSpectra.cpp @@ -18,8 +18,6 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD isSignalSlotActive = true; setWindowTitle("Single Histograms"); - - //setWindowFlags( this->windowFlags() & ~Qt::WindowCloseButtonHint ); //====== resize window if screen too small QScreen * screen = QGuiApplication::primaryScreen(); @@ -29,7 +27,6 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD }else{ setGeometry(0, 0, 1000, 800); } - // setGeometry(0, 0, 1000, 800); QWidget * layoutWidget = new QWidget(this); setCentralWidget(layoutWidget); @@ -174,11 +171,10 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD }); - QCheckBox * chkIsFillHistogram = new QCheckBox("Fill Histograms", this); + chkIsFillHistogram = new QCheckBox("Fill Histograms", this); ctrlLayout->addWidget(chkIsFillHistogram, 0, 8); - connect(chkIsFillHistogram, &QCheckBox::stateChanged, this, [=](int state){ fillHistograms = state;}); chkIsFillHistogram->setChecked(false); - fillHistograms = false; + isFillingHistograms = false; QLabel * lbSettingPath = new QLabel( settingPath , this); ctrlLayout->addWidget(lbSettingPath, 1, 0, 1, 6); @@ -237,10 +233,28 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD ClearInternalDataCount(); + workerThread = new QThread(this); + histWorker = new HistWorker(this); + timer = new QTimer(this); + + histWorker->moveToThread(workerThread); + + // Setup the timer to trigger every second + connect(timer, &QTimer::timeout, histWorker, &HistWorker::FillHistograms); + workerThread->start(); + } SingleSpectra::~SingleSpectra(){ DebugPrint("%s", "SingleSpectra"); + + timer->stop(); + + if( workerThread->isRunning() ){ + workerThread->quit(); + workerThread->wait(); + } + SaveSetting(); for( unsigned int i = 0; i < nDigi; i++ ){ @@ -309,82 +323,6 @@ void SingleSpectra::ChangeHistView(){ } -void SingleSpectra::FillHistograms(){ - // DebugPrint("%s", "SingleSpectra"); - if( !fillHistograms ) return; - - timespec t0, t1; - - QVector randomDigiList = generateNonRepeatedCombination(nDigi); - - // qDebug() << randomDigiList; - - for( int i = 0; i < nDigi; i++){ - int ID = randomDigiList[i]; - - QVector randomChList = generateNonRepeatedCombination(digi[ID]->GetNumInputCh()); - - // qDebug() << randomChList; - - // digiMTX[ID].lock(); - - // digi[ID]->GetData()->PrintAllData(); - - clock_gettime(CLOCK_REALTIME, &t0); - for( int k = 0; k < digi[ID]->GetNumInputCh(); k ++ ){ - int ch = randomChList[k]; - int lastIndex = digi[ID]->GetData()->GetDataIndex(ch); - // printf("--- ch %2d | last index %d \n", ch, lastIndex); - if( lastIndex < 0 ) continue; - - int loopIndex = digi[ID]->GetData()->GetLoopIndex(ch); - - int temp1 = lastIndex + loopIndex * digi[ID]->GetData()->GetDataSize(); - int temp2 = lastFilledIndex[ID][ch] + loopFilledIndex[ID][ch] * digi[ID]->GetData()->GetDataSize() + 1; - - // printf("loopIndx : %d | ID now : %d, ID old : %d \n", loopIndex, temp1, temp2); - - if( temp1 <= temp2 ) continue; - - if( temp1 - temp2 > digi[ID]->GetData()->GetDataSize() ) { //DefaultDataSize = 10k - temp2 = temp1 - digi[ID]->GetData()->GetDataSize(); - lastFilledIndex[ID][ch] = lastIndex; - lastFilledIndex[ID][ch] = loopIndex - 1; - } - - // printf("ch %d | regulated ID now %d new %d | last fill idx %d\n", ch, temp2, temp1, lastFilledIndex[ID][ch]); - - for( int j = 0 ; j <= temp1 - temp2; j ++){ - lastFilledIndex[ID][ch] ++; - if( lastFilledIndex[ID][ch] > digi[ID]->GetData()->GetDataSize() ) { - lastFilledIndex[ID][ch] = 0; - loopFilledIndex[ID][ch] ++; - } - - uShort data = digi[ID]->GetData()->GetEnergy(ch, lastFilledIndex[ID][ch]); - - // printf(" ch: %d, last fill idx : %d | %d \n", ch, lastFilledIndex[ID][ch], data); - - hist[ID][ch]->Fill( data ); - if( digi[i]->GetDPPType() == DPPTypeCode::DPP_PSD_CODE ){ - uShort e2 = digi[ID]->GetData()->GetEnergy2(ch, lastFilledIndex[ID][ch]); - // printf("%u \n", e2); - hist[ID][ch]->Fill( e2, 1); - } - hist2D[ID]->Fill(ch, data); - } - if( histVisibility[ID][ch] ) hist[ID][ch]->UpdatePlot(); - - clock_gettime(CLOCK_REALTIME, &t1); - if( t1.tv_nsec - t0.tv_nsec + (t1.tv_sec - t0.tv_sec)*1e9 > maxFillTimePerDigi * 1e6 ) break; - } - - if( hist2DVisibility[ID] ) hist2D[ID]->UpdatePlot(); - // digiMTX[ID].unlock(); - - } -} - void SingleSpectra::SaveSetting(){ DebugPrint("%s", "SingleSpectra"); diff --git a/SingleSpectra.h b/SingleSpectra.h index 721f05c..491bb99 100644 --- a/SingleSpectra.h +++ b/SingleSpectra.h @@ -20,6 +20,7 @@ #include "Histogram1D.h" #include "Histogram2D.h" +class HistWorker; //Forward decalration //^==================================================== //^==================================================== @@ -31,8 +32,8 @@ public: ~SingleSpectra(); void ClearInternalDataCount(); - void SetFillHistograms(bool onOff) { fillHistograms = onOff;} - bool IsFillHistograms() const {return fillHistograms;} + // void SetFillHistograms(bool onOff) { fillHistograms = onOff;} + // bool IsFillHistograms() const {return fillHistograms;} void LoadSetting(); void SaveSetting(); @@ -42,20 +43,39 @@ public: QVector generateNonRepeatedCombination(int size); -public slots: - void FillHistograms(); - void ChangeHistView(); -private: + /// This should be private.... + int lastFilledIndex[MaxNDigitizer][MaxNChannels]; + int loopFilledIndex[MaxNDigitizer][MaxNChannels]; + bool histVisibility[MaxNDigitizer][MaxNChannels]; + bool hist2DVisibility[MaxNDigitizer]; + unsigned short maxFillTimePerDigi; + + bool isFillingHistograms; + Histogram1D * hist[MaxNDigitizer][MaxNChannels]; + Histogram2D * hist2D[MaxNDigitizer]; Digitizer ** digi; unsigned short nDigi; - Histogram1D * hist[MaxNDigitizer][MaxNChannels]; - Histogram2D * hist2D[MaxNDigitizer]; + void startWork(){ + printf("timer start\n"); + timer->start(maxFillTimeinMilliSec); + } + void stopWork(){ + printf("timer stop\n"); + timer->stop(); + ClearInternalDataCount(); + } + + QCheckBox * chkIsFillHistogram; + +public slots: + // void FillHistograms(); + void ChangeHistView(); + +private: - bool histVisibility[MaxNDigitizer][MaxNChannels]; - bool hist2DVisibility[MaxNDigitizer]; RComboBox * cbDivision; @@ -66,17 +86,110 @@ private: QGridLayout * histLayout; int oldBd, oldCh; - int lastFilledIndex[MaxNDigitizer][MaxNChannels]; - int loopFilledIndex[MaxNDigitizer][MaxNChannels]; - - bool fillHistograms; - QString settingPath; unsigned short maxFillTimeinMilliSec; - unsigned short maxFillTimePerDigi; bool isSignalSlotActive; + QThread * workerThread; + HistWorker * histWorker; + QTimer * timer; + }; + +//^#======================================================== HistWorker +class HistWorker : public QObject{ + Q_OBJECT +public: + HistWorker(SingleSpectra * parent): SS(parent){} + +public slots: + void FillHistograms(){ + + // printf("%s | %d %d \n", __func__, SS->chkIsFillHistogram->checkState(), SS->isFillingHistograms); + if( SS->chkIsFillHistogram->checkState() == Qt::Unchecked ) return; + if( SS->isFillingHistograms) return; + + SS->isFillingHistograms = true; + timespec t0, t1; + + QVector randomDigiList = SS->generateNonRepeatedCombination(SS->nDigi); + + // qDebug() << randomDigiList; + + for( int i = 0; i < SS->nDigi; i++){ + int ID = randomDigiList[i]; + + QVector randomChList = SS->generateNonRepeatedCombination(SS->digi[ID]->GetNumInputCh()); + + // qDebug() << randomChList; + // digiMTX[ID].lock(); + // digi[ID]->GetData()->PrintAllData(); + + clock_gettime(CLOCK_REALTIME, &t0); + for( int k = 0; k < SS->digi[ID]->GetNumInputCh(); k ++ ){ + int ch = randomChList[k]; + int lastIndex = SS->digi[ID]->GetData()->GetDataIndex(ch); + if( lastIndex < 0 ) continue; + printf("--- ch %2d | last index %d \n", ch, lastIndex); + + int loopIndex = SS->digi[ID]->GetData()->GetLoopIndex(ch); + + int temp1 = lastIndex + loopIndex * SS->digi[ID]->GetData()->GetDataSize(); + int temp2 = SS->lastFilledIndex[ID][ch] + SS->loopFilledIndex[ID][ch] * SS->digi[ID]->GetData()->GetDataSize() + 1; + + // printf("loopIndx : %d | ID now : %d, ID old : %d \n", loopIndex, temp1, temp2); + + if( temp1 <= temp2 ) continue; + + if( temp1 - temp2 > SS->digi[ID]->GetData()->GetDataSize() ) { //DefaultDataSize = 10k + temp2 = temp1 - SS->digi[ID]->GetData()->GetDataSize(); + SS->lastFilledIndex[ID][ch] = lastIndex; + SS->lastFilledIndex[ID][ch] = loopIndex - 1; + } + + // printf("ch %d | regulated ID now %d new %d | last fill idx %d\n", ch, temp2, temp1, lastFilledIndex[ID][ch]); + + for( int j = 0 ; j <= temp1 - temp2; j ++){ + SS->lastFilledIndex[ID][ch] ++; + if( SS->lastFilledIndex[ID][ch] > SS->digi[ID]->GetData()->GetDataSize() ) { + SS->lastFilledIndex[ID][ch] = 0; + SS->loopFilledIndex[ID][ch] ++; + } + + uShort data = SS->digi[ID]->GetData()->GetEnergy(ch, SS->lastFilledIndex[ID][ch]); + + // printf(" ch: %d, last fill idx : %d | %d \n", ch, lastFilledIndex[ID][ch], data); + + SS->hist[ID][ch]->Fill( data ); + if( SS->digi[i]->GetDPPType() == DPPTypeCode::DPP_PSD_CODE ){ + uShort e2 = SS->digi[ID]->GetData()->GetEnergy2(ch, SS->lastFilledIndex[ID][ch]); + // printf("%u \n", e2); + SS->hist[ID][ch]->Fill( e2, 1); + } + SS->hist2D[ID]->Fill(ch, data); + } + if( SS->histVisibility[ID][ch] ) SS->hist[ID][ch]->UpdatePlot(); + + clock_gettime(CLOCK_REALTIME, &t1); + if( t1.tv_nsec - t0.tv_nsec + (t1.tv_sec - t0.tv_sec)*1e9 > SS->maxFillTimePerDigi * 1e6 ) break; + } + + if( SS->hist2DVisibility[ID] ) SS->hist2D[ID]->UpdatePlot(); + // digiMTX[ID].unlock(); + + } + + SS->isFillingHistograms = false; + emit workDone(); + } + +signals: + void workDone(); + +private: + SingleSpectra * SS; +}; + #endif \ No newline at end of file