Belle II Software  release-08-01-10
Ds2RawFileModule.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/storage/modules/Ds2RawFileModule.h>
10 
11 #include <daq/dataobjects/SendHeader.h>
12 #include <daq/dataobjects/SendTrailer.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/dataobjects/EventMetaData.h>
16 
17 #include <fcntl.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
23 // static int fdout;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_MODULE(Ds2RawFile)
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
35 {
36  //Set module properties
37  setDescription("Encode DataStore into RingBuffer");
38 
39  //addParam("RingBufferName", m_rbufname, "Name of RingBuffer",
40  // string("OutputRbuf"));
41  addParam("CompressionLevel", m_compressionLevel, "Compression level",
42  0);
43 
44  //m_rbuf = NULL;
45  m_nsent = 0;
46  m_compressionLevel = 0;
47 
48  //Parameter definition
49  B2INFO("Ds2Raw: Constructor done.");
50 }
51 
52 
53 Ds2RawFileModule::~Ds2RawFileModule()
54 {
55 }
56 
57 void Ds2RawFileModule::initialize()
58 {
59 
60  // m_rbuf = new RingBuffer(m_rbufname.c_str(), RBUFSIZE);
61  //m_rbuf = new RingBuffer(m_rbufname.c_str());
62  m_streamer = new DataStoreStreamer(m_compressionLevel);
63  m_file = open("file.data", O_CREAT | O_RDWR | O_TRUNC, 0644);
64 
67 
68  B2INFO("Ds2Raw initialized.");
69 }
70 
71 
72 void Ds2RawFileModule::beginRun()
73 {
74  B2INFO("Ds2Raw: beginRun called.");
75 }
76 
77 
78 void Ds2RawFileModule::event()
79 {
80  // Stream DataStore in EvtMessage
81  EvtMessage* msg = m_streamer->streamDataStore(DataStore::c_Event);
82 
83  // Event Meta Data
85 
86  // Fill Header and Trailer
87  SendHeader hdr;
88  SendTrailer trl;
89 
90  // Number of total words
91  int msgsize = (msg->size() - 1) / 4 + 1;
92  int total_nwrds = msgsize + hdr.GetHdrNwords() + trl.GetTrlNwords();
93 
94  // Fill header and trailer
95  hdr.SetNwords(total_nwrds);
96  hdr.SetNumEventsinPacket(1);
97  hdr.SetNumNodesinPacket(1);
98  hdr.SetEventNumber(evtmeta->getEvent());
99  // hdr.SetExpRunWord(evtmeta->getRun());
100  hdr.SetSubRunNum(0); // temporary
101  hdr.SetRunNum(evtmeta->getRun());
102  hdr.SetExpNum(evtmeta->getExperiment());
103  hdr.SetNodeID(HLT_SUBSYS_ID);
104  // trl.SetMagicWord();
105 
106  // Allocate raw buffer
107  int* buffer = new int[total_nwrds];
108 
109  // Fill header
110  memcpy(buffer, hdr.GetBuffer(), hdr.GetHdrNwords()*sizeof(int));
111 
112  // Fill EvtMessage
113  memcpy(buffer + hdr.GetHdrNwords(), msg->buffer(), msg->size());
114 
115  // Fill trailer
116  memcpy(buffer + hdr.GetHdrNwords() + msgsize, trl.GetBuffer(),
117  trl.GetTrlNwords()*sizeof(int));
118 
119  write(m_file, buffer, total_nwrds * 4);
120 
121  B2INFO("Ds2Raw: objs sent in buffer. Size = " << msg->size());
122 
123  // Release EvtMessage buffer
124  delete[] buffer;
125  delete msg;
126 
127  // return
128  m_nsent++;
129 
130 }
131 
132 void Ds2RawFileModule::endRun()
133 {
134  //fill Run data
135 
136  printf("Ds2Raw: endRun called.....\n");
137  B2INFO("Ds2Raw: endRun done.");
138 }
139 
140 
141 void Ds2RawFileModule::terminate()
142 {
143  delete m_streamer;
144 
145  // RingBuffer should not be deleted
146 
147  B2INFO("Ds2Raw: terminate called");
148 }
149 
Stream/restore DataStore objects to/from EvtMessage.
A class definition of an input module for Sequential ROOT I/O.
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
Base class for Modules.
Definition: Module.h:72
void SetNumEventsinPacket(int num_events)
set contents of Header
Definition: SendHeader.cc:61
int GetHdrNwords()
get contents of Header
Definition: SendHeader.cc:124
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
#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.