11 #include <pxd/unpacking/PXDRawDataDefinitions.h>
12 #include <pxd/modules/pxdUnpacking/PXDPackerModule.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/dataobjects/EventMetaData.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 #include <framework/core/ModuleParam.templateDetails.h>
19 #include <boost/crc.hpp>
20 #include <boost/algorithm/clamp.hpp>
22 #include <pxd/unpacking/PXDMappingLookup.h>
40 using boost::crc_optimal;
41 typedef crc_optimal<32, 0x04C11DB7, 0, 0, false, false> dhe_crc_32_type;
47 PXDPackerModule::PXDPackerModule() :
59 addParam(
"PXDDigitsName",
m_PXDDigitsName,
"The name of the StoreArray of PXDDigits to be processed", std::string(
""));
60 addParam(
"RawPXDsName",
m_RawPXDsName,
"The name of the StoreArray of generated RawPXDs", std::string(
""));
62 addParam(
"dhe_to_dhc",
m_dhe_to_dhc,
"DHE to DHC mapping (DHC_ID, DHE1, DHE2, ..., DHE5) ; -1 disable port");
63 addParam(
"InvertMapping",
m_InvertMapping,
"Use invers mapping to DHP row/col instead of \"remapped\" coordinates",
false);
86 B2DEBUG(27,
"PXD Packer --> DHC/DHE");
90 B2WARNING(
"PXD Packer --> DHC/DHE maps 1 dhc to 5 dhe (1+5 values), but I found " << it.size());
92 for (
auto& it2 : it) {
96 B2DEBUG(27,
"PXD Packer --> ... DHE " << it2);
97 if (it2 < -1 || it2 >= 64) {
98 if (it2 != -1) B2ERROR(
"PXD Packer --> DHC id " << it2 <<
" is out of range (0-64 or -1)! disable channel.");
105 B2DEBUG(27,
"PXD Packer --> DHC .. " << it2);
106 if (dhc_id < 0 || dhc_id >= 16) {
107 B2ERROR(
"PXD Packer --> DHC id " << it2 <<
" is out of range (0-15)! skip");
114 B2DEBUG(27,
"PXD Packer --> DHC/DHE done");
122 B2DEBUG(27,
"PXD Packer --> DHC " << it.first);
123 for (
auto& it2 : it.second) {
124 B2DEBUG(27,
"PXD Packer --> .. connects to DHE " << it2 <<
" port " << port);
152 B2DEBUG(27,
"PXD Packer --> Nr of Digis: " << nDigis);
156 VxdID lastVxdId = -1;
161 currentVxdId = it->getSensorID();
163 if (currentVxdId != lastVxdId) {
165 lastVxdId = currentVxdId;
166 B2DEBUG(27,
"VxdId: " << currentVxdId <<
" " << (
int)currentVxdId);
168 unsigned int layer, ladder, sensor, segment, dhe_id;
169 layer = currentVxdId.getLayerNumber();
170 ladder = currentVxdId.getLadderNumber();
171 sensor = currentVxdId.getSensorNumber();
172 segment = currentVxdId.getSegmentNumber();
173 dhe_id = ((layer - 1) << 5) | ((ladder) << 1) | (sensor - 1);
174 B2DEBUG(27,
"Layer: " << layer <<
" Ladder " << ladder <<
" Sensor " << sensor <<
" Segment(Frame) " << segment <<
" =>DHEID: " <<
178 if (
startOfVxdID.count(currentVxdId) > 0) B2FATAL(
"PXD Digits are not sorted by VxdID!");
185 m_run_nr_word1 = ((evtPtr->getRun() & 0xFF) << 8) | (evtPtr->getSubrun() & 0xFF);
186 m_run_nr_word2 = ((evtPtr->getExperiment() & 0x3FF) << 6) | ((evtPtr->getRun() >> 8) & 0x3F);
196 int dhe_ids[5] = {0, 0, 0, 0, 0};
197 B2DEBUG(27,
"PXD Packer --> pack_event");
202 int port = 1, port_inx = 0;
205 for (
auto& it2 : it.second) {
206 if (it2 >= 0) act_port += port;
208 dhe_ids[port_inx] = it2;
210 if (port_inx == 5)
break;
220 pack_dhc(it.first, act_port, dhe_ids);
231 B2ERROR(
"Frame is not 32bit aligned!!! Unsupported by Unpacker!");
234 dhe_crc_32_type current_crc;
275 B2DEBUG(27,
"PXD Packer --> pack_dhc ID " << dhc_id <<
" DHE act: " << dhe_active);
294 append_int32((EDHCFrameHeaderDataType::c_DHC_START << 27) | ((dhc_id & 0xF) << 21) | ((dhe_active & 0x1F) << 16) |
298 uint32_t mm = (
unsigned int)std::round((
m_meta_time % 1000000000ull) * 0.127216);
299 uint32_t ss = (
unsigned int)(
m_meta_time / 1000000000ull) ;
301 append_int16(((mm >> 12) & 0x7FFF) | ((ss & 1) ? 0x8000 : 0x0));
310 for (
int i = 0; i < 5; i++) {
311 if (dhe_active & 0x1)
pack_dhe(dhe_ids[i], 0xF);
333 B2DEBUG(27,
"PXD Packer --> pack_dhe ID " << dhe_id <<
" DHP act: " << dhp_active);
340 B2FATAL(
"Inverse Mapping not implemented in Packer");
346 append_int32((EDHCFrameHeaderDataType::c_DHE_START << 27) | ((dhe_id & 0x3F) << 20) | ((dhp_active & 0xF) << 16) |
362 if (dhp_active != 0) {
364 const int ladder_max_row = PACKER_NUM_ROWS - 1;
366 const int ladder_max_col = PACKER_NUM_COLS - 1;
371 VxdID currentVxdId = 0;
378 unsigned short sensor, ladder, layer;
379 sensor = (dhe_id & 0x1) + 1;
380 ladder = (dhe_id & 0x1E) >> 1;
381 layer = ((dhe_id & 0x20) >> 5) + 1;
382 currentVxdId =
VxdID(layer, ladder, sensor);
384 B2DEBUG(27,
"pack_dhe: VxdId: " << currentVxdId <<
" " << (
int)currentVxdId);
391 auto id = it->getSensorID();
392 id.setSegmentNumber(0);
393 if (currentVxdId !=
id)
break;
396 unsigned int row, col;
397 row = it->getVCellID();
398 col = it->getUCellID();
399 if (row > ladder_max_row || col > ladder_max_col) {
400 B2ERROR(
"U/V out of range U: " << col <<
" V: " << row);
403 B2DEBUG(26,
"Pixel: V: " << row <<
", U: " << col <<
", Ch " << it->getCharge());
404 if (!dhe_has_remapped) {
405 do_the_reverse_mapping(row, col, layer, sensor);
407 halfladder_pixmap[row][col] = (
unsigned char) boost::algorithm::clamp(lrint(it->getCharge()), 0, 255);
414 B2FATAL(
"Clusterizer not supported in Packer");
416 for (
int i = 0; i < 4; i++) {
417 if (dhp_active & 0x1) {
418 pack_dhp(i, dhe_id, dhe_has_remapped ? 1 : 0);
440 void PXDPackerModule::do_the_reverse_mapping(
unsigned int& ,
unsigned int& ,
unsigned short ,
443 B2FATAL(
"code needs to be written");
452 B2FATAL(
"This code needs to be checked agains new firmware");
453 B2DEBUG(27,
"PXD Packer --> pack_dhp Raw Chip " << chip_id <<
" of DHE id: " << dhe_id);
456 append_int32((EDHCFrameHeaderDataType::c_DHP_RAW << 27) | ((dhe_id & 0x3F) << 20) | ((chip_id & 0x03) << 16) |
458 append_int32((EDHPFrameHeaderDataType::c_RAW << 29) | ((dhe_id & 0x3F) << 18) | ((chip_id & 0x03) << 16) |
464 if (c2 >= PACKER_NUM_COLS) c2 = PACKER_NUM_COLS;
467 for (
int row = 0; row < PACKER_NUM_ROWS; row++) {
468 for (
int col = c1; col < c2; col++) {
472 for (
int col = c2; col < c1 + 64; col++) {
482 B2DEBUG(27,
"PXD Packer --> pack_dhp Chip " << chip_id <<
" of DHE id: " << dhe_id);
485 unsigned short last_rowstart = 0;
487 if (dhe_has_remapped == 0) {
490 assert(dhe_has_remapped == 0);
495 append_int32((EDHCFrameHeaderDataType::c_DHP_ZSD << 27) | ((dhe_id & 0x3F) << 20) | ((dhe_has_remapped & 0x1) << 19) | ((
497 append_int32((EDHPFrameHeaderDataType::c_ZSD << 29) | ((dhe_id & 0x3F) << 18) | ((chip_id & 0x03) << 16) |
503 if (c2 >= PACKER_NUM_COLS) c2 = PACKER_NUM_COLS;
504 for (
int rr = startrow; rr < startrow + PACKER_NUM_ROWS; rr++) {
505 int row = (rr % PACKER_NUM_ROWS);
506 bool rowstart =
true;
507 for (
int col = c1; col < c2; col++) {
509 B2DEBUG(26,
"Pixel: ROW: " << row <<
", COL: " << col <<
", Ch " << (
int)
halfladder_pixmap[row][col]);
511 last_rowstart = ((row & 0x3FE) << (6 - 1)) | 0;
522 B2DEBUG(27,
"Repeat last rowstart to align to 32bit.");
528 B2DEBUG(27,
"Found no data for halfladder! DHEID: " << dhe_id <<
" Chip: " << chip_id);
534 append_int32((EDHCFrameHeaderDataType::c_GHOST << 27) | ((dhe_id & 0x3F) << 20) | ((chip_id & 0x03) << 16) |