Belle II Software  release-08-01-10
Rbuf2RbufModule.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 <daq/rfarm/event/modules/Rbuf2RbufModule.h>
10 #include <TSystem.h>
11 #include <stdlib.h>
12 
13 #include "framework/datastore/StoreObjPtr.h"
14 #include "framework/dataobjects/EventMetaData.h"
15 
16 using namespace std;
17 using namespace Belle2;
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(Rbuf2Rbuf)
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
29 {
30  //Set module properties
31  setDescription("Encode DataStore into RingBuffer");
32  // setPropertyFlags(c_Input | c_ParallelProcessingCertified);
33 
34  addParam("InputRbufName", m_name_rbufin, "Name of Input RingBuffer",
35  string("RBUFIN"));
36  addParam("OutputRbufName", m_name_rbufout, "Name of Output RingBuffer",
37  string("RBUFOUT"));
38 
39  m_rbufin = NULL;
40  m_rbufout = NULL;
41  m_nrecv = 0;
42 
43  //Parameter definition
44  B2INFO("Rx: Constructor done.");
45 }
46 
47 
48 Rbuf2RbufModule::~Rbuf2RbufModule()
49 {
50 }
51 
52 void Rbuf2RbufModule::initialize()
53 {
54  gSystem->Load("libdataobjects");
55 
56  // Connect to Ring Buffers
57  m_rbufin = new RingBuffer(m_name_rbufin.c_str());
58  m_rbufout = new RingBuffer(m_name_rbufout.c_str());
59 
60  // Initialize EvtMetaData
61  m_eventMetaData.registerInDataStore();
62 
63  // Calls event function for TTree
64  event();
65 
66  // Set m_nrecv to negative to tell the module is initialized
67  m_nrecv = -1;
68 
69  B2INFO("Rx initialized.");
70 }
71 
72 
73 void Rbuf2RbufModule::beginRun()
74 {
75  B2INFO("beginRun called.");
76 }
77 
78 
79 void Rbuf2RbufModule::event()
80 {
81  m_nrecv++;
82  // First event is already processed
83  if (m_nrecv == 0) return;
84 
85  // Event buffer
86  int size;
87  int* evtbuf = new int[MAXEVTSIZE];
88 
89  // Get a record from input ringbuf
90  while ((size = m_rbufin->remq(evtbuf)) == 0) {
91  // usleep(100);
92  usleep(20);
93  }
94 
95  // Put the record in output ringbuf
96  for (;;) {
97  int stat = m_rbufout->insq(evtbuf, size);
98  if (stat >= 0) break;
99  // usleep(100);
100  usleep(20);
101  }
102 
103  // Set EventMetaData
104  StoreObjPtr<EventMetaData> evtmetadata;
105  evtmetadata.create();
106  evtmetadata->setExperiment(1);
107  evtmetadata->setRun(1);
108  evtmetadata->setEvent(m_nrecv);
109 
110  delete[] evtbuf;
111 
112  B2INFO("Rbuf2Rbuf: RingBuffer copied!!");
113  return;
114  // return type;
115 }
116 
117 void Rbuf2RbufModule::endRun()
118 {
119  //fill Run data
120 
121  B2INFO("Rbuf2Rbuf: endRun done.");
122 }
123 
124 
125 void Rbuf2RbufModule::terminate()
126 {
127  B2INFO("Rbuf2Rbuf: terminate called");
128 }
129 
Base class for Modules.
Definition: Module.h:72
A class definition of an input module for Sequential ROOT I/O.
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.