SOLARIS_GTK4_DAQ/SolReader.h

196 lines
5.0 KiB
C
Raw Normal View History

2023-01-11 17:53:37 -05:00
#ifndef SOLREADER_H
#define SOLREADER_H
#include <stdio.h> /// for FILE
#include <cstdlib>
#include <string>
#include <vector>
#include <unistd.h>
#include <time.h> // time in nano-sec
#include "Event.h"
class SolReader {
private:
FILE * inFile;
unsigned int inFileSize;
unsigned int filePos;
unsigned int totNumBlock;
unsigned short blockStart;
unsigned int numBlock;
void init();
public:
SolReader();
SolReader(std::string fileName, unsigned short dataType);
~SolReader();
void OpenFile(std::string fileName);
int ReadBlock(int opt); // opt = 0, noraml, 1, fast
void ScanNumBlock();
2023-01-13 17:35:37 -05:00
unsigned int GetNumBlock() {return numBlock;}
unsigned int GetTotalNumBlock() {return totNumBlock;}
unsigned int GetFilePos() {return filePos;}
unsigned int GetFileSize() {return inFileSize;}
2023-01-11 17:53:37 -05:00
Event * evt;
};
void SolReader::init(){
numBlock = 0;
filePos = 0;
totNumBlock = 0;
evt = new Event();
}
SolReader::SolReader(){
init();
}
SolReader::SolReader(std::string fileName, unsigned short dataType = 0){
init();
OpenFile(fileName);
evt->SetDataType(dataType);
}
SolReader::~SolReader(){
if( !inFile ) fclose(inFile);
}
inline void SolReader::OpenFile(std::string fileName){
inFile = fopen(fileName.c_str(), "r");
if( inFile == NULL ){
printf("Cannot open file : %s \n", fileName.c_str());
}else{
fseek(inFile, 0L, SEEK_END);
inFileSize = ftell(inFile);
rewind(inFile);
}
}
inline int SolReader::ReadBlock(int opt = 0){
if( inFile == NULL ) return -1;
if( feof(inFile) ) return -1;
2023-01-13 17:35:37 -05:00
if( filePos >= inFileSize) return -1;
2023-01-11 17:53:37 -05:00
fread(&blockStart, 2, 1, inFile);
if( (blockStart & 0xAAA0) != 0xAAA0 ) {
printf("header fail.\n");
return -2 ;
}
if( ( blockStart & 0xF ) == 15 ){
evt->SetDataType(15);
}
evt->dataType = blockStart & 0xF;
if( evt->dataType == 0){
if( opt == 0 ){
fread(&evt->channel, 1, 1, inFile);
fread(&evt->energy, 2, 1, inFile);
fread(&evt->timestamp, 6, 1, inFile);
fread(&evt->fine_timestamp, 2, 1, inFile);
fread(&evt->flags_high_priority, 1, 1, inFile);
fread(&evt->flags_low_priority, 2, 1, inFile);
fread(&evt->downSampling, 1, 1, inFile);
fread(&evt->board_fail, 1, 1, inFile);
fread(&evt->flush, 1, 1, inFile);
fread(&evt->trigger_threashold, 2, 1, inFile);
fread(&evt->event_size, 8, 1, inFile);
fread(&evt->aggCounter, 4, 1, inFile);
}else{
fseek(inFile, 31, SEEK_CUR);
}
fread(&evt->traceLenght, 8, 1, inFile);
if( opt == 0){
fread(evt->analog_probes_type, 2, 1, inFile);
fread(evt->digital_probes_type, 4, 1, inFile);
fread(evt->analog_probes[0], evt->traceLenght*4, 1, inFile);
fread(evt->analog_probes[1], evt->traceLenght*4, 1, inFile);
fread(evt->digital_probes[0], evt->traceLenght, 1, inFile);
fread(evt->digital_probes[1], evt->traceLenght, 1, inFile);
fread(evt->digital_probes[2], evt->traceLenght, 1, inFile);
fread(evt->digital_probes[3], evt->traceLenght, 1, inFile);
}else{
fseek(inFile, 6 + evt->traceLenght*(12), SEEK_CUR);
}
}else if( evt->dataType == 1){
if( opt == 0 ){
fread(&evt->channel, 1, 1, inFile);
fread(&evt->energy, 2, 1, inFile);
fread(&evt->timestamp, 6, 1, inFile);
fread(&evt->fine_timestamp, 2, 1, inFile);
fread(&evt->flags_high_priority, 1, 1, inFile);
fread(&evt->flags_low_priority, 2, 1, inFile);
}else{
fseek(inFile, 14, SEEK_CUR);
}
fread(&evt->traceLenght, 8, 1, inFile);
if( opt == 0){
fread(&evt->analog_probes_type[0], 1, 1, inFile);
fread(evt->analog_probes[0], evt->traceLenght*4, 1, inFile);
}else{
fseek(inFile, 1 + evt->traceLenght*4, SEEK_CUR);
}
}else if( evt->dataType == 2){
if( opt == 0 ){
fread(&evt->channel, 1, 1, inFile);
fread(&evt->energy, 2, 1, inFile);
fread(&evt->timestamp, 6, 1, inFile);
fread(&evt->fine_timestamp, 2, 1, inFile);
fread(&evt->flags_high_priority, 1, 1, inFile);
fread(&evt->flags_low_priority, 2, 1, inFile);
}else{
fseek(inFile, 14, SEEK_CUR);
}
}else if( evt->dataType == 3){
if( opt == 0 ){
fread(&evt->channel, 1, 1, inFile);
fread(&evt->energy, 2, 1, inFile);
fread(&evt->timestamp, 6, 1, inFile);
}else{
fseek(inFile, 9, SEEK_CUR);
}
}else if( evt->dataType == 15){
fread(&evt->dataSize, 8, 1, inFile);
if( opt == 0){
fread(evt->data, evt->dataSize, 1, inFile);
}else{
fseek(inFile, evt->dataSize, SEEK_CUR);
}
}
numBlock ++;
filePos = ftell(inFile);
return 0;
}
void SolReader::ScanNumBlock(){
if( inFile == NULL ) return;
if( feof(inFile) ) return;
2023-01-13 17:35:37 -05:00
numBlock = 0;
2023-01-11 17:53:37 -05:00
while( ReadBlock(1) == 0){
//printf("%u, traceLen %lu, %u/%u\n", numBlock, evt->traceLenght, filePos, inFileSize);
}
totNumBlock = numBlock;
numBlock = 0;
printf("Scan complete: number of data Block : %u\n", totNumBlock);
rewind(inFile);
filePos = 0;
}
#endif