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 "TMath.h"
#define MaxNSample 10000 //trace length
struct Data{
unsigned short Header; /// only the last 4 bits
unsigned short BoardID;
unsigned short Channel;
uint16_t Header; /// only the last 4 bits
uint16_t BoardID;
uint16_t Channel;
unsigned long long TimeStamp;
unsigned short Energy;
unsigned int Flags;
uint64_t TimeStamp;
uint16_t Energy;
uint16_t Energy_short;
uint32_t Flags;
char WaveformCode;
unsigned int NSample;
unsigned short Trace[MaxNSample];
uint8_t WaveformCode;
uint32_t NSample;
std::vector<uint16_t> Trace;
void Clear(){
Header = 0;
Header = 0xCAE0;
BoardID = 0;
Channel = 0;
TimeStamp = 0;
Energy = 0;
Energy_short = 0;
Flags = 0;
WaveformCode = 0;
NSample = 0;
for( int i = 0; i < MaxNSample; i++) Trace[i] = 0;
Trace.clear();
};
void Print(){
printf("header : 0x%X \n", Header);
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);
if( (Header & 0x8 ) == 1 ){ /// is waevform exist
if( (Header & 0x8 ) >= 1 ){ /// is waevform exist
printf(" Wave form code : %d , nSample : %d\n", WaveformCode, NSample);
for( int i = 0 ; i < NSample ; i++){
printf("%4d | %d \n", i, Trace[i]);
@ -96,7 +97,7 @@ class BinReader{
Long64_t GetBlockID() {return blockID;}
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
void ScanNumberOfBlock();
void JumptoPrecent(int precent); ///this is offset by 1 block
@ -169,7 +170,6 @@ void BinReader::OpenFile(TString inFileName){
return ;
}
data.Header = (data.Header & 0xF);
isHeaderOK = true;
}
};
@ -206,7 +206,8 @@ template <typename T> int BinReader::FillData(T * dataItem){
return 1;
}
int BinReader::ReadBlock(int opt){
int BinReader::ReadBlock(int skipTrace){
if( inFile == nullptr ) return -1;
if( feof(inFile) ) return -1;
if( endOfFile ) return -1;
if( !isHeaderOK ) return -2;
@ -217,21 +218,30 @@ int BinReader::ReadBlock(int opt){
FillData(&data.Channel);
FillData(&data.TimeStamp);
if( (data.Header & 0x5) != 0 ) FillData(&data.Energy);
if( (data.Header & 0x3) != 0 ) FillData(&data.Energy);
if( (data.Header & 0x2) == 1 ) {
unsigned long long dummy = 0;
uint64_t dummy = 0;
FillData(&dummy);
}
if( (data.Header & 0x4 ) != 0 ) FillData(&data.Energy_short);
FillData(&data.Flags);
/// check is wave form exist
if( (data.Header & 0x8) == 1){
FillData(&data.WaveformCode);
FillData(&data.NSample);
if( opt == 0 ){
if ( fread(data.Trace, sizeof(data.Trace), 1, inFile) != 1 ) endOfFile = IsEndOfFile();
if( skipTrace == 0 ){
// 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
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 ++;
inFilePos = ftell(inFile);
if( inFilePos >= inFileSize ) endOfFile = true;
return 1;
}
void BinReader::ScanNumberOfBlock(){
@ -264,8 +276,8 @@ void BinReader::ScanNumberOfBlock(){
blockID = -1;
rewind(inFile); ///back to the File begining
unsigned short dummy;
FillData(&dummy);
// unsigned short dummy;
// FillData(&dummy);
endOfFile = false;
}