Belle II Software development
Root2Raw.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/rawdata/modules/Root2Raw.h>
10
11#include <daq/dataobjects/SendHeader.h>
12#include <daq/dataobjects/SendTrailer.h>
13#include <framework/core/Module.h>
14#include <framework/core/ModuleParamBase.h>
15#include <framework/datastore/StoreArray.h>
16#include <framework/logging/Logger.h>
17#include <rawdata/dataobjects/RawARICH.h>
18#include <rawdata/dataobjects/RawCDC.h>
19#include <rawdata/dataobjects/RawECL.h>
20#include <rawdata/dataobjects/RawFTSW.h>
21#include <rawdata/dataobjects/RawHeader_v1.h>
22#include <rawdata/dataobjects/RawKLM.h>
23#include <rawdata/dataobjects/RawSVD.h>
24#include <rawdata/dataobjects/RawTOP.h>
25#include <rawdata/dataobjects/RawTRG.h>
26
27#include <fcntl.h>
28
29using namespace std;
30using namespace Belle2;
31
32//#define DEBUG
33
34//-----------------------------------------------------------------
35// Register the Module
36//-----------------------------------------------------------------
37REG_MODULE(Root2Raw);
38
39//-----------------------------------------------------------------
40// Implementation
41//-----------------------------------------------------------------
42
44{
45 addParam("outputFileName", m_filename, "Output binary filename", string("datafile.binary"));
46 addParam("expno", m_expno, "experiment number for output raw file", 0);
47 addParam("runno", m_runno, "run number for output raw file", 0);
48}
49
50Root2RawModule::~Root2RawModule()
51{
52}
53
55{
56 B2INFO("Root2Raw: initialize() started.");
57
58 m_file = open(m_filename.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
59 if (m_file <= 0) {
60 perror("open");
61 exit(-1);
62 }
63
64 B2INFO("Root2Raw: initialize() done.");
65}
66
68{
69 //fill Run data
70 // fclose( m_fp_out );
71 B2INFO("endRun done.");
72}
73
74
76{
77 close(m_file);
78 B2INFO("terminate called");
79}
80
81
83{
84
85
86 B2INFO("Root2Raw: event() started.");
87
88 // Prepare SendHeader
89 SendHeader hdr;
90 hdr.Initialize();
91 SendTrailer trl;
92 trl.Initialize();
93
94 int* evtbuf = static_cast<int*>(malloc(MAXEVTSIZE));
95 if (evtbuf == NULL) B2FATAL("Root2Raw: cannot allocate the event buffer");
96 int* databuf = evtbuf + hdr.GetHdrNwords();
97
98 int nwords = hdr.GetHdrNwords();
99 int nblock = 0;
100
101
102 // Fill PXD
103 /*
104 StoreArray<RawPXD> pxdarray;
105 for (int i = 0; i < pxdarray.getEntries(); i++) {
106 // for ( int i=0; i< 1; i++ ) {
107 RawPXD* pxd = pxdarray[i];
108 // printf ( "SVD COPPER %d to be filled\n", i );
109 memcpy(databuf, pxd->GetBuffer(0), pxd->GetBlockNwords(0)*sizeof(int));
110 nwords += pxd->GetBlockNwords(0);
111 databuf += pxd->GetBlockNwords(0);
112 nblock++;
113 }
114 */
115
116 // Fill SVD
117 StoreArray<RawSVD> svdarray;
118 for (int i = 0; i < svdarray.getEntries(); i++) {
119 // for ( int i=0; i< 1; i++ ) {
120 RawSVD* svd = svdarray[i];
121 // printf ( "SVD COPPER %d to be filled\n", i );
122 memcpy(databuf, svd->GetBuffer(0), svd->GetBlockNwords(0)*sizeof(int));
123 nwords += svd->GetBlockNwords(0);
124 databuf += svd->GetBlockNwords(0);
125 nblock++;
126 }
127
128 // Fill CDC
129 StoreArray<RawCDC> cdcarray;
130 for (int i = 0; i < cdcarray.getEntries(); i++) {
131 // for ( int i=0; i< 1; i++ ) {
132 RawCDC* cdc = cdcarray[i];
133 // printf ( "SVD COPPER %d to be filled\n", i );
134 memcpy(databuf, cdc->GetBuffer(0), cdc->GetBlockNwords(0)*sizeof(int));
135 nwords += cdc->GetBlockNwords(0);
136 databuf += cdc->GetBlockNwords(0);
137 nblock++;
138 }
139
140 // Fill TOP
141 StoreArray<RawTOP> toparray;
142 for (int i = 0; i < toparray.getEntries(); i++) {
143 // for ( int i=0; i< 1; i++ ) {
144 RawTOP* top = toparray[i];
145 // printf ( "SVD COPPER %d to be filled\n", i );
146 memcpy(databuf, top->GetBuffer(0), top->GetBlockNwords(0)*sizeof(int));
147 nwords += top->GetBlockNwords(0);
148 databuf += top->GetBlockNwords(0);
149 nblock++;
150 }
151
152 // Fill ARICH
153 StoreArray<RawARICH> aricharray;
154 for (int i = 0; i < aricharray.getEntries(); i++) {
155 // for ( int i=0; i< 1; i++ ) {
156 RawARICH* arich = aricharray[i];
157 // printf ( "SVD COPPER %d to be filled\n", i );
158 memcpy(databuf, arich->GetBuffer(0), arich->GetBlockNwords(0)*sizeof(int));
159 nwords += arich->GetBlockNwords(0);
160 databuf += arich->GetBlockNwords(0);
161 nblock++;
162 }
163
164 // Fill ECL
165 StoreArray<RawECL> eclarray;
166 for (int i = 0; i < eclarray.getEntries(); i++) {
167 // for ( int i=0; i< 1; i++ ) {
168 RawECL* ecl = eclarray[i];
169 // printf ( "SVD COPPER %d to be filled\n", i );
170 memcpy(databuf, ecl->GetBuffer(0), ecl->GetBlockNwords(0)*sizeof(int));
171 nwords += ecl->GetBlockNwords(0);
172 databuf += ecl->GetBlockNwords(0);
173 nblock++;
174 }
175
176 // Fill KLM
177 StoreArray<RawKLM> klmarray;
178 for (int i = 0; i < klmarray.getEntries(); i++) {
179 // for ( int i=0; i< 1; i++ ) {
180 RawKLM* klm = klmarray[i];
181 // printf ( "SVD COPPER %d to be filled\n", i );
182 memcpy(databuf, klm->GetBuffer(0), klm->GetBlockNwords(0)*sizeof(int));
183 nwords += klm->GetBlockNwords(0);
184 databuf += klm->GetBlockNwords(0);
185 nblock++;
186 }
187
188 // Fill TRG
189 StoreArray<RawTRG> trgarray;
190 for (int i = 0; i < trgarray.getEntries(); i++) {
191 // for ( int i=0; i< 1; i++ ) {
192 RawTRG* trg = trgarray[i];
193 // printf ( "SVD COPPER %d to be filled\n", i );
194 memcpy(databuf, trg->GetBuffer(0), trg->GetBlockNwords(0)*sizeof(int));
195 nwords += trg->GetBlockNwords(0);
196 databuf += trg->GetBlockNwords(0);
197 nblock++;
198 }
199
200 // Fill FTSW
201 StoreArray<RawFTSW> ftswarray;
202 for (int i = 0; i < ftswarray.getEntries(); i++) {
203 // for ( int i=0; i< 1; i++ ) {
204 RawFTSW* ftsw = ftswarray[i];
205 // printf ( "SVD COPPER %d to be filled\n", i );
206 memcpy(databuf, ftsw->GetBuffer(0), ftsw->GetBlockNwords(0)*sizeof(int));
207 nwords += ftsw->GetBlockNwords(0);
208 databuf += ftsw->GetBlockNwords(0);
209 nblock++;
210 }
211
212 /*
213 // Fill FTSW
214 StoreArray<RawFTSW> ftswarray;
215 RawFTSW* ftsw = ftswarray[0];
216 memcpy(databuf, ftsw->GetBuffer(0), ftsw->GetBlockNwords(0)*sizeof(int));
217 nwords += ftsw->GetBlockNwords(0);
218 databuf += ftsw->GetBlockNwords(0);
219 nblock++;
220 */
221
222 // skip empty events, mostly discarded from HLT
223 if (svdarray.getEntries() < 1 &&
224 cdcarray.getEntries() < 1 &&
225 toparray.getEntries() < 1 &&
226 aricharray.getEntries() < 1 &&
227 eclarray.getEntries() < 1 &&
228 klmarray.getEntries() < 1 &&
229 trgarray.getEntries() < 1) {
230 return;
231 }
232
233 // printf ( "COPPERs filling completed\n" );
234
235 // Fill SendHeader/Trailer
236
237 nwords += trl.GetTrlNwords();
238
239 // printf ( "nwords = %d\n", nwords );
240
241 hdr.SetNwords(nwords);
243 hdr.SetNodeID(0);
244 hdr.SetNumNodesinPacket(nblock);
245
246 RawHeader_v1 rhdr;
247 rhdr.SetBuffer(cdcarray[0]->GetBuffer(0));
248 hdr.SetEventNumber(rhdr.GetEveNo());
249 hdr.SetNodeID(0);
250 hdr.SetExpRunWord(rhdr.GetExpRunSubrun());
251
252 // int* hdrbuf = hdr.GetBuffer();
253 // printf ( "%8.8x %8.8x %8.8x %8.8x ", *hdrbuf, *(hdrbuf+1), *(hdrbuf+2), *(hdrbuf+3) );
254 // printf ( "%8.8x %8.8x %8.8x %8.8x\n", *(hdrbuf+4), *(hdrbuf+5), *(hdrbuf+6), *(hdrbuf+7) );
255
256 // Set exp/rnu number explicitly if there are parameters
257 if (m_expno != 0 && m_runno != 0) {
258 hdr.SetExpNum(m_expno);
259 hdr.SetRunNum(m_runno);
260 }
261
262 memcpy(evtbuf, hdr.GetBuffer(), hdr.GetHdrNwords()*sizeof(int));
263 memcpy(databuf, trl.GetBuffer(), trl.GetTrlNwords()*sizeof(int));
264
265 write(m_file, evtbuf, nwords * sizeof(int));
266 // databuf = evtbuf+ hdr.GetHdrNwords();
267 // int datanwords = nwords - hdr.GetHdrNwords() - trl.GetTrlNwords();
268 // int is = write ( m_file, databuf, datanwords*sizeof(int) );
269 return;
270
271}
The Raw ARICH class Class for RawCOPPER class data taken by ARICH Currently, this class is almost sam...
Definition RawARICH.h:27
The Raw CDC class Class for RawCOPPER class data taken by CDC Currently, this class is almost same as...
Definition RawCDC.h:27
virtual int * GetBuffer(int n)
get nth buffer pointer
virtual int GetBlockNwords(int n)
get size of a data block
The Raw ECL class Class for RawCOPPER class data taken by ECL Currently, this class is almost same as...
Definition RawECL.h:26
The Raw FTSW class.
Definition RawFTSW.h:30
The Raw Header class ver.1 ( the latest version since May, 2014 ) This class defines the format of th...
void SetBuffer(int *bufin)
set buffer
The Raw KLM class Class for RawCOPPER class data taken by KLM.
Definition RawKLM.h:27
The Raw SVD class Class for RawCOPPER class data taken by SVD Currently, this class is almost same as...
Definition RawSVD.h:26
The Raw TOP class Class for RawCOPPER class data taken by TOP Currently, this class is almost same as...
Definition RawTOP.h:27
The Raw TOP class Class for RawCOPPER class data taken by TOP Currently, this class is almost same as...
Definition RawTRG.h:27
Root2RawModule()
Constructor / Destructor.
Definition Root2Raw.cc:43
void initialize() override
Module functions to be called from main process.
Definition Root2Raw.cc:54
void event() override
Module functions to be called from event process.
Definition Root2Raw.cc:82
void endRun() override
This method is called if the current run ends.
Definition Root2Raw.cc:67
void terminate() override
This method is called at the end of the event processing.
Definition Root2Raw.cc:75
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
void Initialize()
set buffer
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition StoreArray.h:216
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
unsigned int GetEveNo()
get restart #(8bit)
unsigned int GetExpRunSubrun()
get contents of header
Abstract base class for different kinds of events.
STL namespace.