9 #include <rawdata/dataobjects/PreRawCOPPERFormat_v2.h>
20 PreRawCOPPERFormat_v2::PreRawCOPPERFormat_v2()
24 PreRawCOPPERFormat_v2::~PreRawCOPPERFormat_v2()
28 unsigned int PreRawCOPPERFormat_v2::CalcDriverChkSum(
int n)
30 int min = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS;
31 int max = GetBufferPos(n) + GetBlockNwords(n)
32 - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER;
33 unsigned int chksum = 0;
34 for (
int i = min; i < max; i++) {
35 chksum ^= m_buffer[ i ];
42 int PreRawCOPPERFormat_v2::GetDetectorNwords(
int n,
int finesse_num)
46 if (GetFINESSENwords(n, finesse_num) > 0) {
47 nwords = GetFINESSENwords(n, finesse_num)
48 - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER);
55 int PreRawCOPPERFormat_v2::GetFINESSENwords(
int n,
int finesse_num)
57 if (!CheckCOPPERMagic(n)) {
60 GetNodeName(n, hostname,
sizeof(hostname));
62 "[FATAL] %s ch=%d : ERROR_EVENT : COPPER's magic word is invalid. Exiting... Maybe it is due to data corruption or different version of the data format. : slot%c eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
63 hostname, finesse_num,
64 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
65 __FILE__, __PRETTY_FUNCTION__, __LINE__);
66 printf(
"[DEBUG] %s", err_buf); fflush(stdout);
67 PrintData(m_buffer, m_nwords);
69 for (
int i = 0; i < 4; i++) {
70 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
71 if (GetFINESSENwords(n, i) > 0) {
75 printf(
"[DEBUG] ========== No CRC error. : block %d =========\n", n);
80 switch (finesse_num) {
82 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_A_DATA_LENGTH;
85 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_B_DATA_LENGTH;
88 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_C_DATA_LENGTH;
91 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_D_DATA_LENGTH;
96 GetNodeName(n, hostname,
sizeof(hostname));
98 "[FATAL] %s ch=%d : ERROR_EVENT : Specified FINESSE number( = %d ) is invalid. Exiting... : slot%c eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
99 hostname, finesse_num,
101 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
102 __FILE__, __PRETTY_FUNCTION__, __LINE__);
103 printf(
"%s", err_buf); fflush(stdout);
107 return m_buffer[ pos_nwords ];
112 unsigned int PreRawCOPPERFormat_v2::GetB2LFEE32bitEventNumber(
int n)
115 #ifndef READ_OLD_B2LFEE_FORMAT_FILE
118 unsigned int eve_num = 0;
121 for (
int i = 0; i < 4 ; i++) {
122 eve[ i ] = 0xbaadf00d;
123 if (GetFINESSENwords(n, i) > 0) {
124 int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
125 eve[ i ] = m_buffer[ pos_nwords ];
126 if (flag != 1) eve_num = eve[ i ];
127 if (eve_num != eve[ i ]) err_flag = 1;
133 PrintData(m_buffer, m_nwords);
136 GetNodeName(n, hostname,
sizeof(hostname));
138 "[FATAL] %s ch=%d : ERROR_EVENT : No HSLB data in COPPER data. Exiting... : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
140 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
141 __FILE__, __PRETTY_FUNCTION__, __LINE__);
142 printf(
"%s", err_buf); fflush(stdout);
150 GetNodeName(n, hostname,
sizeof(hostname));
152 "[FATAL] %s ch=%d : ERROR_EVENT : CORRUPTED DATA: Different event number over HSLBs : slot A 0x%.8x : B 0x%.8x :C 0x%.8x : D 0x%.8x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
154 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
155 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
156 __FILE__, __PRETTY_FUNCTION__, __LINE__);
157 printf(
"[DEBUG] %s\n", err_buf);
158 PrintData(m_buffer, m_nwords);
159 for (
int i = 0; i < 4; i++) {
160 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
161 if (GetFINESSENwords(n, i) > 0) {
165 printf(
"[DEBUG] ========== No CRC error. : block %d =========\n", n);
166 #ifndef NO_ERROR_STOP
176 sprintf(err_buf,
"[FATAL] You need comment out READ_OLD_B2LFEE_FORMAT_FILE if you are handling a new data format\n%s %s %d\n",
177 __FILE__, __PRETTY_FUNCTION__, __LINE__);
178 printf(
"%s", err_buf); fflush(stdout);
185 void PreRawCOPPERFormat_v2::CheckData(
int n,
186 unsigned int prev_evenum,
unsigned int* cur_evenum_rawcprhdr,
187 unsigned int prev_copper_ctr,
unsigned int* cur_copper_ctr,
188 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
196 if (!CheckCOPPERMagic(n)) {
198 GetNodeName(n, hostname,
sizeof(hostname));
200 "[FATAL] %s ch=%d : ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
202 GetMagicDriverHeader(n), GetMagicFPGAHeader(n), GetMagicFPGATrailer(n), GetMagicDriverTrailer(n),
203 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
204 __FILE__, __PRETTY_FUNCTION__, __LINE__);
211 *cur_evenum_rawcprhdr = GetEveNo(n);
212 unsigned int evenum_feehdr = GetB2LFEE32bitEventNumber(n);
213 if (*cur_evenum_rawcprhdr != evenum_feehdr) {
216 GetNodeName(n, hostname,
sizeof(hostname));
218 "[FATAL] %s ch=%d : ERROR_EVENT : Event # in PreRawCOPPERFormat_v2 header and FEE header is different : cprhdr 0x%x feehdr 0x%x : Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
220 *cur_evenum_rawcprhdr, evenum_feehdr,
221 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
222 __FILE__, __PRETTY_FUNCTION__, __LINE__);
229 *cur_exprunsubrun_no = GetExpRunSubrun(n);
230 *cur_copper_ctr = GetCOPPERCounter(n);
231 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
232 if ((
unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) {
234 GetNodeName(n, hostname,
sizeof(hostname));
236 "[FATAL] %s ch=%d : ERROR_EVENT : Event # jump : i %d prev 0x%x cur 0x%x : prevrun %.8x currun %.8x: Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
238 n, prev_evenum, *cur_evenum_rawcprhdr, prev_exprunsubrun_no, *cur_exprunsubrun_no,
239 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
240 __FILE__, __PRETTY_FUNCTION__, __LINE__);
243 if ((
unsigned int)(prev_copper_ctr + 1) != *cur_copper_ctr) {
245 GetNodeName(n, hostname,
sizeof(hostname));
247 "[FATAL] %s ch=%d : ERROR_EVENT : COPPER counter jump : i %d prev 0x%x cur 0x%x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
249 n, prev_copper_ctr, *cur_copper_ctr,
250 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
251 __FILE__, __PRETTY_FUNCTION__, __LINE__);
255 printf(
"[DEBUG] New run started. cur run %.8x prev. run %.8x cur eve %.8x prev eve %8.x : eve 0x%x exp %d run %d sub %d\n",
256 *cur_exprunsubrun_no, prev_exprunsubrun_no , *cur_evenum_rawcprhdr, prev_evenum,
257 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n));
260 if ((
unsigned int)GetRunNo(n) != (prev_exprunsubrun_no & RawHeader_v2::RUNNO_MASK) >> RawHeader_v2::RUNNO_SHIFT) {
261 if (*cur_evenum_rawcprhdr != 0) {
264 for (
int i = 0; i < 4 ; i++) {
265 eve[ i ] = 0xbaadf00d;
266 if (GetFINESSENwords(n, i) > 0) {
267 int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
268 eve[ i ] = m_buffer[ pos_nwords ];
273 GetNodeName(n, hostname,
sizeof(hostname));
275 "[FATAL] %s ch=%d : ERROR_EVENT : Invalid Event # at the beginning of the run (It should be zero.): preveve 0x%x cureve 0x%x : prev(exp %u run %u sub %u ) cur(exp %u run %u sub %u ) ( A:0x%.8x B:0x%.8x C:0x%.8x D:0x%.8x ) Exiting... : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
277 prev_evenum, *cur_evenum_rawcprhdr,
278 prev_exprunsubrun_no >> 22 , (prev_exprunsubrun_no >> 8) & 0x3FFF, prev_exprunsubrun_no & 0xFF,
279 *cur_exprunsubrun_no >> 22 , (*cur_exprunsubrun_no >> 8) & 0x3FFF, *cur_exprunsubrun_no & 0xFF,
280 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
281 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
282 __FILE__, __PRETTY_FUNCTION__, __LINE__);
294 CheckUtimeCtimeTRGType(n);
299 if (GetDriverChkSum(n) != CalcDriverChkSum(n)) {
301 GetNodeName(n, hostname,
sizeof(hostname));
303 "[FATAL] %s ch=%d : ERROR_EVENT : COPPER driver checkSum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
305 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, GetDriverChkSum(n), CalcDriverChkSum(n),
306 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
307 __FILE__, __PRETTY_FUNCTION__, __LINE__);
314 tmp_trailer.SetBuffer(GetRawTrlBufPtr(n));
315 unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords());
316 if (tmp_trailer.GetChksum() != xor_chksum) {
318 GetNodeName(n, hostname,
sizeof(hostname));
320 "[FATAL] %s ch=%d : ERROR_EVENT : PreRawCOPPERFormat_v2 checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
322 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum,
323 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
324 __FILE__, __PRETTY_FUNCTION__, __LINE__);
329 printf(
"%s", err_buf); fflush(stdout);
330 printf(
"[DEBUG] ========== dump a data blcok : block # %d==========\n", n);
331 PrintData(GetBuffer(n), GetBlockNwords(n));
332 for (
int i = 0; i < 4; i++) {
333 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
334 if (GetFINESSENwords(n, i) > 0) {
338 printf(
"[DEBUG] ========== No CRC error : block %d =========\n", n);
347 bool PreRawCOPPERFormat_v2::CheckCOPPERMagic(
int n)
349 if (GetMagicDriverHeader(n) != COPPER_MAGIC_DRIVER_HEADER) {
351 }
else if (GetMagicFPGAHeader(n) != COPPER_MAGIC_FPGA_HEADER) {
353 }
else if (GetMagicFPGATrailer(n) != COPPER_MAGIC_FPGA_TRAILER) {
355 }
else if (GetMagicDriverTrailer(n) != COPPER_MAGIC_DRIVER_TRAILER) {
361 void PreRawCOPPERFormat_v2::CheckUtimeCtimeTRGType(
int n)
364 #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2
365 CheckB2LFEEHeaderVersion(n);
369 unsigned int temp_utime = 0, temp_ctime_trgtype = 0, temp_eve = 0, temp_exprun = 0;
370 unsigned int temp_ctime_trgtype_footer, temp_eve_footer;
371 unsigned int utime[4], ctime_trgtype[4], eve[4], exprun[4];
375 memset(utime, 0,
sizeof(utime));
376 memset(ctime_trgtype, 0,
sizeof(ctime_trgtype));
377 memset(eve, 0,
sizeof(eve));
378 memset(exprun, 0,
sizeof(exprun));
381 for (
int i = 0; i < 4; i++) {
382 int finesse_nwords = GetFINESSENwords(n, i);
383 if (finesse_nwords > 0) {
384 int offset_finesse = GetOffsetFINESSE(n, i);
385 ctime_trgtype[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
386 utime[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
387 eve[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
388 exprun[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
389 temp_ctime_trgtype_footer =
390 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_TT_CTIME_B2LFEE ];
392 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_CHKSUM_B2LFEE ];
395 temp_ctime_trgtype = ctime_trgtype[ i ];
396 temp_utime = utime[ i ];
398 temp_exprun = exprun[ i ];
402 if (temp_ctime_trgtype != ctime_trgtype[ i ] || temp_utime != utime[ i ] ||
403 temp_eve != eve[ i ] || temp_exprun != exprun[ i ]) {
405 GetNodeName(n, hostname,
sizeof(hostname));
407 for (
int j = 0; j < 4; j++) {
408 printf(
"[DEBUG] %s ch=%d : FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x\n",
410 j, GetFINESSENwords(n, j), ctime_trgtype[ j ], utime[ j ], eve[ j ], exprun[ j ]);
414 char err_buf_1[2500], err_buf_2[2500], err_buf_3[2500];
416 "[FATAL] %s ch=%d : ERROR_EVENT : mismatch header value over FINESSEs. Exiting...",
419 "%s FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x",
421 first_ch, GetFINESSENwords(n, first_ch), ctime_trgtype[ first_ch ], utime[ first_ch ], eve[ first_ch ], exprun[ first_ch ]);
423 "%s FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x",
425 i, GetFINESSENwords(n, i), ctime_trgtype[ i ], utime[ i ], eve[ i ], exprun[ i ]);
426 sprintf(err_buf,
"%s\n %s %s %d\n",
428 __FILE__, __PRETTY_FUNCTION__, __LINE__);
429 printf(
"%s", err_buf); fflush(stdout);
433 }
else if (temp_ctime_trgtype != temp_ctime_trgtype_footer ||
434 (temp_eve & 0xffff) != ((temp_eve_footer >> 16) & 0xffff)) {
436 GetNodeName(n, hostname,
sizeof(hostname));
438 "[FATAL] %s ch=%d : ERROR_EVENT : mismatch(finesse %d) between header(ctime %.8x eve %.8x) and footer(ctime %.8x eve_crc16 %.8x). Exiting...\n %s %s %d\n",
440 i, temp_ctime_trgtype, temp_eve, temp_ctime_trgtype_footer, temp_eve_footer,
441 __FILE__, __PRETTY_FUNCTION__, __LINE__);
442 printf(
"%s", err_buf); fflush(stdout);
450 for (
int i = 0; i < 4; i++) {
451 if (GetFINESSENwords(n, i) > 0) {
452 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
454 printf(
"[DEBUG] ========== CRC check is done. : block %d =========\n", n);
457 #ifndef NO_ERROR_STOP
465 unsigned int PreRawCOPPERFormat_v2::FillTopBlockRawHeader(
unsigned int m_node_id,
unsigned int prev_eve32,
466 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
468 const int datablock_id = 0;
473 if (m_num_nodes * m_num_events != 1) {
476 "[FATAL] This function should be used for PreRawCOPPERFormat_v2 containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n",
477 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
478 printf(
"%s", err_buf); fflush(stdout);
492 memset(m_buffer, 0,
sizeof(
int) * tmp_header.RAWHEADER_NWORDS);
493 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] = tmp_header.RAWHEADER_NWORDS & tmp_header.HDR_NWORDS_MASK;
494 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) &
495 tmp_header.FORMAT_VERSION__MASK;
496 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (0x80 << tmp_header.FORMAT_VERSION_SHIFT);
497 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= tmp_header.MAGIC_WORD;
502 int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]);
503 if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 &&
504 copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 &&
505 copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 &&
506 copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) {
509 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
511 "[FATAL] %s ch=%d : ERROR_EVENT : No FINESSE data in a copper data block. Exiting...\n %s %s %d\n",
513 __FILE__, __PRETTY_FUNCTION__, __LINE__);
514 printf(
"%s", err_buf); fflush(stdout);
522 int datablock_nwords =
523 tmp_header.RAWHEADER_NWORDS +
524 (copper_buf[ POS_DATA_LENGTH ]
525 + SIZE_COPPER_DRIVER_HEADER
526 + SIZE_COPPER_DRIVER_TRAILER)
527 + tmp_trailer.RAWTRAILER_NWORDS;
528 m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords;
534 if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) {
538 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
540 "[FATAL] %s ch=%d : ERROR_EVENT : Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n",
542 m_nwords, m_buffer[ tmp_header.POS_NWORDS ],
543 __FILE__, __PRETTY_FUNCTION__, __LINE__);
544 printf(
"%s", err_buf); fflush(stdout);
552 int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
553 int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ];
554 int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ];
555 int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ];
556 m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse;
557 m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse;
558 m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse;
559 m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse;
565 (m_buffer[ offset_1st_finesse ]);
566 m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
572 unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
573 m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32;
578 m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
579 m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
584 m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id;
594 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = 0;
595 unsigned int ff55_higher_bits, ff55_lower_bits;
597 if (copper_buf[ POS_CH_A_DATA_LENGTH ] != 0) {
598 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
601 ff55_lower_bits = m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF;
603 if (ff55_higher_bits != 0xff550000) {
606 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
608 "[FATAL] %s ch=%d : ERROR_EVENT : HSLB slotA's trailer magic word(0xff55) is invalid. : eve %8u run %d foooter %.8x : %s %s %d\n",
610 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
611 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
612 __FILE__, __PRETTY_FUNCTION__, __LINE__);
613 printf(
"%s", err_buf); fflush(stdout);
614 PrintData(m_buffer, m_nwords); fflush(stdout);
618 if (ff55_lower_bits != 0) {
619 const int linkdown_bit = 15;
623 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
625 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
626 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot A eve %8u foooter %.8x : %s %s %d\n",
629 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
630 __FILE__, __PRETTY_FUNCTION__, __LINE__);
631 printf(
"%s", err_buf); fflush(stdout);
632 PrintData(m_buffer, m_nwords);
636 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
637 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
638 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x1 << 28);
640 "[WARNING] %s ch=%d : ARICH : B2link packet CRC error slot A eve %8u foooter %.8x : This error is ignored and the error event will be recorded in .sroot file acording to request from ARICH group: %s %s %d\n",
643 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
644 __FILE__, __PRETTY_FUNCTION__, __LINE__);
645 printf(
"%s", err_buf); fflush(stdout);
646 PrintData(m_buffer, m_nwords);
649 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
650 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x1 << 28);
651 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot A eve %8u foooter %.8x : %s %s %d\n",
654 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
655 __FILE__, __PRETTY_FUNCTION__, __LINE__);
656 printf(
"%s", err_buf); fflush(stdout);
657 PrintData(m_buffer, m_nwords);
665 if (copper_buf[ POS_CH_B_DATA_LENGTH ] != 0) {
666 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
669 ff55_lower_bits = (m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
671 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
672 if (ff55_higher_bits != 0xff550000) {
675 "[FATAL] %s ch=%d : ERROR_EVENT : HSLB slotB's trailer magic word(0xff55) is invalid. : eve %8u run %d foooter %.8x : %s %s %d\n",
677 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
678 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
679 __FILE__, __PRETTY_FUNCTION__, __LINE__);
680 printf(
"%s", err_buf); fflush(stdout);
681 PrintData(m_buffer, m_nwords); fflush(stdout);
685 if (ff55_lower_bits != 0) {
687 const int linkdown_bit = 15;
689 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
690 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot B eve %8u foooter %.8x : %s %s %d\n",
693 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
694 __FILE__, __PRETTY_FUNCTION__, __LINE__);
695 printf(
"%s", err_buf); fflush(stdout);
696 PrintData(m_buffer, m_nwords);
700 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
701 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
702 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x2 << 28);
704 "[WARNING] %s ch=%d : ARICH : B2link packet CRC error slot B eve %8u foooter %.8x : This error is ignored and the error event will be recorded in .sroot file acording to request from ARICH group: %s %s %d\n",
707 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
708 __FILE__, __PRETTY_FUNCTION__, __LINE__);
709 printf(
"%s", err_buf); fflush(stdout);
710 PrintData(m_buffer, m_nwords);
713 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
714 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x2 << 28);
715 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot B eve %8u foooter %.8x : %s %s %d\n",
718 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
719 __FILE__, __PRETTY_FUNCTION__, __LINE__);
720 printf(
"%s", err_buf); fflush(stdout);
721 PrintData(m_buffer, m_nwords);
729 if (copper_buf[ POS_CH_C_DATA_LENGTH ] != 0) {
730 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
733 ff55_lower_bits = (m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
735 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
736 if (ff55_higher_bits != 0xff550000) {
739 "[FATAL] %s ch=%d : ERROR_EVENT : HSLB slotC's trailer magic word(0xff55) is invalid. : eve %8u run %d foooter %.8x : %s %s %d\n",
741 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
742 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
743 __FILE__, __PRETTY_FUNCTION__, __LINE__);
744 printf(
"%s", err_buf); fflush(stdout);
745 PrintData(m_buffer, m_nwords); fflush(stdout);
749 if (ff55_lower_bits != 0) {
751 const int linkdown_bit = 15;
753 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
754 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot C eve %8u foooter %.8x : %s %s %d\n",
757 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
758 __FILE__, __PRETTY_FUNCTION__, __LINE__);
759 printf(
"%s", err_buf); fflush(stdout);
760 PrintData(m_buffer, m_nwords);
764 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
765 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
766 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x4 << 28);
768 "[WARNING] %s ch=%d : ARICH : B2link packet CRC error slot C eve %8u foooter %.8x : This error is ignored and the error event will be recorded in .sroot file acording to request from ARICH group: %s %s %d\n",
771 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
772 __FILE__, __PRETTY_FUNCTION__, __LINE__);
773 printf(
"%s", err_buf); fflush(stdout);
774 PrintData(m_buffer, m_nwords);
777 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
778 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x4 << 28);
779 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot C eve %8u foooter %.8x : %s %s %d\n",
782 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
783 __FILE__, __PRETTY_FUNCTION__, __LINE__);
784 printf(
"%s", err_buf); fflush(stdout);
785 PrintData(m_buffer, m_nwords);
794 if (copper_buf[ POS_CH_D_DATA_LENGTH ] != 0) {
795 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
798 ff55_lower_bits = (m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
800 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
801 if (ff55_higher_bits != 0xff550000) {
804 "[FATAL] %s ch=%d : ERROR_EVENT : HSLB slotD's trailer magic word(0xff55) is invalid. : eve %8u run %d foooter %.8x : %s %s %d\n",
806 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
807 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
808 __FILE__, __PRETTY_FUNCTION__, __LINE__);
809 printf(
"%s", err_buf); fflush(stdout);
810 PrintData(m_buffer, m_nwords); fflush(stdout);
814 if (ff55_lower_bits != 0) {
816 const int linkdown_bit = 15;
818 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
819 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot D eve %8u foooter %.8x : %s %s %d\n",
822 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
823 __FILE__, __PRETTY_FUNCTION__, __LINE__);
824 printf(
"%s", err_buf); fflush(stdout);
825 PrintData(m_buffer, m_nwords);
829 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
830 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
831 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x8 << 28);
833 "[WARNING] %s ch=%d : ARICH : B2link packet CRC error slot D eve %8u foooter %.8x : This error is ignored and the error event will be recorded in .sroot file acording to request from ARICH group: %s %s %d\n",
836 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
837 __FILE__, __PRETTY_FUNCTION__, __LINE__);
838 printf(
"%s", err_buf); fflush(stdout);
839 PrintData(m_buffer, m_nwords);
842 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
843 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x8 << 28);
844 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot D eve %8u foooter %.8x : %s %s %d\n",
847 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
848 __FILE__, __PRETTY_FUNCTION__, __LINE__);
849 printf(
"%s", err_buf); fflush(stdout);
850 PrintData(m_buffer, m_nwords);
868 unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0;
870 int top_end = tmp_header.RAWHEADER_NWORDS;
871 for (
int i = 0; i < top_end; i++) {
872 chksum_top ^= m_buffer[ i ];
874 int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS;
875 for (
int i = top_end; i < body_end; i++) {
876 chksum_body ^= m_buffer[ i ];
879 int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS;
880 for (
int i = body_end; i < bottom_end; i++) {
881 chksum_bottom ^= m_buffer[ i ];
887 if (chksum_body != (
unsigned int)(m_buffer[ body_end ])) {
890 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
891 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n",
893 chksum_body, m_buffer[ body_end ],
894 __FILE__, __PRETTY_FUNCTION__, __LINE__);
895 printf(
"%s", err_buf); fflush(stdout);
902 unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom;
903 int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]);
904 trl[ tmp_trailer.POS_CHKSUM ] = chksum;
905 trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
918 int* fpga_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_3);
919 int* driver_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_4);
921 if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) {
923 }
else if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) {
925 }
else if ((
unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) {
927 }
else if ((
unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) {
933 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
935 "[FATAL] %s ch=%d : ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n",
937 GetMagicDriverHeader(datablock_id),
938 GetMagicFPGAHeader(datablock_id),
939 GetMagicFPGATrailer(datablock_id),
940 GetMagicDriverTrailer(datablock_id),
941 __FILE__, __PRETTY_FUNCTION__, __LINE__);
942 printf(
"[DEBUG] %s\n", err_buf);
943 #ifndef NO_ERROR_STOP
953 *cur_exprunsubrun_no = GetExpRunSubrun(datablock_id);
959 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
960 if (prev_eve32 + 1 != cur_ftsw_eve32) {
961 unsigned int eve[4] = {0xbaadf00d, 0xbaadf00d, 0xbaadf00d, 0xbaadf00d };
962 #ifndef NO_ERROR_STOP
963 if (m_num_nodes * m_num_events != 1) {
966 "[FATAL] This function should be used for PreRawCOPPERFormat_v2 containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n",
967 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
968 printf(
"%s", err_buf); fflush(stdout);
972 for (
int i = 0; i < 4 ; i++) {
975 if (GetFINESSENwords(0 , i) > 0) {
976 int pos_nwords = GetOffsetFINESSE(0, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
977 eve[ i ] = m_buffer[ pos_nwords ];
984 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
986 "[FATAL] %s ch=%d : ERROR_EVENT : Invalid event_number. Exiting...: cur 32bit eve %u preveve %u ( A:0x%.8x B:0x%.8x C:0x%.8x D:0x%.8x ) prun %u crun %u\n %s %s %d\n",
988 cur_ftsw_eve32, prev_eve32,
989 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
990 prev_exprunsubrun_no, *cur_exprunsubrun_no,
991 __FILE__, __PRETTY_FUNCTION__, __LINE__);
992 printf(
"[DEBUG] %s\n", err_buf);
995 printf(
"[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords());
996 PrintData(GetBuffer(datablock_id), TotalBufNwords());
998 for (
int i = 0; i < 4; i++) {
999 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", datablock_id, i);
1000 if (GetFINESSENwords(datablock_id, i) > 0) {
1001 CheckCRC16(datablock_id, i);
1004 printf(
"[DEBUG] ========== No CRC error : block %d =========\n", datablock_id);
1012 return cur_ftsw_eve32;
1018 #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2
1019 void PreRawCOPPERFormat_v2::CheckB2LFEEHeaderVersion(
int n)
1022 for (
int i = 0; i < 4; i++) {
1023 if (GetFINESSENwords(n, i) > 0) {
1024 temp_buf = GetFINESSEBuffer(n, i);
1025 if ((temp_buf[ 3 ] & 0x40000000) == 0) {
1029 printf(
"[DEBUG] \033[31m");
1030 printf(
"[DEBUG] ===Firmware ver. ERROR===\n ");
1031 printf(
"[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n");
1032 printf(
"[DEBUG] If you are going to take data now, Please update the firmware.\n");
1033 printf(
"[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n");
1034 printf(
"[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn revision 7419\n");
1035 printf(
"[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n");
1036 printf(
"[DEBUG] Sorry for inconvenience.\n");
1037 printf(
"[DEBUG] \033[0m");
1041 GetNodeName(n, hostname,
sizeof(hostname));
1042 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n",
1044 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1058 GetNodeName(n, hostname,
sizeof(hostname));
1059 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : PreRawCOPPERFormat_v2 contains no FINESSE data. Exiting...\n %s %s %d\n",
1061 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1062 printf(
"%s", err_buf); fflush(stdout);
1073 int PreRawCOPPERFormat_v2::CalcReducedDataSize(
int* bufin,
int nwords,
int num_events,
int num_nodes)
1079 int delete_flag = 0;
1080 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
1082 int reduced_nwords = 0;
1084 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
1085 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
1086 int entry_id = l + k * num_nodes_in_sendblock;
1092 int temp_delete_flag = 0, temp_num_eve = 1, temp_num_nodes = 1;
1097 temp_delete_flag, temp_num_eve,
1103 return reduced_nwords;
1108 void PreRawCOPPERFormat_v2::CopyReducedData(
int* bufin,
int nwords,
int num_events,
int num_nodes,
int* buf_to,
int* nwords_to)
1114 int delete_flag = 0;
1115 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
1117 int pos_nwords_to = 0;
1119 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
1120 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
1121 int entry_id = l + k * num_nodes_in_sendblock;
1124 radblk_fmt.
CopyBlock(entry_id, buf_to + pos_nwords_to);
1128 SetBuffer(radblk_fmt.
GetBuffer(entry_id),
1131 pos_nwords_to += CopyReducedBuffer(0, buf_to + pos_nwords_to);
1138 *nwords_to = pos_nwords_to;
1144 int PreRawCOPPERFormat_v2::CalcReducedNwords(
int n)
1153 nwords_to += tmp_header.RAWHEADER_NWORDS;
1155 for (
int j = 0; j < 4; j++) {
1157 int finesse_nwords = GetFINESSENwords(n, j);
1158 if (finesse_nwords > 0) {
1165 - (SIZE_B2LHSLB_HEADER - m_reduced_rawcpr.SIZE_B2LHSLB_HEADER)
1166 - (SIZE_B2LFEE_HEADER - m_reduced_rawcpr.SIZE_B2LFEE_HEADER)
1167 - (SIZE_B2LFEE_TRAILER - m_reduced_rawcpr.SIZE_B2LFEE_TRAILER)
1168 - (SIZE_B2LHSLB_TRAILER - m_reduced_rawcpr.SIZE_B2LHSLB_TRAILER);
1174 nwords_to += tmp_trailer.GetTrlNwords();
1184 int PreRawCOPPERFormat_v2::CopyReducedBuffer(
int n,
int* buf_to)
1190 int* buf_from = NULL;
1191 int nwords_buf_to = CalcReducedNwords(n);
1192 int pos_nwords_to = 0;
1193 int copy_nwords = 0;
1195 unsigned int removed_xor_chksum = 0;
1200 copy_nwords = tmp_header.RAWHEADER_NWORDS;
1201 buf_from = GetBuffer(n);
1202 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1206 removed_xor_chksum ^= buf_from[ tmp_header.POS_VERSION_HDRNWORDS ];
1209 buf_to[ tmp_header.POS_VERSION_HDRNWORDS ] &= 0xFFFF7FFF;
1212 removed_xor_chksum ^= buf_to[ tmp_header.POS_VERSION_HDRNWORDS ];
1213 for (
int i = 0; i < SIZE_COPPER_HEADER; i++) {
1214 removed_xor_chksum ^= buf_from[ tmp_header.RAWHEADER_NWORDS + i ];
1222 int pos_nwords_finesse[ 4 ];
1223 for (
int j = 0; j < 4; j++) {
1224 pos_nwords_finesse[ j ] = pos_nwords_to;
1225 if (GetFINESSENwords(n, j) > 0) {
1226 int* finesse_buf = GetFINESSEBuffer(n, j);
1227 int finesse_nwords = GetFINESSENwords(n, j);
1230 CheckB2LHSLBMagicWords(finesse_buf, finesse_nwords);
1232 buf_to[ pos_nwords_to ] = finesse_buf[ POS_MAGIC_B2LHSLB ];
1238 + SIZE_B2LHSLB_HEADER
1244 if (finesse_nwords - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER
1245 - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER < 0) {
1248 GetNodeName(n, hostname,
sizeof(hostname));
1250 "[FATAL] %s ch=%d : ERROR_EVENT : Finesse buffer size is too small( %d words < %d words). May be the data are corrupted. Exiting...\n %s %s %d\n",
1252 finesse_nwords, SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER,
1253 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1254 printf(
"%s", err_buf); fflush(stdout);
1260 for (
int k = 1; k <= 4 ; k++) {
1261 removed_xor_chksum ^= finesse_buf[ k ];
1266 - SIZE_B2LHSLB_HEADER
1268 - SIZE_B2LFEE_TRAILER
1269 - SIZE_B2LHSLB_TRAILER;
1270 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1274 buf_to[ pos_nwords_to ] = 0xffff & finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER - (SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) ];
1276 buf_to[ pos_nwords_to ] |= (finesse_buf[ finesse_nwords - (SIZE_B2LHSLB_TRAILER - POS_MAGIC_B2LHSLB) ] << 16) & 0xFFFF0000;
1279 for (
int k = 0; k <= 2 ; k++) {
1280 removed_xor_chksum ^= finesse_buf[ finesse_nwords - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER + k ];
1282 removed_xor_chksum ^= buf_to[ pos_nwords_to ];
1295 - tmp_trailer.GetTrlNwords();
1296 copy_nwords = tmp_trailer.GetTrlNwords();
1297 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1300 unsigned int old_rawcopper_chksum = buf_from[ tmp_trailer.POS_CHKSUM ];
1301 for (
int i = 0; i < SIZE_COPPER_TRAILER; i++) {
1302 removed_xor_chksum ^= (
unsigned int) * (buf_from - SIZE_COPPER_TRAILER + i);
1307 if (pos_nwords_to != nwords_buf_to) {
1309 sprintf(err_buf,
"Buffer overflow. Exiting... %d %d\n", pos_nwords_to, nwords_buf_to);
1310 printf(
"%s", err_buf); fflush(stdout);
1316 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1317 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1318 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1319 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1320 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1325 *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS) = nwords_buf_to;
1326 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE) = pos_nwords_finesse[ 0 ];
1327 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE) = pos_nwords_finesse[ 1 ];
1328 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE) = pos_nwords_finesse[ 2 ];
1329 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE) = pos_nwords_finesse[ 3 ];
1332 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1333 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1334 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1335 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1336 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1339 unsigned int new_rawcopper_chksum = CalcXORChecksum(buf_to, pos_nwords_to - tmp_trailer.GetTrlNwords());
1342 if ((old_rawcopper_chksum ^ removed_xor_chksum) != new_rawcopper_chksum) {
1345 GetNodeName(n, hostname,
sizeof(hostname));
1347 "[FATAL] %s ch=%d : ERROR_EVENT : RawCOPPER XOR checksum is inconsistent between before/after data reduction.(%.8x != %.8x ^ %.8x = %.8x ) Exiting...\n %s %s %d\n",
1349 new_rawcopper_chksum, old_rawcopper_chksum, removed_xor_chksum, old_rawcopper_chksum ^ removed_xor_chksum,
1350 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1351 printf(
"%s", err_buf); fflush(stdout);
1355 *(buf_to + pos_nwords_to - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM) = new_rawcopper_chksum;
1362 m_reduced_rawcpr.SetBuffer(buf_to, nwords_buf_to, 0, GetNumEvents(), GetNumNodes());
1363 if (m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes() <= 0) {
1365 sprintf(err_buf,
"Invalid data block numbers.(# of events %d, # of nodes %d) Exiting...\n",
1366 m_reduced_rawcpr.GetNumEvents(), m_reduced_rawcpr.GetNumNodes());
1367 printf(
"%s", err_buf); fflush(stdout);
1374 for (
int i = 0; i < m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes(); i++) {
1375 int nonzero_finesse_buf = 0;
1376 for (
int j = 0; j < 4; j++) {
1378 if (GetFINESSENwords(n, j) > 0) {
1380 nonzero_finesse_buf++;
1383 if (nonzero_finesse_buf == 0) {
1385 sprintf(err_buf,
"No non-zero FINESSE buffer. Exiting...\n");
1386 printf(
"%s", err_buf); fflush(stdout);
1410 return pos_nwords_to;
1414 int PreRawCOPPERFormat_v2::CheckB2LHSLBMagicWords(
int* finesse_buf,
int finesse_nwords)
1417 if ((finesse_buf[ POS_MAGIC_B2LHSLB ] & 0xFFFF0000) == B2LHSLB_HEADER_MAGIC &&
1418 ((finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ] & 0xFFFF0000)
1419 == B2LHSLB_TRAILER_MAGIC)) {
1422 PrintData(m_buffer, m_nwords);
1425 "Invalid B2LHSLB magic words : header 0x%x (= should be ffaa**** ) or trailer 0x%x (= should be ff55**** ). Exiting... :%s %s %d\n",
1426 finesse_buf[ POS_MAGIC_B2LHSLB ],
1427 finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ],
1428 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1429 #ifndef NO_ERROR_STOP
1430 printf(
"%s", err_buf); fflush(stdout);
1438 int PreRawCOPPERFormat_v2::CheckCRC16(
int n,
int finesse_num)
1443 int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER;
1444 int nwords = GetFINESSENwords(n, finesse_num) - (SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER);
1445 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, buf, nwords);
1450 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, finesse_num)
1451 - ((SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) + SIZE_B2LHSLB_TRAILER) ;
1453 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
1454 PrintData(GetBuffer(n), *(GetBuffer(n) + tmp_header.POS_NWORDS));
1457 GetNodeName(n, hostname,
sizeof(hostname));
1458 printf(
"[FATAL] %s ch=%d : ERROR_EVENT : PRE CRC16 error : slot %c : B2LCRC16 %x Calculated CRC16 %x : Nwords of FINESSE buf %d\n",
1459 hostname, finesse_num,
1460 65 + finesse_num, *buf , temp_crc16, GetFINESSENwords(n, finesse_num));
1461 int* temp_buf = GetFINESSEBuffer(n, finesse_num);
1462 for (
int k = 0; k < GetFINESSENwords(n, finesse_num); k++) {
1463 printf(
"%.8x ", temp_buf[ k ]);
1464 if ((k + 1) % 10 == 0) {
1471 "[FATAL] %s ch=%d : ERROR_EVENT : slot %c : B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PreRawCOPPERfromat class. Exiting...\n %s %s %d\n",
1472 hostname, finesse_num,
1473 65 + finesse_num, (
unsigned short)(*buf & 0xFFFF), temp_crc16,
1474 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1475 printf(
"%s", err_buf); fflush(stdout);
1482 int* PreRawCOPPERFormat_v2::PackDetectorBuf(
int* packed_buf_nwords,
1483 int* detector_buf_1st,
int nwords_1st,
1484 int* detector_buf_2nd,
int nwords_2nd,
1485 int* detector_buf_3rd,
int nwords_3rd,
1486 int* detector_buf_4th,
int nwords_4th,
1489 int* packed_buf = NULL;
1491 int poswords_to = 0;
1492 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
1493 int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
1496 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
1498 for (
int i = 0; i < 4; i++) {
1499 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1500 length_nwords += nwords[ i ];
1501 length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER
1502 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1506 packed_buf =
new int[ length_nwords ];
1507 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
1512 tmp_header.SetBuffer(packed_buf);
1514 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
1515 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = 0x7f7f8000
1516 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
1517 | tmp_header.RAWHEADER_NWORDS;
1518 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT)
1520 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
1521 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1522 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
1523 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
1529 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
1531 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
1532 if (nwords[ 0 ] > 0) {
1533 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
1534 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1537 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
1538 if (nwords[ 1 ] > 0) {
1539 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
1540 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1543 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
1544 if (nwords[ 2 ] > 0) {
1545 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
1546 SIZE_B2LHSLB_TRAILER;
1548 poswords_to += tmp_header.GetHdrNwords();
1551 packed_buf[ poswords_to + POS_MAGIC_COPPER_1 ] = COPPER_MAGIC_DRIVER_HEADER;
1552 packed_buf[ poswords_to + POS_MAGIC_COPPER_2 ] = COPPER_MAGIC_FPGA_HEADER;
1553 packed_buf[ poswords_to + POS_EVE_NUM_COPPER ] = rawcpr_info.
eve_num;
1555 int size_b2l_hdrtrl = SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1556 if (nwords[ 0 ] != 0) packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] = nwords[ 0 ] + size_b2l_hdrtrl;
1557 if (nwords[ 1 ] != 0) packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] = nwords[ 1 ] + size_b2l_hdrtrl;
1558 if (nwords[ 2 ] != 0) packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] = nwords[ 2 ] + size_b2l_hdrtrl;
1559 if (nwords[ 3 ] != 0) packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] = nwords[ 3 ] + size_b2l_hdrtrl;
1561 packed_buf[ poswords_to + POS_DATA_LENGTH ] =
1562 packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] +
1563 packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] +
1564 packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] +
1565 packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] +
1566 (SIZE_COPPER_HEADER - SIZE_COPPER_DRIVER_HEADER) +
1567 (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER);
1569 poswords_to += SIZE_COPPER_HEADER;
1572 for (
int i = 0; i < 4; i++) {
1574 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1577 packed_buf[ poswords_to + POS_MAGIC_B2LHSLB ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
1578 poswords_to += SIZE_B2LHSLB_HEADER;
1579 int* crc16_start = &(packed_buf[ poswords_to ]);
1583 packed_buf[ poswords_to + POS_TT_CTIME_TYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1584 unsigned int temp_ctime_type = packed_buf[ poswords_to + POS_TT_CTIME_TYPE ];
1585 packed_buf[ poswords_to + POS_TT_TAG ] = rawcpr_info.
eve_num;
1586 packed_buf[ poswords_to + POS_TT_UTIME ] = rawcpr_info.
tt_utime;
1587 packed_buf[ poswords_to + POS_EXP_RUN ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT) | (rawcpr_info.
run_subrun_num &
1589 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
1590 poswords_to += SIZE_B2LFEE_HEADER;
1593 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
1594 poswords_to += nwords[ i ];
1597 packed_buf[ poswords_to + POS_TT_CTIME_B2LFEE ] = temp_ctime_type;
1600 unsigned short crc16 = CalcCRC16LittleEndian(0xffff, crc16_start, nwords[ i ] + SIZE_B2LFEE_HEADER);
1601 packed_buf[ poswords_to + POS_CHKSUM_B2LFEE ] = ((0xffff & rawcpr_info.
eve_num) << 16) | (crc16 & 0xffff);
1602 poswords_to += SIZE_B2LFEE_TRAILER;
1605 packed_buf[ poswords_to + POS_CHKSUM_B2LHSLB ] = 0xff550000;
1606 poswords_to += SIZE_B2LHSLB_TRAILER;
1611 packed_buf[ poswords_to + POS_MAGIC_COPPER_3 ] = COPPER_MAGIC_FPGA_TRAILER;
1612 packed_buf[ poswords_to + POS_MAGIC_COPPER_4 ] = COPPER_MAGIC_DRIVER_TRAILER;
1613 unsigned int chksum = 0;
1614 for (
int i = tmp_header.GetHdrNwords(); i < poswords_to + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); i++) {
1615 chksum ^= packed_buf[ i ];
1617 packed_buf[ poswords_to + POS_CHKSUM_COPPER ] = chksum;
1618 poswords_to += SIZE_COPPER_TRAILER;
1622 for (
int i = 0; i < poswords_to; i++) {
1623 chksum ^= packed_buf[ i ];
1625 packed_buf[ poswords_to + tmp_trailer.POS_CHKSUM ] = chksum;
1627 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
1628 poswords_to += tmp_trailer.GetTrlNwords();
1630 *packed_buf_nwords = poswords_to;
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)
Abstract base class for different kinds of events.