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