From 322cbca165070d6c0b153bd0ab193d89ad4fd4dc Mon Sep 17 00:00:00 2001 From: splitPoleDAQ Date: Fri, 26 May 2023 15:01:54 -0400 Subject: [PATCH] improve the online event builder. --- ClassData.h | 6 ++-- DataGenerator.cpp | 71 +++++++++++++++++++++++------------------- OnlineEventBuilder.cpp | 65 ++++++++++++++++++++------------------ OnlineEventBuilder.h | 3 +- 4 files changed, 79 insertions(+), 66 deletions(-) diff --git a/ClassData.h b/ClassData.h index ed188f6..4aa9189 100644 --- a/ClassData.h +++ b/ClassData.h @@ -14,7 +14,7 @@ #include "CAENDigitizerType.h" #include "macro.h" -#define MaxNData 10000 /// store 100k events per channels +#define MaxNData 10000 /// store 10k events per channels class Data{ @@ -272,8 +272,8 @@ inline void Data::PrintAllData() const{ printf("============================= Print Data\n"); for( int ch = 0; ch < MaxNChannels ; ch++){ if( DataIndex[ch] < 0 ) continue; - printf("------------ ch : %d, %d \n", ch, DataIndex[ch]); - for( int ev = 0; ev <= DataIndex[ch] ; ev++){ + printf("------------ ch : %d, DataIndex : %d, loop : %d\n", ch, DataIndex[ch], LoopIndex[ch]); + for( int ev = 0; ev <= (LoopIndex[ch] > 0 ? MaxNData : DataIndex[ch]) ; ev++){ if( DPPType == V1730_DPP_PHA_CODE ) printf("%4d, %5u, %15llu, %5u \n", ev, Energy[ch][ev], Timestamp[ch][ev], fineTime[ch][ev]); if( DPPType == V1730_DPP_PSD_CODE ) printf("%4d, %5u, %5u, %15llu, %5u \n", ev, Energy[ch][ev], Energy2[ch][ev], Timestamp[ch][ev], fineTime[ch][ev]); } diff --git a/DataGenerator.cpp b/DataGenerator.cpp index 31b7ecf..76c8f6b 100644 --- a/DataGenerator.cpp +++ b/DataGenerator.cpp @@ -15,45 +15,52 @@ int main(){ std::mt19937 gen(rd()); std::uniform_int_distribution RanNext(0, 1); - std::uniform_int_distribution RanCh(1, 2); + std::uniform_int_distribution RanCh(0, 4); std::uniform_int_distribution RanEnergy(1, 1000); std::uniform_int_distribution RanTime(1, 50); - int count = 0; - unsigned long long time = 0; - do{ - - int ch = RanCh(gen); - unsigned short energy = RanEnergy(gen); - - unsigned long long timestamp = time + RanTime(gen) + RanNext(gen)*100; - time = timestamp; - - count ++; - - data->DataIndex[ch] ++; - int index = data->DataIndex[ch]; - - if( ch == 2 && index > 2) { - data->DataIndex[ch] --; - continue; - } - data->Energy[ch][index] = energy; - data->Timestamp[ch][index] = timestamp; - - printf("%02d | %5d %10lld\n", ch, energy, time); - - }while( count < 50 ); - - data->PrintAllData(); - - printf("===================================\n"); - OnlineEventBuilder * eb = new OnlineEventBuilder(digi); + unsigned long long time = 0; - eb->BuildEvents(100); + for( int q = 0; q < 3; q ++ ){ + int count = 0; + do{ + int ch = RanCh(gen); + unsigned short energy = RanEnergy(gen); + unsigned long long timestamp = time + RanTime(gen) + RanNext(gen)*100; + time = timestamp; + + count ++; + + data->DataIndex[ch] ++; + if( data->DataIndex[ch] > MaxNData ) { + data->LoopIndex[ch] ++; + data->DataIndex[ch] = 0; + } + int index = data->DataIndex[ch]; + + // if( ch == 2 && index > 2) { + // data->DataIndex[ch] --; + // continue; + // } + data->Energy[ch][index] = energy; + data->Timestamp[ch][index] = timestamp; + + printf("%02d | %5d %10lld\n", ch, energy, time); + + }while( count < 40 ); + + data->PrintAllData(); + + printf("===================================\n"); + + eb->BuildEvents(100, true); + + } + + delete eb; delete digi; } \ No newline at end of file diff --git a/OnlineEventBuilder.cpp b/OnlineEventBuilder.cpp index c693011..686f01f 100644 --- a/OnlineEventBuilder.cpp +++ b/OnlineEventBuilder.cpp @@ -8,7 +8,6 @@ OnlineEventBuilder::OnlineEventBuilder(Digitizer * digi){ nCh = digi->GetNChannels(); eventIndex = -1; - isNeverBuild = true; for( int i = 0; i < MaxNEvent; i++ ) events[i].clear(); for( int i = 0; i < MaxNChannels; i++ ){ @@ -46,10 +45,9 @@ void OnlineEventBuilder::FindEarlistTimeAndCh(){ continue; } - if( isNeverBuild || nextIndex[ch] == -1 ) nextIndex[ch] = 0; + if( nextIndex[ch] == -1 ) nextIndex[ch] = 0; unsigned long long time = data->Timestamp[ch][nextIndex[ch]]; - if( time < earlistTime ) { earlistTime = time; earlistCh = ch; @@ -59,20 +57,30 @@ void OnlineEventBuilder::FindEarlistTimeAndCh(){ } +void OnlineEventBuilder::FindLatestTime(){ -void OnlineEventBuilder::BuildEvents(unsigned short timeWindow, bool verbose = false){ + latestTime = 0; + for( unsigned ch = 0; ch < nCh; ch++ ){ + int index = data->DataIndex[ch]; + if( index == -1 ) continue; + if( data->Timestamp[ch][index] > latestTime ) { + latestTime = data->Timestamp[ch][index]; + } + } + + printf("--- latest time %lld \n", latestTime); +} + +void OnlineEventBuilder::BuildEvents(unsigned short timeWindow, bool verbose){ this->timeWindow = timeWindow; - //======= Find the earlist time and ch + FindLatestTime(); FindEarlistTimeAndCh(); - if( earlistCh == -1 ) { /// no data - return; - } - - //======= Start bilding event + if( earlistCh == -1 || nExhaushedCh == nCh) return; /// no data + //======= Start building event do{ eventIndex ++; @@ -92,15 +100,16 @@ void OnlineEventBuilder::BuildEvents(unsigned short timeWindow, bool verbose = f do { - dT = data->Timestamp[ch][nextIndex[ch]] - earlistTime; + unsigned long long time = data->Timestamp[ch][nextIndex[ch]]; - if( dT < timeWindow ){ + if( time - earlistTime < timeWindow ){ dp.ch = ch; dp.energy = data->Energy[ch][nextIndex[ch]]; - dp.timeStamp = data->Timestamp[ch][nextIndex[ch]]; + dp.timeStamp = time; events[eventIndex].push_back(dp); nextIndex[ch]++; + if( nextIndex[ch] >= MaxNData) nextIndex[ch] = 0; }else{ break; } @@ -109,37 +118,33 @@ void OnlineEventBuilder::BuildEvents(unsigned short timeWindow, bool verbose = f } - - isNeverBuild = false; - std::sort(events[eventIndex].begin(), events[eventIndex].end(), [](const dataPoint& a, const dataPoint& b) { return a.timeStamp < b.timeStamp; }); - if( verbose ){ - //===== print - printf("&&&&&&&&&&&&&&&&&&&&&&&&& Event ID : %ld\n", eventIndex); - for( int i = 0; i <(int) events[eventIndex].size(); i++){ - printf("%02d | %5d %llu \n", events[eventIndex][i].ch, events[eventIndex][i].energy, events[eventIndex][i].timeStamp); - } - } - - ///Find the next earlist FindEarlistTimeAndCh(); if( verbose ){ + printf(">>>>>>>>>>>>>>>>>>>>>>>>> Event ID : %ld\n", eventIndex); + for( int i = 0; i <(int) events[eventIndex].size(); i++){ + printf("%02d | %5d %llu \n", events[eventIndex][i].ch, events[eventIndex][i].energy, events[eventIndex][i].timeStamp); + } + if( nExhaushedCh == nCh ) { - printf("######################### no more eevent to be built\n"); + printf("######################### no more event to be built\n"); break; } printf("----- next ch : %d, next earlist Time : %llu \n", earlistCh, earlistTime); } + if( latestTime - earlistTime <= timeWindow ) { + if( verbose ) { + printf("######################### left over data for next build\n"); + } + break; + } + }while(nExhaushedCh < nCh); - //TODO ----- to speed up continue with data->DataIndex - data->ClearData(); - for( int i = 0 ; i < MaxNChannels; i++) nextIndex[i] = -1; - } \ No newline at end of file diff --git a/OnlineEventBuilder.h b/OnlineEventBuilder.h index fd90c60..9a02fae 100644 --- a/OnlineEventBuilder.h +++ b/OnlineEventBuilder.h @@ -41,7 +41,6 @@ private: Data * data; unsigned short timeWindow; - bool isNeverBuild; int nextIndex[MaxNChannels]; int nExhaushedCh; @@ -49,6 +48,8 @@ private: unsigned long long earlistTime; int earlistCh; void FindEarlistTimeAndCh(); + unsigned long long latestTime; + void FindLatestTime(); };