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