improve the online event builder.

This commit is contained in:
splitPoleDAQ 2023-05-26 15:01:54 -04:00
parent 197a49f678
commit 322cbca165
4 changed files with 79 additions and 66 deletions

View File

@ -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]);
}

View File

@ -15,45 +15,52 @@ int main(){
std::mt19937 gen(rd());
std::uniform_int_distribution<int> RanNext(0, 1);
std::uniform_int_distribution<int> RanCh(1, 2);
std::uniform_int_distribution<int> RanCh(0, 4);
std::uniform_int_distribution<unsigned short> RanEnergy(1, 1000);
std::uniform_int_distribution<unsigned long long> 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;
}

View File

@ -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;
}

View File

@ -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();
};