fix BinReader.h for no header data format

This commit is contained in:
Ryan@Home 2024-09-25 16:47:15 -04:00
parent 84851051f7
commit 5ae7c256f2
5 changed files with 48 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
*.o *.o
*.fsu *.fsu
*.root *.root
*.BIN
data data

View File

@ -106,8 +106,7 @@ int main(int argc, char **argv) {
printf(" %s 100 0 '\\ls -1 *001*.bin' (event build with 100 ns, no trace, no verbose)\n", argv[0]); printf(" %s 100 0 '\\ls -1 *001*.bin' (event build with 100 ns, no trace, no verbose)\n", argv[0]);
printf("\n"); printf("\n");
printf(" Time offset file format :\n"); printf(" Time offset file format :\n");
printf("[n, sn appears between the n-th - (n+1)-th underscore of the fileName] 0\n"); printf("[board SN] [time offset(int64_t)]\n");
printf("[digi sn] [time offset(int64_t)]\n");
printf("\n"); printf("\n");
printf(" file name should be this format CH0@V1725_324_Data_run_196.bin\n"); printf(" file name should be this format CH0@V1725_324_Data_run_196.bin\n");
printf(" so that the serial number is between the 1st and 2nd underscore\n"); printf(" so that the serial number is between the 1st and 2nd underscore\n");
@ -160,8 +159,8 @@ int main(int argc, char **argv) {
if( timeOffsetFile != "0" ) { if( timeOffsetFile != "0" ) {
printf(" Time Offset File = %s\n", timeOffsetFile.c_str()); printf(" Time Offset File = %s\n", timeOffsetFile.c_str());
timeOffsetList = readFileAndExtractData(timeOffsetFile); timeOffsetList = readFileAndExtractData(timeOffsetFile);
for (size_t i = 0 ; timeOffsetList.size(); i++) { for (size_t i = 0 ; i < timeOffsetList.size(); i++) {
printf("Digi-%4d %16llu ps\n", timeOffsetList[i].first, timeOffsetList[i].second); printf("Board-%4d %16llu ps\n", timeOffsetList[i].first, timeOffsetList[i].second);
} }
} }
printf("========================================= Number of Files : %d \n", nFile); printf("========================================= Number of Files : %d \n", nFile);

View File

@ -60,7 +60,7 @@ struct Data{
} }
}; };
//^################################################
class BinReader{ class BinReader{
public: public:
Data data; Data data;
@ -83,16 +83,18 @@ class BinReader{
bool isHeaderOK; bool isHeaderOK;
bool isOldFormat; bool isOldFormat;
bool isNoHeaderFormat;
template <typename T> int FillData(T * dataItem); // if successful, return 1, else 0; cannot fill Trace template <typename T> int FillData(T * dataItem); // if successful, return 1, else 0; cannot fill Trace
///============================================ Methods ///============================================ Methods
public: public:
BinReader(); BinReader();
BinReader(TString inFileName); BinReader(TString inFileName, int64_t timeOffset = 0, bool noHeader = false);
~BinReader(); ~BinReader();
void OpenFile(TString inFileName, int64_t timeOffset = 0); void OpenFile(TString inFileName, int64_t timeOffset = 0, bool noHeader = false);
// void SetDataFormat(bool has );
void CloseFile(); void CloseFile();
void UpdateFileSize(); void UpdateFileSize();
bool IsEndOfFile(); bool IsEndOfFile();
@ -128,6 +130,7 @@ BinReader::BinReader(){
isHeaderOK = false; isHeaderOK = false;
isOldFormat = false; isOldFormat = false;
isNoHeaderFormat = false;
} }
@ -135,8 +138,7 @@ BinReader::~BinReader(){
fclose(inFile); /// fclose already delete inFile; fclose(inFile); /// fclose already delete inFile;
} }
BinReader::BinReader(TString inFileName, int64_t timeOffset, bool noHeader){
BinReader::BinReader(TString inFileName){
inFile = 0; inFile = 0;
data.Clear(); data.Clear();
@ -147,10 +149,10 @@ BinReader::BinReader(TString inFileName){
endOfFile = false; endOfFile = false;
isOpened = false; isOpened = false;
OpenFile(inFileName); OpenFile(inFileName, timeOffset, noHeader);
} }
void BinReader::OpenFile(TString inFileName, int64_t timeOffset){ void BinReader::OpenFile(TString inFileName, int64_t timeOffset, bool noHeader){
inFile = fopen(inFileName, "r"); inFile = fopen(inFileName, "r");
this->timeOffset = timeOffset; this->timeOffset = timeOffset;
if( inFile == NULL ){ if( inFile == NULL ){
@ -166,6 +168,14 @@ void BinReader::OpenFile(TString inFileName, int64_t timeOffset){
isOpened = true; isOpened = true;
if( noHeader ){
data.Header = 0x0;
isOldFormat = false;
isHeaderOK = false;
isNoHeaderFormat = true;
return;
}
///============= Read the Header, ///============= Read the Header,
/// the header only at the beginning of a file /// the header only at the beginning of a file
isHeaderOK = FillData(&data.Header); isHeaderOK = FillData(&data.Header);
@ -179,13 +189,16 @@ void BinReader::OpenFile(TString inFileName, int64_t timeOffset){
printf(" Old format of CoMPASS, 0x%04x\n", data.Header); printf(" Old format of CoMPASS, 0x%04x\n", data.Header);
isHeaderOK = true; isHeaderOK = true;
isOldFormat = true; isOldFormat = true;
isNoHeaderFormat = false;
data.Header = 0xCAE5; // only with energy and energy short data.Header = 0xCAE5; // only with energy and energy short
return; return;
} }
if( (data.Header >> 4 ) != 0xCAE ) { if( (data.Header >> 4 ) != 0xCAE ) {
printf(" Header format not right. 0x%04x\n", data.Header); printf(" Header format not right. 0x%04x\n", data.Header);
isOldFormat = false;
isHeaderOK = false; isHeaderOK = false;
isNoHeaderFormat = false;
return ; return ;
} }
@ -230,10 +243,27 @@ int BinReader::ReadBlock(int skipTrace){
if( inFile == nullptr ) return -1; if( inFile == nullptr ) return -1;
if( feof(inFile) ) return -1; if( feof(inFile) ) return -1;
if( endOfFile ) return -1; if( endOfFile ) return -1;
if( isNoHeaderFormat ) {
FillData(&data.BoardID);
FillData(&data.Channel);
FillData(&data.TimeStamp);
if( timeOffset != 0 ) data.TimeStamp += timeOffset;
FillData(&data.Energy);
FillData(&data.Flags);
hitID ++;
inFilePos = ftell(inFile);
if( inFilePos >= inFileSize ) endOfFile = true;
return 1;
}
if( !isHeaderOK ) return -2; if( !isHeaderOK ) return -2;
/// see the CoMPASS manual v19, P.67 /// see the CoMPASS manual v19, P.67
FillData(&data.BoardID); FillData(&data.BoardID);
FillData(&data.Channel); FillData(&data.Channel);
FillData(&data.TimeStamp); FillData(&data.TimeStamp);
@ -280,6 +310,7 @@ int BinReader::ReadBlock(int skipTrace){
return 1; return 1;
} }
void BinReader::ScanNumHit(){ void BinReader::ScanNumHit(){
numHit = 0; numHit = 0;
@ -300,7 +331,7 @@ void BinReader::ScanNumHit(){
printf("scan complete: number of data Block : %ld\n", numHit); printf("scan complete: number of data Block : %ld\n", numHit);
rewind(inFile); ///back to the File begining rewind(inFile); ///back to the File begining
if( !isOldFormat ){ if( !isOldFormat && isHeaderOK ){
FillData(&data.Header); FillData(&data.Header);
} }
inFilePos = 0; inFilePos = 0;

5
test.C
View File

@ -18,7 +18,8 @@ TH1I * he[NChannel];
// BinReader * reader = new BinReader("/home/ryan/ExpData/fsu_testing/testing_014_single.bin"); // BinReader * reader = new BinReader("/home/ryan/ExpData/fsu_testing/testing_014_single.bin");
// BinReader * reader = new BinReader("data/run_123/UNFILTERED/Data_CH9@V1725S_19555_run_123.BIN"); // BinReader * reader = new BinReader("data/run_123/UNFILTERED/Data_CH9@V1725S_19555_run_123.BIN");
BinReader * reader = new BinReader("run_268/CH0@V1725_324_Data_run_268.bin"); // BinReader * reader = new BinReader("run_268/CH0@V1725_324_Data_run_268.bin");
BinReader * reader = new BinReader("DataR_run_34_108.BIN", 0, true);
void test(){ void test(){
@ -35,7 +36,7 @@ void test(){
reader->data.Print(); reader->data.Print();
count ++; count ++;
// if( count > 10 ) break; if( count > 10 ) break;
}while( !reader->IsEndOfFile() ); }while( !reader->IsEndOfFile() );

View File

@ -1,4 +1,3 @@
1 0
123 456 123 456
86 49 86 49
19267 0 19267 0