Belle II Software development
SVDPackerModule.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 <svd/modules/svdPacker/SVDPackerModule.h>
10#include <svd/online/SVDOnlineToOfflineMap.h>
11
12#include <framework/datastore/DataStore.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/datastore/RelationArray.h>
15#include <framework/datastore/RelationIndex.h>
16#include <framework/logging/Logger.h>
17#include <framework/utilities/FileSystem.h>
18
19#include <arpa/inet.h>
20#include <boost/crc.hpp>
21
22#include <cstring>
23#include <iomanip>
24#include <iostream>
25#include <sstream>
26#include <vector>
27
28
29using namespace std;
30using namespace Belle2;
31using namespace Belle2::SVD;
32//-----------------------------------------------------------------
33// Register the Module
34//-----------------------------------------------------------------
35REG_MODULE(SVDPacker);
36
37//-----------------------------------------------------------------
38// Implementation
39//-----------------------------------------------------------------
40
41std::string Belle2::SVD::SVDPackerModule::m_xmlFileName = std::string("SVDChannelMapping.xml");
42
46{
47 // Set module properties
48
49
50 // Parameter definitions
51 addParam("SVDEventInfo", m_svdEventInfoName, "Name of the SVDEventInfo object", string("SVDEventInfoSim"));
52 addParam("svdShaperDigitListName", m_svdShaperDigitListName, "Name of the SVD Shaper Digits List", string(""));
53 addParam("rawSVDListName", m_rawSVDListName, "Name of the raw SVD List", string(""));
54 //addParam("xmlMapFileName", m_xmlMapFileName, "path+name of the xml file", FileSystem::findFile("data/svd/svd_mapping.xml"));
55 addParam("NodeID", m_nodeid, "Node ID", 0);
56 addParam("FADCTriggerNumberOffset", m_FADCTriggerNumberOffset,
57 "number to be added to the FADC trigger number to match the main trigger number", 0);
58 addParam("binPrintout", m_binPrintout, "Print binary data created by the Packer", bool(false));
59 // initialize event #
60 n_basf2evt = 0;
61
62}
63
64
68
69
70
72{
73
74 m_rawSVD.registerInDataStore(m_rawSVDListName);
76 m_eventMetaDataPtr.isRequired();
78}
79
80
82{
83 if (m_mapping.hasChanged()) { m_map = std::make_unique<SVDOnlineToOfflineMap>(m_mapping->getFileName()); }
84
85 //number of FADC boards
86 nFADCboards = m_map->getFADCboardsNumber();
87
88 // filling FADCnumberMap and FADCnumberMapRev
89 m_map->prepFADCmaps(FADCnumberMap, FADCnumberMapRev);
90
91 // passing APV<->FADC mapping from SVDOnlineToOfflineMap object
92 APVmap = &(m_map->APVforFADCmap);
93
94}
95
96
97#ifndef __clang__
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-Wstack-usage="
100#endif
102{
103
104 if (!m_eventMetaDataPtr.isValid()) { // give up...
105 B2ERROR("Missing valid EventMetaData.");
106 return;
107 }
108
109 if (!m_svdEventInfoPtr.isValid()) {
110 B2ERROR("Missing valid SVDEventInfo.");
111 return;
112 }
113
114 // retrieve the information from SVDEventInfo to store it in the FADCHeaders for this event
115 SVDModeByte modeByte = m_svdEventInfoPtr->getModeByte();
116
117 uint8_t triggerBin = modeByte.getTriggerBin();
118 uint8_t runType = modeByte.getRunType();
119 uint8_t daqMode = modeByte.getDAQMode();
120 uint8_t daqType = 0;
121 if (daqMode == 3) daqType = 1;
122
123 uint8_t xTalk = m_svdEventInfoPtr->isCrossTalkEvent();
124 uint8_t triggerType = (m_svdEventInfoPtr->getTriggerType()).getType();
125
126
127 m_rawSVD.clear();
128
129 if (! m_map) {
130 B2ERROR("xml map not loaded, going to the next module");
131 return;
132 }
133
134
135 RawCOPPERPackerInfo rawcprpacker_info;
136 rawcprpacker_info.exp_num = 0;
137 rawcprpacker_info.run_subrun_num = 1; // run number : 14bits, subrun # : 8bits
138 rawcprpacker_info.eve_num = n_basf2evt;
139 rawcprpacker_info.node_id = m_nodeid;
140 rawcprpacker_info.tt_ctime = 0x7123456;
141 rawcprpacker_info.tt_utime = 0xF1234567;
142 rawcprpacker_info.b2l_ctime = 0x7654321;
143
144
145 unsigned int nEntries_SVDShaperDigits = m_svdShaperDigit.getEntries();
146
147 // DataInfo contains info on 6 samples and strip number
148 vector<DataInfo> (*fadc_apv_matrix)[48] = new
149 vector<DataInfo>[nFADCboards][48]; // for all layers, nFADCboards FADCs and at most 48 APV25 for each
150
151 for (unsigned int i = 0; i < nEntries_SVDShaperDigits; i++) {
152
153 const SVDShaperDigit* hit = m_svdShaperDigit[i];
154
155 short int cellID = hit->getCellID();
156 VxdID sensID = hit->getSensorID();
157 bool isU = hit->isUStrip();
158
159 int sensor = sensID.getSensorNumber();
160 int ladder = sensID.getLadderNumber();
161 int layer = sensID.getLayerNumber();
162
163 // find FADC/APV for given DSSD sensor
164 const SVDOnlineToOfflineMap::ChipInfo& CHIP_info = m_map->getChipInfo(layer, ladder, sensor, isU, cellID);
165 unsigned short fadc = CHIP_info.fadc;
166 unsigned short apv = CHIP_info.apv;
167 unsigned short apvChannel = CHIP_info.apvChannel;
168
169 //do not create data words for APV missing in the hardware mapping
170 if (fadc == 0) continue;
171
172 // return 0-47 for given FADC number
173 auto fadcIter = FADCnumberMap.find(fadc);
174
175 //getting charge samples from SVDShaperDigit object
176 SVDShaperDigit::APVFloatSamples samples = hit->getSamples();
177
178 // fill DataInfo
179 for (unsigned short j = 0; j < 6; j++) {
180 dataInfo.data[j] = samples[j];
181 }
182 dataInfo.channel = apvChannel;
183
184 //fill fadc_apv_matrix[fadc][apv] with DataInfo object
185 fadc_apv_matrix[fadcIter->second][apv].push_back(dataInfo);
186
187 }// end of the loop that fills fadc_apv_matrix !!!!
188
189
190 for (unsigned int iFADC = 0; iFADC < nFADCboards; iFADC++) {
191
192 //get original FADC number
193 unsigned short FADCorg = FADCnumberMapRev[iFADC];
194
195 //new RawSVD entry --> moved inside FADC loop
196 RawSVD* raw_svd = m_rawSVD.appendNew();
197 data_words.clear();
198
199
200 //--------------- start creating Raw Data words
201
202 // here goes FTB header
203 data32 = 0xffaa0000;
204
205 //adds data32 to data vector
207
208 m_FTBHeader.errorsField = 0xf0;
209 m_FTBHeader.eventNumber = (m_eventMetaDataPtr->getEvent() & 0xFFFFFF);
210
212
213 // here goes FADC header
214 m_MainHeader.trgNumber = ((m_eventMetaDataPtr->getEvent() - m_FADCTriggerNumberOffset) & 0xFF);
215 m_MainHeader.trgType = triggerType;
216 m_MainHeader.trgTiming = triggerBin;
217 m_MainHeader.xTalk = xTalk;
218 m_MainHeader.FADCnum = FADCorg; // write original FADC number
219 m_MainHeader.DAQType = daqType;
220 m_MainHeader.DAQMode = daqMode;
221
222 m_MainHeader.runType = runType; //zero-suppressed mode
223 m_MainHeader.check = 6; // 110
224
226
227 // getting a set of APV numbers for current FADC
228 auto apv_range = APVmap->equal_range(FADCorg);
229
230 for (auto& it = apv_range.first; it != apv_range.second; ++it) {
231 unsigned short iAPV = it->second;
232
233 // here goes APV header
234 m_APVHeader.CMC1 = 0;
235 m_APVHeader.CMC2 = 0;
236
237 m_APVHeader.fifoErr = 0;
238 m_APVHeader.frameErr = 0;
239 m_APVHeader.detectErr = 0;
240 m_APVHeader.apvErr = 0;
241 m_APVHeader.pipelineAddr = 0;
242 m_APVHeader.APVnum = iAPV;
243 m_APVHeader.check = 2;
244
246
247 // alias for table element (DataInfo object)
248 vector<DataInfo>& apv_data_vec = fadc_apv_matrix[iFADC][iAPV];
249
250 if (apv_data_vec.size() > 0) { // if any data for given FADC/APV
251 for (std::vector<DataInfo>::iterator apv_data = apv_data_vec.begin(); apv_data != apv_data_vec.end(); ++apv_data) {
252
253 // here go DATA words
254
255 //skip 1st data frame if simulate 3-sample data
256 if (m_svdEventInfoPtr->getNSamples() == 6) {
257 m_data_A.sample1 = apv_data->data[0];
258 m_data_A.sample2 = apv_data->data[1];
259 m_data_A.sample3 = apv_data->data[2];
260 m_data_A.stripNum = apv_data->channel;
261 m_data_A.check = 0;
262
264 }
265
266 m_data_B.sample4 = apv_data->data[3];
267 m_data_B.sample5 = apv_data->data[4];
268 m_data_B.sample6 = apv_data->data[5];
269 m_data_B.stripNum = apv_data->channel;
270 m_data_B.check = 0;
271
273
274 }
275 }
276
277 } // end APV loop
278
279 // here goes FADC trailer
280 m_FADCTrailer.FTBFlags = 0;
281 m_FADCTrailer.APVdataSizeCut = 0;
282 m_FADCTrailer.FADCdataSizeCut = 0;
283 m_FADCTrailer.DummyAPVHeader = 0;
284 m_FADCTrailer.PiplAddrMismatch = 0;
285 m_FADCTrailer.nullDigits = 0;
286 m_FADCTrailer.fifoErrOR = 0;
287 m_FADCTrailer.frameErrOR = 0;
288 m_FADCTrailer.detectErrOR = 0;
289 m_FADCTrailer.apvErrOR = 0;
290 m_FADCTrailer.check = 14;
291
293
294
295 // crc16 calculation
296 //first swap all 32-bits word -> big endian
297 unsigned short nCRC = data_words.size();
298 std::vector<uint32_t> tmpBuffer;
299 tmpBuffer.reserve(nCRC);
300
301 for (unsigned short i = 0; i < nCRC; i++)
302 tmpBuffer.push_back(htonl(data_words[i]));
303
304 //compute crc16
305 boost::crc_basic<16> bcrc(0x8005, 0xffff, 0, false, false);
306 bcrc.process_bytes(tmpBuffer.data(), tmpBuffer.size() * sizeof(uint32_t));
307 unsigned int crc = bcrc.checksum();
308
309
310 // here goes FTB trailer
311 m_FTBTrailer.crc16 = crc;
312 m_FTBTrailer.controlWord = 0xff55;
313
315
316
317 // ******* modified and moved inside FADC loop **********
318
319 unsigned int nwords_1st = data_words.size();
320 unsigned int nwords_2nd = 0;
321 unsigned int nwords_3rd = 0;
322 unsigned int nwords_4th = 0;
323
324 int* buf1 = new int[nwords_1st];
325 int* buf2 = nullptr;
326 int* buf3 = nullptr;
327 int* buf4 = nullptr;
328
329 // filling buffers
330 for (unsigned int j = 0; j < nwords_1st; j++) {
331 buf1[j] = data_words[j];
332 }
333
334 raw_svd->PackDetectorBuf(buf1, nwords_1st,
335 buf2, nwords_2nd,
336 buf3, nwords_3rd,
337 buf4, nwords_4th,
338 rawcprpacker_info);
339
340 delete [] buf1;
341
342 // ********************************************************
343
344 } // end FADC loop
345
346
348
349 delete [] fadc_apv_matrix;
350
351 n_basf2evt++;
352
353} // end event function
354#ifndef __clang__
355#pragma GCC diagnostic pop
356#endif
357
358
362
363
365{
366}
367
368
369
370// function for printing 32-bit frames in binary mode
371void SVDPackerModule::binPrintout(unsigned int nwords)
372{
373
374 B2INFO("\nbinary printout:");
375 for (unsigned int j = 0; j < nwords; j++) {
376
377 uint32_t ulFlag = 1;
378 ulFlag <<= (sizeof(data_words[j]) * 8 - 1);
379 for (; ulFlag > 0; ulFlag >>= 1)
380 printf("%d", (data_words[j] & ulFlag) ? 1 : 0);
381
382 cout << endl;
383 }
384
385}
386
Module()
Constructor.
Definition Module.cc:30
struct to contain header information used by RawCOPPERFormat::Packer()
unsigned int b2l_ctime
32bit unitx time at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user ...
unsigned int eve_num
Run # and subrun # ( 22bit )
unsigned int tt_ctime
Node ID (32bit)
unsigned int tt_utime
27bit clock ticks at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user...
unsigned int node_id
Event Number (32bit)
unsigned int run_subrun_num
Experiment number (10bit)
unsigned int exp_num
Experiment number (10bit)
void PackDetectorBuf(int *detector_buf_1st, int nwords_1st, int *detector_buf_2nd, int nwords_2nd, int *detector_buf_3rd, int nwords_3rd, int *detector_buf_4th, int nwords_4th, RawCOPPERPackerInfo rawcprpacker_info)
Packer for RawCOPPER class Pack data (format ver.
Definition RawCOPPER.cc:183
The Raw SVD class Class for RawCOPPER class data taken by SVD Currently, this class is almost same as...
Definition RawSVD.h:26
Class to store SVD mode information.
Definition SVDModeByte.h:69
baseType getRunType() const
Get the runMode id.
baseType getTriggerBin() const
Get the triggerBin id.
baseType getDAQMode() const
Get the daqMode id.
The SVD ShaperDigit class.
std::array< APVFloatSampleType, c_nAPVSamples > APVFloatSamples
array of APVFloatSampleType objects
MainHeader m_MainHeader
Implementation of FADC Header.
StoreArray< RawSVD > m_rawSVD
output for RawSVD
FADCTrailer m_FADCTrailer
Implementation of FADC Trailer.
StoreObjPtr< SVDEventInfo > m_svdEventInfoPtr
SVDEventInfo from simulation.
virtual void initialize() override
initialize
virtual void event() override
event
struct Belle2::SVD::SVDPackerModule::DataInfo dataInfo
data info
FTBHeader m_FTBHeader
Implementation of FTB Header.
virtual void endRun() override
end run
std::string m_rawSVDListName
RawSVD StoreArray name.
StoreArray< SVDShaperDigit > m_svdShaperDigit
Required input for SVDShaperDigit.
DBObjPtr< PayloadFile > m_mapping
channel map payload
virtual void terminate() override
terminate
data_B m_data_B
Implementation of 2nd data word.
int m_FADCTriggerNumberOffset
FADC trigger number offset.
static std::string m_xmlFileName
< xml filename
SVDPackerModule()
default constructor
void addData32(uint32_t adata32)
adds packed 32-bit data word to the raw data vector
FTBTrailer m_FTBTrailer
Implementation of FTB Trailer.
APVHeader m_APVHeader
Implementation of APV Header.
virtual void beginRun() override
begin run
std::string m_svdShaperDigitListName
SVDShaperDigit StoreArray name.
std::string m_svdEventInfoName
SVDEventInfo name.
virtual ~SVDPackerModule()
default destructor
data_A m_data_A
Implementation of 1st data word.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Required input for EventMetaData.
std::unordered_multimap< unsigned char, unsigned char > * APVmap
pointer to APVforFADCmap filled by mapping procedure
std::unique_ptr< SVDOnlineToOfflineMap > m_map
Pointer to online-to-offline map.
uint32_t data32
Output 32-bit data word.
std::vector< uint32_t > data_words
vector of raw data words
void binPrintout(unsigned int nwords)
tool: print out N words
bool m_binPrintout
if true, print data created by the Packer
FADCmap FADCnumberMapRev
maps containing assignment (0,1,2,3,4,..,nFADCboards-1) <-> FADC numbers
FADCmap FADCnumberMap
maps containing assignment (0,1,2,3,4,..,nFADCboards-1) <-> FADC numbers
unsigned short nFADCboards
how many FADCs we have
Class to uniquely identify a any structure of the PXD and SVD.
Definition VxdID.h:33
baseType getSensorNumber() const
Get the sensor id.
Definition VxdID.h:100
baseType getLadderNumber() const
Get the ladder id.
Definition VxdID.h:98
baseType getLayerNumber() const
Get the layer id.
Definition VxdID.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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
Abstract base class for different kinds of events.
STL namespace.
Struct to hold data about an APV25 chip.