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 <QStandardItemModel>
#include <QLabel>
#include <QRandomGenerator>
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();
}
}

View File

@ -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;
};

View File

@ -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<QValueAxis*> (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<QValueAxis*> (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");

View File

@ -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("<font style=\"color: green;\"><b>ACQ On</b></font>");
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("<font style=\"color: red;\"><b>ACQ Off</b></font>");

View File

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

View File

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