Belle II Software  release-05-01-25
b2hlt_rb2file.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 #include <daq/rfarm/manager/RFFlowStat.h>
11 #include <framework/pcore/RingBuffer.h>
12 #include <daq/rfarm/event/hltsocket/HLTMainLoop.h>
13 #include <daq/rfarm/event/hltsocket/HLTFile.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/pcore/EvtMessage.h>
16 
17 #include <boost/program_options.hpp>
18 #include <iostream>
19 
20 #define MAXEVTSIZE 80000000
21 
22 using namespace Belle2;
23 namespace po = boost::program_options;
24 
25 int main(int argc, char* argv[])
26 {
27  std::string ringBufferName;
28  std::string shmName;
29  unsigned int shmID;
30  bool raw;
31  std::string file_name;
32 
33  po::options_description desc("b2hlt_rb2socket RING-BUFFER-NAME FILE-NAME SHM-NAME SHM-ID");
34  desc.add_options()
35  ("help,h", "Print this help message")
36  ("ring-buffer-name,r", po::value<std::string>(&ringBufferName)->required(), "name of the ring buffer")
37  ("file-name,f", po::value<std::string>(&file_name)->required(), "file name to write to")
38  ("shm-name,n", po::value<std::string>(&shmName)->required(), "name of the shm for flow output")
39  ("shm-id,i", po::value<unsigned int>(&shmID)->required(), "id in the shm for flow output")
40  ("raw", po::bool_switch(&raw)->default_value(false), "send and receive raw data instead of event buffers");
41 
42 
43  po::positional_options_description p;
44  p.add("ring-buffer-name", 1).add("file-name", 1).add("shm-name", 1).add("shm-id", 1);
45 
46  po::variables_map vm;
47  try {
48  po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
49  } catch (std::exception& e) {
50  B2FATAL(e.what());
51  }
52 
53  if (vm.count("help")) {
54  std::cout << desc << std::endl;
55  return 1;
56  }
57 
58  try {
59  po::notify(vm);
60  } catch (std::exception& e) {
61  B2FATAL(e.what());
62  }
63 
64  // TODO: delete or not?
65  RingBuffer* ringBuffer = new RingBuffer(ringBufferName.c_str());
66  RFFlowStat flow((char*)shmName.c_str(), shmID, ringBuffer);
67  int* buffer = new int[MAXEVTSIZE];
68 
69  HLTMainLoop mainLoop;
70 
71  HLTFile file;
72  int returnValue;
73  int nevt = 0;
74  bool terminate = false;
75 
76  if (not file.open(file_name, raw, "w")) {
77  B2ERROR("Can not open file");
78  terminate = true;
79  }
80 
81  while (mainLoop.isRunning() and not terminate) {
82  // Read from ring buffer
83  const int size = mainLoop.readFromRingBufferWaiting(ringBuffer, buffer);
84  // Error checking ring buffer
85  if (size <= 0) {
86  if (mainLoop.isRunning()) {
87  B2ERROR("Writing to the ring buffer failed!");
88  }
89  // This is fine if we are terminating anyways
90  break;
91  }
92  B2ASSERT("Size is negative! This should be handled above. Not good!", size > 0);
93 
94  // Monitoring
95  flow.log(size * sizeof(int));
96 
97  if (raw) {
98  returnValue = file.put_wordbuf(buffer, size);
99  } else {
100  EvtMessage message(reinterpret_cast<char*>(buffer));
101  returnValue = file.put(message.buffer(), message.size());
102  // Terminate messages make us terminate
103  if (message.type() == MSG_TERMINATE) {
104  B2RESULT("Having received terminate message");
105  terminate = true;
106  }
107  }
108  if (returnValue <= 0) {
109  if (mainLoop.isRunning()) {
110  B2ERROR("Error in writing the event! Aborting.");
111  }
112  // This is fine if we are terminating anyways
113  break;
114  }
115  B2ASSERT("Written size is negative! This should be handled above. Not good!", returnValue > 0);
116 
117  // Logging
118  nevt++;
119  if (nevt % 5000 == 0) {
120  B2RESULT("b2hlt_rb2file event number: " << nevt);
121  }
122  }
123 
124  B2RESULT("Program terminated.");
125 }
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::EvtMessage
Class to manage streamed object.
Definition: EvtMessage.h:60
Belle2::RFFlowStat
Definition: RFFlowStat.h:28
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
Belle2::HLTFile
Definition: HLTFile.h:11