CbmRoot
Loading...
Searching...
No Matches
reco/app/cbmreco_fairrun/ProgramOptions.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 Johann Wolfgang Goethe-Universitaet Frankfurt, Frankfurt am Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Jan de Cuveland [committer] */
4
5#include "ProgramOptions.h"
6
7#include "log.hpp"
8
9#include <boost/program_options.hpp>
10
11#include <fstream>
12#include <iostream>
13
14namespace po = boost::program_options;
15
16void ProgramOptions::ParseOptions(int argc, char* argv[])
17{
18 unsigned log_level = 2;
19 unsigned log_syslog = 2;
20 std::string log_file;
21 std::string options_file;
22
23 // --- Define generic options
24 po::options_description generic("Generic options");
25 auto generic_add = generic.add_options();
26 generic_add("options-file,f", po::value<std::string>(&options_file)->value_name("<filename>"),
27 "read program options from file");
28 generic_add("log-level,l", po::value<unsigned>(&log_level)->default_value(log_level)->value_name("<n>"),
29 "set the file log level (all:0)");
30 generic_add("log-file,L", po::value<std::string>(&log_file)->value_name("<filename>"), "write log output to file");
31 generic_add("log-syslog,S", po::value<unsigned>(&log_syslog)->implicit_value(log_syslog)->value_name("<n>"),
32 "enable logging to syslog at given log level");
33 generic_add(
34 "monitor,m",
35 po::value<std::string>(&fMonitorUri)->value_name("<uri>")->implicit_value("influx1:login:8086:cbmreco_status"),
36 "publish program status to InfluxDB (or \"file:cout\" for "
37 "console output)");
38 generic_add("help,h", "display this help and exit");
39 generic_add("version,V", "output version information and exit");
40
41 // --- Define configuration options
42 po::options_description config("Configuration");
43 auto config_add = config.add_options();
44 config_add(
45 "input,i",
46 po::value<std::vector<std::string>>()->multitoken()->value_name("scheme://host/path?param=value ... | <filename>"),
47 "uri of a timeslice source");
48 config_add("output-root,o",
49 po::value<std::string>(&fOutputRootFile)->default_value(fOutputRootFile)->value_name("<filename>"),
50 "name of an output root file to write");
51 config_add("config,c", po::value<std::string>(&fConfigYamlFile)->value_name("<filename>"),
52 "name of a yaml config file containing the reconstruction chain configuration");
53 config_add("save-config", po::value<std::string>(&fSaveConfigYamlFile)->value_name("<filename>"),
54 "save configuration to yaml file (mostly for debugging)");
55 config_add("dump-setup", po::bool_switch(&fDumpSetup), "dump the readout setup to yaml");
56 config_add("max-timeslice-number,n", po::value<int32_t>(&fMaxNumTs)->value_name("<n>"),
57 "quit after processing given number of timeslices (default: unlimited)");
58 config_add("http-server-port,p", po::value<uint16_t>(&fHttpServerPort)->value_name("<n>"),
59 "port number for the HTTP server. If 0, server will not be activated (default)");
60
61 po::options_description cmdline_options("Allowed options");
62 cmdline_options.add(generic).add(config);
63
64 po::variables_map vm;
65 po::store(po::parse_command_line(argc, argv, cmdline_options), vm);
66 po::notify(vm);
67
68 // --- Read program options from file if requested
69 if (!options_file.empty()) {
70 std::ifstream ifs(options_file.c_str());
71 if (!ifs) {
72 throw ProgramOptionsException("cannot open options file: " + options_file);
73 }
74 po::store(po::parse_config_file(ifs, config), vm);
75 notify(vm);
76 }
77
78 if (vm.count("help") != 0u) {
79 std::cout << cmdline_options << std::endl;
80 exit(EXIT_SUCCESS);
81 }
82
83 if (vm.count("version") != 0u) {
84 std::cout << "cbmreco, version (unspecified)" << std::endl;
85 exit(EXIT_SUCCESS);
86 }
87
88 // --- Set up logging
89 logging::add_console(static_cast<severity_level>(log_level));
90 if (vm.count("log-file") != 0u) {
91 L_(info) << "Logging output to " << log_file;
92 logging::add_file(log_file, static_cast<severity_level>(log_level));
93 }
94 if (vm.count("log-syslog") != 0u) {
95 logging::add_syslog(logging::syslog::local0, static_cast<severity_level>(log_syslog));
96 }
97
98 if (vm.count("input") == 0u) {
99 throw ProgramOptionsException("no input source specified");
100 }
101 fInputUri = vm["input"].as<std::vector<std::string>>();
102
103 if (vm.count("config") == 0u) {
104 throw ProgramOptionsException("no configuration file specified");
105 }
106
107 L_(debug) << "input sources (" << fInputUri.size() << "):";
108 for (auto& inputUri : fInputUri) {
109 L_(debug) << " " << inputUri;
110 }
111}
#define L_(level)
std::vector< std::string > fInputUri
URI(s) specifying input timeslice stream source(s)
std::string fConfigYamlFile
Configuration file name (.yaml format)
uint16_t fHttpServerPort
Port number for the HTTP server. If 0, server will not be activated.
std::string fMonitorUri
URI specifying the monitoring server interface.
std::string fSaveConfigYamlFile
Save configuration file name (.yaml format)
std::string fOutputRootFile
Output file name (.root format)
bool fDumpSetup
Dump the readout setup to yaml.
void ParseOptions(int argc, char *argv[])
Parse command line arguments using boost program_options.
int32_t fMaxNumTs
Maximum number of timeslices to process.