9 #include <pxd/unpacking/PXDRawDataDefinitions.h>
10 #include <pxd/modules/pxdUnpacking/PXDPackerErrModule.h>
11 #include <framework/datastore/DataStore.h>
12 #include <framework/logging/Logger.h>
13 #include <framework/dataobjects/EventMetaData.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/core/ModuleParam.templateDetails.h>
17 #include <boost/crc.hpp>
26 using namespace Belle2::PXD::PXDError;
34 using boost::crc_optimal;
35 typedef crc_optimal<32, 0x04C11DB7, 0, 0, false, false> dhc_crc_32_type;
40 std::vector <PXDErrorFlags> PXDPackerErrModule::m_errors = {
48 c_META_MM | c_META_MM_DHC | c_META_MM_DHE | c_META_MM_ONS_HLT | c_META_MM_ONS_DC,
49 c_META_MM | c_META_MM_DHC | c_META_MM_DHE | c_META_MM_ONS_HLT | c_META_MM_ONS_DC,
50 c_META_MM_DHC_ERS | c_META_MM_ONS_HLT | c_META_MM_ONS_DC,
51 c_META_MM_DHC_ERS | c_META_MM_ONS_HLT | c_META_MM_ONS_DC,
52 c_META_MM_DHC_ERS | c_META_MM_ONS_HLT | c_META_MM_ONS_DC,
69 c_ONSEN_TRG_FIRST | c_DHC_START_SECOND | c_DHE_START_THIRD | c_EVENT_STRUCT,
72 c_DHE_END_WO_START | c_DHE_START_THIRD,
79 c_ONSEN_TRG_FIRST | c_DHC_START_SECOND | c_DHE_START_THIRD | c_EVENT_STRUCT,
80 c_DHC_START_SECOND | c_DHE_START_THIRD,
82 c_DHE_WRONG_ID_SEQ | c_DHE_START_WO_END,
88 c_DHE_END_WO_START | c_DHE_START_END_ID,
89 c_MERGER_TRIGNR | c_META_MM_ONS_DC,
92 c_NO_ERROR | c_NOTSENDALL_TYPE,
98 c_MERGER_TRIGNR | c_META_MM_ONS_HLT,
101 c_DHE_START_THIRD | c_DHE_ACTIVE,
108 c_DHC_DHCID_START_END_MM,
118 c_META_MM_ONS_HLT | c_MERGER_TRIGNR,
138 c_UNEXPECTED_FRAME_TYPE,
139 c_UNEXPECTED_FRAME_TYPE,
140 c_UNEXPECTED_FRAME_TYPE,
150 c_NR_FRAMES_TO_SMALL,
161 c_FRAME_TNR_MM | c_META_MM | c_META_MM_DHC,
162 c_FRAME_TNR_MM | c_META_MM | c_META_MM_DHE,
168 c_FRAME_TNR_MM | c_META_MM,
170 c_DHE_START_ID | c_DHE_DHP_DHEID,
203 c_ROI_PACKET_INV_SIZE,
204 c_ROI_PACKET_INV_SIZE,
209 bool PXDPackerErrModule::CheckErrorMaskInEvent(
unsigned int eventnr, PXDErrorFlags mask)
213 PXDErrorFlags expected;
214 if (eventnr > 0 && eventnr < m_errors.size()) {
215 expected = m_errors[eventnr];
217 B2INFO(
"-- PXD Packer Error Check for Event Nr: " << eventnr);
218 for (
int i = 0; i < ONSEN_MAX_TYPE_ERR; i++) {
221 if ((m & (mask | expected)) == m) {
222 if ((m & expected) == m && (m & mask) != m) {
223 B2ERROR(
"Bit " << i <<
": Was NOT Set: " << getPXDBitErrorName(i));
224 m_found_fatal =
true;
225 }
else if ((m & expected) != m && (m & mask) == m) {
226 B2RESULT(
"Bit " << i <<
": Optional : " << getPXDBitErrorName(i));
227 }
else if ((m & expected) == m && (m & mask) == m) {
228 B2INFO(
"Bit " << i <<
": As Expected: " << getPXDBitErrorName(i));
232 bool flag = (mask & expected) == expected;
233 if (expected == PXDErrorFlags(0)) {
235 if (mask != PXDErrorFlags(0)) {
236 B2ERROR(
"There should be no error in this event, but there were (see above)!");
237 m_found_fatal =
true;
239 flag = (mask == PXDErrorFlags(0));
241 B2INFO(
"-- PXD Packer Error Check END --- ");
243 if (m_found_fatal) B2FATAL(
"This check failed (see details above)!");
247 PXDPackerErrModule::PXDPackerErrModule() :
256 setDescription(
"Create 'broken' PXD raw data to challenge Unpacker");
259 addParam(
"RawPXDsName",
m_RawPXDsName,
"The name of the StoreArray of generated RawPXDs", std::string(
""));
260 addParam(
"dhe_to_dhc",
m_dhe_to_dhc,
"DHE to DHC mapping (DHC_ID, DHE1, DHE2, ..., DHE5) ; -1 disable port");
261 addParam(
"InvertMapping",
m_InvertMapping,
"Use invers mapping to DHP row/col instead of \"remapped\" coordinates",
false);
271 B2DEBUG(27,
"PXD Packer Err --> Init");
273 B2WARNING(
"=== Important! A lot of intentional errors will follow. To find the reason for a failing test, check the lines before the FATAL. ====");
286 B2DEBUG(27,
"PXD Packer Err --> DHC/DHE");
288 if (it.size() != 6) {
290 B2WARNING(
"PXD Packer Err --> DHC/DHE maps 1 dhc to 5 dhe (1+5 values), but I found " << it.size());
292 for (
auto& it2 : it) {
296 B2DEBUG(27,
"PXD Packer Err --> ... DHE " << it2);
297 if (it2 < -1 || it2 >= 64) {
298 if (it2 != -1) B2ERROR(
"PXD Packer Err --> DHC id " << it2 <<
" is out of range (0-64 or -1)! disable channel.");
304 B2DEBUG(27,
"PXD Packer Err --> DHC .. " << it2);
305 if (dhc_id < 0 || dhc_id >= 16) {
306 B2ERROR(
"PXD Packer Err --> DHC id " << it2 <<
" is out of range (0-15)! skip");
313 B2DEBUG(27,
"PXD Packer Err --> DHC/DHE done");
317 B2DEBUG(27,
"PXD Packer Err --> DHC " << it.first);
318 for (
auto& it2 : it.second) {
319 B2DEBUG(27,
"PXD Packer Err --> .. connects to DHE " << it2 <<
" port " << port);
330 if (
m_found_fatal) B2FATAL(
"At least one of the checks failed (see details above)!");
332 B2INFO(
"Check Test coverage:");
333 PXDErrorFlags mask = c_NO_ERROR;
334 mask = std::accumulate(
m_errors.begin(),
m_errors.end(), mask, std::bit_or<PXDErrorFlags>());
335 for (
int i = 0; i < ONSEN_MAX_TYPE_ERR; i++) {
338 if ((m & mask) == 0) {
339 B2WARNING(
"Bit " << i <<
": Not Covered : " << getPXDBitErrorName(i));
347 B2DEBUG(27,
"PXD Packer Err --> Event");
365 uint32_t run = evtPtr->getRun();
366 uint32_t exp = evtPtr->getExperiment();
367 uint32_t sub = evtPtr->getSubrun();
377 B2INFO(
"Pack Event : " << evtPtr->getEvent() <<
"," << evtPtr->getRun() <<
"," << evtPtr->getSubrun() <<
"," <<
378 evtPtr->getExperiment() <<
"," << evtPtr->getTime() <<
" (MetaInfo)");
389 int dhe_ids[5] = {0, 0, 0, 0, 0};
390 B2DEBUG(27,
"PXD Packer Err --> pack_event");
395 int port = 1, port_inx = 0;
398 for (
auto& it2 : it.second) {
399 if (it2 >= 0) act_port += port;
401 dhe_ids[port_inx] = it2;
403 if (port_inx == 5)
break;
416 append_int32(EDHCFrameHeaderDataType::c_DHC_START << 27);
439 append_int32(EDHCFrameHeaderDataType::c_DHC_START << 27);
456 objptr->data()[0] = 0xDEADBEAF;
459 objptr->data()[1] = 0x00110000;
462 objptr->data()[1] = 0x00010000;
465 objptr->data()[2] = 0x01010101;
474 B2WARNING(
"Frame is not 32bit aligned!!! Unsupported by Unpacker!");
529 B2DEBUG(27,
"PXD Packer Err --> pack_dhc ID " << dhc_id <<
" DHE act: " << dhe_active);
540 append_int32(0xCAFE8000 | (send_all ? 0x4000 : 0) | (send_roi ? 0x2000 : 0));
581 append_int32(EDHCFrameHeaderDataType::c_DHC_START << 27);
591 uint32_t header = (EDHCFrameHeaderDataType::c_DHC_START << 27) | ((dhc_id & 0xF) << 21) | ((dhe_active & 0x1F) << 16) |
598 uint32_t mm = (
unsigned int)std::round((
m_meta_time % 1000000000ull) * 0.127216);
599 uint32_t ss = (
unsigned int)(
m_meta_time / 1000000000ull) ;
603 append_int16(((mm >> 12) & 0x7FFF) | ((ss & 1) ? 0x8000 : 0x0));
621 for (
int i = 0; i < 5; i++) {
675 append_int32(EDHCFrameHeaderDataType::c_DHC_START << 27);
695 B2DEBUG(27,
"PXD Packer Err --> pack_dhe ID " << dhe_id <<
" DHP act: " << dhp_active);
702 B2FATAL(
"Inverse Mapping not implemented in Packer");
709 uint32_t header = (EDHCFrameHeaderDataType::c_DHE_START << 27) | ((dhe_id & 0x3F) << 20) | ((dhp_active & 0xF) << 16) |
770 if (dhp_active != 0) {
781 unsigned short sensor, ladder, layer;
782 sensor = (dhe_id & 0x1) + 1;
783 ladder = (dhe_id & 0x1E) >> 1;
784 layer = ((dhe_id & 0x20) >> 5) + 1;
786 B2DEBUG(27,
"pack_dhe: VxdId: " <<
VxdID(layer, ladder, sensor) <<
" " << (
int)
VxdID(layer, ladder, sensor));
789 for (
auto j = 0; j < 10; j++) {
790 unsigned int row, col;
791 unsigned char charge;
795 if (!dhe_has_remapped) {
803 B2FATAL(
"Clusterizer not supported in Packer");
805 for (
int i = 0; i < 4; i++) {
806 if ((dhp_active & 0x1) or
isErrorIn(37)) {
807 pack_dhp(i, dhe_id, dhe_has_remapped ? 1 : 0);
824 header = (EDHCFrameHeaderDataType::c_DHE_END << 27) | ((dhe_id & 0x3F) << 20) | (
m_trigger_nr & 0xFFFF);
843 B2FATAL(
"code needs to be written");
852 B2DEBUG(27,
"PXD Packer Err --> pack_dhp Raw Chip " << chip_id <<
" of DHE id: " << dhe_id);
855 append_int32((EDHCFrameHeaderDataType::c_DHP_RAW << 27) | ((dhe_id & 0x3F) << 20) | ((chip_id & 0x03) << 16) |
857 append_int32((EDHPFrameHeaderDataType::c_RAW << 29) | ((dhe_id & 0x3F) << 18) | ((chip_id & 0x03) << 16) |
863 if (c2 >= PACKER_NUM_COLS) c2 = PACKER_NUM_COLS;
866 for (
int row = 0; row < PACKER_NUM_ROWS; row++) {
867 for (
int col = c1; col < c2; col++) {
871 for (
int col = c2; col < c1 + 64; col++) {
881 B2DEBUG(27,
"PXD Packer Err --> pack_dhp Chip " << chip_id <<
" of DHE id: " << dhe_id);
884 unsigned short last_rowstart = 0;
885 bool error_done =
false;
887 if (dhe_has_remapped == 0) {
890 B2FATAL(
"dhe_has_remapped == 0");
895 uint32_t header = (EDHCFrameHeaderDataType::c_DHP_ZSD << 27) | ((dhe_id & 0x3F) << 20) | ((dhe_has_remapped & 0x1) << 19) | ((
907 header = (EDHPFrameHeaderDataType::c_ZSD << 29) | ((dhe_id & 0x3F) << 18) | ((chip_id & 0x03) << 16) |
916 for (
int row = 0; row < PACKER_NUM_ROWS; row++) {
918 if (
isErrorIn(43) && !error_done) rowstart =
false;
919 else rowstart =
true;
923 if (c2 >= PACKER_NUM_COLS) c2 = PACKER_NUM_COLS;
924 for (
int col = c1; col < c2; col++) {
926 unsigned char charge;
928 if (
isErrorIn(41) and row == PACKER_NUM_ROWS - 1 and col == PACKER_NUM_COLS - 1 and !error_done) {
929 row = PACKER_NUM_ROWS;
933 if (
isErrorIn(42) and row == PACKER_NUM_ROWS - 1 and col == PACKER_NUM_COLS - 1 and !error_done) {
934 col = PACKER_NUM_COLS;
938 last_rowstart = ((row & 0x3FE) << (6 - 1)) | 0;
949 B2DEBUG(27,
"Repeat last rowstart to align to 32bit.");
955 B2DEBUG(27,
"Found no data for halfladder! DHEID: " << dhe_id <<
" Chip: " << chip_id);
961 append_int32((EDHCFrameHeaderDataType::c_GHOST << 27) | ((dhe_id & 0x3F) << 20) | ((chip_id & 0x03) << 16) |
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
void do_the_reverse_mapping(unsigned int &row, unsigned int &col, unsigned short layer, unsigned short sensor)
function still to be implemented
std::vector< std::vector< unsigned char > > m_onsen_payload
For one DHC event, we utilize one payload for all DHE/DHP frames.
void initialize() override final
Initialize the module.
bool m_Check
false=Pack Raw Data, true=Check unpacked result
void add_frame_to_payload(void)
Add Frame to Event payload.
unsigned short m_run_nr_word2
Exp+Run Nr.
std::string m_PXDDAQEvtStatsName
The name of the StoreObjPtr of PXDDAQStatus to be read.
void append_int16(unsigned short w)
cat 16bit value to frame
StoreObjPtr< PXDDAQStatus > m_daqStatus
Output array for RawPxds.
void pack_dhe(int dhe_id, int dhp_mask)
Pack one DHE (several DHP) to buffer.
bool m_found_fatal
flag if we found one test failing
void pack_dhp_raw(int dhp_id, int dhe_id)
Pack one DHP RAW to buffer.
std::string m_RawPXDsName
The name of the StoreArray of generated RawPXDs.
bool CheckErrorMaskInEvent(unsigned int eventnr, PXDError::PXDErrorFlags mask)
void pack_event(void)
Pack one event (several DHC) stored in seperate RawPXD object.
void terminate() override final
Terminate the module.
void event() override final
do the packing
unsigned int dhc_byte_count
Byte count in current DHC package.
std::vector< std::vector< int > > m_dhe_to_dhc
Parameter dhc<->dhe list, mapping from steering file.
bool m_Clusterize
Use clusterizer (FCE format)
unsigned short m_run_nr_word1
Run+Subrun Nr.
bool isErrorIn(uint32_t enr)
Check if we want this type of error in this event.
std::vector< unsigned int > m_onsen_header
For one DHC event, we utilize one header (writing out, beware of endianess!)
unsigned int m_real_trigger_nr
Real Trigger Nr.
void append_int32(unsigned int w)
cat 32value value to frame
StoreArray< RawPXD > m_storeRaws
Output array for RawPxds.
unsigned int m_trigger_dhp_framenr
DHP Readout Frame Nr for DHP and DHE headers.
unsigned int dhe_byte_count
Byte count in current DHE package.
static std::vector< PXDErrorFlags > m_errors
The pxd error flags.
void pack_dhp(int dhp_id, int dhe_id, int dhe_reformat)
Pack one DHP to buffer.
std::map< int, std::vector< int > > m_dhc_mapto_dhe
mapping calculated from m_dhe_to_dhc for easier handling
void start_frame(void)
Start with a new Frame.
void append_int8(unsigned char w)
cat 8bit value to frame
unsigned int m_trigger_dhe_gate
DHE Trigger Gate for DHE headers.
unsigned int m_packed_events
Event counter.
bool m_InvertMapping
Flag if we invert mapping to DHP row/col or use premapped coordinates.
unsigned long long int m_meta_time
Time(Tag) from MetaInfo.
std::vector< unsigned char > m_current_frame
For current processed frames.
unsigned char halfladder_pixmap[PACKER_NUM_ROWS][PACKER_NUM_COLS]
temporary hitmap buffer for pixel to raw data conversion
unsigned int m_trigger_nr
Trigger Nr.
void pack_dhc(int dhc_id, int dhe_mask, int *dhe_ids, bool send_all=true, bool send_roi=false)
Pack one DHC (several DHE) stored in one RawPXD object.
Type-safe access to single objects in the data store.
Class to uniquely identify a any structure of the PXD and SVD.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Namespace to encapsulate code needed for simulation and reconstrucion of the PXD.
boost::crc_optimal< 32, 0x04C11DB7, 0, 0, false, false > dhc_crc_32_type
define our CRC function
Abstract base class for different kinds of events.