File indexing completed on 2025-01-18 09:11:49
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 <cstdint>
0016 #include <cstdio>
0017 #include <filesystem>
0018 #include <map>
0019 #include <regex>
0020 #include <sstream>
0021 #include <stdexcept>
0022 #include <type_traits>
0023 #include <vector>
0024
0025 std::string ActsExamples::ensureWritableDirectory(const std::string& dir) {
0026 using std::filesystem::current_path;
0027 using std::filesystem::path;
0028
0029 auto dir_path = dir.empty() ? current_path() : path(dir);
0030 if (exists(dir_path) && !is_directory(dir_path)) {
0031 throw std::runtime_error("'" + dir +
0032 "' already exists but is not a directory");
0033 }
0034 create_directories(dir_path);
0035 return canonical(dir_path).native();
0036 }
0037
0038 std::string ActsExamples::joinPaths(const std::string& dir,
0039 const std::string& name) {
0040 if (dir.empty()) {
0041 return name;
0042 } else {
0043 return dir + '/' + name;
0044 }
0045 }
0046
0047 std::string ActsExamples::perEventFilepath(const std::string& dir,
0048 const std::string& name,
0049 std::size_t event) {
0050 char prefix[64];
0051
0052 snprintf(prefix, sizeof(prefix), "event%09zu-", event);
0053
0054 if (dir.empty()) {
0055 return prefix + name;
0056 } else {
0057 return dir + '/' + prefix + name;
0058 }
0059 }
0060
0061 std::pair<std::size_t, std::size_t> ActsExamples::determineEventFilesRange(
0062 const std::string& dir, const std::string& name) {
0063 using std::filesystem::current_path;
0064 using std::filesystem::directory_iterator;
0065 using std::filesystem::path;
0066
0067 ACTS_LOCAL_LOGGER(
0068 Acts::getDefaultLogger("EventFilesRange", Acts::Logging::INFO));
0069
0070
0071 auto dir_path = dir.empty() ? current_path() : path(dir);
0072 if (!exists(dir_path)) {
0073 throw std::runtime_error("'" + dir_path.native() + "' does not exists");
0074 }
0075 if (!is_directory(dir_path)) {
0076 throw std::runtime_error("'" + dir_path.native() + "' is not a directory");
0077 }
0078
0079
0080 std::size_t eventMin = std::numeric_limits<std::size_t>::max();
0081 std::size_t eventMax = 0;
0082
0083
0084 std::string filename;
0085 std::regex re("^event([0-9]+)-" + name + "$");
0086 std::cmatch match;
0087
0088 for (const auto& f : directory_iterator(dir_path)) {
0089 if ((!exists(f.status())) || (!is_regular_file(f.status()))) {
0090 continue;
0091 }
0092
0093 filename = f.path().filename().native();
0094 if (std::regex_match(filename.c_str(), match, re)) {
0095 ACTS_VERBOSE("Matching file " << filename);
0096
0097
0098 std::size_t event = 0;
0099 auto ret = std::from_chars(match[1].first, match[1].second, event);
0100 if (ret.ptr == match[1].first) {
0101 throw std::runtime_error(
0102 "Could not extract event number from filename");
0103 }
0104
0105 eventMin = std::min(eventMin, event);
0106 eventMax = std::max(eventMax, event);
0107 }
0108 }
0109 ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
0110
0111
0112 if (eventMax < eventMin) {
0113 return {0u, 0u};
0114 }
0115 return {eventMin, eventMax + 1};
0116 }