created a histogram class for 1D histogram

This commit is contained in:
splitPoleDAQ 2023-05-18 17:14:24 -04:00
parent a3deb59e0c
commit 73c2286005
6 changed files with 137 additions and 71 deletions

View File

@ -6,76 +6,59 @@
#include <QStandardItemModel>
#include <QLabel>
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<int> 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<int>((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<int> 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<int>((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(){
}

View File

@ -23,34 +23,23 @@
#include "CustomWidgets.h"
#include <QWidget>
#include <QChart>
#include <QChartView>
#include <QBarSeries>
#include <QBarSet>
#include <QBarCategoryAxis>
//^====================================================
//^====================================================
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;
};

View File

@ -13,6 +13,9 @@
#include <QRubberBand>
#include <QMouseEvent>
#include <QGestureEvent>
#include <QLineSeries>
#include <QAreaSeries>
#include <QValueAxis>
//^====================================================
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<QValueAxis*> (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<QValueAxis*> (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

View File

@ -12,8 +12,6 @@
#include <QFileDialog>
#include <QScrollArea>
#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();

View File

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

View File

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