2023-06-14 15:20:15 -04:00
|
|
|
#include "MultiBuilder.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2023-06-19 12:48:17 -04:00
|
|
|
MultiBuilder::MultiBuilder(Data ** multiData, std::vector<int> type, std::vector<int> sn) : nData(type.size()){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-19 12:48:17 -04:00
|
|
|
data = multiData;
|
2023-06-14 15:20:15 -04:00
|
|
|
typeList = type;
|
2023-06-15 11:48:18 -04:00
|
|
|
snList = sn;
|
2024-08-29 14:45:11 -04:00
|
|
|
numTotCh = 0;
|
2024-01-18 23:09:50 -05:00
|
|
|
for( uShort i = 0; i < nData; i++) {
|
|
|
|
idList.push_back(i);
|
|
|
|
dataSize.push_back(data[i]->GetDataSize());
|
2024-08-29 14:45:11 -04:00
|
|
|
numTotCh += data[i]->GetNChannel();
|
2024-01-18 23:09:50 -05:00
|
|
|
}
|
2023-06-15 11:48:18 -04:00
|
|
|
timeWindow = 100;
|
2023-12-11 18:55:12 -05:00
|
|
|
leftOverTime = 100;
|
|
|
|
breakTime = -1;
|
2024-01-11 17:32:58 -05:00
|
|
|
timeJump = 1e8;
|
|
|
|
lastEventTime = 0;
|
2024-08-29 14:45:11 -04:00
|
|
|
forceStop = false;
|
2023-06-15 11:48:18 -04:00
|
|
|
ClearEvents();
|
2024-01-11 11:52:40 -05:00
|
|
|
|
2024-01-18 23:09:50 -05:00
|
|
|
|
2024-01-11 11:52:40 -05:00
|
|
|
// for( int i = 0; i < nData; i++){
|
|
|
|
// printf("sn: %d, numCh : %d \n", snList[i], data[i]->GetNChannel());
|
|
|
|
// }
|
|
|
|
|
2023-06-15 11:48:18 -04:00
|
|
|
}
|
|
|
|
|
2023-10-05 13:13:15 -04:00
|
|
|
MultiBuilder::MultiBuilder(Data * singleData, int type, int sn): nData(1){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-19 12:48:17 -04:00
|
|
|
data = new Data *[1];
|
|
|
|
data[0] = singleData;
|
2024-08-29 14:45:11 -04:00
|
|
|
numTotCh = data[0]->GetNChannel();
|
2023-06-19 12:48:17 -04:00
|
|
|
typeList.push_back(type);
|
2023-10-05 13:13:15 -04:00
|
|
|
snList.push_back(sn);
|
2023-06-19 12:48:17 -04:00
|
|
|
idList.push_back(0);
|
|
|
|
timeWindow = 100;
|
2023-12-11 18:55:12 -05:00
|
|
|
leftOverTime = 100;
|
|
|
|
breakTime = -1;
|
2024-01-11 17:32:58 -05:00
|
|
|
timeJump = 1e8;
|
|
|
|
lastEventTime = 0;
|
2024-08-29 14:45:11 -04:00
|
|
|
forceStop = false;
|
2023-06-19 12:48:17 -04:00
|
|
|
ClearEvents();
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
MultiBuilder::~MultiBuilder(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiBuilder::ClearEvents(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-14 15:20:15 -04:00
|
|
|
eventIndex = -1;
|
|
|
|
eventBuilt = 0;
|
|
|
|
totalEventBuilt = 0;
|
|
|
|
for( int i = 0; i < MaxNEvent; i++) events[i].clear();
|
|
|
|
|
|
|
|
for( int i = 0; i < MaxNDigitizer; i++){
|
2023-11-21 17:17:31 -05:00
|
|
|
for( int j = 0; j < MaxNChannels; j++){
|
2023-06-14 15:20:15 -04:00
|
|
|
nextIndex[i][j] = -1;
|
|
|
|
chExhaused[i][j] = false;
|
2024-08-29 14:45:11 -04:00
|
|
|
lastBackWardIndex[i][j] = 0;
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
earlistDigi = -1;
|
|
|
|
earlistCh = -1;
|
|
|
|
earlistTime = -1;
|
|
|
|
latestTime = 0;
|
|
|
|
|
|
|
|
nExhaushedCh = 0;
|
|
|
|
}
|
2023-06-20 11:57:39 -04:00
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
2023-06-19 12:48:17 -04:00
|
|
|
void MultiBuilder::PrintStat(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-19 12:48:17 -04:00
|
|
|
printf("Total number of evet built : %ld\n", totalEventBuilt);
|
|
|
|
for( int i = 0; i < nData ; i++){
|
2023-11-21 17:17:31 -05:00
|
|
|
for( int ch = 0; ch < data[i]->GetNChannel() ; ch++){
|
2024-10-22 12:56:26 -04:00
|
|
|
if( nextIndex[i][ch] >= 0 ) printf("%d %3d %2d | %7ld\n", i, snList[i], ch, nextIndex[i][ch]);
|
2023-06-19 12:48:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 18:03:10 -05:00
|
|
|
void MultiBuilder::PrintAllEvent(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-11-21 18:03:10 -05:00
|
|
|
printf("Total number of evet built : %ld\n", totalEventBuilt);
|
|
|
|
for( int i = 0; i < totalEventBuilt; i++){
|
|
|
|
printf("%5d ------- size: %ld\n", i, events[i].size());
|
|
|
|
for( int j = 0; j < (int)events[i].size(); j++){
|
|
|
|
events[i][j].Print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
//^############################################### forward event builder
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
void MultiBuilder::FindEarlistTimeAndCh(bool verbose){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-06-14 15:20:15 -04:00
|
|
|
earlistTime = -1;
|
|
|
|
earlistDigi = -1;
|
|
|
|
earlistCh = -1;
|
|
|
|
nExhaushedCh = 0;
|
|
|
|
for( int i = 0; i < nData; i++){
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
for( int j = 0; j < data[i]->GetNChannel(); j++ ) chExhaused[i][j] = false;
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2024-03-13 17:07:34 -04:00
|
|
|
for(unsigned int ch = 0; ch < data[i]->GetNChannel(); ch ++){
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
{// check is dataIndex is valid
|
|
|
|
int index = data[i]->GetDataIndex(ch);
|
|
|
|
if( index < 0 ) {
|
|
|
|
nExhaushedCh ++;
|
|
|
|
chExhaused[i][ch] = true;
|
|
|
|
continue;
|
|
|
|
}
|
2023-11-21 17:17:31 -05:00
|
|
|
|
2024-10-21 19:53:42 -04:00
|
|
|
if( data[i]->GetTimestamp(ch, index) == 0 || nextIndex[i][ch] > data[i]->GetAbsDataIndex(ch)) {
|
2024-08-29 14:45:11 -04:00
|
|
|
nExhaushedCh ++;
|
|
|
|
chExhaused[i][ch] = true;
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( nextIndex[i][ch] == -1 ) nextIndex[i][ch] = 0;
|
|
|
|
|
2024-01-26 14:53:52 -05:00
|
|
|
unsigned long long time = data[i]->GetTimestamp(ch, nextIndex[i][ch]);
|
2023-06-14 15:20:15 -04:00
|
|
|
if( time < earlistTime ) {
|
|
|
|
earlistTime = time;
|
|
|
|
earlistDigi = i;
|
|
|
|
earlistCh = ch;
|
|
|
|
}
|
2024-08-29 14:45:11 -04:00
|
|
|
// printf(" ch : %d | time %llu | %llu\n", ch, time, earlistTime);
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose ) printf("%s | bd : %d, ch : %d, %llu\n", __func__, earlistDigi, earlistCh, earlistTime);
|
2023-06-15 11:48:18 -04:00
|
|
|
}
|
|
|
|
|
2023-12-12 15:35:47 -05:00
|
|
|
void MultiBuilder::FindEarlistTimeAmongLastData(bool verbose){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-12-12 15:35:47 -05:00
|
|
|
latestTime = -1;
|
|
|
|
latestCh = -1;
|
|
|
|
latestDigi = -1;
|
|
|
|
for( int i = 0; i < nData; i++){
|
|
|
|
for( unsigned ch = 0; ch < data[i]->GetNChannel(); ch++ ){
|
2024-01-18 23:09:50 -05:00
|
|
|
if( chExhaused[i][ch] ) continue;
|
2024-01-26 14:53:52 -05:00
|
|
|
int index = data[i]->GetDataIndex(ch);
|
2023-12-12 15:35:47 -05:00
|
|
|
if( index == -1 ) continue;
|
2024-01-26 14:53:52 -05:00
|
|
|
if( data[i]->GetTimestamp(ch, index) < latestTime ) {
|
|
|
|
latestTime = data[i]->GetTimestamp(ch, index);
|
2023-12-12 15:35:47 -05:00
|
|
|
latestCh = ch;
|
|
|
|
latestDigi = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( verbose ) printf("%s | bd : %d, ch : %d, %lld \n", __func__, latestDigi, latestCh, latestTime);
|
|
|
|
}
|
2024-09-30 18:38:48 -04:00
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2024-08-29 14:45:11 -04:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
FindEarlistTimeAndCh(verbose); //Give the earliest time, ch, digi
|
|
|
|
|
2024-08-06 17:28:25 -04:00
|
|
|
FindEarlistTimeAmongLastData(verbose); // give lastest Time, Ch, and Digi for event building
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
if( earlistCh == -1 || nExhaushedCh == numTotCh) return; /// no data
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
eventBuilt = 0;
|
|
|
|
//======= Start building event
|
2023-06-23 13:50:21 -04:00
|
|
|
Hit em;
|
2023-06-14 15:20:15 -04:00
|
|
|
do{
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
if( forceStop ) break;
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
eventIndex ++;
|
|
|
|
if( eventIndex >= MaxNEvent ) eventIndex = 0;
|
|
|
|
events[eventIndex].clear();
|
|
|
|
em.Clear();
|
|
|
|
|
|
|
|
for( int k = 0; k < nData; k++){
|
2023-06-30 17:32:09 -04:00
|
|
|
int bd = (k + earlistDigi) % nData;
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
// printf("##### %d/%d | ", bd, nData);
|
|
|
|
// data[bd]->PrintAllData(true);
|
2023-06-30 17:32:09 -04:00
|
|
|
|
2024-01-11 11:52:40 -05:00
|
|
|
const int numCh = data[bd]->GetNChannel();
|
2023-11-21 17:17:31 -05:00
|
|
|
|
|
|
|
for( int i = 0; i < numCh; i++){
|
|
|
|
int ch = (i + earlistCh ) % numCh;
|
2024-01-11 11:52:40 -05:00
|
|
|
// printf("ch : %d | exhaused ? %s \n", ch, chExhaused[bd][ch] ? "Yes" : "No");
|
2023-06-30 17:32:09 -04:00
|
|
|
if( chExhaused[bd][ch] ) continue;
|
2024-09-30 18:38:48 -04:00
|
|
|
|
|
|
|
// printf(" ch : %2d | %d(%d) | %d(%d)\n", ch, loopIndex[bd][ch], nextIndex[bd][ch], data[bd]->GetLoopIndex(ch), data[bd]->GetDataIndex(ch) );
|
|
|
|
|
2024-10-21 19:53:42 -04:00
|
|
|
if( nextIndex[bd][ch] == -1 || nextIndex[bd][ch] > data[bd]->GetAbsDataIndex(ch)) {
|
2023-06-14 15:20:15 -04:00
|
|
|
nExhaushedCh ++;
|
2023-06-30 17:32:09 -04:00
|
|
|
chExhaused[bd][ch] = true;
|
2024-09-30 18:38:48 -04:00
|
|
|
|
|
|
|
// printf(" ch : %d exhaused\n", ch);
|
2023-06-14 15:20:15 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
2024-01-26 14:53:52 -05:00
|
|
|
unsigned long long time = data[bd]->GetTimestamp(ch, nextIndex[bd][ch]);
|
2024-09-30 18:38:48 -04:00
|
|
|
// printf("%6d, sn: %5d, ch: %2d, timestamp : %16llu | earlistTime : %16llu | timeWindow : %u \n", nextIndex[bd][ch], data[bd]->boardSN, ch, time, earlistTime, timeWindow);
|
2024-01-11 17:32:58 -05:00
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
if( time >= earlistTime && (time - earlistTime <= timeWindow) ){
|
2023-06-30 17:32:09 -04:00
|
|
|
em.sn = snList[bd];
|
2023-06-14 15:20:15 -04:00
|
|
|
em.ch = ch;
|
2024-01-26 14:53:52 -05:00
|
|
|
em.energy = data[bd]->GetEnergy(ch, nextIndex[bd][ch]);
|
2023-06-14 15:20:15 -04:00
|
|
|
em.timestamp = time;
|
2024-01-26 14:53:52 -05:00
|
|
|
em.fineTime = data[bd]->GetFineTime(ch, nextIndex[bd][ch]);
|
2023-06-23 13:50:21 -04:00
|
|
|
|
2023-06-30 17:32:09 -04:00
|
|
|
if( !skipTrace ) em.trace = data[bd]->Waveform1[ch][nextIndex[bd][ch]];
|
2024-03-02 00:33:08 -05:00
|
|
|
if( typeList[bd] == DPPTypeCode::DPP_PSD_CODE ) em.energy2 = data[bd]->GetEnergy2(ch, nextIndex[bd][ch]);
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
events[eventIndex].push_back(em);
|
2023-06-30 17:32:09 -04:00
|
|
|
nextIndex[bd][ch]++;
|
2024-10-21 19:53:42 -04:00
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
}else{
|
|
|
|
break;
|
|
|
|
}
|
2024-09-30 18:38:48 -04:00
|
|
|
if( timeWindow == 0 ) break;
|
2023-06-14 15:20:15 -04:00
|
|
|
}while( true );
|
2024-09-30 18:38:48 -04:00
|
|
|
if( timeWindow == 0 ) break;
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
2024-09-30 18:38:48 -04:00
|
|
|
if( timeWindow == 0 ) break;
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
if( events[eventIndex].size() == 0 ) {
|
|
|
|
if( eventIndex > 1) {
|
|
|
|
eventIndex --;
|
|
|
|
}else{
|
|
|
|
eventIndex = MaxNEvent - 1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2024-08-01 17:03:50 -04:00
|
|
|
|
2024-01-11 17:32:58 -05:00
|
|
|
if( events[eventIndex].size() > 1) {
|
2023-06-30 17:32:09 -04:00
|
|
|
std::sort(events[eventIndex].begin(), events[eventIndex].end(), [](const Hit& a, const Hit& b) {
|
|
|
|
return a.timestamp < b.timestamp;
|
|
|
|
});
|
|
|
|
}
|
2024-01-11 17:32:58 -05:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
// lastEventTime = events[eventIndex].back().timestamp;
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
///Find the next earlist
|
2024-01-11 11:52:40 -05:00
|
|
|
FindEarlistTimeAndCh(false);
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2024-01-11 17:32:58 -05:00
|
|
|
// //if there is a time jump, say, bigger than TimeJump. break
|
2024-09-30 18:38:48 -04:00
|
|
|
// if( earlistTime - lastEventTime > timeJump ) {
|
|
|
|
// if( verbose ){
|
|
|
|
// printf("!!!!!!!! Time Jump detected stop event building and get more data.\n");
|
|
|
|
// printf("event index : %6lu, last event time : %16llu\n", eventIndex, lastEventTime);
|
|
|
|
// printf(" %6s earilest time : %16llu \n", "", earlistTime);
|
|
|
|
// printf(" %6s time jump > %16llu \n", "", timeJump);
|
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// }
|
2024-01-11 17:32:58 -05:00
|
|
|
|
2024-08-06 17:28:25 -04:00
|
|
|
eventBuilt ++;
|
|
|
|
totalEventBuilt ++;
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
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++){
|
|
|
|
int chxxx = events[eventIndex][i].ch;
|
2024-03-02 00:33:08 -05:00
|
|
|
int sn = events[eventIndex][i].sn;
|
|
|
|
int bd = 0;
|
|
|
|
for( int pp = 0; pp < nData; pp++){
|
|
|
|
if( sn == data[pp]->boardSN ) {
|
|
|
|
bd = pp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 12:56:26 -04:00
|
|
|
printf("%05d, %02d | %7ld | %5d %llu \n", sn, chxxx, nextIndex[bd][chxxx], events[eventIndex][i].energy, events[eventIndex][i].timestamp);
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
if( nExhaushedCh == numTotCh ) {
|
2023-06-14 15:20:15 -04:00
|
|
|
printf("######################### no more event to be built\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-06-30 17:32:09 -04:00
|
|
|
printf("----- next bd : %d, ch : %d, next earlist Time : %llu.\n", earlistDigi, earlistCh, earlistTime);
|
2024-01-18 18:55:37 -05:00
|
|
|
//printf("leftOver %llu, breakTime %llu \n", leftOverTime, breakTime);
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
2023-12-11 18:55:12 -05:00
|
|
|
if( !isFinal ){
|
|
|
|
if( latestTime - earlistTime <= leftOverTime){
|
|
|
|
if( verbose ) printf("######################### left over data for next build, latesTime : %llu. | leftOverTime : %llu\n", latestTime, leftOverTime);
|
|
|
|
break;
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
2023-12-11 18:55:12 -05:00
|
|
|
|
|
|
|
if( earlistTime > breakTime ) {
|
|
|
|
if( verbose ) printf("######################### left over data for next build, earlistTime : %llu. | breakTime : %llu\n", earlistTime, breakTime);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
2024-09-30 18:38:48 -04:00
|
|
|
}while(nExhaushedCh < numTotCh);
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
forceStop = false;
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
2023-06-15 11:48:18 -04:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
//^############################################### backward event builder
|
|
|
|
|
|
|
|
void MultiBuilder::FindLatestTimeAndCh(bool verbose){
|
|
|
|
DebugPrint("%s", "MultiBuilder");
|
|
|
|
latestTime = 0;
|
|
|
|
latestDigi = -1;
|
|
|
|
latestCh = -1;
|
|
|
|
|
|
|
|
nExhaushedCh = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i < nData; i++){
|
|
|
|
for( int j = 0; j < data[i]->GetNChannel(); j++ ) chExhaused[i][j] = false;
|
|
|
|
|
|
|
|
for(unsigned int ch = 0; ch < data[i]->GetNChannel(); ch ++){
|
|
|
|
if( nextIndex[i][ch] < 0 || data[i]->GetDataIndex(ch) < 0 || nextIndex[i][ch] <= lastBackWardIndex[i][ch] ) {
|
|
|
|
nExhaushedCh ++;
|
|
|
|
chExhaused[i][ch] = true;
|
|
|
|
// printf(", exhanshed. %d \n", nExhaushedCh);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long time = data[i]->GetTimestamp(ch, nextIndex[i][ch]);
|
|
|
|
// printf(", time : %llu\n", time );
|
|
|
|
if( time > latestTime ) {
|
|
|
|
latestTime = time;
|
|
|
|
latestDigi = i;
|
|
|
|
latestCh = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose ) printf("%s | bd : %d, ch : %d, %llu\n", __func__, latestDigi, latestCh, latestTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiBuilder::FindLatestTimeOfData(bool verbose){
|
|
|
|
DebugPrint("%s", "MultiBuilder");
|
|
|
|
latestTime = 0;
|
|
|
|
latestCh = -1;
|
|
|
|
latestDigi = -1;
|
|
|
|
for( int i = 0; i < nData; i++){
|
|
|
|
// printf("%s | digi-%d-th | %d\n", __func__, i, data[i]->GetNChannel());
|
|
|
|
for( unsigned ch = 0; ch < data[i]->GetNChannel(); ch++ ){
|
|
|
|
int index = data[i]->GetDataIndex(ch);
|
|
|
|
// printf("ch-%2d | index : %d \n", ch, index);
|
|
|
|
if( index == -1 ) continue;
|
|
|
|
if( data[i]->GetTimestamp(ch, index) > latestTime ) {
|
|
|
|
latestTime = data[i]->GetTimestamp(ch, index);
|
|
|
|
latestCh = ch;
|
|
|
|
latestDigi = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( verbose ) printf("%s | bd : %d, ch : %d, %lld \n", __func__, latestDigi, latestCh, latestTime);
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:57:39 -04:00
|
|
|
void MultiBuilder::BuildEventsBackWard(int maxNumEvent, bool verbose){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "MultiBuilder");
|
2023-11-21 16:50:22 -05:00
|
|
|
//skip trace, and only build for maxNumEvent events max
|
2023-06-19 12:48:17 -04:00
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
// Get the last data index and loop index
|
|
|
|
|
2023-06-19 12:48:17 -04:00
|
|
|
for( int k = 0; k < nData; k++){
|
2023-12-08 17:03:52 -05:00
|
|
|
for( int i = 0; i < data[k]->GetNChannel(); i++){
|
2024-10-21 19:53:42 -04:00
|
|
|
nextIndex[k][i] = data[k]->GetAbsDataIndex(i);
|
2023-06-19 12:48:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 11:48:18 -04:00
|
|
|
FindLatestTimeAndCh(verbose);
|
|
|
|
|
|
|
|
//========== build event
|
|
|
|
eventBuilt = 0;
|
2023-06-23 13:50:21 -04:00
|
|
|
Hit em;
|
2023-06-15 11:48:18 -04:00
|
|
|
do{
|
2024-08-29 14:45:11 -04:00
|
|
|
if( forceStop ) break;
|
2023-06-15 11:48:18 -04:00
|
|
|
eventIndex ++;
|
|
|
|
if( eventIndex >= MaxNEvent ) eventIndex = 0;
|
|
|
|
events[eventIndex].clear();
|
|
|
|
|
|
|
|
em.Clear();
|
|
|
|
|
|
|
|
for( int k = 0; k < nData; k++){
|
2023-06-30 17:32:09 -04:00
|
|
|
int bd = (k + latestDigi) % nData;
|
|
|
|
|
2023-11-21 17:17:31 -05:00
|
|
|
const int numCh = data[k]->GetNChannel();
|
|
|
|
|
|
|
|
for( int i = 0; i < numCh; i++){
|
|
|
|
int ch = (i + latestCh) % numCh;
|
2023-06-30 17:32:09 -04:00
|
|
|
if( chExhaused[bd][ch] ) continue;
|
2024-08-29 14:45:11 -04:00
|
|
|
if( nextIndex[bd][ch] <= lastBackWardIndex[bd][ch] || nextIndex[bd][ch] <= 0){
|
2023-06-15 11:48:18 -04:00
|
|
|
nExhaushedCh ++;
|
2024-08-29 14:45:11 -04:00
|
|
|
chExhaused[bd][ch] = true;
|
2023-06-15 11:48:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
|
2024-01-26 14:53:52 -05:00
|
|
|
unsigned long long time = data[bd]->GetTimestamp(ch, nextIndex[bd][ch]);
|
2023-06-15 11:48:18 -04:00
|
|
|
if( time <= latestTime && (latestTime - time <= timeWindow)){
|
2023-06-30 17:32:09 -04:00
|
|
|
em.sn = snList[bd];
|
2023-06-15 11:48:18 -04:00
|
|
|
em.ch = ch;
|
2024-01-26 14:53:52 -05:00
|
|
|
em.energy = data[bd]->GetEnergy(ch, nextIndex[bd][ch]);
|
2023-06-15 11:48:18 -04:00
|
|
|
em.timestamp = time;
|
2024-03-02 00:33:08 -05:00
|
|
|
if( typeList[bd] == DPPTypeCode::DPP_PSD_CODE ) em.energy2 = data[bd]->GetEnergy2(ch, nextIndex[bd][ch]);
|
2023-06-15 11:48:18 -04:00
|
|
|
|
|
|
|
events[eventIndex].push_back(em);
|
2023-06-30 17:32:09 -04:00
|
|
|
nextIndex[bd][ch]--;
|
2024-10-21 19:53:42 -04:00
|
|
|
// if( nextIndex[bd][ch] < 0 && data[bd]->GetLoopIndex(ch) > 0 ) nextIndex[bd][ch] = dataSize[bd] - 1;
|
2023-06-15 11:48:18 -04:00
|
|
|
|
|
|
|
}else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( timeWindow == 0 ) break;
|
|
|
|
}while(true);
|
|
|
|
if( timeWindow == 0 ) break;
|
|
|
|
}
|
|
|
|
if( timeWindow == 0 ) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FindLatestTimeAndCh(verbose);
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
if( verbose ) printf(" nExhaushedCh %d | numToCh %d \n", nExhaushedCh, numTotCh);
|
|
|
|
if( nExhaushedCh == numTotCh ) {
|
|
|
|
if( verbose ) printf("######################### no more event to be built\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( verbose ) printf("----- next bd: %d, ch : %d, next latest Time : %llu.\n", latestDigi, latestCh, latestTime);
|
|
|
|
|
|
|
|
if( events[eventIndex].size() > 0 ) {
|
|
|
|
eventBuilt ++;
|
|
|
|
totalEventBuilt ++;
|
|
|
|
|
|
|
|
std::sort(events[eventIndex].begin(), events[eventIndex].end(), [](const Hit& a, const Hit& b) {
|
|
|
|
return a.timestamp < b.timestamp;
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-15 11:48:18 -04:00
|
|
|
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++){
|
|
|
|
int chxxx = events[eventIndex][i].ch;
|
2024-03-02 00:33:08 -05:00
|
|
|
int sn = events[eventIndex][i].sn;
|
|
|
|
int bd = 0;
|
|
|
|
for( int pp = 0; pp < nData; pp++){
|
|
|
|
if( sn == data[pp]->boardSN ) {
|
|
|
|
bd = pp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 12:56:26 -04:00
|
|
|
printf("%5d, %02d | %7ld | %5d %llu \n", sn, chxxx, nextIndex[bd][chxxx], events[eventIndex][i].energy, events[eventIndex][i].timestamp);
|
2023-06-15 11:48:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
}while(nExhaushedCh < numTotCh && eventBuilt < maxNumEvent);
|
2023-06-15 11:48:18 -04:00
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
forceStop = false;
|
|
|
|
|
|
|
|
// remember the end of DataIndex, prevent over build
|
|
|
|
for( int k = 0; k < nData; k++){
|
|
|
|
for( int i = 0; i < data[k]->GetNChannel(); i++){
|
2024-10-21 19:53:42 -04:00
|
|
|
lastBackWardIndex[k][i] = data[k]->GetAbsDataIndex(i);
|
2024-08-29 14:45:11 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-15 11:48:18 -04:00
|
|
|
|
|
|
|
}
|