Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Paths.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Paths.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 #include <algorithm>
14 #include <charconv>
15 #include <cstdint>
16 #include <cstdio>
17 #include <filesystem>
18 #include <map>
19 #include <regex>
20 #include <sstream>
21 #include <stdexcept>
22 #include <type_traits>
23 #include <vector>
24 
26  using std::filesystem::current_path;
28 
29  auto dir_path = dir.empty() ? current_path() : path(dir);
30  if (exists(dir_path) and not is_directory(dir_path)) {
31  throw std::runtime_error("'" + dir +
32  "' already exists but is not a directory");
33  }
34  create_directories(dir_path);
35  return canonical(dir_path).native();
36 }
37 
39  const std::string& name) {
40  if (dir.empty()) {
41  return name;
42  } else {
43  return dir + '/' + name;
44  }
45 }
46 
48  const std::string& name,
49  size_t event) {
50  char prefix[64];
51 
52  snprintf(prefix, sizeof(prefix), "event%09zu-", event);
53 
54  if (dir.empty()) {
55  return prefix + name;
56  } else {
57  return dir + '/' + prefix + name;
58  }
59 }
60 
61 std::pair<size_t, size_t> ActsExamples::determineEventFilesRange(
62  const std::string& dir, const std::string& name) {
63  using std::filesystem::current_path;
64  using std::filesystem::directory_iterator;
66 
68  Acts::getDefaultLogger("EventFilesRange", Acts::Logging::INFO));
69 
70  // ensure directory path is valid
71  auto dir_path = dir.empty() ? current_path() : path(dir);
72  if (not exists(dir_path)) {
73  throw std::runtime_error("'" + dir_path.native() + "' does not exists");
74  }
75  if (not is_directory(dir_path)) {
76  throw std::runtime_error("'" + dir_path.native() + "' is not a directory");
77  }
78 
79  // invalid default range that allows simple restriction later on
80  size_t eventMin = SIZE_MAX;
81  size_t eventMax = 0;
82 
83  // filter matching event files from the directory listing
85  std::regex re("^event([0-9]+)-" + name + "$");
86  std::cmatch match;
87 
88  for (const auto& f : directory_iterator(dir_path)) {
89  if ((not exists(f.status())) or (not is_regular_file(f.status()))) {
90  continue;
91  }
92  // keep a copy so the match can refer to the underlying const char*
93  filename = f.path().filename().native();
94  if (std::regex_match(filename.c_str(), match, re)) {
95  ACTS_VERBOSE("Matching file " << filename);
96 
97  // first sub_match is the whole string, second should be the event number
98  size_t event = 0;
99  auto ret = std::from_chars(match[1].first, match[1].second, event);
100  if (ret.ptr == match[1].first) {
101  throw std::runtime_error(
102  "Could not extract event number from filename");
103  }
104  // enlarge available event range
105  eventMin = std::min(eventMin, event);
106  eventMax = std::max(eventMax, event);
107  }
108  }
109  ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
110 
111  // should only occur if no files matched and the initial values persisted.
112  if (eventMax < eventMin) {
113  return std::make_pair(0u, 0u);
114  }
115  return std::make_pair(eventMin, eventMax + 1);
116 }