Belle II Software development
Ds2RawModule.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/Ds2RawModule.h>
10#include <framework/datastore/StoreObjPtr.h>
11#include <framework/dataobjects/EventMetaData.h>
12#include <daq/dataobjects/SendHeader.h>
13#include <daq/dataobjects/SendTrailer.h>
14
15#include <stdlib.h>
16
17using namespace std;
18using namespace Belle2;
19
21// static int fdout;
22
23//-----------------------------------------------------------------
24// Register the Module
25//-----------------------------------------------------------------
26REG_MODULE(Ds2Raw);
27
28//-----------------------------------------------------------------
29// Implementation
30//-----------------------------------------------------------------
31
33{
34 //Set module properties
35 setDescription("Encode DataStore into RingBuffer");
36
37 addParam("RingBufferName", m_rbufname, "Name of RingBuffer",
38 string("OutputRbuf"));
39 addParam("CompressionLevel", m_compressionLevel, "Compression level",
40 0);
41
42 m_rbuf = NULL;
43 m_nsent = 0;
45
46 //Parameter definition
47 B2INFO("Ds2Raw: Constructor done.");
48}
49
50
51Ds2RawModule::~Ds2RawModule()
52{
53}
54
56{
57
58 // m_rbuf = new RingBuffer(m_rbufname.c_str(), RBUFSIZE);
59 // cppcheck-suppress publicAllocationError ; initialize() is called exactly once per job
60 m_rbuf = new RingBuffer(m_rbufname.c_str());
62
65
66 B2INFO("Ds2Raw initialized.");
67}
68
69
71{
72 B2INFO("Ds2Raw: beginRun called.");
73}
74
75
77{
78 // Stream DataStore in EvtMessage
79 EvtMessage* msg = m_streamer->streamDataStore(DataStore::c_Event);
80
81 // Event Meta Data
83
84 // Fill Header and Trailer
85 SendHeader hdr;
86 SendTrailer trl;
87
88 // Number of total words
89 int msgsize = (msg->size() - 1) / 4 + 1;
90 int total_nwrds = msgsize + hdr.GetHdrNwords() + trl.GetTrlNwords();
91
92 // Fill header and trailer
93 hdr.SetNwords(total_nwrds);
95 hdr.SetNumNodesinPacket(1);
96 hdr.SetEventNumber(evtmeta->getEvent());
97 // hdr.SetExpRunWord(evtmeta->getRun());
98 hdr.SetSubRunNum(evtmeta->getSubrun()); // modified on Apr. 20, 2016 by SY
99 hdr.SetRunNum(evtmeta->getRun());
100 hdr.SetExpNum(evtmeta->getExperiment());
101 hdr.SetNodeID(HLT_SUBSYS_ID);
102 // trl.SetMagicWord();
103
104 // Allocate raw buffer
105 int* buffer = new int[total_nwrds];
106
107 // Fill header
108 memcpy(buffer, hdr.GetBuffer(), hdr.GetHdrNwords()*sizeof(int));
109
110 // Fill EvtMessage
111 memcpy(buffer + hdr.GetHdrNwords(), msg->buffer(), msg->size());
112
113 // Fill trailer
114 memcpy(buffer + hdr.GetHdrNwords() + msgsize, trl.GetBuffer(),
115 trl.GetTrlNwords()*sizeof(int));
116 // printf ( "trailer = %8.8x, %8.8x\n", *(buffer+hdr.GetHdrNwords()+msgsize),
117 // *(buffer+hdr.GetHdrNwords()+msgsize+1) );
118
119 // Put the raw buffer in ring buffer
120 for (;;) {
121 int stat = m_rbuf->insq(buffer, total_nwrds);
122 if (stat >= 0) break;
123 // usleep(200);
124 usleep(20);
125 }
126
129
130 B2INFO("Ds2Raw: objs sent in buffer. Size = " << msg->size());
131
132 // Release EvtMessage buffer
133 delete[] buffer;
134 delete msg;
135
136 // return
137 m_nsent++;
138
139}
140
142{
143 //fill Run data
144
145 printf("Ds2Raw: endRun called.....\n");
146 B2INFO("Ds2Raw: endRun done.");
147}
148
149
151{
152 delete m_streamer;
153
154 // RingBuffer should not be deleted
155
156 B2INFO("Ds2Raw: terminate called");
157}
158
Stream/restore DataStore objects to/from EvtMessage.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
DataStoreStreamer * m_streamer
DataStore streamer.
void initialize() override
Module functions to be called from main process.
void event() override
This method is the core of the module.
void endRun() override
This method is called if the current run ends.
Ds2RawModule()
Constructor / Destructor.
void terminate() override
This method is called at the end of the event processing.
void beginRun() override
Module functions to be called from event process.
int m_nsent
No. of sent events.
int m_compressionLevel
Compression parameter.
std::string m_rbufname
RingBuffer ID.
Class to manage streamed object.
Definition EvtMessage.h:59
char * buffer()
Get buffer address.
Definition EvtMessage.cc:76
int size() const
Get size of message including headers.
Definition EvtMessage.cc:94
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition RingBuffer.h:39
void SetNumEventsinPacket(int num_events)
set contents of Header
Definition SendHeader.cc:61
static int GetHdrNwords()
get contents of Header
void SetNwords(int total_data_nwords)
initialize Header
Definition SendHeader.cc:51
int * GetBuffer(void)
Get Header contents.
Definition SendHeader.cc:32
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:560
#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.
STL namespace.