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