Line data Source code
1 : #ifndef artdaq_proto_LoadParameterSet_hh
2 : #define artdaq_proto_LoadParameterSet_hh 1
3 :
4 : // note: in header files (this LoadParameterSet.hh) consider if you want TRACE/LOG to use "name" from .cc or name for this file
5 : #include "TRACE/tracemf.h"
6 :
7 : #include "fhiclcpp/ParameterSet.h"
8 : #include "fhiclcpp/types/Name.h"
9 : #include "fhiclcpp/types/Table.h"
10 :
11 : #include "cetlib/filepath_maker.h"
12 :
13 : #include <boost/program_options.hpp>
14 : namespace bpo = boost::program_options;
15 :
16 : #include <iostream>
17 : #include <sstream>
18 : #include <string>
19 :
20 3 : inline fhicl::ParameterSet LoadParameterSet(std::string const& psetOrFile)
21 : {
22 3 : fhicl::ParameterSet pset;
23 :
24 : try
25 : {
26 3 : pset = fhicl::ParameterSet::make(psetOrFile);
27 : }
28 3 : catch (const fhicl::exception& e)
29 : {
30 3 : if (getenv("FHICL_FILE_PATH") == nullptr)
31 0 : setenv("FHICL_FILE_PATH", ".", 0);
32 3 : cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
33 3 : pset = fhicl::ParameterSet::make(psetOrFile, lookup_policy);
34 3 : }
35 :
36 3 : return pset;
37 0 : }
38 :
39 : template<typename C>
40 : void PrintConfigurationToConsole(std::string const& name)
41 : {
42 : fhicl::Table<C> config_description(fhicl::Name{name});
43 : config_description.print_allowed_configuration(std::cout);
44 : }
45 :
46 : template<typename C>
47 3 : inline fhicl::ParameterSet LoadParameterSet(int argc, char* argv[], std::string const& name, std::string const& description)
48 : {
49 3 : std::ostringstream descstr;
50 : descstr << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
51 3 : << " <-c <config>> <other-options> [<source-file>]+";
52 3 : bpo::options_description desc(descstr.str());
53 3 : desc.add_options()("config,c", bpo::value<std::string>(), "Configuration")("help,h", "produce help message");
54 3 : bpo::variables_map vm;
55 : try
56 : {
57 3 : bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
58 3 : bpo::notify(vm);
59 : }
60 0 : catch (bpo::error const& e)
61 : {
62 0 : TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
63 0 : << ": " << e.what() << "\n";
64 0 : exit(-1);
65 : }
66 6 : if (vm.count("help"))
67 : {
68 0 : std::cout << desc << std::endl;
69 0 : std::cout << description << std::endl;
70 0 : std::cout << "Sample FHiCL configuration for this application: " << std::endl;
71 0 : fhicl::Table<C> config_description(fhicl::Name{name});
72 0 : config_description.print_allowed_configuration(std::cout);
73 0 : exit(1);
74 0 : }
75 :
76 3 : fhicl::ParameterSet pset;
77 :
78 6 : if (vm.count("config"))
79 : {
80 3 : std::string config = vm["config"].as<std::string>();
81 :
82 3 : if (config == "-" || config == "--")
83 : {
84 0 : TLOG_ERROR("LoadParameterSet") << "Reading configuration from standard input. Press Ctrl-D to end" << std::endl;
85 0 : std::stringstream ss;
86 0 : std::string line;
87 0 : while (std::getline(std::cin, line))
88 : {
89 0 : ss << line << std::endl;
90 : }
91 0 : std::cin.clear();
92 :
93 0 : pset = fhicl::ParameterSet::make(ss.str());
94 0 : }
95 : else
96 : {
97 6 : TLOG(TLVL_DEBUG + 32, "LoadParameterSet") << config << std::endl;
98 3 : auto pset_tmp = LoadParameterSet(config);
99 3 : if (pset_tmp.has_key(name)) { pset = pset_tmp.get<fhicl::ParameterSet>(name); }
100 : else
101 : {
102 3 : pset = pset_tmp;
103 : }
104 3 : }
105 3 : }
106 : else
107 : {
108 0 : TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
109 0 : << ": no configuration given.\n"
110 0 : << "For usage and an options list, please do '"
111 0 : << argv[0] << " --help" // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
112 0 : << "'.\n";
113 0 : exit(2);
114 : }
115 6 : return pset;
116 3 : }
117 : #endif // artdaq_proto_LoadParameterSet_hh
|