2023-06-02 15:41:26 -04:00
# include "SingleSpectra.h"
2023-05-17 17:40:32 -04:00
# include <QValueAxis>
# include <QRandomGenerator>
# include <QGroupBox>
# include <QStandardItemModel>
# include <QLabel>
2023-05-19 16:23:04 -04:00
# include <QRandomGenerator>
2023-05-17 17:40:32 -04:00
2023-06-02 15:41:26 -04:00
SingleSpectra : : SingleSpectra ( Digitizer * * digi , unsigned int nDigi , QString rawDataPath , QMainWindow * parent ) : QMainWindow ( parent ) {
2023-05-17 17:40:32 -04:00
this - > digi = digi ;
this - > nDigi = nDigi ;
2023-06-02 15:41:26 -04:00
this - > rawDataPath = rawDataPath ;
2023-05-17 17:40:32 -04:00
2024-03-22 12:43:17 -04:00
maxFillTimeinMilliSec = 500 ;
2023-10-26 17:57:06 -04:00
isSignalSlotActive = true ;
2023-06-02 15:41:26 -04:00
setWindowTitle ( " 1-D Histograms " ) ;
2023-05-17 17:40:32 -04:00
setGeometry ( 0 , 0 , 1000 , 800 ) ;
//setWindowFlags( this->windowFlags() & ~Qt::WindowCloseButtonHint );
2023-05-18 17:14:24 -04:00
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 ) ;
2023-10-26 17:57:06 -04:00
connect ( cbDigi , & RComboBox : : currentIndexChanged , this , [ = ] ( int index ) {
isSignalSlotActive = false ;
cbCh - > clear ( ) ;
2023-11-06 17:46:43 -05:00
cbCh - > addItem ( " All Ch " , digi [ index ] - > GetNumInputCh ( ) ) ;
2023-10-26 17:57:06 -04:00
for ( int i = 0 ; i < digi [ index ] - > GetNumInputCh ( ) ; i + + ) cbCh - > addItem ( " ch- " + QString : : number ( i ) , i ) ;
isSignalSlotActive = true ;
2023-12-12 18:09:43 -05:00
//printf("oldCh = %d \n", oldCh);
if ( oldCh > = digi [ index ] - > GetNumInputCh ( ) ) {
2023-10-26 17:57:06 -04:00
cbCh - > setCurrentIndex ( 0 ) ;
} else {
if ( oldCh > = 0 ) {
cbCh - > setCurrentIndex ( oldCh ) ;
} else {
cbCh - > setCurrentIndex ( 0 ) ;
}
}
ChangeHistView ( ) ;
} ) ;
2023-06-01 17:51:00 -04:00
cbCh = new RComboBox ( this ) ;
2023-11-06 17:46:43 -05:00
cbCh - > addItem ( " All Ch " , digi [ 0 ] - > GetNumInputCh ( ) ) ;
2023-10-26 17:57:06 -04:00
for ( int i = 0 ; i < digi [ 0 ] - > GetNumInputCh ( ) ; i + + ) cbCh - > addItem ( " ch- " + QString : : number ( i ) , i ) ;
2023-06-01 17:51:00 -04:00
ctrlLayout - > addWidget ( cbCh , 0 , 2 , 1 , 2 ) ;
2023-06-02 15:41:26 -04:00
connect ( cbCh , & RComboBox : : currentIndexChanged , this , & SingleSpectra : : ChangeHistView ) ;
2023-06-01 17:51:00 -04:00
2023-07-11 18:29:38 -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 + + ) {
2023-10-26 17:57:06 -04:00
for ( int j = 0 ; j < digi [ i ] - > GetNumInputCh ( ) ; j + + ) {
2023-06-01 17:51:00 -04:00
if ( hist [ i ] [ j ] ) hist [ i ] [ j ] - > Clear ( ) ;
2023-08-16 17:54:35 -04:00
lastFilledIndex [ i ] [ j ] = - 1 ;
loopFilledIndex [ i ] [ j ] = 0 ;
2023-06-01 17:51:00 -04:00
}
2023-11-06 17:46:43 -05:00
if ( hist2D [ i ] ) hist2D [ i ] - > Clear ( ) ;
2023-05-19 16:23:04 -04:00
}
2023-06-01 17:51:00 -04:00
} ) ;
2023-06-02 15:41:26 -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
}
{ //^========================
2023-11-06 17:46:43 -05:00
for ( unsigned int i = 0 ; i < nDigi ; i + + ) {
hist2DVisibility [ i ] = false ;
for ( int j = 0 ; j < digi [ i ] - > GetNumInputCh ( ) ; j + + ) {
histVisibility [ i ] [ j ] = false ;
}
}
2023-06-01 17:51:00 -04:00
histBox = new QGroupBox ( " Histgrams " , this ) ;
layout - > addWidget ( histBox ) ;
histLayout = new QGridLayout ( histBox ) ;
histBox - > setLayout ( histLayout ) ;
2023-11-06 17:46:43 -05:00
double eMax = 5000 ;
double eMin = 0 ;
2023-06-02 15:41:26 -04:00
double nBin = 200 ;
2023-06-01 17:51:00 -04:00
for ( unsigned int i = 0 ; i < MaxNDigitizer ; i + + ) {
if ( i > = nDigi ) continue ;
2023-10-09 17:46:32 -04:00
for ( int j = 0 ; j < digi [ i ] - > GetNumInputCh ( ) ; j + + ) {
2023-06-01 17:51:00 -04:00
if ( i < nDigi ) {
2023-11-06 17:46:43 -05:00
hist [ i ] [ j ] = new Histogram1D ( " Digi- " + QString : : number ( digi [ i ] - > GetSerialNumber ( ) ) + " , Ch- " + QString : : number ( j ) , " Raw Energy [ch] " , nBin , eMin , eMax ) ;
2023-06-01 17:51:00 -04:00
} else {
hist [ i ] [ j ] = nullptr ;
}
2023-05-19 16:23:04 -04:00
}
2023-11-06 17:46:43 -05:00
hist2D [ i ] = new Histogram2D ( " Digi- " + QString : : number ( digi [ i ] - > GetSerialNumber ( ) ) , " Channel " , " Raw Energy [ch] " , digi [ i ] - > GetNumInputCh ( ) , 0 , digi [ i ] - > GetNumInputCh ( ) , nBin , eMin , eMax ) ;
2023-11-21 15:34:29 -05:00
hist2D [ i ] - > SetChannelMap ( true , digi [ i ] - > GetNumInputCh ( ) < 20 ? 1 : 4 ) ;
hist2D [ i ] - > Rebin ( digi [ i ] - > GetNumInputCh ( ) , - 0.5 , digi [ i ] - > GetNumInputCh ( ) + 0.5 , nBin , eMin , eMax ) ;
2023-05-19 16:23:04 -04:00
}
2023-06-01 17:51:00 -04:00
2023-06-02 15:41:26 -04:00
LoadSetting ( ) ;
2023-11-06 17:46:43 -05:00
histLayout - > addWidget ( hist2D [ 0 ] , 0 , 0 ) ;
hist2DVisibility [ 0 ] = true ;
oldBd = 0 ;
oldCh = digi [ 0 ] - > GetNumInputCh ( ) ;
2023-05-19 16:23:04 -04:00
}
2023-05-18 17:14:24 -04:00
2023-06-01 17:51:00 -04:00
layout - > setStretch ( 0 , 1 ) ;
layout - > setStretch ( 1 , 6 ) ;
2023-08-16 17:54:35 -04:00
ClearInternalDataCount ( ) ;
2023-06-01 17:51:00 -04:00
2023-05-17 17:40:32 -04:00
}
2023-06-02 15:41:26 -04:00
SingleSpectra : : ~ SingleSpectra ( ) {
SaveSetting ( ) ;
2023-05-31 17:30:46 -04:00
for ( unsigned int i = 0 ; i < nDigi ; i + + ) {
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < digi [ i ] - > GetNumInputCh ( ) ; ch + + ) {
2023-05-31 17:30:46 -04:00
delete hist [ i ] [ ch ] ;
2023-05-19 16:23:04 -04:00
}
2023-11-06 17:46:43 -05:00
delete hist2D [ i ] ;
2023-05-19 16:23:04 -04:00
}
}
2023-08-16 17:54:35 -04:00
void SingleSpectra : : ClearInternalDataCount ( ) {
for ( unsigned int i = 0 ; i < nDigi ; i + + ) {
2023-10-06 16:50:28 -04:00
for ( int ch = 0 ; ch < MaxRegChannel ; ch + + ) {
2023-08-16 17:54:35 -04:00
lastFilledIndex [ i ] [ ch ] = - 1 ;
loopFilledIndex [ i ] [ ch ] = 0 ;
}
}
}
2023-06-02 15:41:26 -04:00
void SingleSpectra : : ChangeHistView ( ) {
2023-05-17 17:40:32 -04:00
2023-10-26 17:57:06 -04:00
if ( ! isSignalSlotActive ) return ;
2023-11-06 17:46:43 -05:00
int bd = cbDigi - > currentIndex ( ) ;
int ch = cbCh - > currentData ( ) . toInt ( ) ;
2023-12-12 18:09:43 -05:00
//printf("bd : %d, ch : %d \n", bd, ch);
2023-11-06 17:46:43 -05:00
// Remove oldCh
if ( oldCh > = 0 & & oldCh < digi [ oldBd ] - > GetNumInputCh ( ) ) {
2023-05-31 17:30:46 -04:00
histLayout - > removeWidget ( hist [ oldBd ] [ oldCh ] ) ;
hist [ oldBd ] [ oldCh ] - > setParent ( nullptr ) ;
2023-11-06 17:46:43 -05:00
histVisibility [ oldBd ] [ oldCh ] = false ;
}
if ( oldCh = = digi [ oldBd ] - > GetNumInputCh ( ) ) {
histLayout - > removeWidget ( hist2D [ oldBd ] ) ;
hist2D [ oldBd ] - > setParent ( nullptr ) ;
hist2DVisibility [ oldBd ] = false ;
}
// Add ch
if ( ch > = 0 & & ch < digi [ bd ] - > GetNumInputCh ( ) ) {
histLayout - > addWidget ( hist [ bd ] [ ch ] , 0 , 0 ) ;
histVisibility [ bd ] [ ch ] = true ;
2023-12-12 18:09:43 -05:00
hist [ bd ] [ ch ] - > UpdatePlot ( ) ;
2023-05-19 16:23:04 -04:00
}
2023-11-06 17:46:43 -05:00
if ( ch = = digi [ bd ] - > GetNumInputCh ( ) ) {
histLayout - > addWidget ( hist2D [ bd ] , 0 , 0 ) ;
hist2DVisibility [ bd ] = true ;
2023-12-12 18:09:43 -05:00
hist2D [ bd ] - > UpdatePlot ( ) ;
2023-11-06 17:46:43 -05:00
}
2023-05-19 16:23:04 -04:00
oldBd = bd ;
oldCh = ch ;
2023-11-06 17:46:43 -05:00
// for( unsigned int i = 0; i < nDigi; i++ ){
// if( hist2DVisibility[i] ) printf(" hist2D-%d is visible\n", i);
// for( int j = 0; j < digi[i]->GetNumInputCh(); j++){
// if( histVisibility[i][j] ) printf(" hist-%d-%d is visible\n", i, j);
// }
// }
2023-05-18 17:14:24 -04:00
}
2023-06-02 15:41:26 -04:00
void SingleSpectra : : FillHistograms ( ) {
if ( ! fillHistograms ) return ;
2023-05-18 17:14:24 -04:00
2024-03-22 12:43:17 -04:00
unsigned short maxFillTimePerDigi = maxFillTimeinMilliSec / nDigi ;
timespec t0 , t1 ;
2023-05-19 16:23:04 -04:00
for ( int i = 0 ; i < nDigi ; i + + ) {
2023-05-18 17:14:24 -04:00
2023-05-19 16:23:04 -04:00
digiMTX [ i ] . lock ( ) ;
2024-03-22 12:43:17 -04:00
clock_gettime ( CLOCK_REALTIME , & t0 ) ;
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < digi [ i ] - > GetNumInputCh ( ) ; ch + + ) {
2024-01-26 14:53:52 -05:00
int lastIndex = digi [ i ] - > GetData ( ) - > GetDataIndex ( ch ) ;
2024-03-21 16:57:34 -04:00
if ( lastIndex < 0 ) continue ;
2024-01-26 14:53:52 -05:00
int loopIndex = digi [ i ] - > GetData ( ) - > GetLoopIndex ( ch ) ;
2023-06-01 17:51:00 -04:00
2024-01-18 23:09:50 -05:00
int temp1 = lastIndex + loopIndex * digi [ i ] - > GetData ( ) - > GetDataSize ( ) ;
int temp2 = lastFilledIndex [ i ] [ ch ] + loopFilledIndex [ i ] [ ch ] * digi [ i ] - > GetData ( ) - > GetDataSize ( ) ;
2023-06-01 17:51:00 -04:00
2023-08-16 17:54:35 -04:00
// printf("%d |%d %d \n", ch, temp2, temp1);
2023-06-01 17:51:00 -04:00
if ( temp1 < = temp2 ) continue ;
2024-03-21 16:57:34 -04:00
if ( temp1 - temp2 > digi [ i ] - > GetData ( ) - > GetDataSize ( ) ) { //DefaultDataSize = 10k
temp2 = temp1 - digi [ i ] - > GetData ( ) - > GetDataSize ( ) ;
lastFilledIndex [ i ] [ ch ] = lastIndex ;
lastFilledIndex [ i ] [ ch ] = loopIndex - 1 ;
}
2023-06-01 17:51:00 -04:00
for ( int j = 0 ; j < = temp1 - temp2 ; j + + ) {
lastFilledIndex [ i ] [ ch ] + + ;
2024-01-18 23:09:50 -05:00
if ( lastFilledIndex [ i ] [ ch ] > digi [ i ] - > GetData ( ) - > GetDataSize ( ) ) {
2023-06-01 17:51:00 -04:00
lastFilledIndex [ i ] [ ch ] = 0 ;
loopFilledIndex [ i ] [ ch ] + + ;
}
2024-01-26 14:53:52 -05:00
hist [ i ] [ ch ] - > Fill ( digi [ i ] - > GetData ( ) - > GetEnergy ( ch , lastFilledIndex [ i ] [ ch ] ) ) ;
hist2D [ i ] - > Fill ( ch , digi [ i ] - > GetData ( ) - > GetEnergy ( ch , lastFilledIndex [ i ] [ ch ] ) ) ;
2023-05-19 16:23:04 -04:00
}
2023-11-06 17:46:43 -05:00
if ( histVisibility [ i ] [ ch ] ) hist [ i ] [ ch ] - > UpdatePlot ( ) ;
if ( hist2DVisibility [ i ] ) hist2D [ i ] - > UpdatePlot ( ) ;
2024-03-22 12:43:17 -04:00
clock_gettime ( CLOCK_REALTIME , & t1 ) ;
if ( t1 . tv_nsec - t0 . tv_nsec + ( t1 . tv_sec - t0 . tv_sec ) * 1e9 > maxFillTimePerDigi * 1e6 ) break ;
2023-05-19 16:23:04 -04:00
}
digiMTX [ i ] . unlock ( ) ;
2023-05-18 17:14:24 -04:00
2023-05-19 16:23:04 -04:00
}
2023-06-02 15:41:26 -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 ( ) ) ;
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < digi [ i ] - > GetNumInputCh ( ) ; ch + + ) {
2023-06-02 15:41:26 -04:00
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 ( ) ) ;
}
2023-11-21 15:34:29 -05:00
QString a = QString : : number ( digi [ i ] - > GetNumInputCh ( ) ) . rightJustified ( 2 , ' ' ) ;
QString b = QString : : number ( hist2D [ i ] - > GetXNBin ( ) ) . rightJustified ( 6 , ' ' ) ;
QString c = QString : : number ( hist2D [ i ] - > GetXMin ( ) ) . rightJustified ( 6 , ' ' ) ;
QString d = QString : : number ( hist2D [ i ] - > GetXMax ( ) ) . rightJustified ( 6 , ' ' ) ;
QString e = QString : : number ( hist2D [ i ] - > GetYNBin ( ) ) . rightJustified ( 6 , ' ' ) ;
QString f = QString : : number ( hist2D [ i ] - > GetYMin ( ) ) . rightJustified ( 6 , ' ' ) ;
QString g = QString : : number ( hist2D [ i ] - > GetYMax ( ) ) . rightJustified ( 6 , ' ' ) ;
file . write ( QString ( " %1 %2 %3 %4 %5 %6 %7 \n " ) . arg ( a ) . arg ( b ) . arg ( c ) . arg ( d ) . arg ( e ) . arg ( f ) . arg ( g ) . toStdString ( ) . c_str ( ) ) ;
2023-06-02 15:41:26 -04:00
}
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 ( ) ) ;
}
2023-11-21 15:34:29 -05:00
if ( 0 < = data [ 0 ] & & data [ 0 ] < digi [ digiID ] - > GetNumInputCh ( ) ) {
hist [ digiID ] [ data [ 0 ] ] - > Rebin ( data [ 1 ] , data [ 2 ] , data [ 3 ] ) ;
}
if ( data [ 0 ] = = digi [ digiID ] - > GetNumInputCh ( ) & & data . size ( ) = = 7 ) {
hist2D [ digiID ] - > Rebin ( data [ 1 ] , data [ 2 ] , data [ 3 ] , data [ 4 ] , data [ 5 ] , data [ 6 ] ) ;
}
2023-06-02 15:41:26 -04:00
}
line = in . readLine ( ) ;
}
} else {
}
2023-05-17 17:40:32 -04:00
}