2023-12-12 15:35:47 -05:00
|
|
|
#ifndef AGGSEPARATOR_H
|
|
|
|
#define AGGSEPARATOR_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring> ///memset
|
|
|
|
#include <iostream> ///cout
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip> // for setw
|
|
|
|
#include <algorithm>
|
|
|
|
#include <bitset>
|
|
|
|
#include <vector>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
#define NumCoupledChannel 8 // the max numnber of Coupled/RegChannel is 8 for PHA, PSD, QDC
|
2023-12-12 15:35:47 -05:00
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
|
|
|
|
std::vector<std::string> AggSeperator(std::string inFileName, std::string saveFolder = "./", short verbose = false){
|
|
|
|
|
|
|
|
printf("================ AggSeperator \n");
|
|
|
|
|
|
|
|
std::vector<std::string> outputFileList;
|
|
|
|
outputFileList.clear();
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
FILE * file = fopen(inFileName.c_str(), "r");
|
|
|
|
|
|
|
|
if( file == NULL ) {
|
|
|
|
printf("file : %s cannot be open. exit program.\n", inFileName.c_str());
|
2024-01-18 18:55:37 -05:00
|
|
|
return outputFileList;
|
2023-12-12 15:35:47 -05:00
|
|
|
}
|
|
|
|
|
2024-01-17 18:19:50 -05:00
|
|
|
std::string folder = "";
|
|
|
|
size_t found = inFileName.find_last_of('/');
|
|
|
|
std::string fileName = inFileName;
|
|
|
|
if( found != std::string::npos ){
|
|
|
|
folder = inFileName.substr(0, found + 1);
|
|
|
|
fileName = inFileName.substr(found +1 );
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
if( saveFolder.empty() ) saveFolder = "./";
|
|
|
|
if( saveFolder.back() != '/') saveFolder += '/';
|
|
|
|
|
|
|
|
printf(" fileName : %s \n", fileName.c_str());
|
|
|
|
printf(" folder : %s \n", folder.c_str());
|
|
|
|
printf(" save folder : %s\n", saveFolder.c_str());
|
2024-01-17 18:19:50 -05:00
|
|
|
|
2023-12-12 15:35:47 -05:00
|
|
|
char * buffer = nullptr;
|
|
|
|
unsigned int word; // 4 bytes = 32 bits
|
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
bool newFileFlag[NumCoupledChannel];
|
|
|
|
|
|
|
|
for( int i = 0; i < NumCoupledChannel; i++){
|
|
|
|
newFileFlag[i] = true;
|
|
|
|
outputFileList.push_back( saveFolder + fileName + "." + std::to_string(i));
|
|
|
|
}
|
2023-12-12 15:35:47 -05:00
|
|
|
|
2024-01-17 18:19:50 -05:00
|
|
|
do{
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
size_t dummy = fread(&word, 4, 1, file);
|
|
|
|
if( dummy != 1 ){
|
2024-01-18 18:55:37 -05:00
|
|
|
printf("=====> End of File.\n");
|
2023-12-12 15:35:47 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
short header = ((word >> 28 ) & 0xF);
|
|
|
|
if( header != 0xA ) {
|
|
|
|
printf("header error. abort.\n");
|
|
|
|
break;
|
|
|
|
}
|
2024-01-18 18:55:37 -05:00
|
|
|
//unsigned int aggSize = (word & 0x0FFFFFFF) * 4; ///byte
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
dummy = fread(&word, 4, 1, file);
|
2024-01-18 18:55:37 -05:00
|
|
|
//unsigned int BoardID = ((word >> 27) & 0x1F);
|
|
|
|
//unsigned short pattern = ((word >> 8 ) & 0x7FFF );
|
|
|
|
//bool BoardFailFlag = ((word >> 26) & 0x1 );
|
2023-12-12 15:35:47 -05:00
|
|
|
unsigned int ChannelMask = ( word & 0xFF ) ;
|
|
|
|
|
|
|
|
dummy = fread(&word, 4, 1, file);
|
|
|
|
unsigned int bdAggCounter = word;
|
|
|
|
|
2024-01-17 18:19:50 -05:00
|
|
|
if( verbose ) printf("Agg counter : %u\n", bdAggCounter);
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
dummy = fread(&word, 4, 1, file);
|
2024-01-18 18:55:37 -05:00
|
|
|
//unsigned int aggTimeTag = word;
|
2023-12-12 15:35:47 -05:00
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
for( int chMask = 0; chMask < NumCoupledChannel ; chMask ++ ){
|
2023-12-12 15:35:47 -05:00
|
|
|
if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue;
|
2024-01-18 18:55:37 -05:00
|
|
|
if( verbose ) printf("==================== Dual/Group Channel Block, ch Mask : 0x%X (%d)\n", chMask *2, chMask );
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
dummy = fread(&word, 4, 1, file);
|
|
|
|
unsigned int dualChannelBlockSize = ( word & 0x7FFFFFFF ) * 4 ;
|
|
|
|
|
2024-01-17 18:19:50 -05:00
|
|
|
if( verbose ) printf("dual channel size : %d words\n", dualChannelBlockSize / 4);
|
2023-12-12 15:35:47 -05:00
|
|
|
|
|
|
|
buffer = new char[dualChannelBlockSize];
|
|
|
|
fseek(file, -4, SEEK_CUR);
|
|
|
|
dummy = fread(buffer, dualChannelBlockSize, 1, file);
|
|
|
|
|
|
|
|
FILE * haha = nullptr;
|
|
|
|
|
|
|
|
if( newFileFlag[chMask] ) {
|
2024-01-18 18:55:37 -05:00
|
|
|
haha = fopen( outputFileList[chMask].c_str(), "wb");
|
2023-12-12 15:35:47 -05:00
|
|
|
newFileFlag[chMask] = false;
|
|
|
|
}else{
|
2024-01-18 18:55:37 -05:00
|
|
|
haha = fopen( outputFileList[chMask].c_str(), "a+");
|
2023-12-12 15:35:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(buffer, dualChannelBlockSize, 1, haha);
|
|
|
|
|
|
|
|
fclose(haha);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-17 18:19:50 -05:00
|
|
|
}while( !feof(file));
|
|
|
|
|
2023-12-12 15:35:47 -05:00
|
|
|
fclose(file);
|
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
printf("======================= Duel channels seperated \n");
|
|
|
|
|
|
|
|
for( int i = NumCoupledChannel -1 ; i >= 0 ; i--){
|
|
|
|
if( newFileFlag[i] == true ) outputFileList.erase(outputFileList.begin() + i );
|
|
|
|
}
|
2023-12-12 15:35:47 -05:00
|
|
|
|
2024-01-18 18:55:37 -05:00
|
|
|
return outputFileList;
|
2024-01-17 18:19:50 -05:00
|
|
|
|
2023-12-12 15:35:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|