1 #ifndef artdaq_proto_LoadParameterSet_hh
2 #define artdaq_proto_LoadParameterSet_hh 1
5 #include "TRACE/tracemf.h"
7 #include "fhiclcpp/ParameterSet.h"
8 #include "fhiclcpp/types/Name.h"
9 #include "fhiclcpp/types/Table.h"
11 #include "cetlib/filepath_maker.h"
13 #include <boost/program_options.hpp>
14 namespace bpo = boost::program_options;
20 inline fhicl::ParameterSet LoadParameterSet(std::string
const& psetOrFile)
22 fhicl::ParameterSet pset;
26 pset = fhicl::ParameterSet::make(psetOrFile);
28 catch (
const fhicl::exception& e)
30 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
31 setenv(
"FHICL_FILE_PATH",
".", 0);
32 cet::filepath_lookup_after1 lookup_policy(
"FHICL_FILE_PATH");
33 pset = fhicl::ParameterSet::make(psetOrFile, lookup_policy);
40 void PrintConfigurationToConsole(std::string
const& name)
42 fhicl::Table<C> config_description(fhicl::Name{name});
43 config_description.print_allowed_configuration(std::cout);
47 inline fhicl::ParameterSet LoadParameterSet(
int argc,
char* argv[], std::string
const& name, std::string
const& description)
49 std::ostringstream descstr;
51 <<
" <-c <config>> <other-options> [<source-file>]+";
52 bpo::options_description desc(descstr.str());
53 desc.add_options()(
"config,c", bpo::value<std::string>(),
"Configuration")(
"help,h",
"produce help message");
54 bpo::variables_map vm;
57 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
60 catch (bpo::error
const& e)
62 TLOG_ERROR(
"LoadParameterSet") <<
"Exception from command line processing in " << argv[0]
63 <<
": " << e.what() <<
"\n";
68 std::cout << desc << std::endl;
69 std::cout << description << std::endl;
70 std::cout <<
"Sample FHiCL configuration for this application: " << std::endl;
71 fhicl::Table<C> config_description(fhicl::Name{name});
72 config_description.print_allowed_configuration(std::cout);
76 fhicl::ParameterSet pset;
78 if (vm.count(
"config"))
80 std::string config = vm[
"config"].as<std::string>();
82 if (config ==
"-" || config ==
"--")
84 TLOG_ERROR(
"LoadParameterSet") <<
"Reading configuration from standard input. Press Ctrl-D to end" << std::endl;
87 while (std::getline(std::cin, line))
89 ss << line << std::endl;
93 pset = fhicl::ParameterSet::make(ss.str());
97 TLOG(TLVL_DEBUG + 32,
"LoadParameterSet") << config << std::endl;
98 auto pset_tmp = LoadParameterSet(config);
99 if (pset_tmp.has_key(name)) { pset = pset_tmp.get<fhicl::ParameterSet>(name); }
108 TLOG_ERROR(
"LoadParameterSet") <<
"Exception from command line processing in " << argv[0]
109 <<
": no configuration given.\n"
110 <<
"For usage and an options list, please do '"
111 << argv[0] <<
" --help"
117 #endif // artdaq_proto_LoadParameterSet_hh