Belle II Software development
RxModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/pcore/RxModule.h>
10#include <framework/pcore/EvtMessage.h>
11#include <framework/pcore/DataStoreStreamer.h>
12#include <framework/core/RandomNumbers.h>
13
14#include <TSystem.h>
15
16using namespace std;
17using namespace Belle2;
18
20{
21 //Set module properties
22 setDescription("Decode data from RingBuffer into DataStore");
24 setType("Rx");
25
26 m_rbuf = rbuf;
28 if (rbuf) {
29 setName("Rx" + std::to_string(rbuf->shmid()));
30 B2DEBUG(32, "Rx: Constructor with RingBuffer done.");
31 }
32}
33
34RxModule::~RxModule() = default;
35
41
43{
44 while (!m_rbuf->isDead()) {
45 const int size = m_rbuf->remq(m_evtbuf.data(), m_evtbuf.size());
46 if (size > 0) {
47 B2DEBUG(35, "Rx: got an event from RingBuffer, size=" << size);
48
49 // Restore objects in DataStore
50 EvtMessage evtmsg(reinterpret_cast<char*>(m_evtbuf.data()));
51 m_streamer->restoreDataStore(&evtmsg);
52 // Restore the event dependent random number object from Datastore
53 if (m_randomgenerator.isValid()) {
55 }
56 break;
57 }
58 if (size < 0) {
59 // The record was too big for m_evtbuf, so remq() left it queued and returned minus the
60 // size it needs: grow the buffer and loop round to read the very same record. The 50%
61 // headroom keeps the next slightly larger event from triggering another round trip.
62 m_evtbuf.resize(-size + -size / 2);
63 B2DEBUG(32, "Rx: grew receive buffer to " << m_evtbuf.size() << " words");
64 } else {
65 usleep(20);
66 }
67 }
68}
69
71{
72 gSystem->Load("libdataobjects");
73
74 // Start small and non-empty; readEvent() grows it to whatever the records need.
75 // 4096 words -> 16 kB
76 m_evtbuf.resize(4096);
77
79
81
82 // Read the first event in RingBuffer and restore in DataStore.
83 // This is necessary to create object tables before TTree initialization
84 // if used together with TTree based output (RootOutput module).
85 readEvent();
86}
87
89{
90 m_nrecv++;
91 // First event is already loaded in initialize()
92 if (m_nrecv == 0) return;
93
94 // Get a record from ringbuf
95 readEvent();
96}
97
99{
100 B2DEBUG(32, "Rx: terminate called");
101 delete m_streamer;
102}
Stream/restore DataStore objects to/from EvtMessage.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
Class to manage streamed object.
Definition EvtMessage.h:59
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_Input
This module is an input module (reads data).
Definition Module.h:78
@ c_InternalSerializer
This module is an internal serializer/deserializer for parallel processing.
Definition Module.h:82
void setName(const std::string &name)
Set the name of the module.
Definition Module.h:213
void setType(const std::string &type)
Set the module type.
Definition Module.cc:48
static RandomGenerator & getEventRandomGenerator()
return reference to the event dependent random generator
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition RingBuffer.h:39
int shmid() const
Return ID of the shared memory.
DataStoreStreamer * m_streamer
Used for serialization.
Definition RxModule.h:63
virtual void initialize() override
Module functions to be called from main process.
Definition RxModule.cc:70
void initStreamer()
initialize m_streamer.
Definition RxModule.cc:36
virtual void event() override
This method is the core of the module.
Definition RxModule.cc:88
virtual void terminate() override
This method is called at the end of the event processing.
Definition RxModule.cc:98
std::vector< int > m_evtbuf
Receive buffer in words, grown on demand by readEvent().
Definition RxModule.h:76
RingBuffer * m_rbuf
attached RingBuffer.
Definition RxModule.h:60
int m_nrecv
Current event number.
Definition RxModule.h:69
void readEvent()
Gets data from m_rbuf and puts it into the data store.
Definition RxModule.cc:42
StoreObjPtr< RandomGenerator > m_randomgenerator
Random Generator object to receive from TxModule.
Definition RxModule.h:81
bool m_handleMergeable
Whether to handle Mergeable objects.
Definition RxModule.h:78
int m_compressionLevel
Compression Level.
Definition RxModule.h:66
RxModule(RingBuffer *rbuf)
Constructor.
Definition RxModule.cc:19
Abstract base class for different kinds of events.
STL namespace.