XIAEventBuilder/armory/evtReader.h

488 lines
13 KiB
C
Raw Normal View History

2022-01-13 18:23:41 -05:00
#ifndef EVTREADER_H
#define EVTREADER_H
#include <stdio.h> /// for FILE
2022-01-13 18:23:41 -05:00
#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 timePos{
public:
timePos(ULong64_t time, unsigned long int inFilePos){
this->time = time;
this->inFilePos = inFilePos;
}
ULong64_t time;
unsigned long int inFilePos;
void Print(){
printf("time: %16llu, filePos: %lu\n", time, inFilePos);
}
};
2022-01-13 18:23:41 -05:00
class evtReader{
2022-02-14 16:06:34 -05:00
public:
DataBlock * data;
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
private:
FILE * inFile;
2022-02-14 16:14:26 -05:00
long int inFileSize;
long int inFilePos0;
2022-02-14 16:14:26 -05:00
long int inFilePos;
bool endOfFile;
bool isOpened;
Long64_t blockID;
long int nBlock;
2022-02-14 16:06:34 -05:00
unsigned int extraHeader[14];
unsigned int traceBlock[MAX_TRACE_LENGHT/2];
2022-02-14 16:06:34 -05:00
TBenchmark gClock;
long int inFilePosPrecent[10];
Long64_t blockIDPrecent[10];
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
///============================================ Methods
public:
2022-02-14 16:06:34 -05:00
evtReader();
evtReader(TString inFileName);
~evtReader();
2022-02-14 16:06:34 -05:00
void OpenFile(TString inFileName);
2022-03-10 13:59:06 -05:00
void CloseFile();
2022-02-14 16:06:34 -05:00
void UpdateFileSize();
2022-02-14 16:14:26 -05:00
bool IsEndOfFile();
2022-02-14 16:06:34 -05:00
2022-02-14 16:14:26 -05:00
bool IsOpen() {return isOpened;}
long int GetFilePos() {return inFilePos;}
long int GetFileSize() {return inFileSize;}
Long64_t GetBlockID() {return blockID;}
Long64_t GetNumberOfBlock() {return nBlock;}
2022-02-14 16:06:34 -05:00
2022-02-14 16:06:34 -05:00
int ReadBlock(int opt = 0); /// 0 = default, fill data
/// 1 = no fill data
int ReadBlockAtPos(unsigned long int filePos);
2022-02-14 16:06:34 -05:00
void ScanNumberOfBlock();
void JumptoPrecent(int precent); ///this is offset by 1 block
2022-02-14 16:06:34 -05:00
void PrintStatus(int mod);
std::vector<timePos> timePosList;
unsigned long int filePosK;
std::vector<timePos> ReadBatchPos(long batchSize, bool verbose = false);
void SortTimePos();
2022-02-14 16:06:34 -05:00
};
//========================== implementation
evtReader::evtReader(){
2022-02-14 16:14:26 -05:00
inFile = 0;
data = new DataBlock();
2022-02-14 16:06:34 -05:00
inFileSize = 0;
2022-02-14 16:14:26 -05:00
inFilePos = 0;
inFilePos0 = 0;
filePosK = 0;
2022-02-14 16:14:26 -05:00
nBlock = 0;
blockID = -1;
endOfFile = false;
isOpened = false;
2022-02-14 16:06:34 -05:00
}
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
evtReader::~evtReader(){
fclose(inFile); //already delete inFile
2022-02-14 16:06:34 -05:00
delete data;
}
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
evtReader::evtReader(TString inFileName){
2022-02-14 16:14:26 -05:00
inFile = 0;
data = new DataBlock();
2022-02-14 16:06:34 -05:00
inFileSize = 0;
2022-02-14 16:14:26 -05:00
inFilePos = 0;
inFilePos0 = 0;
filePosK = 0;
2022-02-14 16:06:34 -05:00
2022-02-14 16:14:26 -05:00
nBlock = 0;
blockID = -1;
endOfFile = false;
isOpened = false;
2022-02-14 16:06:34 -05:00
OpenFile(inFileName);
}
void evtReader::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;
2022-01-13 18:23:41 -05:00
}
2022-02-14 16:06:34 -05:00
};
2022-01-13 18:23:41 -05:00
2022-03-10 13:59:06 -05:00
void evtReader::CloseFile(){
fclose(inFile);
isOpened = false;
data->Clear();
inFileSize = 0;
inFilePos = 0;
nBlock = 0;
blockID = -1;
endOfFile = false;
};
2022-02-14 16:06:34 -05:00
void evtReader::UpdateFileSize(){
if( inFile == NULL ) return;
fseek(inFile, 0L, SEEK_END);
inFileSize = ftell(inFile);
fseek(inFile, inFilePos, SEEK_SET);
}
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
bool evtReader::IsEndOfFile() {
int haha = feof(inFile);
return haha > 0 ? true: false;
}
2022-01-13 18:23:41 -05:00
int evtReader::ReadBlockAtPos(unsigned long int filePos){
fseek( inFile, filePos, SEEK_SET);
return ReadBlock(0);
}
2022-01-13 18:23:41 -05:00
int evtReader::ReadBlock(int opt){
2022-02-14 16:06:34 -05:00
if( feof(inFile) ) return -1;
if( endOfFile ) return -1;
unsigned int header[4]; ///read 4 header, unsigned int = 4 byte = 32 bits.
inFilePos0 = inFilePos;
2022-02-14 16:06:34 -05:00
if ( fread(header, sizeof(header), 1, inFile) != 1 ) {
endOfFile = true;
return -1;
}
blockID ++;
if( opt == 0 ){
2022-01-13 18:23:41 -05:00
/// 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 = header[2] >> 16 ;
2022-01-21 15:57:40 -05:00
data->energy = (header[3] & 0xFFFF );
2022-01-13 18:23:41 -05:00
data->trace_length = (header[3] >> 16) & 0x7FFF;
data->trace_out_of_range = header[3] >> 31;
2022-01-18 14:23:47 -05:00
data->ClearQDC();
data->ClearTrace();
2022-01-18 14:23:47 -05:00
2022-01-13 18:23:41 -05:00
///======== read QDCsum
if( data->headerLength >= 4 ){
2022-02-14 16:06:34 -05:00
fread(extraHeader, sizeof(unsigned int) * (data->headerLength-4), 1, inFile);
2022-01-13 18:23:41 -05: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];
}
}
2022-01-18 14:23:47 -05:00
}else{
for( int i = 0 ; i < 8; i++){ data->QDCsum[i] = 0;}
data->trailing = 0;
data->leading = 0;
data->gap = 0;
data->baseline = 0;
2022-01-13 18:23:41 -05:00
}
///====== read trace
if( data->eventLength > data->headerLength ){
2022-02-14 16:06:34 -05:00
fread(traceBlock, sizeof(unsigned int) * ( data->trace_length / 2 ), 1, inFile);
2022-01-13 18:23:41 -05:00
for( int i = 0; i < data->trace_length/2 ; i++){
data->trace[2*i+0] = traceBlock[i] & 0xFFFF ;
2022-01-13 18:23:41 -05:00
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;}
2022-01-13 18:23:41 -05:00
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-01-13 18:23:41 -05:00
}
2022-02-14 16:06:34 -05:00
}
if( opt == 1 ){
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
data->headerLength = (header[0] >> 12) & 0x1F;
data->eventLength = (header[0] >> 17) & 0x3FFF;
data->trace_length = (header[3] >> 16) & 0x7FFF;
if( data->headerLength >= 4 ){
fread(extraHeader, sizeof(unsigned int) * (data->headerLength-4), 1, inFile);
}
if( data->eventLength > data->headerLength ){
fread(traceBlock, sizeof(unsigned int) * ( data->trace_length / 2 ), 1, inFile);
}
2022-01-13 18:23:41 -05:00
}
2022-02-14 16:06:34 -05:00
inFilePos = ftell(inFile);
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
return 1;
}
2022-01-13 18:23:41 -05:00
2022-02-14 16:06:34 -05:00
void evtReader::ScanNumberOfBlock(){
nBlock = 0;
int count = 0;
2022-02-14 16:06:34 -05:00
while( ReadBlock(1) != -1 ){
nBlock ++;
int haha = (inFilePos*10/inFileSize)%10;
if( haha == count ) {
inFilePosPrecent[count] = inFilePos;
blockIDPrecent[count] = blockID;
count++;
}
2022-02-14 16:06:34 -05:00
PrintStatus(10000);
2022-01-13 18:23:41 -05:00
}
2022-02-14 16:06:34 -05:00
printf("\n\n\n");
printf("scan complete: number of data Block : %ld\n", nBlock);
inFilePos = 0;
inFilePos0 = 0;
filePosK = 0;
2022-02-14 16:06:34 -05:00
blockID = -1;
rewind(inFile); ///back to the File begining
endOfFile = false;
}
2022-01-13 18:23:41 -05:00
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];
}
2022-02-14 16:06:34 -05:00
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.);
}
}
2022-01-13 18:23:41 -05:00
void evtReader::SortTimePos(){
std::sort(timePosList.begin(), timePosList.end(), [](const timePos & a, const timePos & b) { return a.time < b.time; });
}
std::vector<timePos> evtReader::ReadBatchPos(long batchSize, bool verbose){
if( verbose ) printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& %s / %lu / %lu\n", __func__, filePosK, inFileSize );
fseek( inFile, filePosK, SEEK_SET);
std::vector<timePos> timePosList_A;
if( filePosK == inFileSize ){
timePosList_A = timePosList;
timePosList.clear();
return timePosList_A;
}
if( timePosList.size() == 0 ){
int res = 0;
do{
res = ReadBlock();
timePosList.push_back( timePos(data->time, inFilePos0));
}while( timePosList.size() < batchSize && res == 1);
SortTimePos();
unsigned long long t0_B = timePosList.front().time;
unsigned long long t1_B = timePosList.back().time;
if( verbose ) {
printf(" hit in memeory : %7zu | %lu | %lu \n", timePosList.size(), inFilePos, inFileSize);
printf("t0 : %15llu\n", t0_B);
printf("t1 : %15llu\n", t1_B);
}
timePosList_A = timePosList;
timePosList.clear();
}else{
timePosList_A = timePosList;
timePosList.clear();
}
if( feof(inFile) ) return timePosList_A;
int res = 0;
do{
res = ReadBlock();
timePosList.push_back(timePos(data->time, inFilePos0));
}while( timePosList.size() < batchSize && res == 1);
SortTimePos();
unsigned long long t0_B = timePosList.front().time;
unsigned long long t1_B = timePosList.back().time;
if( verbose ) {
printf(" hit in memeory : %7zu | %lu | %lu \n", timePosList.size(), inFilePos, inFileSize);
printf("t0 : %15llu\n", t0_B);
printf("t1 : %15llu\n", t1_B);
}
unsigned long long t0_A = timePosList_A.front().time;
unsigned long long t1_A = timePosList_A.back().time;
ulong ID_A = 0;
ulong ID_B = 0;
if( t0_A >= t0_B) {
printf("\033[0;31m!!!!!!!!!!!!!!!!! %s | Need to increase the batch size. \033[0m\n", __func__);
printf("present batch size : %lu \n", batchSize);
printf("timePosList_A t0_A : %15llu\n", t0_A);
printf("timePosList t0_B : %15llu\n", t0_B);
return std::vector<timePos> ();
}
if( t1_A > t0_B) { // need to sort between two hitList
if( verbose ) {
printf("############# need to sort \n");
printf("=========== sume of A + B : %zu \n", timePosList_A.size() + timePosList.size());
}
std::vector<timePos> timePosListTemp;
// find the hit that is >= t0_B, save them to timePosListTemp
for( size_t j = 0; j < timePosList_A.size() ; j++){
if( timePosList_A[j].time < t0_B ) continue;;
if( ID_A == 0 ) ID_A = j;
timePosListTemp.push_back(timePosList_A[j]);
}
// remove timePosList_A element that is >= t0_B
timePosList_A.erase(timePosList_A.begin() + ID_A, timePosList_A.end() );
// find the hit that is <= t1_A, save them to timePosListTemp
for( size_t j = 0; j < timePosList.size(); j++){
if( timePosList[j].time > t1_A ) {
break;
}
timePosListTemp.push_back(timePosList[j]);
ID_B = j + 1;
}
// remove hit elements that is <= t1_A
timePosList.erase(timePosList.begin(), timePosList.begin() + ID_B );
// sort timePosListTemp
std::sort(timePosListTemp.begin(), timePosListTemp.end(), [](const timePos& a, const timePos& b) {
return a.time < b.time;
});
if( verbose ) {
printf("----------------- ID_A : %lu, Drop\n", ID_A);
printf("----------------- ID_B : %lu, Drop\n", ID_B);
printf("=========== sume of A + B + Temp : %zu \n", timePosList_A.size() + timePosList.size() + timePosListTemp.size());
printf("----------------- refill timePosList_A \n");
}
for( size_t j = 0; j < timePosListTemp.size(); j++){
timePosList_A.push_back(timePosListTemp[j]);
}
timePosListTemp.clear();
if( verbose ) {
printf("=========== sume of A + B : %zu \n", timePosList_A.size() + timePosList.size());
printf(" A in memeory : %7zu \n", timePosList_A.size());
printf("t0 : %15llu\n", timePosList_A.front().time);
printf("t1 : %15llu\n", timePosList_A.back().time);
printf(" B in memeory : %7zu | %lu | %lu \n", timePosList.size(), inFilePos, inFileSize);
printf("t0 : %15llu\n", timePosList.front().time);
printf("t1 : %15llu\n", timePosList.back().time);
}
}
filePosK = inFilePos;
return timePosList_A;
}
2022-01-13 18:23:41 -05:00
#endif