From c2265f3cc6933f178390ddc60695c6cf7c726f4e Mon Sep 17 00:00:00 2001 From: "Ryan@SOLARIS_testStation" Date: Thu, 29 Aug 2024 16:49:52 -0400 Subject: [PATCH] remove TimingThread, Scope::UpdateScope() in Qthread via a worker. ScalarUpdateTimeinMiliSec in macro.h --- ClassDigitizer.cpp | 2 + CustomThreads.h | 59 ++++++++++++++-------------- FSUDAQ.cpp | 4 +- FSUDAQ.h | 2 - Scope.cpp | 74 +++++++++++++++++------------------ Scope.h | 29 +++++++++++++- analyzers/SplitPoleAnalyzer.h | 13 +++--- macro.h | 2 + 8 files changed, 102 insertions(+), 83 deletions(-) diff --git a/ClassDigitizer.cpp b/ClassDigitizer.cpp index bae427c..4fd7db6 100644 --- a/ClassDigitizer.cpp +++ b/ClassDigitizer.cpp @@ -606,6 +606,8 @@ void Digitizer::StopACQ(){ data->ClearBuffer(); data->ClearReferenceTime(); data->ZeroTotalFileSize(); + + ReadACQStatus(); } unsigned int Digitizer::CalByteForBuffer(bool verbose){ diff --git a/CustomThreads.h b/CustomThreads.h index 995f7ca..ea87b1d 100644 --- a/CustomThreads.h +++ b/CustomThreads.h @@ -69,7 +69,6 @@ public: digi->WriteRegister(DPP::SoftwareClear_W, 1); digi->GetData()->ClearData(); } - digi->ReadACQStatus(); digiMTX[ID].unlock(); emit sendMsg("Digi-" + QString::number(digi->GetSerialNumber()) + " ACQ off."); stop = true; @@ -112,34 +111,34 @@ private: }; //^#======================================================= Timing Thread -class TimingThread : public QThread { - Q_OBJECT -public: - TimingThread(QObject * parent = 0 ) : QThread(parent){ - waitTime = 20; // multiple of 100 mili sec - stop = false; - } - bool isStopped() const {return stop;} - void Stop() { this->stop = true;} - void SetWaitTimeinSec(float sec) {waitTime = sec * 10 ;} - float GetWaitTimeinSec() const {return waitTime/10.;} - void DoOnce() {emit timeUp();}; - void run(){ - unsigned int count = 0; - stop = false; - do{ - usleep(100000); - count ++; - if( count % waitTime == 0){ - emit timeUp(); - } - }while(!stop); - } -signals: - void timeUp(); -private: - bool stop; - unsigned int waitTime; -}; +// class TimingThread : public QThread { +// Q_OBJECT +// public: +// TimingThread(QObject * parent = 0 ) : QThread(parent){ +// waitTime = 20; // multiple of 100 mili sec +// stop = false; +// } +// bool isStopped() const {return stop;} +// void Stop() { this->stop = true;} +// void SetWaitTimeinSec(float sec) {waitTime = sec * 10 ;} +// float GetWaitTimeinSec() const {return waitTime/10.;} +// void DoOnce() {emit timeUp();}; +// void run(){ +// unsigned int count = 0; +// stop = false; +// do{ +// usleep(100000); +// count ++; +// if( count % waitTime == 0){ +// emit timeUp(); +// } +// }while(!stop); +// } +// signals: +// void timeUp(); +// private: +// bool stop; +// unsigned int waitTime; +// }; #endif diff --git a/FSUDAQ.cpp b/FSUDAQ.cpp index 6b06d4d..57eb1e9 100644 --- a/FSUDAQ.cpp +++ b/FSUDAQ.cpp @@ -34,7 +34,6 @@ FSUDAQ::FSUDAQ(QWidget *parent) : QMainWindow(parent){ isACQStarted= false; scalar = nullptr; - scalarUpdateTimeMilliSec = 1000; scope = nullptr; digiSettings = nullptr; singleHistograms = nullptr; @@ -1163,7 +1162,7 @@ void FSUDAQ::StartACQ(){ // printf("------------ Go! \n"); // for( unsigned int i = 0; i < nDigi; i++) readDataThread[i]->go(); - if( scalar ) scalarTimer->start(scalarUpdateTimeMilliSec); + if( scalar ) scalarTimer->start(ScalarUpdateinMiliSec); if( !scalar->isVisible() ) { scalar->show(); @@ -1238,7 +1237,6 @@ void FSUDAQ::StopACQ(){ if( chkSaveData->isChecked() ) digi[i]->GetData()->CloseSaveFile(); LogMsg("Digi-" + QString::number(digi[i]->GetSerialNumber()) + " ACQ is stopped." ); QCoreApplication::processEvents(); - digi[i]->ReadACQStatus(); } if( scalar ) scalarTimer->stop(); diff --git a/FSUDAQ.h b/FSUDAQ.h index 750a8c3..b892d1a 100644 --- a/FSUDAQ.h +++ b/FSUDAQ.h @@ -187,8 +187,6 @@ private: QLabel * lbFileSize[MaxNDigitizer]; QLabel * lbTotalFileSize; - unsigned short scalarUpdateTimeMilliSec; - //@----- Run Record QMainWindow * runRecord; QStandardItemModel *model; diff --git a/Scope.cpp b/Scope.cpp index d151237..556aa80 100644 --- a/Scope.cpp +++ b/Scope.cpp @@ -6,7 +6,7 @@ #include #include #include -// #include +#include QVector Scope::TrapezoidFilter(QVector data, int baseLineEndS, int riseTimeS, int flatTopS, float decayTime_ns){ DebugPrint("%s", "Scope"); @@ -41,12 +41,12 @@ QVector Scope::TrapezoidFilter(QVector data, int baseLineEndS, trapezoid.append(QPointF(data[i].x(), sn / decayTime_ns / riseTimeS)); - } - + } return trapezoid; } - +//^======================================================== +//^======================================================== Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataThread, QMainWindow * parent) : QMainWindow(parent){ DebugPrint("%s", "Scope"); this->digi = digi; @@ -64,7 +64,6 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh }else{ setGeometry(0, 0, 1000, 800); } - // setGeometry(0, 0, 1000, 800); enableSignalSlot = false; @@ -100,16 +99,6 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh xaxis->setLabelFormat("%.0f"); xaxis->setTitleText("Time [ns]"); - updateTraceThread = new TimingThread(); - updateTraceThread->SetWaitTimeinSec(ScopeUpdateMiliSec / 1000.); - connect(updateTraceThread, &TimingThread::timeUp, this, &Scope::UpdateScope); - - updateScalarThread = new TimingThread(); - updateScalarThread->SetWaitTimeinSec(2); - connect(updateScalarThread, &TimingThread::timeUp, this, [=](){ - emit UpdateScaler(); - }); - NullThePointers(); //*================================== UI @@ -226,7 +215,7 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh QLabel * lbhints = new QLabel("Type 'r' to restore view, '+/-' Zoom in/out, arrow key to pan.", this); layout->addWidget(lbhints, rowID, 0, 1, 4); - QLabel * lbinfo = new QLabel("Trace updates every " + QString::number(updateTraceThread->GetWaitTimeinSec()) + " sec.", this); + QLabel * lbinfo = new QLabel("Trace updates every " + QString::number(ScopeUpdateMiliSec / 1000.) + " sec.", this); lbinfo->setAlignment(Qt::AlignRight); layout->addWidget(lbinfo, rowID, 6); @@ -288,22 +277,35 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh yaxis->setRange(0, 0xFFF); } + workerThread = new QThread(this); + scopeWorker = new ScopeWorker(this); + scopeTimer = new QTimer(this); + + scopeWorker->moveToThread(workerThread); + + // Setup the timer to trigger every second + connect(scopeTimer, &QTimer::timeout, scopeWorker, [=](){ + scopeWorker->UpdateScope(); + }); + workerThread->start(); + + scalarTimer = new QTimer(this); + connect(scalarTimer, &QTimer::timeout, this, &Scope::UpdateScaler); + enableSignalSlot = true; } - Scope::~Scope(){ DebugPrint("%s", "Scope"); - updateTraceThread->Stop(); - updateTraceThread->quit(); - updateTraceThread->wait(); - delete updateTraceThread; - updateScalarThread->Stop(); - updateScalarThread->quit(); - updateScalarThread->wait(); - delete updateScalarThread; + scopeTimer->stop(); + scalarTimer->stop(); + + if( workerThread->isRunning() ){ + workerThread->quit(); + workerThread->wait(); + } for( int i = 0; i < MaxNumberOfTrace; i++) delete dataTrace[i]; delete plot; @@ -432,8 +434,8 @@ void Scope::StartScope(){ } - updateTraceThread->start(); - updateScalarThread->start(); + scopeTimer->start(ScopeUpdateMiliSec); + scalarTimer->start(ScalarUpdateinMiliSec); bnScopeStart->setEnabled(false); bnScopeStart->setStyleSheet(""); @@ -444,7 +446,7 @@ void Scope::StartScope(){ EnableControl(false); - TellACQOnOff(true); + emit TellACQOnOff(true); isACQStarted = true; @@ -455,13 +457,8 @@ void Scope::StopScope(){ if( !digi ) return; // printf("------ Scope::%s \n", __func__); - updateTraceThread->Stop(); - updateTraceThread->quit(); - updateTraceThread->exit(); - - updateScalarThread->Stop(); - updateScalarThread->quit(); - updateScalarThread->exit(); + scopeTimer->stop(); + scalarTimer->stop(); if( chkSoleRun->isChecked() ){ @@ -477,7 +474,6 @@ void Scope::StopScope(){ digiMTX[ID].lock(); digi[ID]->StopACQ(); - digi[ID]->ReadACQStatus(); digiMTX[ID].unlock(); //restore setting @@ -508,9 +504,9 @@ void Scope::StopScope(){ readDataThread[iDigi]->wait(); readDataThread[iDigi]->SetScopeMode(false); } + digiMTX[iDigi].lock(); digi[iDigi]->StopACQ(); - digi[iDigi]->ReadACQStatus(); //digi[iDigi]->GetData()->PrintAllData(); digiMTX[iDigi].unlock(); @@ -521,6 +517,8 @@ void Scope::StopScope(){ } + runStatus->setStyleSheet(""); // cheated, don;t know why digi[iDigi]->GetACQStatusFromMemory(), sometimes return ACQ on + emit UpdateOtherPanels(); bnScopeStart->setEnabled(true); @@ -533,7 +531,7 @@ void Scope::StopScope(){ EnableControl(true); - TellACQOnOff(false); + emit TellACQOnOff(false); isACQStarted = false; diff --git a/Scope.h b/Scope.h index 2f2c769..d1268db 100644 --- a/Scope.h +++ b/Scope.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,8 @@ #include "CustomThreads.h" #include "CustomWidgets.h" +class ScopeWorker; //Forward declaration + //^==================================================== //^==================================================== class Scope : public QMainWindow{ @@ -87,8 +90,7 @@ private: unsigned short oldCh, oldDigi; ReadDataThread ** readDataThread; - TimingThread * updateTraceThread; - TimingThread * updateScalarThread; + // TimingThread * updateScalarThread; bool enableSignalSlot; @@ -147,8 +149,31 @@ private: //sbGateOffset -> GateOffset //sbTriggerHoldOff ->Trigger Hold Off + QThread * workerThread; + ScopeWorker * scopeWorker; + QTimer * scopeTimer; + QTimer * scalarTimer; + + }; +//^#======================================================== ScopeWorker +class ScopeWorker : public QObject{ + Q_OBJECT +public: + ScopeWorker(Scope * parent): SS(parent){} +public slots: + void UpdateScope(){ + SS->UpdateScope(); + emit workDone(); + } + +signals: + void workDone(); + +private: + Scope * SS; +}; #endif \ No newline at end of file diff --git a/analyzers/SplitPoleAnalyzer.h b/analyzers/SplitPoleAnalyzer.h index 5137997..088a1fe 100644 --- a/analyzers/SplitPoleAnalyzer.h +++ b/analyzers/SplitPoleAnalyzer.h @@ -30,8 +30,7 @@ public: RedefineEventBuilder({0}); // only build for the 0-th digitizer, otherwise, it will build event accross all digitizers tick2ns = digi[0]->GetTick2ns(); SetBackwardBuild(false, 100); // using normal building (acceding in time) or backward building, int the case of backward building, default events to be build is 100. - evtbder = GetEventBuilder(); - evtbder->SetTimeWindow(3000); + mb->SetTimeWindow(3000); //========== use the influx from the Analyzer influx = new InfluxDB("https://fsunuc.physics.fsu.edu/influx/"); @@ -67,8 +66,6 @@ public slots: private: - MultiBuilder *evtbder; - // declaie histograms Histogram2D * hPID; @@ -224,7 +221,7 @@ inline void SplitPole::SetUpCanvas(){ boxLayout->addWidget(sbEventWin, 4, 1); sbEventWin->setValue(3000); connect(sbEventWin, &RSpinBox::returnPressed, this, [=](){ - evtbder->SetTimeWindow(sbEventWin->value()); + mb->SetTimeWindow(sbEventWin->value()); }); chkRunAnalyzer = new QCheckBox("Run Analyzer", this); @@ -352,7 +349,7 @@ inline void SplitPole::UpdateHistograms(){ BuildEvents(); // call the event builder to build events //============ Get events, and do analysis - long eventBuilt = evtbder->eventBuilt; + long eventBuilt = mb->eventBuilt; if( eventBuilt == 0 ) return; //============ Get the cut list, if any @@ -362,12 +359,12 @@ inline void SplitPole::UpdateHistograms(){ unsigned int count[nCut]={0}; //============ Processing data and fill histograms - long eventIndex = evtbder->eventIndex; + long eventIndex = mb->eventIndex; long eventStart = eventIndex - eventBuilt + 1; if(eventStart < 0 ) eventStart += MaxNEvent; for( long i = eventStart ; i <= eventIndex; i ++ ){ - std::vector event = evtbder->events[i]; + std::vector event = mb->events[i]; //printf("-------------- %ld\n", i); hMulti->Fill((int) event.size()); diff --git a/macro.h b/macro.h index d071d78..34c802d 100644 --- a/macro.h +++ b/macro.h @@ -11,6 +11,8 @@ #define MaxRecordLength 0x3fff * 8 #define MaxSaveFileSize 1024 * 1024 * 1024 * 2 +#define ScalarUpdateinMiliSec 1000 // msec + #define MaxDisplayTraceTimeLength 20000 //ns #define ScopeUpdateMiliSec 200 // msec #define MaxNumberOfTrace 5 // in an event