9 #include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h>
20 PreRawCOPPERFormat_latest::PreRawCOPPERFormat_latest()
24 PreRawCOPPERFormat_latest::~PreRawCOPPERFormat_latest()
28 unsigned int PreRawCOPPERFormat_latest::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_latest::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_latest::GetFINESSENwords(
int n,
int finesse_num)
57 if (!CheckCOPPERMagic(n)) {
60 "[FATAL] 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",
61 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
62 __FILE__, __PRETTY_FUNCTION__, __LINE__);
63 printf(
"[DEBUG] %s", err_buf); fflush(stdout);
64 PrintData(m_buffer, m_nwords);
66 for (
int i = 0; i < 4; i++) {
67 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
68 if (GetFINESSENwords(n, i) > 0) {
72 printf(
"[DEBUG] ========== No CRC error. : block %d =========\n", n);
77 switch (finesse_num) {
79 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_A_DATA_LENGTH;
82 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_B_DATA_LENGTH;
85 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_C_DATA_LENGTH;
88 pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_D_DATA_LENGTH;
93 "[FATAL] ERROR_EVENT : Specifined FINESSE number( = %d ) is invalid. Exiting... : slot%c eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
95 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
96 __FILE__, __PRETTY_FUNCTION__, __LINE__);
97 printf(
"%s", err_buf); fflush(stdout);
101 return m_buffer[ pos_nwords ];
106 unsigned int PreRawCOPPERFormat_latest::GetB2LFEE32bitEventNumber(
int n)
109 #ifndef READ_OLD_B2LFEE_FORMAT_FILE
112 unsigned int eve_num = 0;
115 for (
int i = 0; i < 4 ; i++) {
116 eve[ i ] = 0xbaadf00d;
117 if (GetFINESSENwords(n, i) > 0) {
118 int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
119 eve[ i ] = m_buffer[ pos_nwords ];
120 if (flag != 1) eve_num = eve[ i ];
121 if (eve_num != eve[ i ]) err_flag = 1;
127 PrintData(m_buffer, m_nwords);
129 sprintf(err_buf,
"[FATAL] ERROR_EVENT : No HSLB data in COPPER data. Exiting... : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
130 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
131 __FILE__, __PRETTY_FUNCTION__, __LINE__);
132 printf(
"%s", err_buf); fflush(stdout);
141 "[FATAL] 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",
142 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
143 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
144 __FILE__, __PRETTY_FUNCTION__, __LINE__);
145 printf(
"[DEBUG] %s\n", err_buf);
146 PrintData(m_buffer, m_nwords);
147 for (
int i = 0; i < 4; i++) {
148 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
149 if (GetFINESSENwords(n, i) > 0) {
153 printf(
"[DEBUG] ========== No CRC error. : block %d =========\n", n);
154 #ifndef NO_ERROR_STOP
157 #endif //NO_ERROR_STOP
161 #else // READ_OLD_B2LFEE_FORMAT_FILE
164 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",
165 __FILE__, __PRETTY_FUNCTION__, __LINE__);
166 printf(
"%s", err_buf); fflush(stdout);
168 #endif // READ_OLD_B2LFEE_FORMAT_FILE
173 void PreRawCOPPERFormat_latest::CheckData(
int n,
174 unsigned int prev_evenum,
unsigned int* cur_evenum_rawcprhdr,
175 unsigned int prev_copper_ctr,
unsigned int* cur_copper_ctr,
176 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
184 if (!CheckCOPPERMagic(n)) {
186 "[FATAL] 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",
187 GetMagicDriverHeader(n), GetMagicFPGAHeader(n), GetMagicFPGATrailer(n), GetMagicDriverTrailer(n),
188 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
189 __FILE__, __PRETTY_FUNCTION__, __LINE__);
196 *cur_evenum_rawcprhdr = GetEveNo(n);
197 unsigned int evenum_feehdr = GetB2LFEE32bitEventNumber(n);
198 if (*cur_evenum_rawcprhdr != evenum_feehdr) {
200 "[FATAL] ERROR_EVENT : Event # in PreRawCOPPERFormat_latest 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",
201 *cur_evenum_rawcprhdr, evenum_feehdr,
202 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
203 __FILE__, __PRETTY_FUNCTION__, __LINE__);
210 *cur_exprunsubrun_no = GetExpRunSubrun(n);
211 *cur_copper_ctr = GetCOPPERCounter(n);
212 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
213 if ((
unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) {
215 "[FATAL] 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",
216 n, prev_evenum, *cur_evenum_rawcprhdr, prev_exprunsubrun_no, *cur_exprunsubrun_no,
217 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
218 __FILE__, __PRETTY_FUNCTION__, __LINE__);
221 if ((
unsigned int)(prev_copper_ctr + 1) != *cur_copper_ctr) {
222 sprintf(err_buf,
"[FATAL] 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",
223 n, prev_copper_ctr, *cur_copper_ctr,
224 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
225 __FILE__, __PRETTY_FUNCTION__, __LINE__);
229 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",
230 *cur_exprunsubrun_no, prev_exprunsubrun_no , *cur_evenum_rawcprhdr, prev_evenum,
231 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n));
234 if ((
unsigned int)GetRunNo(n) != (prev_exprunsubrun_no & RawHeader_latest::RUNNO_MASK) >> RawHeader_latest::RUNNO_SHIFT) {
235 if (*cur_evenum_rawcprhdr != 0) {
238 for (
int i = 0; i < 4 ; i++) {
239 eve[ i ] = 0xbaadf00d;
240 if (GetFINESSENwords(n, i) > 0) {
241 int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
242 eve[ i ] = m_buffer[ pos_nwords ];
246 "[FATAL] ERROR_EVENT : Invalid Event # at the beginning of the run (It should be zero.): preveve 0x%x cureve 0x%x : prev(exp %u run %d sub %u ) cur(exp %u run %d 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",
247 prev_evenum, *cur_evenum_rawcprhdr,
248 prev_exprunsubrun_no >> 22 , (prev_exprunsubrun_no >> 8) & 0x3FFF, prev_exprunsubrun_no & 0xFF,
249 *cur_exprunsubrun_no >> 22 , (*cur_exprunsubrun_no >> 8) & 0x3FFF, *cur_exprunsubrun_no & 0xFF,
250 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
251 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
252 __FILE__, __PRETTY_FUNCTION__, __LINE__);
264 CheckUtimeCtimeTRGType(n);
269 if (GetDriverChkSum(n) != CalcDriverChkSum(n)) {
271 "[FATAL] 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",
272 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, GetDriverChkSum(n), CalcDriverChkSum(n),
273 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
274 __FILE__, __PRETTY_FUNCTION__, __LINE__);
281 tmp_trailer.SetBuffer(GetRawTrlBufPtr(n));
282 unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords());
283 if (tmp_trailer.GetChksum() != xor_chksum) {
285 "[FATAL] ERROR_EVENT : PreRawCOPPERFormat_latest 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",
286 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum,
287 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
288 __FILE__, __PRETTY_FUNCTION__, __LINE__);
293 printf(
"%s", err_buf); fflush(stdout);
294 printf(
"[DEBUG] ========== dump a data blcok : block # %d==========\n", n);
295 PrintData(GetBuffer(n), GetBlockNwords(n));
296 for (
int i = 0; i < 4; i++) {
297 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
298 if (GetFINESSENwords(n, i) > 0) {
302 printf(
"[DEBUG] ========== No CRC error : block %d =========\n", n);
311 bool PreRawCOPPERFormat_latest::CheckCOPPERMagic(
int n)
313 if (GetMagicDriverHeader(n) != COPPER_MAGIC_DRIVER_HEADER) {
315 }
else if (GetMagicFPGAHeader(n) != COPPER_MAGIC_FPGA_HEADER) {
317 }
else if (GetMagicFPGATrailer(n) != COPPER_MAGIC_FPGA_TRAILER) {
319 }
else if (GetMagicDriverTrailer(n) != COPPER_MAGIC_DRIVER_TRAILER) {
325 void PreRawCOPPERFormat_latest::CheckUtimeCtimeTRGType(
int n)
328 #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2
329 CheckB2LFEEHeaderVersion(n);
333 unsigned int temp_utime = 0, temp_ctime_trgtype = 0, temp_eve = 0, temp_exprun = 0;
334 unsigned int temp_ctime_trgtype_footer = 0, temp_eve_footer = 0;
335 unsigned int utime[4], ctime_trgtype[4], eve[4], exprun[4];
338 memset(utime, 0,
sizeof(utime));
339 memset(ctime_trgtype, 0,
sizeof(ctime_trgtype));
340 memset(eve, 0,
sizeof(eve));
341 memset(exprun, 0,
sizeof(exprun));
344 for (
int i = 0; i < 4; i++) {
345 int finesse_nwords = GetFINESSENwords(n, i);
346 if (finesse_nwords > 0) {
347 int offset_finesse = GetOffsetFINESSE(n, i);
348 ctime_trgtype[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
349 utime[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
350 eve[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
351 exprun[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
352 temp_ctime_trgtype_footer =
353 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_TT_CTIME_B2LFEE ];
355 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_CHKSUM_B2LFEE ];
358 temp_ctime_trgtype = ctime_trgtype[ i ];
359 temp_utime = utime[ i ];
361 temp_exprun = exprun[ i ];
364 if (temp_ctime_trgtype != ctime_trgtype[ i ] || temp_utime != utime[ i ] ||
365 temp_eve != eve[ i ] || temp_exprun != exprun[ i ]) {
367 for (
int j = 0; j < 4; j++) {
368 printf(
"[DEBUG] FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x\n",
369 j, GetFINESSENwords(n, j), ctime_trgtype[ j ], utime[ j ], eve[ j ], exprun[ j ]);
372 sprintf(err_buf,
"[FATAL] ERROR_EVENT : mismatch header value over FINESSEs. Exiting...\n %s %s %d\n",
373 __FILE__, __PRETTY_FUNCTION__, __LINE__);
374 printf(
"%s", err_buf); fflush(stdout);
378 }
else if (temp_ctime_trgtype != temp_ctime_trgtype_footer ||
379 (temp_eve & 0xffff) != ((temp_eve_footer >> 16) & 0xffff)) {
381 "[FATAL] ERROR_EVENT : mismatch(finesse %d) between header(ctime %.8x eve %.8x) and footer(ctime %.8x eve_crc16 %.8x). Exiting...\n %s %s %d\n",
382 i, temp_ctime_trgtype, temp_eve, temp_ctime_trgtype_footer, temp_eve_footer,
383 __FILE__, __PRETTY_FUNCTION__, __LINE__);
384 printf(
"%s", err_buf); fflush(stdout);
392 for (
int i = 0; i < 4; i++) {
393 if (GetFINESSENwords(n, i) > 0) {
394 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
396 printf(
"[DEBUG] ========== CRC check is done. : block %d =========\n", n);
399 #ifndef NO_ERROR_STOP
407 unsigned int PreRawCOPPERFormat_latest::FillTopBlockRawHeader(
unsigned int m_node_id,
unsigned int prev_eve32,
408 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
410 const int datablock_id = 0;
415 if (m_num_nodes * m_num_events != 1) {
418 "[FATAL] This function should be used for PreRawCOPPERFormat_latest containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n",
419 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
420 printf(
"%s", err_buf); fflush(stdout);
434 memset(m_buffer, 0,
sizeof(
int) * tmp_header.RAWHEADER_NWORDS);
435 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] = tmp_header.RAWHEADER_NWORDS & tmp_header.HDR_NWORDS_MASK;
436 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) &
437 tmp_header.FORMAT_VERSION__MASK;
438 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (0x80 << tmp_header.FORMAT_VERSION_SHIFT);
439 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= tmp_header.MAGIC_WORD;
444 int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]);
445 if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 &&
446 copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 &&
447 copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 &&
448 copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) {
451 "[FATAL] ERROR_EVENT : No FINESSE data in a copper data block. Exiting...\n %s %s %d\n",
452 __FILE__, __PRETTY_FUNCTION__, __LINE__);
453 printf(
"%s", err_buf); fflush(stdout);
461 int datablock_nwords =
462 tmp_header.RAWHEADER_NWORDS +
463 (copper_buf[ POS_DATA_LENGTH ]
464 + SIZE_COPPER_DRIVER_HEADER
465 + SIZE_COPPER_DRIVER_TRAILER)
466 + tmp_trailer.RAWTRAILER_NWORDS;
467 m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords;
473 if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) {
476 "[FATAL] ERROR_EVENT : Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n",
477 m_nwords, m_buffer[ tmp_header.POS_NWORDS ],
478 __FILE__, __PRETTY_FUNCTION__, __LINE__);
479 printf(
"%s", err_buf); fflush(stdout);
487 int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
488 int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ];
489 int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ];
490 int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ];
491 m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse;
492 m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse;
493 m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse;
494 m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse;
500 (m_buffer[ offset_1st_finesse ]);
501 m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
507 unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
508 m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32;
513 m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
514 m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
519 m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id;
529 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = 0;
530 unsigned int ff55_higher_bits = 0, ff55_lower_bits = 0;
532 if (copper_buf[ POS_CH_A_DATA_LENGTH ] != 0) {
533 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
535 ff55_lower_bits = m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF;
537 if (ff55_higher_bits != 0xff550000) {
540 "[FATAL] ERROR_EVENT : HSLB slotA's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n",
541 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
542 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
543 __FILE__, __PRETTY_FUNCTION__, __LINE__);
544 printf(
"%s", err_buf); fflush(stdout);
545 PrintData(m_buffer, m_nwords); fflush(stdout);
549 if (ff55_lower_bits != 0) {
550 const int linkdown_bit = 15;
553 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
554 sprintf(err_buf,
"[FATAL] B2link down on slot A eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
555 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
556 __FILE__, __PRETTY_FUNCTION__, __LINE__);
558 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
559 sprintf(err_buf,
"[FATAL] B2link packet CRC error slot A eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
560 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
561 __FILE__, __PRETTY_FUNCTION__, __LINE__);
563 printf(
"%s", err_buf); fflush(stdout);
564 PrintData(m_buffer, m_nwords);
570 if (copper_buf[ POS_CH_B_DATA_LENGTH ] != 0) {
571 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
573 ff55_lower_bits = (m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
575 if (ff55_higher_bits != 0xff550000) {
578 "[FATAL] ERROR_EVENT : HSLB slotB's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n",
579 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
580 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
581 __FILE__, __PRETTY_FUNCTION__, __LINE__);
582 printf(
"%s", err_buf); fflush(stdout);
583 PrintData(m_buffer, m_nwords); fflush(stdout);
587 if (ff55_lower_bits != 0) {
589 const int linkdown_bit = 15;
591 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
592 sprintf(err_buf,
"[FATAL] B2link down on slot B eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
593 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
594 __FILE__, __PRETTY_FUNCTION__, __LINE__);
596 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
597 sprintf(err_buf,
"[FATAL] B2link packet CRC error slot B eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
598 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
599 __FILE__, __PRETTY_FUNCTION__, __LINE__);
601 printf(
"%s", err_buf); fflush(stdout);
602 PrintData(m_buffer, m_nwords); fflush(stdout);
607 if (copper_buf[ POS_CH_C_DATA_LENGTH ] != 0) {
608 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
610 ff55_lower_bits = (m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
612 if (ff55_higher_bits != 0xff550000) {
615 "[FATAL] ERROR_EVENT : HSLB slotC's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n",
616 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
617 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
618 __FILE__, __PRETTY_FUNCTION__, __LINE__);
619 printf(
"%s", err_buf); fflush(stdout);
620 PrintData(m_buffer, m_nwords); fflush(stdout);
624 if (ff55_lower_bits != 0) {
626 const int linkdown_bit = 15;
628 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
629 sprintf(err_buf,
"[FATAL] B2link down on slot C eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
630 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
631 __FILE__, __PRETTY_FUNCTION__, __LINE__);
633 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
634 sprintf(err_buf,
"[FATAL] B2link packet CRC error slot C eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
635 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
636 __FILE__, __PRETTY_FUNCTION__, __LINE__);
638 printf(
"%s", err_buf); fflush(stdout);
639 PrintData(m_buffer, m_nwords); fflush(stdout);
644 if (copper_buf[ POS_CH_D_DATA_LENGTH ] != 0) {
645 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
647 ff55_lower_bits = (m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
649 if (ff55_higher_bits != 0xff550000) {
652 "[FATAL] ERROR_EVENT : HSLB slotD's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n",
653 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
654 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
655 __FILE__, __PRETTY_FUNCTION__, __LINE__);
656 printf(
"%s", err_buf); fflush(stdout);
657 PrintData(m_buffer, m_nwords); fflush(stdout);
661 if (ff55_lower_bits != 0) {
663 const int linkdown_bit = 15;
664 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
665 sprintf(err_buf,
"[FATAL] B2link down on slot D eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
666 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
667 __FILE__, __PRETTY_FUNCTION__, __LINE__);
669 sprintf(err_buf,
"[FATAL] B2link packet CRC error slot D eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32,
670 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
671 __FILE__, __PRETTY_FUNCTION__, __LINE__);
673 printf(
"%s", err_buf); fflush(stdout);
674 PrintData(m_buffer, m_nwords); fflush(stdout);
689 unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0;
691 int top_end = tmp_header.RAWHEADER_NWORDS;
692 for (
int i = 0; i < top_end; i++) {
693 chksum_top ^= m_buffer[ i ];
695 int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS;
696 for (
int i = top_end; i < body_end; i++) {
697 chksum_body ^= m_buffer[ i ];
700 int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS;
701 for (
int i = body_end; i < bottom_end; i++) {
702 chksum_bottom ^= m_buffer[ i ];
708 if (chksum_body != (
unsigned int)(m_buffer[ body_end ])) {
710 sprintf(err_buf,
"[FATAL] ERROR_EVENT : COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n",
711 chksum_body, m_buffer[ body_end ],
712 __FILE__, __PRETTY_FUNCTION__, __LINE__);
713 printf(
"%s", err_buf); fflush(stdout);
720 unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom;
721 int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]);
722 trl[ tmp_trailer.POS_CHKSUM ] = chksum;
723 trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
736 int* fpga_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_3);
737 int* driver_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_4);
739 if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) {
741 }
else if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) {
743 }
else if ((
unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) {
745 }
else if ((
unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) {
750 sprintf(err_buf,
"[FATAL] ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n",
751 GetMagicDriverHeader(datablock_id),
752 GetMagicFPGAHeader(datablock_id),
753 GetMagicFPGATrailer(datablock_id),
754 GetMagicDriverTrailer(datablock_id),
755 __FILE__, __PRETTY_FUNCTION__, __LINE__);
756 printf(
"[DEBUG] %s\n", err_buf);
757 #ifndef NO_ERROR_STOP
767 *cur_exprunsubrun_no = GetExpRunSubrun(datablock_id);
773 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
774 if (prev_eve32 + 1 != cur_ftsw_eve32) {
775 unsigned int eve[4] = {0xbaadf00d, 0xbaadf00d, 0xbaadf00d, 0xbaadf00d };
776 #ifndef NO_ERROR_STOP
777 if (m_num_nodes * m_num_events != 1) {
780 "[FATAL] This function should be used for PreRawCOPPERFormat_latest containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n",
781 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
782 printf(
"%s", err_buf); fflush(stdout);
786 for (
int i = 0; i < 4 ; i++) {
789 if (GetFINESSENwords(0 , i) > 0) {
790 int pos_nwords = GetOffsetFINESSE(0, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
791 eve[ i ] = m_buffer[ pos_nwords ];
798 "[FATAL] 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",
799 cur_ftsw_eve32, prev_eve32,
800 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
801 prev_exprunsubrun_no, *cur_exprunsubrun_no,
802 __FILE__, __PRETTY_FUNCTION__, __LINE__);
803 printf(
"[DEBUG] %s\n", err_buf);
805 string err_str = err_buf;
806 printf(
"[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords());
807 PrintData(GetBuffer(datablock_id), TotalBufNwords());
809 for (
int i = 0; i < 4; i++) {
810 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", datablock_id, i);
811 if (GetFINESSENwords(datablock_id, i) > 0) {
812 CheckCRC16(datablock_id, i);
815 printf(
"[DEBUG] ========== No CRC error : block %d =========\n", datablock_id);
823 return cur_ftsw_eve32;
829 #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2
830 void PreRawCOPPERFormat_latest::CheckB2LFEEHeaderVersion(
int n)
833 for (
int i = 0; i < 4; i++) {
834 if (GetFINESSENwords(n, i) > 0) {
835 temp_buf = GetFINESSEBuffer(n, i);
836 if ((temp_buf[ 3 ] & 0x40000000) == 0) {
840 printf(
"[DEBUG] \033[31m");
841 printf(
"[DEBUG] ===Firmware ver. ERROR===\n ");
842 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");
843 printf(
"[DEBUG] If you are going to take data now, Please update the firmware.\n");
844 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");
845 printf(
"[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn revision 7419\n");
846 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");
847 printf(
"[DEBUG] Sorry for inconvenience.\n");
848 printf(
"[DEBUG] \033[0m");
851 sprintf(err_buf,
"[FATAL] ERROR_EVENT : FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n",
852 __FILE__, __PRETTY_FUNCTION__, __LINE__);
865 sprintf(err_buf,
"[FATAL] ERROR_EVENT : PreRawCOPPERFormat_latest contains no FINESSE data. Exiting...\n %s %s %d\n",
866 __FILE__, __PRETTY_FUNCTION__, __LINE__);
867 printf(
"%s", err_buf); fflush(stdout);
878 int PreRawCOPPERFormat_latest::CalcReducedDataSize(
int* bufin,
int nwords,
int num_events,
int num_nodes)
885 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
887 int reduced_nwords = 0;
889 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
890 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
891 int entry_id = l + k * num_nodes_in_sendblock;
897 int temp_delete_flag = 0, temp_num_eve = 1, temp_num_nodes = 1;
902 temp_delete_flag, temp_num_eve,
908 return reduced_nwords;
913 void PreRawCOPPERFormat_latest::CopyReducedData(
int* bufin,
int nwords,
int num_events,
int num_nodes,
int* buf_to,
int* nwords_to)
920 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
922 int pos_nwords_to = 0;
924 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
925 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
926 int entry_id = l + k * num_nodes_in_sendblock;
929 radblk_fmt.
CopyBlock(entry_id, buf_to + pos_nwords_to);
933 SetBuffer(radblk_fmt.
GetBuffer(entry_id),
936 pos_nwords_to += CopyReducedBuffer(0, buf_to + pos_nwords_to);
943 *nwords_to = pos_nwords_to;
949 int PreRawCOPPERFormat_latest::CalcReducedNwords(
int n)
958 nwords_to += tmp_header.RAWHEADER_NWORDS;
960 for (
int j = 0; j < 4; j++) {
962 int finesse_nwords = GetFINESSENwords(n, j);
963 if (finesse_nwords > 0) {
970 - (SIZE_B2LHSLB_HEADER - m_reduced_rawcpr.SIZE_B2LHSLB_HEADER)
971 - (SIZE_B2LFEE_HEADER - m_reduced_rawcpr.SIZE_B2LFEE_HEADER)
972 - (SIZE_B2LFEE_TRAILER - m_reduced_rawcpr.SIZE_B2LFEE_TRAILER)
973 - (SIZE_B2LHSLB_TRAILER - m_reduced_rawcpr.SIZE_B2LHSLB_TRAILER);
979 nwords_to += tmp_trailer.GetTrlNwords();
989 int PreRawCOPPERFormat_latest::CopyReducedBuffer(
int n,
int* buf_to)
995 int* buf_from = NULL;
996 int nwords_buf_to = CalcReducedNwords(n);
997 int pos_nwords_to = 0;
1000 unsigned int removed_xor_chksum = 0;
1005 copy_nwords = tmp_header.RAWHEADER_NWORDS;
1006 buf_from = GetBuffer(n);
1007 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1011 removed_xor_chksum ^= buf_from[ tmp_header.POS_VERSION_HDRNWORDS ];
1014 buf_to[ tmp_header.POS_VERSION_HDRNWORDS ] &= 0xFFFF7FFF;
1017 removed_xor_chksum ^= buf_to[ tmp_header.POS_VERSION_HDRNWORDS ];
1018 for (
int i = 0; i < SIZE_COPPER_HEADER; i++) {
1019 removed_xor_chksum ^= buf_from[ tmp_header.RAWHEADER_NWORDS + i ];
1027 int pos_nwords_finesse[ 4 ];
1028 for (
int j = 0; j < 4; j++) {
1029 pos_nwords_finesse[ j ] = pos_nwords_to;
1030 if (GetFINESSENwords(n, j) > 0) {
1031 int* finesse_buf = GetFINESSEBuffer(n, j);
1032 int finesse_nwords = GetFINESSENwords(n, j);
1035 CheckB2LHSLBMagicWords(finesse_buf, finesse_nwords);
1037 buf_to[ pos_nwords_to ] = finesse_buf[ POS_MAGIC_B2LHSLB ];
1043 + SIZE_B2LHSLB_HEADER
1049 if (finesse_nwords - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER
1050 - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER < 0) {
1053 "[FATAL] ERROR_EVENT : Finesse buffer size is too small( %d words < %d words). May be the data are corrupted. Exiting...\n %s %s %d\n",
1054 finesse_nwords, SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER,
1055 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1056 printf(
"%s", err_buf); fflush(stdout);
1062 for (
int k = 1; k <= 4 ; k++) {
1063 removed_xor_chksum ^= finesse_buf[ k ];
1068 - SIZE_B2LHSLB_HEADER
1070 - SIZE_B2LFEE_TRAILER
1071 - SIZE_B2LHSLB_TRAILER;
1072 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1076 buf_to[ pos_nwords_to ] = 0xffff & finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER - (SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) ];
1078 buf_to[ pos_nwords_to ] |= (finesse_buf[ finesse_nwords - (SIZE_B2LHSLB_TRAILER - POS_MAGIC_B2LHSLB) ] << 16) & 0xFFFF0000;
1081 for (
int k = 0; k <= 2 ; k++) {
1082 removed_xor_chksum ^= finesse_buf[ finesse_nwords - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER + k ];
1084 removed_xor_chksum ^= buf_to[ pos_nwords_to ];
1097 - tmp_trailer.GetTrlNwords();
1098 copy_nwords = tmp_trailer.GetTrlNwords();
1099 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1102 unsigned int old_rawcopper_chksum = buf_from[ tmp_trailer.POS_CHKSUM ];
1103 for (
int i = 0; i < SIZE_COPPER_TRAILER; i++) {
1104 removed_xor_chksum ^= (
unsigned int) * (buf_from - SIZE_COPPER_TRAILER + i);
1109 if (pos_nwords_to != nwords_buf_to) {
1111 sprintf(err_buf,
"Buffer overflow. Exiting... %d %d\n", pos_nwords_to, nwords_buf_to);
1112 printf(
"%s", err_buf); fflush(stdout);
1118 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1119 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1120 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1121 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1122 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1127 *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS) = nwords_buf_to;
1128 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE) = pos_nwords_finesse[ 0 ];
1129 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE) = pos_nwords_finesse[ 1 ];
1130 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE) = pos_nwords_finesse[ 2 ];
1131 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE) = pos_nwords_finesse[ 3 ];
1134 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1135 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1136 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1137 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1138 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1141 unsigned int new_rawcopper_chksum = CalcXORChecksum(buf_to, pos_nwords_to - tmp_trailer.GetTrlNwords());
1144 if ((old_rawcopper_chksum ^ removed_xor_chksum) != new_rawcopper_chksum) {
1147 "[FATAL] ERROR_EVENT : RawCOPPER XOR checksum is inconsistent between before/after data reduction.(%.8x != %.8x ^ %.8x = %.8x ) Exiting...\n %s %s %d\n",
1148 new_rawcopper_chksum, old_rawcopper_chksum, removed_xor_chksum, old_rawcopper_chksum ^ removed_xor_chksum,
1149 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1150 printf(
"%s", err_buf); fflush(stdout);
1154 *(buf_to + pos_nwords_to - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM) = new_rawcopper_chksum;
1161 m_reduced_rawcpr.SetBuffer(buf_to, nwords_buf_to, 0, GetNumEvents(), GetNumNodes());
1162 if (m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes() <= 0) {
1164 sprintf(err_buf,
"Invalid data block numbers.(# of events %d, # of nodes %d) Exiting...\n",
1165 m_reduced_rawcpr.GetNumEvents(), m_reduced_rawcpr.GetNumNodes());
1166 printf(
"%s", err_buf); fflush(stdout);
1173 for (
int i = 0; i < m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes(); i++) {
1174 int nonzero_finesse_buf = 0;
1175 for (
int j = 0; j < 4; j++) {
1177 if (GetFINESSENwords(n, j) > 0) {
1179 nonzero_finesse_buf++;
1182 if (nonzero_finesse_buf == 0) {
1184 sprintf(err_buf,
"No non-zero FINESSE buffer. Exiting...\n");
1185 printf(
"%s", err_buf); fflush(stdout);
1209 return pos_nwords_to;
1213 int PreRawCOPPERFormat_latest::CheckB2LHSLBMagicWords(
int* finesse_buf,
int finesse_nwords)
1216 if ((finesse_buf[ POS_MAGIC_B2LHSLB ] & 0xFFFF0000) == B2LHSLB_HEADER_MAGIC &&
1217 ((finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ] & 0xFFFF0000)
1218 == B2LHSLB_TRAILER_MAGIC)) {
1221 PrintData(m_buffer, m_nwords);
1224 "Invalid B2LHSLB magic words : header 0x%x (= should be ffaa**** ) or trailer 0x%x (= should be ff55**** ). Exiting... :%s %s %d\n",
1225 finesse_buf[ POS_MAGIC_B2LHSLB ],
1226 finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ],
1227 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1228 #ifndef NO_ERROR_STOP
1229 printf(
"%s", err_buf); fflush(stdout);
1237 int PreRawCOPPERFormat_latest::CheckCRC16(
int n,
int finesse_num)
1242 int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER;
1243 int nwords = GetFINESSENwords(n, finesse_num) - (SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER);
1244 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, buf, nwords);
1249 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, finesse_num)
1250 - ((SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) + SIZE_B2LHSLB_TRAILER) ;
1252 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
1253 PrintData(GetBuffer(n), *(GetBuffer(n) + tmp_header.POS_NWORDS));
1254 printf(
"[FATAL] ERROR_EVENT : PRE CRC16 error : slot %c B2LCRC16 %x Calculated CRC16 %x : Nwords of FINESSE buf %d\n",
1255 65 + finesse_num, *buf , temp_crc16, GetFINESSENwords(n, finesse_num));
1256 int* temp_buf = GetFINESSEBuffer(n, finesse_num);
1257 for (
int k = 0; k < GetFINESSENwords(n, finesse_num); k++) {
1258 printf(
"%.8x ", temp_buf[ k ]);
1259 if ((k + 1) % 10 == 0) {
1267 "[FATAL] ERROR_EVENT : B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PreRawCOPPERfromat class. Exiting...\n %s %s %d\n",
1268 (
unsigned short)(*buf & 0xFFFF), temp_crc16,
1269 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1270 printf(
"%s", err_buf); fflush(stdout);
1277 int* PreRawCOPPERFormat_latest::PackDetectorBuf(
int* packed_buf_nwords,
1278 int* detector_buf_1st,
int nwords_1st,
1279 int* detector_buf_2nd,
int nwords_2nd,
1280 int* detector_buf_3rd,
int nwords_3rd,
1281 int* detector_buf_4th,
int nwords_4th,
1284 int* packed_buf = NULL;
1286 int poswords_to = 0;
1287 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
1288 int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
1291 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
1293 for (
int i = 0; i < 4; i++) {
1294 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1295 length_nwords += nwords[ i ];
1296 length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER
1297 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1301 packed_buf =
new int[ length_nwords ];
1302 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
1307 tmp_header.SetBuffer(packed_buf);
1309 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
1310 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = 0x7f7f8000
1311 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
1312 | tmp_header.RAWHEADER_NWORDS;
1313 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT)
1315 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
1316 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1317 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
1318 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
1324 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
1326 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
1327 if (nwords[ 0 ] > 0) {
1328 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
1329 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1332 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
1333 if (nwords[ 1 ] > 0) {
1334 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
1335 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1338 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
1339 if (nwords[ 2 ] > 0) {
1340 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
1341 SIZE_B2LHSLB_TRAILER;
1343 poswords_to += tmp_header.GetHdrNwords();
1346 packed_buf[ poswords_to + POS_MAGIC_COPPER_1 ] = COPPER_MAGIC_DRIVER_HEADER;
1347 packed_buf[ poswords_to + POS_MAGIC_COPPER_2 ] = COPPER_MAGIC_FPGA_HEADER;
1348 packed_buf[ poswords_to + POS_EVE_NUM_COPPER ] = rawcpr_info.
eve_num;
1350 int size_b2l_hdrtrl = SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1351 if (nwords[ 0 ] != 0) packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] = nwords[ 0 ] + size_b2l_hdrtrl;
1352 if (nwords[ 1 ] != 0) packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] = nwords[ 1 ] + size_b2l_hdrtrl;
1353 if (nwords[ 2 ] != 0) packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] = nwords[ 2 ] + size_b2l_hdrtrl;
1354 if (nwords[ 3 ] != 0) packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] = nwords[ 3 ] + size_b2l_hdrtrl;
1356 packed_buf[ poswords_to + POS_DATA_LENGTH ] =
1357 packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] +
1358 packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] +
1359 packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] +
1360 packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] +
1361 (SIZE_COPPER_HEADER - SIZE_COPPER_DRIVER_HEADER) +
1362 (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER);
1364 poswords_to += SIZE_COPPER_HEADER;
1367 for (
int i = 0; i < 4; i++) {
1369 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1372 packed_buf[ poswords_to + POS_MAGIC_B2LHSLB ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
1373 poswords_to += SIZE_B2LHSLB_HEADER;
1374 int* crc16_start = &(packed_buf[ poswords_to ]);
1378 packed_buf[ poswords_to + POS_TT_CTIME_TYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1379 unsigned int temp_ctime_type = packed_buf[ poswords_to + POS_TT_CTIME_TYPE ];
1380 packed_buf[ poswords_to + POS_TT_TAG ] = rawcpr_info.
eve_num;
1381 packed_buf[ poswords_to + POS_TT_UTIME ] = rawcpr_info.
tt_utime;
1382 packed_buf[ poswords_to + POS_EXP_RUN ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT) | (rawcpr_info.
run_subrun_num &
1384 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
1385 poswords_to += SIZE_B2LFEE_HEADER;
1388 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
1389 poswords_to += nwords[ i ];
1392 packed_buf[ poswords_to + POS_TT_CTIME_B2LFEE ] = temp_ctime_type;
1395 unsigned short crc16 = CalcCRC16LittleEndian(0xffff, crc16_start, nwords[ i ] + SIZE_B2LFEE_HEADER);
1396 packed_buf[ poswords_to + POS_CHKSUM_B2LFEE ] = ((0xffff & rawcpr_info.
eve_num) << 16) | (crc16 & 0xffff);
1397 poswords_to += SIZE_B2LFEE_TRAILER;
1400 packed_buf[ poswords_to + POS_CHKSUM_B2LHSLB ] = 0xff550000;
1401 poswords_to += SIZE_B2LHSLB_TRAILER;
1406 packed_buf[ poswords_to + POS_MAGIC_COPPER_3 ] = COPPER_MAGIC_FPGA_TRAILER;
1407 packed_buf[ poswords_to + POS_MAGIC_COPPER_4 ] = COPPER_MAGIC_DRIVER_TRAILER;
1408 unsigned int chksum = 0;
1409 for (
int i = tmp_header.GetHdrNwords(); i < poswords_to + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); i++) {
1410 chksum ^= packed_buf[ i ];
1412 packed_buf[ poswords_to + POS_CHKSUM_COPPER ] = chksum;
1413 poswords_to += SIZE_COPPER_TRAILER;
1417 for (
int i = 0; i < poswords_to; i++) {
1418 chksum ^= packed_buf[ i ];
1420 packed_buf[ poswords_to + tmp_trailer.POS_CHKSUM ] = chksum;
1422 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
1423 poswords_to += tmp_trailer.GetTrlNwords();
1425 *packed_buf_nwords = poswords_to;