Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-05 07:48:56

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Utilities/Paths.hpp"
0010 
0011 #include "Acts/Utilities/Logger.hpp"
0012 
0013 #include <algorithm>
0014 #include <charconv>
0015 #include <cstdio>
0016 #include <filesystem>
0017 #include <regex>
0018 #include <stdexcept>
0019 
0020 std::string ActsExamples::ensureWritableDirectory(const std::string& dir) {
0021   using std::filesystem::current_path;
0022   using std::filesystem::path;
0023 
0024   auto dir_path = dir.empty() ? current_path() : path(dir);
0025   if (exists(dir_path) && !is_directory(dir_path)) {
0026     throw std::runtime_error("'" + dir +
0027                              "' already exists but is not a directory");
0028   }
0029   create_directories(dir_path);
0030   return canonical(dir_path).native();
0031 }
0032 
0033 std::string ActsExamples::joinPaths(const std::string& dir,
0034                                     const std::string& name) {
0035   if (dir.empty()) {
0036     return name;
0037   } else {
0038     return dir + '/' + name;
0039   }
0040 }
0041 
0042 std::string ActsExamples::perEventFilepath(const std::string& dir,
0043                                            const std::string& name,
0044                                            std::size_t event) {
0045   char prefix[64];
0046 
0047   snprintf(prefix, sizeof(prefix), "event%09zu-", event);
0048 
0049   if (dir.empty()) {
0050     return prefix + name;
0051   } else {
0052     return dir + '/' + prefix + name;
0053   }
0054 }
0055 
0056 std::pair<std::size_t, std::size_t> ActsExamples::determineEventFilesRange(
0057     const std::string& dir, const std::string& name) {
0058   using std::filesystem::current_path;
0059   using std::filesystem::directory_iterator;
0060   using std::filesystem::path;
0061 
0062   ACTS_LOCAL_LOGGER(
0063       Acts::getDefaultLogger("EventFilesRange", Acts::Logging::INFO));
0064 
0065   // ensure directory path is valid
0066   auto dir_path = dir.empty() ? current_path() : path(dir);
0067   if (!exists(dir_path)) {
0068     throw std::runtime_error("'" + dir_path.native() + "' does not exist");
0069   }
0070   if (!is_directory(dir_path)) {
0071     throw std::runtime_error("'" + dir_path.native() + "' is not a directory");
0072   }
0073 
0074   // invalid default range that allows simple restriction later on
0075   std::size_t eventMin = std::numeric_limits<std::size_t>::max();
0076   std::size_t eventMax = 0;
0077 
0078   // filter matching event files from the directory listing
0079   std::string filename;
0080   std::regex re("^event([0-9]+)-" + name + "$");
0081   std::cmatch match;
0082 
0083   for (const auto& f : directory_iterator(dir_path)) {
0084     if ((!exists(f.status())) || (!is_regular_file(f.status()))) {
0085       continue;
0086     }
0087     // keep a copy so the match can refer to the underlying const char*
0088     filename = f.path().filename().native();
0089     if (std::regex_match(filename.c_str(), match, re)) {
0090       ACTS_VERBOSE("Matching file " << filename);
0091 
0092       // first sub_match is the whole string, second should be the event number
0093       std::size_t event = 0;
0094       auto ret = std::from_chars(match[1].first, match[1].second, event);
0095       if (ret.ptr == match[1].first) {
0096         throw std::runtime_error(
0097             "Could not extract event number from filename");
0098       }
0099       // enlarge available event range
0100       eventMin = std::min(eventMin, event);
0101       eventMax = std::max(eventMax, event);
0102     }
0103   }
0104   ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
0105 
0106   // should only occur if no files matched and the initial values persisted.
0107   if (eventMax < eventMin) {
0108     return {0u, 0u};
0109   }
0110   return {eventMin, eventMax + 1};
0111 }