DAS  3.0
Das Analysis System
Options.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPLv3-or-later
2 //
3 // SPDX-FileCopyrightText: Patrick L.S. Connor <patrick.connor@cern.ch>
4 
5 #pragma once
6 
7 #include <functional>
8 #include <vector>
9 #include <filesystem>
10 #include <iostream>
11 #include <optional>
12 
13 #include <boost/program_options/options_description.hpp>
14 #include <boost/program_options/positional_options.hpp>
15 #include <boost/property_tree/ptree.hpp>
16 
17 #ifndef DARWIN_GIT_COMMIT
18 # define DARWIN_GIT_COMMIT nullptr
19 #endif
20 
21 #ifndef DARWIN_EXAMPLE
22 # define DARWIN_EXAMPLE nullptr
23 #endif
24 
25 namespace Darwin::Tools {
26 
27 enum Mode {
28  none = 0b0000000,
29  verbose = 0b0000001,
30  config = 0b0000010,
31  split = 0b0000100,
32  fill = 0b0001000,
33  Friend = 0b0010000,
34  syst = 0b0100000,
35  plugin = 0b1000000
36 };
37 
38 using enum Mode;
39 
63 class Options {
64 
65  // internal parsing
66  boost::program_options::options_description
71  boost::program_options::positional_options_description
73  boost::property_tree::ptree
75 
76  const std::filesystem::path example;
77 
82  void parse_helper (int, const char * const []);
83 
87  void parse_common (int, const char * const []);
88 
93  void parse_custom (int, const char * const []);
94 
95  std::filesystem::path config_file;
96 
97  // for helper
98  const std::string tutorial;
99  std::string synopsis;
100  std::vector<std::string>
103 
107  static void check_input (const std::filesystem::path&
108  );
109 
116  static void check_output (const std::filesystem::path&
117  );
118 
124  Options& set (const char *,
125  const boost::program_options::value_semantic *,
126  const char *
127  );
128 
133  template<typename T
134  > std::function<void(T)> put (const std::string& configpath
135  )
136  {
137  return [configpath,this](T value) { pt_conf.put<T>(configpath, value); };
138  }
139 
140  std::optional<unsigned> registered;
141 
144  bool allow_unregistered () const { return bool(registered); }
145 
148  std::string exec (const std::string&
149  );
150 
151  enum Stage {
154  Arg,
155  Args
156  };
157 
159 
160  int params;
161  const std::string m_commit;
162  int steer;
163  static std::filesystem::path prefix;
164  int j,
165  k;
166 
167  std::vector<std::filesystem::path> plugin_paths;
168 
169  std::vector<std::vector<std::string>>
172 
173 public:
174 
181  Options (const std::string&,
182  int = none,
183  const char* commit = DARWIN_GIT_COMMIT,
184  const char* example = DARWIN_EXAMPLE
185  );
186 
189  Options () = delete;
190 
193  ~Options () { if (steer & verbose) std::cout << "Options: destroying options" << std::endl; }
194 
201  Options& input (const char *,
202  std::filesystem::path *,
203  const char *,
204  const std::vector<std::string>& = {".root", ".xml"});
205 
212  Options& inputs (const char *,
213  std::vector<std::filesystem::path> *,
214  const char *,
215  const std::vector<std::string>& = {".root", ".xml"});
216 
223  Options& output (const char *,
224  std::filesystem::path *,
225  const char *,
226  const std::vector<std::string>& = {".root", ".xml"});
227 
233  template<typename T> Options& arg (
234  const char * name,
235  const char * configpath,
236  const char * desc)
237  {
238  if (stage > Stage::Arg)
239  BOOST_THROW_EXCEPTION(std::runtime_error(
240  "Not possible to add another argument at this stage"));
241  stage = Stage::Arg;
242 
243  names.push_back(name);
244  configpaths.push_back(configpath);
245  const boost::program_options::value_semantic * s =
246  boost::program_options::value<T>()->notifier(put<T>(configpath));
247  return set(name, s, desc);
248  }
249 
254  Options& args (const char * name,
255  const char * configpath,
256  const char * desc
257  );
258 
262  const boost::property_tree::ptree& operator() (int, const char * const []);
263 
269  static std::string parse_env_var (std::string);
270 
273  static std::string parse_env_var (const char * p)
274  { return parse_env_var(std::string(p)); }
275 
278  static std::filesystem::path parse_env_var (const std::filesystem::path& p)
279  { return std::filesystem::path(parse_env_var(p.string())); }
280 
283  static void parse_config
284  (boost::property_tree::ptree&,
285  std::string = "");
286 
287  // result of parsing
288  static std::string full_cmd;
289 
292  auto commit () const { return m_commit; }
293 
296  int steering () const { return steer; }
297 
302  std::vector<std::filesystem::path> plugins () const
303  {
304  using namespace std;
305  if (!(params & plugin))
306  BOOST_THROW_EXCEPTION( runtime_error("Plugins have not been activated") );
307  return plugin_paths;
308  }
309 
312  std::pair<unsigned, unsigned> slice () const
313  {
314  using namespace std;
315  if (!(params & split))
316  BOOST_THROW_EXCEPTION(invalid_argument("No splitting of the input file."));
317  return make_pair(j,k);
318  }
319 };
320 
321 } // end of Darwin::Tools namespace
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
Darwin::Tools::Options::prefix
static std::filesystem::path prefix
prefix command to steer -j and -k
Definition: Options.h:163
Darwin::Tools::Options::parse_env_var
static std::string parse_env_var(std::string)
Definition: Options.cc:77
Darwin::Tools::Options::steering
int steering() const
Steering information for running of executable.
Definition: Options.h:296
Darwin::Tools::Options::check_input
static void check_input(const std::filesystem::path &)
Definition: Options.cc:39
Darwin::Tools::Options::k
int k
slice index
Definition: Options.h:165
Darwin::Tools::Options::tutorial
const std::string tutorial
define in constructor, shown with option -t
Definition: Options.h:98
Darwin::Tools::Options::Options
Options()=delete
No default constructor is allowed.
Darwin::Tools::fill
@ fill
activate -f to fill the tree
Definition: Options.h:32
Darwin::Tools::Options::custom
boost::program_options::options_description custom
for positional arguments, depending on the actual command
Definition: Options.h:70
Darwin::Tools::Options::j
int j
Definition: Options.h:164
Darwin::Tools::Friend
@ Friend
activate -F to only fill the new branches
Definition: Options.h:33
Darwin::Tools::Options::plugins
std::vector< std::filesystem::path > plugins() const
Definition: Options.h:302
Darwin::Tools::Options::allow_unregistered
bool allow_unregistered() const
Definition: Options.h:144
Darwin::Tools::Options::inputs
Options & inputs(const char *, std::vector< std::filesystem::path > *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:492
Darwin::Tools::Options::put
std::function< void(T)> put(const std::string &configpath)
Definition: Options.h:134
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:31
Darwin::Tools::Options::args
Options & args(const char *name, const char *configpath, const char *desc)
Definition: Options.cc:553
Ntupliser_cfg.p
p
Definition: Ntupliser_cfg.py:178
Darwin::Tools::Options::check_output
static void check_output(const std::filesystem::path &)
Definition: Options.cc:52
Darwin::Tools::Options::Input
@ Input
first the inputs
Definition: Options.h:152
Darwin::Tools::Options::hidden
boost::program_options::options_description hidden
hidden interface (not for lambda user)
Definition: Options.h:67
Darwin::Tools::syst
@ syst
activate -s to systematic uncertainties
Definition: Options.h:34
Darwin::Tools::none
@ none
default (for simple executables)
Definition: Options.h:28
Darwin::Tools::Options::operator()
const boost::property_tree::ptree & operator()(int, const char *const [])
Definition: Options.cc:417
Darwin::Tools::Options::synopsis
std::string synopsis
stores a clean version of the command, displayed w. -h
Definition: Options.h:99
Darwin::Tools::Options::pos_hide
boost::program_options::positional_options_description pos_hide
parser for positional arguments
Definition: Options.h:72
Darwin::Tools::Options::parse_config
static void parse_config(boost::property_tree::ptree &, std::string="")
Parse config if given. This method calls itself to parse the tree structure.
Darwin::Tools::Options::~Options
~Options()
Destructor.
Definition: Options.h:193
Darwin::Tools::plugin
@ plugin
activate -p to run a plugin
Definition: Options.h:35
Darwin::Tools::Options::steer
int steer
output parameters for code executation
Definition: Options.h:162
Darwin::Tools::Options::commit
auto commit() const
Git commit information.
Definition: Options.h:292
Darwin::Tools::Options::Output
@ Output
then the outputs (inputs are no longer allowed)
Definition: Options.h:153
DARWIN_EXAMPLE
#define DARWIN_EXAMPLE
Definition: Options.h:22
Darwin::Tools::Options::parse_common
void parse_common(int, const char *const [])
Definition: Options.cc:273
DARWIN_GIT_COMMIT
#define DARWIN_GIT_COMMIT
Definition: Options.h:18
Darwin::Tools::Options::parse_helper
void parse_helper(int, const char *const [])
Definition: Options.cc:188
Darwin::Tools::Options::names
std::vector< std::string > names
names of the different options (shown in synopsis)
Definition: Options.h:101
Darwin::Tools::Options::inputExt
std::vector< std::vector< std::string > > inputExt
expected extensions of input files
Definition: Options.h:170
Darwin::Tools::verbose
@ verbose
bit for debug mode (-v is always available)
Definition: Options.h:29
Darwin::Tools::Options::slice
std::pair< unsigned, unsigned > slice() const
Compactify slice information into a pair.
Definition: Options.h:312
Darwin::Tools::Options::set
Options & set(const char *, const boost::program_options::value_semantic *, const char *)
Definition: Options.cc:452
Darwin::Tools::Options
Common class to interpret the command line, based on Boost Program Options.
Definition: Options.h:63
Darwin::Tools::Options::Args
@ Args
finally the remaining arguments / garbage collector (must be very last)
Definition: Options.h:155
Darwin::Tools::Options::arg
Options & arg(const char *name, const char *configpath, const char *desc)
Definition: Options.h:233
Darwin::Tools::Options::stage
Stage stage
internal stage
Definition: Options.h:158
Darwin::Tools::Options::parse_env_var
static std::filesystem::path parse_env_var(const std::filesystem::path &p)
Parse environment variable in a path.
Definition: Options.h:278
Darwin::Tools::Options::Stage
Stage
Definition: Options.h:151
Darwin::Tools
Classes and functions related to the framework.
Definition: Darwin_dict.cxx:1144
Darwin::Tools::Options::registered
std::optional< unsigned > registered
collect the number of registered options (only if Options::args has been called before)
Definition: Options.h:140
Darwin::Tools::Options::parse_env_var
static std::string parse_env_var(const char *p)
Parse environment variable in C-style string.
Definition: Options.h:273
Darwin::Tools::Options::m_commit
const std::string m_commit
Commit SHA for –git.
Definition: Options.h:161
Darwin::Tools::Options::helper
boost::program_options::options_description helper
to display the helper
Definition: Options.h:68
Darwin::Tools::Options::parse_custom
void parse_custom(int, const char *const [])
Definition: Options.cc:324
Darwin::Tools::Options::config_file
std::filesystem::path config_file
path to INFO, JSON, or XML config file
Definition: Options.h:95
Darwin::Tools::Options::outputExt
std::vector< std::vector< std::string > > outputExt
expected extensions of output files
Definition: Options.h:171
Darwin::Tools::Options::output
Options & output(const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:536
Darwin::Tools::Options::input
Options & input(const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:465
Darwin::Tools::Options::exec
std::string exec(const std::string &)
Definition: Options.cc:478
Darwin::Tools::Mode
Mode
Definition: Options.h:27
Darwin::Tools::Options::Arg
@ Arg
then the registered arguments (inputs and outputs no longer allowed)
Definition: Options.h:154
Darwin::Tools::Options::plugin_paths
std::vector< std::filesystem::path > plugin_paths
plugin locations
Definition: Options.h:167
Darwin::Tools::Options::params
int params
input parameters to interpret explicit options
Definition: Options.h:160
Darwin::Tools::Options::configpaths
std::vector< std::string > configpaths
path to the options in config (except for I/O)
Definition: Options.h:102
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:288
Darwin::Tools::config
@ config
activate -c option to provide config file
Definition: Options.h:30
Darwin::Tools::Options::pt_conf
boost::property_tree::ptree pt_conf
internal config obtained from arugments and input config
Definition: Options.h:74
Darwin::Tools::Options::common
boost::program_options::options_description common
generic + explicit options like --verbose
Definition: Options.h:69
Darwin::Tools::Options::example
const std::filesystem::path example
path to example config
Definition: Options.h:76