DAS  3.0
Das Analysis System
Options.h
Go to the documentation of this file.
1 #ifndef DARWIN_OPTIONS_H
2 #define DARWIN_OPTIONS_H
3 
4 #include <functional>
5 #include <vector>
6 #include <filesystem>
7 #include <iostream>
8 #include <optional>
9 
10 #include <boost/program_options/options_description.hpp>
11 #include <boost/program_options/positional_options.hpp>
12 #include <boost/property_tree/ptree.hpp>
13 
14 #ifndef DARWIN_GIT_COMMIT
15 # define DARWIN_GIT_COMMIT nullptr
16 #endif
17 
18 #ifndef DARWIN_EXAMPLE
19 # define DARWIN_EXAMPLE nullptr
20 #endif
21 
22 namespace Darwin::Tools {
23 
24 enum {
25  none = 0b000000,
26  config = 0b000001,
27  split = 0b000010,
28  fill = 0b000100,
29  Friend = 0b001100,
30  syst = 0b010000,
31  verbose = 0b100000
32 };
33 
56 class Options {
57 
58  // internal parsing
59  boost::program_options::options_description hidden,
63  boost::program_options::positional_options_description pos_hide;
64  boost::property_tree::ptree pt_conf;
65 
66  const std::filesystem::path example;
67 
72  void parse_helper (int, const char * const []);
73 
77  void parse_common (int, const char * const []);
78 
83  void parse_custom (int, const char * const []);
84 
85  std::filesystem::path config_file;
86 
87  // for helper
88  const std::string tutorial;
89  std::string synopsis;
90  std::vector<std::string> names,
92 
96  static void check_input (const std::filesystem::path&
97  );
98 
105  static void check_output (const std::filesystem::path&
106  );
107 
113  Options& set (const char *,
114  const boost::program_options::value_semantic *,
115  const char *
116  );
117 
122  template<typename T
123  > std::function<void(T)> put (const char * configpath
124  )
125  {
126  return [configpath,this](T value) { pt_conf.put<T>(configpath, value); };
127  }
128 
129  std::optional<unsigned> registered;
130 
133  inline bool allow_unregistered () const { return bool(registered); }
134 
137  std::string exec (const std::string&
138  );
139 
140  enum Stage {
143  Arg,
144  Args
145  };
146 
148 
149  const int params;
150  const std::string m_commit;
151  int steer;
152  static std::filesystem::path prefix;
153  unsigned j,
154  k;
155 
156 public:
157 
164  Options (const std::string&,
165  int = none,
166  const char* commit = DARWIN_GIT_COMMIT,
167  const char* example = DARWIN_EXAMPLE
168  );
169 
172  Options () = delete;
173 
180  Options& input (const char *,
181  std::filesystem::path *,
182  const char *
183  );
184 
191  Options& inputs (const char *,
192  std::vector<std::filesystem::path> *,
193  const char *
194  );
195 
202  Options& output (const char *,
203  std::filesystem::path *,
204  const char *
205  );
206 
212  template<typename T> Options& arg (const char * name,
213  const char * configpath,
214  const char * desc
215  )
216  {
217  if (stage > Stage::Arg)
218  BOOST_THROW_EXCEPTION(std::runtime_error("Not possible to add another argument at this stage"));
219  stage = Stage::Arg;
220 
221  names.push_back(name);
222  configpaths.push_back(configpath);
223  const boost::program_options::value_semantic * s =
224  boost::program_options::value<T>()->notifier(put<T>(configpath));
225  return set(name, s, desc);
226  }
227 
232  Options& args (const char * name,
233  const char * configpath,
234  const char * desc
235  );
236 
240  const boost::property_tree::ptree& operator() (int, const char * const []);
241 
247  static std::string parse_env_var (std::string);
248 
251  static inline const char * parse_env_var (const char * p)
252  { return parse_env_var(std::string(p)).c_str(); }
253 
256  static inline std::filesystem::path parse_env_var (const std::filesystem::path& p)
257  { return std::filesystem::path(parse_env_var(p.string())); }
258 
261  static void parse_config (boost::property_tree::ptree&,
262  std::string = ""
263  );
264 
265  // result of parsing
266  static std::string full_cmd;
267 
270  auto commit () const { return m_commit; }
271 
274  inline int steering () const { return steer; }
275 
278  inline std::pair<unsigned, unsigned> slice () const
279  {
280  if (!(params & split))
281  BOOST_THROW_EXCEPTION(std::invalid_argument("No splitting of the input file."));
282  return std::make_pair(j,k);
283  }
284 };
285 
286 } // end of namespace
287 
288 #endif
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:152
Darwin::Tools::Options::parse_env_var
static std::string parse_env_var(std::string)
Definition: Options.cc:73
Darwin::Tools::Options::steering
int steering() const
Steering information for running of executable.
Definition: Options.h:274
Darwin::Tools::Options::check_input
static void check_input(const std::filesystem::path &)
Definition: Options.cc:37
Darwin::Tools::Options::tutorial
const std::string tutorial
define in constructor, shown with option -t
Definition: Options.h:88
Darwin::Tools::Options::Options
Options()=delete
No default constructor is allowed.
Darwin::Tools::Options::custom
boost::program_options::options_description custom
for positional arguments, depending on the actual command
Definition: Options.h:62
Darwin::Tools::syst
@ syst
activate -s to systematic uncertainties
Definition: Options.h:30
Darwin::Tools::verbose
@ verbose
bit for debug mode (-v is always available)
Definition: Options.h:31
Darwin::Tools::none
@ none
default (for simple executables)
Definition: Options.h:25
Darwin::Tools::Options::inputs
Options & inputs(const char *, std::vector< std::filesystem::path > *, const char *)
Definition: Options.cc:421
Darwin::Tools::Options::allow_unregistered
bool allow_unregistered() const
Definition: Options.h:133
Darwin::Tools::Options::output
Options & output(const char *, std::filesystem::path *, const char *)
Definition: Options.cc:460
Darwin::Tools::Options::args
Options & args(const char *name, const char *configpath, const char *desc)
Definition: Options.cc:473
Ntupliser_cfg.p
p
Definition: Ntupliser_cfg.py:361
Darwin::Tools::Options::check_output
static void check_output(const std::filesystem::path &)
Definition: Options.cc:50
Darwin::Tools::Options::Input
@ Input
first the inputs
Definition: Options.h:141
Darwin::Tools::Options::hidden
boost::program_options::options_description hidden
hidden interface (not for lambda user)
Definition: Options.h:59
Darwin::Tools::Options::params
const int params
input parameters to interpret explicit options
Definition: Options.h:149
Darwin::Tools::Options::input
Options & input(const char *, std::filesystem::path *, const char *)
Definition: Options.cc:397
Darwin::Tools::Options::operator()
const boost::property_tree::ptree & operator()(int, const char *const [])
Definition: Options.cc:360
Darwin::Tools::Options::synopsis
std::string synopsis
stores the clean version of the command, displayed with -h
Definition: Options.h:89
Darwin::Tools::Options::pos_hide
boost::program_options::positional_options_description pos_hide
parser for positional arguments
Definition: Options.h:63
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::steer
int steer
output parameters for code executation
Definition: Options.h:151
Darwin::Tools::Options::commit
auto commit() const
Git commit information.
Definition: Options.h:270
Darwin::Tools::Options::Output
@ Output
then the outputs (inputs are no longer allowed)
Definition: Options.h:142
DARWIN_EXAMPLE
#define DARWIN_EXAMPLE
Definition: Options.h:19
Darwin::Tools::Options::parse_common
void parse_common(int, const char *const [])
Definition: Options.cc:237
Darwin::Tools::fill
@ fill
activate -f to fill the tree
Definition: Options.h:28
DARWIN_GIT_COMMIT
#define DARWIN_GIT_COMMIT
Definition: Options.h:15
Darwin::Tools::Options::parse_helper
void parse_helper(int, const char *const [])
Definition: Options.cc:171
Darwin::Tools::Options::names
std::vector< std::string > names
names of the different options (shown in synopsis)
Definition: Options.h:90
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:27
Darwin::Tools::Options::slice
std::pair< unsigned, unsigned > slice() const
Compactify slice information into a pair.
Definition: Options.h:278
Darwin::Tools::Options::k
unsigned k
slice index
Definition: Options.h:154
Darwin::Tools::Options::set
Options & set(const char *, const boost::program_options::value_semantic *, const char *)
Definition: Options.cc:386
Darwin::Tools::Options
Common class to interpret the command line, based on Boost Program Options.
Definition: Options.h:56
Darwin::Tools::config
@ config
activate -c option to provide config file
Definition: Options.h:26
Darwin::Tools::Options::Args
@ Args
finally the remaining arguments / garbage collector (must be very last)
Definition: Options.h:144
Darwin::Tools::Options::arg
Options & arg(const char *name, const char *configpath, const char *desc)
Definition: Options.h:212
Darwin::Tools::Options::stage
Stage stage
internal stage
Definition: Options.h:147
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:256
Darwin::Tools::Options::Stage
Stage
Definition: Options.h:140
Darwin::Tools
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
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:129
Darwin::Tools::Options::m_commit
const std::string m_commit
Commit SHA for –git.
Definition: Options.h:150
Darwin::Tools::Options::helper
boost::program_options::options_description helper
to display the helper
Definition: Options.h:60
Darwin::Tools::Options::j
unsigned j
Definition: Options.h:153
Darwin::Tools::Options::parse_custom
void parse_custom(int, const char *const [])
Definition: Options.cc:270
Darwin::Tools::Options::config_file
std::filesystem::path config_file
path to INFO, JSON, or XML config file
Definition: Options.h:85
Darwin::Tools::Options::parse_env_var
static const char * parse_env_var(const char *p)
Parse environment variable in C-style string.
Definition: Options.h:251
Darwin::Tools::Options::exec
std::string exec(const std::string &)
Definition: Options.cc:407
Darwin::Tools::Friend
@ Friend
activate -F to only fill the new branches
Definition: Options.h:29
Darwin::Tools::Options::Arg
@ Arg
then the registered arguments (inputs and outputs no longer allowed)
Definition: Options.h:143
Darwin::Tools::Options::put
std::function< void(T)> put(const char *configpath)
Definition: Options.h:123
Darwin::Tools::Options::configpaths
std::vector< std::string > configpaths
path to the options in config (except for I/O)
Definition: Options.h:91
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:266
Darwin::Tools::Options::pt_conf
boost::property_tree::ptree pt_conf
internal config obtained from arugments and input config
Definition: Options.h:64
Darwin::Tools::Options::common
boost::program_options::options_description common
generic + explicit options like --verbose
Definition: Options.h:61
Darwin::Tools::Options::example
const std::filesystem::path example
path to example config
Definition: Options.h:66