From 73c2286005d524b3d97e7ede9eaab8417da6cc95 Mon Sep 17 00:00:00 2001 From: splitPoleDAQ Date: Thu, 18 May 2023 17:14:24 -0400 Subject: [PATCH] created a histogram class for 1D histogram --- CanvasClass.cpp | 85 ++++++++++++++++++------------------------- CanvasClass.h | 17 ++------- CustomWidgets.h | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ FSUDAQ.cpp | 6 ++-- FSUDAQ.h | 3 +- Scope.cpp | 1 - 6 files changed, 137 insertions(+), 71 deletions(-) diff --git a/CanvasClass.cpp b/CanvasClass.cpp index c8b98ad..c2f17df 100644 --- a/CanvasClass.cpp +++ b/CanvasClass.cpp @@ -6,76 +6,59 @@ #include #include -Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataThread, QMainWindow * parent) : QMainWindow(parent){ +Canvas::Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent) : QMainWindow(parent){ this->digi = digi; this->nDigi = nDigi; - this->readDataThread = readDataThread; setWindowTitle("Canvas"); setGeometry(0, 0, 1000, 800); //setWindowFlags( this->windowFlags() & ~Qt::WindowCloseButtonHint ); + QWidget * layoutWidget = new QWidget(this); + setCentralWidget(layoutWidget); + QVBoxLayout * layout = new QVBoxLayout(layoutWidget); + layoutWidget->setLayout(layout); - QVector data = {1, 1, 2, 2, 3, 1, 2, 4, 5, 6, 5, 3, 4, 1, 3}; - double xMax = 5; - double xMin = -1; - double binSize = 0.5; - // Calculate the number of bins based on the X-axis range and bin size - int numBins = static_cast((xMax - xMin) / binSize) + 1; + //======================== + QGroupBox * controlBox = new QGroupBox("Control", this); + layout->addWidget(controlBox); + QGridLayout * ctrlLayout = new QGridLayout(controlBox); + controlBox->setLayout(ctrlLayout); - // Create a bar series - barSeries = new QBarSeries(); + QPushButton * bnClearHist = new QPushButton("Clear Hist.", this); + ctrlLayout->addWidget(bnClearHist, 0, 0); - // Create a histogram data array with the number of bins - QVector histogramData(numBins, 0); + //======================== + QGroupBox * histBox = new QGroupBox("Histgrams", this); + layout->addWidget(histBox); + QGridLayout * histLayout = new QGridLayout(histBox); + histBox->setLayout(histLayout); - // Calculate the histogram bin counts - for (double value : data) { - int binIndex = static_cast((value - xMin) / binSize); - if (binIndex >= 0 && binIndex < numBins) { - histogramData[binIndex]++; - } - } + double xMax = 4000; + double xMin = 0; + double nBin = 100; - // Create bar sets and add them to the series - for (int i = 0; i < numBins; ++i) { - double binStart = xMin + i * binSize; - double binEnd = binStart + binSize; - QString binLabel = QString("%1-%2").arg(binStart).arg(binEnd); + Histogram * hist = new Histogram("test", xMin, xMax, nBin); - QBarSet *barSet = new QBarSet(binLabel); - *barSet << histogramData[i]; - barSeries->append(barSet); - } + hist->Fill(1); + hist->Fill(300); + hist->Fill(350); - // Create the chart and set the series - chart = new QChart(); - chart->addSeries(barSeries); + TraceView * plotView = new TraceView(hist->GetTrace(), this); + histLayout->addWidget(plotView, 0, 0); - // Create the X-axis category axis - axisX = new QBarCategoryAxis(); - chart->setAxisX(axisX, barSeries); - - // Create category labels for the bins - QStringList categories; - for (int i = 0; i < numBins; ++i) { - double binStart = xMin + i * binSize; - double binEnd = binStart + binSize; - QString binLabel = QString("%1-%2").arg(binStart).arg(binEnd); - categories.append(binLabel); - axisX->append(binLabel); - } - - // Create a chart view and set the chart - chartView = new QChartView(chart); - chartView->setRenderHint(QPainter::Antialiasing); - - // Set the chart view as the main widget - setCentralWidget(chartView); } Canvas::~Canvas(){ +} + +void Canvas::UpdateCanvas(){ + + + + + } \ No newline at end of file diff --git a/CanvasClass.h b/CanvasClass.h index 0ec64f8..da9510a 100644 --- a/CanvasClass.h +++ b/CanvasClass.h @@ -23,34 +23,23 @@ #include "CustomWidgets.h" -#include -#include -#include -#include -#include -#include - - //^==================================================== //^==================================================== class Canvas : public QMainWindow{ Q_OBJECT public: - Canvas(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataThread, QMainWindow * parent = nullptr); + Canvas(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent = nullptr); ~Canvas(); +public slots: + void UpdateCanvas(); private: Digitizer ** digi; unsigned short nDigi; - ReadDataThread ** readDataThread; - QChartView *chartView; - QChart *chart; - QBarSeries *barSeries; - QBarCategoryAxis *axisX; }; diff --git a/CustomWidgets.h b/CustomWidgets.h index 801d407..c69a292 100644 --- a/CustomWidgets.h +++ b/CustomWidgets.h @@ -13,6 +13,9 @@ #include #include #include +#include +#include +#include //^==================================================== class RSpinBox : public QDoubleSpinBox{ @@ -90,6 +93,7 @@ private: }; +//^==================================================== class TraceView : public QChartView{ public: TraceView(QChart * chart, QWidget * parent = nullptr): QChartView(chart, parent){ @@ -101,6 +105,8 @@ public: m_coordinateLabel->setVisible(false); m_coordinateLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft); setMouseTracking(true); + + setRenderHints(QPainter::Antialiasing); } void SetHRange(int min, int max) { @@ -164,7 +170,97 @@ private: }; //^==================================================== +class Histogram { +public: + Histogram(QString title, double xMin, double xMax, int nBin){ + + plot = new Trace(); + dataSeries = new QLineSeries(); + this->xMin = xMin; + this->xMax = xMax; + this->nBin = nBin; + dX = (xMax-xMin)/nBin; + Clear(); + maxBin = -1; + maxBinValue = 0; + + //dataSeries->setPen(QPen(Qt::blue, 1)); + areaSeries = new QAreaSeries(dataSeries); + areaSeries->setName(title); + areaSeries->setBrush(Qt::blue); + + plot->addSeries(areaSeries); + + plot->setAnimationDuration(1); // msec + plot->setAnimationOptions(QChart::NoAnimation); + plot->createDefaultAxes(); + + QValueAxis * xaxis = qobject_cast (plot->axes(Qt::Horizontal).first()); + xaxis->setRange(xMin, xMax); + xaxis->setTickCount( nBin + 1 > 11 ? 11 : nBin + 1); + //xaxis->setLabelFormat("%.1f"); + //xaxis->setTitleText("Time [ns]"); + + } + ~Histogram(){ + delete areaSeries; + delete dataSeries; + delete plot; + } + + Trace * GetTrace() { return plot;} + + void Clear(){ + for( int i = 0; i <= nBin; i++) { + dataSeries->append(xMin + i * dX, 0 ); + dataSeries->append(xMin + i * dX, 0 ); + } + } + + void SetColor(Qt::GlobalColor color){ areaSeries->setBrush(color);} + + void Fill(double value){ + + double bin = (value - xMin)/dX; + if( bin < 0 || bin >= nBin ) return; + + int index1 = 2*qFloor(bin) + 1; + int index2 = index1 + 1; + + QPointF point1 = dataSeries->at(index1); + dataSeries->replace(index1, point1.x(), point1.y() + 1); + + QPointF point2 = dataSeries->at(index2); + dataSeries->replace(index2, point2.x(), point2.y() + 1); + + if( point2.y() + 1 > maxBinValue ){ + maxBinValue = point2.y() + 1; + maxBin = index2/2; + } + + QValueAxis * yaxis = qobject_cast (plot->axes(Qt::Vertical).first()); + //yaxis->setTickInterval(1); + yaxis->setRange(0, ((double)maxBinValue) * 1.2 ); + //yaxis->setTickCount(10); + //yaxis->setLabelFormat("%.0f"); + + } + +private: + Trace * plot; + QLineSeries * dataSeries; + QAreaSeries * areaSeries; + + double dX, xMin, xMax; + int nBin; + + int maxBin; + int maxBinValue; + +}; + +//^==================================================== #endif \ No newline at end of file diff --git a/FSUDAQ.cpp b/FSUDAQ.cpp index 049d6e0..5f972c4 100644 --- a/FSUDAQ.cpp +++ b/FSUDAQ.cpp @@ -12,8 +12,6 @@ #include #include -#include "CustomWidgets.h" - MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ setWindowTitle("FSU DAQ"); @@ -91,7 +89,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ leRunID->setAlignment(Qt::AlignHCenter); chkSaveData = new QCheckBox("Save Data", this); - cbAutoRun = new QComboBox(this); + cbAutoRun = new RComboBox(this); cbAutoRun->addItem("Single Infinite", 0); cbAutoRun->addItem("Single 30 mins", 30); cbAutoRun->addItem("Single 60 mins", 60); @@ -828,7 +826,7 @@ void MainWindow::OpenDigiSettings(){ void MainWindow::OpenCanvas(){ if( canvas == nullptr ) { - canvas = new Canvas(digi, nDigi, readDataThread); + canvas = new Canvas(digi, nDigi); canvas->show(); }else{ canvas->show(); diff --git a/FSUDAQ.h b/FSUDAQ.h index fec7587..7fbdb49 100644 --- a/FSUDAQ.h +++ b/FSUDAQ.h @@ -13,6 +13,7 @@ #include "ClassDigitizer.h" #include "CustomThreads.h" +#include "CustomWidgets.h" #include "Scope.h" #include "DigiSettingsPanel.h" #include "CanvasClass.h" @@ -94,7 +95,7 @@ private: QLineEdit * leRunID; QCheckBox * chkSaveData; - QComboBox * cbAutoRun; + RComboBox * cbAutoRun; QString startComment; QString stopComment; diff --git a/Scope.cpp b/Scope.cpp index 51ca754..c372696 100644 --- a/Scope.cpp +++ b/Scope.cpp @@ -150,7 +150,6 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh //================ Plot view rowID ++; plotView = new TraceView(plot, this); - plotView->setRenderHints(QPainter::Antialiasing); layout->addWidget(plotView, rowID, 0, 1, 6);