added MultiBuilder.cpp/h for event building across multiple digitizer, EventKenshikushi.cpp use MultiBuilder

This commit is contained in:
splitPoleDAQ 2023-06-14 15:20:15 -04:00
parent 922b09f89a
commit b62f452d06
9 changed files with 641 additions and 13 deletions

View File

@ -153,6 +153,7 @@
"qmainwindow": "cpp",
"qchartview": "cpp",
"qthread": "cpp",
"qrandomgenerator": "cpp"
"qrandomgenerator": "cpp",
"source_location": "cpp"
}
}

View File

@ -188,7 +188,7 @@ inline void Data::ClearData(){
}
inline void Data::ClearBuffer(){
printf("==== Data::%s \n", __func__);
//printf("==== Data::%s \n", __func__);
delete buffer;
buffer = nullptr;
AllocatedSize = 0;

View File

@ -5,7 +5,7 @@ void DataReaderScript(){
Data * data = new Data();
data->DPPType = V1730_DPP_PSD_CODE;
std::string fileName = "data/temp_006_089_PSD_000.fsu";
std::string fileName = "temp_036_089_PSD_000.fsu";
FILE * haha = fopen(fileName.c_str(), "r");
fseek(haha, 0L, SEEK_END);
@ -33,23 +33,23 @@ void DataReaderScript(){
dump = fread(buffer, aggSize, 1, haha);
long fPos2 = ftell(haha);
printf("Board Agg. has %d word = %d bytes | %ld - %ld\n", aggSize/4, aggSize, fPos1, fPos2);
countBdAgg ++;
printf("==================== %d Agg\n", countBdAgg);
//printf("Board Agg. has %d word = %d bytes | %ld - %ld\n", aggSize/4, aggSize, fPos1, fPos2);
//printf("==================== %d Agg\n", countBdAgg);
data->DecodeBuffer(buffer, aggSize, false, 0); // data own the buffer
data->ClearBuffer(); // this will clear the buffer.
if( !data->IsNotRollOverFakeAgg ) continue;
//if( countBdAgg % 100 == 0)
data->PrintStat();
//if( countBdAgg % 100 == 0) data->PrintStat();
//data->ClearData();
if( countBdAgg > 1 ) break;
// if( countBdAgg > 10 ) break;
}while(!feof(haha) && ftell(haha) < inFileSize);
data->PrintStat();
data->PrintAllData();
fclose(haha);

BIN
EventKenshikushi Executable file

Binary file not shown.

346
EventKenshikushi.cpp Normal file
View File

@ -0,0 +1,346 @@
#include "ClassData.h"
#include "MultiBuilder.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TClonesArray.h"
#include "TGraph.h"
#include "TFile.h"
#include "TTree.h"
#define MAX_MULTI 100
template<typename T> void swap(T * a, T *b );
int partition(int arr[], int kaka[], TString file[], int start, int end);
void quickSort(int arr[], int kaka[], TString file[], int start, int end);
//^#############################################################
//^#############################################################
int main(int argc, char **argv) {
printf("=========================================\n");
printf("=== *.fsu Events Kenshikushi ===\n");
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(" traceOn/Off : is traces stored \n");
printf(" verbose : > 0 for debug \n");
printf(" Output file name is contructed from inFile1 \n");
return 1;
}
/// File format must be YYY...Y_runXXX_AAA_BBB_CCC.fsu
/// YYY...Y = prefix
/// XXX = runID, 3 digits
/// AAA = board Serial Number, 3 digits
/// BBB = DPPtype, 3 digits
/// CCC = over size index, 3 digits
///============= read input
unsigned int timeWindow = atoi(argv[1]);
bool traceOn = atoi(argv[2]);
unsigned int debug = atoi(argv[3]);
int nFile = argc - 4;
TString inFileName[nFile];
for( int i = 0 ; i < nFile ; i++){
inFileName[i] = argv[i+4];
}
/// Form outFileName;
TString outFileName = inFileName[0];
int pos = outFileName.Index("_");
pos = outFileName.Index("_", pos+1);
outFileName.Remove(pos);
outFileName += ".root";
printf("-------> Out file name : %s \n", outFileName.Data());
printf(" Number of Files : %d \n", nFile);
for( int i = 0; i < nFile; i++) printf("%2d | %s \n", i, inFileName[i].Data());
printf("=====================================\n");
printf(" Time Window = %u \n", timeWindow);
printf("=====================================\n");
///============= sorting file by the serial number & order
int ID[nFile]; /// serial+ order*1000;
int type[nFile];
for( int i = 0; i < nFile; i++){
int snPos = inFileName[i].Index("_"); // first "_"
//snPos = inFileName[i].Index("_", snPos + 1);
int sn = atoi(&inFileName[i][snPos+5]);
TString typeStr = &inFileName[i][snPos+9];
typeStr.Resize(3);
if( typeStr == "PHA" ) type[i] = V1730_DPP_PHA_CODE;
if( typeStr == "PSD" ) type[i] = V1730_DPP_PSD_CODE;
int order = atoi(&inFileName[i][snPos+13]);
ID[i] = sn + order * 1000;
//printf("sn:%d, type:%d (%s), order:%d \n", sn, type[i], typeStr.Data(), order);
}
quickSort(&(ID[0]), &(type[0]), &(inFileName[0]), 0, nFile-1);
for( int i = 0 ; i < nFile; i++){
printf("%d | %6d | %3d | %s \n", i, ID[i], type[i], inFileName[i].Data());
}
//*======================================= Sort files in to group
std::vector<int> snList; // store the serial number of the group
std::vector<int> typeList; // store the DPP type of the group
std::vector<std::vector<TString>> fileList; // store the file list of the group
for( int i = 0; i < nFile; i++){
if( ID[i] / 1000 == 0 ) {
std::vector<TString> temp = {inFileName[i]};
fileList.push_back(temp);
typeList.push_back(type[i]);
snList.push_back(ID[i]%1000);
}else{
for( int p = 0; p < (int) snList.size(); p++){
if( (ID[i] % 1000) == snList[p] ) {
fileList[p].push_back(inFileName[i]);
}
}
}
}
int nGroup = snList.size();
printf("===================================== nGroup %d.\n", nGroup);
for( int i = 0; i < nGroup; i++){
printf("............ %d \n", snList[i]);
for( int j = 0; j< (int) fileList[i].size(); j++){
printf("%s | %d\n", fileList[i][j].Data(), typeList[i]);
}
}
//*======================================= open raw files
printf("##############################################\n");
/// for each detector, open it
std::vector<int> inFileIndex(nGroup); // the index of the the opened file for each group
FILE ** inFile = new FILE *[nGroup];
Data ** data = new Data *[nGroup];
for( int i = 0; i < nGroup; i++){
inFile[i] = fopen(fileList[i][0], "r");
if( inFile[i] ){
inFileIndex[i] = 0;
data[i] = new Data();
data[i]->DPPType = typeList[0];
data[i]->boardSN = snList[0];
}else{
inFileIndex[i] = -1;
data[i] = nullptr;
}
}
//*====================================== create tree
TFile * outRootFile = new TFile(outFileName, "recreate");
TTree * tree = new TTree("tree", outFileName);
unsigned long long evID = 0;
unsigned short multi = 0;
unsigned short bd[MAX_MULTI] = {0}; /// boardID
unsigned short ch[MAX_MULTI] = {0}; /// chID
unsigned short e[MAX_MULTI] = {0}; /// 15 bit
unsigned short e2[MAX_MULTI] = {0}; /// 15 bit
unsigned long long e_t[MAX_MULTI] = {0}; /// timestamp 47 bit
unsigned short e_f[MAX_MULTI] = {0}; /// fine time 10 bit
tree->Branch("evID", &evID, "event_ID/l");
tree->Branch("multi", &multi, "multi/s");
tree->Branch("bd", bd, "bd[multi]/s");
tree->Branch("ch", ch, "ch[multi]/s");
tree->Branch("e", e, "e[multi]/s");
tree->Branch("e2", e2, "e2[multi]/s");
tree->Branch("e_t", e_t, "e_timestamp[multi]/l");
tree->Branch("e_f", e_f, "e_timestamp[multi]/s");
TClonesArray * arrayTrace = nullptr;
unsigned short traceLength[MAX_MULTI] = {0};
TGraph * trace = nullptr;
if( traceOn ) {
arrayTrace = new TClonesArray("TGraph");
tree->Branch("traceLength", traceLength, "traceLength[multi]/s");
tree->Branch("trace", arrayTrace, 2560000);
arrayTrace->BypassStreamer();
}
//*====================================== build events
MultiBuilder * mb = new MultiBuilder(data, typeList);
mb->SetTimeWindow(timeWindow);
///------------------ re data
char * buffer = nullptr;
unsigned int word[1]; // 4 byte = 32 bit
do{
/// fill the data class with some agg;
int aggCount = 0;
do{
for ( int i = 0; i < nGroup; i++){
if( inFile[i] == nullptr ) continue;
size_t dummy = fread(word, 4, 1, inFile[i]);
if( dummy != 1) printf("fread error, should read 4 bytes, but read %ld x 4 byte, file pos: %ld byte (%s)\n", dummy, ftell(inFile[i]), fileList[i][inFileIndex[i]].Data());
fseek(inFile[i], -4, SEEK_CUR); // rool back
short header = ((word[0] >> 28 ) & 0xF);
if( header != 0xA ) break;
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
buffer = new char[aggSize];
dummy = fread(buffer, aggSize, 1, inFile[i]);
if( dummy != 1) printf("fread error, should read %d bytes, but read %ld x %d byte, file pos: %ld byte (%s)\n", aggSize, dummy, aggSize, ftell(inFile[i]), fileList[i][inFileIndex[i]].Data());
data[i]->DecodeBuffer(buffer, aggSize, false, 0);
data[i]->ClearBuffer();
if( feof(inFile[i]) ){
fclose(inFile[i]);
inFile[i] = fopen(fileList[i][inFileIndex[i]+1], "r");
if( inFile[i] ){
inFileIndex[i]++;
}else{
inFileIndex[i] = -1;
}
}
}
aggCount++;
}while(aggCount < 10); // get 10 agg
mb->BuildEvents(0, 0, debug);
///----------- save to tree;
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
if( startIndex < 0 ) startIndex += MaxNEvent;
//printf("startIndex : %ld, %ld\n", startIndex, mb->eventIndex);
for( long i = startIndex; i <= mb->eventIndex; i++){
evID = i;
multi = mb->events[i].size();
for( int j = 0; j < multi; j ++){
bd[j] = mb->events[i][j].bd;
ch[j] = mb->events[i][j].ch;
e[j] = mb->events[i][j].energy;
e2[j] = mb->events[i][j].energy2;
e_t[j] = mb->events[i][j].timestamp;
e_f[j] = mb->events[i][j].fineTime;
if( traceOn ){
traceLength[j] = mb->events[i][j].trace.size();
trace = (TGraph *) arrayTrace->ConstructedAt(j, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[j]; hh++){
trace->SetPoint(hh, hh, mb->events[i][j].trace[hh]);
}
}
}
outRootFile->cd();
tree->Fill();
}
int okFileNum = 0;
for( int i = 0; i < nGroup; i++){
if( inFileIndex[i] != -1 ) okFileNum ++;
}
if( okFileNum == 0 ) break;
}while(true);
mb->BuildEvents(1, 0, debug);
///----------- save to tree;
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
if( startIndex < 0 ) startIndex += MaxNEvent;
//printf("startIndex : %ld, %ld\n", startIndex, mb->eventIndex);
for( long i = startIndex; i <= mb->eventIndex; i++){
evID = i;
multi = mb->events[i].size();
for( int j = 0; j < multi; j ++){
bd[j] = mb->events[i][j].bd;
ch[j] = mb->events[i][j].ch;
e[j] = mb->events[i][j].energy;
e2[j] = mb->events[i][j].energy2;
e_t[j] = mb->events[i][j].timestamp;
e_f[j] = mb->events[i][j].fineTime;
if( traceOn ){
traceLength[j] = mb->events[i][j].trace.size();
trace = (TGraph *) arrayTrace->ConstructedAt(j, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[j]; hh++){
trace->SetPoint(hh, hh, mb->events[i][j].trace[hh]);
}
}
}
outRootFile->cd();
tree->Fill();
}
tree->Write();
outRootFile->Close();
printf("========================= finsihed.\n");
printf("total events built = %llu \n", evID + 1);
printf("=======> saved to %s \n", outFileName.Data());
delete mb;
for( int i = 0 ; i < nGroup; i++){
delete data[i];
}
delete [] data;
}
//^#############################################################
//^#############################################################
template<typename T> void swap(T * a, T *b ){
T temp = * b;
*b = *a;
*a = temp;
}
int partition(int arr[], int kaka[], TString file[], int start, int end){
int pivot = arr[start];
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot) count++;
}
/// Giving pivot element its correct position
int pivotIndex = start + count;
swap(&arr[pivotIndex], &arr[start]);
swap(&file[pivotIndex], &file[start]);
swap(&kaka[pivotIndex], &kaka[start]);
/// Sorting left and right parts of the pivot element
int i = start, j = end;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {i++;}
while (arr[j] > pivot) {j--;}
if (i < pivotIndex && j > pivotIndex) {
int ip = i++;
int jm = j--;
swap( &arr[ip], &arr[jm]);
swap(&file[ip], &file[jm]);
swap(&kaka[ip], &kaka[jm]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int kaka[], TString file[], int start, int end){
/// base case
if (start >= end) return;
/// partitioning the array
int p = partition(arr, kaka, file, start, end);
/// Sorting the left part
quickSort(arr, kaka, file, start, p - 1);
/// Sorting the right part
quickSort(arr, kaka, file, p + 1, end);
}

View File

@ -12,9 +12,9 @@ CAENLIBS = -lCAENDigitizer
ROOTLIBS = `root-config --cflags --glibs`
OBJS = ClassDigitizer.o OnlineEventBuilder.o
OBJS = ClassDigitizer.o OnlineEventBuilder.o MultiBuilder.o
ALL = test test_indep EventBuilder DataGenerator
ALL = test test_indep EventBuilder DataGenerator EventKenshikushi
#########################################################################
@ -26,6 +26,9 @@ clean :
OnlineEventBuilder.o : OnlineEventBuilder.cpp OnlineEventBuilder.h
$(CC) $(COPTS) -c OnlineEventBuilder.cpp
MultiBuilder.o : MultiBuilder.cpp MultiBuilder.h
$(CC) $(COPTS) -c MultiBuilder.cpp
ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ClassData.h
$(CC) $(COPTS) -c ClassDigitizer.cpp
@ -44,3 +47,7 @@ EventBuilder : EventBuilder.cpp ClassData.h
DataGenerator : DataGenerator.cpp ClassDigitizer.o OnlineEventBuilder.o
@echo "--------- making DataGenerator"
$(CC) $(COPTS) -o DataGenerator DataGenerator.cpp ClassDigitizer.o OnlineEventBuilder.o $(CAENLIBS)
EventKenshikushi : EventKenshikushi.cpp ClassData.h MultiBuilder.o
@echo "--------- making EventKenshikushi"
$(CC) $(COPTS) -o EventKenshikushi EventKenshikushi.cpp MultiBuilder.o $(CAENLIBS) $(ROOTLIBS)

184
MultiBuilder.cpp Normal file
View File

@ -0,0 +1,184 @@
#include "MultiBuilder.h"
#include <algorithm>
MultiBuilder::MultiBuilder(Data ** inData, std::vector<int> type) : nData(type.size()){
data = inData;
typeList = type;
timeWindow = 100;
ClearEvents();
}
MultiBuilder::~MultiBuilder(){
}
void MultiBuilder::ClearEvents(){
eventIndex = -1;
eventBuilt = 0;
totalEventBuilt = 0;
for( int i = 0; i < MaxNEvent; i++) events[i].clear();
for( int i = 0; i < MaxNDigitizer; i++){
for( int j = 0; j < MaxNChannels; j++){
loopIndex[i][j] = 0;
nextIndex[i][j] = -1;
chExhaused[i][j] = false;
}
earlistDigi = -1;
earlistCh = -1;
earlistTime = -1;
latestTime = 0;
nExhaushedCh = 0;
}
}
void MultiBuilder::FindEarlistTimeAndCh(bool verbose){
earlistTime = -1;
earlistDigi = -1;
earlistCh = -1;
nExhaushedCh = 0;
for( int i = 0; i < nData; i++){
for( int j = 0; j < MaxNChannels; j++ ){
chExhaused[i][j] = false;
}
for(unsigned int ch = 0; ch < MaxNChannels; ch ++){
if( data[i]->Timestamp[ch][data[i]->DataIndex[ch]] == 0 || data[i]->DataIndex[ch] == -1 || loopIndex[i][ch] * MaxNData + nextIndex[i][ch] > data[i]->LoopIndex[ch] * MaxNData + data[i]->DataIndex[ch]) {
nExhaushedCh ++;
chExhaused[i][ch] = true;
continue;
}
if( nextIndex[i][ch] == -1 ) nextIndex[i][ch] = 0;
unsigned long long time = data[i]->Timestamp[ch][nextIndex[i][ch]];
if( time < earlistTime ) {
earlistTime = time;
earlistDigi = i;
earlistCh = ch;
}
}
}
if( verbose ) printf("%s | bd : %d, ch : %d, %llu\n", __func__, earlistDigi, earlistCh, earlistTime);
}
void MultiBuilder::FindLatestTime(bool verbose){
latestTime = 0;
int latestCh = -1;
int latestDigi = -1;
for( int i = 0; i < nData; i++){
for( unsigned ch = 0; ch < MaxNChannels; ch++ ){
int index = data[i]->DataIndex[ch];
if( index == -1 ) continue;
if( data[i]->Timestamp[ch][index] > latestTime ) {
latestTime = data[i]->Timestamp[ch][index];
latestCh = ch;
latestDigi = i;
}
}
}
if( verbose ) printf("%s | bd : %d, ch : %d, %lld \n", __func__, latestDigi, latestCh, latestTime);
}
void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){
FindLatestTime(verbose);
FindEarlistTimeAndCh(verbose);
if( earlistCh == -1 || nExhaushedCh == nData * MaxNChannels) return; /// no data
eventBuilt = 0;
//======= Start building event
EventMember em;
do{
eventIndex ++;
if( eventIndex >= MaxNEvent ) eventIndex = 0;
events[eventIndex].clear();
eventBuilt ++;
totalEventBuilt ++;
em.Clear();
for( int k = 0; k < nData; k++){
for( unsigned int i = 0; i < MaxNChannels; i++){
int ch = (i + earlistCh ) % MaxNChannels;
if( chExhaused[k][ch] ) continue;
if( loopIndex[k][ch] * MaxNData + nextIndex[k][ch] > data[k]->LoopIndex[ch] * MaxNData + data[k]->DataIndex[ch]) {
nExhaushedCh ++;
chExhaused[k][ch] = true;
continue;
}
do {
unsigned long long time = data[k]->Timestamp[ch][nextIndex[k][ch]];
if( time >= earlistTime && (time - earlistTime <= timeWindow) ){
em.bd = k; // TODO serial number
em.ch = ch;
em.energy = data[k]->Energy[ch][nextIndex[k][ch]];
em.timestamp = time;
if( !skipTrace ) em.trace = data[k]->Waveform1[ch][nextIndex[k][ch]];
if( typeList[k] == V1730_DPP_PSD_CODE ) em.energy2 = data[k]->Energy2[ch][nextIndex[k][ch]];
events[eventIndex].push_back(em);
nextIndex[k][ch]++;
if( nextIndex[k][ch] >= MaxNData) {
loopIndex[k][ch] ++;
nextIndex[k][ch] = 0;
}
}else{
break;
}
if( timeWindow == 0 ) break;
}while( true );
if( timeWindow == 0 ) break;
}
if( timeWindow == 0 ) break;
}
std::sort(events[eventIndex].begin(), events[eventIndex].end(), [](const EventMember& a, const EventMember& b) {
return a.timestamp < b.timestamp;
});
///Find the next earlist
FindEarlistTimeAndCh(verbose);
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;
int bd = events[eventIndex][i].bd;
printf("%02d, %02d | %d | %5d %llu \n", bd, chxxx, nextIndex[bd][chxxx], events[eventIndex][i].energy, events[eventIndex][i].timestamp);
}
if( nExhaushedCh == nData * MaxNChannels ) {
printf("######################### no more event to be built\n");
break;
}
printf("----- next ch : %d, next earlist Time : %llu.\n", earlistCh, earlistTime);
}
if( !isFinal && latestTime - earlistTime <= timeWindow ) {
if( verbose ) {
printf("######################### left over data for next build, latesTime : %llu.\n", latestTime);
}
break;
}
}while(nExhaushedCh < nData * MaxNChannels);
}

90
MultiBuilder.h Normal file
View File

@ -0,0 +1,90 @@
#ifndef MuLTI_BUILDER_H
#define MuLTI_BUILDER_H
#include "ClassData.h"
#define MaxNEvent 5000 // circular
class EventMember{
public:
unsigned short bd;
unsigned short ch;
unsigned short energy;
unsigned short energy2;
unsigned long long timestamp;
unsigned short fineTime;
std::vector<short> trace;
EventMember(){
Clear();
}
EventMember operator = (EventMember e){
bd = e.bd;
ch = e.ch;
energy = e.energy;
energy2 = e.energy2;
timestamp = e.timestamp;
fineTime = e.fineTime;
trace = e.trace;
return *this;
}
void Clear(){
bd = 0;
ch = 0;
energy = 0;
energy2 = 0;
timestamp = 0;
fineTime = 0;
trace.clear();
}
};
class MultiBuilder {
public:
MultiBuilder(Data ** inData, std::vector<int> type);
~MultiBuilder();
void SetTimeWindow(int ticks) {timeWindow = ticks;}
int GetTimeWindow() const{return timeWindow;}
void BuildEvents(bool isFinal = false, bool skipTrace = false, bool verbose = false);
void ClearEvents();
// void PrintStat();
long eventIndex;
long eventBuilt; // reset once call BuildEvents()
long totalEventBuilt;
std::vector<EventMember> events[MaxNEvent];
private:
std::vector<int> typeList;
const unsigned short nData;
Data ** data; // assume all data has MaxNChannel (16)
unsigned short timeWindow;
int loopIndex[MaxNDigitizer][MaxNChannels];
int nextIndex[MaxNDigitizer][MaxNChannels];
int nExhaushedCh;
bool chExhaused[MaxNDigitizer][MaxNChannels];
unsigned long long earlistTime;
unsigned long long latestTime;
int earlistDigi;
int earlistCh;
void FindEarlistTimeAndCh(bool verbose = false);
void FindLatestTime(bool verbose = false);
};
#endif

View File

@ -198,7 +198,6 @@ int main(int argc, char* argv[]){
delete dig;
/**
{///============ Checking the buffer size calculation
unsigned short B = 10; /// BLT
unsigned short Eg = 511; /// Event / dual channel
@ -246,7 +245,8 @@ int main(int argc, char* argv[]){
printf(" diff : %8u byte \n", size > 2*bSize ? size - 2*bSize : 2*bSize - size);
delete buffer;
}/**/
}
*/
//dig->GetData()->SetSaveWaveToMemory(true);