DAS  3.0
Das Analysis System
Options.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 #include <filesystem>
6 #include <iostream>
7 #include <optional>
8 
9 #include <boost/program_options/options_description.hpp>
10 #include <boost/program_options/positional_options.hpp>
11 #include <boost/property_tree/ptree.hpp>
12 
13 #ifndef DARWIN_GIT_COMMIT
14 # define DARWIN_GIT_COMMIT nullptr
15 #endif
16 
17 #ifndef DARWIN_EXAMPLE
18 # define DARWIN_EXAMPLE nullptr
19 #endif
20 
21 namespace Darwin::Tools {
22 
23 enum Mode {
24  none = 0b000000,
25  config = 0b000001,
26  split = 0b000010,
27  fill = 0b000100,
28  Friend = 0b001000,
29  syst = 0b010000,
30  verbose = 0b100000
31 };
32 
33 using enum Mode;
34 
58 class Options {
59 
60  // internal parsing
61  boost::program_options::options_description
66  boost::program_options::positional_options_description
68  boost::property_tree::ptree
70 
71  const std::filesystem::path example;
72 
77  void parse_helper (int, const char * const []);
78 
82  void parse_common (int, const char * const []);
83 
88  void parse_custom (int, const char * const []);
89 
90  std::filesystem::path config_file;
91 
92  // for helper
93  const std::string tutorial;
94  std::string synopsis;
95  std::vector<std::string>
98 
102  static void check_input (const std::filesystem::path&
103  );
104 
111  static void check_output (const std::filesystem::path&
112  );
113 
119  Options& set (const char *,
120  const boost::program_options::value_semantic *,
121  const char *
122  );
123 
128  template<typename T
129  > std::function<void(T)> put (const std::string& configpath
130  )
131  {
132  return [configpath,this](T value) { pt_conf.put<T>(configpath, value); };
133  }
134 
135  std::optional<unsigned> registered;
136 
139  inline bool allow_unregistered () const { return bool(registered); }
140 
143  std::string exec (const std::string&
144  );
145 
146  enum Stage {
149  Arg,
150  Args
151  };
152 
154 
155  const int params;
156  const std::string m_commit;
157  int steer;
158  static std::filesystem::path prefix;
159  int j,
160  k;
161 
162  std::vector<std::vector<std::string>>
165 
166 public:
167 
174  Options (const std::string&,
175  int = none,
176  const char* commit = DARWIN_GIT_COMMIT,
177  const char* example = DARWIN_EXAMPLE
178  );
179 
182  Options () = delete;
183 
190  Options& input (const char *,
191  std::filesystem::path *,
192  const char *,
193  const std::vector<std::string>& = {".root", ".xml"});
194 
201  Options& inputs (const char *,
202  std::vector<std::filesystem::path> *,
203  const char *,
204  const std::vector<std::string>& = {".root", ".xml"});
205 
212  Options& output (const char *,
213  std::filesystem::path *,
214  const char *,
215  const std::vector<std::string>& = {".root", ".xml"});
216 
222  template<typename T> Options& arg (
223  const char * name,
224  const char * configpath,
225  const char * desc)
226  {
227  if (stage > Stage::Arg)
228  BOOST_THROW_EXCEPTION(std::runtime_error(
229  "Not possible to add another argument at this stage"));
230  stage = Stage::Arg;
231 
232  names.push_back(name);
233  configpaths.push_back(configpath);
234  const boost::program_options::value_semantic * s =
235  boost::program_options::value<T>()->notifier(put<T>(configpath));
236  return set(name, s, desc);
237  }
238 
243  Options& args (const char * name,
244  const char * configpath,
245  const char * desc
246  );
247 
251  const boost::property_tree::ptree& operator() (int, const char * const []);
252 
258  static std::string parse_env_var (std::string);
259 
262  static inline std::string parse_env_var (const char * p)
263  { return parse_env_var(std::string(p)); }
264 
267  static inline std::filesystem::path parse_env_var (const std::filesystem::path& p)
268  { return std::filesystem::path(parse_env_var(p.string())); }
269 
272  static void parse_config
273  (boost::property_tree::ptree&,
274  std::string = "");
275 
276  // result of parsing
277  static std::string full_cmd;
278 
281  auto commit () const { return m_commit; }
282 
285  inline int steering () const { return steer; }
286 
289  inline std::pair<unsigned, unsigned> slice () const
290  {
291  using namespace std;
292  if (!(params & split))
293  BOOST_THROW_EXCEPTION(invalid_argument("No splitting of the input file."));
294  return make_pair(j,k);
295  }
296 };
297 
298 } // 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:158
Darwin::Tools::Options::parse_env_var
static std::string parse_env_var(std::string)
Definition: Options.cc:75
Darwin::Tools::Options::steering
int steering() const
Steering information for running of executable.
Definition: Options.h:285
Darwin::Tools::Options::check_input
static void check_input(const std::filesystem::path &)
Definition: Options.cc:37
Darwin::Tools::Options::k
int k
slice index
Definition: Options.h:160
Darwin::Tools::Options::tutorial
const std::string tutorial
define in constructor, shown with option -t
Definition: Options.h:93
Darwin::Tools::Options::Options
Options()=delete
No default constructor is allowed.
Darwin::Tools::fill
@ fill
activate -f to fill the tree
Definition: Options.h:27
Darwin::Tools::Options::custom
boost::program_options::options_description custom
for positional arguments, depending on the actual command
Definition: Options.h:65
Darwin::Tools::Options::j
int j
Definition: Options.h:159
Darwin::Tools::Friend
@ Friend
activate -F to only fill the new branches
Definition: Options.h:28
Darwin::Tools::Options::allow_unregistered
bool allow_unregistered() const
Definition: Options.h:139
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:474
Darwin::Tools::Options::put
std::function< void(T)> put(const std::string &configpath)
Definition: Options.h:129
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:26
Darwin::Tools::Options::args
Options & args(const char *name, const char *configpath, const char *desc)
Definition: Options.cc:535
Ntupliser_cfg.p
p
Definition: Ntupliser_cfg.py:358
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:147
Darwin::Tools::Options::hidden
boost::program_options::options_description hidden
hidden interface (not for lambda user)
Definition: Options.h:62
Darwin::Tools::Options::params
const int params
input parameters to interpret explicit options
Definition: Options.h:155
Darwin::Tools::syst
@ syst
activate -s to systematic uncertainties
Definition: Options.h:29
Darwin::Tools::none
@ none
default (for simple executables)
Definition: Options.h:24
Darwin::Tools::Options::operator()
const boost::property_tree::ptree & operator()(int, const char *const [])
Definition: Options.cc:407
Darwin::Tools::Options::synopsis
std::string synopsis
stores a clean version of the command, displayed w. -h
Definition: Options.h:94
Darwin::Tools::Options::pos_hide
boost::program_options::positional_options_description pos_hide
parser for positional arguments
Definition: Options.h:67
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:157
Darwin::Tools::Options::commit
auto commit() const
Git commit information.
Definition: Options.h:281
Darwin::Tools::Options::Output
@ Output
then the outputs (inputs are no longer allowed)
Definition: Options.h:148
DARWIN_EXAMPLE
#define DARWIN_EXAMPLE
Definition: Options.h:18
Darwin::Tools::Options::parse_common
void parse_common(int, const char *const [])
Definition: Options.cc:268
DARWIN_GIT_COMMIT
#define DARWIN_GIT_COMMIT
Definition: Options.h:14
Darwin::Tools::Options::parse_helper
void parse_helper(int, const char *const [])
Definition: Options.cc:183
Darwin::Tools::Options::names
std::vector< std::string > names
names of the different options (shown in synopsis)
Definition: Options.h:96
Darwin::Tools::Options::inputExt
std::vector< std::vector< std::string > > inputExt
expected extensions of input files
Definition: Options.h:163
Darwin::Tools::verbose
@ verbose
bit for debug mode (-v is always available)
Definition: Options.h:30
Darwin::Tools::Options::slice
std::pair< unsigned, unsigned > slice() const
Compactify slice information into a pair.
Definition: Options.h:289
Darwin::Tools::Options::set
Options & set(const char *, const boost::program_options::value_semantic *, const char *)
Definition: Options.cc:434
Darwin::Tools::Options
Common class to interpret the command line, based on Boost Program Options.
Definition: Options.h:58
Darwin::Tools::Options::Args
@ Args
finally the remaining arguments / garbage collector (must be very last)
Definition: Options.h:150
Darwin::Tools::Options::arg
Options & arg(const char *name, const char *configpath, const char *desc)
Definition: Options.h:222
Darwin::Tools::Options::stage
Stage stage
internal stage
Definition: Options.h:153
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:267
Darwin::Tools::Options::Stage
Stage
Definition: Options.h:146
Darwin::Tools
Classes and functions related to the framework.
Definition: Dict_rdict.cxx:990
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:135
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:262
Darwin::Tools::Options::m_commit
const std::string m_commit
Commit SHA for –git.
Definition: Options.h:156
Darwin::Tools::Options::helper
boost::program_options::options_description helper
to display the helper
Definition: Options.h:63
Darwin::Tools::Options::parse_custom
void parse_custom(int, const char *const [])
Definition: Options.cc:317
Darwin::Tools::Options::config_file
std::filesystem::path config_file
path to INFO, JSON, or XML config file
Definition: Options.h:90
Darwin::Tools::Options::outputExt
std::vector< std::vector< std::string > > outputExt
expected extensions of output files
Definition: Options.h:164
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:518
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:447
Darwin::Tools::Options::exec
std::string exec(const std::string &)
Definition: Options.cc:460
Darwin::Tools::Mode
Mode
Definition: Options.h:23
Darwin::Tools::Options::Arg
@ Arg
then the registered arguments (inputs and outputs no longer allowed)
Definition: Options.h:149
Darwin::Tools::Options::configpaths
std::vector< std::string > configpaths
path to the options in config (except for I/O)
Definition: Options.h:97
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:277
Darwin::Tools::config
@ config
activate -c option to provide config file
Definition: Options.h:25
Darwin::Tools::Options::pt_conf
boost::property_tree::ptree pt_conf
internal config obtained from arugments and input config
Definition: Options.h:69
Darwin::Tools::Options::common
boost::program_options::options_description common
generic + explicit options like --verbose
Definition: Options.h:64
Darwin::Tools::Options::example
const std::filesystem::path example
path to example config
Definition: Options.h:71