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 - (
static_cast<int>(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 - (
static_cast<int>(SIZE_B2LHSLB_HEADER) + POS_B2L_CTIME + SIZE_B2LFEE_TRAILER +
258 SIZE_B2LHSLB_TRAILER);
259 temp_crc16 = CalcCRC16LittleEndian(temp_crc16, buf, pos_nwords);
264 buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n,
265 finesse_num) - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_ERRCNT_CRC16) + SIZE_B2LHSLB_TRAILER) ;
267 if (GetEveNo(n) % 100000 == 0) {
268 printf(
"#### PostRawCOPPER : Eve %.8x block %d finesse %d B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), n, finesse_num,
273 if ((
unsigned short)(*buf & 0xFFFF) != temp_crc16) {
276 int copper_nwords = copper_buf[ tmp_header.POS_NWORDS ];
277 PrintData(copper_buf, copper_nwords);
279 if (copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] & tmp_header.B2LINK_PACKET_CRC_ERROR) {
285 GetNodeName(n, hostname,
sizeof(hostname));
287 if ((GetNodeID(n) & DETECTOR_MASK) == ARICH_ID) {
289 "[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",
290 hostname, finesse_num,
291 *buf, temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
292 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
293 __FILE__, __PRETTY_FUNCTION__, __LINE__);
294 printf(
"%s", err_buf); fflush(stdout);
295 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
298 "[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",
299 hostname, finesse_num,
300 *buf, temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
301 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
302 __FILE__, __PRETTY_FUNCTION__, __LINE__);
303 printf(
"%s", err_buf); fflush(stdout);
304 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
305 #ifndef NO_ERROR_STOP
318 GetNodeName(n, hostname,
sizeof(hostname));
320 "[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",
321 hostname, finesse_num,
322 *buf, temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ],
323 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n),
324 __FILE__, __PRETTY_FUNCTION__, __LINE__);
325 printf(
"%s", err_buf); fflush(stdout);
326 PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num));
327 #ifndef NO_ERROR_STOP
332 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
333 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
334 copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_EVENT_CRC_ERROR;
335 copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ]
336 ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ];
345 int* PostRawCOPPERFormat_v2::PackDetectorBuf(
int* packed_buf_nwords,
346 int* detector_buf_1st,
int nwords_1st,
347 int* detector_buf_2nd,
int nwords_2nd,
348 int* detector_buf_3rd,
int nwords_3rd,
349 int* detector_buf_4th,
int nwords_4th,
352 int* packed_buf = NULL;
355 int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th };
356 const int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th };
359 int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords();
361 for (
int i = 0; i < 4; i++) {
362 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
363 length_nwords += nwords[ i ];
364 length_nwords +=
static_cast<int>(SIZE_B2LHSLB_HEADER) + SIZE_B2LFEE_HEADER
365 + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
369 packed_buf =
new int[ length_nwords ];
370 memset(packed_buf, 0,
sizeof(
int) * length_nwords);
375 tmp_header.SetBuffer(packed_buf);
377 packed_buf[ tmp_header.POS_NWORDS ] = length_nwords;
378 packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] =
380 | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK)
381 | tmp_header.RAWHEADER_NWORDS;
382 packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.
exp_num << 22)
384 packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.
eve_num;
385 packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.
tt_ctime & 0x7FFFFFF) << 4;
386 packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.
tt_utime;
387 packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.
node_id;
390 packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] =
static_cast<int>(tmp_header.RAWHEADER_NWORDS) + SIZE_COPPER_HEADER;
392 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ];
393 if (nwords[ 0 ] > 0) {
394 packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] +=
395 nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
398 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ];
399 if (nwords[ 1 ] > 0) {
400 packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] +=
401 nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER;
404 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ];
405 if (nwords[ 2 ] > 0) {
406 packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER +
407 SIZE_B2LHSLB_TRAILER;
409 poswords_to += tmp_header.GetHdrNwords();
412 poswords_to += SIZE_COPPER_HEADER;
415 for (
int i = 0; i < 4; i++) {
417 if (detector_buf[ i ] == NULL || nwords[ i ] <= 0)
continue;
420 packed_buf[ poswords_to + POS_B2LHSLB_MAGIC ] = 0xffaa0000 | (0xffff & rawcpr_info.
eve_num);
421 poswords_to += SIZE_B2LHSLB_HEADER;
424 packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.
b2l_ctime & 0x7FFFFFF) << 4;
425 poswords_to += SIZE_B2LFEE_HEADER;
428 memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*
sizeof(
int));
429 poswords_to += nwords[ i ];
432 unsigned int crc16 = 0;
433 packed_buf[ poswords_to + POS_B2LFEE_ERRCNT_CRC16 ] = crc16 &
435 poswords_to += SIZE_B2LFEE_TRAILER;
438 poswords_to += SIZE_B2LHSLB_TRAILER;
443 poswords_to += SIZE_COPPER_TRAILER;
446 packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER;
447 poswords_to += tmp_trailer.GetTrlNwords();
449 *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.