some fix on the BinReader.h

This commit is contained in:
Ryan Tang 2024-07-17 15:46:23 -04:00
parent 3c322e1e7b
commit 8d3f28d864
3 changed files with 58 additions and 24 deletions

17
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/root/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"test.C": "cpp"
}
}

View File

@ -10,42 +10,43 @@
#include "TBenchmark.h" #include "TBenchmark.h"
#include "TMath.h" #include "TMath.h"
#define MaxNSample 10000 //trace length
struct Data{ struct Data{
unsigned short Header; /// only the last 4 bits uint16_t Header; /// only the last 4 bits
unsigned short BoardID; uint16_t BoardID;
unsigned short Channel; uint16_t Channel;
unsigned long long TimeStamp; uint64_t TimeStamp;
unsigned short Energy; uint16_t Energy;
unsigned int Flags; uint16_t Energy_short;
uint32_t Flags;
char WaveformCode; uint8_t WaveformCode;
unsigned int NSample; uint32_t NSample;
unsigned short Trace[MaxNSample]; std::vector<uint16_t> Trace;
void Clear(){ void Clear(){
Header = 0; Header = 0xCAE0;
BoardID = 0; BoardID = 0;
Channel = 0; Channel = 0;
TimeStamp = 0; TimeStamp = 0;
Energy = 0; Energy = 0;
Energy_short = 0;
Flags = 0; Flags = 0;
WaveformCode = 0; WaveformCode = 0;
NSample = 0; NSample = 0;
for( int i = 0; i < MaxNSample; i++) Trace[i] = 0; Trace.clear();
}; };
void Print(){ void Print(){
printf("header : 0x%X \n", Header); printf("header : 0x%X \n", Header);
printf(" Board : %u , Channel : %u\n", BoardID, Channel); printf(" Board : %u , Channel : %u\n", BoardID, Channel);
printf("Energy : %u , TimeStamp : %llu\n", Energy, TimeStamp); printf("Energy : %u , TimeStamp : %lu ps\n", Energy, TimeStamp);
printf(" Flag : 0x%X\n", Flags); printf(" Flag : 0x%X\n", Flags);
if( (Header & 0x8 ) == 1 ){ /// is waevform exist if( (Header & 0x8 ) >= 1 ){ /// is waevform exist
printf(" Wave form code : %d , nSample : %d\n", WaveformCode, NSample); printf(" Wave form code : %d , nSample : %d\n", WaveformCode, NSample);
for( int i = 0 ; i < NSample ; i++){ for( int i = 0 ; i < NSample ; i++){
printf("%4d | %d \n", i, Trace[i]); printf("%4d | %d \n", i, Trace[i]);
@ -96,7 +97,7 @@ class BinReader{
Long64_t GetBlockID() {return blockID;} Long64_t GetBlockID() {return blockID;}
Long64_t GetNumberOfBlock() {return nBlock;} Long64_t GetNumberOfBlock() {return nBlock;}
int ReadBlock(int opt = 0); /// 0 = default, fill waveform if any. slow int ReadBlock(int skipTrace = 0); /// 0 = default, fill waveform if any. slow
/// 1 = no fill waveform, fast /// 1 = no fill waveform, fast
void ScanNumberOfBlock(); void ScanNumberOfBlock();
void JumptoPrecent(int precent); ///this is offset by 1 block void JumptoPrecent(int precent); ///this is offset by 1 block
@ -169,7 +170,6 @@ void BinReader::OpenFile(TString inFileName){
return ; return ;
} }
data.Header = (data.Header & 0xF);
isHeaderOK = true; isHeaderOK = true;
} }
}; };
@ -206,7 +206,8 @@ template <typename T> int BinReader::FillData(T * dataItem){
return 1; return 1;
} }
int BinReader::ReadBlock(int opt){ int BinReader::ReadBlock(int skipTrace){
if( inFile == nullptr ) return -1;
if( feof(inFile) ) return -1; if( feof(inFile) ) return -1;
if( endOfFile ) return -1; if( endOfFile ) return -1;
if( !isHeaderOK ) return -2; if( !isHeaderOK ) return -2;
@ -217,12 +218,14 @@ int BinReader::ReadBlock(int opt){
FillData(&data.Channel); FillData(&data.Channel);
FillData(&data.TimeStamp); FillData(&data.TimeStamp);
if( (data.Header & 0x5) != 0 ) FillData(&data.Energy); if( (data.Header & 0x3) != 0 ) FillData(&data.Energy);
if( (data.Header & 0x2) == 1 ) { if( (data.Header & 0x2) == 1 ) {
unsigned long long dummy = 0; uint64_t dummy = 0;
FillData(&dummy); FillData(&dummy);
} }
if( (data.Header & 0x4 ) != 0 ) FillData(&data.Energy_short);
FillData(&data.Flags); FillData(&data.Flags);
@ -230,8 +233,15 @@ int BinReader::ReadBlock(int opt){
if( (data.Header & 0x8) == 1){ if( (data.Header & 0x8) == 1){
FillData(&data.WaveformCode); FillData(&data.WaveformCode);
FillData(&data.NSample); FillData(&data.NSample);
if( opt == 0 ){ if( skipTrace == 0 ){
if ( fread(data.Trace, sizeof(data.Trace), 1, inFile) != 1 ) endOfFile = IsEndOfFile(); // if ( fread(data.Trace, sizeof(data.Trace), 1, inFile) != 1 ) endOfFile = IsEndOfFile();
for( int i = 0; i < data.NSample; i++ ){
uint16_t haha;
fread(&haha, 2, 1, inFile);
data.Trace.push_back(haha);
}
}else{ /// skip trace }else{ /// skip trace
fseek(inFile, inFilePos + data.NSample*2, SEEK_SET); /// 2 becasue each trace is 2 bytes fseek(inFile, inFilePos + data.NSample*2, SEEK_SET); /// 2 becasue each trace is 2 bytes
} }
@ -240,6 +250,8 @@ int BinReader::ReadBlock(int opt){
blockID ++; blockID ++;
inFilePos = ftell(inFile); inFilePos = ftell(inFile);
if( inFilePos >= inFileSize ) endOfFile = true;
return 1; return 1;
} }
void BinReader::ScanNumberOfBlock(){ void BinReader::ScanNumberOfBlock(){
@ -264,8 +276,8 @@ void BinReader::ScanNumberOfBlock(){
blockID = -1; blockID = -1;
rewind(inFile); ///back to the File begining rewind(inFile); ///back to the File begining
unsigned short dummy; // unsigned short dummy;
FillData(&dummy); // FillData(&dummy);
endOfFile = false; endOfFile = false;
} }