artdaq  v3_12_02
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 "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 inline fhicl::ParameterSet LoadParameterSet(std::string const& psetOrFile)
21 {
22  fhicl::ParameterSet pset;
23 
24  try
25  {
26  pset = fhicl::ParameterSet::make(psetOrFile);
27  }
28  catch (const fhicl::exception& e)
29  {
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);
34  }
35 
36  return pset;
37 }
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 inline fhicl::ParameterSet LoadParameterSet(int argc, char* argv[], std::string const& name, std::string const& description)
48 {
49  std::ostringstream descstr;
50  descstr << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
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;
55  try
56  {
57  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
58  bpo::notify(vm);
59  }
60  catch (bpo::error const& e)
61  {
62  TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
63  << ": " << e.what() << "\n";
64  exit(-1);
65  }
66  if (vm.count("help"))
67  {
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);
73  exit(1);
74  }
75 
76  fhicl::ParameterSet pset;
77 
78  if (vm.count("config"))
79  {
80  std::string config = vm["config"].as<std::string>();
81 
82  if (config == "-" || config == "--")
83  {
84  TLOG_ERROR("LoadParameterSet") << "Reading configuration from standard input. Press Ctrl-D to end" << std::endl;
85  std::stringstream ss;
86  std::string line;
87  while (std::getline(std::cin, line))
88  {
89  ss << line << std::endl;
90  }
91  std::cin.clear();
92 
93  pset = fhicl::ParameterSet::make(ss.str());
94  }
95  else
96  {
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); }
100  else
101  {
102  pset = pset_tmp;
103  }
104  }
105  }
106  else
107  {
108  TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0] // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
109  << ": no configuration given.\n"
110  << "For usage and an options list, please do '"
111  << argv[0] << " --help" // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
112  << "'.\n";
113  exit(2);
114  }
115  return pset;
116 }
117 #endif // artdaq_proto_LoadParameterSet_hh