2022-12-01 12:50:48 -05:00
|
|
|
#ifndef BINREADER_H
|
|
|
|
#define BINREADER_H
|
|
|
|
#include <stdio.h> /// for FILE
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "TString.h"
|
|
|
|
#include "TBenchmark.h"
|
|
|
|
#include "TMath.h"
|
|
|
|
|
|
|
|
struct Data{
|
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
uint16_t Header; /// only the last 4 bits
|
|
|
|
uint16_t BoardID;
|
|
|
|
uint16_t Channel;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
uint64_t TimeStamp;
|
|
|
|
uint16_t Energy;
|
|
|
|
uint16_t Energy_short;
|
|
|
|
uint32_t Flags;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
uint8_t WaveformCode;
|
|
|
|
uint32_t NSample;
|
|
|
|
std::vector<uint16_t> Trace;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
void Clear(){
|
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
Header = 0xCAE0;
|
2022-12-01 12:50:48 -05:00
|
|
|
BoardID = 0;
|
|
|
|
Channel = 0;
|
|
|
|
TimeStamp = 0;
|
|
|
|
Energy = 0;
|
2024-07-17 15:46:23 -04:00
|
|
|
Energy_short = 0;
|
2022-12-01 12:50:48 -05:00
|
|
|
Flags = 0;
|
|
|
|
WaveformCode = 0;
|
|
|
|
NSample = 0;
|
2024-07-17 15:46:23 -04:00
|
|
|
Trace.clear();
|
|
|
|
|
2022-12-01 12:50:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void Print(){
|
|
|
|
|
|
|
|
printf("header : 0x%X \n", Header);
|
|
|
|
printf(" Board : %u , Channel : %u\n", BoardID, Channel);
|
2024-07-18 19:02:22 -04:00
|
|
|
if( Header & 4 ) {
|
|
|
|
printf("Energy : %u, Energy2 : %u, TimeStamp : %lu ps\n", Energy, Energy_short, TimeStamp);
|
|
|
|
}else{
|
|
|
|
printf("Energy : %u, TimeStamp : %lu ps\n", Energy, TimeStamp);
|
|
|
|
}
|
2022-12-01 12:50:48 -05:00
|
|
|
printf(" Flag : 0x%X\n", Flags);
|
2024-07-17 15:46:23 -04:00
|
|
|
if( (Header & 0x8 ) >= 1 ){ /// is waevform exist
|
2022-12-01 12:50:48 -05:00
|
|
|
printf(" Wave form code : %d , nSample : %d\n", WaveformCode, NSample);
|
|
|
|
for( int i = 0 ; i < NSample ; i++){
|
|
|
|
printf("%4d | %d \n", i, Trace[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BinReader{
|
|
|
|
public:
|
|
|
|
Data data;
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE * inFile;
|
|
|
|
|
|
|
|
long int inFileSize;
|
|
|
|
long int inFilePos;
|
|
|
|
bool endOfFile;
|
|
|
|
bool isOpened;
|
2024-07-18 19:02:22 -04:00
|
|
|
Long64_t hitID;
|
|
|
|
long int numHit;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
TBenchmark gClock;
|
|
|
|
|
|
|
|
long int inFilePosPrecent[10];
|
|
|
|
Long64_t blockIDPrecent[10];
|
|
|
|
|
|
|
|
bool isHeaderOK;
|
|
|
|
|
|
|
|
template <typename T> int FillData(T * dataItem); // if successful, return 1, else 0; cannot fill Trace
|
|
|
|
|
|
|
|
///============================================ Methods
|
|
|
|
public:
|
|
|
|
BinReader();
|
|
|
|
BinReader(TString inFileName);
|
|
|
|
~BinReader();
|
|
|
|
|
|
|
|
void OpenFile(TString inFileName);
|
|
|
|
void CloseFile();
|
|
|
|
void UpdateFileSize();
|
|
|
|
bool IsEndOfFile();
|
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
bool IsOpen() const {return isOpened;}
|
|
|
|
long int GetFilePos() const {return inFilePos;}
|
|
|
|
long int GetFileSize() const {return inFileSize;}
|
|
|
|
Long64_t GetHitID() const {return hitID;}
|
|
|
|
Long64_t GetNumHit() const {return numHit;}
|
2022-12-01 12:50:48 -05:00
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
int ReadBlock(int skipTrace = 0); /// 0 = default, fill waveform if any. slow
|
2022-12-01 12:50:48 -05:00
|
|
|
/// 1 = no fill waveform, fast
|
2024-07-18 19:02:22 -04:00
|
|
|
void ScanNumHit();
|
2022-12-01 12:50:48 -05:00
|
|
|
void JumptoPrecent(int precent); ///this is offset by 1 block
|
|
|
|
void PrintStatus(int mod);
|
|
|
|
|
|
|
|
};
|
|
|
|
//========================== implementation
|
|
|
|
|
|
|
|
BinReader::BinReader(){
|
|
|
|
inFile = 0;
|
|
|
|
data.Clear();
|
|
|
|
|
|
|
|
inFileSize = 0;
|
|
|
|
inFilePos = 0;
|
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
numHit = 0;
|
|
|
|
hitID = -1;
|
2022-12-01 12:50:48 -05:00
|
|
|
endOfFile = false;
|
|
|
|
isOpened = false;
|
|
|
|
|
|
|
|
isHeaderOK = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BinReader::~BinReader(){
|
|
|
|
fclose(inFile); /// fclose already delete inFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BinReader::BinReader(TString inFileName){
|
|
|
|
inFile = 0;
|
|
|
|
data.Clear();
|
|
|
|
|
|
|
|
inFileSize = 0;
|
|
|
|
inFilePos = 0;
|
2024-07-18 19:02:22 -04:00
|
|
|
numHit = 0;
|
|
|
|
hitID = -1;
|
2022-12-01 12:50:48 -05:00
|
|
|
endOfFile = false;
|
|
|
|
isOpened = false;
|
|
|
|
|
|
|
|
OpenFile(inFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinReader::OpenFile(TString inFileName){
|
|
|
|
inFile = fopen(inFileName, "r");
|
|
|
|
if( inFile == NULL ){
|
|
|
|
printf("Cannot read file : %s \n", inFileName.Data());
|
|
|
|
}else{
|
|
|
|
fseek(inFile, 0L, SEEK_END);
|
|
|
|
inFileSize = ftell(inFile);
|
|
|
|
rewind(inFile); ///back to the File begining
|
|
|
|
data.Clear();
|
|
|
|
|
|
|
|
gClock.Reset();
|
|
|
|
gClock.Start("timer");
|
|
|
|
|
|
|
|
isOpened = true;
|
|
|
|
|
|
|
|
///============= Read the Header,
|
|
|
|
/// the header only at the beginning of a file
|
|
|
|
isHeaderOK = FillData(&data.Header);
|
|
|
|
if( isHeaderOK == false ){
|
|
|
|
printf(" header cannot read. \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
///printf("HEADER: 0x%X , 0x%X\n", data.Header, (data.Header >> 4));
|
|
|
|
if( (data.Header >> 4 ) != 0xCAE ) {
|
|
|
|
printf(" Header format not right. \n");
|
|
|
|
isHeaderOK = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
isHeaderOK = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void BinReader::CloseFile(){
|
|
|
|
fclose(inFile);
|
|
|
|
isOpened = false;
|
|
|
|
data.Clear();
|
|
|
|
inFileSize = 0;
|
|
|
|
inFilePos = 0;
|
2024-07-18 19:02:22 -04:00
|
|
|
numHit = 0;
|
|
|
|
hitID = -1;
|
2022-12-01 12:50:48 -05:00
|
|
|
endOfFile = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BinReader::UpdateFileSize(){
|
|
|
|
if( inFile == NULL ) return;
|
|
|
|
fseek(inFile, 0L, SEEK_END);
|
|
|
|
inFileSize = ftell(inFile);
|
|
|
|
fseek(inFile, inFilePos, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinReader::IsEndOfFile() {
|
2024-07-18 19:02:22 -04:00
|
|
|
if( endOfFile ) return true;
|
|
|
|
return feof(inFile) > 0 ? true: false;
|
2022-12-01 12:50:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> int BinReader::FillData(T * dataItem){
|
|
|
|
if ( fread(dataItem, sizeof(*dataItem), 1, inFile) != 1 ) {
|
2024-07-18 19:02:22 -04:00
|
|
|
printf("!!!!! problem for reading data\n");
|
2022-12-01 12:50:48 -05:00
|
|
|
endOfFile = IsEndOfFile();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
///printf("----- 0x%16llX \n", *dataItem);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
int BinReader::ReadBlock(int skipTrace){
|
|
|
|
if( inFile == nullptr ) return -1;
|
2022-12-01 12:50:48 -05:00
|
|
|
if( feof(inFile) ) return -1;
|
|
|
|
if( endOfFile ) return -1;
|
|
|
|
if( !isHeaderOK ) return -2;
|
|
|
|
|
|
|
|
/// see the CoMPASS manual v19, P.67
|
|
|
|
|
|
|
|
FillData(&data.BoardID);
|
|
|
|
FillData(&data.Channel);
|
|
|
|
FillData(&data.TimeStamp);
|
|
|
|
|
2024-07-17 15:46:23 -04:00
|
|
|
if( (data.Header & 0x3) != 0 ) FillData(&data.Energy);
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
if( (data.Header & 0x2) == 1 ) {
|
2024-07-17 15:46:23 -04:00
|
|
|
uint64_t dummy = 0;
|
2022-12-01 12:50:48 -05:00
|
|
|
FillData(&dummy);
|
|
|
|
}
|
2024-07-17 15:46:23 -04:00
|
|
|
|
|
|
|
if( (data.Header & 0x4 ) != 0 ) FillData(&data.Energy_short);
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
FillData(&data.Flags);
|
|
|
|
|
|
|
|
/// check is wave form exist
|
|
|
|
if( (data.Header & 0x8) == 1){
|
|
|
|
FillData(&data.WaveformCode);
|
|
|
|
FillData(&data.NSample);
|
2024-07-17 15:46:23 -04:00
|
|
|
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;
|
2024-07-18 19:02:22 -04:00
|
|
|
size_t dummy = fread(&haha, 2, 1, inFile);
|
2024-07-17 15:46:23 -04:00
|
|
|
data.Trace.push_back(haha);
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:50:48 -05:00
|
|
|
}else{ /// skip trace
|
|
|
|
fseek(inFile, inFilePos + data.NSample*2, SEEK_SET); /// 2 becasue each trace is 2 bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
hitID ++;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
inFilePos = ftell(inFile);
|
2024-07-17 15:46:23 -04:00
|
|
|
if( inFilePos >= inFileSize ) endOfFile = true;
|
|
|
|
|
2022-12-01 12:50:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2024-07-18 19:02:22 -04:00
|
|
|
void BinReader::ScanNumHit(){
|
2022-12-01 12:50:48 -05:00
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
numHit = 0;
|
2022-12-01 12:50:48 -05:00
|
|
|
int count = 0;
|
|
|
|
while( ReadBlock(1) != -1 ){
|
2024-07-18 19:02:22 -04:00
|
|
|
numHit ++;
|
2022-12-01 12:50:48 -05:00
|
|
|
int haha = (inFilePos*10/inFileSize)%10;
|
|
|
|
if( haha == count ) {
|
|
|
|
inFilePosPrecent[count] = inFilePos;
|
2024-07-18 19:02:22 -04:00
|
|
|
blockIDPrecent[count] = hitID;
|
2022-12-01 12:50:48 -05:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
PrintStatus(10000);
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
PrintStatus(1);
|
2022-12-01 12:50:48 -05:00
|
|
|
printf("\n\n\n");
|
2024-07-18 19:02:22 -04:00
|
|
|
printf("scan complete: number of data Block : %ld\n", numHit);
|
2022-12-01 12:50:48 -05:00
|
|
|
|
2024-07-18 19:02:22 -04:00
|
|
|
rewind(inFile); ///back to the File begining
|
|
|
|
FillData(&data.Header);
|
2022-12-01 12:50:48 -05:00
|
|
|
inFilePos = 0;
|
2024-07-18 19:02:22 -04:00
|
|
|
hitID = -1;
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
endOfFile = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
void BinReader::JumptoPrecent(int precent){
|
|
|
|
|
|
|
|
if( precent < 0 || precent > 10 ) {
|
|
|
|
printf("input precent should be 0 to 10\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(inFile, inFilePosPrecent[precent], SEEK_SET);
|
2024-07-18 19:02:22 -04:00
|
|
|
hitID = blockIDPrecent[precent];
|
2022-12-01 12:50:48 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
void BinReader::PrintStatus(int mod){
|
|
|
|
|
|
|
|
///==== event stats, print status every 10000 events
|
2024-07-18 19:02:22 -04:00
|
|
|
if ( hitID % mod == 0 ) {
|
2022-12-01 12:50:48 -05:00
|
|
|
UpdateFileSize();
|
|
|
|
gClock.Stop("timer");
|
|
|
|
double time = gClock.GetRealTime("timer");
|
|
|
|
gClock.Start("timer");
|
2024-07-18 19:02:22 -04:00
|
|
|
printf("Hit ID: \x1B[32m%llu \x1B[0m\nReading Pos: \x1B[32m %.3f/%.3f GB\x1B[0m\nTime used:%3.0f min %5.2f sec\033[A\033[A\r",
|
|
|
|
hitID, inFilePos/(1024.*1024.*1024.), inFileSize/1024./1024./1024, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
|
2022-12-01 12:50:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|