From ea2bf2cb01b9dd76b7cf703f45e159ddf7046528 Mon Sep 17 00:00:00 2001 From: splitPoleDAQ Date: Thu, 11 Jan 2024 17:32:58 -0500 Subject: [PATCH] fix the time jumping problem for eventbuilder --- Aux/EventBuilder.cpp | 42 ++++++++++++++++++++++++++---------------- MultiBuilder.cpp | 26 +++++++++++++++++++++----- MultiBuilder.h | 7 +++++++ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/Aux/EventBuilder.cpp b/Aux/EventBuilder.cpp index ef301f4..8289b07 100644 --- a/Aux/EventBuilder.cpp +++ b/Aux/EventBuilder.cpp @@ -10,7 +10,7 @@ #include "TTree.h" #define MAX_MULTI 100 -#define BUFFERFILL 0.1 // only 0.5 * MAXNData will be filled in memeory each time +#define TIMEJUMP 1e8 // 0.1 sec or 10 Hz, any signal less than 10 Hz should increase the value. template void swap(T * a, T *b ); int partition(int arr[], int kaka[], TString file[], unsigned int fileSize[], unsigned int numBlock[], int t2ns[], int start, int end); @@ -25,11 +25,16 @@ int main(int argc, char **argv) { printf("=========================================\n"); if (argc <= 3) { printf("Incorrect number of arguments:\n"); - printf("%s [timeWindow] [traceOn/Off] [verbose] [inFile1] [inFile2] .... \n", argv[0]); - printf(" timeWindow : number of tick, 1 tick. default = 100 \n"); + printf("%s [timeWindow] [Buffer] [traceOn/Off] [verbose] [inFile1] [inFile2] .... \n", argv[0]); + printf(" timeWindow : in ns \n"); + printf(" Buffer : Fraction of %d, recommand 0.4 \n", MaxNData); printf(" traceOn/Off : is traces stored \n"); printf(" verbose : > 0 for debug \n"); printf(" Output file name is contructed from inFile1 \n"); + printf("\n"); + printf(" * there is a TIMEJUMP = 1e8 ns in EventBuilder.cpp.\n"); + printf(" This control the time diff for a time jumping.\n"); + printf(" Any signal with trigger rate < 1/TIMEJUMP should increase the value.\n"); return 1; } @@ -42,12 +47,13 @@ int main(int argc, char **argv) { ///============= read input unsigned int timeWindow = atoi(argv[1]); - bool traceOn = atoi(argv[2]); - unsigned int debug = atoi(argv[3]); - int nFile = argc - 4; + float bufferSize = atof(argv[2]); + bool traceOn = atoi(argv[3]); + unsigned int debug = atoi(argv[4]); + int nFile = argc - 5; TString inFileName[nFile]; for( int i = 0 ; i < nFile ; i++){ - inFileName[i] = argv[i+4]; + inFileName[i] = argv[i+5]; } /// Form outFileName; @@ -63,8 +69,9 @@ int main(int argc, char **argv) { for( int i = 0; i < nFile; i++) printf("%2d | %s \n", i, inFileName[i].Data()); printf("=====================================\n"); printf(" Time Window = %u \n", timeWindow); - + printf(" Buffer size = %.0f event/channel\n", MaxNData * bufferSize); printf("===================================== input files:\n"); + printf("Scanning files.....\n"); ///============= sorting file by the serial number & order int ID[nFile]; /// serial+ order*1000; @@ -192,13 +199,14 @@ int main(int argc, char **argv) { printf("================= Building events....\n"); MultiBuilder * mb = new MultiBuilder(data, typeList, snList); mb->SetTimeWindow(timeWindow); + mb->SetTimeJump(TIMEJUMP); ///------------------ read data char * buffer = nullptr; unsigned int word[1]; // 4 byte = 32 bit int lastDataIndex[nGroup][MAX_MULTI]; // keep track of the DataIndex - int lastLoopIndex[nGroup][MAX_MULTI]; // keep track of the DataIndex + int lastLoopIndex[nGroup][MAX_MULTI]; // keep track of the LoopIndex int aggCount[nGroup]; for( int i = 0; i < nGroup; i++){ @@ -276,6 +284,12 @@ int main(int argc, char **argv) { //check if Data Index near MaxNData. if near by 50%, break //printf("-----------------------------------\n"); for( int i = 0; i < nGroup; i++){ + + if( debug ){ + printf("-------------------------> %3d | agg : %d | %u \n", snList[i], aggCount[i], data[i]->aggTime); + //data[i]->PrintStat(); + } + for( int ch = 0; ch < data[i]->GetNChannel(); ch ++){ int iData = data[i]->DataIndex[ch]; @@ -283,21 +297,17 @@ int main(int argc, char **argv) { if( iData < 0 ) continue; - if( (iLoop*MaxNData + iData) - (lastLoopIndex[i][ch]*MaxNData + lastDataIndex[i][ch]) > MaxNData * BUFFERFILL ) { - if( debug ) printf("############# BREAK!!!! Group: %d, ch : %d | last : %d(%d), Present : %d(%d) | BufferSize : %.0f \n", i, ch, lastDataIndex[i][ch], lastLoopIndex[i][ch], iData, iLoop, MaxNData * BUFFERFILL); + if( (iLoop*MaxNData + iData) - (lastLoopIndex[i][ch]*MaxNData + lastDataIndex[i][ch]) > MaxNData * bufferSize ) { + if( debug ) printf("############# BREAK!!!! Group: %d, ch : %d | last : %d(%d), Present : %d(%d) | BufferSize : %.0f \n", i, ch, lastDataIndex[i][ch], lastLoopIndex[i][ch], iData, iLoop, MaxNData * bufferSize); fillFlag = false; } if( debug ){ unsigned long long t1 = data[i]->Timestamp[ch][iData]; - printf("digi:%d | ch: %2d DataIndex: %5d (%d) [%5d(%d)] | %llu\n", data[i]->boardSN, ch, iData, iLoop, lastDataIndex[i][ch], lastLoopIndex[i][ch], t1); + printf("digi:%5d | ch: %2d DataIndex: %5d (%d) [%5d(%d)] | %16llu\n", data[i]->boardSN, ch, iData, iLoop, lastDataIndex[i][ch], lastLoopIndex[i][ch], t1); } } - if( debug ){ - printf("-------------------------> %3d | agg : %d | %u \n", snList[i], aggCount[i], data[i]->aggTime); - //data[i]->PrintStat(); - } } }while(fillFlag); diff --git a/MultiBuilder.cpp b/MultiBuilder.cpp index 9e2db5a..41375ff 100644 --- a/MultiBuilder.cpp +++ b/MultiBuilder.cpp @@ -10,6 +10,8 @@ MultiBuilder::MultiBuilder(Data ** multiData, std::vector type, std::vector timeWindow = 100; leftOverTime = 100; breakTime = -1; + timeJump = 1e8; + lastEventTime = 0; ClearEvents(); // for( int i = 0; i < nData; i++){ @@ -27,6 +29,9 @@ MultiBuilder::MultiBuilder(Data * singleData, int type, int sn): nData(1){ timeWindow = 100; leftOverTime = 100; breakTime = -1; + timeJump = 1e8; + lastEventTime = 0; + ClearEvents(); } @@ -199,10 +204,9 @@ void MultiBuilder::FindLatestTimeOfData(bool verbose){ void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){ - FindEarlistTimeAmongLastData(verbose); - //FindLatestTimeOfData(verbose); + FindEarlistTimeAmongLastData(verbose); // give lastest Time, Ch, and Digi - FindEarlistTimeAndCh(verbose); + FindEarlistTimeAndCh(verbose); //Give the earliest time, ch, digi if( earlistCh == -1 || nExhaushedCh == nData * MaxNChannels) return; /// no data eventBuilt = 0; @@ -240,7 +244,8 @@ void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){ do { unsigned long long time = data[bd]->Timestamp[ch][nextIndex[bd][ch]]; - // printf("Check timestamp : %llu | earlistTime : %llu | timeWindow : %u \n", time, earlistTime, timeWindow); + //printf("%6ld, sn: %5d, ch: %2d, timestamp : %16llu | earlistTime : %16llu | timeWindow : %u \n", eventIndex, data[bd]->boardSN, ch, time, earlistTime, timeWindow); + if( time >= earlistTime && (time - earlistTime <= timeWindow) ){ em.sn = snList[bd]; em.bd = bd; @@ -268,15 +273,26 @@ void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){ if( timeWindow <= 0 ) break; } - if( timeWindow > 0 ) { + if( events[eventIndex].size() > 1) { std::sort(events[eventIndex].begin(), events[eventIndex].end(), [](const Hit& a, const Hit& b) { return a.timestamp < b.timestamp; }); } + + lastEventTime = events[eventIndex].back().timestamp; ///Find the next earlist FindEarlistTimeAndCh(false); + // //if there is a time jump, say, bigger than TimeJump. break + if( earlistTime - lastEventTime > timeJump ) { + printf("%6lu, %16llu\n", eventIndex, earlistTime); + printf("%5s - %16llu \n", "", lastEventTime); + printf("%5s > %16llu \n", "", timeJump); + printf("!!!!!!!! Time Jump detected stop event building. stop event buinding and get more data.\n"); + return; + } + if( verbose ){ printf(">>>>>>>>>>>>>>>>> Event ID : %ld, total built: %ld, multiplicity : %ld\n", eventIndex, totalEventBuilt, events[eventIndex].size()); for( int i = 0; i <(int) events[eventIndex].size(); i++){ diff --git a/MultiBuilder.h b/MultiBuilder.h index 2d63478..6227d16 100644 --- a/MultiBuilder.h +++ b/MultiBuilder.h @@ -48,6 +48,9 @@ public: void SetTimeWindow(unsigned short ticks) {timeWindow = ticks; leftOverTime = ticks;} unsigned short GetTimeWindow() const{return timeWindow;} + void SetTimeJump(unsigned long long TimeJumpInNanoSec) {timeJump = TimeJumpInNanoSec;} + unsigned long long GetTimeJump() const {return timeJump;} + void SetLeftOverTime(unsigned long long ticks) {leftOverTime = ticks;} unsigned long long GetLeftOverTime() const{return leftOverTime;} @@ -80,6 +83,10 @@ private: unsigned short timeWindow; unsigned long long leftOverTime; unsigned long long breakTime; // timestamp for breaking the event builder + + unsigned long long timeJump; //time diff for a time jump, default is 1e8 ns + unsigned long long lastEventTime; // timestamp for detect time jump + int loopIndex[MaxNDigitizer][MaxNChannels]; int nextIndex[MaxNDigitizer][MaxNChannels];