From 3b9d47bbefc6b229ab3cd68cf386e68daa32913a Mon Sep 17 00:00:00 2001 From: splitPoleDAQ Date: Fri, 19 May 2023 16:23:04 -0400 Subject: [PATCH] when ACQ start, fill histograms --- CanvasClass.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++------ CanvasClass.h | 12 ++++++ CustomWidgets.h | 31 +++++++++++----- FSUDAQ.cpp | 29 +++++++++++++-- FSUDAQ.h | 1 + Scope.cpp | 1 - 6 files changed, 148 insertions(+), 24 deletions(-) diff --git a/CanvasClass.cpp b/CanvasClass.cpp index c2f17df..f1ad2f4 100644 --- a/CanvasClass.cpp +++ b/CanvasClass.cpp @@ -5,6 +5,7 @@ #include #include #include +#include Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QMainWindow(parent){ @@ -20,7 +21,6 @@ Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QM QVBoxLayout * layout = new QVBoxLayout(layoutWidget); layoutWidget->setLayout(layout); - //======================== QGroupBox * controlBox = new QGroupBox("Control", this); layout->addWidget(controlBox); @@ -29,36 +29,112 @@ Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QM QPushButton * bnClearHist = new QPushButton("Clear Hist.", this); ctrlLayout->addWidget(bnClearHist, 0, 0); + connect(bnClearHist, &QPushButton::clicked, this, [=](){ + for( int i = 0; i < MaxNDigitizer; i++){ + for( int j = 0; j < MaxNChannels; j++){ + if( hist[i][j] ) hist[i][j]->Clear(); + } + } + }); + + QPushButton * bnAddRandom = new QPushButton("Add Random", this); + ctrlLayout->addWidget(bnAddRandom, 0, 1); + connect(bnAddRandom, &QPushButton::clicked, this, [=](){ + + double mean = 2000.0; // Mean of the distribution + double standardDeviation = 1000.0; // Standard deviation of the distribution + + double randomNumber = QRandomGenerator::global()->generateDouble(); // Generate a random number between 0 and 1 + double gaussianNumber = qSqrt(-2 * qLn(randomNumber)) * qCos(2 * M_PI * randomNumber); // Transform the number to follow a Gaussian distribution + + // Scale and shift the number to match the desired mean and standard deviation + gaussianNumber = (gaussianNumber * standardDeviation) + mean; + + int bd = cbDigi->currentIndex(); + int ch = cbCh->currentIndex(); + + hist[bd][ch]->Fill(gaussianNumber); + }); + + cbDigi = new RComboBox(this); + for( unsigned int i = 0; i < nDigi; i++) cbDigi->addItem("Digi-" + QString::number( digi[i]->GetSerialNumber() ), i); + ctrlLayout->addWidget(cbDigi, 1, 0); + connect( cbDigi, &RComboBox::currentIndexChanged, this, &Canvas::ChangeHistView); + + cbCh = new RComboBox(this); + for( int i = 0; i < MaxNChannels; i++) cbCh->addItem("ch-" + QString::number( i ), i); + ctrlLayout->addWidget(cbCh, 1, 1); + connect( cbCh, &RComboBox::currentIndexChanged, this, &Canvas::ChangeHistView); //======================== - QGroupBox * histBox = new QGroupBox("Histgrams", this); + histBox = new QGroupBox("Histgrams", this); layout->addWidget(histBox); - QGridLayout * histLayout = new QGridLayout(histBox); + histLayout = new QGridLayout(histBox); histBox->setLayout(histLayout); double xMax = 4000; double xMin = 0; double nBin = 100; - Histogram * hist = new Histogram("test", xMin, xMax, nBin); + for( unsigned int i = 0; i < MaxNDigitizer; i++){ + for( int j = 0; j < MaxNChannels; j++){ + if( i < nDigi ) { + hist[i][j] = new Histogram("Digi-" + QString::number(digi[i]->GetSerialNumber()) +", Ch-" + QString::number(j), xMin, xMax, nBin); + histView[i][j] = new TraceView(hist[i][j]->GetTrace()); + histView[i][j]->SetVRange(0, 10); + }else{ + hist[i][j] = nullptr; + } + } + } - hist->Fill(1); - hist->Fill(300); - hist->Fill(350); - TraceView * plotView = new TraceView(hist->GetTrace(), this); - histLayout->addWidget(plotView, 0, 0); + histLayout->addWidget(histView[0][0], 0, 0); + oldBd = -1; + oldCh = -1; } Canvas::~Canvas(){ + for( int i = 0; i < MaxNDigitizer; i++){ + for( int j = 0; j < MaxNChannels; j++){ + if( hist[i][j] ) { + delete hist[i][j]; + delete histView[i][j]; + } + } + } +} +void Canvas::ChangeHistView(){ + + if( oldCh >= 0 ) { + histLayout->removeWidget(histView[oldBd][oldCh]); + histView[oldBd][oldCh]->setParent(nullptr); + } + int bd = cbDigi->currentIndex(); + int ch = cbCh->currentIndex(); + + histLayout->addWidget(histView[bd][ch], 0, 0); + + oldBd = bd; + oldCh = ch; } void Canvas::UpdateCanvas(){ + for( int i = 0; i < nDigi; i++){ - - + digiMTX[i].lock(); + for( int ch = 0; ch < digi[i]->GetNChannels(); ch ++ ){ + int lastIndex = digi[i]->GetData()->EventIndex[ch]; + int nDecoded = digi[i]->GetData()->NumEventsDecoded[ch]; + + for( int j = lastIndex - nDecoded + 1; j <= lastIndex; j ++){ + hist[i][ch]->Fill( digi[i]->GetData()->Energy[ch][j]); + } + } + digiMTX[i].unlock(); + } } \ No newline at end of file diff --git a/CanvasClass.h b/CanvasClass.h index da9510a..768cd58 100644 --- a/CanvasClass.h +++ b/CanvasClass.h @@ -34,12 +34,24 @@ public: public slots: void UpdateCanvas(); + void ChangeHistView(); private: Digitizer ** digi; unsigned short nDigi; + Histogram * hist[MaxNDigitizer][MaxNChannels]; + TraceView * histView[MaxNDigitizer][MaxNChannels]; + + RComboBox * cbDivision; + + RComboBox * cbDigi; + RComboBox * cbCh; + + QGroupBox * histBox; + QGridLayout * histLayout; + int oldBd, oldCh; }; diff --git a/CustomWidgets.h b/CustomWidgets.h index c69a292..5fadf3a 100644 --- a/CustomWidgets.h +++ b/CustomWidgets.h @@ -107,13 +107,19 @@ public: setMouseTracking(true); setRenderHints(QPainter::Antialiasing); + + vRangeMin = -(0x1FFF); + vRangeMax = 0x1FFF; } void SetHRange(int min, int max) { this->hRangeMin = min; this->hRangeMax = max; + } + void SetVRange(int min, int max) { + this->vRangeMin = min; + this->vRangeMax = max; } - protected: bool viewportEvent(QEvent *event) override{ if (event->type() == QEvent::TouchBegin) { @@ -127,7 +133,6 @@ protected: QChartView::mousePressEvent(event); } void mouseMoveEvent(QMouseEvent *event) override{ - QPointF chartPoint = this->chart()->mapToValue(event->pos()); QString coordinateText = QString("x: %1, y: %2").arg(QString::number(chartPoint.x(), 'f', 0)).arg(QString::number(chartPoint.y(), 'f', 0)); m_coordinateLabel->setText(coordinateText); @@ -135,7 +140,6 @@ protected: m_coordinateLabel->setVisible(true); if (m_isTouching) return; QChartView::mouseMoveEvent(event); - } void mouseReleaseEvent(QMouseEvent *event) override{ if (m_isTouching) m_isTouching = false; @@ -155,17 +159,20 @@ protected: case Qt::Key_Up: chart()->scroll(0, 10); break; case Qt::Key_Down: chart()->scroll(0, -10); break; case Qt::Key_R : - chart()->axes(Qt::Vertical).first()->setRange(-(0x1FFF), 0x1FFF); + //chart()->axes(Qt::Vertical).first()->setRange(-(0x1FFF), 0x1FFF); + chart()->axes(Qt::Vertical).first()->setRange(vRangeMin, vRangeMax); //chart()->axes(Qt::Horizontal).first()->setRange(hRangeMin, hRangeMax); break; default: QGraphicsView::keyPressEvent(event); break; } } - + private: bool m_isTouching; int hRangeMin; int hRangeMax; + int vRangeMin; + int vRangeMax; QLabel * m_coordinateLabel; }; @@ -181,7 +188,10 @@ public: this->xMax = xMax; this->nBin = nBin; dX = (xMax-xMin)/nBin; - Clear(); + for( int i = 0; i <= nBin; i++) { + dataSeries->append(xMin + i * dX, 0 ); + dataSeries->append(xMin + i * dX, 0 ); + } maxBin = -1; maxBinValue = 0; @@ -203,6 +213,9 @@ public: //xaxis->setLabelFormat("%.1f"); //xaxis->setTitleText("Time [ns]"); + QValueAxis * yaxis = qobject_cast (plot->axes(Qt::Vertical).first()); + yaxis->setRange(0, 10); + } ~Histogram(){ delete areaSeries; @@ -214,8 +227,8 @@ public: void Clear(){ for( int i = 0; i <= nBin; i++) { - dataSeries->append(xMin + i * dX, 0 ); - dataSeries->append(xMin + i * dX, 0 ); + dataSeries->replace(2*i, xMin + i * dX, 0); + dataSeries->replace(2*i+1, xMin + i * dX, 0); } } @@ -241,8 +254,8 @@ public: } QValueAxis * yaxis = qobject_cast (plot->axes(Qt::Vertical).first()); + yaxis->setRange(0, maxBinValue < 10 ? 10 : ((double)maxBinValue) * 1.2 ); //yaxis->setTickInterval(1); - yaxis->setRange(0, ((double)maxBinValue) * 1.2 ); //yaxis->setTickCount(10); //yaxis->setLabelFormat("%.0f"); diff --git a/FSUDAQ.cpp b/FSUDAQ.cpp index 5f972c4..c498d8a 100644 --- a/FSUDAQ.cpp +++ b/FSUDAQ.cpp @@ -51,7 +51,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ layout->addWidget(bnDigiSettings, 1, 1); connect(bnDigiSettings, &QPushButton::clicked, this, &MainWindow::OpenDigiSettings); - bnCanvas = new QPushButton("Canvas", this); + bnCanvas = new QPushButton("Online Histograms", this); layout->addWidget(bnCanvas, 2, 0); connect(bnCanvas, &QPushButton::clicked, this, &MainWindow::OpenCanvas); @@ -393,6 +393,13 @@ void MainWindow::OpenDigitizers(){ QCoreApplication::processEvents(); //to prevent Qt said application not responding. } + histThread = new TimingThread(this); + histThread->SetWaitTimeinSec(1); + connect(histThread, &TimingThread::timeUp, this, [=](){ + if( canvas == nullptr ) return; + canvas->UpdateCanvas(); + }); + LogMsg(QString("Done. Opened %1 digitizer(s).").arg(nDigi)); WaitForDigitizersOpen(false); @@ -612,7 +619,11 @@ void MainWindow::StartACQ(){ if( chkSaveData->isChecked() ) commentResult = CommentDialog(true); if( commentResult == false) return; - LogMsg("===================== Start a new Run-" + QString::number(runID)); + if( chkSaveData->isChecked() ) { + LogMsg("===================== Start a new Run-" + QString::number(runID)); + }else{ + LogMsg("===================== Start a non-save Run"); + } for( unsigned int i = 0; i < nDigi ; i++){ if( chkSaveData->isChecked() ) { @@ -636,6 +647,8 @@ void MainWindow::StartACQ(){ } lbScalarACQStatus->setText("ACQ On"); + if( canvas != nullptr ) histThread->start(); + bnStartACQ->setEnabled(false); bnStopACQ->setEnabled(true); bnOpenScope->setEnabled(false); @@ -644,7 +657,11 @@ void MainWindow::StartACQ(){ void MainWindow::StopACQ(){ if( digi == nullptr ) return; - LogMsg("===================== Stop Run-" + QString::number(runID)); + if( chkSaveData->isChecked() ) { + LogMsg("===================== Stop Run-" + QString::number(runID)); + }else{ + LogMsg("===================== Stop a non-save Run"); + } bool commentResult = true; if( chkSaveData->isChecked() ) commentResult = CommentDialog(true); @@ -666,6 +683,12 @@ void MainWindow::StopACQ(){ scalarThread->quit(); scalarThread->wait(); } + + if( histThread->isRunning()){ + histThread->Stop(); + histThread->quit(); + histThread->wait(); + } lbScalarACQStatus->setText("ACQ Off"); diff --git a/FSUDAQ.h b/FSUDAQ.h index 7fbdb49..6d78a5f 100644 --- a/FSUDAQ.h +++ b/FSUDAQ.h @@ -120,6 +120,7 @@ private: //@----- Canvas Canvas * canvas; + TimingThread * histThread; }; diff --git a/Scope.cpp b/Scope.cpp index c372696..ee9e70b 100644 --- a/Scope.cpp +++ b/Scope.cpp @@ -451,7 +451,6 @@ void Scope::CleanUpSettingsGroupBox(){ } - void Scope::SetUpPHAPanel(){ printf("--- %s \n", __func__);