change the SingleSpectra::FillHistogram() Algorithm

This commit is contained in:
Ryan Tang 2024-10-16 16:56:59 -04:00
parent f8b9abc70d
commit 84b89cb36a
2 changed files with 135 additions and 76 deletions

View File

@ -4,6 +4,7 @@
#include <QGroupBox> #include <QGroupBox>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QLabel> #include <QLabel>
#include <QRandomGenerator>
// #include <QScreen> // #include <QScreen>
SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawDataPath, QMainWindow * parent) : QMainWindow(parent){ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawDataPath, QMainWindow * parent) : QMainWindow(parent){
@ -12,7 +13,7 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD
this->nDigi = nDigi; this->nDigi = nDigi;
this->settingPath = rawDataPath + "/HistogramSettings.txt"; this->settingPath = rawDataPath + "/HistogramSettings.txt";
maxFillTimeinMilliSec = 1000; maxFillTimeinMilliSec = 900;
maxFillTimePerDigi = maxFillTimeinMilliSec/nDigi; maxFillTimePerDigi = maxFillTimeinMilliSec/nDigi;
isSignalSlotActive = true; isSignalSlotActive = true;
@ -51,15 +52,17 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD
isSignalSlotActive = true; isSignalSlotActive = true;
//printf("oldCh = %d \n", oldCh); //printf("oldCh = %d \n", oldCh);
if( oldCh >= digi[index]->GetNumInputCh()) { // if( oldCh >= digi[index]->GetNumInputCh()) {
cbCh->setCurrentIndex(0); // cbCh->setCurrentIndex(0);
}else{ // }else{
if( oldCh >= 0 ){ // if( oldCh >= 0 ){
cbCh->setCurrentIndex(oldCh); // cbCh->setCurrentIndex(oldCh);
}else{ // }else{
cbCh->setCurrentIndex(0); // cbCh->setCurrentIndex(0);
} // }
} // }
cbCh->setCurrentIndex(oldChComboBoxindex[index]);
ChangeHistView(); ChangeHistView();
}); });
@ -81,6 +84,7 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD
} }
}); });
chkIsFillHistogram = new QCheckBox("Fill Histograms", this); chkIsFillHistogram = new QCheckBox("Fill Histograms", this);
ctrlLayout->addWidget(chkIsFillHistogram, 0, 6, 1, 2); ctrlLayout->addWidget(chkIsFillHistogram, 0, 6, 1, 2);
chkIsFillHistogram->setChecked(false); chkIsFillHistogram->setChecked(false);
@ -133,10 +137,12 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD
histLayout->addWidget(hist2D[0], 0, 0); histLayout->addWidget(hist2D[0], 0, 0);
hist2DVisibility[0] = true; hist2DVisibility[0] = true;
oldBd = 0;
oldCh = digi[0]->GetNumInputCh();
} }
//set default oldChComboBoxindex
for( unsigned int i = 0; i < nDigi; i++ ) oldChComboBoxindex[i] = 0;
oldBd = 0;
layout->setStretch(0, 1); layout->setStretch(0, 1);
layout->setStretch(1, 6); layout->setStretch(1, 6);
@ -149,12 +155,17 @@ SingleSpectra::SingleSpectra(Digitizer ** digi, unsigned int nDigi, QString rawD
histWorker->moveToThread(workerThread); histWorker->moveToThread(workerThread);
// Setup the timer to trigger every second // connect(timer, &QTimer::timeout, histWorker, &HistWorker::FillHistograms);
connect(timer, &QTimer::timeout, histWorker, &HistWorker::FillHistograms);
connect( histWorker, &HistWorker::workDone, this, &SingleSpectra::ReplotHistograms); connect( histWorker, &HistWorker::workDone, this, &SingleSpectra::ReplotHistograms);
workerThread->start(); // workerThread->start();
connect(timer, &QTimer::timeout, this, [=](){
if( isFillingHistograms == false){
histWorker->FillHistograms();
// ReplotHistograms();
}
});
} }
@ -198,23 +209,34 @@ void SingleSpectra::ChangeHistView(){
//printf("bd : %d, ch : %d \n", bd, ch); //printf("bd : %d, ch : %d \n", bd, ch);
// Remove oldCh // Remove oldCh
if( oldCh >= 0 && oldCh < digi[oldBd]->GetNumInputCh()){ int oldCh = oldChComboBoxindex[oldBd] == 0 ? digi[oldBd]->GetNumInputCh() : oldChComboBoxindex[oldBd] - 1;
histLayout->removeWidget(hist[oldBd][oldCh]);
hist[oldBd][oldCh]->setParent(nullptr);
histVisibility[oldBd][oldCh] = false;
}
if( oldCh == digi[oldBd]->GetNumInputCh() ){ if( oldChComboBoxindex[oldBd] > 0 ){
histLayout->removeWidget(hist[oldBd][oldCh]);
histVisibility[oldBd][oldCh] = false;
hist[oldBd][oldCh]->setParent(nullptr);
}else{
histLayout->removeWidget(hist2D[oldBd]); histLayout->removeWidget(hist2D[oldBd]);
hist2D[oldBd]->setParent(nullptr); hist2D[oldBd]->setParent(nullptr);
hist2DVisibility[oldBd] = false; hist2DVisibility[oldBd] = false;
} }
// if( oldCh >= 0 && oldCh < digi[oldBd]->GetNumInputCh()){
// histLayout->removeWidget(hist[oldBd][oldCh]);
// hist[oldBd][oldCh]->setParent(nullptr);
// histVisibility[oldBd][oldCh] = false;
// }
// if( oldCh == digi[oldBd]->GetNumInputCh() ){
// histLayout->removeWidget(hist2D[oldBd]);
// hist2D[oldBd]->setParent(nullptr);
// hist2DVisibility[oldBd] = false;
// }
// Add ch // Add ch
if( ch >=0 && ch < digi[bd]->GetNumInputCh()) { if( ch >=0 && ch < digi[bd]->GetNumInputCh()) {
histLayout->addWidget(hist[bd][ch], 0, 0); histLayout->addWidget(hist[bd][ch], 0, 0);
histVisibility[bd][ch] = true; histVisibility[bd][ch] = true;
hist[bd][ch]->UpdatePlot(); hist[bd][ch]->UpdatePlot();
} }
@ -225,7 +247,7 @@ void SingleSpectra::ChangeHistView(){
} }
oldBd = bd; oldBd = bd;
oldCh = ch; oldChComboBoxindex[bd] = cbCh->currentIndex();
// for( unsigned int i = 0; i < nDigi; i++ ){ // for( unsigned int i = 0; i < nDigi; i++ ){
// if( hist2DVisibility[i] ) printf(" hist2D-%d is visible\n", i); // if( hist2DVisibility[i] ) printf(" hist2D-%d is visible\n", i);
@ -244,74 +266,111 @@ void SingleSpectra::FillHistograms(){
if( isFillingHistograms) return; if( isFillingHistograms) return;
isFillingHistograms = true; isFillingHistograms = true;
timespec t0, t1; // timespec t0, t1;
timespec ta, tb;
QVector<int> randomDigiList = generateNonRepeatedCombination(nDigi); printf("####################### SingleSpectra::%s\n", __func__);
clock_gettime(CLOCK_REALTIME, &ta);
// qDebug() << randomDigiList; std::vector<int> digiChList; // (digi*1000 + ch)
std::vector<int> digiChLastIndex; // lastIndex
std::vector<int> digiChLoopIndex; // loopIndex
std::vector<bool> digiChFilled;
for( int i = 0; i < nDigi; i++){ for( int ID = 0; ID < nDigi; ID++){
int ID = randomDigiList[i]; for( int ch = 0; ch < digi[ID]->GetNumInputCh(); ch++){
QVector<int> randomChList = generateNonRepeatedCombination(digi[ID]->GetNumInputCh());
// qDebug() << randomChList;
// digiMTX[ID].lock();
// digi[ID]->GetData()->PrintAllData();
clock_gettime(CLOCK_REALTIME, &t0);
for( int k = 0; k < digi[ID]->GetNumInputCh(); k ++ ){
int ch = randomChList[k];
int lastIndex = digi[ID]->GetData()->GetDataIndex(ch); int lastIndex = digi[ID]->GetData()->GetDataIndex(ch);
if( lastIndex < 0 ) continue;
// printf("--- ch %2d | last index %d \n", ch, lastIndex);
int loopIndex = digi[ID]->GetData()->GetLoopIndex(ch); int loopIndex = digi[ID]->GetData()->GetLoopIndex(ch);
int temp1 = lastIndex + loopIndex * digi[ID]->GetData()->GetDataSize(); int temp1 = lastIndex + loopIndex * digi[ID]->GetData()->GetDataSize();
int temp2 = lastFilledIndex[ID][ch] + loopFilledIndex[ID][ch] * digi[ID]->GetData()->GetDataSize() + 1; int temp2 = lastFilledIndex[ID][ch] + loopFilledIndex[ID][ch] * digi[ID]->GetData()->GetDataSize() + 1;
// printf("loopIndx : %d | ID now : %d, ID old : %d \n", loopIndex, temp1, temp2);
if( temp1 <= temp2 ) continue; if( temp1 <= temp2 ) continue;
digiChList.push_back( ID*1000 + ch ) ;
digiChLastIndex.push_back(lastIndex);
digiChLoopIndex.push_back(loopIndex);
digiChFilled.push_back(false);
}
}
if( temp1 - temp2 > digi[ID]->GetData()->GetDataSize() ) { //DefaultDataSize = 10k int nSize = digiChList.size();
temp2 = temp1 - digi[ID]->GetData()->GetDataSize();
lastFilledIndex[ID][ch] = lastIndex;
lastFilledIndex[ID][ch] = loopIndex - 1;
}
// printf("ch %d | regulated ID now %d new %d | last fill idx %d\n", ch, temp2, temp1, lastFilledIndex[ID][ch]); // printf("------------ nSize : %d \n", nSize);
for( int j = 0 ; j <= temp1 - temp2; j ++){
lastFilledIndex[ID][ch] ++;
if( lastFilledIndex[ID][ch] > digi[ID]->GetData()->GetDataSize() ) {
lastFilledIndex[ID][ch] = 0;
loopFilledIndex[ID][ch] ++;
}
uShort data = digi[ID]->GetData()->GetEnergy(ch, lastFilledIndex[ID][ch]); if( nSize == 0 ) {
isFillingHistograms = false;
return;
}
// printf(" ch: %d, last fill idx : %d | %d \n", ch, lastFilledIndex[ID][ch], data); // this method, small trigger rate channel will have more chance to fill all data
do{
size_t filledCount = 0;
for( size_t i = 0; i < digiChFilled.size() ; i++ ){
if( digiChFilled[i] ) filledCount ++;
}
if( filledCount == digiChFilled.size() ) break;
hist[ID][ch]->Fill( data ); int randomValue = QRandomGenerator::global()->bounded(nSize);
if( digi[i]->GetDPPType() == DPPTypeCode::DPP_PSD_CODE ){ if( digiChFilled[randomValue] == true ) continue;
uShort e2 = digi[ID]->GetData()->GetEnergy2(ch, lastFilledIndex[ID][ch]);
// printf("%u \n", e2); int digiCh = digiChList[randomValue];
hist[ID][ch]->Fill( e2, 1); int ID = digiCh / 1000;
} int ch = digiCh % 1000;
hist2D[ID]->Fill(ch, data); // printf(" -------------------- %d / %d | %d\n", randomValue, nSize-1, digiCh);
}
// if( histVisibility[ID][ch] ) hist[ID][ch]->UpdatePlot();
clock_gettime(CLOCK_REALTIME, &t1); int lastIndex = digiChLastIndex[randomValue];
if( t1.tv_nsec - t0.tv_nsec + (t1.tv_sec - t0.tv_sec)*1e9 > maxFillTimePerDigi * 1e6 ) break; int loopIndex = digiChLoopIndex[randomValue];
int temp1 = lastIndex + loopIndex * digi[ID]->GetData()->GetDataSize();
int temp2 = lastFilledIndex[ID][ch] + loopFilledIndex[ID][ch] * digi[ID]->GetData()->GetDataSize() + 1;
if( temp1 <= temp2 ) {
digiChFilled[randomValue] = true;
// printf("Digi-%2d ch-%2d all filled | %zu\n", ID, ch, digiChList.size());
continue;
}
if( temp1 - temp2 > digi[ID]->GetData()->GetDataSize() ) { //DefaultDataSize = 10k
temp2 = temp1 - digi[ID]->GetData()->GetDataSize();
lastFilledIndex[ID][ch] = lastIndex;
lastFilledIndex[ID][ch] = loopIndex - 1;
} }
// if( hist2DVisibility[ID] ) hist2D[ID]->UpdatePlot(); lastFilledIndex[ID][ch] ++;
// digiMTX[ID].unlock(); if( lastFilledIndex[ID][ch] > digi[ID]->GetData()->GetDataSize() ) {
lastFilledIndex[ID][ch] = 0;
loopFilledIndex[ID][ch] ++;
}
} uShort data = digi[ID]->GetData()->GetEnergy(ch, lastFilledIndex[ID][ch]);
hist[ID][ch]->Fill( data );
if( digi[ID]->GetDPPType() == DPPTypeCode::DPP_PSD_CODE ){
uShort e2 = digi[ID]->GetData()->GetEnergy2(ch, lastFilledIndex[ID][ch]);
hist[ID][ch]->Fill( e2, 1);
}
hist2D[ID]->Fill(ch, data);
clock_gettime(CLOCK_REALTIME, &tb);
}while( (tb.tv_nsec - ta.tv_nsec)/1e6 + (tb.tv_sec - ta.tv_sec)*1e3 < maxFillTimeinMilliSec );
//*--------------- generate fillign report
for( size_t i = 0; i < digiChFilled.size() ; i++){
int digiCh = digiChList[i];
int ID = digiCh / 1000;
int ch = digiCh % 1000;
// printf(" -------------------- %d / %d | %d\n", randomValue, nSize-1, digiCh);
int lastIndex = digiChLastIndex[i];
int loopIndex = digiChLoopIndex[i];
int temp1 = lastIndex + loopIndex * digi[ID]->GetData()->GetDataSize();
int temp2 = lastFilledIndex[ID][ch] + loopFilledIndex[ID][ch] * digi[ID]->GetData()->GetDataSize() + 1;
printf("Digi-%2d ch-%2d | event unfilled %d\n", ID, ch, temp1 - temp2 );
}
clock_gettime(CLOCK_REALTIME, &tb);
printf("total time : %8.3f ms\n", (tb.tv_nsec - ta.tv_nsec)/1e6 + (tb.tv_sec - ta.tv_sec)*1e3 );
isFillingHistograms = false; isFillingHistograms = false;

View File

@ -73,7 +73,6 @@ private:
Histogram1D * hist[MaxNDigitizer][MaxNChannels]; Histogram1D * hist[MaxNDigitizer][MaxNChannels];
Histogram2D * hist2D[MaxNDigitizer]; Histogram2D * hist2D[MaxNDigitizer];
QCheckBox * chkIsFillHistogram; QCheckBox * chkIsFillHistogram;
RComboBox * cbDivision; RComboBox * cbDivision;
@ -83,7 +82,8 @@ private:
QGroupBox * histBox; QGroupBox * histBox;
QGridLayout * histLayout; QGridLayout * histLayout;
int oldBd, oldCh; int oldBd;
int oldChComboBoxindex[MaxNDigitizer]; // the ID of hist for display
QString settingPath; QString settingPath;
@ -97,7 +97,7 @@ private:
}; };
//^#======================================================== HistWorker // //^#======================================================== HistWorker
class HistWorker : public QObject{ class HistWorker : public QObject{
Q_OBJECT Q_OBJECT
public: public: