00001 #ifndef artdaq_proto_LoadParameterSet_hh
00002 #define artdaq_proto_LoadParameterSet_hh 1
00003
00004 #include "tracemf.h"
00005 #include "fhiclcpp/make_ParameterSet.h"
00006 #include <boost/program_options.hpp>
00007 #include <iostream>
00008 namespace bpo = boost::program_options;
00009
00010 fhicl::ParameterSet LoadParameterSet(std::string const& psetOrFile)
00011 {
00012 fhicl::ParameterSet pset;
00013
00014 try
00015 {
00016 make_ParameterSet(psetOrFile, pset);
00017 }
00018 catch (fhicl::exception e)
00019 {
00020 if (getenv("FHICL_FILE_PATH") == nullptr)
00021 setenv("FHICL_FILE_PATH", ".", 0);
00022 cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
00023 make_ParameterSet(psetOrFile, lookup_policy, pset);
00024 }
00025
00026 return pset;
00027 }
00028
00029 fhicl::ParameterSet LoadParameterSet(int argc, char* argv[])
00030 {
00031 std::ostringstream descstr;
00032 descstr << argv[0]
00033 << " <-c <config>> <other-options> [<source-file>]+";
00034 bpo::options_description desc(descstr.str());
00035 desc.add_options()
00036 ("config,c", bpo::value<std::string>(), "Configuration")
00037 ("help,h", "produce help message");
00038 bpo::variables_map vm;
00039 try
00040 {
00041 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
00042 bpo::notify(vm);
00043 }
00044 catch (bpo::error const & e)
00045 {
00046 TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0]
00047 << ": " << e.what() << "\n";
00048 exit(-1);
00049 }
00050 if (vm.count("help"))
00051 {
00052 TLOG_INFO("LoadParameterSet") << desc << std::endl;
00053 exit(1);
00054 }
00055
00056
00057 fhicl::ParameterSet pset;
00058
00059 if (vm.count("config"))
00060 {
00061 std::string config = vm["config"].as<std::string>();
00062
00063 if (config == "-" || config == "--")
00064 {
00065 TLOG_ERROR("LoadParameterSet") << "Reading configuration from standard input. Press Ctrl-D to end" << std::endl;
00066 std::stringstream ss;
00067 std::string line;
00068 while (std::getline(std::cin, line))
00069 {
00070 ss << line << std::endl;
00071 }
00072 std::cin.clear();
00073
00074 make_ParameterSet(ss.str(), pset);
00075 }
00076 else
00077 {
00078 TLOG_DEBUG("LoadParameterSet") << config << std::endl;
00079 pset = LoadParameterSet(config);
00080 }
00081 }
00082 else
00083 {
00084 TLOG_ERROR("LoadParameterSet") << "Exception from command line processing in " << argv[0]
00085 << ": no configuration given.\n"
00086 << "For usage and an options list, please do '"
00087 << argv[0] << " --help"
00088 << "'.\n";
00089 exit(2);
00090 }
00091 return pset;
00092 }
00093 #endif //artdaq_proto_LoadParameterSet_hh