FRIB_e21062/armory/evtReader.h

397 lines
10 KiB
C
Raw Normal View History

2022-05-24 18:21:12 -04:00
#ifndef EVTREADER_H
#define EVTREADER_H
#include <stdio.h> /// for FILE
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "TString.h"
#include "TBenchmark.h"
#include "../armory/DataBlock.h"
#define MAX_CRATES 2
#define MAX_BOARDS_PER_CRATE 13
#define MAX_CHANNELS_PER_BOARD 16
#define BOARD_START 2
class evtReader{
public:
DataBlock * data;
private:
FILE * inFile;
long int inFileSize;
long int inFilePos;
bool endOfFile;
bool isOpened;
Long64_t blockID;
long int nBlock;
unsigned int extraHeader[14];
unsigned int traceBlock[MAX_TRACE_LENGHT/2];
TBenchmark gClock;
long int inFilePosPrecent[10];
Long64_t blockIDPrecent[10];
int * pxidata;
long nWords;
2022-06-06 19:57:17 -04:00
bool isNSCL;
unsigned int rib_size[1];
unsigned int readRingItemByte;
2022-05-24 18:21:12 -04:00
///============================================ Methods
public:
evtReader();
2022-06-06 19:57:17 -04:00
evtReader(TString inFileName, bool isNSCL);
2022-05-24 18:21:12 -04:00
~evtReader();
2022-06-06 19:57:17 -04:00
void OpenFile(TString inFileName, bool isNSCL);
2022-05-24 18:21:12 -04:00
void CloseFile();
void UpdateFileSize();
bool IsEndOfFile();
bool IsOpen() {return isOpened;}
long int GetFilePos() {return inFilePos;}
long int GetFileSize() {return inFileSize;}
Long64_t GetBlockID() {return blockID;}
Long64_t GetNumberOfBlock() {return nBlock;}
int ReadBlock(int opt = 0); /// 0 = default, fill data
/// 1 = no fill data
/// 2 = fill data and print
void ScanNumberOfBlock();
void JumptoPrecent(int precent); ///this is offset by 1 block
void PrintStatus(int mod);
2022-06-06 19:57:17 -04:00
void SetNSCL(bool isNSCL_Evt);
2022-05-24 18:21:12 -04:00
};
//========================== implementation
evtReader::evtReader(){
inFile = 0;
data = new DataBlock();
inFileSize = 0;
inFilePos = 0;
nBlock = 0;
blockID = -1;
endOfFile = false;
isOpened = false;
pxidata = NULL;
nWords = 0;
2022-06-06 19:57:17 -04:00
isNSCL = false;
rib_size[0] = 0;
readRingItemByte = 0;
2022-05-24 18:21:12 -04:00
}
evtReader::~evtReader(){
fclose(inFile);
delete inFile;
delete data;
delete pxidata;
}
2022-06-06 19:57:17 -04:00
evtReader::evtReader(TString inFileName, bool isNSCL = false){
2022-05-24 18:21:12 -04:00
inFile = 0;
data = new DataBlock();
inFileSize = 0;
inFilePos = 0;
nBlock = 0;
blockID = -1;
endOfFile = false;
isOpened = false;
pxidata = NULL;
nWords = 0;
2022-06-06 19:57:17 -04:00
OpenFile(inFileName, isNSCL);
2022-05-24 18:21:12 -04:00
}
2022-06-06 19:57:17 -04:00
void evtReader::OpenFile(TString inFileName, bool isNSCL = false){
2022-05-24 18:21:12 -04:00
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();
2022-06-06 19:57:17 -04:00
this->isNSCL = isNSCL;
rib_size[0] = 0;
readRingItemByte = 0;
2022-05-24 18:21:12 -04:00
gClock.Reset();
gClock.Start("timer");
isOpened = true;
}
};
void evtReader::CloseFile(){
fclose(inFile);
isOpened = false;
data->Clear();
inFileSize = 0;
inFilePos = 0;
nBlock = 0;
blockID = -1;
endOfFile = false;
2022-06-06 19:57:17 -04:00
isNSCL = false;
2022-05-24 18:21:12 -04:00
};
void evtReader::UpdateFileSize(){
if( inFile == NULL ) return;
fseek(inFile, 0L, SEEK_END);
inFileSize = ftell(inFile);
fseek(inFile, inFilePos, SEEK_SET);
}
bool evtReader::IsEndOfFile() {
int haha = feof(inFile);
return haha > 0 ? true: false;
}
2022-06-06 19:57:17 -04:00
void evtReader::SetNSCL(bool isNSCL_Evt){
isNSCL = isNSCL_Evt;
}
2022-05-24 18:21:12 -04:00
int evtReader::ReadBlock(int opt){
if( feof(inFile) ) return -1;
if( endOfFile ) return -1;
2022-06-06 19:57:17 -04:00
if( isNSCL && readRingItemByte == rib_size[0]) {
//============= Ring item header, [0] = size, [1] = type
unsigned int rih[2]= {0};
do{
if ( fread(rih, sizeof(rih), 1, inFile) != 1 ) {
endOfFile = true;
return -1;
}
inFilePos = ftell(inFile);
///printf("%ld-------- Ring Item Header| size : %d, type : %d \n", inFilePos, rih[0], rih[1]);
///if the type is not 30, skip
if( rih[1] != 30 ) {
fseek(inFile, rih[0]-8, SEEK_CUR);
inFilePos = ftell(inFile);
///printf("%ld---- skip %d bytes\n", inFilePos, rih[0]-8);
}
}while(rih[1] != 30);
2022-05-24 18:21:12 -04:00
2022-06-06 19:57:17 -04:00
//=========== Ring item body header, [0] = size, [1] timestamp-low, [2] timestamp-high, [3] source ID, [4] = barrier type
unsigned int ribh[5]={0};
if ( fread(ribh, sizeof(ribh), 1, inFile) != 1 ) {
endOfFile = true;
return -1;
}
inFilePos = ftell(inFile);
unsigned long long timestamp = ((unsigned long long)ribh[2] << 32) + ribh[1];
///printf("%ld - Ring Item Body Header| size : %d, timestamp : %llu \n", inFilePos, ribh[0], timestamp);
2022-05-24 18:21:12 -04:00
2022-06-06 19:57:17 -04:00
//================================== Ring item body
if ( fread(rib_size, sizeof(rib_size), 1, inFile) != 1 ) {
2022-05-24 18:21:12 -04:00
endOfFile = true;
return -1;
}
2022-06-06 19:57:17 -04:00
inFilePos = ftell(inFile);
///printf("%ld - Size of Ring Item Body : %d \n", inFilePos, rib_size[0]);
readRingItemByte = 4;
}
if( isNSCL){
if(rib_size[0] > 48 && readRingItemByte < rib_size[0] ){
fseek(inFile, 14*4, SEEK_CUR);
inFilePos = ftell(inFile);
readRingItemByte += 14 * 4;
}else{
return -2;
}
2022-05-24 18:21:12 -04:00
}
2022-06-06 19:57:17 -04:00
unsigned int header[4]; ///read 4 header, unsigned int = 4 byte = 32 bits.
if ( fread(header, sizeof(header), 1, inFile) != 1 ) {
endOfFile = true;
return -1;
}
readRingItemByte += sizeof(header);
blockID ++;
2022-05-24 18:21:12 -04:00
if( opt == 0 || opt == 2){
/// see the Pixie-16 user manual, Table4-2
data->eventID = blockID;
data->ch = header[0] & 0xF ;
data->slot = (header[0] >> 4) & 0xF;
data->crate = (header[0] >> 8) & 0xF;
data->headerLength = (header[0] >> 12) & 0x1F;
data->eventLength = (header[0] >> 17) & 0x3FFF;
data->pileup = header[0] >> 31 ;
data->time = ((ULong64_t)(header[2] & 0xFFFF) << 32) + header[1];
data->cfd_forced = header[2] >> 16 & 0x8000;
data->cfd_source = header[2] >> 16 & 0x4000;
data->cfd = header[2] >> 16 & 0x3FFF; // 0x3FFF for 250MHz , 0x7FFF for 100MHz
data->energy = (header[3] & 0xFFFF );
data->trace_length = (header[3] >> 16) & 0x7FFF;
data->trace_out_of_range = header[3] >> 31;
data->ClearQDC();
data->ClearTrace();
///======== read QDCsum
if( data->headerLength >= 4 ){
2022-06-06 19:57:17 -04:00
fread(extraHeader, sizeof(unsigned int) * (data->headerLength-4), 1, inFile);
readRingItemByte += (data->headerLength-4)*4;
2022-05-24 18:21:12 -04:00
if( data->headerLength == 8 || data->headerLength == 16){
data->trailing = extraHeader[0];
data->leading = extraHeader[1];
data->gap = extraHeader[2];
data->baseline = extraHeader[3];
}
if( data->headerLength == 12 || data->headerLength == 16){
for( int i = 0; i < 8; i++){
int startID = 0;
if( data->headerLength > 12) startID = 4; ///the 1st 4 words
data->QDCsum[i] = extraHeader[i+startID];
}
}
}else{
for( int i = 0 ; i < 8; i++){ data->QDCsum[i] = 0;}
data->trailing = 0;
data->leading = 0;
data->gap = 0;
data->baseline = 0;
}
///====== read trace
if( data->eventLength > data->headerLength ){
2022-06-06 19:57:17 -04:00
fread(traceBlock, sizeof(unsigned int) * ( data->trace_length / 2 ), 1, inFile);
readRingItemByte += data->trace_length / 2 * 4;
2022-05-24 18:21:12 -04:00
for( int i = 0; i < data->trace_length/2 ; i++){
data->trace[2*i+0] = traceBlock[i] & 0xFFFF ;
data->trace[2*i+1] = (traceBlock[i] >> 16 ) & 0xFFFF ;
}
///make QDC by trace
/**
if( data->headerLength == 4 || data->headerLength == 8 ) {
for( int i = 0; i < 8; i++){ data->QDCsum[i] = 0;}
for( int i = 0; i < data->trace_length; i++){
if( 0 <= i && i < 31 ) data->QDCsum[0] += data->trace[i];
if( 31 <= i && i < 60 ) data->QDCsum[1] += data->trace[i];
if( 60 <= i && i < 75 ) data->QDCsum[2] += data->trace[i];
if( 75 <= i && i < 95 ) data->QDCsum[3] += data->trace[i];
if( 95 <= i && i < 105 ) data->QDCsum[4] += data->trace[i];
if( 105 <= i && i < 160 ) data->QDCsum[5] += data->trace[i];
if( 160 <= i && i < 175 ) data->QDCsum[6] += data->trace[i];
if( 175 <= i && i < 200 ) data->QDCsum[7] += data->trace[i];
}
}*/
}
}
2022-06-06 19:57:17 -04:00
2022-05-24 18:21:12 -04:00
if( opt == 1 ){
2022-06-06 19:57:17 -04:00
2022-05-24 18:21:12 -04:00
data->headerLength = (header[0] >> 12) & 0x1F;
data->eventLength = (header[0] >> 17) & 0x3FFF;
data->trace_length = (header[3] >> 16) & 0x7FFF;
if( data->headerLength >= 4 ){
2022-06-06 19:57:17 -04:00
fread(extraHeader, sizeof(unsigned int) * (data->headerLength-4), 1, inFile);
2022-05-24 18:21:12 -04:00
}
if( data->eventLength > data->headerLength ){
2022-06-06 19:57:17 -04:00
fread(traceBlock, sizeof(unsigned int) * ( data->trace_length / 2 ), 1, inFile);
2022-05-24 18:21:12 -04:00
}
}
2022-06-06 19:57:17 -04:00
inFilePos = ftell(inFile);
2022-05-24 18:21:12 -04:00
2022-06-06 19:57:17 -04:00
if( opt == 2) data->Print();
2022-05-24 18:21:12 -04:00
return 1;
}
void evtReader::ScanNumberOfBlock(){
nBlock = 0;
int count = 0;
while( ReadBlock(1) != -1 ){
nBlock ++;
int haha = (inFilePos*10/inFileSize)%10;
if( haha == count ) {
inFilePosPrecent[count] = inFilePos;
blockIDPrecent[count] = blockID;
count++;
}
PrintStatus(10000);
}
printf("\n\n\n");
printf("scan complete: number of data Block : %ld\n", nBlock);
inFilePos = 0;
blockID = -1;
rewind(inFile); ///back to the File begining
endOfFile = false;
}
void evtReader::JumptoPrecent(int precent){
if( precent < 0 || precent > 10 ) {
printf("input precent should be 0 to 10\n");
return;
}
fseek(inFile, inFilePosPrecent[precent], SEEK_SET);
blockID = blockIDPrecent[precent];
}
void evtReader::PrintStatus(int mod){
///==== event stats, print status every 10000 events
if ( blockID % mod == 0 ) {
UpdateFileSize();
gClock.Stop("timer");
double time = gClock.GetRealTime("timer");
gClock.Start("timer");
printf("Total measurements: \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",
blockID, inFilePos/(1024.*1024.*1024.), inFileSize/1024./1024./1024, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
}
}
#endif