diff --git a/hit2023v2/display.cpp b/hit2023v2/display.cpp index c914535..f904c93 100644 --- a/hit2023v2/display.cpp +++ b/hit2023v2/display.cpp @@ -28,6 +28,8 @@ Display::Display(QWidget *parent) : connect(buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*))); connect(ui->pushButton_savebkg, &QPushButton::clicked, this, &Display::onSaveBackgroundClicked); + connect(ui->pushButton_loadbkg, &QPushButton::clicked, this, &Display::onLoadBackgroundClicked); + connect(ui->checkBox_subbkg, &QCheckBox::stateChanged, this, &Display::onCheckBoxStateChanged); } Display::~Display() @@ -77,6 +79,23 @@ void Display::plot(const QVector &data) max = dataY[i]; } + + if (subtractBackground && ui->checkBox_subbkg->isChecked()) { + // Check if background subtraction is enabled and the checkbox is checked + QString planeName = ui->lineTitle->text(); + planeName.remove(QChar(' ')); + + // Check if background data exists for this plane + if (backgroundDataMap.contains(planeName)) { + const QVector &backgroundData = backgroundDataMap[planeName]; + + // Subtract background data from the current data + for (int i = 0; i < nrPoints; ++i) { + dataY[i] -= backgroundData[i]; + } + } + } + if (ui->radioButtonAutoscale->isChecked()) ui->plot->yAxis->setRange(min-0.05*(max-min),max+0.05*(max-min)); else if (ui->radioButtonFixedScale ->isChecked()) @@ -160,3 +179,48 @@ void Display::onSaveBackgroundClicked() qDebug() << "Error: Failed to open" << filename << "for writing"; } } + +void Display::onLoadBackgroundClicked() +{ + // Get the plane's name (you might need to adjust how you retrieve it) + QString planeName = ui->lineTitle->text(); + + // Remove invalid characters from the plane name (e.g., spaces) + planeName.remove(QChar(' ')); + + // Generate the filename with the plane name appended + QString filename = QString("background_%1.txt").arg(planeName); + + // Open the file for reading + QFile file(filename); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream stream(&file); + + // Read the data from the file and store it in the map + QVector backgroundData; + while (!stream.atEnd()) { + QString line = stream.readLine(); + unsigned short value = line.toUShort(); + backgroundData.append(value); + } + + // Close the file + file.close(); + + // Store the background data in the map + backgroundDataMap[planeName] = backgroundData; + + // Notify the user that the data has been loaded + qDebug() << "Background data loaded for" << planeName; + } else { + // Failed to open the file + qDebug() << "Error: Failed to open" << filename << "for reading"; + } +} + +void Display::onCheckBoxStateChanged(int state) +{ + // The state argument will be Qt::Unchecked (0) or Qt::Checked (2) + subtractBackground = (state == Qt::Checked); +} + diff --git a/hit2023v2/display.h b/hit2023v2/display.h index a67bd79..017b58e 100644 --- a/hit2023v2/display.h +++ b/hit2023v2/display.h @@ -31,6 +31,8 @@ public slots: void showEvent(QShowEvent *event); void onButtonClicked(QAbstractButton *button); void onSaveBackgroundClicked(); + void onLoadBackgroundClicked(); + void onCheckBoxStateChanged(int state); protected: int nrPoints = 0; @@ -41,6 +43,9 @@ private: QRadioButton *radioButtonFixedScale; // Pointer to the Fixed Scale radio button QRadioButton *radioButtonAutoscale; // Pointer to the Autoscale radio button QButtonGroup *buttonGroup; + QMap> backgroundDataMap; // Map to store background data for each plane + bool subtractBackground = false; // Flag to track if background subtraction is enabled + }; diff --git a/hit2023v2/display.ui b/hit2023v2/display.ui index 4215cb9..d3aa2c9 100644 --- a/hit2023v2/display.ui +++ b/hit2023v2/display.ui @@ -144,6 +144,32 @@ save bkg + + + + 420 + 360 + 80 + 24 + + + + load bkg + + + + + + 330 + 360 + 77 + 22 + + + + sub bkg + + diff --git a/hit2023v2/release/hit2023v2.exe b/hit2023v2/release/hit2023v2.exe index 5b8738c..1ce04fa 100644 Binary files a/hit2023v2/release/hit2023v2.exe and b/hit2023v2/release/hit2023v2.exe differ diff --git a/hit2023v2/ui_display.h b/hit2023v2/ui_display.h index f68ca2d..7cb96bd 100644 --- a/hit2023v2/ui_display.h +++ b/hit2023v2/ui_display.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ public: QSpinBox *spinBox_fixedmin; QSpinBox *spinBox_fixedmax; QPushButton *pushButton_savebkg; + QPushButton *pushButton_loadbkg; + QCheckBox *checkBox_subbkg; QButtonGroup *buttonGroup; void setupUi(QDialog *display) @@ -133,6 +136,12 @@ public: pushButton_savebkg = new QPushButton(display); pushButton_savebkg->setObjectName("pushButton_savebkg"); pushButton_savebkg->setGeometry(QRect(510, 360, 80, 24)); + pushButton_loadbkg = new QPushButton(display); + pushButton_loadbkg->setObjectName("pushButton_loadbkg"); + pushButton_loadbkg->setGeometry(QRect(420, 360, 80, 24)); + checkBox_subbkg = new QCheckBox(display); + checkBox_subbkg->setObjectName("checkBox_subbkg"); + checkBox_subbkg->setGeometry(QRect(330, 360, 77, 22)); retranslateUi(display); @@ -146,6 +155,8 @@ public: radioButtonMaxScale->setText(QCoreApplication::translate("display", "Max Y-Scale", nullptr)); radioButtonFixedScale->setText(QCoreApplication::translate("display", "Fixed Y-Scale", nullptr)); pushButton_savebkg->setText(QCoreApplication::translate("display", "save bkg", nullptr)); + pushButton_loadbkg->setText(QCoreApplication::translate("display", "load bkg", nullptr)); + checkBox_subbkg->setText(QCoreApplication::translate("display", "sub bkg", nullptr)); } // retranslateUi };