bug fix on EventBuilder, Eventbuilder will output timesorted data when timeWindow = 0

This commit is contained in:
carina@hades 2022-10-21 18:36:42 -04:00
parent 04f98d1923
commit 5f4fd901c3
3 changed files with 102 additions and 87 deletions

View File

@ -36,6 +36,7 @@ class Data{
unsigned long long lastTimestamp[MaxNChannels]; unsigned long long lastTimestamp[MaxNChannels];
/// stored Raw event /// stored Raw event
bool IsNotRollOverFakeAgg;
unsigned short NumEvents[MaxNChannels]; unsigned short NumEvents[MaxNChannels];
unsigned long long Timestamp[MaxNChannels][MaxNData]; /// 47 bit unsigned long long Timestamp[MaxNChannels][MaxNData]; /// 47 bit
unsigned short fineTime[MaxNChannels][MaxNData]; /// 10 bits, in unit of ch2ns / 1000 = ps unsigned short fineTime[MaxNChannels][MaxNData]; /// 10 bits, in unit of ch2ns / 1000 = ps
@ -58,6 +59,7 @@ class Data{
void ClearData(); void ClearData();
void ClearTriggerRate(); void ClearTriggerRate();
void ClearBuffer();
void SaveBuffer(const char * fileName); void SaveBuffer(const char * fileName);
unsigned int GetPresentFileSize() {return presentFileSizeByte;} unsigned int GetPresentFileSize() {return presentFileSizeByte;}
@ -98,7 +100,7 @@ inline Data::Data(){
ch2ns = 2.0; ch2ns = 2.0;
boardSN = 0; boardSN = 0;
DPPType = V1730_DPP_PHA_CODE; DPPType = V1730_DPP_PHA_CODE;
IsNotRollOverFakeAgg = true;
buffer = NULL; buffer = NULL;
for ( int i = 0; i < MaxNChannels; i++) TotNumEvents[i] = 0; for ( int i = 0; i < MaxNChannels; i++) TotNumEvents[i] = 0;
ClearData(); ClearData();
@ -134,7 +136,7 @@ inline void Data::ClearData(){
nByte = 0; nByte = 0;
AllocatedSize = 0; AllocatedSize = 0;
BufferSize = 0; BufferSize = 0;
IsNotRollOverFakeAgg = true;
for( int i = 0 ; i < MaxNChannels; i++){ for( int i = 0 ; i < MaxNChannels; i++){
NumEvents[i] = 0; NumEvents[i] = 0;
firstTimestamp[0] = -1; firstTimestamp[0] = -1;
@ -157,6 +159,12 @@ inline void Data::ClearData(){
tempDigiWaveform2.clear(); tempDigiWaveform2.clear();
} }
inline void Data::ClearBuffer(){
delete buffer;
BufferSize = 0;
nByte = 0;
}
inline void Data::SaveBuffer(const char * fileName){ inline void Data::SaveBuffer(const char * fileName){
char saveFileName[100]; char saveFileName[100];
@ -182,6 +190,12 @@ inline void Data::SaveBuffer(const char * fileName){
} }
inline void Data::PrintStat() const{ inline void Data::PrintStat() const{
printf("========= %d \n", IsNotRollOverFakeAgg);
if( IsNotRollOverFakeAgg ) {
printf(" this is roll-over fake event.\n");
return;
}
printf("%2s | %6s | %9s | %6s\n", "ch", "# Evt.", "Rate [Hz]", "Tot. Evt."); printf("%2s | %6s | %9s | %6s\n", "ch", "# Evt.", "Rate [Hz]", "Tot. Evt.");
printf("---+--------+-----------+----------\n"); printf("---+--------+-----------+----------\n");
for(int ch = 0; ch < MaxNChannels; ch++){ for(int ch = 0; ch < MaxNChannels; ch++){
@ -201,7 +215,7 @@ inline void Data::PrintData() const{
printf("============================= Print Data\n"); printf("============================= Print Data\n");
for( int ch = 0; ch < MaxNChannels ; ch++){ for( int ch = 0; ch < MaxNChannels ; ch++){
if( NumEvents[ch] == 0 ) continue; if( NumEvents[ch] == 0 ) continue;
printf("------------ ch : %d \n", ch); printf("------------ ch : %d, %d \n", ch, NumEvents[ch]);
for( int ev = 0; ev < NumEvents[ch] ; ev++){ for( int ev = 0; ev < NumEvents[ch] ; ev++){
printf("%4d, %5u, %15llu, %5u \n", ev, Energy[ch][ev], Timestamp[ch][ev], fineTime[ch][ev]); printf("%4d, %5u, %15llu, %5u \n", ev, Energy[ch][ev], Timestamp[ch][ev], fineTime[ch][ev]);
} }
@ -276,15 +290,17 @@ inline void Data::DecodeBuffer(bool fastDecode, int verbose){
} }
} }
}else{ }else{
if( verbose >= 4 ) printf("incorrect buffer header. \n"); if( verbose >= 1 ) printf("nw : %d, incorrect buffer header. \n", nw);
break; break;
} }
nw++; nw++;
}while(true); ///printf("nw : %d ,x 4 = %d, nByte : %d \n", nw, 4*nw, nByte);
}while(4*nw < nByte);
///}while(true);
///Calculate trigger rate and first and last Timestamp ///Calculate trigger rate and first and last Timestamp
for(int ch = 0; ch < MaxNChannels; ch++){ for(int ch = 0; ch < MaxNChannels; ch++){
if( NumEventsDecoded[ch] > 0 ) IsNotRollOverFakeAgg = false;
unsigned long long dTime = Timestamp[ch][NumEvents[ch]-1] - Timestamp[ch][NumEvents[ch] - NumEventsDecoded[ch]]; unsigned long long dTime = Timestamp[ch][NumEvents[ch]-1] - Timestamp[ch][NumEvents[ch] - NumEventsDecoded[ch]];
double sec = dTime * ch2ns / 1e9; double sec = dTime * ch2ns / 1e9;
TriggerRate[ch] = NumEventsDecoded[ch]/sec; TriggerRate[ch] = NumEventsDecoded[ch]/sec;
@ -423,7 +439,7 @@ inline int Data::DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDe
if( isTrigger0 == 1 ) triggerAtSample = 2*wi ; if( isTrigger0 == 1 ) triggerAtSample = 2*wi ;
if( isTrigger1 == 1 ) triggerAtSample = 2*wi + 1; if( isTrigger1 == 1 ) triggerAtSample = 2*wi + 1;
if( verbose >= 3 && ev == 0 ){ if( verbose >= 4 && ev == 0 ){
printf("%4d| %5d, %d, %d \n", 2*wi, wave0, dp0, isTrigger0); printf("%4d| %5d, %d, %d \n", 2*wi, wave0, dp0, isTrigger0);
printf("%4d| %5d, %d, %d \n", 2*wi+1, wave1, dp1, isTrigger1); printf("%4d| %5d, %d, %d \n", 2*wi+1, wave1, dp1, isTrigger1);
} }

View File

@ -139,24 +139,6 @@ int main(int argc, char **argv) {
printf("%s | %d\n", fileCat[i][j].Data(), typeCat[i][j]); printf("%s | %d\n", fileCat[i][j].Data(), typeCat[i][j]);
} }
} }
///============= Open input Files
printf("##############################################\n");
FILE * haha = fopen(fileCat[0][0], "r");
if( haha == NULL ){
printf("#### Cannot open file : %s. Abort.\n", fileCat[0][0].Data());
return -1;
}
fseek(haha, 0L, SEEK_END);
const size_t inFileSize = ftell(haha);
printf("%s | file size : %d Byte = %.2f MB\n", fileCat[0][0].Data(), (int) inFileSize, inFileSize/1024./1024.);
fclose(haha);
Data * data = new Data();
data->DPPType = typeCat[0][0];
data->boardSN = idCat[0];
data->SetSaveWaveToMemory(true);
///============= Set Root Tree ///============= Set Root Tree
outRootFile = new TFile(outFileName, "recreate"); outRootFile = new TFile(outFileName, "recreate");
@ -176,6 +158,23 @@ int main(int argc, char **argv) {
arrayTrace->BypassStreamer(); arrayTrace->BypassStreamer();
} }
///============= Open input Files
printf("##############################################\n");
FILE * haha = fopen(fileCat[0][0], "r");
if( haha == NULL ){
printf("#### Cannot open file : %s. Abort.\n", fileCat[0][0].Data());
return -1;
}
fseek(haha, 0L, SEEK_END);
const size_t inFileSize = ftell(haha);
printf("%s | file size : %d Byte = %.2f MB\n", fileCat[0][0].Data(), (int) inFileSize, inFileSize/1024./1024.);
fclose(haha);
Data * data = new Data();
data->DPPType = typeCat[0][0];
data->boardSN = idCat[0];
data->SetSaveWaveToMemory(true);
///============= Main Loop ///============= Main Loop
haha = fopen(inFileName[0], "r"); haha = fopen(inFileName[0], "r");
@ -184,6 +183,7 @@ int main(int argc, char **argv) {
unsigned long currentTime = 0; unsigned long currentTime = 0;
unsigned long oldTime = 0; unsigned long oldTime = 0;
char * buffer = NULL;
do{ do{
///========== Get 1 aggreration ///========== Get 1 aggreration
@ -199,34 +199,36 @@ int main(int argc, char **argv) {
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
if( debug) printf("Board Agg. has %d word = %d bytes\n", aggSize/4, aggSize); if( debug) printf("Board Agg. has %d word = %d bytes\n", aggSize/4, aggSize);
countBdAgg ++;
char * buffer = new char[aggSize]; buffer = new char[aggSize];
dump = fread(buffer, aggSize, 1, haha); dump = fread(buffer, aggSize, 1, haha);
countBdAgg ++;
if( debug) printf("==================== %d Agg\n", countBdAgg);
data->DecodeBuffer(buffer, aggSize, false, 0); data->DecodeBuffer(buffer, aggSize, false, 0);
data->ClearBuffer();
delete buffer; if( !data->IsNotRollOverFakeAgg ) continue;
currentTime = get_time(); currentTime = get_time();
if( debug) { if( debug) {
printf("~~~~~~~~~~~~~~~~ time used : %lu \n", currentTime - oldTime); printf("~~~~~~~~~~~~~~~~ time used : %lu usec\n", currentTime - oldTime);
data->PrintStat(); data->PrintStat();
} }
EventBuilder(data, timeWindow, traceOn, false, debug); EventBuilder(data, timeWindow, traceOn, false, debug);
if( debug) printf("---------- event built : %llu \n", evID); if( debug) printf("---------- event built : %llu \n", evID);
if( countBdAgg > 74) break;
if( debug > 0 && evID > debug ) break; }while(!feof(haha) && ftell(haha) < inFileSize);
}while(!feof(haha) && ftell(haha) <= inFileSize);
fclose(haha); fclose(haha);
///printf("==================== end of loop \n"); printf("=======@@@@@@@@###############============= end of loop \n");
EventBuilder(data, timeWindow, traceOn, true, debug); EventBuilder(data, timeWindow, traceOn, true, debug);
tree->Write(); tree->Write();
outRootFile->Close(); outRootFile->Close();
@ -239,59 +241,48 @@ int main(int argc, char **argv) {
void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool isLastData, unsigned int verbose){ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool isLastData, unsigned int verbose){
///printf("============= build Event\n"); if( verbose) {
printf("======================== Event Builder \n");
if( verbose) data->PrintData(); data->PrintData();
}
/// find the last event timestamp; /// find the last event timestamp;
unsigned long long firstTimeStamp = -1; unsigned long long firstTimeStamp = -1;
unsigned long long lastTimeStamp = 0; unsigned long long lastTimeStamp = 0;
unsigned long long smallestLastTimeStamp = -1; unsigned long long smallestLastTimeStamp = -1;
for( int chI = 0; chI < MaxNChannels ; chI ++){ for( int chI = 0; chI < MaxNChannels ; chI ++){
if( data->Timestamp[chI][0] > 0 && data->Timestamp[chI][0] < firstTimeStamp ) { if( data->NumEvents[chI] == 0 ) continue;
if( data->Timestamp[chI][0] < firstTimeStamp ) {
firstTimeStamp = data->Timestamp[chI][0]; firstTimeStamp = data->Timestamp[chI][0];
} }
unsigned short ev = data->NumEvents[chI]-1; unsigned short ev = data->NumEvents[chI]-1;
if( data->Timestamp[chI][ev] > 0 && data->Timestamp[chI][ev] > lastTimeStamp ) { if( data->Timestamp[chI][ev] > lastTimeStamp ) {
lastTimeStamp = data->Timestamp[chI][ev]; lastTimeStamp = data->Timestamp[chI][ev];
} }
if( data->Timestamp[chI][ev] > 0 && data->Timestamp[chI][ev] < smallestLastTimeStamp ){ if( data->Timestamp[chI][ev] < smallestLastTimeStamp ){
smallestLastTimeStamp = data->Timestamp[chI][ev]; smallestLastTimeStamp = data->Timestamp[chI][ev];
} }
} }
if( firstTimeStamp == -1 ) return;
if( verbose) printf("================ time range : %llu - %llu, smallest Last %llu\n", firstTimeStamp, lastTimeStamp, smallestLastTimeStamp ); if( verbose) printf("================ time range : %llu - %llu, smallest Last %llu\n", firstTimeStamp, lastTimeStamp, smallestLastTimeStamp );
unsigned short lastEv[MaxNChannels] = {0}; /// store the last event number for each ch unsigned short lastEv[MaxNChannels] = {0}; /// store the last event number for each ch
unsigned short exhaustedCh = 0; unsigned short exhaustedCh = 0; /// to stop
bool breakFlag = false;
do { do {
exhaustedCh = 0;
bool breakFlag = false;
/// find the 1st event /// find the 1st event
int ch1st = -1; int ch1st = -1;
unsigned long long time1st = -1; unsigned long long time1st = -1;
for( int chI = 0; chI < MaxNChannels ; chI ++){ for( int chI = 0; chI < MaxNChannels ; chI ++){
if( data->Timestamp[chI][lastEv[chI]] == 0 ) { if( data->NumEvents[chI] == 0 ) continue;
exhaustedCh ++; if( data->NumEvents[chI] <= lastEv[chI] ) continue;
continue;
}
if( data->NumEvents[chI] <= lastEv[chI] ) {
breakFlag = true;
exhaustedCh ++;
break;
}
if( data->Timestamp[chI][lastEv[chI]] < time1st ) { if( data->Timestamp[chI][lastEv[chI]] < time1st ) {
time1st = data->Timestamp[chI][lastEv[chI]]; time1st = data->Timestamp[chI][lastEv[chI]];
ch1st = chI; ch1st = chI;
} }
} }
if( traceOn ) arrayTrace->Clear("C");
if( breakFlag ) break;
if( !isLastData && ((smallestLastTimeStamp - time1st) < NTimeWinForBuffer * timeWin) ) break; if( !isLastData && ((smallestLastTimeStamp - time1st) < NTimeWinForBuffer * timeWin) ) break;
if( ch1st > MaxNChannels ) break; if( ch1st > MaxNChannels ) break;
@ -302,28 +293,31 @@ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool is
e_t[multi-1] = data->Timestamp[ch1st][lastEv[ch1st]]; e_t[multi-1] = data->Timestamp[ch1st][lastEv[ch1st]];
e_f[multi-1] = data->fineTime[ch1st][lastEv[ch1st]]; e_f[multi-1] = data->fineTime[ch1st][lastEv[ch1st]];
if( traceOn ){ if( traceOn ){
arrayTrace->Clear("C");
traceLength[multi-1] = (unsigned short) data->Waveform1[ch1st][lastEv[ch1st]].size(); traceLength[multi-1] = (unsigned short) data->Waveform1[ch1st][lastEv[ch1st]].size();
///if( verbose )printf("------- trace Length : %u \n", traceLength[multi-1]);
if( verbose )printf("------- trace Length : %u \n", traceLength[multi-1]);
trace = (TGraph *) arrayTrace->ConstructedAt(multi-1, "C"); trace = (TGraph *) arrayTrace->ConstructedAt(multi-1, "C");
trace->Clear(); trace->Clear();
for( int hh = 0; hh < traceLength[multi-1]; hh++){ for( int hh = 0; hh < traceLength[multi-1]; hh++){
trace->SetPoint(hh, hh, data->Waveform1[ch1st][lastEv[ch1st]][hh]); trace->SetPoint(hh, hh, data->Waveform1[ch1st][lastEv[ch1st]][hh]);
///if( verbose )if( hh % 200 == 0 ) printf("%3d | %u \n", hh, data->Waveform1[ch1st][lastEv[ch1st]][hh]);
if( verbose )if( hh % 200 == 0 ) printf("%3d | %u \n", hh, data->Waveform1[ch1st][lastEv[ch1st]][hh]);
} }
///if( verbose) trace->Print();
} }
lastEv[ch1st] ++; lastEv[ch1st] ++;
/// build event /// build the rest of the event
exhaustedCh = 0;
for( int chI = ch1st; chI < ch1st + MaxNChannels; chI ++){ for( int chI = ch1st; chI < ch1st + MaxNChannels; chI ++){
unsigned short chX = chI % MaxNChannels; unsigned short chX = chI % MaxNChannels;
if( data->NumEvents[chX] == 0 ) continue; if( data->NumEvents[chX] == 0 ) {
if( data->NumEvents[chX] <= lastEv[chX] ) continue; exhaustedCh ++;
continue;
}
if( data->NumEvents[chX] <= lastEv[chX] ) {
exhaustedCh ++;
continue;
}
if( timeWin == 0 ) continue;
for( int ev = lastEv[chX]; ev < data->NumEvents[chX] ; ev++){ for( int ev = lastEv[chX]; ev < data->NumEvents[chX] ; ev++){
if( data->Timestamp[chX][ev] > 0 && (data->Timestamp[chX][ev] - e_t[0] ) < timeWin ) { if( data->Timestamp[chX][ev] > 0 && (data->Timestamp[chX][ev] - e_t[0] ) < timeWin ) {
multi ++; multi ++;
@ -334,7 +328,6 @@ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool is
e_f[multi-1] = data->fineTime[chX][ev]; e_f[multi-1] = data->fineTime[chX][ev];
if( traceOn ){ if( traceOn ){
traceLength[multi-1] = (unsigned short) data->Waveform1[chX][ev].size(); traceLength[multi-1] = (unsigned short) data->Waveform1[chX][ev].size();
trace = (TGraph *) arrayTrace->ConstructedAt(multi-1, "C"); trace = (TGraph *) arrayTrace->ConstructedAt(multi-1, "C");
trace->Clear(); trace->Clear();
for( int hh = 0; hh < traceLength[multi-1]; hh++){ for( int hh = 0; hh < traceLength[multi-1]; hh++){
@ -342,7 +335,6 @@ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool is
} }
} }
lastEv[chX] = ev + 1; lastEv[chX] = ev + 1;
if( lastEv[chX] == data->NumEvents[chX] ) exhaustedCh ++; if( lastEv[chX] == data->NumEvents[chX] ) exhaustedCh ++;
} }
} }
@ -357,24 +349,27 @@ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool is
printf("=============== Last Ev , exhaustedCh %d \n", exhaustedCh); printf("=============== Last Ev , exhaustedCh %d \n", exhaustedCh);
for( int chI = 0; chI < MaxNChannels ; chI++){ for( int chI = 0; chI < MaxNChannels ; chI++){
if( lastEv[chI] == 0 ) continue; if( lastEv[chI] == 0 ) continue;
printf("%2d, %d \n", chI, lastEv[chI]); printf("%2d, %d %d\n", chI, lastEv[chI], data->NumEvents[chI]);
} }
} }
///========== Quick Sort the timestamp ??or left it to analyzer, monitor?
/// fill Tree /// fill Tree
outRootFile->cd(); outRootFile->cd();
tree->Fill(); tree->Fill();
evID++; evID++;
/// clear
multi = 0; multi = 0;
if( verbose > 0 && evID > verbose ) break; /// Check Last event break;
breakFlag = false;
}while( exhaustedCh < MaxNChannels - 1 ); for( int chI = 0; chI < MaxNChannels ; chI++){
if( lastEv[chI] == 0 ) continue;
if( lastEv[chI] >= data->NumEvents[chI] ) {
breakFlag = true;
break;
}
}
}while( !breakFlag || (exhaustedCh < MaxNChannels) );
///========== clear built data ///========== clear built data
/// move the last data to the top, /// move the last data to the top,
for( int chI = 0; chI < MaxNChannels; chI++){ for( int chI = 0; chI < MaxNChannels; chI++){
@ -386,10 +381,14 @@ void EventBuilder(Data * data, const unsigned int timeWin, bool traceOn, bool is
data->fineTime[chI][count] = data->fineTime[chI][ev]; data->fineTime[chI][count] = data->fineTime[chI][ev];
count++; count++;
} }
data->NumEvents[chI] = data->NumEvents[chI] - lastEv[chI]; int lala = data->NumEvents[chI] - lastEv[chI];
data->NumEvents[chI] = (lala >= 0 ? lala: 0);
} }
if( verbose > 0 ) data->PrintData(); if( verbose > 0 ) {
printf("&&&&&&&&&&&&&&&&&&&&&&&&&& end of one event build loop\n");
data->PrintData();
}
} }

View File

@ -18,7 +18,7 @@ OBJS = programSetting.o triggerSummary.o registerSetting.o channelSettingPSD.o c
all : test FSUDAQ test_indep EventBuilder all : test FSUDAQ test_indep EventBuilder
clean : clean :
/bin/rm -f $(OBJS) test FSUDAQ FSUDAQDict.cxx *.pcm /bin/rm -f $(OBJS) test FSUDAQ test_indep EventBuilder FSUDAQDict.cxx *.pcm
ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ClassData.h ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ClassData.h
$(CC) $(COPTS) -c ClassDigitizer.cpp $(CC) $(COPTS) -c ClassDigitizer.cpp