FSUDAQ/DAQ/EventBuilder_raw.cpp

447 lines
16 KiB
C++
Raw Normal View History

2023-02-08 12:00:30 -05:00
#include "macro.h"
#include "ClassData.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TClonesArray.h"
#include "TGraph.h"
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TSystem.h"
#define MAX_MULTI 100
#define MAX_File 100
#define NTimeWinForBuffer 3
char* path_to_run="/home/bavarians/FSUDAQ_MUSIC/Run/";
char* path_to_DAQ="/home/bavarians/FSUDAQ_MUSIC/";
char* path_to_con="/home/bavarians/FSUDAQ_MUSIC/Conversion/Raw/";
TFile * outRootFile = NULL;
TTree * tree = NULL;
unsigned long long evID = 0;
unsigned short multi_evt=0;
unsigned short bd[MAX_MULTI] = {0}; /// boardPort
unsigned short ch[MAX_MULTI] = {0}; /// chID
unsigned short e[MAX_MULTI] = {0}; /// 15 bit
unsigned long long e_t[MAX_MULTI] = {0}; /// timestamp 47 bit --> to get en sec *4e-9
unsigned short e_f[MAX_MULTI] = {0}; /// fine time 10 bit
/// using TClonesArray to hold the trace in TGraph
TClonesArray * arrayTrace = NULL;
unsigned short traceLength[MAX_MULTI] = {0};
TGraph * trace = NULL;
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);
void EventBuilder_raw(Data * data[],int nFile, const unsigned int timeWin, bool traceOn = false, bool isLastData = false, unsigned int verbose = 0);
int main(int argc, char **argv) {
printf("=====================================\n");
printf("=== *.fsu Events Builder ===\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 : in microsecond, default = 1 \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 Run_XXX_BB_YYY.fsu
/// XXX = 3 digits, run number
/// BB = board number, 2 digits
/// YYY = 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 += Form("_%03u",atoi(&inFileName[0][inFileName[0].Index("B")+5]));
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("_");
snPos = inFileName[i].Index("_", snPos+1);
int sn = atoi(&inFileName[i][snPos+1]);
type[i] = atoi(&inFileName[i][snPos+5]);
int order = atoi(&inFileName[i][snPos+9]);
ID[i] = sn + order * 1000;
}
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());
}
///=============== Separate files
std::vector<int> idCat;
std::vector<std::vector<int>> typeCat;
std::vector<std::vector<TString>> fileCat;
for( int i = 0; i < nFile; i++){
if( ID[i] / 1000 == 0 ) {
std::vector<TString> temp = {inFileName[i]};
std::vector<int> temp2 = {type[i]};
fileCat.push_back(temp);
typeCat.push_back(temp2);
idCat.push_back(ID[i]%1000);
}else{
for( int p = 0; p < (int) idCat.size(); p++){
if( (ID[i] % 1000) == idCat[p] ) {
fileCat[p].push_back(inFileName[i]);
typeCat[p].push_back(type[i]);
}
}
}
}
printf("=====================================\n");
for( int i = 0; i < (int) idCat.size(); i++){
printf("............ %d \n", idCat[i]);
for( int j = 0; j< (int) fileCat[i].size(); j++){
printf("%s | %d\n", fileCat[i][j].Data(), typeCat[i][j]);
}
}
///============= Set Root Tree
gSystem->cd(path_to_con);
outRootFile = new TFile(outFileName, "recreate");
// gSystem->cd(path_to_DAQ);
tree = new TTree("tree", outFileName);
tree->Branch("evID", &evID, "event_ID/l");
tree->Branch("multi_evt", &multi_evt, "multi_evt/s");
tree->Branch("bd", bd, "bd[multi_evt]/s");
tree->Branch("ch", ch, "ch[multi_evt]/s");
tree->Branch("e", e, "e[multi_evt]/s");
tree->Branch("e_t", e_t, "e_timestamp[multi_evt]/l");
tree->Branch("e_f", e_f, "e_timestamp[multi_evt]/s");
if( traceOn ) {
arrayTrace = new TClonesArray("TGraph");
tree->Branch("traceLength", traceLength, "traceLength[multi_evt]/s");
tree->Branch("trace", arrayTrace, 2560000);
arrayTrace->BypassStreamer();
}
///============= Open input Files
printf("##############################################\n");
gSystem->cd(path_to_run);
printf("#### number of files: %i \n", nFile);
FILE * haha[nFile];
size_t inFileSize[nFile];
for( int i = 0; i < nFile; i++){
haha[i]= fopen(fileCat[i][0], "r");
if( haha[i] == NULL ){
printf("#### Cannot open file : %s. Abort.\n", fileCat[i][0].Data());
return -1;
}
fseek(haha[i], 0L, SEEK_END);
inFileSize[i] = ftell(haha[i]);
printf("%s | file size : %d Byte = %.2f MB\n", fileCat[i][0].Data(), (int) inFileSize[i], inFileSize[i]/1024./1024.);
fclose(haha[i]);
}
///============= Main Loop
gSystem->cd(path_to_run);
int countBdAgg = 0;
unsigned long currentTime = 0;
unsigned long oldTime = 0;
char * buffer = NULL;
Data * data[nFile];
for( int i = 0; i < nFile; i++){
haha[i] = fopen(inFileName[i], "r");
data[i] = new Data();
data[i]->DPPType ;//typeCat[0][0];
// data->boardSN = atoi(&inFileName[0][inFileName[0].Index("_")+1]); //idCat[0];
data[i]->boardSN = atoi(&inFileName[i][inFileName[i].Index("B")+1]); //idCat[0];
data[i]->SetSaveWaveToMemory(true);
// fclose(haha[i]);
}
int end_of_loop=1;
do{
for( int i = 0; i < nFile; i++){
///========== Get 1 aggreration for each file
oldTime = get_time();
if( debug) printf("*********************** file pos : %d, %lu\n", (int) ftell(haha[i]), oldTime);
unsigned int word[1]; /// 4 bytes
size_t dump = fread(word, 4, 1, haha[i]);
fseek(haha[i], -4, SEEK_CUR);
short header = ((word[0] >> 28 ) & 0xF);
if( header != 0xA ) break;
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
if( debug) printf("Board Agg. has %d word = %d bytes\n", aggSize/4, aggSize);
buffer = new char[aggSize];
dump = fread(buffer, aggSize, 1, haha[i]);
countBdAgg ++;
if( debug) printf("==================== %d Agg\n", countBdAgg);
data[i]->DecodeBuffer(buffer, aggSize,false, 0);//false
if(!data[i]->IsNotRollOverFakeAgg ) continue;
currentTime = get_time();
if( debug) {
printf("~~~~~~~~~~~~~~~~ time used : %lu usec\n", currentTime - oldTime);
}
}
EventBuilder_raw(data, nFile, timeWindow, traceOn, false, debug);
if( debug) printf("---------- event built : %llu \n", evID);
for( int i = 0; i < nFile; i++){
data[i]->ClearBuffer();
//if( countBdAgg > 74) break;
end_of_loop= end_of_loop*(!feof(haha[i]) && ftell(haha[i]) < inFileSize[i]);
}
}while(end_of_loop==1);
for( int i = 0; i < nFile; i++){ fclose(haha[i]);}
printf("=======@@@@@@@@###############============= end of loop \n");
EventBuilder_raw(data, nFile,timeWindow, traceOn, true, debug);
gSystem->cd(path_to_con);
tree->Write();
outRootFile->Close();
printf("========================= finsihed.\n");
printf("total events built = %llu \n", evID);
printf("=======> saved to %s \n", outFileName.Data());
}
void EventBuilder_raw(Data * data[], int nFile, const unsigned int timeWin, bool traceOn, bool isLastData, unsigned int verbose){
if( verbose) {
printf("======================== Event Builder \n");
}
/// find the last event timestamp;
long long firstTimeStamp = -1;
unsigned long long lastTimeStamp = 0;
long long smallestLastTimeStamp = -1;
unsigned int maxNumEvent[nFile] ;
for( int b = 0; b < nFile ; b++){
maxNumEvent[b] = 0;
for( int chI = 0; chI < MaxNChannels ; chI ++){
if(data[b]->NumEvents[chI] == 0 ) continue;
if(data[b]->Timestamp[chI][0] < firstTimeStamp ) {
firstTimeStamp = data[b]->Timestamp[chI][0];
}
unsigned short ev = data[b]->NumEvents[chI]-1;
if( data[b]->Timestamp[chI][ev] > lastTimeStamp ) {
lastTimeStamp = data[b]->Timestamp[chI][ev];
}
if( ev + 1 > maxNumEvent[b] ) maxNumEvent[b] = ev + 1;
if( data[b]->Timestamp[chI][ev] < smallestLastTimeStamp ){
smallestLastTimeStamp = data[b]->Timestamp[chI][ev];
}
}
if( maxNumEvent[b] == 0 ) return;
}
if( verbose) printf("================ time range : %llu - %llu, smallest Last %llu\n", firstTimeStamp, lastTimeStamp, smallestLastTimeStamp);
unsigned short lastEv[nFile][MaxNChannels] = {0}; /// store the last event number for each ch
unsigned short exhaustedCh[nFile] = {0}; /// when exhaustedCh == MaxNChannels ==> stop
bool singleChannelExhaustedFlag[nFile] = {false}; /// when a single ch has data but exhaused ==> stop
unsigned short exhaustedBd = 0;
bool BoardExhaustedFlag[nFile] = {false};
for( int b = 0; b < nFile ; b++){
singleChannelExhaustedFlag[b]=false;
exhaustedCh[b]=0;
for( int c = 0; c < MaxNChannels; c++){
lastEv[b][c]=0;
}
}
do {
/// find the 1st event
unsigned long long time1st=-1;
int ch1st=-1; int b1st=-1;
for( int b = 0; b < nFile ; b++){
for( int chI = 0; chI < MaxNChannels ; chI ++){
if( data[b]->NumEvents[chI] == 0 ) continue;
if( data[b]->NumEvents[chI] <= lastEv[b][chI] ) continue;
if( data[b]->Timestamp[chI][lastEv[b][chI]] < time1st ) {
time1st= data[b]->Timestamp[chI][lastEv[b][chI]];
ch1st = chI; b1st=b;
}
}
}
//&& maxNumEvent[b] < MaxNData * 0.6
if( !isLastData && ((smallestLastTimeStamp - time1st) < NTimeWinForBuffer * timeWin) ) break;
if( ch1st > MaxNChannels ) break;
if( b1st < 0 ) break;
bd[multi_evt]= data[b1st]->boardSN;
ch[multi_evt]= ch1st;
e[multi_evt]= data[b1st]->Energy[ch1st][lastEv[b1st][ch1st]];
e_t[multi_evt]= data[b1st]->Timestamp[ch1st][lastEv[b1st][ch1st]];
e_f[multi_evt]= data[b1st]->fineTime[ch1st][lastEv[b1st][ch1st]];
if( traceOn ){
arrayTrace->Clear("C");
traceLength[multi_evt] = (unsigned short) data[b1st]->Waveform1[ch1st][lastEv[b1st][ch1st]].size();
trace = (TGraph *) arrayTrace->ConstructedAt(multi_evt, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[multi_evt]; hh++){
trace->SetPoint(hh, hh, data[b1st]->Waveform1[ch1st][lastEv[b1st][ch1st]][hh]);
}
}
multi_evt ++;
lastEv[b1st][ch1st] ++;
/// build the rest of the event
exhaustedBd=0;
for( int b = 0; b < nFile ; b++){
exhaustedCh[b] = 0;
singleChannelExhaustedFlag[b] = false;
//for( int chI = ch1st[b]; chI < ch1st[b] + MaxNChannels; chI ++){
//unsigned short chX = chI % MaxNChannels;
for( int chI = 0; chI < MaxNChannels; chI ++){
if( data[b]->NumEvents[chI] == 0 ) {
exhaustedCh[b] ++;
continue;
}
if(data[b]->NumEvents[chI] <= lastEv[b][chI] ) {
exhaustedCh[b] ++;
singleChannelExhaustedFlag[b] = true;
continue;
}
if(singleChannelExhaustedFlag[b] && exhaustedCh[b] >= MaxNChannels){BoardExhaustedFlag[b]=true;}
if( timeWin == 0 ) continue;
if( BoardExhaustedFlag[b]) continue;
for( int ev = lastEv[b][chI]; ev < data[b]->NumEvents[chI] ; ev++){
if( data[b]->Timestamp[chI][ev] > 0 && ((data[b]->Timestamp[chI][ev] - e_t[0] )*ch2ns_value*1e-9)*1e6 <= timeWin ) {
bd[multi_evt] = data[b]->boardSN;
ch[multi_evt] = chI;
e[multi_evt] = data[b]->Energy[chI][ev];
e_t[multi_evt] = data[b]->Timestamp[chI][ev];
e_f[multi_evt] = data[b]->fineTime[chI][ev];
if( traceOn ){
traceLength[multi_evt] = (unsigned short) data[b]->Waveform1[chI][ev].size();
trace = (TGraph *) arrayTrace->ConstructedAt(multi_evt, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[multi_evt]; hh++){
trace->SetPoint(hh, hh, data[b]->Waveform1[chI][ev][hh]);
}
}
lastEv[b][chI] = ev + 1;
multi_evt ++;
if(lastEv[b][chI] == data[b]->NumEvents[chI] ) exhaustedCh[b] ++;
}
}
}
}
for(int b=0;b<nFile;b++){if(singleChannelExhaustedFlag[b] && exhaustedCh[b] >= MaxNChannels){BoardExhaustedFlag[b]=true;exhaustedBd ++;};}
if( verbose) {
printf("=============== multi : %d , ev : %llu\n", multi_evt, evID);
for( int ev = 0; ev < multi_evt; ev++){
printf("%3d, bd, ch : %2d, %2d, %u, %llu \n", ev, bd[ev], ch[ev], e[ev], e_t[ev]);
}
printf("=============== Last Ev , exhaustedBd %d \n", exhaustedBd);
for( int chI = 0; chI < MaxNChannels ; chI++){
for( int b = 0; b < nFile ; b++){
if(lastEv[b][chI] == 0 ) continue;
printf("board %d %2d, %d %d\n", b+1, chI, lastEv[b][chI], data[b]->NumEvents[chI]);
}
}
}
/// fill Tree
outRootFile->cd();
tree->Fill();
evID++;
multi_evt=0;
}while(exhaustedBd < nFile && exhaustedBd<MaxNBoards);
//!singleChannelExhaustedFlag || (exhaustedCh < MaxNChannels) );
///========== clear built data
/// move the last data to the top,
for(int b=0;b<nFile;b++){
for( int chI = 0; chI < MaxNChannels; chI++){
if( data[b]->NumEvents[chI] == 0 ) continue;
int count = 0;
for( int ev = lastEv[b][chI] ; ev < data[b]->NumEvents[chI] ; ev++){
data[b]->Energy[chI][count] = data[b]->Energy[chI][ev];
data[b]->Timestamp[chI][count] = data[b]->Timestamp[chI][ev];
data[b]->fineTime[chI][count] = data[b]->fineTime[chI][ev];
count++;
}
int lala = data[b]->NumEvents[chI] - lastEv[b][chI];
data[b]->NumEvents[chI] = (lala >= 0 ? lala: 0);
}
}
}
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);
}