EIC 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 <charconv>
14 #include <cstdio>
15 #include <regex>
16 #include <sstream>
17 #include <stdexcept>
18 
19 #include <boost/filesystem.hpp>
20 
21 std::string ActsExamples::ensureWritableDirectory(const std::string& dir) {
22  using boost::filesystem::current_path;
23  using boost::filesystem::path;
24 
25  auto dir_path = dir.empty() ? current_path() : path(dir);
26  if (exists(dir_path) and not is_directory(dir_path)) {
27  throw std::runtime_error("'" + dir +
28  "' already exists but is not a directory");
29  }
30  create_directories(dir_path);
31  return canonical(dir_path).native();
32 }
33 
34 std::string ActsExamples::joinPaths(const std::string& dir,
35  const std::string& name) {
36  if (dir.empty()) {
37  return name;
38  } else {
39  return dir + '/' + name;
40  }
41 }
42 
43 std::string ActsExamples::perEventFilepath(const std::string& dir,
44  const std::string& name,
45  size_t event) {
46  char prefix[64];
47 
48  snprintf(prefix, sizeof(prefix), "event%09zu-", event);
49 
50  if (dir.empty()) {
51  return prefix + name;
52  } else {
53  return dir + '/' + prefix + name;
54  }
55 }
56 
57 std::pair<size_t, size_t> ActsExamples::determineEventFilesRange(
58  const std::string& dir, const std::string& name) {
59  using boost::filesystem::current_path;
60  using boost::filesystem::directory_iterator;
61  using boost::filesystem::path;
62 
65 
66  // ensure directory path is valid
67  auto dir_path = dir.empty() ? current_path() : path(dir);
68  if (not exists(dir_path)) {
69  throw std::runtime_error("'" + dir_path.native() + "' does not exists");
70  }
71  if (not is_directory(dir_path)) {
72  throw std::runtime_error("'" + dir_path.native() + "' is not a directory");
73  }
74 
75  // invalid default range that allows simple restriction later on
76  size_t eventMin = SIZE_MAX;
77  size_t eventMax = 0;
78 
79  // filter matching event files from the directory listing
80  std::string filename;
81  std::regex re("^event([0-9]+)-" + name + "$");
82  std::cmatch match;
83 
84  for (const auto& f : directory_iterator(dir_path)) {
85  if ((not exists(f.status())) or (not is_regular_file(f.status()))) {
86  continue;
87  }
88  // keep a copy so the match can refer to the underlying const char*
89  filename = f.path().filename().native();
90  if (std::regex_match(filename.c_str(), match, re)) {
91  ACTS_VERBOSE("Matching file " << filename);
92 
93  // first sub_match is the whole string, second should be the event number
94  size_t event = 0;
95  auto ret = std::from_chars(match[1].first, match[1].second, event);
96  if (ret.ptr == match[1].first) {
97  throw std::runtime_error(
98  "Could not extract event number from filename");
99  }
100  // enlarge available event range
101  eventMin = std::min(eventMin, event);
102  eventMax = std::max(eventMax, event);
103  }
104  }
105  ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
106 
107  // should only occur if no files matched and the initial values persisted.
108  if (eventMax < eventMin) {
109  return std::make_pair(0u, 0u);
110  }
111  return std::make_pair(eventMin, eventMax + 1);
112 }