when ACQ start, fill histograms

This commit is contained in:
splitPoleDAQ 2023-05-19 16:23:04 -04:00
parent 73c2286005
commit 3b9d47bbef
6 changed files with 148 additions and 24 deletions

View File

@ -5,6 +5,7 @@
#include <QGroupBox> #include <QGroupBox>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QLabel> #include <QLabel>
#include <QRandomGenerator>
Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QMainWindow(parent){ 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); QVBoxLayout * layout = new QVBoxLayout(layoutWidget);
layoutWidget->setLayout(layout); layoutWidget->setLayout(layout);
//======================== //========================
QGroupBox * controlBox = new QGroupBox("Control", this); QGroupBox * controlBox = new QGroupBox("Control", this);
layout->addWidget(controlBox); layout->addWidget(controlBox);
@ -29,36 +29,112 @@ Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QM
QPushButton * bnClearHist = new QPushButton("Clear Hist.", this); QPushButton * bnClearHist = new QPushButton("Clear Hist.", this);
ctrlLayout->addWidget(bnClearHist, 0, 0); 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); layout->addWidget(histBox);
QGridLayout * histLayout = new QGridLayout(histBox); histLayout = new QGridLayout(histBox);
histBox->setLayout(histLayout); histBox->setLayout(histLayout);
double xMax = 4000; double xMax = 4000;
double xMin = 0; double xMin = 0;
double nBin = 100; 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(histView[0][0], 0, 0);
histLayout->addWidget(plotView, 0, 0); oldBd = -1;
oldCh = -1;
} }
Canvas::~Canvas(){ 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(){ 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();
}
} }

View File

@ -34,12 +34,24 @@ public:
public slots: public slots:
void UpdateCanvas(); void UpdateCanvas();
void ChangeHistView();
private: private:
Digitizer ** digi; Digitizer ** digi;
unsigned short nDigi; unsigned short nDigi;
Histogram * hist[MaxNDigitizer][MaxNChannels];
TraceView * histView[MaxNDigitizer][MaxNChannels];
RComboBox * cbDivision;
RComboBox * cbDigi;
RComboBox * cbCh;
QGroupBox * histBox;
QGridLayout * histLayout;
int oldBd, oldCh;
}; };

View File

@ -107,13 +107,19 @@ public:
setMouseTracking(true); setMouseTracking(true);
setRenderHints(QPainter::Antialiasing); setRenderHints(QPainter::Antialiasing);
vRangeMin = -(0x1FFF);
vRangeMax = 0x1FFF;
} }
void SetHRange(int min, int max) { void SetHRange(int min, int max) {
this->hRangeMin = min; this->hRangeMin = min;
this->hRangeMax = max; this->hRangeMax = max;
}
void SetVRange(int min, int max) {
this->vRangeMin = min;
this->vRangeMax = max;
} }
protected: protected:
bool viewportEvent(QEvent *event) override{ bool viewportEvent(QEvent *event) override{
if (event->type() == QEvent::TouchBegin) { if (event->type() == QEvent::TouchBegin) {
@ -127,7 +133,6 @@ protected:
QChartView::mousePressEvent(event); QChartView::mousePressEvent(event);
} }
void mouseMoveEvent(QMouseEvent *event) override{ void mouseMoveEvent(QMouseEvent *event) override{
QPointF chartPoint = this->chart()->mapToValue(event->pos()); 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)); 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); m_coordinateLabel->setText(coordinateText);
@ -135,7 +140,6 @@ protected:
m_coordinateLabel->setVisible(true); m_coordinateLabel->setVisible(true);
if (m_isTouching) return; if (m_isTouching) return;
QChartView::mouseMoveEvent(event); QChartView::mouseMoveEvent(event);
} }
void mouseReleaseEvent(QMouseEvent *event) override{ void mouseReleaseEvent(QMouseEvent *event) override{
if (m_isTouching) m_isTouching = false; if (m_isTouching) m_isTouching = false;
@ -155,17 +159,20 @@ protected:
case Qt::Key_Up: chart()->scroll(0, 10); break; case Qt::Key_Up: chart()->scroll(0, 10); break;
case Qt::Key_Down: chart()->scroll(0, -10); break; case Qt::Key_Down: chart()->scroll(0, -10); break;
case Qt::Key_R : 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); //chart()->axes(Qt::Horizontal).first()->setRange(hRangeMin, hRangeMax);
break; break;
default: QGraphicsView::keyPressEvent(event); break; default: QGraphicsView::keyPressEvent(event); break;
} }
} }
private: private:
bool m_isTouching; bool m_isTouching;
int hRangeMin; int hRangeMin;
int hRangeMax; int hRangeMax;
int vRangeMin;
int vRangeMax;
QLabel * m_coordinateLabel; QLabel * m_coordinateLabel;
}; };
@ -181,7 +188,10 @@ public:
this->xMax = xMax; this->xMax = xMax;
this->nBin = nBin; this->nBin = nBin;
dX = (xMax-xMin)/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; maxBin = -1;
maxBinValue = 0; maxBinValue = 0;
@ -203,6 +213,9 @@ public:
//xaxis->setLabelFormat("%.1f"); //xaxis->setLabelFormat("%.1f");
//xaxis->setTitleText("Time [ns]"); //xaxis->setTitleText("Time [ns]");
QValueAxis * yaxis = qobject_cast<QValueAxis*> (plot->axes(Qt::Vertical).first());
yaxis->setRange(0, 10);
} }
~Histogram(){ ~Histogram(){
delete areaSeries; delete areaSeries;
@ -214,8 +227,8 @@ public:
void Clear(){ void Clear(){
for( int i = 0; i <= nBin; i++) { for( int i = 0; i <= nBin; i++) {
dataSeries->append(xMin + i * dX, 0 ); dataSeries->replace(2*i, xMin + i * dX, 0);
dataSeries->append(xMin + i * dX, 0 ); dataSeries->replace(2*i+1, xMin + i * dX, 0);
} }
} }
@ -241,8 +254,8 @@ public:
} }
QValueAxis * yaxis = qobject_cast<QValueAxis*> (plot->axes(Qt::Vertical).first()); QValueAxis * yaxis = qobject_cast<QValueAxis*> (plot->axes(Qt::Vertical).first());
yaxis->setRange(0, maxBinValue < 10 ? 10 : ((double)maxBinValue) * 1.2 );
//yaxis->setTickInterval(1); //yaxis->setTickInterval(1);
yaxis->setRange(0, ((double)maxBinValue) * 1.2 );
//yaxis->setTickCount(10); //yaxis->setTickCount(10);
//yaxis->setLabelFormat("%.0f"); //yaxis->setLabelFormat("%.0f");

View File

@ -51,7 +51,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
layout->addWidget(bnDigiSettings, 1, 1); layout->addWidget(bnDigiSettings, 1, 1);
connect(bnDigiSettings, &QPushButton::clicked, this, &MainWindow::OpenDigiSettings); connect(bnDigiSettings, &QPushButton::clicked, this, &MainWindow::OpenDigiSettings);
bnCanvas = new QPushButton("Canvas", this); bnCanvas = new QPushButton("Online Histograms", this);
layout->addWidget(bnCanvas, 2, 0); layout->addWidget(bnCanvas, 2, 0);
connect(bnCanvas, &QPushButton::clicked, this, &MainWindow::OpenCanvas); connect(bnCanvas, &QPushButton::clicked, this, &MainWindow::OpenCanvas);
@ -393,6 +393,13 @@ void MainWindow::OpenDigitizers(){
QCoreApplication::processEvents(); //to prevent Qt said application not responding. 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)); LogMsg(QString("Done. Opened %1 digitizer(s).").arg(nDigi));
WaitForDigitizersOpen(false); WaitForDigitizersOpen(false);
@ -612,7 +619,11 @@ void MainWindow::StartACQ(){
if( chkSaveData->isChecked() ) commentResult = CommentDialog(true); if( chkSaveData->isChecked() ) commentResult = CommentDialog(true);
if( commentResult == false) return; 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++){ for( unsigned int i = 0; i < nDigi ; i++){
if( chkSaveData->isChecked() ) { if( chkSaveData->isChecked() ) {
@ -636,6 +647,8 @@ void MainWindow::StartACQ(){
} }
lbScalarACQStatus->setText("<font style=\"color: green;\"><b>ACQ On</b></font>"); lbScalarACQStatus->setText("<font style=\"color: green;\"><b>ACQ On</b></font>");
if( canvas != nullptr ) histThread->start();
bnStartACQ->setEnabled(false); bnStartACQ->setEnabled(false);
bnStopACQ->setEnabled(true); bnStopACQ->setEnabled(true);
bnOpenScope->setEnabled(false); bnOpenScope->setEnabled(false);
@ -644,7 +657,11 @@ void MainWindow::StartACQ(){
void MainWindow::StopACQ(){ void MainWindow::StopACQ(){
if( digi == nullptr ) return; 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; bool commentResult = true;
if( chkSaveData->isChecked() ) commentResult = CommentDialog(true); if( chkSaveData->isChecked() ) commentResult = CommentDialog(true);
@ -666,6 +683,12 @@ void MainWindow::StopACQ(){
scalarThread->quit(); scalarThread->quit();
scalarThread->wait(); scalarThread->wait();
} }
if( histThread->isRunning()){
histThread->Stop();
histThread->quit();
histThread->wait();
}
lbScalarACQStatus->setText("<font style=\"color: red;\"><b>ACQ Off</b></font>"); lbScalarACQStatus->setText("<font style=\"color: red;\"><b>ACQ Off</b></font>");

View File

@ -120,6 +120,7 @@ private:
//@----- Canvas //@----- Canvas
Canvas * canvas; Canvas * canvas;
TimingThread * histThread;
}; };

View File

@ -451,7 +451,6 @@ void Scope::CleanUpSettingsGroupBox(){
} }
void Scope::SetUpPHAPanel(){ void Scope::SetUpPHAPanel(){
printf("--- %s \n", __func__); printf("--- %s \n", __func__);