From 57bcb16a0de47393d8349d33596a8d0b553e0d3e Mon Sep 17 00:00:00 2001 From: splitPoleDAQ Date: Thu, 20 Apr 2023 14:39:59 -0400 Subject: [PATCH] improve the trigger rate calculation --- ClassData.h | 60 +++++++++++++++++++++++++++++++++++++++++++---------- Scope.cpp | 3 ++- test.cpp | 2 +- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/ClassData.h b/ClassData.h index 4cfa849..f78374a 100644 --- a/ClassData.h +++ b/ClassData.h @@ -30,6 +30,7 @@ class Data{ double TriggerRate[MaxNChannels]; /// Hz double NonPileUpRate[MaxNChannels]; /// Hz + short calIndexes[MaxNChannels][2]; /// the index for trigger rate calculation unsigned long TotNumEvents[MaxNChannels]; unsigned short NumEventsDecoded[MaxNChannels]; /// reset at every decode unsigned short NumNonPileUpDecoded[MaxNChannels]; /// reset at every decode @@ -42,6 +43,7 @@ class Data{ unsigned short fineTime[MaxNChannels][MaxNData]; /// 10 bits, in unit of ch2ns / 1000 = ps unsigned short Energy[MaxNChannels][MaxNData]; /// 15 bit unsigned short Energy2[MaxNChannels][MaxNData]; /// 15 bit, in PSD, Energy = Qshort, Energy2 = Qlong + bool PileUp[MaxNChannels][MaxNData]; /// pile up flag std::vector Waveform1[MaxNChannels][MaxNData]; std::vector Waveform2[MaxNChannels][MaxNData]; @@ -169,6 +171,9 @@ inline void Data::ClearData(){ DigiWaveform1[i][j].clear(); DigiWaveform2[i][j].clear(); } + + calIndexes[i][0] = -1; + calIndexes[i][1] = -1; } tempWaveform1.clear(); @@ -232,12 +237,12 @@ inline void Data::PrintStat() const{ printf(" this is roll-over fake event or no events.\n"); return; } - printf("%2s | %6s | %9s | %6s\n", "ch", "# Evt.", "Rate [Hz]", "Tot. Evt."); - printf("---+--------+-----------+----------\n"); + printf("%2s | %6s | %9s | %9s | %6s\n", "ch", "# Evt.", "Rate [Hz]", "N-PileUp", "Tot. Evt."); + printf("---+--------+-----------+-----------+----------\n"); for(int ch = 0; ch < MaxNChannels; ch++){ - printf("%2d | %6d | %9.2f | %6lu\n", ch, NumEventsDecoded[ch], TriggerRate[ch], TotNumEvents[ch]); + printf("%2d | %6d | %9.2f | %9.2f | %6lu\n", ch, NumEventsDecoded[ch], TriggerRate[ch], NonPileUpRate[ch], TotNumEvents[ch]); } - printf("---+--------+-----------+----------\n"); + printf("---+--------+-----------+-----------+----------\n"); } inline void Data::PrintBuffer() const{ @@ -284,7 +289,7 @@ inline void Data::DecodeBuffer(bool fastDecode, int verbose){ if( nByte == 0 ) return; nw = 0; - ClearTriggerRate(); + //ClearTriggerRate(); do{ if( verbose >= 1 ) printf("Data::DecodeBuffer ######################################### Board Agg.\n"); @@ -337,14 +342,46 @@ inline void Data::DecodeBuffer(bool fastDecode, int verbose){ ///Calculate trigger rate and first and last Timestamp for(int ch = 0; ch < MaxNChannels; ch++){ - //TODO ====== when NumEventsDecoded is too small, the trigger rate is not reliable? if( NumEventsDecoded[ch] > 0 ) IsNotRollOverFakeAgg = true; - unsigned long long dTime = Timestamp[ch][NumEvents[ch]-1] - Timestamp[ch][NumEvents[ch] - NumEventsDecoded[ch]]; - double sec = dTime * ch2ns / 1e9; - if( sec != 0 && NumEventsDecoded[ch] > 1 ){ - TriggerRate[ch] = NumEventsDecoded[ch]/sec; - NonPileUpRate[ch] = NumNonPileUpDecoded[ch]/sec; + + //TODO ====== when NumEventsDecoded is too small, the trigger rate is not reliable? + // unsigned long long dTime = Timestamp[ch][NumEvents[ch]-1] - Timestamp[ch][NumEvents[ch] - NumEventsDecoded[ch]]; + // double sec = dTime * ch2ns / 1e9; + // if( sec != 0 && NumEventsDecoded[ch] > 1 ){ + // TriggerRate[ch] = NumEventsDecoded[ch]/sec; + // NonPileUpRate[ch] = NumNonPileUpDecoded[ch]/sec; + // } + + if( calIndexes[ch][0] == -1 ) calIndexes[ch][0] = 0; + if( calIndexes[ch][0] > -1 && calIndexes[ch][1] == -1 ) calIndexes[ch][1] = NumEvents[ch]-1; + + short nEvent = calIndexes[ch][1] - calIndexes[ch][0]; + //printf("ch %2d ----- %d %d | %d \n", ch, calIndexes[ch][0], calIndexes[ch][1], nEvent); + + if( calIndexes[ch][0] > -1 && calIndexes[ch][1] > -1 && nEvent > 10 ){ + unsigned long long dTime = Timestamp[ch][calIndexes[ch][1]] - Timestamp[ch][calIndexes[ch][0]]; + double sec = dTime * ch2ns / 1e9; + + //printf(" %10llu %10llu, %f = %f sec, rate = %f \n", Timestamp[ch][calIndexes[ch][0]], Timestamp[ch][calIndexes[ch][1]], ch2ns, sec, nEvent / sec); + + if( sec > 0.1 ){ /// at least 100 msec + TriggerRate[ch] = nEvent / sec; + + short pileUpCount = 0; + for( int i = calIndexes[ch][0] ; i <= calIndexes[ch][1]; i++ ) { + if( PileUp[ch][i] ) pileUpCount ++; + } + + NonPileUpRate[ch] = (nEvent - pileUpCount)/sec; + + calIndexes[ch][0] = calIndexes[ch][1]; + calIndexes[ch][1] = -1; + + } + }else{ + calIndexes[ch][1] = -1; } + } } @@ -557,6 +594,7 @@ inline int Data::DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDe Energy[channel][NumEvents[channel]] = energy; Timestamp[channel][NumEvents[channel]] = timeStamp; if(extra2Option == 0 || extra2Option == 2 ) fineTime[channel][NumEvents[channel]] = (extra2 & 0x07FF ); + PileUp[channel][NumEvents[channel]] = pileUp; NumEvents[channel] ++; NumEventsDecoded[channel] ++; TotNumEvents[channel] ++; diff --git a/Scope.cpp b/Scope.cpp index 3fadc0d..3516655 100644 --- a/Scope.cpp +++ b/Scope.cpp @@ -23,7 +23,7 @@ Scope::Scope(Digitizer ** digi, unsigned int nDigi, ReadDataThread ** readDataTh for( int i = 0; i < MaxNumberOfTrace; i++) { dataTrace[i] = new QLineSeries(); dataTrace[i]->setName("Trace " + QString::number(i)); - for(int j = 0; j < 100; j ++) dataTrace[i]->append(40*j, QRandomGenerator::global()->bounded(8000)); + for(int j = 0; j < 100; j ++) dataTrace[i]->append(40*j, QRandomGenerator::global()->bounded(8000) - 4000); plot->addSeries(dataTrace[i]); } @@ -230,6 +230,7 @@ void Scope::UpdateScope(){ Data * data = digi[ID]->GetData(); digiMTX[ID].lock(); + //leTriggerRate->setText(QString::number(data->TriggerRate[ch]) + " [" + QString::number(data->NumEventsDecoded[ch]) + "]"); leTriggerRate->setText(QString::number(data->TriggerRate[ch])); unsigned short index = data->NumEvents[ch] - 1; diff --git a/test.cpp b/test.cpp index 16dbdf1..9b7f36f 100644 --- a/test.cpp +++ b/test.cpp @@ -44,7 +44,7 @@ int main(int argc, char* argv[]){ for( int i = 0; i < 50; i ++ ){ usleep(100*1000); dig[0]->ReadData(); - data->DecodeBuffer(false, 5); + data->DecodeBuffer(false); data->PrintStat(); int index = data->NumEventsDecoded[0];