Analysis Software
Documentation for sPHENIX simulation 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-2021 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 
13 
14 #include <exception>
15 #include <fstream>
16 #include <regex>
17 #include <system_error>
18 
19 using namespace boost::program_options;
20 
21 boost::program_options::options_description
23  std::cout
24  << "\n\n======================= DEPRECATION NOTICE "
25  "========================\n"
26  "The examples executables is deprecated. They will be removed in a\n"
27  "future version.\n"
28  "Consider using the python bindings for the example algorithms: \n"
29  "https://acts.readthedocs.io/en/latest/examples/python_bindings.html\n"
30  "==================================================================="
31  "\n\n"
32  << std::endl;
33 
34  options_description opt(caption);
35 
36  opt.add_options()("help,h", "Produce help message");
37  opt.add_options()(
38  "loglevel,l", value<size_t>()->default_value(2),
39  "The output log level. Please set the wished number (0 = VERBOSE, 1 = "
40  "DEBUG, 2 = INFO, 3 = WARNING, 4 = ERROR, 5 = FATAL).");
41  opt.add_options()(
42  "response-file", value<std::string>()->default_value(""),
43  "Configuration file (response file) replacing command line options.");
44 
45  return opt;
46 }
47 
49  boost::program_options::options_description& opt) {
50  // sequencer options
51  opt.add_options()("events,n", value<size_t>(),
52  "The number of events to process. If not given, all "
53  "available events will be processed.")(
54  "skip", value<size_t>()->default_value(0),
55  "The number of events to skip")("jobs,j", value<int>()->default_value(-1),
56  "Number of parallel jobs, negative for "
57  "automatic.");
58 }
59 
61  boost::program_options::options_description& opt) {
62  opt.add_options()("rnd-seed", value<uint64_t>()->default_value(1234567890u),
63  "Random numbers seed.");
64 }
65 
67  boost::program_options::options_description& opt) {
68  opt.add_options()("geo-surface-loglevel", value<size_t>()->default_value(3),
69  "The outoput log level for the surface building.")(
70  "geo-layer-loglevel", value<size_t>()->default_value(3),
71  "The output log level for the layer building.")(
72  "geo-volume-loglevel", value<size_t>()->default_value(3),
73  "The output log level "
74  "for the volume "
75  "building.");
76 }
77 
79  boost::program_options::options_description& opt) {
80  opt.add_options()(
81  "mat-input-type", value<std::string>()->default_value("build"),
82  "The way material is loaded: 'none', 'build', 'proto', 'file'.")(
83  "mat-input-file", value<std::string>()->default_value(""),
84  "Name of the material map input file, supported: '.json', '.cbor' or "
85  "'.root'.")("mat-output-file", value<std::string>()->default_value(""),
86  "Name of the material map output file (without extension).")(
87  "mat-output-sensitives", value<bool>()->default_value(true),
88  "Write material information of sensitive surfaces.")(
89  "mat-output-approaches", value<bool>()->default_value(true),
90  "Write material information of approach surfaces.")(
91  "mat-output-representing", value<bool>()->default_value(true),
92  "Write material information of representing surfaces.")(
93  "mat-output-boundaries", value<bool>()->default_value(true),
94  "Write material information of boundary surfaces.")(
95  "mat-output-volumes", value<bool>()->default_value(true),
96  "Write material information of volumes.")(
97  "mat-output-dense-volumes", value<bool>()->default_value(false),
98  "Write material information of dense volumes.")(
99  "mat-output-allmaterial", value<bool>()->default_value(false),
100  "Add protoMaterial to all surfaces and volume for the mapping.");
101 }
102 
104  boost::program_options::options_description& opt,
105  OutputFormat formatFlags) {
106  // Add specific options for this example
107  opt.add_options()("output-dir", value<std::string>()->default_value(""),
108  "Output directory location.");
109 
110  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Root)) {
111  opt.add_options()("output-root", bool_switch(),
112  "Switch on to write '.root' output file(s).");
113  }
114 
115  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Csv)) {
116  opt.add_options()("output-csv", bool_switch(),
117  "Switch on to write '.csv' output file(s).");
118  }
119 
120  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Obj)) {
121  opt.add_options()("output-obj", bool_switch(),
122  "Switch on to write '.obj' output file(s).");
123  }
124 
125  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Json)) {
126  opt.add_options()("output-json", bool_switch(),
127  "Switch on to write '.json' output file(s).");
128  }
129 
130  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Cbor)) {
131  opt.add_options()("output-cbor", bool_switch(),
132  "Switch on to write '.cbor' output file(s).");
133  }
134 
135  if (ACTS_CHECK_BIT(formatFlags, OutputFormat::Txt)) {
136  opt.add_options()("output-txt", bool_switch(),
137  "Switch on to write '.txt' output file(s).");
138  }
139 }
140 
142  boost::program_options::options_description& opt) {
143  // Add specific options for this example
144  opt.add_options()("input-dir", value<std::string>()->default_value(""),
145  "Input directory location.")(
146  "input-files", value<std::vector<std::string>>(),
147  "Input files, can occur multiple times.")(
148  "input-root", value<bool>()->default_value(false),
149  "Switch on to read '.root' file(s).")(
150  "input-csv", value<bool>()->default_value(false),
151  "Switch on to read '.csv' file(s).")("input-obj",
152  value<bool>()->default_value(false),
153  "Switch on to read '.obj' file(s).")(
154  "input-json", value<bool>()->default_value(false),
155  "Switch on to read '.json' file(s).")(
156  "input-cbor", value<bool>()->default_value(false),
157  "Switch on to read '.cbor' file(s).");
158 }
159 
160 boost::program_options::variables_map ActsExamples::Options::parse(
161  const boost::program_options::options_description& opt, int argc,
162  char* argv[]) noexcept(false) {
163  variables_map vm;
164  store(command_line_parser(argc, argv).options(opt).run(), vm);
165  notify(vm);
166 
167  if (vm.count("response-file") != 0u and
168  not vm["response-file"].template as<std::string>().empty()) {
169  // Load the file and tokenize it
170  std::ifstream ifs(vm["response-file"].as<std::string>().c_str());
171  if (!ifs) {
172  throw(std::system_error(std::error_code(),
173  "Could not open response file."));
174  }
175  // Read the whole file into a string
176  std::stringstream ss;
177  ss << ifs.rdbuf();
178  std::string rString = ss.str();
179  std::vector<std::string> args;
180  const std::regex rgx("[ \t\r\n\f]");
181  std::sregex_token_iterator iter(rString.begin(), rString.end(), rgx, -1);
182  std::sregex_token_iterator end;
183  for (; iter != end; ++iter) {
184  if (std::string(*iter).empty()) {
185  continue;
186  }
187  args.push_back(*iter);
188  }
189  // Parse the file and store the options
190  store(command_line_parser(args).options(opt).run(), vm);
191  }
192 
193  // Automatically handle help
194  if (vm.count("help") != 0u) {
195  std::cout << opt << std::endl;
196  vm.clear();
197  }
198  return vm;
199 }
200 
202  const boost::program_options::variables_map& vm) {
203  return Acts::Logging::Level(vm["loglevel"].as<size_t>());
204 }
205 
207  const boost::program_options::variables_map& vm) {
209  cfg.skip = vm["skip"].as<size_t>();
210  if (not vm["events"].empty()) {
211  cfg.events = vm["events"].as<size_t>();
212  }
213  cfg.logLevel = readLogLevel(vm);
214  cfg.numThreads = vm["jobs"].as<int>();
215  if (not vm["output-dir"].empty()) {
216  cfg.outputDir = vm["output-dir"].as<std::string>();
217  }
218  return cfg;
219 }
220 
221 // Read the random numbers config.
224  const boost::program_options::variables_map& vm) {
226  cfg.seed = vm["rnd-seed"].as<uint64_t>();
227  return cfg;
228 }