Belle II Software  release-05-01-25
b2hlt_rb2rb.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Ryosuke Itoh, Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/pcore/RingBuffer.h>
12 #include <daq/rfarm/event/hltsocket/HLTMainLoop.h>
13 #include <framework/logging/Logger.h>
14 #include <framework/pcore/EvtMessage.h>
15 
16 #include <boost/program_options.hpp>
17 #include <iostream>
18 
19 #define MAXEVTSIZE 80000000
20 #define RBUFSIZE 10000000
21 
22 
23 using namespace Belle2;
24 namespace po = boost::program_options;
25 
26 int main(int argc, char* argv[])
27 {
28  std::vector<std::string> inputRingBufferNames;
29  std::vector<std::string> outputRingBufferNames;
30 
31  po::options_description desc("b2hlt_rb2rb");
32  desc.add_options()
33  ("help,h", "Print this help message")
34  ("input-ring-buffer-name,r", po::value<std::vector<std::string>>(&inputRingBufferNames)->required(),
35  "name of the input ring buffers")
36  ("output-ring-buffer-name,r", po::value<std::vector<std::string>>(&outputRingBufferNames)->required(),
37  "name of the output ring buffers");
38 
39  po::variables_map vm;
40  try {
41  po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
42  } catch (std::exception& e) {
43  B2FATAL(e.what());
44  }
45 
46  if (vm.count("help")) {
47  std::cout << desc << std::endl;
48  return 1;
49  }
50 
51  try {
52  po::notify(vm);
53  } catch (std::exception& e) {
54  B2FATAL(e.what());
55  }
56 
57  if (outputRingBufferNames.empty() or inputRingBufferNames.empty()) {
58  B2FATAL("Need at least one output and input ring buffer!");
59  }
60 
61  // TODO: delete or not?
62  std::vector<RingBuffer*> inputRingBuffers;
63  for (const std::string& bufferName : inputRingBufferNames) {
64  inputRingBuffers.push_back(new RingBuffer(bufferName.c_str(), RBUFSIZE));
65  inputRingBuffers.back()->dump_db();
66  }
67 
68  std::vector<RingBuffer*> outputRingBuffers;
69  for (const std::string& bufferName : outputRingBufferNames) {
70  outputRingBuffers.push_back(new RingBuffer(bufferName.c_str(), RBUFSIZE));
71  outputRingBuffers.back()->dump_db();
72  }
73 
74  HLTMainLoop mainLoop;
75 
76  int* buffer = new int[MAXEVTSIZE];
77  int nevt = 0;
78 
79  auto inputRingBuffer = inputRingBuffers.begin();
80  auto outputRingBuffer = outputRingBuffers.begin();
81 
82  while (mainLoop.isRunning()) {
83  // Read from ring buffer
84  const int size = mainLoop.readFromRingBufferWaiting(*inputRingBuffer, buffer);
85  // Error checking ring buffer
86  if (size <= 0) {
87  if (mainLoop.isRunning()) {
88  B2ERROR("Writing to the ring buffer failed!");
89  }
90  // This is fine if we are terminating anyways
91  break;
92  }
93  B2ASSERT("Size is negative! This should be handled above. Not good!", size > 0);
94 
95  // Write to ring buffer
96  const int returnValue = mainLoop.writeToRingBufferWaiting(*outputRingBuffer, buffer, size);
97  // Error check ring buffer
98  if (returnValue <= 0) {
99  if (mainLoop.isRunning()) {
100  B2ERROR("Writing to the ring buffer failed!");
101  }
102  // This is fine if we are terminating anyways
103  break;
104  }
105  B2ASSERT("Written size is negative! This should be handled above. Not good!", returnValue > 0);
106 
107  // Logging
108  nevt++;
109  if (nevt % 5000 == 0) {
110  B2RESULT("b2hlt_rb2socket event number: " << nevt);
111  }
112 
113  inputRingBuffer++;
114  if (inputRingBuffer == inputRingBuffers.end()) {
115  inputRingBuffer = inputRingBuffers.begin();
116  }
117  outputRingBuffer++;
118  if (outputRingBuffer == outputRingBuffers.end()) {
119  outputRingBuffer = outputRingBuffers.begin();
120  }
121  }
122 
123  B2RESULT("Program terminated.");
124 }
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::RingBuffer
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:36
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::HLTMainLoop
Definition: HLTMainLoop.h:28