9 #include <rawdata/dataobjects/PostRawCOPPERFormat_v2.h>
19 PostRawCOPPERFormat_v2::PostRawCOPPERFormat_v2()
23 PostRawCOPPERFormat_v2::~PostRawCOPPERFormat_v2()
28 int PostRawCOPPERFormat_v2::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_v2::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_v2::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) {
90 GetNodeName(n, hostname,
sizeof(hostname));
91 sprintf(err_buf,
"[FATAL] %s ch=%d : ERROR_EVENT : # of words is strange. %d : eve 0x%x exp %d run %d sub %d\n %s %s %d\n",
92 hostname, finesse_num,
94 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
95 __FILE__, __PRETTY_FUNCTION__, __LINE__);
96 printf(
"[DEBUG] %s\n", err_buf);
107 unsigned int PostRawCOPPERFormat_v2::GetB2LFEE32bitEventNumber(
int n)
111 GetNodeName(n, hostname,
sizeof(hostname));
113 "[FATAL] %s ch=%d : ERROR_EVENT : No event # in B2LFEE header. (block %d) Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
116 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
117 __FILE__, __PRETTY_FUNCTION__, __LINE__);
118 printf(
"[DEBUG] %s\n", err_buf);
125 void PostRawCOPPERFormat_v2::CheckData(
int n,
126 unsigned int prev_evenum,
unsigned int* cur_evenum_rawcprhdr,
127 unsigned int prev_copper_ctr,
unsigned int* cur_copper_ctr,
128 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
136 *cur_evenum_rawcprhdr = GetEveNo(n);
137 *cur_exprunsubrun_no = GetExpRunSubrun(n);
139 if (prev_exprunsubrun_no == *cur_exprunsubrun_no) {
140 if ((
unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) {
141 sprintf(err_buf,
"CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n",
142 n, prev_evenum, *cur_evenum_rawcprhdr,
143 __FILE__, __PRETTY_FUNCTION__, __LINE__);
152 tmp_trailer.SetBuffer(GetRawTrlBufPtr(n));
153 unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords());
154 if (tmp_trailer.GetChksum() != xor_chksum) {
156 GetNodeName(n, hostname,
sizeof(hostname));
158 "[FATAL] %s ch=%d : 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",
160 n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum,
161 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
162 __FILE__, __PRETTY_FUNCTION__, __LINE__);
168 printf(
"[DEBUG] %s\n", err_buf);
169 printf(
"[DEBUG] ========== dump a data blcok : block # %d==========\n", n);
170 PrintData(GetBuffer(n), GetBlockNwords(n));
171 printf(
"Print out variables to reduce unused-variables-warnings : %u %u\n", prev_copper_ctr, *cur_copper_ctr);
179 bool PostRawCOPPERFormat_v2::CheckCOPPERMagic(
int n)
183 GetNodeName(n, hostname,
sizeof(hostname));
185 "[FATAL] %s ch=%d : 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",
188 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
189 __FILE__, __PRETTY_FUNCTION__, __LINE__);
190 printf(
"[DEBUG] %s\n", err_buf);
195 void PostRawCOPPERFormat_v2::CheckUtimeCtimeTRGType(
int n)
198 sprintf(err_buf,
"[FATAL] This function is not supported (block %d). Exiting...\n%s %s %d\n",
199 n, __FILE__, __PRETTY_FUNCTION__, __LINE__);
200 printf(
"[DEBUG] %s\n", err_buf);
204 unsigned int PostRawCOPPERFormat_v2::FillTopBlockRawHeader(
unsigned int m_node_id,
unsigned int prev_eve32,
205 unsigned int prev_exprunsubrun_no,
unsigned int* cur_exprunsubrun_no)
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 : %u %u %u %u\n",
212 m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no);
213 printf(
"[DEBUG] %s\n", err_buf);
218 int PostRawCOPPERFormat_v2::CheckB2LHSLBMagicWords(
int* finesse_buf,
int finesse_nwords)
221 sprintf(err_buf,
"[FATAL] This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n",
222 __FILE__, __PRETTY_FUNCTION__, __LINE__);
223 printf(
"Print out variables to reduce unused-variables-warnings : %p %d\n", finesse_buf, finesse_nwords);
224 printf(
"[DEBUG] %s\n", err_buf);
228 int PostRawCOPPERFormat_v2::CheckCRC16(
int n,
int finesse_num)
234 int finesse_nwords = GetFINESSENwords(n, finesse_num);
235 if (finesse_nwords <= 0) {
238 GetNodeName(n, hostname,
sizeof(hostname));
240 "[FATAL] %s ch=%d : 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",
241 hostname, finesse_num,
242 65 + finesse_num, finesse_nwords,
243 GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
244 __FILE__, __PRETTY_FUNCTION__, __LINE__);
245 printf(
"%s", err_buf); fflush(stdout);
249 int* copper_buf = GetBuffer(n);
252 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, &(copper_buf[ tmp_header.POS_TTCTIME_TRGTYPE ]), 1);
253 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EVE_NO ]), 1);
254 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_TTUTIME ]), 1);
255 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EXP_RUN_NO ]), 1);
256 int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER + POS_B2L_CTIME;
257 int pos_nwords = finesse_nwords - (SIZE_B2LHSLB_HEADER + POS_B2L_CTIME + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER);
258 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, buf, pos_nwords);
263 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n,
264 finesse_num) - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_ERRCNT_CRC16) + SIZE_B2LHSLB_TRAILER) ;
266 if (GetEveNo(n) % 100000 == 0) {
267 printf(
"#### PostRawCOPPER : Eve %.8x block %d finesse %d B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), n, finesse_num,
272 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
275 int copper_nwords = copper_buf[ tmp_header.POS_NWORDS ];
276 PrintData(copper_buf, copper_nwords);
278 if (copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] & tmp_header.B2LINK_PACKET_CRC_ERROR) {
284 GetNodeName(n, hostname,
sizeof(hostname));
286 if ((GetNodeID(n) & DETECTOR_MASK) == ARICH_ID) {
288 "[WARNING] %s ch=%d : ARICH : POST B2link event CRC16 error with B2link Packet CRC error. data(%x) calc(%x) fns nwords %d type 0x%.8x : This error is ignored and the error event will be recorded in .sroot file acording to request from ARICH group: slot%c eve 0x%x exp %d run %d sub %d\n%s %s %d\n",
289 hostname, finesse_num,
290 *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
291 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
292 __FILE__, __PRETTY_FUNCTION__, __LINE__);
293 printf(
"%s", err_buf); fflush(stdout);
294 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
297 "[FATAL] %s ch=%d : ERROR_EVENT : 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",
298 hostname, finesse_num,
299 *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
300 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
301 __FILE__, __PRETTY_FUNCTION__, __LINE__);
302 printf(
"%s", err_buf); fflush(stdout);
303 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
304 #ifndef NO_ERROR_STOP
317 GetNodeName(n, hostname,
sizeof(hostname));
319 "[FATAL] %s ch=%d : 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",
320 hostname, finesse_num,
321 *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
322 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
323 __FILE__, __PRETTY_FUNCTION__, __LINE__);
324 printf(
"%s", err_buf); fflush(stdout);
325 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
326 #ifndef NO_ERROR_STOP
331 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
332 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
333 copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_EVENT_CRC_ERROR;
334 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
335 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
344 int* PostRawCOPPERFormat_v2::PackDetectorBuf(
int* packed_buf_nwords,
345 int* detector_buf_1st,
int nwords_1st,
346 int* detector_buf_2nd,
int nwords_2nd,
347 int* detector_buf_3rd,
int nwords_3rd,
348 int* detector_buf_4th,
int nwords_4th,
351 int* packed_buf = NULL;
354 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
355 int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
358 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
360 for (
int i = 0; i < 4; i++) {
361 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
362 length_nwords += nwords[ i ];
363 length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER
364 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
368 packed_buf =
new int[ length_nwords ];
369 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
374 tmp_header.SetBuffer(packed_buf);
376 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
377 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] =
379 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
380 | tmp_header.RAWHEADER_NWORDS;
381 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << 22)
383 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
384 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
385 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
386 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
389 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER;
391 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
392 if (nwords[ 0 ] > 0) {
393 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
394 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
397 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
398 if (nwords[ 1 ] > 0) {
399 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
400 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
403 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
404 if (nwords[ 2 ] > 0) {
405 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
406 SIZE_B2LHSLB_TRAILER;
408 poswords_to += tmp_header.GetHdrNwords();
411 poswords_to += SIZE_COPPER_HEADER;
414 for (
int i = 0; i < 4; i++) {
416 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
419 packed_buf[ poswords_to + POS_B2LHSLB_MAGIC ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
420 poswords_to += SIZE_B2LHSLB_HEADER;
423 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
424 poswords_to += SIZE_B2LFEE_HEADER;
427 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
428 poswords_to += nwords[ i ];
431 unsigned int crc16 = 0;
432 packed_buf[ poswords_to + POS_B2LFEE_ERRCNT_CRC16 ] = crc16 &
434 poswords_to += SIZE_B2LFEE_TRAILER;
437 poswords_to += SIZE_B2LHSLB_TRAILER;
442 poswords_to += SIZE_COPPER_TRAILER;
445 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
446 poswords_to += tmp_trailer.GetTrlNwords();
448 *packed_buf_nwords = poswords_to;
struct to contain header information used by RawCOPPERFormat::Packer()
unsigned int b2l_ctime
32bit unitx time at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user ...
unsigned int eve_num
Run # and subrun # ( 22bit )
unsigned int tt_ctime
Node ID (32bit)
unsigned int tt_utime
27bit clock ticks at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user...
unsigned int node_id
Event Number (32bit)
unsigned int run_subrun_num
Experiment number (10bit)
unsigned int exp_num
Experiment number (10bit)
Abstract base class for different kinds of events.