Belle II Software  release-08-01-10
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 
29 using namespace std;
30 using namespace Belle2;
31 using namespace Belle2::SVD;
32 //-----------------------------------------------------------------
33 // Register the Module
34 //-----------------------------------------------------------------
35 REG_MODULE(SVDPacker);
36 
37 //-----------------------------------------------------------------
38 // Implementation
39 //-----------------------------------------------------------------
40 
41 std::string Belle2::SVD::SVDPackerModule::m_xmlFileName = std::string("SVDChannelMapping.xml");
42 
43 SVDPackerModule::SVDPackerModule() : Module(),
44  m_mapping(m_xmlFileName),
45  m_FADCTriggerNumberOffset(0)
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 
66 {
67 }
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
206  addData32(data32);
207 
208  m_FTBHeader.errorsField = 0xf0;
209  m_FTBHeader.eventNumber = (m_eventMetaDataPtr->getEvent() & 0xFFFFFF);
210 
211  addData32(data32);
212 
213  // here goes FADC header
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 
225  addData32(data32);
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;
240  m_APVHeader.apvErr = 0;
241 
243  m_APVHeader.APVnum = iAPV;
244  m_APVHeader.check = 2;
245 
246  addData32(data32);
247 
248  // alias for table element (DataInfo object)
249  vector<DataInfo>& apv_data_vec = fadc_apv_matrix[iFADC][iAPV];
250 
251  if (apv_data_vec.size() > 0) { // if any data for given FADC/APV
252  for (std::vector<DataInfo>::iterator apv_data = apv_data_vec.begin(); apv_data != apv_data_vec.end(); ++apv_data) {
253 
254  // here go DATA words
255 
256  //skip 1st data frame if simulate 3-sample data
257  if (m_svdEventInfoPtr->getNSamples() == 6) {
258  m_data_A.sample1 = apv_data->data[0];
259  m_data_A.sample2 = apv_data->data[1];
260  m_data_A.sample3 = apv_data->data[2];
261  m_data_A.stripNum = apv_data->channel;
262  m_data_A.check = 0;
263 
264  addData32(data32);
265  }
266 
267  m_data_B.sample4 = apv_data->data[3];
268  m_data_B.sample5 = apv_data->data[4];
269  m_data_B.sample6 = apv_data->data[5];
270  m_data_B.stripNum = apv_data->channel;
271  m_data_B.check = 0;
272 
273  addData32(data32);
274 
275  }
276  }
277 
278  } // end APV loop
279 
280  // here goes FADC trailer
288  m_FADCTrailer.check = 14;
289 
290  addData32(data32);
291 
292 
293  // crc16 calculation
294  //first swap all 32-bits word -> big endian
295  unsigned short nCRC = data_words.size();
296  uint32_t tmpBuffer[nCRC];
297 
298  for (unsigned short i = 0; i < nCRC; i++)
299  tmpBuffer[i] = htonl(data_words[i]);
300 
301  //compute crc16
302  boost::crc_basic<16> bcrc(0x8005, 0xffff, 0, false, false);
303  bcrc.process_block(tmpBuffer, tmpBuffer + nCRC);
304  unsigned int crc = bcrc.checksum();
305 
306 
307  // here goes FTB trailer
308  m_FTBTrailer.crc16 = crc;
309  m_FTBTrailer.controlWord = 0xff55;
310 
311  addData32(data32);
312 
313 
314  // ******* modified and moved inside FADC loop **********
315 
316  unsigned int nwords_1st = data_words.size();
317  unsigned int nwords_2nd = 0;
318  unsigned int nwords_3rd = 0;
319  unsigned int nwords_4th = 0;
320 
321  int* buf1 = new int[nwords_1st];
322  int* buf2 = nullptr;
323  int* buf3 = nullptr;
324  int* buf4 = nullptr;
325 
326  // filling buffers
327  for (unsigned int j = 0; j < nwords_1st; j++) {
328  buf1[j] = data_words[j];
329  }
330 
331  raw_svd->PackDetectorBuf(buf1, nwords_1st,
332  buf2, nwords_2nd,
333  buf3, nwords_3rd,
334  buf4, nwords_4th,
335  rawcprpacker_info);
336 
337  delete [] buf1;
338 
339  // ********************************************************
340 
341  } // end FADC loop
342 
343 
344  if (m_binPrintout) binPrintout(data_words.size());
345 
346  delete [] fadc_apv_matrix;
347 
348  n_basf2evt++;
349 
350 } // end event function
351 #ifndef __clang__
352 #pragma GCC diagnostic pop
353 #endif
354 
355 
357 {
358 }
359 
360 
362 {
363 }
364 
365 
366 
367 // function for printing 32-bit frames in binary mode
368 void SVDPackerModule::binPrintout(unsigned int nwords)
369 {
370 
371  B2INFO("\nbinary printout:");
372  for (unsigned int j = 0; j < nwords; j++) {
373 
374  uint32_t ulFlag = 1;
375  ulFlag <<= (sizeof(data_words[j]) * 8 - 1);
376  for (; ulFlag > 0; ulFlag >>= 1)
377  printf("%d", (data_words[j] & ulFlag) ? 1 : 0);
378 
379  cout << endl;
380  }
381 
382 }
383 
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
Base class for Modules.
Definition: Module.h:72
const std::string & getType() const
Returns the type of the module (i.e.
Definition: Module.cc:41
std::string getFileName() const
Get the name of the downloaded payload file.
Definition: PayloadFile.h:35
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.
Definition: SVDModeByte.h:146
baseType getTriggerBin() const
Get the triggerBin id.
Definition: SVDModeByte.h:140
baseType getDAQMode() const
Get the daqMode id.
Definition: SVDModeByte.h:142
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 numnber offset.
static std::string m_xmlFileName
< xml filename
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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
Definition: GeoSVDCreator.h:23
Abstract base class for different kinds of events.
Struct to hold data about an APV25 chip.
unsigned int detectErr
Detection Error.
unsigned int pipelineAddr
Pipeline Address.
unsigned int APVnum
APV chip number.
unsigned int CMC1
Common Mode Noise w/o masking out particle signals.
unsigned int check
MSB "10" - for APV Header identification.
unsigned int fifoErr
FIFO full Error.
unsigned int CMC2
Common Mode Noise after masking out particle signals.
unsigned short channel
APV channel number.
unsigned int frameErrOR
Frame Error OR.
unsigned int apvErrOR
APV chip number OR.
unsigned int fifoErrOR
FIFO full Error OR.
unsigned int check
MSB "1110" - for FADC Trailer identification.
unsigned int FTBFlags
FTB Flags Field.
unsigned int dataSizeCut
APV data-size cut flag
unsigned int detectErrOR
Detection Error OR.
unsigned int errorsField
FTB error fields.
unsigned int eventNumber
FTB event number.
unsigned int controlWord
MSB "ff55" - FADC Trailer ID.
unsigned int crc16
FTB CRC16 Checksum
unsigned int DAQType
(from 2020c) Event type(0): "0"…3 or …6 acquisition mode, "1"…3-mixed-6 acquisition mode
unsigned int check
MSB "110" - for FADC Header identification.
unsigned int trgNumber
Trigger Number.
unsigned int trgTiming
Trigger Timing.
unsigned int DAQMode
Event type(2:1): "00"…1-sample, "01"…3-sample, "10"…6-sample.
unsigned int sample3
3rd data sample
unsigned int stripNum
Strip number.
unsigned int sample2
2nd data sample
unsigned int check
MSB "1" - for Data word identification.
unsigned int sample1
1st data sample
unsigned int sample6
6th data sample
unsigned int stripNum
Strip number.
unsigned int check
MSB "1" - for Data word identification.
unsigned int sample4
4th data sample
unsigned int sample5
5th data sample