artdaq  v3_02_01
genToArt.cc
1 // genToArt
3 //
4 // This application is intended to invoke a configurable set of fragment
5 // generators, and funnel the result to an invocation of art. This is
6 // not an MPI application (see caveat below), so is not intended for
7 // high performance production DAQ scenarios -- for that, see the pmt
8 // application driver and its associated applcations boardreader and
9 // eventbuilder.
11 
12 #define TRACE_NAME "genToArt"
13 
14 #include "art/Framework/Art/artapp.h"
15 #include "canvas/Utilities/Exception.h"
16 #include "artdaq-core/Generators/FragmentGenerator.hh"
17 #include "artdaq-core/Data/Fragment.hh"
18 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
19 #include "artdaq/Application/CommandableFragmentGenerator.hh"
20 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
21 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
22 #include "artdaq-core/Core/SimpleMemoryReader.hh"
23 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
24 #include "cetlib/container_algorithms.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "fhiclcpp/make_ParameterSet.h"
27 
28 #include <boost/program_options.hpp>
29 
30 #include <deque>
31 #include <iostream>
32 #include <map>
33 #include <memory>
34 #include <string>
35 #include <utility>
36 
37 namespace bpo = boost::program_options;
38 
39 namespace
40 {
48  int process_cmd_line(int argc, char** argv,
49  bpo::variables_map& vm)
50  {
51  std::ostringstream descstr;
52  descstr << argv[0]
53  << " <-c <config-file>> <other-options> [<source-file>]+";
54  bpo::options_description desc(descstr.str());
55  desc.add_options()
56  ("config,c", bpo::value<std::string>(), "Configuration file.")
57  ("help,h", "produce help message");
58  try
59  {
60  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
61  bpo::notify(vm);
62  }
63  catch (bpo::error const& e)
64  {
65  TLOG(TLVL_ERROR) << "Exception from command line processing in " << argv[0]
66  << ": " << e.what();
67  return -1;
68  }
69  if (vm.count("help"))
70  {
71  std::cout << desc << std::endl;
72  return 1;
73  }
74  if (!vm.count("config"))
75  {
76  TLOG(TLVL_ERROR) << "Exception from command line processing in " << argv[0]
77  << ": no configuration file given.\n"
78  << "For usage and an options list, please do '"
79  << argv[0] << " --help"
80  << "'.";
81  return 2;
82  }
83  return 0;
84  }
85 
91  {
92  public:
98  ThrottledGenerator(std::string const& generator,
99  fhicl::ParameterSet const& ps);
100 
106  bool getNext(artdaq::FragmentPtrs& newFrags);
107 
112  size_t numFragIDs() const;
113 
120  void start(int run, uint64_t timeout, uint64_t timestamp) const
121  {
122  auto gen_ptr = dynamic_cast<artdaq::CommandableFragmentGenerator*>(generator_.get());
123  if (gen_ptr != nullptr) gen_ptr->StartCmd(run, timeout, timestamp);
124  }
130  void stop(uint64_t timeout, uint64_t timestamp) const
131  {
132  auto gen_ptr = dynamic_cast<artdaq::CommandableFragmentGenerator*>(generator_.get());
133  if (gen_ptr != nullptr) gen_ptr->StopCmd(timeout, timestamp);
134  }
135 
136  private:
137  bool generateFragments_();
138 
139  std::unique_ptr<artdaq::FragmentGenerator> generator_;
140  size_t const numFragIDs_;
141  std::map<artdaq::Fragment::fragment_id_t,
142  std::deque<artdaq::FragmentPtr>> frags_;
143  };
144 
145  ThrottledGenerator::
146  ThrottledGenerator(std::string const& generator,
147  fhicl::ParameterSet const& ps)
148  :
149  generator_(artdaq::makeFragmentGenerator(generator, ps))
150  , numFragIDs_(generator_->fragmentIDs().size())
151  , frags_()
152  {
153  assert(generator_);
154  }
155 
156  bool
158  getNext(artdaq::FragmentPtrs& newFrags)
159  {
160  if (frags_.size() && frags_.begin()->second.size())
161  { // Something stored.
162  for (auto& fQp : frags_)
163  {
164  assert(fQp.second.size());
165  newFrags.emplace_back(std::move(fQp.second.front()));
166  fQp.second.pop_front();
167  }
168  }
169  else
170  { // Need fresh fragments.
171  return generateFragments_() && getNext(newFrags);
172  }
173  return true;
174  }
175 
176  bool
177  ThrottledGenerator::
178  generateFragments_()
179  {
180  artdaq::FragmentPtrs incomingFrags;
181  bool result{ false };
182  while ((result = generator_->getNext(incomingFrags)) &&
183  incomingFrags.empty())
184  {
185  }
186  for (auto&& frag : incomingFrags)
187  {
188  frags_[frag->fragmentID()].emplace_back(std::move(frag));
189  }
190  return result;
191  }
192 
193  size_t
195  numFragIDs() const
196  {
197  return numFragIDs_;
198  }
199 
200  // artdaq::FragmentGenerator &
201  // ThrottledGenerator::
202  // generator() const
203  // {
204  // return *generator_;
205  // }
206 
224  int process_data(fhicl::ParameterSet const& pset)
225  {
226  auto const gta_pset = pset.get<fhicl::ParameterSet>("genToArt");
227 
228  // Make the generators based on the configuration.
229  std::vector<ThrottledGenerator> generators;
230 
231  auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>("fragment_receivers");
232  for (auto const& gen_ps : fr_pset)
233  {
234  generators.emplace_back(gen_ps.get<std::string>("generator"),
235  gen_ps);
236  }
237 
238  artdaq::FragmentPtrs frags;
239  auto eb_pset = gta_pset.get<fhicl::ParameterSet>("event_builder", {});
240  size_t expected_frags_per_event = 0;
241  for (auto& gen : generators)
242  {
243  gen.start(1000, 0, 0);
244  expected_frags_per_event += gen.numFragIDs();
245  }
246  eb_pset.put_or_replace<size_t>("expected_fragments_per_event", expected_frags_per_event);
247 
248  artdaq::SharedMemoryEventManager store(eb_pset, pset);
249  store.startRun(gta_pset.get<int>("run_number", 1000));
250 
251  auto const events_to_generate =
252  gta_pset.get<artdaq::Fragment::sequence_id_t>("events_to_generate", -1);
253  auto const reset_sequenceID = pset.get<bool>("reset_sequenceID", true);
254  bool done = false;
255  for (artdaq::Fragment::sequence_id_t event_count = 1;
256  (events_to_generate == static_cast<decltype(events_to_generate)>(-1)
257  || event_count <= events_to_generate) && (!done);
258  ++event_count)
259  {
260  for (auto& gen : generators)
261  {
262  done |= !gen.getNext(frags);
263  }
264  TLOG(TLVL_TRACE) << "There are " << frags.size() << " Fragments in event " << event_count << ".";
265  artdaq::Fragment::sequence_id_t current_sequence_id = -1;
266  for (auto& val : frags)
267  {
268  if (reset_sequenceID)
269  {
270  TLOG(TLVL_DEBUG) << "Setting fragment sequence id to " << event_count;
271  val->setSequenceID(event_count);
272  }
273  if (current_sequence_id ==
274  static_cast<artdaq::Fragment::sequence_id_t>(-1))
275  {
276  current_sequence_id = val->sequenceID();
277  }
278  else if (val->sequenceID() != current_sequence_id)
279  {
280  throw art::Exception(art::errors::DataCorruption)
281  << "Data corruption: apparently related fragments have "
282  << " different sequence IDs: "
283  << val->sequenceID()
284  << " and "
285  << current_sequence_id
286  << ".\n";
287  }
288 
289  auto start_time = std::chrono::steady_clock::now();
290  bool sts = false;
291  auto loop_count = 0;
292  while (!sts)
293  {
294  artdaq::FragmentPtr tempFrag;
295  sts = store.AddFragment(std::move(val), 1000000, tempFrag);
296  if (!sts && event_count <= 10 && loop_count > 100)
297  {
298  TLOG(TLVL_ERROR) << "Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) << " s. Check art thread status!";
299  store.endOfData();
300  exit(1);
301  }
302  val = std::move(tempFrag);
303  if (!sts)
304  {
305  loop_count++;
306  usleep(10000);
307  }
308  }
309  }
310  frags.clear();
311  TLOG(TLVL_TRACE) << "Event " << event_count << " END";
312  }
313  for (auto& gen : generators)
314  {
315  gen.stop(0, 0);
316  }
317 
318  bool endSucceeded = store.endOfData();
319  if (endSucceeded)
320  {
321  return 0;
322  }
323  else
324  {
325  return 15;
326  }
327  }
328 }
329 
330 int main(int argc, char* argv[]) try
331 {
332  artdaq::configureMessageFacility("genToArt");
333  // Command line handling.
334  bpo::variables_map vm;
335  auto result = process_cmd_line(argc, argv, vm);
336  if (result != 0)
337  {
338  return (result);
339  }
340  // Read FHiCL configuration file.
341  fhicl::ParameterSet pset;
342  if (getenv("FHICL_FILE_PATH") == nullptr)
343  {
344  TLOG(TLVL_ERROR)
345  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
346  setenv("FHICL_FILE_PATH", ".", 0);
347  }
348  artdaq::SimpleLookupPolicy lookup_policy("FHICL_FILE_PATH");
349  make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
350  return process_data(pset);
351 }
352 catch (std::string& x)
353 {
354  TLOG(TLVL_ERROR) << "Exception (type string) caught in genToArt: " << x << '\n';
355  return 1;
356 }
357 catch (char const* m)
358 {
359  TLOG(TLVL_ERROR) << "Exception (type char const*) caught in genToArt: ";
360  if (m)
361  {
362  TLOG(TLVL_ERROR) << m;
363  }
364  else
365  {
366  TLOG(TLVL_ERROR) << "[the value was a null pointer, so no message is available]";
367  }
368 }
void start(int run, uint64_t timeout, uint64_t timestamp) const
Send start signal to FragmentGenerator, if it&#39;s a CommandableFragmentGenerator.
Definition: genToArt.cc:120
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
int process_cmd_line(int argc, char **argv, bpo::variables_map &vm)
Process the command line.
Definition: genToArt.cc:48
bool getNext(artdaq::FragmentPtrs &newFrags)
Get the next fragment from the generator.
Definition: genToArt.cc:158
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
int process_data(fhicl::ParameterSet const &pset)
Run the test, instantiating configured generators and an EventStore.
Definition: genToArt.cc:224
void stop(uint64_t timeout, uint64_t timestamp) const
Send stop signal to FragmentGenerator, if it&#39;s a CommandableFragmentGenerator.
Definition: genToArt.cc:130
size_t numFragIDs() const
Get the number of Fragment IDs handled by this generator.
Definition: genToArt.cc:195
ThrottledGenerator: ensure that we only get one fragment per type at a time from the generator...
Definition: genToArt.cc:90