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