rebuild the eventbuilder using wonderful method

This commit is contained in:
Ryan Tang 2024-03-12 16:43:12 -04:00
parent 663d99a5f8
commit ed7ee00510
4 changed files with 818 additions and 308 deletions

View File

@ -9,10 +9,18 @@
#include "TTree.h"
#include "TMacro.h"
#include "CustomStruct.h"
//#include "CustomStruct.h"
#define MAX_MULTI 1000
struct FileInfo{
std::string fileName;
int fileID;
unsigned long hitCount;
};
//^#############################################################
//^#############################################################
int main(int argc, char **argv) {
@ -20,30 +28,28 @@ int main(int argc, char **argv) {
printf("=========================================\n");
printf("=== *.fsu Events Builder ===\n");
printf("=========================================\n");
if (argc <= 6) {
if (argc < 6) {
printf("Incorrect number of arguments:\n");
printf("%s [timeWindow] [withTrace] [verbose] [tempFolder] [inFile1] [inFile2] .... \n", argv[0]);
printf("%s [timeWindow] [withTrace] [verbose] [batchSize] [inFile1] [inFile2] .... \n", argv[0]);
printf(" timeWindow : in ns \n");
printf(" withTrace : 0 for no trace, 1 for trace \n");
printf(" verbose : > 0 for debug \n");
printf(" tempFolder : temperary folder for file breakdown \n");
printf(" batchSize : the size of hit in a batch \n");
printf(" Output file name is contructed from inFile1 \n");
printf("\n");
printf("=========================== Working flow\n");
printf(" 1) Break down the fsu files into dual channel, save in tempFolder as *.fsu.X\n");
printf(" 1a) if the *.fsu file has trace, *.fsu.ts will also has trace.\n");
printf(" 2) Load the *.fsu.X files and do the event building\n");
printf(" Example: %s 0 0 0 10000 '\\ls -1 *001*.fsu'\n", argv[0]);
printf("\n\n");
return 1;
}
uInt runStartTime = getTime_us();
///============= read input
unsigned int timeWindow = atoi(argv[1]);
long timeWindow = atoi(argv[1]);
bool traceOn = atoi(argv[2]);
unsigned int debug = atoi(argv[3]);
std::string tempFolder = argv[4];
unsigned int batchSize = atoi(argv[4]);
int nFile = argc - 5;
TString inFileName[nFile];
for( int i = 0 ; i < nFile ; i++){ inFileName[i] = argv[i+5];}
@ -58,156 +64,61 @@ int main(int argc, char **argv) {
outFileName += "_" + std::to_string(timeWindow);
outFileName += ".root";
printf("-------> Out file name : %s \n", outFileName.Data());
printf(" Number of Files : %d \n", nFile);
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 ns = %.1f us\n", timeWindow, timeWindow/1000.);
//printf(" Buffer size = %.0f event/channel\n", MaxNData * bufferSize);
printf("===================================== Breaking down files\n");
///========================================
printf("===================================== Load the files\n");
printf("=========================================\n");
printf(" Time Window = %ld ns = %.1f us\n", timeWindow, timeWindow/1000.);
printf(" Include Trace = %s\n", traceOn ? "Yes" : "No");
printf(" Debug level = %d\n", debug);
printf(" Batch size = %d events/file\n", batchSize);
printf(" Max multiplity = %d hits/event (hard coded)\n", MAX_MULTI);
printf("========================================= Grouping files\n");
//check if all input files is ts file;
bool isTSFiles = false;
int count = 0;
for( int i = 0; i < nFile; i++){
FILE * temp = fopen(inFileName[i].Data(), "r");
uint32_t header;
fread(&header, 4, 1, temp);
if( (header >> 24) == 0xAA ) count++;
}
if( count == nFile ) isTSFiles = true;
std::vector<std::vector<FileInfo>> fileGroupList; // fileName and ID = SN * 1000 + index
std::vector<FileInfo> fileList;
std::vector<FileInfo> fileInfo;
unsigned long long int totalHitCount = 0;
if( !isTSFiles ){
printf("######### All files are not time-sorted files\n");
FSUReader * readerA = new FSUReader(inFileName[0].Data(), 1, 1);
readerA->ScanNumBlock(0,0);
FileInfo fileInfo = {inFileName[0].Data(), readerA->GetSN() * 1000 + readerA->GetFileOrder(), readerA->GetHitCount()};
fileList.push_back(fileInfo);
totalHitCount += readerA->GetHitCount();
///============= sorting file by the serial number & order
FSUReader ** reader = new FSUReader*[nFile];
// file name format is expName_runID_SN_DPP_tick2ns_order.fsu
for( int i = 0; i < nFile; i++){
printf("Processing %s (%d/%d) ..... \n", inFileName[i].Data(), i+1, nFile);
reader[i] = new FSUReader(inFileName[i].Data(), 600, false);
if( !reader[i]->isOpen() ){
printf("------- cannot open file.\n");
continue;
}
if( reader[i]->GetFileByteSize() == 0 ){
printf("------- file size is ZERO.\n");
continue;
}
reader[i]->ScanNumBlock(false, 2);
for( int i = 1; i < nFile; i++){
FSUReader * readerB = new FSUReader(inFileName[i].Data(), 1, 0);
readerB->ScanNumBlock(0,0);
totalHitCount += readerB->GetHitCount();
fileInfo = {inFileName[i].Data(), readerB->GetSN() * 1000 + readerB->GetFileOrder(), readerB->GetHitCount()};
std::string outFileName = reader[i]->SaveHit2NewFile(tempFolder);
FileInfo tempInfo;
tempInfo.fileName = outFileName;
tempInfo.readerID = i;
tempInfo.SN = reader[i]->GetSN();
tempInfo.hitCount = reader[i]->GetHitCount();
tempInfo.fileSize = reader[i]->GetTSFileSize();
tempInfo.tick2ns = reader[i]->GetTick2ns();
tempInfo.DPPType = reader[i]->GetDPPType();
tempInfo.order = reader[i]->GetFileOrder();
tempInfo.CalOrder();
tempInfo.t0 = reader[i]->GetHit(0).timestamp;
fileInfo.push_back(tempInfo);
delete reader[i];
}
delete [] reader;
}else{
printf("######### All files are time sorted files\n");
FSUTSReader ** reader = new FSUTSReader*[nFile];
// file name format is expName_runID_SN_DPP_tick2ns_order.fsu
for( int i = 0; i < nFile; i++){
printf("Processing %s (%d/%d) ..... \n", inFileName[i].Data(), i+1, nFile);
reader[i] = new FSUTSReader(inFileName[i].Data(), false);
if( !reader[i]->isOpen() ){
printf("------- cannot open file.\n");
continue;
}
//reader[i]->ScanFile(1);
if( reader[i]->GetNumHitFromHeader() == 0 ){
printf("------- file has no data.\n");
continue;
}
FileInfo tempInfo;
tempInfo.fileName = inFileName[i].Data();
tempInfo.readerID = i;
tempInfo.SN = reader[i]->GetSN();
tempInfo.hitCount = reader[i]->GetNumHitFromHeader();
tempInfo.fileSize = reader[i]->GetFileByteSize();
tempInfo.order = reader[i]->GetFileOrder();
tempInfo.CalOrder();
tempInfo.t0 = reader[i]->GetT0();;
fileInfo.push_back(tempInfo);
delete reader[i];
}
delete [] reader;
}
nFile = (int) fileInfo.size();
std::sort(fileInfo.begin(), fileInfo.end(), [](const FileInfo& a, const FileInfo& b) {
return a.ID < b.ID;
});
unsigned int totHitCount = 0;
printf("===================================== number of file %d.\n", nFile);
for( int i = 0 ; i < nFile; i++){
printf("%d |", i);
fileInfo[i].Print();
totHitCount += fileInfo[i].hitCount;
//printf(" %30s | ID %10ld \n", fileInfo[i].fileName.Data(), fileInfo[i].ID);
}
printf("----- total number of hit : %u.\n", totHitCount);
//*======================================= Sort files into groups
std::vector<GroupInfo> group; // group by SN and chMask
for( int i = 0; i < nFile; i++){
if( i == 0 || group.back().sn != fileInfo[i].SN ){
group.push_back(GroupInfo());
group.back().fileIDList.push_back(i); // an empty struct
group.back().currentID = 0;
group.back().hitCount = fileInfo[i].hitCount;
group.back().sn = fileInfo[i].SN;
group.back().finished = false;
if( readerA->GetSN() == readerB->GetSN() ){
fileList.push_back(fileInfo);
}else{
group.back().fileIDList.push_back(i);
fileGroupList.push_back(fileList);
fileList.clear();
fileList.push_back(fileInfo);
}
delete readerA;
readerA = readerB;
}
fileGroupList.push_back(fileList);
delete readerA;
int nGroup = group.size();
printf("===================================== number of file Group by digitizer %d.\n", nGroup);
for( int i = 0; i < nGroup; i++){
printf(" Digi-%d, DPPType: %d \n", group[i].sn, fileInfo[group[i].currentID].DPPType);
for( int j = 0; j< (int) group[i].fileIDList.size(); j++){
uShort fID = group[i].fileIDList[j];
printf(" %s \n", fileInfo[fID].fileName.c_str());
printf("======================= total Hit Count : %llu\n", totalHitCount);
for( size_t i = 0; i < fileGroupList.size(); i++){
printf("group ----- %ld \n", i);
//sort by ID
std::sort(fileGroupList[i].begin(), fileGroupList[i].end(), [](const FileInfo & a, const FileInfo & b) {
return a.fileID < b.fileID;
});
for( size_t j = 0; j < fileGroupList[i].size(); j++){
printf("%3ld | %8d | %9lu| %s \n", j, fileGroupList[i][j].fileID, fileGroupList[i][j].hitCount, fileGroupList[i][j].fileName.c_str() );
}
}
// //*====================================== create tree
@ -231,7 +142,7 @@ int main(int argc, char **argv) {
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");
tree->Branch("e_f", e_f, "e_fineTime[multi]/s");
tree->Branch("traceLength", traceLength, "traceLength[multi]/s");
TClonesArray * arrayTrace = nullptr;
@ -243,192 +154,197 @@ int main(int argc, char **argv) {
arrayTrace->BypassStreamer();
}
//*======================================= Open time-sorted files
printf("===================================== Open time-sorted files.\n");
//*======================================= Open files
printf("========================================= Open files & Build Events.\n");
FSUTSReader ** tsReader = new FSUTSReader * [nGroup];
for( int i = 0; i < nGroup; i++){
std::string fileName = fileInfo[group[i].fileIDList[0]].fileName;
tsReader[i] = new FSUTSReader(fileName);
// tsReader[i]->ScanFile(1);
group[i].usedHitCount = 0;
const short nGroup = fileGroupList.size();
std::vector<Hit> hitList[nGroup];
FSUReader ** reader = new FSUReader * [nGroup];
ulong ID[nGroup];
for( short i = 0; i < nGroup; i++){
std::vector<std::string> fList;
for( size_t j = 0; j < fileGroupList[i].size(); j++){
fList.push_back( fileGroupList[i][j].fileName );
}
reader[i] = new FSUReader(fList, 1, debug);
hitList[i] = reader[i]->ReadBatch(batchSize, debug );
reader[i]->PrintHitListInfo(hitList[i], "hitList-" + std::to_string(reader[i]->GetSN()));
ID[i] = 0;
if( debug ) {
for( size_t p = 0; p < 10; p ++ ){
if( hitList[i].size() <= p ) break;
hitList[i][p].Print();
}
}
}
//*====================================== build events
printf("================= Building events....\n");
uInt hitProcessed = 0;
unsigned long long tStart = 0;
unsigned long long tEnd = 0;
//find the earliest time
ullong t0 = -1;
uShort gp0 = -1;
ullong tStart = 0;
ullong tEnd = 0;
bool hasEvent = false;
for( int i = 0; i < nGroup; i++){
if( fileInfo[group[i].fileIDList[0]].t0 < t0 ) {
t0 = fileInfo[group[i].fileIDList[0]].t0;
gp0 = i;
//find earliest time group;
unsigned long long t0 = -1;
short g0 = 0 ;
for( short i = 0; i < nGroup; i++){
if( hitList[i].size() == 0 ) continue;
if( hitList[i][0].timestamp < t0 ) {
t0 = hitList[i][0].timestamp;
g0 = i;
}
}
}
tStart = t0;
if( debug ) printf("First timestamp is %llu, group : %u\n", t0, g0);
int nFileFinished = 0;
multi = 0;
evID = 0;
std::vector<Hit> events;
unsigned long long hitProcessed = 0;
if( debug ) printf("First timestamp is %llu, group : %u\n", t0, gp0);
do{
//*============= Build events from hitList[i]
if( debug ) printf("################################ ev build %llu \n", evID);
events.clear();
///===================== check if the file is finished.
for( int i = 0; i < nGroup; i++){
uShort gpID = (i + gp0) % nGroup;
if( group[gpID].finished ) continue;
short endCount = 0;
do{
for( short i = 0; i < nGroup; i++){
short ig = (i + g0 ) % nGroup;
if( group[gpID].usedHitCount > tsReader[gpID]->GetHitID() || tsReader[gpID]->GetFilePos() <= 4){
if( tsReader[gpID]->ReadNextHit(traceOn, 0) == 0 ){
hitProcessed ++;
if( debug ){ printf("............ Get Data | "); tsReader[gpID]->GetHit()->Print();}
}
}
if( hitList[ig].size() == 0 ) continue;
if( tsReader[gpID]->GetHit()->timestamp - t0 <= timeWindow ) {
//chekc if reached the end of hitList
if( ID[ig] >= hitList[ig].size() ) {
hitList[ig] = reader[ig]->ReadBatch(batchSize, debug + 1);
ID[ig] = 0;
if( hitList[ig].size() == 0 ) continue;
}
if( evID == 0) tStart = tsReader[gpID]->GetHit()->timestamp;
if( hitProcessed >= totHitCount ) tEnd = tsReader[gpID]->GetHit()->timestamp;
if( timeWindow >= 0 ){
do{
sn[multi] = tsReader[gpID]->GetHit()->sn;
ch[multi] = tsReader[gpID]->GetHit()->ch;
e[multi] = tsReader[gpID]->GetHit()->energy;
e2[multi] = tsReader[gpID]->GetHit()->energy2;
e_t[multi] = tsReader[gpID]->GetHit()->timestamp;
e_f[multi] = tsReader[gpID]->GetHit()->fineTime;
traceLength[multi] = tsReader[gpID]->GetHit()->traceLength;
if( traceOn ){
trace = (TGraph *) arrayTrace->ConstructedAt(multi, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[multi]; hh++){
trace->SetPoint(hh, hh, tsReader[gpID]->GetHit()->trace[hh]);
}
if( (long int)(hitList[ig].at(ID[ig]).timestamp - t0) <= timeWindow ){
events.push_back(hitList[ig].at(ID[ig]));
ID[ig] ++;
}else{
break;
}
if( debug ) printf("(%5d, %2d) %6d %16llu, %u\n", sn[multi], ch[multi], e[multi], e_t[multi], traceLength[multi]);
hasEvent = true;
multi ++;
group[gpID].usedHitCount ++;
if( tsReader[gpID]->ReadNextHit(traceOn, 0) == 0 ){
hitProcessed ++;
if( debug ){ printf("..Get Data after fill | "); tsReader[gpID]->GetHit()->Print();}
//check if reached the end of hitList
if( ID[ig] >= hitList[ig].size() ) {
hitList[ig] = reader[ig]->ReadBatch(batchSize, debug);
ID[ig] = 0;
if( hitList[ig].size() == 0 ) break;
}
}while(ID[ig] < hitList[ig].size());
if( multi > MAX_MULTI) {
printf(" !!!!!! multi > MAX_MULTI = %d\n", MAX_MULTI);
}
}else{
events.push_back(hitList[ig].at(ID[ig]));
ID[ig] ++;
}
}else{
break;
}
if( timeWindow == 0) break;
if( tsReader[gpID]->GetHitID() + 1 >= tsReader[gpID]->GetNumHitFromHeader() ) endCount ++;
if( endCount == 2 ) break;
}while(true);
if( timeWindow < 0) break;
}
if( hasEvent ){
outRootFile->cd();
tree->Fill();
multi = 0;
evID ++;
hasEvent = false;
if( events.size() > 1 ){
std::sort(events.begin(), events.end(), [](const Hit& a, const Hit& b) {
return a.timestamp < b.timestamp;
});
}
if( hitProcessed % 10000 == 0 ) printf("hit Porcessed %u/%u hit....%.2f%%\n\033[A\r", hitProcessed, totHitCount, hitProcessed*100./totHitCount);
tEnd = events.back().timestamp;
hitProcessed += events.size();
if( hitProcessed % 10000 == 0 ) printf("hit Porcessed %llu/%llu hit....%.2f%%\n\033[A\r", hitProcessed, totalHitCount, hitProcessed*100./totalHitCount);
multi = events.size() ;
if( events.size() >= MAX_MULTI ) {
printf("event %lld has size = %d > MAX_MULTI = %d\n", evID, multi, MAX_MULTI);
multi = MAX_MULTI;
}
if( debug ) printf("=================================== filling data | %u \n", multi);
for( size_t p = 0; p < multi ; p ++ ) {
if( debug ) {printf("%4zu | ", p); events[p].Print();}
sn[p] = events[p].sn;
ch[p] = events[p].ch;
e[p] = events[p].energy;
e2[p] = events[p].energy2;
e_t[p] = events[p].timestamp;
e_f[p] = events[p].fineTime;
traceLength[p] = events[p].traceLength;
if( traceOn ){
trace = (TGraph *) arrayTrace->ConstructedAt(multi, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[multi]; hh++){
trace->SetPoint(hh, hh, events[p].trace[hh]);
}
}
}
outRootFile->cd();
tree->Fill();
// tree->Write();
multi = 0;
evID ++;
///===================== find the next first timestamp
t0 = -1;
gp0 = -1;
g0 = -1;
for( int i = 0; i < nGroup; i++) {
if( group[i].finished ) continue;
if( tsReader[i]->GetHit()->timestamp < t0) {
t0 = tsReader[i]->GetHit()->timestamp;
gp0 = i;
if( hitList[i].size() == 0 ) continue;
if( hitList[i][ID[i]].timestamp < t0 ) {
t0 = hitList[i][ID[i]].timestamp;
g0 = i;
}
}
if( debug ) printf("Next First timestamp is %llu, group : %u\n", t0, gp0);
if( debug ) printf("Next First timestamp is %llu, group : %u\n", t0, g0);
///===================== check if the file is finished.
int gpCount = 0;
for( int gpID = 0; gpID < nGroup; gpID ++) {
if( group[gpID].finished ) {
gpCount ++;
//*=============
nFileFinished = 0;
for( int i = 0 ; i < nGroup; i++) {
if( hitList[i].size() == 0 ) {
nFileFinished ++;
continue;
}
if( group[gpID].usedHitCount >= tsReader[gpID]->GetNumHitFromHeader() ) {
group[gpID].currentID ++;
if( group[gpID].currentID >= group[gpID].fileIDList.size() ) {
group[gpID].finished = true;
printf("-----> no more file for this group, S/N : %d.\n", group[gpID].sn);
}else{
uShort fID = group[gpID].fileIDList[group[gpID].currentID];
std::string fileName = fileInfo[fID].fileName;
delete tsReader[gpID];
tsReader[gpID] = new FSUTSReader(fileName);
tsReader[gpID]->ScanFile(1);
printf("-----> go to the next file, %s \n", fileName.c_str() );
group[gpID].usedHitCount = 0;
}else{
if( ID[i] >= hitList[i].size( )) {
hitList[i] = reader[i]->ReadBatch(batchSize, debug);
ID[i] = 0;
if( hitList[i].size() == 0 ) nFileFinished ++;
}
}
if( group[gpID].finished ) gpCount ++;
}
if( gpCount == (int) group.size() ) break;
if( debug > 1 ) printf("========== nFileFinished : %d\n", nFileFinished);
}while(true);
}while( nFileFinished < nGroup);
tree->Write();
uInt runEndTime = getTime_us();
double runTime = (runEndTime - runStartTime) * 1e-6;
printf("========================= finished.\n");
printf("========================================= finished.\n");
printf(" event building time = %.2f sec = %.2f min\n", runTime, runTime/60.);
printf(" total events built = %llu by event builder (%llu in tree)\n", evID, tree->GetEntriesFast());
double tDuration_sec = (tEnd - tStart) * 1e-9;
printf(" first timestamp = %20llu ns\n", tStart);
printf(" last timestamp = %20llu ns\n", tEnd);
printf(" total data duration = %.2f sec = %.2f min\n", tDuration_sec, tDuration_sec/60.);
printf("=======> saved to %s \n", outFileName.Data());
TMacro info;
info.AddLine(Form("tStart= %20llu ns",tStart));
info.AddLine(Form(" tEnd= %20llu ns",tEnd));
info.Write("info");
printf("==============> saved to %s \n", outFileName.Data());
outRootFile->Close();
for( int i = 0; i < nGroup; i++){
delete tsReader[i];
}
delete [] tsReader;
for( int i = 0; i < nGroup; i++) delete reader[i];
delete [] reader;
return 0;
}

434
Aux/EventBuilder_clumsy.cpp Normal file
View File

@ -0,0 +1,434 @@
#include "fsuReader.h"
#include "fsutsReader.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TClonesArray.h"
#include "TGraph.h"
#include "TFile.h"
#include "TTree.h"
#include "TMacro.h"
#include "CustomStruct.h"
#define MAX_MULTI 1000
//^#############################################################
//^#############################################################
int main(int argc, char **argv) {
printf("=========================================\n");
printf("=== *.fsu Events Builder ===\n");
printf("=========================================\n");
if (argc <= 6) {
printf("Incorrect number of arguments:\n");
printf("%s [timeWindow] [withTrace] [verbose] [tempFolder] [inFile1] [inFile2] .... \n", argv[0]);
printf(" timeWindow : in ns \n");
printf(" withTrace : 0 for no trace, 1 for trace \n");
printf(" verbose : > 0 for debug \n");
printf(" tempFolder : temperary folder for file breakdown \n");
printf(" Output file name is contructed from inFile1 \n");
printf("\n");
printf("=========================== Working flow\n");
printf(" 1) Break down the fsu files into dual channel, save in tempFolder as *.fsu.X\n");
printf(" 1a) if the *.fsu file has trace, *.fsu.ts will also has trace.\n");
printf(" 2) Load the *.fsu.X files and do the event building\n");
printf("\n\n");
return 1;
}
uInt runStartTime = getTime_us();
///============= read input
unsigned int timeWindow = atoi(argv[1]);
bool traceOn = atoi(argv[2]);
unsigned int debug = atoi(argv[3]);
std::string tempFolder = argv[4];
int nFile = argc - 5;
TString inFileName[nFile];
for( int i = 0 ; i < nFile ; i++){ inFileName[i] = argv[i+5];}
/// Form outFileName;
TString outFileName = inFileName[0];
int pos = outFileName.Last('/');
pos = outFileName.Index("_", pos+1); // find next "_"
pos = outFileName.Index("_", pos+1); // find next "_"
if( nFile == 1 ) pos = outFileName.Index("_", pos+1); // find next "_", S/N
outFileName.Remove(pos); // remove the rest
outFileName += "_" + std::to_string(timeWindow);
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 ns = %.1f us\n", timeWindow, timeWindow/1000.);
//printf(" Buffer size = %.0f event/channel\n", MaxNData * bufferSize);
printf("===================================== Breaking down files\n");
///========================================
printf("===================================== Load the files\n");
//check if all input files is ts file;
bool isTSFiles = false;
int count = 0;
for( int i = 0; i < nFile; i++){
FILE * temp = fopen(inFileName[i].Data(), "r");
uint32_t header;
fread(&header, 4, 1, temp);
if( (header >> 24) == 0xAA ) count++;
}
if( count == nFile ) isTSFiles = true;
std::vector<FileInfo> fileInfo;
if( !isTSFiles ){
printf("######### All files are not time-sorted files\n");
///============= sorting file by the serial number & order
FSUReader ** reader = new FSUReader*[nFile];
// file name format is expName_runID_SN_DPP_tick2ns_order.fsu
for( int i = 0; i < nFile; i++){
printf("Processing %s (%d/%d) ..... \n", inFileName[i].Data(), i+1, nFile);
reader[i] = new FSUReader(inFileName[i].Data(), 600, false);
if( !reader[i]->IsOpen() ){
printf("------- cannot open file.\n");
continue;
}
if( reader[i]->GetFileByteSize() == 0 ){
printf("------- file size is ZERO.\n");
continue;
}
reader[i]->ScanNumBlock(false, 2);
std::string outFileName = reader[i]->SaveHit2NewFile(tempFolder);
FileInfo tempInfo;
tempInfo.fileName = outFileName;
tempInfo.readerID = i;
tempInfo.SN = reader[i]->GetSN();
tempInfo.hitCount = reader[i]->GetHitCount();
tempInfo.fileSize = reader[i]->GetTSFileSize();
tempInfo.tick2ns = reader[i]->GetTick2ns();
tempInfo.DPPType = reader[i]->GetDPPType();
tempInfo.order = reader[i]->GetFileOrder();
tempInfo.CalOrder();
tempInfo.t0 = reader[i]->GetHit(0).timestamp;
fileInfo.push_back(tempInfo);
delete reader[i];
}
delete [] reader;
}else{
printf("######### All files are time sorted files\n");
FSUTSReader ** reader = new FSUTSReader*[nFile];
// file name format is expName_runID_SN_DPP_tick2ns_order.fsu
for( int i = 0; i < nFile; i++){
printf("Processing %s (%d/%d) ..... \n", inFileName[i].Data(), i+1, nFile);
reader[i] = new FSUTSReader(inFileName[i].Data(), false);
if( !reader[i]->isOpen() ){
printf("------- cannot open file.\n");
continue;
}
//reader[i]->ScanFile(1);
if( reader[i]->GetNumHitFromHeader() == 0 ){
printf("------- file has no data.\n");
continue;
}
FileInfo tempInfo;
tempInfo.fileName = inFileName[i].Data();
tempInfo.readerID = i;
tempInfo.SN = reader[i]->GetSN();
tempInfo.hitCount = reader[i]->GetNumHitFromHeader();
tempInfo.fileSize = reader[i]->GetFileByteSize();
tempInfo.order = reader[i]->GetFileOrder();
tempInfo.CalOrder();
tempInfo.t0 = reader[i]->GetT0();;
fileInfo.push_back(tempInfo);
delete reader[i];
}
delete [] reader;
}
nFile = (int) fileInfo.size();
std::sort(fileInfo.begin(), fileInfo.end(), [](const FileInfo& a, const FileInfo& b) {
return a.ID < b.ID;
});
unsigned int totHitCount = 0;
printf("===================================== number of file %d.\n", nFile);
for( int i = 0 ; i < nFile; i++){
printf("%d |", i);
fileInfo[i].Print();
totHitCount += fileInfo[i].hitCount;
//printf(" %30s | ID %10ld \n", fileInfo[i].fileName.Data(), fileInfo[i].ID);
}
printf("----- total number of hit : %u.\n", totHitCount);
//*======================================= Sort files into groups
std::vector<GroupInfo> group; // group by SN and chMask
for( int i = 0; i < nFile; i++){
if( i == 0 || group.back().sn != fileInfo[i].SN ){
group.push_back(GroupInfo());
group.back().fileIDList.push_back(i); // an empty struct
group.back().currentID = 0;
group.back().hitCount = fileInfo[i].hitCount;
group.back().sn = fileInfo[i].SN;
group.back().finished = false;
}else{
group.back().fileIDList.push_back(i);
}
}
int nGroup = group.size();
printf("===================================== number of file Group by digitizer %d.\n", nGroup);
for( int i = 0; i < nGroup; i++){
printf(" Digi-%d, DPPType: %d \n", group[i].sn, fileInfo[group[i].currentID].DPPType);
for( int j = 0; j< (int) group[i].fileIDList.size(); j++){
uShort fID = group[i].fileIDList[j];
printf(" %s \n", fileInfo[fID].fileName.c_str());
}
}
// //*====================================== create tree
TFile * outRootFile = new TFile(outFileName, "recreate");
TTree * tree = new TTree("tree", outFileName);
unsigned long long evID = 0;
unsigned int multi = 0;
unsigned short sn[MAX_MULTI] = {0}; /// board SN
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
unsigned short traceLength[MAX_MULTI];
tree->Branch("evID", &evID, "event_ID/l");
tree->Branch("multi", &multi, "multi/i");
tree->Branch("sn", sn, "sn[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");
tree->Branch("traceLength", traceLength, "traceLength[multi]/s");
TClonesArray * arrayTrace = nullptr;
TGraph * trace = nullptr;
if( traceOn ) {
arrayTrace = new TClonesArray("TGraph");
tree->Branch("trace", arrayTrace, 2560000);
arrayTrace->BypassStreamer();
}
//*======================================= Open time-sorted files
printf("===================================== Open time-sorted files.\n");
FSUTSReader ** tsReader = new FSUTSReader * [nGroup];
for( int i = 0; i < nGroup; i++){
std::string fileName = fileInfo[group[i].fileIDList[0]].fileName;
tsReader[i] = new FSUTSReader(fileName);
// tsReader[i]->ScanFile(1);
group[i].usedHitCount = 0;
}
//*====================================== build events
printf("================= Building events....\n");
uInt hitProcessed = 0;
//find the earliest time
ullong t0 = -1;
uShort gp0 = -1;
ullong tStart = 0;
ullong tEnd = 0;
bool hasEvent = false;
for( int i = 0; i < nGroup; i++){
if( fileInfo[group[i].fileIDList[0]].t0 < t0 ) {
t0 = fileInfo[group[i].fileIDList[0]].t0;
gp0 = i;
}
}
if( debug ) printf("First timestamp is %llu, group : %u\n", t0, gp0);
do{
if( debug ) printf("################################ ev build %llu \n", evID);
///===================== check if the file is finished.
for( int i = 0; i < nGroup; i++){
uShort gpID = (i + gp0) % nGroup;
if( group[gpID].finished ) continue;
short endCount = 0;
do{
if( group[gpID].usedHitCount > tsReader[gpID]->GetHitID() || tsReader[gpID]->GetFilePos() <= 4){
if( tsReader[gpID]->ReadNextHit(traceOn, 0) == 0 ){
hitProcessed ++;
if( debug ){ printf("............ Get Data | "); tsReader[gpID]->GetHit()->Print();}
}
}
if( tsReader[gpID]->GetHit()->timestamp - t0 <= timeWindow ) {
if( evID == 0) tStart = tsReader[gpID]->GetHit()->timestamp;
if( hitProcessed >= totHitCount ) tEnd = tsReader[gpID]->GetHit()->timestamp;
sn[multi] = tsReader[gpID]->GetHit()->sn;
ch[multi] = tsReader[gpID]->GetHit()->ch;
e[multi] = tsReader[gpID]->GetHit()->energy;
e2[multi] = tsReader[gpID]->GetHit()->energy2;
e_t[multi] = tsReader[gpID]->GetHit()->timestamp;
e_f[multi] = tsReader[gpID]->GetHit()->fineTime;
traceLength[multi] = tsReader[gpID]->GetHit()->traceLength;
if( traceOn ){
trace = (TGraph *) arrayTrace->ConstructedAt(multi, "C");
trace->Clear();
for( int hh = 0; hh < traceLength[multi]; hh++){
trace->SetPoint(hh, hh, tsReader[gpID]->GetHit()->trace[hh]);
}
}
if( debug ) printf("(%5d, %2d) %6d %16llu, %u\n", sn[multi], ch[multi], e[multi], e_t[multi], traceLength[multi]);
hasEvent = true;
multi ++;
group[gpID].usedHitCount ++;
if( tsReader[gpID]->ReadNextHit(traceOn, 0) == 0 ){
hitProcessed ++;
if( debug ){ printf("..Get Data after fill | "); tsReader[gpID]->GetHit()->Print();}
}
if( multi > MAX_MULTI) {
printf(" !!!!!! multi > MAX_MULTI = %d\n", MAX_MULTI);
}
}else{
break;
}
if( timeWindow == 0) break;
if( tsReader[gpID]->GetHitID() + 1 >= tsReader[gpID]->GetNumHitFromHeader() ) endCount ++;
if( endCount == 2 ) break;
}while(true);
}
if( hasEvent ){
outRootFile->cd();
tree->Fill();
multi = 0;
evID ++;
hasEvent = false;
}
if( hitProcessed % 10000 == 0 ) printf("hit Porcessed %u/%u hit....%.2f%%\n\033[A\r", hitProcessed, totHitCount, hitProcessed*100./totHitCount);
///===================== find the next first timestamp
t0 = -1;
gp0 = -1;
for( int i = 0; i < nGroup; i++) {
if( group[i].finished ) continue;
if( tsReader[i]->GetHit()->timestamp < t0) {
t0 = tsReader[i]->GetHit()->timestamp;
gp0 = i;
}
}
if( debug ) printf("Next First timestamp is %llu, group : %u\n", t0, gp0);
///===================== check if the file is finished.
int gpCount = 0;
for( int gpID = 0; gpID < nGroup; gpID ++) {
if( group[gpID].finished ) {
gpCount ++;
continue;
}
if( group[gpID].usedHitCount >= tsReader[gpID]->GetNumHitFromHeader() ) {
group[gpID].currentID ++;
if( group[gpID].currentID >= group[gpID].fileIDList.size() ) {
group[gpID].finished = true;
printf("-----> no more file for this group, S/N : %d.\n", group[gpID].sn);
}else{
uShort fID = group[gpID].fileIDList[group[gpID].currentID];
std::string fileName = fileInfo[fID].fileName;
delete tsReader[gpID];
tsReader[gpID] = new FSUTSReader(fileName);
tsReader[gpID]->ScanFile(1);
printf("-----> go to the next file, %s \n", fileName.c_str() );
group[gpID].usedHitCount = 0;
}
}
if( group[gpID].finished ) gpCount ++;
}
if( gpCount == (int) group.size() ) break;
}while(true);
tree->Write();
uInt runEndTime = getTime_us();
double runTime = (runEndTime - runStartTime) * 1e-6;
printf("========================= finished.\n");
printf(" event building time = %.2f sec = %.2f min\n", runTime, runTime/60.);
printf(" total events built = %llu by event builder (%llu in tree)\n", evID, tree->GetEntriesFast());
double tDuration_sec = (tEnd - tStart) * 1e-9;
printf(" first timestamp = %20llu ns\n", tStart);
printf(" last timestamp = %20llu ns\n", tEnd);
printf(" total data duration = %.2f sec = %.2f min\n", tDuration_sec, tDuration_sec/60.);
printf("=======> saved to %s \n", outFileName.Data());
TMacro info;
info.AddLine(Form("tStart= %20llu ns",tStart));
info.AddLine(Form(" tEnd= %20llu ns",tEnd));
info.Write("info");
outRootFile->Close();
for( int i = 0; i < nGroup; i++){
delete tsReader[i];
}
delete [] tsReader;
return 0;
}

View File

@ -14,7 +14,23 @@ class FSUReader{
~FSUReader();
void OpenFile(std::string fileName, uInt dataSize, int verbose = 1);
bool isOpen() const{return inFile == nullptr ? false : true;}
bool IsOpen() const{return inFile == nullptr ? false : true;}
bool IsEndOfFile() const {
// printf("%s : %d | %ld |%ld\n", __func__, feof(inFile), ftell(inFile), inFileSize);
if(fileList.empty() ) {
if( (uLong )ftell(inFile) >= inFileSize){
return true;
}else{
return false;
}
}else{
if( fileID + 1 == (int) fileList.size() && ((uLong)ftell(inFile) >= inFileSize) ) {
return true;
}else{
return false;
}
}
}
void ScanNumBlock(int verbose = 1, uShort saveData = 0); // saveData = 0 (no save), 1 (no trace), 2 (with trace);
int ReadNextBlock(bool traceON = false, int verbose = 0, uShort saveData = 0); // saveData = 0 (no save), 1 (no trace), 2 (with trace);
@ -43,27 +59,46 @@ class FSUReader{
if( id < 0 ) id = hit.size() + id;
return hit[id];
}
void ClearHitCount() {hitCount = 0;}
ulong GetHitCount() const{return hitCount;}
void SortAndSaveTS(int batchSize = 1000000, bool verbose = false);
std::vector<Hit> ReadBatch(unsigned int batchSize = 1000000, bool verbose = false); // output the sorted Hit
void SortAndSaveTS(unsigned int batchSize = 1000000, bool verbose = false);
std::string SaveHit(std::vector<Hit> hitList, bool isAppend = false);
// std::string SaveHit2NewFile(std::string saveFolder = "./", std::string indexStr = "");
off_t GetTSFileSize() const {return tsFileSize;}
//TODO
//void SplitFile(unsigned long hitSizePreFile);
void PrintHit(ulong numHit = -1, ulong startIndex = 0) {
for( ulong i = startIndex; i < std::min(numHit, hitCount); i++){
printf("%10zu ", i); hit[i].Print();
}
}
static void PrintHitList(std::vector<Hit> hitList, std::string name){
printf("============== %s, size : %zu \n", name.c_str(), hitList.size());
printf("t0 : %15llu \n", hitList.at(0).timestamp);
printf("t1 : %15llu \n", hitList.back().timestamp);
static void PrintHitListInfo(std::vector<Hit> hitList, std::string name){
size_t n = hitList.size();
size_t s = sizeof(Hit);
printf("============== %s, size : %zu | %.2f MByte\n", name.c_str(), n, n*s/1024./1024.);
if( n > 0 ){
printf("t0 : %15llu \n", hitList.at(0).timestamp);
printf("t1 : %15llu \n", hitList.back().timestamp);
}
}
void PrintHitListInfo(){
size_t n = hit.size();
size_t s = sizeof(Hit);
printf("============== reader.hit, size : %zu | %.2f MByte\n", n, n*s/1024./1024.);
if( n > 0 ){
printf("t0 : %15llu \n", hit.at(0).timestamp);
printf("t1 : %15llu \n", hit.back().timestamp);
}
}
@ -139,7 +174,7 @@ inline FSUReader::FSUReader(std::string fileName, uInt dataSize, int verbose){
OpenFile(fileName, dataSize, verbose);
}
inline FSUReader::FSUReader(std::vector<std::string> fileList, uInt dataSize = 100, int verbose = 1){
inline FSUReader::FSUReader(std::vector<std::string> fileList, uInt dataSize, int verbose){
inFile = nullptr;
data = nullptr;
@ -196,13 +231,13 @@ inline void FSUReader::OpenFile(std::string fileName, uInt dataSize, int verbose
std::string ext = fileName.substr(found + 1);
if( ext.find("fsu") != std::string::npos ) {
if(verbose) printf("It is an raw data *.fsu format\n");
if(verbose > 1) printf("It is an raw data *.fsu format\n");
isDualBlock = false;
chMask = -1;
}else{
chMask = atoi(ext.c_str());
isDualBlock = true;
if(verbose) printf("It is a splitted dual block data *.fsu.X format, dual channel mask : %d \n", chMask);
if(verbose > 1) printf("It is a splitted dual block data *.fsu.X format, dual channel mask : %d \n", chMask);
}
std::string fileNameNoExt;
@ -247,7 +282,7 @@ inline void FSUReader::OpenFile(std::string fileName, uInt dataSize, int verbose
inline int FSUReader::ReadNextBlock(bool traceON, int verbose, uShort saveData){
if( inFile == NULL ) return -1;
if( feof(inFile) || filePos >= inFileSize) {
if( fileID >= 0 && fileID + 1 < fileList.size() ){
if( fileID >= 0 && fileID + 1 < (short) fileList.size() ){
printf("-------------- next file\n");
fileID ++;
OpenFile(fileList[fileID], data->GetDataSize(), 1 );
@ -270,7 +305,8 @@ inline int FSUReader::ReadNextBlock(bool traceON, int verbose, uShort saveData){
if( header == 0xA ) { ///normal header
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
if( aggSize > inFileSize - ftell(inFile)) aggSize = inFileSize - ftell(inFile);
buffer = new char[aggSize];
dummy = fread(buffer, aggSize, 1, inFile);
filePos = ftell(inFile);
@ -372,7 +408,7 @@ inline void FSUReader::ScanNumBlock(int verbose, uShort saveData){
blockPos.push_back(0);
data->ClearData();
fseek(inFile, 0L, SEEK_SET);
rewind(inFile);
filePos = 0;
bool isTraceOn = saveData < 2 ? false : true;
@ -417,7 +453,131 @@ inline void FSUReader::ScanNumBlock(int verbose, uShort saveData){
}
}
inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
inline std::vector<Hit> FSUReader::ReadBatch(unsigned int batchSize, bool verbose){
// printf("%s sn:%d. filePos : %lu\n", __func__, sn, ftell(inFile));
std::vector<Hit> hitList_A;
if( IsEndOfFile() ) {
hitList_A = hit;
hit.clear();
return hitList_A;
}
if( hit.size() == 0 ){
int res = 0;
do{
res = ReadNextBlock(true, 0, 3);
}while ( hit.size() < batchSize && res == 0);
SortHit();
uLong t0_B = hit.at(0).timestamp;
uLong t1_B = hit.back().timestamp;
if( verbose ) {
printf(" hit in memeory : %7zu | %u | %lu \n", hit.size(), filePos, inFileSize);
printf("t0 : %15lu\n", t0_B);
printf("t1 : %15lu\n", t1_B);
}
hitList_A = hit;
hit.clear();
}else{
hitList_A = hit;
hit.clear();
}
if( IsEndOfFile() ) return hitList_A; // when file finished for 1st batch read
int res = 0;
do{
res = ReadNextBlock(true, 0, 3);
}while ( hit.size() < batchSize && res == 0);
SortHit();
uLong t0_B = hit.at(0).timestamp;
uLong t1_B = hit.back().timestamp;
if( verbose ) {
printf(" hit in memeory : %7zu | %u | %lu \n", hit.size(), filePos, inFileSize);
printf("t0 : %15lu\n", t0_B);
printf("t1 : %15lu\n", t1_B);
}
uLong t0_A = hitList_A.at(0).timestamp;
uLong t1_A = hitList_A.back().timestamp;
ulong ID_A = 0;
ulong ID_B = 0;
if( t0_A >= t0_B) {
printf("\033[0;31m!!!!!!!!!!!!!!!!! %s | Need to increase the batch size. \033[0m\n", __func__);
return std::vector<Hit> ();
}
if( t1_A > t0_B) { // need to sort between two hitList
if( verbose ) {
printf("############# need to sort \n");
printf("=========== sume of A + B : %zu \n", hitList_A.size() + hit.size());
}
std::vector<Hit> hitTemp;
// find the hit that is >= t0_B, save them to hitTemp
for( size_t j = 0; j < hitList_A.size() ; j++){
if( hitList_A[j].timestamp < t0_B ) continue;;
if( ID_A == 0 ) ID_A = j;
hitTemp.push_back(hitList_A[j]);
}
// remove hitList_A element that is >= t0_B
hitList_A.erase(hitList_A.begin() + ID_A, hitList_A.end() );
// find the hit that is <= t1_A, save them to hitTemp
for( size_t j = 0; j < hit.size(); j++){
if( hit[j].timestamp > t1_A ) {
break;
}
hitTemp.push_back(hit[j]);
ID_B = j + 1;
}
// remove hit elements that is <= t1_A
hit.erase(hit.begin(), hit.begin() + ID_B );
// sort hitTemp
std::sort(hitTemp.begin(), hitTemp.end(), [](const Hit& a, const Hit& b) {
return a.timestamp < b.timestamp;
});
if( verbose ) {
printf("----------------- ID_A : %lu, Drop\n", ID_A);
printf("----------------- ID_B : %lu, Drop\n", ID_B);
PrintHitListInfo(hitList_A, "hitList_A");
PrintHitListInfo(hitTemp, "hitTemp");
PrintHitListInfo();
printf("=========== sume of A + B + Temp : %zu \n", hitList_A.size() + hit.size() + hitTemp.size());
printf("----------------- refill hitList_A \n");
}
for( size_t j = 0; j < hitTemp.size(); j++){
hitList_A.push_back(hitTemp[j]);
}
hitTemp.clear();
if( verbose ) {
PrintHitListInfo(hitList_A, "hitList_A");
PrintHitListInfo();
printf("=========== sume of A + B : %zu \n", hitList_A.size() + hit.size());
}
}
return hitList_A;
}
inline void FSUReader::SortAndSaveTS(unsigned int batchSize, bool verbose){
int count = 0;
std::vector<Hit> hitList_A ;
@ -473,7 +633,7 @@ inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
hitList_A.erase(hitList_A.begin() + ID_A, hitList_A.end() );
if( verbose ) {
printf("----------------- ID_A : %lu, Drop\n", ID_A);
PrintHitList(hitList_A, "hitList_A");
PrintHitListInfo(hitList_A, "hitList_A");
}
@ -492,9 +652,9 @@ inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
hit.erase(hit.begin(), hit.begin() + ID_B );
if( verbose ) {
PrintHitList(hitTemp, "hitTemp");
PrintHitListInfo(hitTemp, "hitTemp");
printf("----------------- ID_B : %lu, Drop\n", ID_B);
PrintHitList(hit, "hit");
PrintHitListInfo(hit, "hit");
printf("=========== sume of A + B + Temp : %zu \n", hitList_A.size() + hit.size() + hitTemp.size());
printf("----------------- refill hitList_A \n");
}
@ -514,8 +674,8 @@ inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
SaveHit(hitList_A, count <= 1 ? false : true);
if( verbose ) {
PrintHitList(hitList_A, "hitList_A");
PrintHitList(hitTemp, "hitTemp");
PrintHitListInfo(hitList_A, "hitList_A");
PrintHitListInfo(hitTemp, "hitTemp");
printf("----------------- replace hitList_A by hitTemp \n");
}
@ -524,7 +684,7 @@ inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
hit.clear();
if( verbose ) {
PrintHitList(hitList_A, "hitList_A");
PrintHitListInfo(hitList_A, "hitList_A");
printf("===========================================\n");
}
@ -533,7 +693,7 @@ inline void FSUReader::SortAndSaveTS(int batchSize, bool verbose){
SaveHit(hitList_A, count <= 1? false : true);
hitList_A.clear();
hitList_A = hit;
if( verbose ) PrintHitList(hitList_A, "hitList_A");
if( verbose ) PrintHitListInfo(hitList_A, "hitList_A");
}
}

View File

@ -194,7 +194,7 @@ inline void Data::AllocateDataSize(int dataSize){
printf("dataSize cannot < 1, set dataSize = 1.\n");
dataSize = 1;
}
printf("Data::%s, size: %u, No. Ch: %u\n", __func__, dataSize, numInputCh);
//printf("Data::%s, size: %u, No. Ch: %u\n", __func__, dataSize, numInputCh);
this->dataSize = dataSize;