9 #include <rawdata/dataobjects/PostRawCOPPERFormat_latest.h>
19 PostRawCOPPERFormat_latest::PostRawCOPPERFormat_latest()
23 PostRawCOPPERFormat_latest::~PostRawCOPPERFormat_latest()
28 int PostRawCOPPERFormat_latest::GetDetectorNwords(
int n,
int finesse_num)
31 if (GetFINESSENwords(n, finesse_num) > 0) {
32 nwords = GetFINESSENwords(n, finesse_num)
33 - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER);
39 unsigned int PostRawCOPPERFormat_latest::CalcDriverChkSum(
int n)
42 sprintf(err_buf,
"[FATAL] This function is not supported.(block %d) Exiting...: \n%s %s %d\n",
43 n, __FILE__, __PRETTY_FUNCTION__, __LINE__);
44 printf(
"[DEBUG] %s\n", err_buf);
50 int PostRawCOPPERFormat_latest::GetFINESSENwords(
int n,
int finesse_num)
52 int pos_nwords_0, pos_nwords_1;
54 switch (finesse_num) {
56 pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_1ST_FINESSE;
57 pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE;
58 nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ];
61 pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE;
62 pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE;
63 nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ];
66 pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE;
67 pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE;
68 nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ];
71 pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE;
73 int nwords_1 = GetBlockNwords(n)
74 - SIZE_COPPER_DRIVER_TRAILER
75 - tmp_trailer.GetTrlNwords();
76 nwords = nwords_1 - m_buffer[ pos_nwords_0 ];
81 sprintf(err_buf,
"[FATAL] Invalid finesse # : %s %s %d\n",
82 __FILE__, __PRETTY_FUNCTION__, __LINE__);
83 printf(
"[DEBUG] %s\n", err_buf);
87 if (nwords < 0 || nwords > 1e6) {
89 sprintf(err_buf,
"[FATAL] ERROR_EVENT : # of words is strange. %d : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
91 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
92 __FILE__, __PRETTY_FUNCTION__, __LINE__);
93 printf(
"[DEBUG] %s\n", err_buf);
104 unsigned int PostRawCOPPERFormat_latest::GetB2LFEE32bitEventNumber(
int n)
108 "[FATAL] ERROR_EVENT : No event # in B2LFEE header. (block %d) Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
110 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
111 __FILE__, __PRETTY_FUNCTION__, __LINE__);
112 printf(
"[DEBUG] %s\n", err_buf);
119 void PostRawCOPPERFormat_latest::CheckData(
int n,
120 unsigned int prev_evenum,
unsigned int* cur_evenum_rawcprhdr,
121 unsigned int prev_copper_ctr,
unsigned int* cur_copper_ctr,
122 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
130 *cur_evenum_rawcprhdr = GetEveNo(n);
131 *cur_exprunsubrun_no = GetExpRunSubrun(n);
133 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
134 if ((
unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) {
135 sprintf(err_buf,
"CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n",
136 n, prev_evenum, *cur_evenum_rawcprhdr,
137 __FILE__, __PRETTY_FUNCTION__, __LINE__);
146 tmp_trailer.SetBuffer(GetRawTrlBufPtr(n));
147 unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords());
148 if (tmp_trailer.GetChksum() != xor_chksum) {
150 "[FATAL] ERROR_EVENT : 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",
151 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum,
152 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
153 __FILE__, __PRETTY_FUNCTION__, __LINE__);
159 printf(
"[DEBUG] %s\n", err_buf);
160 printf(
"[DEBUG] ========== dump a data blcok : block # %d==========\n", n);
161 PrintData(GetBuffer(n), GetBlockNwords(n));
162 printf(
"Print out variables to reduce unused-variables-warnings : %u %u\n", prev_copper_ctr, *cur_copper_ctr);
170 bool PostRawCOPPERFormat_latest::CheckCOPPERMagic(
int n)
174 "[FATAL] ERROR_EVENT : No magic word # in COPPER header (block %d). Exiting...: eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
176 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
177 __FILE__, __PRETTY_FUNCTION__, __LINE__);
178 printf(
"[DEBUG] %s\n", err_buf);
183 void PostRawCOPPERFormat_latest::CheckUtimeCtimeTRGType(
int n)
186 sprintf(err_buf,
"[FATAL] This function is not supported (block %d). Exiting...\n%s %s %d\n",
187 n, __FILE__, __PRETTY_FUNCTION__, __LINE__);
188 printf(
"[DEBUG] %s\n", err_buf);
192 unsigned int PostRawCOPPERFormat_latest::FillTopBlockRawHeader(
unsigned int m_node_id,
unsigned int prev_eve32,
193 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
197 sprintf(err_buf,
"[FATAL] This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n",
198 __FILE__, __PRETTY_FUNCTION__, __LINE__);
199 printf(
"Print out variables to reduce unused-variables-warnings : %u %u %u %u\n",
200 m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no);
201 printf(
"[DEBUG] %s\n", err_buf);
206 int PostRawCOPPERFormat_latest::CheckB2LHSLBMagicWords(
int* finesse_buf,
int finesse_nwords)
209 sprintf(err_buf,
"[FATAL] This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n",
210 __FILE__, __PRETTY_FUNCTION__, __LINE__);
211 printf(
"Print out variables to reduce unused-variables-warnings : %p %d\n", finesse_buf, finesse_nwords);
212 printf(
"[DEBUG] %s\n", err_buf);
216 int PostRawCOPPERFormat_latest::CheckCRC16(
int n,
int finesse_num)
222 int finesse_nwords = GetFINESSENwords(n, finesse_num);
223 if (finesse_nwords <= 0) {
226 "[FATAL] ERROR_EVENT : The specified finesse(%c) seems to be empty(nwords = %d). Cannot calculate CRC16. Exiting...: eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
227 65 + finesse_num, finesse_nwords,
228 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
229 __FILE__, __PRETTY_FUNCTION__, __LINE__);
230 printf(
"%s", err_buf); fflush(stdout);
234 int* copper_buf = GetBuffer(n);
237 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, &(copper_buf[ tmp_header.POS_TTCTIME_TRGTYPE ]), 1);
238 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EVE_NO ]), 1);
239 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_TTUTIME ]), 1);
240 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EXP_RUN_NO ]), 1);
241 int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER + POS_B2L_CTIME;
242 int pos_nwords = finesse_nwords - (SIZE_B2LHSLB_HEADER + POS_B2L_CTIME + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER);
243 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, buf, pos_nwords);
248 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n,
249 finesse_num) - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_ERRCNT_CRC16) + SIZE_B2LHSLB_TRAILER) ;
251 if (GetEveNo(n) % 100000 == 0) {
252 printf(
"#### PostRawCOPPER : Eve %.8x block %d finesse %d B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), n, finesse_num,
257 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
260 int copper_nwords = copper_buf[ tmp_header.POS_NWORDS ];
261 PrintData(copper_buf, copper_nwords);
263 if (copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] & tmp_header.B2LINK_PACKET_CRC_ERROR) {
269 "[FATAL] POST B2link event CRC16 error with B2link Packet CRC error. data(%x) calc(%x) fns nwords %d type 0x%.8x : slot%c eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
270 *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
271 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
272 __FILE__, __PRETTY_FUNCTION__, __LINE__);
273 printf(
"%s", err_buf); fflush(stdout);
274 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
275 #ifndef NO_ERROR_STOP
284 "[FATAL] ERROR_EVENT : POST B2link event CRC16 error without B2link Packet CRC error. data(%x) calc(%x) fns nwords %d type 0x%.8x: slot%c eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
285 *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
286 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
287 __FILE__, __PRETTY_FUNCTION__, __LINE__);
288 printf(
"%s", err_buf); fflush(stdout);
289 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
290 #ifndef NO_ERROR_STOP
295 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
296 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
297 copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_EVENT_CRC_ERROR;
298 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
299 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
308 int* PostRawCOPPERFormat_latest::PackDetectorBuf(
int* packed_buf_nwords,
309 int* detector_buf_1st,
int nwords_1st,
310 int* detector_buf_2nd,
int nwords_2nd,
311 int* detector_buf_3rd,
int nwords_3rd,
312 int* detector_buf_4th,
int nwords_4th,
315 int* packed_buf = NULL;
318 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
319 int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
322 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
324 for (
int i = 0; i < 4; i++) {
325 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
326 length_nwords += nwords[ i ];
327 length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER
328 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
332 packed_buf =
new int[ length_nwords ];
333 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
338 tmp_header.SetBuffer(packed_buf);
340 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
341 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] =
343 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
344 | tmp_header.RAWHEADER_NWORDS;
345 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << 22)
347 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
348 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
349 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
350 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
353 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
355 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
356 if (nwords[ 0 ] > 0) {
357 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
358 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
361 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
362 if (nwords[ 1 ] > 0) {
363 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
364 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
367 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
368 if (nwords[ 2 ] > 0) {
369 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
370 SIZE_B2LHSLB_TRAILER;
372 poswords_to += tmp_header.GetHdrNwords();
375 poswords_to += SIZE_COPPER_HEADER;
378 for (
int i = 0; i < 4; i++) {
380 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
383 packed_buf[ poswords_to + POS_B2LHSLB_MAGIC ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
384 poswords_to += SIZE_B2LHSLB_HEADER;
387 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
388 poswords_to += SIZE_B2LFEE_HEADER;
391 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
392 poswords_to += nwords[ i ];
395 unsigned int crc16 = 0;
396 packed_buf[ poswords_to + POS_B2LFEE_ERRCNT_CRC16 ] = crc16 &
398 poswords_to += SIZE_B2LFEE_TRAILER;
401 poswords_to += SIZE_B2LHSLB_TRAILER;
406 poswords_to += SIZE_COPPER_TRAILER;
409 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
410 poswords_to += tmp_trailer.GetTrlNwords();
412 *packed_buf_nwords = poswords_to;