ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CommonOptions.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CommonOptions.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 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 #include <exception>
11 #include <fstream>
12 #include <regex>
13 #include <system_error>
15 
16 using namespace boost::program_options;
17 
18 boost::program_options::options_description FW::Options::makeDefaultOptions(
19  std::string caption) {
20  options_description opt(caption);
21 
22  opt.add_options()("help,h", "Produce help message");
23  opt.add_options()(
24  "loglevel,l", value<size_t>()->default_value(2),
25  "The output log level. Please set the wished number (0 = VERBOSE, 1 = "
26  "DEBUG, 2 = INFO, 3 = WARNING, 4 = ERROR, 5 = FATAL).");
27  opt.add_options()(
28  "response-file", value<std::string>()->default_value(""),
29  "Configuration file (response file) replacing command line options.");
30 
31  return opt;
32 }
33 
35  boost::program_options::options_description& opt) {
36  // sequencer options
37  opt.add_options()("events,n", value<size_t>(),
38  "The number of events to process. If not given, all "
39  "available events will be processed.")(
40  "skip", value<size_t>()->default_value(0),
41  "The number of events to skip")(
42  "jobs,j", value<int>()->default_value(-1),
43  "Number of parallel jobs, negative for automatic.");
44 }
45 
47  boost::program_options::options_description& opt) {
48  opt.add_options()("rnd-seed", value<uint64_t>()->default_value(1234567890u),
49  "Random numbers seed.");
50 }
51 
53  boost::program_options::options_description& opt) {
54  opt.add_options()("geo-surface-loglevel", value<size_t>()->default_value(3),
55  "The outoput log level for the surface building.")(
56  "geo-layer-loglevel", value<size_t>()->default_value(3),
57  "The output log level for the layer building.")(
58  "geo-volume-loglevel", value<size_t>()->default_value(3),
59  "The output log level for the volume building.")(
60  "geo-subdetectors",
61  value<read_strings>()->multitoken()->default_value({{}}),
62  "Sub detectors for the output writing");
63 }
64 
66  boost::program_options::options_description& opt) {
67  opt.add_options()(
68  "mat-input-type", value<std::string>()->default_value("build"),
69  "The way material is loaded: 'none', 'build', 'proto', 'file'.")(
70  "mat-input-file", value<std::string>()->default_value(""),
71  "Name of the material map input file, supported: '.json' or '.root'.")(
72  "mat-output-file", value<std::string>()->default_value(""),
73  "Name of the material map output file (without extension).")(
74  "mat-output-sensitives", value<bool>()->default_value(true),
75  "Write material information of sensitive surfaces.")(
76  "mat-output-approaches", value<bool>()->default_value(true),
77  "Write material information of approach surfaces.")(
78  "mat-output-representing", value<bool>()->default_value(true),
79  "Write material information of representing surfaces.")(
80  "mat-output-boundaries", value<bool>()->default_value(true),
81  "Write material information of boundary surfaces.")(
82  "mat-output-volumes", value<bool>()->default_value(true),
83  "Write material information of dense volumes.")(
84  "mat-output-data", value<bool>()->default_value(true),
85  "Output the data field(s).")(
86  "mat-output-allsurfaces", value<bool>()->default_value(false),
87  "Output all the selected surfaces to create a surface map.");
88 }
89 
91  boost::program_options::options_description& opt) {
92  // Add specific options for this example
93  opt.add_options()("output-dir", value<std::string>()->default_value(""),
94  "Output directory location.")(
95  "output-root", value<bool>()->default_value(false),
96  "Switch on to write '.root' output file(s).")(
97  "output-csv", value<bool>()->default_value(false),
98  "Switch on to write '.csv' output file(s).")(
99  "output-obj", value<bool>()->default_value(false),
100  "Switch on to write '.obj' ouput file(s).")(
101  "output-json", value<bool>()->default_value(false),
102  "Switch on to write '.json' ouput file(s).")(
103  "output-txt", value<bool>()->default_value(false),
104  "Switch on to write '.txt' ouput file(s).");
105 }
106 
108  boost::program_options::options_description& opt) {
109  // Add specific options for this example
110  opt.add_options()("input-dir", value<std::string>()->default_value(""),
111  "Input directory location.")(
112  "input-files", value<read_strings>()->multitoken()->default_value({}),
113  "Input files, space separated.")("input-root",
114  value<bool>()->default_value(false),
115  "Switch on to read '.root' file(s).")(
116  "input-csv", value<bool>()->default_value(false),
117  "Switch on to read '.csv' file(s).")("input-obj",
118  value<bool>()->default_value(false),
119  "Switch on to read '.obj' file(s).")(
120  "input-json", value<bool>()->default_value(false),
121  "Switch on to read '.json' file(s).");
122 }
123 
124 boost::program_options::variables_map FW::Options::parse(
125  const boost::program_options::options_description& opt, int argc,
126  char* argv[]) noexcept(false) {
127  variables_map vm;
128  store(command_line_parser(argc, argv).options(opt).run(), vm);
129  notify(vm);
130 
131  if (vm.count("response-file") and
132  not vm["response-file"].template as<std::string>().empty()) {
133  // Load the file and tokenize it
134  std::ifstream ifs(vm["response-file"].as<std::string>().c_str());
135  if (!ifs) {
136  throw(std::system_error(std::error_code(),
137  "Could not open response file."));
138  }
139  // Read the whole file into a string
140  std::stringstream ss;
141  ss << ifs.rdbuf();
142  std::string rString = ss.str();
143  std::vector<std::string> args;
144  const std::regex rgx("[ \t\r\n\f]");
145  std::sregex_token_iterator iter(rString.begin(), rString.end(), rgx, -1);
146  std::sregex_token_iterator end;
147  for (; iter != end; ++iter) {
148  if (std::string(*iter).empty()) {
149  continue;
150  }
151  args.push_back(*iter);
152  }
153  // Parse the file and store the options
154  store(command_line_parser(args).options(opt).run(), vm);
155  }
156 
157  // Automatically handle help
158  if (vm.count("help")) {
159  std::cout << opt << std::endl;
160  vm.clear();
161  }
162  return vm;
163 }
164 
166  const boost::program_options::variables_map& vm) {
167  return Acts::Logging::Level(vm["loglevel"].as<size_t>());
168 }
169 
171  const boost::program_options::variables_map& vm) {
172  Sequencer::Config cfg;
173  cfg.skip = vm["skip"].as<size_t>();
174  if (not vm["events"].empty()) {
175  cfg.events = vm["events"].as<size_t>();
176  }
177  cfg.logLevel = readLogLevel(vm);
178  cfg.numThreads = vm["jobs"].as<int>();
179  if (not vm["output-dir"].empty()) {
180  cfg.outputDir = vm["output-dir"].as<std::string>();
181  }
182  return cfg;
183 }
184 
185 // Read the random numbers config.
187  const boost::program_options::variables_map& vm) {
189  cfg.seed = vm["rnd-seed"].as<uint64_t>();
190  return cfg;
191 }