File indexing completed on 2026-06-05 07:48:56
0001
0002
0003
0004
0005
0006
0007
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
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
0075 std::size_t eventMin = std::numeric_limits<std::size_t>::max();
0076 std::size_t eventMax = 0;
0077
0078
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
0088 filename = f.path().filename().native();
0089 if (std::regex_match(filename.c_str(), match, re)) {
0090 ACTS_VERBOSE("Matching file " << filename);
0091
0092
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
0100 eventMin = std::min(eventMin, event);
0101 eventMax = std::max(eventMax, event);
0102 }
0103 }
0104 ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
0105
0106
0107 if (eventMax < eventMin) {
0108 return {0u, 0u};
0109 }
0110 return {eventMin, eventMax + 1};
0111 }