artdaq  v3_09_01
artdaqapp.hh
1 #ifndef ARTDAQ_PROTO_ARTDAQAPP_HH
2 #define ARTDAQ_PROTO_ARTDAQAPP_HH
3 
4 #include "artdaq/DAQdata/Globals.hh"
5 
6 #include "artdaq-core/Utilities/configureMessageFacility.hh"
7 #include "artdaq-core/Utilities/ExceptionHandler.hh"
8 #include "artdaq/Application/BoardReaderApp.hh"
9 #include "artdaq/Application/DataLoggerApp.hh"
10 #include "artdaq/Application/DispatcherApp.hh"
11 #include "artdaq/Application/EventBuilderApp.hh"
12 #include "artdaq/Application/RoutingManagerApp.hh"
13 #include "artdaq/Application/TaskType.hh"
14 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
15 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
16 
17 #include <sys/prctl.h>
18 
19 namespace artdaq {
23 class artdaqapp
24 {
25 public:
29  struct Config
30  {
32  fhicl::Atom<std::string> application_name{fhicl::Name{"application_name"}, fhicl::Comment{"Name to use for metrics and logging"}, "BoardReader"};
34  fhicl::Atom<bool> replace_image_name{fhicl::Name{"replace_image_name"}, fhicl::Comment{"Replace the application image name with application_name"}, false};
36  fhicl::Atom<int> rank{fhicl::Name{"rank"}, fhicl::Comment{"The \"rank\" of the application, used for configuring data transfers"}};
37  fhicl::TableFragment<artdaq::CommanderInterface::Config> commanderPluginConfig;
38  fhicl::Atom<bool> auto_run{fhicl::Name{"auto_run"}, fhicl::Comment{"Whether to automatically start a run"}, false};
41  fhicl::Atom<int> run_number{fhicl::Name{"run_number"}, fhicl::Comment{"Run number to use for automatic run"}, 101};
43  fhicl::Atom<uint64_t> transition_timeout{fhicl::Name{"transition_timeout"}, fhicl::Comment{"Timeout to use for automatic transitions"}, 30};
44  fhicl::TableFragment<artdaq::PortManager::Config> portsConfig;
45  };
47  using Parameters = fhicl::WrappedTable<Config>;
48 
54  static void runArtdaqApp(detail::TaskType task, fhicl::ParameterSet const& config_ps)
55  {
56  app_name = config_ps.get<std::string>("application_name", detail::TaskTypeToString(task));
57  portMan->UpdateConfiguration(config_ps);
58 
59  if (config_ps.get<bool>("replace_image_name", config_ps.get<bool>("rin", false)))
60  {
61  int s;
62  s = prctl(PR_SET_NAME, app_name.c_str(), NULL, NULL, NULL);
63  if (s != 0)
64  {
65  std::cerr << "Could not replace process image name with " << app_name << "!" << std::endl;
66  exit(1);
67  }
68  }
69 
70  std::string mf_app_name = artdaq::setMsgFacAppName(app_name, config_ps.get<int>("id"));
71  try
72  {
73  artdaq::configureMessageFacility(mf_app_name.c_str());
74  }
75  catch (cet::exception const&)
76  {
77  ExceptionHandler(ExceptionHandlerRethrow::yes, "Exception occurred while setting up MessageFacility");
78  }
79 
80  if (config_ps.has_key("rank"))
81  {
82  my_rank = config_ps.get<int>("rank");
83  }
84  TLOG_DEBUG(app_name + "Main") << "Setting application name to " << app_name;
85 
86  // 23-May-2018, KAB: added lookup of the partition number from the command line arguments.
87  if (config_ps.has_key("partition_number"))
88  {
89  artdaq::Globals::partition_number_ = config_ps.get<int>("partition_number");
90  }
91  TLOG_DEBUG(app_name + "Main") << "Setting partition number to " << artdaq::Globals::partition_number_;
92 
93  TLOG_DEBUG(app_name + "Main") << "artdaq version " << artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
94  << ", built " << artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
95 
96  artdaq::setMsgFacAppName(app_name, config_ps.get<int>("id"));
97 
98  std::unique_ptr<artdaq::Commandable> comm(nullptr);
99  switch (task)
100  {
101  case (detail::BoardReaderTask):
102  comm = std::make_unique<BoardReaderApp>();
103  break;
104  case (detail::EventBuilderTask):
105  comm = std::make_unique<EventBuilderApp>();
106  break;
107  case (detail::DataLoggerTask):
108  comm = std::make_unique<DataLoggerApp>();
109  break;
110  case (detail::DispatcherTask):
111  comm = std::make_unique<DispatcherApp>();
112  break;
113  case (detail::RoutingManagerTask):
114  comm = std::make_unique<RoutingManagerApp>();
115  break;
116  default:
117  return;
118  }
119 
120  auto auto_run = config_ps.get<bool>("auto_run", false);
121  if (auto_run)
122  {
123  auto run = config_ps.get<int>("run_number", 101);
124  auto timeout = config_ps.get<uint64_t>("transition_timeout", 30);
125  uint64_t timestamp = 0;
126 
127  comm->do_initialize(config_ps, timeout, timestamp);
128  comm->do_start(art::RunID(run), timeout, timestamp);
129 
130  TLOG_INFO(app_name + "Main") << "Running XMLRPC Commander. To stop, either Control-C or " << std::endl
131  << "xmlrpc http://`hostname`:" << config_ps.get<int>("id") << "/RPC2 daq.stop" << std::endl
132  << "xmlrpc http://`hostname`:" << config_ps.get<int>("id") << "/RPC2 daq.shutdown";
133  }
134 
135  auto commander = artdaq::MakeCommanderPlugin(config_ps, *comm);
136  commander->run_server();
138  }
139 };
140 } // namespace artdaq
141 
142 #endif // ARTDAQ_PROTO_ARTDAQAPP_HH
virtual bool do_start(art::RunID, uint64_t, uint64_t)
Perform the start transition.
Definition: Commandable.cc:353
fhicl::Atom< int > run_number
&quot;run_number&quot; (Default: 101): Run number to use for automatic run
Definition: artdaqapp.hh:41
fhicl::Atom< bool > auto_run
&quot;auto_run&quot; (Default: false): Whether to automatically start a run
Definition: artdaqapp.hh:39
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:150
static artdaq::PackageBuildInfo getPackageBuildInfo()
Gets the version number and build timestmap for artdaq.
Class representing an artdaq application. Used by all &quot;main&quot; functions to start artdaq.
Definition: artdaqapp.hh:23
fhicl::TableFragment< artdaq::PortManager::Config > portsConfig
Configuration for artdaq Ports.
Definition: artdaqapp.hh:44
fhicl::Atom< uint64_t > transition_timeout
&quot;transition_timeout&quot; (Default: 30): Timeout to use for automatic transitions
Definition: artdaqapp.hh:43
fhicl::Atom< std::string > application_name
&quot;application_name&quot; (Default: artdaq::detail::TaskTypeToString(task)): Name to use for metrics and log...
Definition: artdaqapp.hh:32
std::unique_ptr< artdaq::CommanderInterface > MakeCommanderPlugin(const fhicl::ParameterSet &commander_pset, artdaq::Commandable &commandable)
Load a CommanderInterface plugin.
std::string TaskTypeToString(TaskType const &task)
Convert a TaskType to string representation
Definition: TaskType.hh:66
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
Definition: artdaqapp.hh:47
fhicl::TableFragment< artdaq::CommanderInterface::Config > commanderPluginConfig
Definition: artdaqapp.hh:37
fhicl::Atom< int > rank
&quot;rank&quot;: The &quot;rank&quot; of the application, used for configuring data transfers
Definition: artdaqapp.hh:36
static void runArtdaqApp(detail::TaskType task, fhicl::ParameterSet const &config_ps)
Run an artdaq Application
Definition: artdaqapp.hh:54
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:39
fhicl::Atom< bool > replace_image_name
&quot;replace_image_name&quot; (Default: false): Replace the application image name with application_name ...
Definition: artdaqapp.hh:34
Configuration of artdaqapp. May be used for parameter validation
Definition: artdaqapp.hh:29
virtual bool do_initialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the initialize transition.
Definition: Commandable.cc:346
TaskType
The types of applications in artdaq.
Definition: TaskType.hh:17