artdaq  v2_02_03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
driver.cc
1 //
2 // artdaqDriver is a program for testing the behavior of the generic
3 // RawInput source. Run 'artdaqDriver --help' to get a description of the
4 // expected command-line parameters.
5 //
6 //
7 // The current version generates simple data fragments, for testing
8 // that data are transmitted without corruption from the
9 // artdaq::EventStore through to the artdaq::RawInput source.
10 //
11 
12 #include "art/Framework/Art/artapp.h"
13 #include "artdaq-core/Generators/FragmentGenerator.hh"
14 #include "artdaq-core/Data/Fragment.hh"
15 #include "artdaq-core/Utilities/ExceptionHandler.hh"
16 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
17 
18 #include "artdaq/DAQdata/Globals.hh"
19 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
20 #include "artdaq/Application/makeCommandableFragmentGenerator.hh"
21 #include "artdaq-utilities/Plugins/MetricManager.hh"
22 #include "artdaq/DAQrate/EventStore.hh"
23 #include "artdaq-core/Core/SimpleQueueReader.hh"
24 #include "cetlib/container_algorithms.h"
25 #include "cetlib/filepath_maker.h"
26 #include "fhiclcpp/ParameterSet.h"
27 #include "fhiclcpp/make_ParameterSet.h"
28 #include "boost/program_options.hpp"
29 
30 #include <signal.h>
31 #include <iostream>
32 #include <memory>
33 #include <utility>
34 
35 using namespace std;
36 using namespace fhicl;
37 namespace bpo = boost::program_options;
38 
39 volatile int events_to_generate;
40 void sig_handler(int) { events_to_generate = -1; }
41 
42 template<typename B, typename D>
43 std::unique_ptr<D>
44 dynamic_unique_ptr_cast(std::unique_ptr<B>& p);
45 
46 int main(int argc, char * argv[]) try
47 {
48  std::ostringstream descstr;
49  descstr << argv[0]
50  << " <-c <config-file>> <other-options> [<source-file>]+";
51  bpo::options_description desc(descstr.str());
52  desc.add_options()
53  ("config,c", bpo::value<std::string>(), "Configuration file.")
54  ("help,h", "produce help message");
55  bpo::variables_map vm;
56  try {
57  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
58  bpo::notify(vm);
59  }
60  catch (bpo::error const & e) {
61  std::cerr << "Exception from command line processing in " << argv[0]
62  << ": " << e.what() << "\n";
63  return -1;
64  }
65  if (vm.count("help")) {
66  std::cout << desc << std::endl;
67  return 1;
68  }
69  if (!vm.count("config")) {
70  std::cerr << "Exception from command line processing in " << argv[0]
71  << ": no configuration file given.\n"
72  << "For usage and an options list, please do '"
73  << argv[0] << " --help"
74  << "'.\n";
75  return 2;
76  }
77  ParameterSet pset;
78  if (getenv("FHICL_FILE_PATH") == nullptr) {
79  std::cerr
80  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
81  setenv("FHICL_FILE_PATH", ".", 0);
82  }
83  cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
84  make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
85 
86  int run = pset.get<int>("run_number", 1);
87  uint64_t timeout = pset.get<uint64_t>("transition_timeout", 30);
88  uint64_t timestamp = 0;
89 
90  ParameterSet fragment_receiver_pset = pset.get<ParameterSet>("fragment_receiver");
91 
92  std::unique_ptr<artdaq::FragmentGenerator>
93  gen(artdaq::makeFragmentGenerator(fragment_receiver_pset.get<std::string>("generator"),
94  fragment_receiver_pset));
95 
96  std::unique_ptr<artdaq::CommandableFragmentGenerator> commandable_gen =
97  dynamic_unique_ptr_cast<artdaq::FragmentGenerator, artdaq::CommandableFragmentGenerator>(gen);
98 
99  artdaq::configureMessageFacility("artdaqDriver");
100  artdaq::MetricManager metricMan_;
101  metricMan = &metricMan_;
102  my_rank = 0;
103  // pull out the Metric part of the ParameterSet
104  fhicl::ParameterSet metric_pset;
105  try {
106  metric_pset = pset.get<fhicl::ParameterSet>("metrics");
107  }
108  catch (...) {} // OK if there's no metrics table defined in the FHiCL
109 
110  if (metric_pset.is_empty()) {
111  TLOG_INFO("artdaqDriver") << "No metric plugins appear to be defined" << TLOG_ENDL;
112  }
113  try {
114  metricMan_.initialize(metric_pset, "artdaqDriver");
115  metricMan_.do_start();
116  }
117  catch (...) {
118  }
119  artdaq::FragmentPtrs frags;
121  // Note: we are constrained to doing all this here rather than
122  // encapsulated neatly in a function due to the lieftime issues
123  // associated with async threads and std::string::c_str().
124  ParameterSet event_builder_pset = pset.get<ParameterSet>("event_builder");
125  bool const want_artapp(event_builder_pset.get<bool>("use_art", false));
126  std::ostringstream os;
127  if (!want_artapp) {
128  os << event_builder_pset.get<int>("events_expected_in_SimpleQueueReader");
129  }
130  std::string const oss(os.str());
131  const char * args[2]{ "SimpleQueueReader", oss.c_str() };
132  int es_argc(want_artapp ? argc : 2);
133  char **es_argv(want_artapp ? argv : const_cast<char**>(args));
135  es_fcn(want_artapp ? &artapp : &artdaq::simpleQueueReaderApp);
136  artdaq::EventStore store(event_builder_pset, event_builder_pset.get<size_t>("expected_fragments_per_event"),
137  pset.get<artdaq::EventStore::run_id_t>("run_number"),
138  es_argc,
139  es_argv,
140  es_fcn);
142 
143  int events_to_generate = pset.get<int>("events_to_generate", 0);
144  int event_count = 0;
145  artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
146 
147  if (commandable_gen) {
148  commandable_gen->StartCmd(run, timeout, timestamp);
149  }
150 
151  TRACE( 50, "driver main before store.startRun" );
152  store.startRun( run );
153 
154  // Read or generate fragments as rapidly as possible, and feed them
155  // into the EventStore. The throughput resulting from this design
156  // choice is likely to have the fragment reading (or generation)
157  // speed as the limiting factor
158  while ((commandable_gen && commandable_gen->getNext(frags)) ||
159  (gen && gen->getNext(frags))) {
160  TRACE( 50, "driver main: getNext returned frags.size()=%zd current event_count=%d"
161  ,frags.size(),event_count );
162  for (auto & val : frags) {
163  if (val->sequenceID() != previous_sequence_id) {
164  ++event_count;
165  previous_sequence_id = val->sequenceID();
166  }
167  if (events_to_generate != 0 && event_count > events_to_generate) {
168  if (commandable_gen) {
169  commandable_gen->StopCmd(timeout, timestamp);
170  }
171  break;
172  }
173  store.insert(std::move(val));
174  }
175  frags.clear();
176 
177  if (events_to_generate != 0 && event_count >= events_to_generate) {
178  if (commandable_gen) {
179  commandable_gen->StopCmd(timeout, timestamp);
180  }
181  break;
182  }
183  }
184 
185 
186  int readerReturnValue;
187  bool endSucceeded = false;
188  int attemptsToEnd = 1;
189  endSucceeded = store.endOfData(readerReturnValue);
190  while (!endSucceeded && attemptsToEnd < 3) {
191  ++attemptsToEnd;
192  endSucceeded = store.endOfData(readerReturnValue);
193  }
194  if (!endSucceeded) {
195  std::cerr << "Failed to shut down the reader and the event store "
196  << "because the endOfData marker could not be pushed "
197  << "onto the queue." << std::endl;
198  }
199 
200  metricMan_.do_stop();
201  return readerReturnValue;
202 }
203 catch (std::string & x)
204 {
205  cerr << "Exception (type string) caught in artdaqDriver: " << x << '\n';
206  return 1;
207 }
208 catch (char const * m)
209 {
210  cerr << "Exception (type char const*) caught in artdaqDriver: ";
211  if (m)
212  {
213  cerr << m;
214  }
215  else
216  {
217  cerr << "[the value was a null pointer, so no message is available]";
218  }
219  cerr << '\n';
220 }
221 catch (...) {
222  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
223  "Exception caught in artdaqDriver");
224 }
225 
226 
227 template<typename B, typename D>
228 std::unique_ptr<D>
229 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
230 {
231  D* result = dynamic_cast<D*>(p.get());
232 
233  if (result) {
234  p.release();
235  return std::unique_ptr<D>(result);
236  }
237  return nullptr;
238 }
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
void configureMessageFacility(char const *progname, bool useConsole=true)
Configure and start the message facility. Provide the program name so that messages will be appropria...
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
The EventStore class collects Fragment objects, until it receives a complete event, at which point the event is handed over to the art thread.
Definition: EventStore.hh:48
int( ART_CMDLINE_FCN)(int, char **)
An art function that accepts standard C main arguments.
Definition: EventStore.hh:55
RawEvent::run_id_t run_id_t
Copy RawEvent::run_id_t into local scope.
Definition: EventStore.hh:63