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];
/// stored Raw event
bool IsNotRollOverFakeAgg;
unsigned short NumEvents[MaxNChannels];
unsigned long long Timestamp[MaxNChannels][MaxNData]; /// 47 bit
unsigned short fineTime[MaxNChannels][MaxNData]; /// 10 bits, in unit of ch2ns / 1000 = ps
@ -58,6 +59,7 @@ class Data{
void ClearData();
void ClearTriggerRate();
void ClearBuffer();
void SaveBuffer(const char * fileName);
unsigned int GetPresentFileSize() {return presentFileSizeByte;}
@ -98,7 +100,7 @@ inline Data::Data(){
ch2ns = 2.0;
boardSN = 0;
DPPType = V1730_DPP_PHA_CODE;
IsNotRollOverFakeAgg = true;
buffer = NULL;
for ( int i = 0; i < MaxNChannels; i++) TotNumEvents[i] = 0;
ClearData();
@ -134,7 +136,7 @@ inline void Data::ClearData(){
nByte = 0;
AllocatedSize = 0;
BufferSize = 0;
IsNotRollOverFakeAgg = true;
for( int i = 0 ; i < MaxNChannels; i++){
NumEvents[i] = 0;
firstTimestamp[0] = -1;
@ -157,6 +159,12 @@ inline void Data::ClearData(){
tempDigiWaveform2.clear();
}
inline void Data::ClearBuffer(){
delete buffer;
BufferSize = 0;
nByte = 0;
}
inline void Data::SaveBuffer(const char * fileName){
char saveFileName[100];
@ -182,6 +190,12 @@ inline void Data::SaveBuffer(const char * fileName){
}
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("---+--------+-----------+----------\n");
for(int ch = 0; ch < MaxNChannels; ch++){
@ -201,7 +215,7 @@ inline void Data::PrintData() const{
printf("============================= Print Data\n");
for( int ch = 0; ch < MaxNChannels ; ch++){
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++){
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{
if( verbose >= 4 ) printf("incorrect buffer header. \n");
if( verbose >= 1 ) printf("nw : %d, incorrect buffer header. \n", nw);
break;
}
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
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]];
double sec = dTime * ch2ns / 1e9;
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( 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+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]);
}
}
///============= 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
outRootFile = new TFile(outFileName, "recreate");
@ -176,6 +158,23 @@ int main(int argc, char **argv) {
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
haha = fopen(inFileName[0], "r");
@ -184,6 +183,7 @@ int main(int argc, char **argv) {
unsigned long currentTime = 0;
unsigned long oldTime = 0;
char * buffer = NULL;
do{
///========== Get 1 aggreration
@ -199,34 +199,36 @@ int main(int argc, char **argv) {
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
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);
countBdAgg ++;
if( debug) printf("==================== %d Agg\n", countBdAgg);
data->DecodeBuffer(buffer, aggSize, false, 0);
delete buffer;
data->ClearBuffer();
if( !data->IsNotRollOverFakeAgg ) continue;
currentTime = get_time();
if( debug) {
printf("~~~~~~~~~~~~~~~~ time used : %lu \n", currentTime - oldTime);
printf("~~~~~~~~~~~~~~~~ time used : %lu usec\n", currentTime - oldTime);
data->PrintStat();
}
EventBuilder(data, timeWindow, traceOn, false, debug);
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);
///printf("==================== end of loop \n");
printf("=======@@@@@@@@###############============= end of loop \n");
EventBuilder(data, timeWindow, traceOn, true, debug);
tree->Write();
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){
///printf("============= build Event\n");
if( verbose) data->PrintData();
if( verbose) {
printf("======================== Event Builder \n");
data->PrintData();
}
/// find the last event timestamp;
unsigned long long firstTimeStamp = -1;
unsigned long long lastTimeStamp = 0;
unsigned long long smallestLastTimeStamp = -1;
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];
}
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];
}
if( data->Timestamp[chI][ev] > 0 && data->Timestamp[chI][ev] < smallestLastTimeStamp ){
if( data->Timestamp[chI][ev] < smallestLastTimeStamp ){
smallestLastTimeStamp = data->Timestamp[chI][ev];
}
}
if( firstTimeStamp == -1 ) return;
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 exhaustedCh = 0;
unsigned short exhaustedCh = 0; /// to stop
bool breakFlag = false;
do {
exhaustedCh = 0;
bool breakFlag = false;
/// find the 1st event
int ch1st = -1;
unsigned long long time1st = -1;
for( int chI = 0; chI < MaxNChannels ; chI ++){
if( data->Timestamp[chI][lastEv[chI]] == 0 ) {
exhaustedCh ++;
continue;
}
if( data->NumEvents[chI] <= lastEv[chI] ) {
breakFlag = true;
exhaustedCh ++;
break;
}
if( data->NumEvents[chI] == 0 ) continue;
if( data->NumEvents[chI] <= lastEv[chI] ) continue;
if( data->Timestamp[chI][lastEv[chI]] < time1st ) {
time1st = data->Timestamp[chI][lastEv[chI]];
ch1st = chI;
}
}
if( traceOn ) arrayTrace->Clear("C");
if( breakFlag ) break;
if( !isLastData && ((smallestLastTimeStamp - time1st) < NTimeWinForBuffer * timeWin) ) 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_f[multi-1] = data->fineTime[ch1st][lastEv[ch1st]];
if( traceOn ){
arrayTrace->Clear("C");
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->Clear();
for( int hh = 0; hh < traceLength[multi-1]; 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] ++;
/// build event
/// build the rest of the event
exhaustedCh = 0;
for( int chI = ch1st; chI < ch1st + MaxNChannels; chI ++){
unsigned short chX = chI % MaxNChannels;
if( data->NumEvents[chX] == 0 ) continue;
if( data->NumEvents[chX] <= lastEv[chX] ) continue;
if( data->NumEvents[chX] == 0 ) {
exhaustedCh ++;
continue;
}
if( data->NumEvents[chX] <= lastEv[chX] ) {
exhaustedCh ++;
continue;
}
if( timeWin == 0 ) continue;
for( int ev = lastEv[chX]; ev < data->NumEvents[chX] ; ev++){
if( data->Timestamp[chX][ev] > 0 && (data->Timestamp[chX][ev] - e_t[0] ) < timeWin ) {
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];
if( traceOn ){
traceLength[multi-1] = (unsigned short) data->Waveform1[chX][ev].size();
trace = (TGraph *) arrayTrace->ConstructedAt(multi-1, "C");
trace->Clear();
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;
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);
for( int chI = 0; chI < MaxNChannels ; chI++){
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
outRootFile->cd();
tree->Fill();
evID++;
/// clear
multi = 0;
if( verbose > 0 && evID > verbose ) break;
}while( exhaustedCh < MaxNChannels - 1 );
/// Check Last event break;
breakFlag = false;
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
/// move the last data to the top,
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];
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
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
$(CC) $(COPTS) -c ClassDigitizer.cpp