artdaq_core  v3_06_00
SimpleQueueReader.cc
1 #include "artdaq-core/Core/SimpleQueueReader.hh"
2 
3 #include <chrono> // for milliseconds
4 #include <cstddef> // for std::size_t
5 #include <iostream>
6 #include <string>
7 #include <thread> // std::this_thread::sleep_for
8 #include "trace.h" // TRACE
9 
10 namespace artdaq {
11 int simpleQueueReaderApp(int argc, char** argv)
12 {
13  try
14  {
15  size_t eec(0);
16  if (argc == 2)
17  {
18  std::istringstream ins(argv[1]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
19  ins >> eec;
20  }
21  SimpleQueueReader reader(eec);
22  reader.run();
23  return 0;
24  }
25  catch (std::string const& msg)
26  {
27  std::cerr << "simpleQueueReaderApp failed: "
28  << msg;
29  return 1;
30  }
31  catch (...)
32  {
33  return 1;
34  }
35 }
36 
38  SimpleQueueReader(std::size_t eec)
39  : queue_(getGlobalQueue())
40  , expectedEventCount_(eec)
41 {
42  queue_.setReaderIsReady();
43  TLOG(50) << "SimpleQueueReader ctor done (after queue_.setReaderIsReady())";
44 }
45 
47 {
48  std::size_t eventsSeen = 0;
49  auto doPrint = getenv("VERBOSE_QUEUE_READING");
50  while (true)
51  {
52  RawEvent_ptr rawEventPtr;
53  if (queue_.deqNowait(rawEventPtr))
54  {
55  // If we got a null pointer, we're done...
56  if (!rawEventPtr) { break; }
57  ++eventsSeen;
58  // Otherwise, do our work ...
59  if (doPrint != nullptr) { std::cout << *rawEventPtr << std::endl; }
60  }
61  else
62  {
63  std::this_thread::sleep_for(std::chrono::milliseconds(250));
64  }
65  }
66  if ((expectedEventCount_ != 0u) && eventsSeen != expectedEventCount_)
67  {
68  throw cet::exception("SimpleQueueReader") // NOLINT(cert-err60-cpp)
69  << "Wrong number of events in SimpleQueueReader ("
70  << eventsSeen << " != " << expectedEventCount_ << ").\n";
71  }
72 }
73 } // namespace artdaq
RawEventQueue & getGlobalQueue(RawEventQueue::SizeType maxSize)
The first thread to call getGlobalQueue() causes the creation of the queue. The queue will be destroy...
Definition: GlobalQueue.cc:9
SimpleQueueReader(std::size_t eec=0)
Constructs a SimpleQueueReader.
int simpleQueueReaderApp(int argc, char **argv)
An application which pops items off a RawEventQueue using the SimpleQueueReader.
std::shared_ptr< RawEvent > RawEvent_ptr
A smart pointer to a RawEvent object.
Definition: GlobalQueue.hh:12
bool deqNowait(ValueType &item)
Assign the value at the head of the queue to item and then remove the head of the queue...
SimpleQueueReader will continue to read RawEvent objects off the queue until it encounters a null poi...
void run()
Run until a null pointer is popped off of the RawEventQueue. Throws an excpetion if expectedEventCoun...
void setReaderIsReady(bool rdy=true)
Set the ready flag for the reader.