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