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
169 #endif //NO_ERROR_STOP
173 #else // READ_OLD_B2LFEE_FORMAT_FILE
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);
180 #endif // READ_OLD_B2LFEE_FORMAT_FILE
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 %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",
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];
374 memset(utime, 0,
sizeof(utime));
375 memset(ctime_trgtype, 0,
sizeof(ctime_trgtype));
376 memset(eve, 0,
sizeof(eve));
377 memset(exprun, 0,
sizeof(exprun));
380 for (
int i = 0; i < 4; i++) {
381 int finesse_nwords = GetFINESSENwords(n, i);
382 if (finesse_nwords > 0) {
383 int offset_finesse = GetOffsetFINESSE(n, i);
384 ctime_trgtype[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
385 utime[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
386 eve[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
387 exprun[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
388 temp_ctime_trgtype_footer =
389 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_TT_CTIME_B2LFEE ];
391 m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_CHKSUM_B2LFEE ];
394 temp_ctime_trgtype = ctime_trgtype[ i ];
395 temp_utime = utime[ i ];
397 temp_exprun = exprun[ i ];
400 if (temp_ctime_trgtype != ctime_trgtype[ i ] || temp_utime != utime[ i ] ||
401 temp_eve != eve[ i ] || temp_exprun != exprun[ i ]) {
403 for (
int j = 0; j < 4; j++) {
404 printf(
"[DEBUG] FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x\n",
405 j, GetFINESSENwords(n, j), ctime_trgtype[ j ], utime[ j ], eve[ j ], exprun[ j ]);
410 GetNodeName(n, hostname,
sizeof(hostname));
412 "[FATAL] %s ch=%d : ERROR_EVENT : mismatch header value over FINESSEs. Exiting... FINESSE #=0 buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x FINESSE #=1 buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x FINESSE #=2 buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x FINESSE #=3 buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x\n %s %s %d\n",
414 GetFINESSENwords(n, 0), ctime_trgtype[ 0 ], utime[ 0 ], eve[ 0 ], exprun[ 0 ],
415 GetFINESSENwords(n, 1), ctime_trgtype[ 1 ], utime[ 1 ], eve[ 1 ], exprun[ 1 ],
416 GetFINESSENwords(n, 2), ctime_trgtype[ 2 ], utime[ 2 ], eve[ 2 ], exprun[ 2 ],
417 GetFINESSENwords(n, 3), ctime_trgtype[ 3 ], utime[ 3 ], eve[ 3 ], exprun[ 3 ],
418 __FILE__, __PRETTY_FUNCTION__, __LINE__);
419 printf(
"%s", err_buf); fflush(stdout);
423 }
else if (temp_ctime_trgtype != temp_ctime_trgtype_footer ||
424 (temp_eve & 0xffff) != ((temp_eve_footer >> 16) & 0xffff)) {
426 GetNodeName(n, hostname,
sizeof(hostname));
428 "[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",
430 i, temp_ctime_trgtype, temp_eve, temp_ctime_trgtype_footer, temp_eve_footer,
431 __FILE__, __PRETTY_FUNCTION__, __LINE__);
432 printf(
"%s", err_buf); fflush(stdout);
440 for (
int i = 0; i < 4; i++) {
441 if (GetFINESSENwords(n, i) > 0) {
442 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i);
444 printf(
"[DEBUG] ========== CRC check is done. : block %d =========\n", n);
447 #ifndef NO_ERROR_STOP
455 unsigned int PreRawCOPPERFormat_v2::FillTopBlockRawHeader(
unsigned int m_node_id,
unsigned int prev_eve32,
456 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
458 const int datablock_id = 0;
463 if (m_num_nodes * m_num_events != 1) {
466 "[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",
467 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
468 printf(
"%s", err_buf); fflush(stdout);
482 memset(m_buffer, 0,
sizeof(
int) * tmp_header.RAWHEADER_NWORDS);
483 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] = tmp_header.RAWHEADER_NWORDS & tmp_header.HDR_NWORDS_MASK;
484 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) &
485 tmp_header.FORMAT_VERSION__MASK;
486 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (0x80 << tmp_header.FORMAT_VERSION_SHIFT);
487 m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= tmp_header.MAGIC_WORD;
492 int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]);
493 if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 &&
494 copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 &&
495 copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 &&
496 copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) {
499 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
501 "[FATAL] %s ch=%d : ERROR_EVENT : No FINESSE data in a copper data block. Exiting...\n %s %s %d\n",
503 __FILE__, __PRETTY_FUNCTION__, __LINE__);
504 printf(
"%s", err_buf); fflush(stdout);
512 int datablock_nwords =
513 tmp_header.RAWHEADER_NWORDS +
514 (copper_buf[ POS_DATA_LENGTH ]
515 + SIZE_COPPER_DRIVER_HEADER
516 + SIZE_COPPER_DRIVER_TRAILER)
517 + tmp_trailer.RAWTRAILER_NWORDS;
518 m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords;
524 if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) {
528 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
530 "[FATAL] %s ch=%d : ERROR_EVENT : Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n",
532 m_nwords, m_buffer[ tmp_header.POS_NWORDS ],
533 __FILE__, __PRETTY_FUNCTION__, __LINE__);
534 printf(
"%s", err_buf); fflush(stdout);
542 int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
543 int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ];
544 int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ];
545 int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ];
546 m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse;
547 m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse;
548 m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse;
549 m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse;
555 (m_buffer[ offset_1st_finesse ]);
556 m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ];
562 unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ];
563 m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32;
568 m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ];
569 m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ];
574 m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id;
584 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = 0;
585 unsigned int ff55_higher_bits, ff55_lower_bits;
587 if (copper_buf[ POS_CH_A_DATA_LENGTH ] != 0) {
588 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
591 ff55_lower_bits = m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF;
593 if (ff55_higher_bits != 0xff550000) {
596 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
598 "[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",
600 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
601 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
602 __FILE__, __PRETTY_FUNCTION__, __LINE__);
603 printf(
"%s", err_buf); fflush(stdout);
604 PrintData(m_buffer, m_nwords); fflush(stdout);
608 if (ff55_lower_bits != 0) {
609 const int linkdown_bit = 15;
613 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
615 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
616 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot A eve %8u foooter %.8x : %s %s %d\n",
619 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
620 __FILE__, __PRETTY_FUNCTION__, __LINE__);
621 printf(
"%s", err_buf); fflush(stdout);
622 PrintData(m_buffer, m_nwords);
626 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
627 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
628 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x1 << 28);
630 "[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",
633 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
634 __FILE__, __PRETTY_FUNCTION__, __LINE__);
635 printf(
"%s", err_buf); fflush(stdout);
636 PrintData(m_buffer, m_nwords);
639 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
640 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x1 << 28);
641 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot A eve %8u foooter %.8x : %s %s %d\n",
644 m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
645 __FILE__, __PRETTY_FUNCTION__, __LINE__);
646 printf(
"%s", err_buf); fflush(stdout);
647 PrintData(m_buffer, m_nwords);
655 if (copper_buf[ POS_CH_B_DATA_LENGTH ] != 0) {
656 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
659 ff55_lower_bits = (m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
661 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
662 if (ff55_higher_bits != 0xff550000) {
665 "[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",
667 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
668 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
669 __FILE__, __PRETTY_FUNCTION__, __LINE__);
670 printf(
"%s", err_buf); fflush(stdout);
671 PrintData(m_buffer, m_nwords); fflush(stdout);
675 if (ff55_lower_bits != 0) {
677 const int linkdown_bit = 15;
679 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
680 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot B eve %8u foooter %.8x : %s %s %d\n",
683 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
684 __FILE__, __PRETTY_FUNCTION__, __LINE__);
685 printf(
"%s", err_buf); fflush(stdout);
686 PrintData(m_buffer, m_nwords);
690 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
691 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
692 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x2 << 28);
694 "[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",
697 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
698 __FILE__, __PRETTY_FUNCTION__, __LINE__);
699 printf(
"%s", err_buf); fflush(stdout);
700 PrintData(m_buffer, m_nwords);
703 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
704 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x2 << 28);
705 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot B eve %8u foooter %.8x : %s %s %d\n",
708 m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
709 __FILE__, __PRETTY_FUNCTION__, __LINE__);
710 printf(
"%s", err_buf); fflush(stdout);
711 PrintData(m_buffer, m_nwords);
719 if (copper_buf[ POS_CH_C_DATA_LENGTH ] != 0) {
720 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
723 ff55_lower_bits = (m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
725 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
726 if (ff55_higher_bits != 0xff550000) {
729 "[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",
731 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
732 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
733 __FILE__, __PRETTY_FUNCTION__, __LINE__);
734 printf(
"%s", err_buf); fflush(stdout);
735 PrintData(m_buffer, m_nwords); fflush(stdout);
739 if (ff55_lower_bits != 0) {
741 const int linkdown_bit = 15;
743 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
744 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot C eve %8u foooter %.8x : %s %s %d\n",
747 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
748 __FILE__, __PRETTY_FUNCTION__, __LINE__);
749 printf(
"%s", err_buf); fflush(stdout);
750 PrintData(m_buffer, m_nwords);
754 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
755 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
756 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x4 << 28);
758 "[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",
761 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
762 __FILE__, __PRETTY_FUNCTION__, __LINE__);
763 printf(
"%s", err_buf); fflush(stdout);
764 PrintData(m_buffer, m_nwords);
767 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
768 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x4 << 28);
769 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot C eve %8u foooter %.8x : %s %s %d\n",
772 m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
773 __FILE__, __PRETTY_FUNCTION__, __LINE__);
774 printf(
"%s", err_buf); fflush(stdout);
775 PrintData(m_buffer, m_nwords);
784 if (copper_buf[ POS_CH_D_DATA_LENGTH ] != 0) {
785 ff55_higher_bits = (
unsigned int)(m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) &
788 ff55_lower_bits = (m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF);
790 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
791 if (ff55_higher_bits != 0xff550000) {
794 "[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",
796 cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF,
797 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
798 __FILE__, __PRETTY_FUNCTION__, __LINE__);
799 printf(
"%s", err_buf); fflush(stdout);
800 PrintData(m_buffer, m_nwords); fflush(stdout);
804 if (ff55_lower_bits != 0) {
806 const int linkdown_bit = 15;
808 if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) {
809 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link down on slot D eve %8u foooter %.8x : %s %s %d\n",
812 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
813 __FILE__, __PRETTY_FUNCTION__, __LINE__);
814 printf(
"%s", err_buf); fflush(stdout);
815 PrintData(m_buffer, m_nwords);
819 if (((
unsigned int)m_node_id & DETECTOR_MASK) == ARICH_ID) {
820 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
821 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x8 << 28);
823 "[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",
826 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
827 __FILE__, __PRETTY_FUNCTION__, __LINE__);
828 printf(
"%s", err_buf); fflush(stdout);
829 PrintData(m_buffer, m_nwords);
832 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR;
833 m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= (
unsigned int)(0x8 << 28);
834 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : B2link packet CRC error slot D eve %8u foooter %.8x : %s %s %d\n",
837 m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ],
838 __FILE__, __PRETTY_FUNCTION__, __LINE__);
839 printf(
"%s", err_buf); fflush(stdout);
840 PrintData(m_buffer, m_nwords);
858 unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0;
860 int top_end = tmp_header.RAWHEADER_NWORDS;
861 for (
int i = 0; i < top_end; i++) {
862 chksum_top ^= m_buffer[ i ];
864 int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS;
865 for (
int i = top_end; i < body_end; i++) {
866 chksum_body ^= m_buffer[ i ];
869 int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS;
870 for (
int i = body_end; i < bottom_end; i++) {
871 chksum_bottom ^= m_buffer[ i ];
877 if (chksum_body != (
unsigned int)(m_buffer[ body_end ])) {
880 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
881 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n",
883 chksum_body, m_buffer[ body_end ],
884 __FILE__, __PRETTY_FUNCTION__, __LINE__);
885 printf(
"%s", err_buf); fflush(stdout);
892 unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom;
893 int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]);
894 trl[ tmp_trailer.POS_CHKSUM ] = chksum;
895 trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
908 int* fpga_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_3);
909 int* driver_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_4);
911 if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) {
913 }
else if ((
unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) {
915 }
else if ((
unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) {
917 }
else if ((
unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) {
923 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
925 "[FATAL] %s ch=%d : ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n",
927 GetMagicDriverHeader(datablock_id),
928 GetMagicFPGAHeader(datablock_id),
929 GetMagicFPGATrailer(datablock_id),
930 GetMagicDriverTrailer(datablock_id),
931 __FILE__, __PRETTY_FUNCTION__, __LINE__);
932 printf(
"[DEBUG] %s\n", err_buf);
933 #ifndef NO_ERROR_STOP
943 *cur_exprunsubrun_no = GetExpRunSubrun(datablock_id);
949 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
950 if (prev_eve32 + 1 != cur_ftsw_eve32) {
951 unsigned int eve[4] = {0xbaadf00d, 0xbaadf00d, 0xbaadf00d, 0xbaadf00d };
952 #ifndef NO_ERROR_STOP
953 if (m_num_nodes * m_num_events != 1) {
956 "[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",
957 m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__);
958 printf(
"%s", err_buf); fflush(stdout);
962 for (
int i = 0; i < 4 ; i++) {
965 if (GetFINESSENwords(0 , i) > 0) {
966 int pos_nwords = GetOffsetFINESSE(0, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG;
967 eve[ i ] = m_buffer[ pos_nwords ];
974 GetNodeName(hostname, m_node_id ,
sizeof(hostname));
976 "[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",
978 cur_ftsw_eve32, prev_eve32,
979 eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ],
980 prev_exprunsubrun_no, *cur_exprunsubrun_no,
981 __FILE__, __PRETTY_FUNCTION__, __LINE__);
982 printf(
"[DEBUG] %s\n", err_buf);
985 printf(
"[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords());
986 PrintData(GetBuffer(datablock_id), TotalBufNwords());
988 for (
int i = 0; i < 4; i++) {
989 printf(
"[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", datablock_id, i);
990 if (GetFINESSENwords(datablock_id, i) > 0) {
991 CheckCRC16(datablock_id, i);
994 printf(
"[DEBUG] ========== No CRC error : block %d =========\n", datablock_id);
1002 return cur_ftsw_eve32;
1008 #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2
1009 void PreRawCOPPERFormat_v2::CheckB2LFEEHeaderVersion(
int n)
1012 for (
int i = 0; i < 4; i++) {
1013 if (GetFINESSENwords(n, i) > 0) {
1014 temp_buf = GetFINESSEBuffer(n, i);
1015 if ((temp_buf[ 3 ] & 0x40000000) == 0) {
1019 printf(
"[DEBUG] \033[31m");
1020 printf(
"[DEBUG] ===Firmware ver. ERROR===\n ");
1021 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");
1022 printf(
"[DEBUG] If you are going to take data now, Please update the firmware.\n");
1023 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");
1024 printf(
"[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn revision 7419\n");
1025 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");
1026 printf(
"[DEBUG] Sorry for inconvenience.\n");
1027 printf(
"[DEBUG] \033[0m");
1031 GetNodeName(n, hostname,
sizeof(hostname));
1032 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n",
1034 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1048 GetNodeName(n, hostname,
sizeof(hostname));
1049 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : PreRawCOPPERFormat_v2 contains no FINESSE data. Exiting...\n %s %s %d\n",
1051 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1052 printf(
"%s", err_buf); fflush(stdout);
1063 int PreRawCOPPERFormat_v2::CalcReducedDataSize(
int* bufin,
int nwords,
int num_events,
int num_nodes)
1069 int delete_flag = 0;
1070 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
1072 int reduced_nwords = 0;
1074 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
1075 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
1076 int entry_id = l + k * num_nodes_in_sendblock;
1082 int temp_delete_flag = 0, temp_num_eve = 1, temp_num_nodes = 1;
1087 temp_delete_flag, temp_num_eve,
1093 return reduced_nwords;
1098 void PreRawCOPPERFormat_v2::CopyReducedData(
int* bufin,
int nwords,
int num_events,
int num_nodes,
int* buf_to,
int* nwords_to)
1104 int delete_flag = 0;
1105 radblk_fmt.
SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes);
1107 int pos_nwords_to = 0;
1109 int num_nodes_in_sendblock = radblk_fmt.
GetNumNodes();
1110 for (
int l = 0; l < num_nodes_in_sendblock; l++) {
1111 int entry_id = l + k * num_nodes_in_sendblock;
1114 radblk_fmt.
CopyBlock(entry_id, buf_to + pos_nwords_to);
1118 SetBuffer(radblk_fmt.
GetBuffer(entry_id),
1121 pos_nwords_to += CopyReducedBuffer(0, buf_to + pos_nwords_to);
1128 *nwords_to = pos_nwords_to;
1134 int PreRawCOPPERFormat_v2::CalcReducedNwords(
int n)
1143 nwords_to += tmp_header.RAWHEADER_NWORDS;
1145 for (
int j = 0; j < 4; j++) {
1147 int finesse_nwords = GetFINESSENwords(n, j);
1148 if (finesse_nwords > 0) {
1155 - (SIZE_B2LHSLB_HEADER - m_reduced_rawcpr.SIZE_B2LHSLB_HEADER)
1156 - (SIZE_B2LFEE_HEADER - m_reduced_rawcpr.SIZE_B2LFEE_HEADER)
1157 - (SIZE_B2LFEE_TRAILER - m_reduced_rawcpr.SIZE_B2LFEE_TRAILER)
1158 - (SIZE_B2LHSLB_TRAILER - m_reduced_rawcpr.SIZE_B2LHSLB_TRAILER);
1164 nwords_to += tmp_trailer.GetTrlNwords();
1174 int PreRawCOPPERFormat_v2::CopyReducedBuffer(
int n,
int* buf_to)
1180 int* buf_from = NULL;
1181 int nwords_buf_to = CalcReducedNwords(n);
1182 int pos_nwords_to = 0;
1183 int copy_nwords = 0;
1185 unsigned int removed_xor_chksum = 0;
1190 copy_nwords = tmp_header.RAWHEADER_NWORDS;
1191 buf_from = GetBuffer(n);
1192 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1196 removed_xor_chksum ^= buf_from[ tmp_header.POS_VERSION_HDRNWORDS ];
1199 buf_to[ tmp_header.POS_VERSION_HDRNWORDS ] &= 0xFFFF7FFF;
1202 removed_xor_chksum ^= buf_to[ tmp_header.POS_VERSION_HDRNWORDS ];
1203 for (
int i = 0; i < SIZE_COPPER_HEADER; i++) {
1204 removed_xor_chksum ^= buf_from[ tmp_header.RAWHEADER_NWORDS + i ];
1212 int pos_nwords_finesse[ 4 ];
1213 for (
int j = 0; j < 4; j++) {
1214 pos_nwords_finesse[ j ] = pos_nwords_to;
1215 if (GetFINESSENwords(n, j) > 0) {
1216 int* finesse_buf = GetFINESSEBuffer(n, j);
1217 int finesse_nwords = GetFINESSENwords(n, j);
1220 CheckB2LHSLBMagicWords(finesse_buf, finesse_nwords);
1222 buf_to[ pos_nwords_to ] = finesse_buf[ POS_MAGIC_B2LHSLB ];
1228 + SIZE_B2LHSLB_HEADER
1234 if (finesse_nwords - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER
1235 - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER < 0) {
1238 GetNodeName(n, hostname,
sizeof(hostname));
1240 "[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",
1242 finesse_nwords, SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER,
1243 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1244 printf(
"%s", err_buf); fflush(stdout);
1250 for (
int k = 1; k <= 4 ; k++) {
1251 removed_xor_chksum ^= finesse_buf[ k ];
1256 - SIZE_B2LHSLB_HEADER
1258 - SIZE_B2LFEE_TRAILER
1259 - SIZE_B2LHSLB_TRAILER;
1260 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1264 buf_to[ pos_nwords_to ] = 0xffff & finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER - (SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) ];
1266 buf_to[ pos_nwords_to ] |= (finesse_buf[ finesse_nwords - (SIZE_B2LHSLB_TRAILER - POS_MAGIC_B2LHSLB) ] << 16) & 0xFFFF0000;
1269 for (
int k = 0; k <= 2 ; k++) {
1270 removed_xor_chksum ^= finesse_buf[ finesse_nwords - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER + k ];
1272 removed_xor_chksum ^= buf_to[ pos_nwords_to ];
1285 - tmp_trailer.GetTrlNwords();
1286 copy_nwords = tmp_trailer.GetTrlNwords();
1287 copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to);
1290 unsigned int old_rawcopper_chksum = buf_from[ tmp_trailer.POS_CHKSUM ];
1291 for (
int i = 0; i < SIZE_COPPER_TRAILER; i++) {
1292 removed_xor_chksum ^= (
unsigned int) * (buf_from - SIZE_COPPER_TRAILER + i);
1297 if (pos_nwords_to != nwords_buf_to) {
1299 sprintf(err_buf,
"Buffer overflow. Exiting... %d %d\n", pos_nwords_to, nwords_buf_to);
1300 printf(
"%s", err_buf); fflush(stdout);
1306 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1307 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1308 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1309 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1310 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1315 *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS) = nwords_buf_to;
1316 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE) = pos_nwords_finesse[ 0 ];
1317 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE) = pos_nwords_finesse[ 1 ];
1318 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE) = pos_nwords_finesse[ 2 ];
1319 *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE) = pos_nwords_finesse[ 3 ];
1322 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS);
1323 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE);
1324 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE);
1325 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE);
1326 removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE);
1329 unsigned int new_rawcopper_chksum = CalcXORChecksum(buf_to, pos_nwords_to - tmp_trailer.GetTrlNwords());
1332 if ((old_rawcopper_chksum ^ removed_xor_chksum) != new_rawcopper_chksum) {
1335 GetNodeName(n, hostname,
sizeof(hostname));
1337 "[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",
1339 new_rawcopper_chksum, old_rawcopper_chksum, removed_xor_chksum, old_rawcopper_chksum ^ removed_xor_chksum,
1340 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1341 printf(
"%s", err_buf); fflush(stdout);
1345 *(buf_to + pos_nwords_to - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM) = new_rawcopper_chksum;
1352 m_reduced_rawcpr.SetBuffer(buf_to, nwords_buf_to, 0, GetNumEvents(), GetNumNodes());
1353 if (m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes() <= 0) {
1355 sprintf(err_buf,
"Invalid data block numbers.(# of events %d, # of nodes %d) Exiting...\n",
1356 m_reduced_rawcpr.GetNumEvents(), m_reduced_rawcpr.GetNumNodes());
1357 printf(
"%s", err_buf); fflush(stdout);
1364 for (
int i = 0; i < m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes(); i++) {
1365 int nonzero_finesse_buf = 0;
1366 for (
int j = 0; j < 4; j++) {
1368 if (GetFINESSENwords(n, j) > 0) {
1370 nonzero_finesse_buf++;
1373 if (nonzero_finesse_buf == 0) {
1375 sprintf(err_buf,
"No non-zero FINESSE buffer. Exiting...\n");
1376 printf(
"%s", err_buf); fflush(stdout);
1400 return pos_nwords_to;
1404 int PreRawCOPPERFormat_v2::CheckB2LHSLBMagicWords(
int* finesse_buf,
int finesse_nwords)
1407 if ((finesse_buf[ POS_MAGIC_B2LHSLB ] & 0xFFFF0000) == B2LHSLB_HEADER_MAGIC &&
1408 ((finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ] & 0xFFFF0000)
1409 == B2LHSLB_TRAILER_MAGIC)) {
1412 PrintData(m_buffer, m_nwords);
1415 "Invalid B2LHSLB magic words : header 0x%x (= should be ffaa**** ) or trailer 0x%x (= should be ff55**** ). Exiting... :%s %s %d\n",
1416 finesse_buf[ POS_MAGIC_B2LHSLB ],
1417 finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ],
1418 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1419 #ifndef NO_ERROR_STOP
1420 printf(
"%s", err_buf); fflush(stdout);
1428 int PreRawCOPPERFormat_v2::CheckCRC16(
int n,
int finesse_num)
1433 int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER;
1434 int nwords = GetFINESSENwords(n, finesse_num) - (SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER);
1435 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, buf, nwords);
1440 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, finesse_num)
1441 - ((SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) + SIZE_B2LHSLB_TRAILER) ;
1443 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
1444 PrintData(GetBuffer(n), *(GetBuffer(n) + tmp_header.POS_NWORDS));
1447 GetNodeName(n, hostname,
sizeof(hostname));
1448 printf(
"[FATAL] %s ch=%d : ERROR_EVENT : PRE CRC16 error : slot %c : B2LCRC16 %x Calculated CRC16 %x : Nwords of FINESSE buf %d\n",
1449 hostname, finesse_num,
1450 65 + finesse_num, *buf , temp_crc16, GetFINESSENwords(n, finesse_num));
1451 int* temp_buf = GetFINESSEBuffer(n, finesse_num);
1452 for (
int k = 0; k < GetFINESSENwords(n, finesse_num); k++) {
1453 printf(
"%.8x ", temp_buf[ k ]);
1454 if ((k + 1) % 10 == 0) {
1461 "[FATAL] %s ch=%d : ERROR_EVENT : slot %c : B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PreRawCOPPERfromat class. Exiting...\n %s %s %d\n",
1462 hostname, finesse_num,
1463 65 + finesse_num, (
unsigned short)(*buf & 0xFFFF), temp_crc16,
1464 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1465 printf(
"%s", err_buf); fflush(stdout);
1472 int* PreRawCOPPERFormat_v2::PackDetectorBuf(
int* packed_buf_nwords,
1473 int* detector_buf_1st,
int nwords_1st,
1474 int* detector_buf_2nd,
int nwords_2nd,
1475 int* detector_buf_3rd,
int nwords_3rd,
1476 int* detector_buf_4th,
int nwords_4th,
1479 int* packed_buf = NULL;
1481 int poswords_to = 0;
1482 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
1483 int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
1486 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
1488 for (
int i = 0; i < 4; i++) {
1489 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1490 length_nwords += nwords[ i ];
1491 length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER
1492 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1496 packed_buf =
new int[ length_nwords ];
1497 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
1502 tmp_header.SetBuffer(packed_buf);
1504 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
1505 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = 0x7f7f8000
1506 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
1507 | tmp_header.RAWHEADER_NWORDS;
1508 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT)
1510 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
1511 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1512 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
1513 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
1519 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
1521 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
1522 if (nwords[ 0 ] > 0) {
1523 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
1524 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1527 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
1528 if (nwords[ 1 ] > 0) {
1529 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
1530 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1533 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
1534 if (nwords[ 2 ] > 0) {
1535 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
1536 SIZE_B2LHSLB_TRAILER;
1538 poswords_to += tmp_header.GetHdrNwords();
1541 packed_buf[ poswords_to + POS_MAGIC_COPPER_1 ] = COPPER_MAGIC_DRIVER_HEADER;
1542 packed_buf[ poswords_to + POS_MAGIC_COPPER_2 ] = COPPER_MAGIC_FPGA_HEADER;
1543 packed_buf[ poswords_to + POS_EVE_NUM_COPPER ] = rawcpr_info.
eve_num;
1545 int size_b2l_hdrtrl = SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
1546 if (nwords[ 0 ] != 0) packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] = nwords[ 0 ] + size_b2l_hdrtrl;
1547 if (nwords[ 1 ] != 0) packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] = nwords[ 1 ] + size_b2l_hdrtrl;
1548 if (nwords[ 2 ] != 0) packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] = nwords[ 2 ] + size_b2l_hdrtrl;
1549 if (nwords[ 3 ] != 0) packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] = nwords[ 3 ] + size_b2l_hdrtrl;
1551 packed_buf[ poswords_to + POS_DATA_LENGTH ] =
1552 packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] +
1553 packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] +
1554 packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] +
1555 packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] +
1556 (SIZE_COPPER_HEADER - SIZE_COPPER_DRIVER_HEADER) +
1557 (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER);
1559 poswords_to += SIZE_COPPER_HEADER;
1562 for (
int i = 0; i < 4; i++) {
1564 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
1567 packed_buf[ poswords_to + POS_MAGIC_B2LHSLB ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
1568 poswords_to += SIZE_B2LHSLB_HEADER;
1569 int* crc16_start = &(packed_buf[ poswords_to ]);
1573 packed_buf[ poswords_to + POS_TT_CTIME_TYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
1574 unsigned int temp_ctime_type = packed_buf[ poswords_to + POS_TT_CTIME_TYPE ];
1575 packed_buf[ poswords_to + POS_TT_TAG ] = rawcpr_info.
eve_num;
1576 packed_buf[ poswords_to + POS_TT_UTIME ] = rawcpr_info.
tt_utime;
1577 packed_buf[ poswords_to + POS_EXP_RUN ] = (rawcpr_info.
exp_num << tmp_header.EXP_SHIFT) | (rawcpr_info.
run_subrun_num &
1579 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
1580 poswords_to += SIZE_B2LFEE_HEADER;
1583 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
1584 poswords_to += nwords[ i ];
1587 packed_buf[ poswords_to + POS_TT_CTIME_B2LFEE ] = temp_ctime_type;
1590 unsigned short crc16 = CalcCRC16LittleEndian(0xffff, crc16_start, nwords[ i ] + SIZE_B2LFEE_HEADER);
1591 packed_buf[ poswords_to + POS_CHKSUM_B2LFEE ] = ((0xffff & rawcpr_info.
eve_num) << 16) | (crc16 & 0xffff);
1592 poswords_to += SIZE_B2LFEE_TRAILER;
1595 packed_buf[ poswords_to + POS_CHKSUM_B2LHSLB ] = 0xff550000;
1596 poswords_to += SIZE_B2LHSLB_TRAILER;
1601 packed_buf[ poswords_to + POS_MAGIC_COPPER_3 ] = COPPER_MAGIC_FPGA_TRAILER;
1602 packed_buf[ poswords_to + POS_MAGIC_COPPER_4 ] = COPPER_MAGIC_DRIVER_TRAILER;
1603 unsigned int chksum = 0;
1604 for (
int i = tmp_header.GetHdrNwords(); i < poswords_to + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); i++) {
1605 chksum ^= packed_buf[ i ];
1607 packed_buf[ poswords_to + POS_CHKSUM_COPPER ] = chksum;
1608 poswords_to += SIZE_COPPER_TRAILER;
1612 for (
int i = 0; i < poswords_to; i++) {
1613 chksum ^= packed_buf[ i ];
1615 packed_buf[ poswords_to + tmp_trailer.POS_CHKSUM ] = chksum;
1617 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
1618 poswords_to += tmp_trailer.GetTrlNwords();
1620 *packed_buf_nwords = poswords_to;