add timeOffset example file

This commit is contained in:
Ryan@MBA2024 2024-09-14 18:31:43 -05:00
parent 0f487c354b
commit 84851051f7
2 changed files with 43 additions and 25 deletions

View File

@ -52,30 +52,40 @@ std::vector<std::pair<int, int64_t>> readFileAndExtractData(const std::string& f
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
}
// Function to extract the number between the n-th and (n+1)-th underscore
int extractDigiSN(const std::string& str, int n = 1) {
std::size_t pos = 0;
std::size_t secondUnderscore = str.find('_', firstUnderscore + 1);
if (secondUnderscore == std::string::npos) {
return -1; // No second underscore found, return an error value
// Find the position of the n-th underscore
for (int i = 0; i < n; ++i) {
pos = str.find('_', pos);
if (pos == std::string::npos) {
std::cerr << "Less than " << n << " underscores in the string." << std::endl;
return -1; // Return error if there are fewer underscores than expected
}
++pos; // Move to the next character after the underscore
}
// 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
}
// Find the position of the (n+1)-th underscore
std::size_t nextUnderscore = str.find('_', pos);
if (nextUnderscore == std::string::npos) {
std::cerr << "No (n+1)-th underscore found." << std::endl;
return -1; // Return error if there's no (n+1)-th underscore
}
// Extract the substring between the n-th and (n+1)-th underscores
std::string extracted = str.substr(pos, nextUnderscore - pos);
// Convert the extracted substring 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
}
}
//^#############################################################
@ -96,7 +106,8 @@ int main(int argc, char **argv) {
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, sn appears between the n-th - (n+1)-th underscore of the fileName] 0\n");
printf("[digi sn] [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");
@ -138,6 +149,7 @@ int main(int argc, char **argv) {
}
std::vector<std::pair<int, int64_t>> timeOffsetList ;
timeOffsetList.clear();
printf("-------> Out file name : %s \n", outFileName.Data());
printf("=========================================\n");
@ -160,9 +172,11 @@ 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);
if( timeOffsetList.size() > 1 ){
for (size_t i = 1 ; timeOffsetList.size(); i++) {
int sn = extractDigiSN(inFileName[i].Data(), timeOffsetList[0].first);
if( sn == timeOffsetList[i].first ) reader[i].SetTimeOffset(timeOffsetList[i].second);
}
}
reader[i].ScanNumHit();
totalHitCount += reader[i].GetNumHit();

4
timeOffset.txt Normal file
View File

@ -0,0 +1,4 @@
1 0
123 456
86 49
19267 0