From 0f487c354b39188c1ae96b14de86b65cab2b5758 Mon Sep 17 00:00:00 2001 From: "Ryan@MBA2024" Date: Sat, 14 Sep 2024 18:21:24 -0500 Subject: [PATCH] added time offset, assume the digiitizer serial number is between first and 2nd underscore --- Bin2Root.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++------ BinReader.h | 13 ++++--- Makefile | 2 +- 3 files changed, 98 insertions(+), 15 deletions(-) diff --git a/Bin2Root.cpp b/Bin2Root.cpp index e00e883..65bb485 100644 --- a/Bin2Root.cpp +++ b/Bin2Root.cpp @@ -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 +#include +#include +#include + +std::vector> readFileAndExtractData(const std::string& filename) { + std::vector> 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(" timeWindow : in ns, -1 = no event building \n"); - printf(" withTrace : 0 for no trace, 1 for trace \n"); + 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,20 +137,33 @@ int main(int argc, char **argv) { } } + std::vector> 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); + 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; BinReader reader[nFile]; for(int i = 0; i < nFile; i++ ){ - printf(">>>>>>>> %2d | %s \n", i, inFileName[i].Data()); + 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(); } diff --git a/BinReader.h b/BinReader.h index 086ca06..b81518b 100644 --- a/BinReader.h +++ b/BinReader.h @@ -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(); @@ -107,6 +108,8 @@ class BinReader{ void ScanNumHit(); 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); diff --git a/Makefile b/Makefile index 1aec35d..90834a6 100644 --- a/Makefile +++ b/Makefile @@ -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)