added time offset, assume the digiitizer serial number is between first and 2nd underscore

This commit is contained in:
Ryan@MBA2024 2024-09-14 18:21:24 -05:00
parent a52db652dd
commit 0f487c354b
3 changed files with 98 additions and 15 deletions

View File

@ -11,16 +11,73 @@ inline unsigned int getTime_us(){
return time_us;
}
#include "TFile.h"
#include "TTree.h"
#include "TMacro.h"
#define MAX_TRACE_LENGTH 2000
#define MAX_MULTI 100
#define NMINARG 4
#define NMINARG 5
#define debug 0 // for > 1 number of hit to debug
#include <sstream>
#include <string>
#include <vector>
#include <utility>
std::vector<std::pair<int, int64_t>> readFileAndExtractData(const std::string& filename) {
std::vector<std::pair<int, int64_t>> data;
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening file!" << std::endl;
return data; // Return empty vector on failure
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
int first;
int64_t second;
// Extract an int and an int64_t from the line
if (iss >> first >> second) {
data.emplace_back(first, second);
} else {
std::cerr << "Error parsing line: " << line << std::endl;
}
}
file.close();
return data;
}
int extractDigiSN(const std::string& str) {
std::size_t firstUnderscore = str.find('_');
if (firstUnderscore == std::string::npos) {
return -1; // No first underscore found, return an error value
}
std::size_t secondUnderscore = str.find('_', firstUnderscore + 1);
if (secondUnderscore == std::string::npos) {
return -1; // No second underscore found, return an error value
}
// Extract the substring between the first and second underscores
std::string extracted = str.substr(firstUnderscore + 1, secondUnderscore - firstUnderscore - 1);
// Convert the extracted string to an integer using std::stoi
try {
return std::stoi(extracted);
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid number format: " << extracted << std::endl;
return -1; // Return an error value on conversion failure
} catch (const std::out_of_range& e) {
std::cerr << "Number out of range: " << extracted << std::endl;
return -1; // Return an error value if the number is too large
}
}
//^#############################################################
//^#############################################################
int main(int argc, char **argv) {
@ -30,12 +87,19 @@ int main(int argc, char **argv) {
printf("=========================================\n");
if (argc < NMINARG) {
printf("Incorrect number of arguments:\n");
printf("%s [timeWindow] [withTrace] [inFile1] [inFile2] .... \n", argv[0]);
printf("%s [timeWindow] [withTrace] [time Offset File] [inFile1] [inFile2] .... \n", argv[0]);
printf(" timeWindow : in ns, -1 = no event building \n");
printf(" withTrace : 0 for no trace, 1 for trace \n");
printf(" time Offset File : 0 for nothing \n");
printf("\n");
printf(" Example: %s -1 0 '\\ls -1 *001*.bin' (no event build, no trace, no verbose)\n", argv[0]);
printf(" %s 100 0 '\\ls -1 *001*.bin' (event build with 100 ns, no trace, no verbose)\n", argv[0]);
printf("\n");
printf(" Time offset file format :\n");
printf("[digi ID] [time offset(int64_t)]\n");
printf("\n");
printf(" file name should be this format CH0@V1725_324_Data_run_196.bin\n");
printf(" so that the serial number is between the 1st and 2nd underscore\n");
printf("\n\n");
return 1;
@ -46,6 +110,7 @@ int main(int argc, char **argv) {
///============= read input
long timeWindow = atoi(argv[1]);
bool traceOn = atoi(argv[2]);
std::string timeOffsetFile = argv[3];
const int nFile = argc - NMINARG + 1;
TString inFileName[nFile];
for( int i = 0 ; i < nFile ; i++){ inFileName[i] = argv[i + NMINARG -1];}
@ -72,12 +137,21 @@ int main(int argc, char **argv) {
}
}
std::vector<std::pair<int, int64_t>> timeOffsetList ;
printf("-------> Out file name : %s \n", outFileName.Data());
printf("=========================================\n");
printf(" Time Window = %ld ns = %.1f us\n", timeWindow, timeWindow/1000.);
printf(" Include Trace = %s\n", traceOn ? "Yes" : "No");
printf(" Max multiplity = %d hits/event (hard coded)\n", MAX_MULTI);
if( traceOn ) printf(" Max Trace Length = %d (hard coded)\n", MAX_TRACE_LENGTH);
if( timeOffsetFile != "0" ) {
printf(" Time Offset File = %s\n", timeOffsetFile.c_str());
timeOffsetList = readFileAndExtractData(timeOffsetFile);
for (size_t i = 0 ; timeOffsetList.size(); i++) {
printf("Digi-%4d %16llu ps\n", timeOffsetList[i].first, timeOffsetList[i].second);
}
}
printf("========================================= Number of Files : %d \n", nFile);
unsigned long long int totalHitCount = 0;
@ -86,6 +160,10 @@ int main(int argc, char **argv) {
for(int i = 0; i < nFile; i++ ){
printf(">>>>>>>> %2d | %s \n", i, inFileName[i].Data());
reader[i].OpenFile(inFileName[i]);
for (size_t i = 0 ; timeOffsetList.size(); i++) {
int sn = extractDigiSN(inFileName[i].Data());
if( sn == timeOffsetList[i].first ) reader[i].SetTimeOffset(timeOffsetList[i].second);
}
reader[i].ScanNumHit();
totalHitCount += reader[i].GetNumHit();
}

View File

@ -45,9 +45,9 @@ struct Data{
printf("header : 0x%X \n", Header);
printf(" Board : %u , Channel : %u\n", BoardID, Channel);
if( Header & 4 ) {
printf("Energy : %5u, Energy2 : %5u, TimeStamp : %16lu ps\n", Energy, Energy_short, TimeStamp);
printf("Energy : %5u, Energy2 : %5u, TimeStamp : %16llu ps\n", Energy, Energy_short, TimeStamp);
}else{
printf("Energy : %5u, TimeStamp : %16lu ps\n", Energy, TimeStamp);
printf("Energy : %5u, TimeStamp : %16llu ps\n", Energy, TimeStamp);
}
printf(" Flag : 0x%08X\n", Flags);
if( (Header & 0x8 ) >= 1 ){ /// is waevform exist
@ -74,6 +74,7 @@ class BinReader{
bool isOpened;
Long64_t hitID;
long int numHit;
int64_t timeOffset;
TBenchmark gClock;
@ -91,7 +92,7 @@ class BinReader{
BinReader(TString inFileName);
~BinReader();
void OpenFile(TString inFileName);
void OpenFile(TString inFileName, int64_t timeOffset = 0);
void CloseFile();
void UpdateFileSize();
bool IsEndOfFile();
@ -108,6 +109,8 @@ class BinReader{
void JumptoPrecent(int precent); ///this is offset by 1 block
void PrintStatus(int mod);
void SetTimeOffset(int64_t timeOffset) { this->timeOffset = timeOffset; }
};
//========================== implementation
@ -147,8 +150,9 @@ BinReader::BinReader(TString inFileName){
OpenFile(inFileName);
}
void BinReader::OpenFile(TString inFileName){
void BinReader::OpenFile(TString inFileName, int64_t timeOffset){
inFile = fopen(inFileName, "r");
this->timeOffset = timeOffset;
if( inFile == NULL ){
printf("Cannot read file : %s \n", inFileName.Data());
}else{
@ -233,6 +237,7 @@ int BinReader::ReadBlock(int skipTrace){
FillData(&data.BoardID);
FillData(&data.Channel);
FillData(&data.TimeStamp);
if( timeOffset != 0 ) data.TimeStamp += timeOffset;
if( (data.Header & 0x3) != 0 ) FillData(&data.Energy);

View File

@ -21,4 +21,4 @@ clean :
Bin2Root : Bin2Root.cpp BinReader.h
@echo "--------- making Bin2Root"
$(CC) $(COPTS) -o Bin2Root Bin2Root.cpp BinReader.h $(ROOTLIBS)
$(CC) $(COPTS) -o Bin2Root Bin2Root.cpp $(ROOTLIBS)