fix EventBuilder, use earlist time among the last data to be the left-over reference

This commit is contained in:
splitPoleDAQ 2023-12-12 15:35:47 -05:00
parent 5e82065fc1
commit 72f5cd98e5
8 changed files with 214 additions and 34 deletions

2
.gitignore vendored
View File

@ -7,6 +7,7 @@ test_indep
programSettings.txt
EventKenshikushi
EventBuilder
EventBuilder_sortTime
DataGenerator
DataReaderScript
DataReader
@ -14,6 +15,7 @@ pid.dat
DAQLock.dat
DumpFSU2ROOT
SettingsExplorer
AggSeparator
data

98
Aux/AggSeparator.cpp Normal file
View File

@ -0,0 +1,98 @@
#ifndef AGGSEPARATOR_H
#define AGGSEPARATOR_H
#include <stdio.h>
#include <string>
#include <sstream>
#include <cmath>
#include <cstring> ///memset
#include <iostream> ///cout
#include <sstream>
#include <iomanip> // for setw
#include <algorithm>
#include <bitset>
#include <vector>
#include <sys/stat.h>
int main(int argc, char **argv) {
std::string inFileName = argv[1];
FILE * file = fopen(inFileName.c_str(), "r");
if( file == NULL ) {
printf("file : %s cannot be open. exit program.\n", inFileName.c_str());
return -1;
}
char * buffer = nullptr;
unsigned int word; // 4 bytes = 32 bits
bool newFileFlag[8] = {true};
while( !feof(file)){
size_t dummy = fread(&word, 4, 1, file);
if( dummy != 1 ){
printf("read file error. abort.\n");
break;
}
short header = ((word >> 28 ) & 0xF);
if( header != 0xA ) {
printf("header error. abort.\n");
break;
}
unsigned int aggSize = (word & 0x0FFFFFFF) * 4; ///byte
dummy = fread(&word, 4, 1, file);
unsigned int BoardID = ((word >> 27) & 0x1F);
unsigned short pattern = ((word >> 8 ) & 0x7FFF );
bool BoardFailFlag = ((word >> 26) & 0x1 );
unsigned int ChannelMask = ( word & 0xFF ) ;
dummy = fread(&word, 4, 1, file);
unsigned int bdAggCounter = word;
printf("Agg counter : %u\n", bdAggCounter);
dummy = fread(&word, 4, 1, file);
unsigned int aggTimeTag = word;
for( int chMask = 0; chMask < 8 ; chMask ++ ){ // the max numnber of Coupled/RegChannel is 8 for PHA, PSD, QDC
if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue;
printf("==================== Dual/Group Channel Block, ch Mask : 0x%X\n", chMask *2);
dummy = fread(&word, 4, 1, file);
unsigned int dualChannelBlockSize = ( word & 0x7FFFFFFF ) * 4 ;
printf("dual channel size : %d words\n", dualChannelBlockSize / 4);
buffer = new char[dualChannelBlockSize];
fseek(file, -4, SEEK_CUR);
dummy = fread(buffer, dualChannelBlockSize, 1, file);
FILE * haha = nullptr;
if( newFileFlag[chMask] ) {
haha = fopen( (inFileName + "." + std::to_string(chMask)).c_str(), "wb");
newFileFlag[chMask] = false;
}else{
haha = fopen( (inFileName + "." + std::to_string(chMask)).c_str(), "a+");
}
fwrite(buffer, dualChannelBlockSize, 1, haha);
fclose(haha);
}
};
fclose(file);
printf("======================= Duel channel seperated \n");
return 0;
}
#endif

View File

@ -9,7 +9,7 @@
#include "TTree.h"
#define MAX_MULTI 100
#define BUFFERFILL 0.5 // only 0.5 * MAXNData will be filled in memeory each time
#define BUFFERFILL 0.1 // only 0.5 * MAXNData will be filled in memeory each time
template<typename T> void swap(T * a, T *b );
int partition(int arr[], int kaka[], TString file[], int start, int end);
@ -279,12 +279,16 @@ int main(int argc, char **argv) {
fillFlag = false;
}
//unsigned long long t1 = data[i]->Timestamp[ch][iData];
//printf("digi:%d | ch: %2d DataIndex: %5d (%d) [%5d(%d)] | %llu\n", data[i]->boardSN, ch, iData, iLoop, lastDataIndex[i][ch], lastLoopIndex[i][ch], t1);
if( debug ){
unsigned long long t1 = data[i]->Timestamp[ch][iData];
printf("digi:%d | ch: %2d DataIndex: %5d (%d) [%5d(%d)] | %llu\n", data[i]->boardSN, ch, iData, iLoop, lastDataIndex[i][ch], lastLoopIndex[i][ch], t1);
}
}
//printf("%3d | agg : %d | %u | %s\n", snList[i], aggCount[i], data[i]->aggTime, fillFlag ? "cont. fill" : "break." );
//data[i]->PrintStat();
if( debug ){
printf("%3d | agg : %d | %u | %s\n", snList[i], aggCount[i], data[i]->aggTime, fillFlag ? "cont. fill" : "break." );
//data[i]->PrintStat();
}
}
}while(fillFlag);
@ -297,7 +301,7 @@ int main(int argc, char **argv) {
}
mb->BuildEvents(0, 0, debug);
//mb->PrintStat();
if( debug ) mb->PrintStat();
///----------- save to tree;
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
@ -350,7 +354,7 @@ int main(int argc, char **argv) {
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
//printf("startIndex : %ld -> %ld, %ld, %ld, %ld\n", startIndex, startIndex < 0 ? startIndex + MaxNEvent : startIndex, mb->eventIndex, mb->eventBuilt, mb->totalEventBuilt);
if( startIndex < 0 ) startIndex += MaxNEvent;
for( long p = startIndex; p <= startIndex + mb->eventBuilt; p++){
for( long p = startIndex; p < startIndex + mb->eventBuilt; p++){
int k = p % MaxNEvent;
multi = mb->events[k].size();
if( multi > MAX_MULTI) {

View File

@ -14,7 +14,7 @@ ROOTLIBS = `root-config --cflags --glibs`
OBJS = ClassDigitizer.o MultiBuilder.o
ALL = test test_indep DataGenerator EventBuilder DataReader DumpFSU2ROOT SettingsExplorer
ALL = test test_indep DataGenerator EventBuilder DataReader DumpFSU2ROOT SettingsExplorer
#########################################################################

View File

@ -24,6 +24,10 @@ class FSUReader{
unsigned long totNumBlock;
unsigned int blockID;
// for dual block
int DPPType;
int chMask;
std::vector<unsigned int> blockPos;
unsigned int word[1]; /// 4 byte
@ -53,6 +57,23 @@ inline FSUReader::FSUReader(std::string fileName, unsigned short numCh){
blockID = 0;
blockPos.clear();
//Get DPPType from file name;
DPPType = -1;
if( fileName.find("PHA") != std::string::npos ) DPPType = DPPType::DPP_PHA_CODE;
if( fileName.find("PSD") != std::string::npos ) DPPType = DPPType::DPP_PSD_CODE;
if( fileName.find("QDC") != std::string::npos ) DPPType = DPPType::DPP_QDC_CODE;
//check is the file is *.fsu or *.fsu.X
size_t found = fileName.find_last_of('.');
std::string ext = fileName.substr(found + 1);
if( ext.find("fsu") != std::string::npos ) {
printf("It is an raw data *.fsu format\n");
}else{
chMask = atoi(ext.c_str());
printf("It is a splitted dual block data *.fsu.X format, dual channel mask : %d \n", chMask);
}
//ScanNumBlock();
}
@ -68,30 +89,45 @@ inline int FSUReader::ReadNextBlock(bool fast, int verbose){
if( filePos >= inFileSize) return -1;
dummy = fread(word, 4, 1, inFile);
fseek(inFile, -4, SEEK_CUR);
if( dummy != 1) {
printf("fread error, should read 4 bytes, but read %ld x 4 byte, file pos: %ld byte\n", dummy, ftell(inFile));
return -10;
}
fseek(inFile, -4, SEEK_CUR);
short header = ((word[0] >> 28 ) & 0xF);
if( header != 0xA ) {
if( header == 0xA ) { ///normal header
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
buffer = new char[aggSize];
dummy = fread(buffer, aggSize, 1, inFile);
filePos = ftell(inFile);
if( dummy != 1) {
printf("fread error, should read %d bytes, but read %ld x %d byte, file pos: %ld byte \n", aggSize, dummy, aggSize, ftell(inFile));
return -30;
}
data->DecodeBuffer(buffer, aggSize, fast, verbose); // data will own the buffer
data->ClearBuffer(); // this will clear the buffer.
}else if( (header & 0xF ) == 0x8 ) { /// dual channel header
unsigned int dualSize = (word[0] & 0x7FFFFFFF) * 4; ///byte
buffer = new char[dualSize];
dummy = fread(buffer, dualSize, 1, inFile);
filePos = ftell(inFile);
data->buffer = buffer;
data->DecodeDualBlock(buffer, dualSize, DPPType, chMask, false, verbose);
data->ClearBuffer();
}else{
printf("incorrect header.\n trminate.");
return -20;
}
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
buffer = new char[aggSize];
dummy = fread(buffer, aggSize, 1, inFile);
filePos = ftell(inFile);
if( dummy != 1) {
printf("fread error, should read %d bytes, but read %ld x %d byte, file pos: %ld byte \n", aggSize, dummy, aggSize, ftell(inFile));
return -30;
}
data->DecodeBuffer(buffer, aggSize, fast, verbose); // data will own the buffer
data->ClearBuffer(); // this will clear the buffer.
return 0;
}
@ -109,7 +145,7 @@ inline int FSUReader::ReadBlock(unsigned int ID, int verbose){
fseek(inFile, blockPos[ID], SEEK_CUR);
filePos = blockPos[ID];
blockID = ID;
return ReadNextBlock(verbose);
return ReadNextBlock(false, verbose);
}

View File

@ -82,8 +82,9 @@ class Data{
void DecodeBuffer(bool fastDecode, int verbose = 0); /// fastDecode will not save waveform
void DecodeBuffer(char * &buffer, unsigned int size, bool fastDecode, int verbose = 0); // for outside data
void PrintStat(bool skipEmpty = true) const;
void DecodeDualBlock(char * &buffer, unsigned int size, int DPPType, int chMask, bool fastDecode, int verbose = 0); // for outside data
void PrintStat(bool skipEmpty = true) const;
void PrintAllData(bool tableMode = true, unsigned int maxRowDisplay = 0) const;
//^================= Saving data
@ -94,7 +95,7 @@ class Data{
unsigned int GetFileSize() const {return outFileSize;}
uint64_t GetTotalFileSize() const {return FinishedOutFilesSize + outFileSize;}
void ZeroTotalFileSize() { FinishedOutFilesSize = 0; }
protected:
const unsigned short numInputCh;
@ -122,7 +123,6 @@ class Data{
int DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose);
int DecodePSDDualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose);
int DecodeQDCGroupedChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose);
};
//==========================================
@ -424,6 +424,8 @@ inline void Data::DecodeBuffer(bool fastDecode, int verbose){
if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue;
if( verbose >= 2 ) printf("==================== Dual/Group Channel Block, ch Mask : 0x%X, nw : %d\n", chMask *2, nw);
nw = nw + 1;
if( DPPType == DPPType::DPP_PHA_CODE ) {
if ( DecodePHADualChannelBlock(chMask, fastDecode, verbose) < 0 ) break;
}
@ -531,11 +533,29 @@ inline void Data::DecodeBuffer(bool fastDecode, int verbose){
}
//*=================================================
inline void Data::DecodeDualBlock(char * &buffer, unsigned int size, int DPPType, int chMask, bool fastDecode, int verbose){
this->buffer = buffer;
this->nByte = size;
nw = 0;
if( DPPType == DPPType::DPP_PHA_CODE ) {
DecodePHADualChannelBlock(chMask, fastDecode, verbose) ;
}
if( DPPType == DPPType::DPP_PSD_CODE ) {
DecodePSDDualChannelBlock(chMask, fastDecode, verbose) ;
}
if( DPPType == DPPType::DPP_QDC_CODE ) {
DecodeQDCGroupedChannelBlock(chMask, fastDecode, verbose) ;
}
}
inline int Data::DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose){
//printf("======= %s\n", __func__);
nw = nw + 1;
//nw = nw + 1;
unsigned int word = ReadBuffer(nw, verbose);
bool hasFormatInfo = ((word >> 31) & 0x1);
@ -777,7 +797,7 @@ inline int Data::DecodePSDDualChannelBlock(unsigned int ChannelMask, bool fastDe
//printf("======= %s\n", __func__);
nw = nw + 1;
//nw = nw + 1;
unsigned int word = ReadBuffer(nw, verbose);
if( (word >> 31) != 1 ) return 0;
@ -986,7 +1006,7 @@ inline int Data::DecodePSDDualChannelBlock(unsigned int ChannelMask, bool fastDe
//*=================================================
inline int Data::DecodeQDCGroupedChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose){
nw = nw + 1;
//nw = nw + 1;
unsigned int word = ReadBuffer(nw, verbose);
if( (word >> 31) != 1 ) return 0;

View File

@ -156,6 +156,24 @@ void MultiBuilder::FindLatestTimeAndCh(bool verbose){
}
void MultiBuilder::FindEarlistTimeAmongLastData(bool verbose){
latestTime = -1;
latestCh = -1;
latestDigi = -1;
for( int i = 0; i < nData; i++){
for( unsigned ch = 0; ch < data[i]->GetNChannel(); ch++ ){
int index = data[i]->DataIndex[ch];
if( index == -1 ) continue;
if( data[i]->Timestamp[ch][index] < latestTime ) {
latestTime = data[i]->Timestamp[ch][index];
latestCh = ch;
latestDigi = i;
}
}
}
if( verbose ) printf("%s | bd : %d, ch : %d, %lld \n", __func__, latestDigi, latestCh, latestTime);
}
void MultiBuilder::FindLatestTimeOfData(bool verbose){
latestTime = 0;
latestCh = -1;
@ -176,7 +194,8 @@ void MultiBuilder::FindLatestTimeOfData(bool verbose){
void MultiBuilder::BuildEvents(bool isFinal, bool skipTrace, bool verbose){
FindLatestTimeOfData(verbose);
FindEarlistTimeAmongLastData(verbose);
//FindLatestTimeOfData(verbose);
FindEarlistTimeAndCh(verbose);
if( earlistCh == -1 || nExhaushedCh == nData * MaxNChannels) return; /// no data

View File

@ -45,7 +45,7 @@ public:
MultiBuilder(Data * singleData, int type, int sn);
~MultiBuilder();
void SetTimeWindow(unsigned short ticks) {timeWindow = ticks;}
void SetTimeWindow(unsigned short ticks) {timeWindow = ticks; leftOverTime = ticks;}
unsigned short GetTimeWindow() const{return timeWindow;}
void SetLeftOverTime(unsigned long long ticks) {leftOverTime = ticks;}
@ -85,15 +85,16 @@ private:
int nExhaushedCh;
bool chExhaused[MaxNDigitizer][MaxNChannels];
void FindEarlistTimeAndCh(bool verbose = false); // search thtough the nextIndex
void FindEarlistTimeAndCh(bool verbose = false); // search through the nextIndex
unsigned long long earlistTime;
int earlistDigi;
int earlistCh;
void FindLatestTimeAndCh(bool verbose = false); // search thtough the nextIndex
void FindLatestTimeAndCh(bool verbose = false); // search through the nextIndex
unsigned long long latestTime;
int latestDigi;
int latestCh;
void FindEarlistTimeAmongLastData(bool verbose = false);
void FindLatestTimeOfData(bool verbose = false);
int lastBackWardIndex[MaxNDigitizer][MaxNChannels];