FSUDAQ_Qt6/SingleSpectra.cpp

238 lines
6.6 KiB
C++
Raw Normal View History

#include "SingleSpectra.h"
#include <QValueAxis>
#include <QRandomGenerator>
#include <QGroupBox>
#include <QStandardItemModel>
#include <QLabel>
2023-05-19 16:23:04 -04:00
#include <QRandomGenerator>
SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawDataPath, QMainWindow * parent) : QMainWindow(parent){
this->digi = digi;
this->nDigi = nDigi;
this->rawDataPath = rawDataPath;
setWindowTitle("1-D Histograms");
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);
2023-06-01 17:51:00 -04:00
{//^========================
QGroupBox * controlBox = new QGroupBox("Control", this);
layout->addWidget(controlBox);
QGridLayout * ctrlLayout = new QGridLayout(controlBox);
controlBox->setLayout(ctrlLayout);
cbDigi = new RComboBox(this);
for( unsigned int i = 0; i < nDigi; i++) cbDigi->addItem("Digi-" + QString::number( digi[i]->GetSerialNumber() ), i);
ctrlLayout->addWidget(cbDigi, 0, 0, 1, 2);
connect( cbDigi, &RComboBox::currentIndexChanged, this, &SingleSpectra::ChangeHistView);
2023-06-01 17:51:00 -04:00
cbCh = new RComboBox(this);
for( int i = 0; i < MaxNChannels; i++) cbCh->addItem("ch-" + QString::number( i ), i);
ctrlLayout->addWidget(cbCh, 0, 2, 1, 2);
connect( cbCh, &RComboBox::currentIndexChanged, this, &SingleSpectra::ChangeHistView);
2023-06-01 17:51:00 -04:00
QPushButton * bnClearHist = new QPushButton("Clear All Hist.", this);
2023-06-01 17:51:00 -04:00
ctrlLayout->addWidget(bnClearHist, 0, 4, 1, 2);
connect(bnClearHist, &QPushButton::clicked, this, [=](){
for( unsigned int i = 0; i < nDigi; i++){
for( int j = 0; j < MaxNChannels; j++){
if( hist[i][j] ) hist[i][j]->Clear();
lastFilledIndex[i][j] = -1;
loopFilledIndex[i][j] = 0;
2023-06-01 17:51:00 -04:00
}
2023-05-19 16:23:04 -04:00
}
2023-06-01 17:51:00 -04:00
});
QCheckBox * chkIsFillHistogram = new QCheckBox("Fill Histograms", this);
ctrlLayout->addWidget(chkIsFillHistogram, 0, 6);
connect(chkIsFillHistogram, &QCheckBox::stateChanged, this, [=](int state){ fillHistograms = state;});
chkIsFillHistogram->setChecked(false);
fillHistograms = false;
2023-06-01 17:51:00 -04:00
}
{//^========================
histBox = new QGroupBox("Histgrams", this);
layout->addWidget(histBox);
histLayout = new QGridLayout(histBox);
histBox->setLayout(histLayout);
double xMax = 5000;
2023-06-01 17:51:00 -04:00
double xMin = 0;
double nBin = 200;
2023-06-01 17:51:00 -04:00
for( unsigned int i = 0; i < MaxNDigitizer; i++){
if( i >= nDigi ) continue;
for( int j = 0; j < digi[i]->GetNChannels(); j++){
if( i < nDigi ) {
hist[i][j] = new Histogram1D("Digi-" + QString::number(digi[i]->GetSerialNumber()) +", Ch-" + QString::number(j), "Raw Energy [ch]", nBin, xMin, xMax);
}else{
hist[i][j] = nullptr;
}
2023-05-19 16:23:04 -04:00
}
}
2023-06-01 17:51:00 -04:00
LoadSetting();
2023-06-01 17:51:00 -04:00
histLayout->addWidget(hist[0][0], 0, 0);
2023-05-19 16:23:04 -04:00
}
2023-06-01 17:51:00 -04:00
layout->setStretch(0, 1);
layout->setStretch(1, 6);
ClearInternalDataCount();
2023-06-01 17:51:00 -04:00
2023-05-19 16:23:04 -04:00
oldBd = -1;
oldCh = -1;
}
SingleSpectra::~SingleSpectra(){
SaveSetting();
for( unsigned int i = 0; i < nDigi; i++ ){
for( int ch = 0; ch < digi[i]->GetNChannels(); ch++){
delete hist[i][ch];
2023-05-19 16:23:04 -04:00
}
}
}
void SingleSpectra::ClearInternalDataCount(){
for( unsigned int i = 0; i < nDigi; i++){
for( int ch = 0; ch < MaxNChannels ; ch++) {
lastFilledIndex[i][ch] = -1;
loopFilledIndex[i][ch] = 0;
}
}
}
void SingleSpectra::ChangeHistView(){
2023-05-19 16:23:04 -04:00
if( oldCh >= 0 ) {
histLayout->removeWidget(hist[oldBd][oldCh]);
hist[oldBd][oldCh]->setParent(nullptr);
2023-05-19 16:23:04 -04:00
}
int bd = cbDigi->currentIndex();
int ch = cbCh->currentIndex();
histLayout->addWidget(hist[bd][ch], 0, 0);
2023-05-19 16:23:04 -04:00
oldBd = bd;
oldCh = ch;
}
void SingleSpectra::FillHistograms(){
if( !fillHistograms ) return;
2023-05-19 16:23:04 -04:00
for( int i = 0; i < nDigi; i++){
2023-05-19 16:23:04 -04:00
digiMTX[i].lock();
for( int ch = 0; ch < digi[i]->GetNChannels(); ch ++ ){
int lastIndex = digi[i]->GetData()->DataIndex[ch];
2023-06-01 17:51:00 -04:00
int loopIndex = digi[i]->GetData()->LoopIndex[ch];
int temp1 = lastIndex + loopIndex*MaxNData;
int temp2 = lastFilledIndex[i][ch] + loopFilledIndex[i][ch]*MaxNData;
// printf("%d |%d %d \n", ch, temp2, temp1);
2023-06-01 17:51:00 -04:00
if( lastIndex < 0 ) continue;
if( temp1 <= temp2 ) continue;
for( int j = 0 ; j <= temp1 - temp2; j ++){
lastFilledIndex[i][ch] ++;
if( lastFilledIndex[i][ch] > MaxNData ) {
lastFilledIndex[i][ch] = 0;
loopFilledIndex[i][ch] ++;
}
hist[i][ch]->Fill( digi[i]->GetData()->Energy[ch][lastFilledIndex[i][ch]]);
2023-05-19 16:23:04 -04:00
}
hist[i][ch]->UpdatePlot();
2023-05-19 16:23:04 -04:00
}
digiMTX[i].unlock();
2023-05-19 16:23:04 -04:00
}
}
void SingleSpectra::SaveSetting(){
QFile file(rawDataPath + "/singleSpectraSetting.txt");
file.open(QIODevice::Text | QIODevice::WriteOnly);
for( unsigned int i = 0; i < nDigi; i++){
file.write(("======= " + QString::number(digi[i]->GetSerialNumber()) + "\n").toStdString().c_str());
for( int ch = 0; ch < digi[i]->GetNChannels() ; ch++){
QString a = QString::number(ch).rightJustified(2, ' ');
QString b = QString::number(hist[i][ch]->GetNBin()).rightJustified(6, ' ');
QString c = QString::number(hist[i][ch]->GetXMin()).rightJustified(6, ' ');
QString d = QString::number(hist[i][ch]->GetXMax()).rightJustified(6, ' ');
file.write( QString("%1 %2 %3 %4\n").arg(a).arg(b).arg(c).arg(d).toStdString().c_str() );
}
}
file.write("//========== End of file\n");
file.close();
}
void SingleSpectra::LoadSetting(){
QFile file(rawDataPath + "/singleSpectraSetting.txt");
if( file.open(QIODevice::Text | QIODevice::ReadOnly) ){
QTextStream in(&file);
QString line = in.readLine();
int digiSN = 0;
int digiID = -1;
while ( !line.isNull() ){
if( line.contains("//========== ") ) break;
if( line.contains("//") ) continue;
if( line.contains("======= ") ){
digiSN = line.mid(7).toInt();
digiID = -1;
for( unsigned int i = 0; i < nDigi; i++){
if( digiSN == digi[i]->GetSerialNumber() ) {
digiID = i;
break;
}
}
line = in.readLine();
continue;
}
if( digiID >= 0 ){
QStringList list = line.split(QRegularExpression("\\s+"));
list.removeAll("");
if( list.count() != 4 ) {
line = in.readLine();
continue;
}
QVector<int> data;
for( int i = 0; i < list.count(); i++){
data.push_back(list[i].toInt());
}
hist[digiID][data[0]]->Rebin(data[1], data[2], data[3]);
}
line = in.readLine();
}
}else{
}
}