Belle II Software development
DeSerializerPC.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <daq/rawdata/modules/DeSerializerPC.h>
10#include <daq/dataobjects/SendHeader.h>
11#include <daq/dataobjects/SendTrailer.h>
12#include <rawdata/dataobjects/RawTLU.h>
13
14#include <netdb.h>
15#include <netinet/tcp.h>
16
17//#define MAXEVTSIZE 400000000
18#define CHECKEVT 5000
19
20
21
22#define USE_DESERIALIZER_PREPC
23
24//#define DEBUG
25//#define NO_DATA_CHECK
26//#define DUMHSLB
27
28using namespace std;
29using namespace Belle2;
30
31//-----------------------------------------------------------------
32// Register the Module
33//-----------------------------------------------------------------
34REG_MODULE(DeSerializerPC);
35
36//-----------------------------------------------------------------
37// Implementation
38//-----------------------------------------------------------------
39
40#ifndef REDUCED_RAWCOPPER
41#ifdef USE_DESERIALIZER_PREPC
42//compile error
43#endif
44#endif
45
47{
48 //Set module properties
49 setDescription("Encode DataStore into RingBuffer");
50 // setPropertyFlags(c_Input | c_ParallelProcessingCertified);
51 addParam("NumConn", m_num_connections, "Number of Connections", 0);
52 addParam("HostNameFrom", m_hostname_from, "Hostnames of data sources");
53 addParam("PortFrom", m_port_from, "port numbers of data sources");
54
55
56 B2INFO("DeSerializerPC: Constructor done.");
57
58
59}
60
61
62
63DeSerializerPCModule::~DeSerializerPCModule()
64{
65
66}
67
68
70{
71 B2INFO("DeSerializerPC: initialize() started.");
72
73 // Set m_socket
74 if (m_num_connections > (int)m_hostname_from.size() || m_num_connections > (int)m_port_from.size()) {
75 B2FATAL("[FATAL] Hostname or port# is not specified for all connections. Please check a python script. Exiting... \n");
76 exit(1);
77 }
78 for (int i = 0; i < m_num_connections; i++) {
79 m_socket.push_back(-1);
80 }
81
82 // allocate buffer
83 for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) {
84 m_bufary[i] = new int[ BUF_SIZE_WORD ];
85 }
86 m_buffer = new int[ BUF_SIZE_WORD ];
87
88
89 // initialize buffer
90 for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) {
91 memset(m_bufary[i], 0, BUF_SIZE_WORD * sizeof(int));
92 }
93
94 // Initialize EvtMetaData
95 m_eventMetaDataPtr.registerInDataStore();
96
97 raw_datablkarray.registerInDataStore();
98 rawcprarray.registerInDataStore();
99 raw_ftswarray.registerInDataStore();
100
101
102 // Initialize Array of RawCOPPER
103
104 if (m_dump_fname.size() > 0) {
106 }
107
108 // Initialize arrays for time monitor
109 memset(time_array0, 0, sizeof(time_array0));
110 memset(time_array1, 0, sizeof(time_array1));
111 memset(time_array2, 0, sizeof(time_array2));
112
113 clearNumUsedBuf();
114
115 // Shared memory
116 if (m_shmflag > 0) {
117 if (m_nodename.size() == 0 || m_nodeid < 0) {
118 m_shmflag = 0;
119 } else {
121 g_status.reportReady();
122 }
123 }
124
125 event_diff = 0;
126
127 m_prev_copper_ctr = 0xFFFFFFFF;
128 m_prev_evenum = 0xFFFFFFFF;
129
130#ifdef NONSTOP
131 openRunPauseNshm();
132#endif
133
134 B2INFO("DeSerializerPC: initialize() done.");
135}
136
137
138int DeSerializerPCModule::recvFD(int sock, char* buf, int data_size_byte, int flag)
139{
140 int n = 0;
141 while (1) {
142 int read_size = 0;
143 if ((read_size = recv(sock, static_cast<char*>(buf) + n, data_size_byte - n, flag)) < 0) {
144 if (errno == EINTR) {
145 continue;
146 } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
147 // No data received within SO_RCVTIMEO
148#ifdef NONSTOP
149 string err_str;
150 callCheckRunPause(err_str);
151#endif
152 continue;
153 } else {
154 char err_buf[500];
155 sprintf(err_buf, "[WARNING] recv() returned error; ret = %d. : %s %s %d",
156 read_size, __FILE__, __PRETTY_FUNCTION__, __LINE__);
157#ifdef NONSTOP
158 g_run_error = 1;
159 B2ERROR(err_buf);
160 string err_str = "RUN_ERROR";
161 throw (err_str);
162#endif
163 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
164 exit(-1);
165 }
166 } else if (read_size == 0) {
167 char err_buf[500];
168 sprintf(err_buf, "[WARNING] Connection is closed by peer(%s).: %s %s %d",
169 strerror(errno), __FILE__, __PRETTY_FUNCTION__, __LINE__);
170#ifdef NONSTOP
171 g_run_error = 1;
172 B2ERROR(err_buf);
173 string err_str = "RUN_ERROR";
174 throw (err_str);
175#endif
176 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
177 exit(-1);
178 } else {
179 n += read_size;
180 if (n == data_size_byte)break;
181 }
182 }
183 return n;
184}
185
187{
188 for (int i = 0; i < m_num_connections; i++) {
189 if (m_socket[ i ] >= 0) continue; // Already have an established socket
190 //
191 // Connect to a downstream node
192 //
193 struct sockaddr_in socPC;
194 socPC.sin_family = AF_INET;
195
196 struct hostent* host;
197 host = gethostbyname(m_hostname_from[ i ].c_str());
198 if (host == NULL) {
199 char err_buf[100];
200 sprintf(err_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...", m_hostname_from[ i ].c_str(),
201 strerror(errno));
202 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
203 sleep(1234567);
204 exit(1);
205 }
206 socPC.sin_addr.s_addr = *reinterpret_cast<unsigned int*>(host->h_addr_list[0]);
207 socPC.sin_port = htons(m_port_from[ i ]);
208 int sd = socket(PF_INET, SOCK_STREAM, 0);
209
210 int val1 = 0;
211 setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &val1, sizeof(val1));
212
213 struct timeval timeout;
214 timeout.tv_sec = 1;
215 timeout.tv_usec = 0;
216 setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, (socklen_t)sizeof(timeout));
217
218 printf("[DEBUG] Connecting to %s port %d ...\n", m_hostname_from[ i ].c_str(), m_port_from[ i ]);
219 while (1) {
220 if (connect(sd, reinterpret_cast<struct sockaddr*>((&socPC)), sizeof(socPC)) < 0) {
221 perror("Failed to connect. Retrying...");
222 usleep(500000);
223 } else {
224 printf("[DEBUG] Done\n");
225 break;
226 }
227 }
228
229 // m_socket.push_back(sd);
230 m_socket[ i ] = sd;
231
232 // check socket paramters
233 int val, len;
234 len = sizeof(val);
235 getsockopt(m_socket[ i ], SOL_SOCKET, SO_RCVBUF, &val, reinterpret_cast<socklen_t*>(&len));
236#ifdef DEBUG
237 printf("[DEBUG] SO_RCVBUF %d\n", val);
238#endif
239 getsockopt(m_socket[ i ], SOL_SOCKET, SO_SNDBUF, &val, reinterpret_cast<socklen_t*>(&len));
240#ifdef DEBUG
241 printf("[DEBUG] SO_SNDBUF %d\n", val);
242#endif
243 getsockopt(m_socket[ i ], IPPROTO_TCP, TCP_MAXSEG, &val, reinterpret_cast<socklen_t*>(&len));
244#ifdef DEBUG
245 printf("[DEBUG] TCP_MAXSEG %d\n", val);
246#endif
247 getsockopt(m_socket[ i ], IPPROTO_TCP, TCP_NODELAY, &val, reinterpret_cast<socklen_t*>(&len));
248#ifdef DEBUG
249 printf("[DEBUG] TCP_NODELAY %d\n", val);
250#endif
251 if (g_status.isAvailable()) {
252 sockaddr_in sa;
253 memset(&sa, 0, sizeof(sockaddr_in));
254 socklen_t sa_len = sizeof(sa);
255 if (getsockname(m_socket[i], reinterpret_cast<struct sockaddr*>(&sa), static_cast<socklen_t*>(&sa_len)) == 0) {
256 g_status.setInputPort(ntohs(sa.sin_port));
257 g_status.setInputAddress(sa.sin_addr.s_addr);
258 }
259 }
260
261 }
262 printf("[DEBUG] Initialization finished\n");
263 return 0;
264}
265
266
267
268int* DeSerializerPCModule::recvData(int* delete_flag, int* total_buf_nwords, int* num_events_in_sendblock,
269 int* num_nodes_in_sendblock)
270{
271
272 int* temp_buf = NULL; // buffer for data-body
273 int flag = 0;
274
275 vector <int> each_buf_nwords;
276 each_buf_nwords.clear();
277 vector <int> each_buf_nodes;
278 each_buf_nodes.clear();
279 vector <int> each_buf_events;
280 each_buf_events.clear();
281
282 *total_buf_nwords = 0;
283 *num_nodes_in_sendblock = 0;
284 *num_events_in_sendblock = 0;
285
286 //
287 // Read Header and obtain data size
288 //
289 int send_hdr_buf[ SendHeader::SENDHDR_NWORDS ];
290
291 // Read header
292 for (int i = 0; i < (int)(m_socket.size()); i++) {
293
294 recvFD(m_socket[ i ], reinterpret_cast<char*>(send_hdr_buf), sizeof(int)*SendHeader::SENDHDR_NWORDS, flag);
295
296 SendHeader send_hdr;
297 send_hdr.SetBuffer(send_hdr_buf);
298
299 int temp_num_events = send_hdr.GetNumEventsinPacket();
300 int temp_num_nodes = send_hdr.GetNumNodesinPacket();
301
302
303
304 if (i == 0) {
305 *num_events_in_sendblock = temp_num_events;
306 } else if (*num_events_in_sendblock != temp_num_events) {
307
308#ifndef NO_DATA_CHECK
309 printf("[DEBUG] *******HDR**********\n");
310 printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS);
311 char err_buf[500];
312 sprintf(err_buf,
313 "[FATAL] CORRUPTED DATA: Different # of events or nodes in SendBlocks( # of eve : %d(socket 0) %d(socket %d), # of nodes: %d(socket 0) %d(socket %d). Exiting...\n",
314 *num_events_in_sendblock, temp_num_events, i, *num_nodes_in_sendblock, temp_num_nodes, i);
315 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
316 sleep(1234567);
317 exit(1);
318#endif
319 }
320
321 *num_nodes_in_sendblock += temp_num_nodes;
322
323 int rawblk_nwords = send_hdr.GetTotalNwords()
324 - SendHeader::SENDHDR_NWORDS
325 - SendTrailer::SENDTRL_NWORDS;
326 *total_buf_nwords += rawblk_nwords;
327
328 //
329 // Data size check1
330 //
331 if (rawblk_nwords > (int)(2.5e6) || rawblk_nwords <= 0) {
332 printf("[DEBUG] *******HDR**********\n");
333 // printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS);
334 printData(send_hdr_buf, 100);
335 printASCIIData(send_hdr_buf, 100);
336 char err_buf[500];
337 sprintf(err_buf, "[FATAL] CORRUPTED DATA: Too large event : Header %d %d %d %d\n", i, temp_num_events, temp_num_nodes,
338 send_hdr.GetTotalNwords());
339 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
340 sleep(123456);
341 exit(1);
342
343 }
344
345 each_buf_nwords.push_back(rawblk_nwords);
346 each_buf_events.push_back(temp_num_events);
347 each_buf_nodes.push_back(temp_num_nodes);
348
349 }
350
351
352 temp_buf = getNewBuffer(*total_buf_nwords, delete_flag); // this include only data body
353 //
354 // Read body
355 //
356 int total_recvd_byte = 0;
357 for (int i = 0; i < (int)(m_socket.size()); i++) {
358
359 try {
360 total_recvd_byte += recvFD(m_socket[ i ], reinterpret_cast<char*>(temp_buf) + total_recvd_byte,
361 each_buf_nwords[ i ] * sizeof(int), flag);
362 } catch (const string& err_str) {
363 if (*delete_flag) {
364 B2WARNING("Delete buffer before going to Run-pause state");
365 delete temp_buf;
366 }
367 throw (err_str);
368 }
369
370 //
371 // Data length check
372 //
373 int temp_length = 0;
374 for (int j = 0; j < each_buf_nodes[ i ] * each_buf_events[ i ]; j++) {
375 int this_length = *(reinterpret_cast<int*>((reinterpret_cast<char*>(temp_buf) + total_recvd_byte - each_buf_nwords[ i ] * sizeof(
376 int) + temp_length)));
377 temp_length += this_length * sizeof(int);
378 }
379 if (temp_length != (int)(each_buf_nwords[ i ] * sizeof(int))) {
380 printf("[DEBUG]*******SENDHDR*********** \n");
381 printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS);
382 printf("[DEBUG]*******BODY***********\n ");
383 printData(temp_buf, (int)(total_recvd_byte / sizeof(int)));
384 char err_buf[500];
385 sprintf(err_buf, "[FATAL] CORRUPTED DATA: Length written on SendHeader(%d) is invalid. Actual data size is %d. Exting...",
386 (int)(*total_buf_nwords * sizeof(int)), temp_length);
387 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
388 sleep(1234567);
389 exit(-1);
390 }
391
392 }
393
394 if ((int)(*total_buf_nwords * sizeof(int)) != total_recvd_byte) {
395 char err_buf[500];
396 sprintf(err_buf, "[FATAL] CORRUPTED DATA: Received data size (%d byte) is not same as expected one (%d) from Sendheader. Exting...",
397 total_recvd_byte, (int)(*total_buf_nwords * sizeof(int)));
398 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
399 sleep(1234567);
400 exit(-1);
401 }
402
403 // Read Traeiler
404 int send_trl_buf[(unsigned int)(SendTrailer::SENDTRL_NWORDS) ];
405 for (int i = 0; i < (int)(m_socket.size()); i++) {
406 try {
407 recvFD(m_socket[ i ], reinterpret_cast<char*>(send_trl_buf), SendTrailer::SENDTRL_NWORDS * sizeof(int), flag);
408 } catch (const string& err_str) {
409 if (*delete_flag) {
410 B2WARNING("Delete buffer before going to Run-pause state");
411 delete temp_buf;
412 }
413 throw (err_str);
414 }
415 }
416
417 return temp_buf;
418}
419
420
421void DeSerializerPCModule::setRecvdBuffer(RawDataBlock* temp_raw_datablk, int* delete_flag)
422{
423 //
424 // Get data from socket
425 //
426 int total_buf_nwords = 0 ;
427 int num_events_in_sendblock = 0;
428 int num_nodes_in_sendblock = 0;
429
430 if (m_start_flag == 0) B2INFO("DeSerializerPC: Reading the 1st packet from eb0...");
431
432 int* temp_buf = recvData(delete_flag, &total_buf_nwords, &num_events_in_sendblock,
433 &num_nodes_in_sendblock);
434 if (m_start_flag == 0) {
435 B2INFO("DeSerializerPC: Done. the size of the 1st packet " << total_buf_nwords << " words");
436 m_start_flag = 1;
437 }
438 m_totbytes += total_buf_nwords * sizeof(int);
439
440 // Fixed for glibc error at Jan. 2017, reported in "Re: data taking with the new firmware".
441 // for temp_raw_datablk, delete_flag should be 0. raw_datablk will take care of deleting buffer
442 // temp_raw_datablk->SetBuffer((int*)temp_buf, total_buf_nwords, *delete_flag,
443 temp_raw_datablk->SetBuffer(static_cast<int*>(temp_buf), total_buf_nwords, 0,
444
445 num_events_in_sendblock, num_nodes_in_sendblock);
446
447 //
448 // check even # and node # in one Sendblock
449 //
450 int num_entries = temp_raw_datablk->GetNumEntries();
451 if (num_entries != num_events_in_sendblock * num_nodes_in_sendblock) {
452 char err_buf[500];
453 sprintf(err_buf,
454 "[FATAL] CORRUPTED DATA: Inconsistent SendHeader value. # of nodes(%d) times # of events(%d) differs from # of entries(%d). Exiting...",
455 num_nodes_in_sendblock, num_events_in_sendblock, num_entries);
456 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
457 sleep(1234567);
458 exit(-1);
459 }
460 return;
461
462}
463
464
465
466void DeSerializerPCModule::checkData(RawDataBlock* raw_datablk, unsigned int* exp_copper_0, unsigned int* run_copper_0,
467 unsigned int* subrun_copper_0, unsigned int* eve_copper_0, unsigned int* error_bit_flag)
468{
469
470 // int data_size_copper_0 = -1;
471 // int data_size_copper_1 = -1;
472
473 //
474 // Data check
475 //
476 int* temp_buf = raw_datablk->GetBuffer(0);
477 int cpr_num = 0;
478 unsigned int cur_evenum = 0, cur_copper_ctr = 0;
479 unsigned int eve_array[32]; // # of noeds is less than 17
480 unsigned int utime_array[32];// # of noeds is less than 17
481 unsigned int ctime_type_array[32];// # of noeds is less than 17
482
483#ifdef DUMHSLB
484 unsigned int exp_run_ftsw = 0, ctime_trgtype_ftsw = 0, utime_ftsw = 0;
485#endif
486
487 for (int k = 0; k < raw_datablk->GetNumEvents(); k++) {
488 memset(eve_array, 0, sizeof(eve_array));
489 memset(utime_array, 0, sizeof(utime_array));
490 memset(ctime_type_array, 0, sizeof(ctime_type_array));
491
492 int num_nodes_in_sendblock = raw_datablk->GetNumNodes();
493 for (int l = 0; l < num_nodes_in_sendblock; l++) {
494 int entry_id = l + k * num_nodes_in_sendblock;
495 //
496 // RawFTSW
497 //
498 if (raw_datablk->CheckFTSWID(entry_id)) {
499 RawFTSW* temp_rawftsw = new RawFTSW;
500 int block_id = 0;
501 temp_rawftsw->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
502 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
503 if (temp_rawftsw->GetEveNo(block_id) < 10) {
504 printf("[DEBUG] ######FTSW#########\n");
505 printData(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id));
506 }
507
508#ifdef DUMHSLB
509 exp_run_ftsw = temp_rawftsw->GetExpRunSubrun(block_id);
510 ctime_trgtype_ftsw = temp_rawftsw->GetTTCtimeTRGType(block_id);
511 utime_ftsw = temp_rawftsw->GetTTUtime(block_id);
512#endif
513
514
515#ifndef NO_DATA_CHECK
516 try {
517 temp_rawftsw->CheckData(0, m_prev_evenum, &cur_evenum, m_prev_exprunsubrun_no, &m_exprunsubrun_no);
518 eve_array[ entry_id ] = cur_evenum;
519 } catch (const string& err_str) {
520 print_err.PrintError(m_shmflag, &g_status, err_str);
521 exit(1);
522 }
523#endif
524 utime_array[ entry_id ] = temp_rawftsw->GetTTUtime(block_id);
525 ctime_type_array[ entry_id ] = temp_rawftsw->GetTTCtimeTRGType(block_id);
526 delete temp_rawftsw;
527
528 //
529 // RawTLU
530 //
531 } else if (raw_datablk->CheckTLUID(entry_id)) {
532
533 RawTLU* temp_rawtlu = new RawTLU;
534 temp_rawtlu->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
535 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
536 if (temp_rawtlu->GetEveNo(0) < 10
537 ) {
538 printf("[DEBUG] ######TLU#########\n");
539 printData(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id));
540 }
541
542#ifndef NO_DATA_CHECK
543 try {
544 temp_rawtlu->CheckData(0, m_prev_evenum, &cur_evenum);
545 eve_array[ entry_id ] = cur_evenum;
546 } catch (const string& err_str) {
547 print_err.PrintError(m_shmflag, &g_status, err_str);
548 exit(1);
549 }
550#endif
551 delete temp_rawtlu;
552 } else {
553
554 //
555 // RawCOPPER
556 //
557
558
559 RawCOPPER* temp_rawcopper = new RawCOPPER;
560 temp_rawcopper->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
561 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
562
563#ifdef DUMHSLB
564 int block_id = 0;
565 (temp_rawcopper->GetBuffer(block_id))[ RawHeader_latest::POS_EXP_RUN_NO ] = exp_run_ftsw;
566 (temp_rawcopper->GetBuffer(block_id))[ RawHeader_latest::POS_TTCTIME_TRGTYPE ] = ctime_trgtype_ftsw;
567 (temp_rawcopper->GetBuffer(block_id))[ RawHeader_latest::POS_TTUTIME ] = utime_ftsw;
568#endif
569
570#ifndef NO_DATA_CHECK
571 try {
572
573 temp_rawcopper->CheckData(0, m_prev_evenum, &cur_evenum,
574 m_prev_copper_ctr, &cur_copper_ctr,
576 eve_array[ entry_id ] = cur_evenum;
577 } catch (const string& err_str) {
578 temp_rawcopper->PrintData(temp_rawcopper->GetWholeBuffer(), temp_rawcopper->TotalBufNwords());
579 print_err.PrintError(m_shmflag, &g_status, err_str);
580 exit(1);
581 }
582#endif
583
584 utime_array[ entry_id ] = temp_rawcopper->GetTTUtime(0);
585 ctime_type_array[ entry_id ] = temp_rawcopper->GetTTCtimeTRGType(0);
586
587 if (cpr_num == 0) {
588 // data_size_copper_0 = raw_datablk->GetBlockNwords(entry_id);
589 // *eve_copper_0 = (raw_datablk->GetBuffer(entry_id))[ 3 ];
590 *eve_copper_0 = temp_rawcopper->GetEveNo(0);
591 *exp_copper_0 = temp_rawcopper->GetExpNo(0);
592 *run_copper_0 = temp_rawcopper->GetRunNo(0);
593 *subrun_copper_0 = temp_rawcopper->GetSubRunNo(0);
594 } else if (cpr_num == 1) {
595 // data_size_copper_1 = raw_datablk->GetBlockNwords(entry_id);
596 }
597 cpr_num++;
598
599 // Check Error bit flag
600 *error_bit_flag |= temp_rawcopper->GetErrorBitFlag(0);
601
602 delete temp_rawcopper;
603 }
604 }
605
606#ifndef NO_DATA_CHECK
607 // event #, ctime, utime over nodes
608 for (int l = 1; l < num_nodes_in_sendblock; l++) {
609 if (eve_array[ 0 ] != eve_array[ l ]) {
610// if (eve_array[ 0 ] != eve_array[ l ] ||
611// utime_array[ 0 ] != utime_array[ l ] ||
612// ctime_type_array[ 0 ] != ctime_type_array[ l ]) {
613 char err_buf[500];
614 for (int m = 0; m < num_nodes_in_sendblock; m++) {
615 printf("[DEBUG] node %d eve # %u utime %x ctime %x\n",
616 m, eve_array[ m ], utime_array[ m ], ctime_type_array[ m ]);
617 }
618 sprintf(err_buf, "[FATAL] CORRUPTED DATA: Event or Time record mismatch. Exiting...");
619 print_err.PrintError(m_shmflag, &g_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
620 sleep(1234567);
621 exit(-1);
622 }
623 }
624#endif
625
626 // Event # monitor in runchange
628 printf("[DEBUG] ##############################################\n");
629 for (int m = 0; m < raw_datablk->GetNumEntries(); m++) {
630 printf("[DEBUG] %d eve %u prev %u\n", m, eve_array[ m ], m_prev_evenum);
631 }
632 printf("[DEBUG] ##############################################\n");
633 fflush(stderr);
634 }
635 m_prev_evenum = cur_evenum;
636 m_prev_copper_ctr = cur_copper_ctr;
638 }
639 return;
640}
641
642#ifdef NONSTOP
643void DeSerializerPCModule::waitResume()
644{
645 while (true) {
646#ifdef NONSTOP_DEBUG
647 printf("\033[31m");
648 printf("###########(Ser) Waiting for Resume ###############\n");
649 fflush(stdout);
650 printf("\033[0m");
651#endif
652 if (checkRunRecovery()) {
653 g_run_pause = 0;
654 for (int i = 0; i < m_num_connections; i++) {
655 if (CheckConnection(m_socket[ i ]) < 0) m_socket[ i ] = -1;
656 }
657 resumeRun();
658 break;
659 }
660 sleep(1);
661 }
662 return;
663
664}
665#endif
666
667void DeSerializerPCModule::setErrorFlag(unsigned int error_flag, StoreObjPtr<EventMetaData> evtmetadata)
668{
669 // RawHeader_latest raw_hdr;
670 int error_set = 0;
671 if (error_flag & RawHeader_latest::B2LINK_PACKET_CRC_ERROR) {
672 evtmetadata->addErrorFlag(EventMetaData::c_B2LinkPacketCRCError);
673 error_set = 1;
674 }
675 if (error_flag & RawHeader_latest::B2LINK_EVENT_CRC_ERROR) {
676 evtmetadata->addErrorFlag(EventMetaData::c_B2LinkEventCRCError);
677 error_set = 1;
678 }
679 if (error_set) B2INFO("Raw2Ds: Error flag was set in EventMetaData.");
680}
681
682
684{
685
686 // For data check
687 unsigned int exp_copper_0 = 0;
688 unsigned int run_copper_0 = 0;
689 unsigned int subrun_copper_0 = 0;
690 unsigned int eve_copper_0 = 0;
691 unsigned int error_bit_flag = 0;
692
693 clearNumUsedBuf();
694
695 if (m_start_flag == 0) {
696 //
697 // Connect to eb0: This should be here because we want Serializer first to accept connection from eb1tx
698 //
699 Connect();
700 if (g_status.isAvailable()) {
701 B2INFO("DeSerializerPC: Waiting for Start...\n");
702 g_status.reportRunning();
703 }
704 m_start_time = getTimeSec();
705 n_basf2evt = 0;
706 }
707
708
709#ifdef NONSTOP
710 if (g_run_pause > 0 || g_run_error > 0) {
711 if (g_run_pause == 0) {
712 while (true) {
713 if (checkRunPause()) break;
714#ifdef NONSTOP_DEBUG
715 printf("\033[31m");
716 printf("###########(DeserializerPC) Waiting for Runpause() ###############\n");
717 fflush(stdout);
718 printf("\033[0m");
719#endif
720 sleep(1);
721 }
722 }
723 waitResume();
724 m_eventMetaDataPtr.create(); // Otherwise endRun() is called.
725 return;
726 }
727#endif
728
729 // Make rawdatablk array
730
731
732 //
733 // Main loop
734 //
735 const int* buf_rc = NULL;
736 for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) {
737 eve_copper_0 = 0;
738 //
739 // Set buffer to the RawData class stored in DataStore
740 //
741 int delete_flag = 0;
742 RawDataBlock temp_rawdatablk;
743 try {
744 setRecvdBuffer(&temp_rawdatablk, &delete_flag);
745 checkData(&temp_rawdatablk, &exp_copper_0, &run_copper_0, &subrun_copper_0, &eve_copper_0, &error_bit_flag);
746 } catch (const string& err_str) {
747#ifdef NONSTOP
748 // Update EventMetaData otherwise basf2 stops.
749 if (err_str == "RUN_PAUSE" || err_str == "RUN_ERROR") {
750 m_eventMetaDataPtr.create();
751 return;
752 }
753#endif
754 print_err.PrintError(const_cast<char*>(err_str.c_str()), __FILE__, __PRETTY_FUNCTION__, __LINE__);
755 exit(1);
756 }
757
758 RawDataBlock* raw_datablk = raw_datablkarray.appendNew();
759 raw_datablk->SetBuffer(static_cast<int*>(temp_rawdatablk.GetWholeBuffer()), temp_rawdatablk.TotalBufNwords(), delete_flag,
760 temp_rawdatablk.GetNumEvents(), temp_rawdatablk.GetNumNodes());
761 buf_rc = temp_rawdatablk.GetWholeBuffer();
762 }
763 if (buf_rc != NULL) {
764 g_status.copyEventHeader(buf_rc);
765 }
766
767 //
768 // Update EventMetaData
769 //
770 m_eventMetaDataPtr.create();
771 m_eventMetaDataPtr->setExperiment(exp_copper_0);
772 m_eventMetaDataPtr->setRun(run_copper_0);
773 m_eventMetaDataPtr->setSubrun(subrun_copper_0);
774 m_eventMetaDataPtr->setEvent(eve_copper_0);
775
776 setErrorFlag(error_bit_flag, m_eventMetaDataPtr);
777 if (error_bit_flag != 0) {
778 m_eventMetaDataPtr->addErrorFlag(EventMetaData::c_B2LinkEventCRCError);
779 printf("[ERROR] error bit was detected. exp %u run %u eve %u count = %u\n",
780 exp_copper_0, run_copper_0, eve_copper_0, error_bit_flag);
781 }
782
783
784 //
785 // Run stop via NSM (Already obsolete. Need to ask Konno-san about implementation)
786 //
787 // if (m_shmflag != 0) {
788 // if (n_basf2evt % 10 == 0) {
789 // if (g_status.isStopped()) {
790 // printf("[DEBUG] [INFO] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n", max_nevt , max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time);
791 // m_eventMetaDataPtr->setEndOfData();
792 // }
793 // }
794 // }
795
796 //
797 // Monitor
798 //
799 if (max_nevt >= 0 || max_seconds >= 0.) {
800 if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC >= max_nevt && max_nevt > 0)
801 || (getTimeSec() - m_start_time > max_seconds && max_seconds > 0.)) {
802 printf("[DEBUG] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n",
803 max_nevt, max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time);
804 m_eventMetaDataPtr->setEndOfData();
805 }
806 }
807
808 if (n_basf2evt % 20000 == 0 || n_basf2evt < 10) {
809 RateMonitor(eve_copper_0, subrun_copper_0, run_copper_0);
810 }
811 n_basf2evt++;
812 if (g_status.isAvailable()) {
813 g_status.setInputNBytes(m_totbytes);
814 g_status.setInputCount(n_basf2evt);
815 }
816
817 return;
818}
819
820
void RateMonitor(unsigned int nevt, int subrun=-1, int run=-1)
monitor rate
unsigned int m_prev_exprunsubrun_no
run no.
std::string m_nodename
Node name.
int BUF_SIZE_WORD
size of buffer for one event (word)
static RunInfoBuffer g_status
buffer class to communicate with NSM client
int n_basf2evt
No. of sent events.
DeSerializerModule()
Constructor / Destructor.
CprErrorMessage print_err
wrapper for B2LOG system
virtual void printData(int *buf, int nwords)
dump error data
virtual int * getNewBuffer(int nwords, int *delete_flag)
Getbuffer.
std::string m_dump_fname
dump filename
int m_shmflag
Use shared memory.
int * m_bufary[NUM_PREALLOC_BUF]
buffer
unsigned int m_exprunsubrun_no
run no.
virtual void printASCIIData(int *buf, int nwords)
dump error data
virtual void openOutputFile()
Module functions to be called from main process.
double max_seconds
time to stop a run
int m_nodeid
Node(PC or COPPER) ID.
static double getTimeSec()
store time info.
std::vector< int > m_socket
Reciever Socket.
virtual int * recvData(int *delete_flag, int *total_m_size_word, int *num_events_in_sendblock, int *num_nodes_in_sendblock)
receive data
void initialize() override
Module functions to be called from main process.
void event() override
Module functions to be called from event process.
std::vector< int > m_port_from
port # to connect data sources
virtual int Connect()
Accept connection.
virtual int recvFD(int fd, char *buf, int data_size_byte, int flag)
receive data
virtual void setRecvdBuffer(RawDataBlock *raw_datablk, int *delete_flag)
attach buffer to RawDataBlock
DeSerializerPCModule()
Constructor / Destructor.
virtual void checkData(RawDataBlock *raw_datablk, unsigned int *exp_copper_0, unsigned int *run_copper_0, unsigned int *subrun_copper_0, unsigned int *eve_copper_0, unsigned int *error_bit_flag)
check data contents
std::vector< std::string > m_hostname_from
hostname of upstream Data Sources
@ c_B2LinkPacketCRCError
Belle2link CRC error is detected in the event.
@ c_B2LinkEventCRCError
HSLB_COPPER CRC error is detected in the event.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition RawCOPPER.h:52
void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17
set buffer ( delete_flag : m_buffer is freed( = 0 )/ not freed( = 1 ) in Destructor )
Definition RawCOPPER.cc:141
The RawDataBlock class Base class for rawdata handling.
virtual void PrintData(int *buf, int nwords)
print data
virtual int * GetBuffer(int n)
get nth buffer pointer
virtual int GetBufferPos(int n)
get position of data block in word
virtual int CheckFTSWID(int n)
get FTSW ID to check whether this data block is FTSW data or not
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
virtual void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes)
set buffer ( delete_flag : m_buffer is freed( = 0 )/ not freed( = 1 ) in Destructor )
virtual int GetNumNodes()
get # of data sources(e.g. # of COPPER boards) in m_buffer
virtual int CheckTLUID(int n)
get FTSW ID to check whether this data block is FTSW data or not
virtual int GetBlockNwords(int n)
get size of a data block
virtual int * GetWholeBuffer()
get pointer to buffer(m_buffer)
virtual int GetNumEvents()
get # of events in m_buffer
virtual int TotalBufNwords()
Get total length of m_buffer.
The Raw FTSW class.
Definition RawFTSW.h:30
unsigned int GetTTCtimeTRGType(int n)
Get a word containing ctime and trigger type info.
Definition RawFTSW.h:72
unsigned int GetExpRunSubrun(int n)
Exp# (10bit) run# (14bit) restart # (8bit)
Definition RawFTSW.h:130
unsigned int GetEveNo(int n)
Get event #.
Definition RawFTSW.h:65
unsigned int GetTTUtime(int n)
get unixtime of the trigger
Definition RawFTSW.h:79
void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17
set buffer ( delete_flag : m_buffer is freed( = 0 )/ not freed( = 1 ) in Destructor )
Definition RawFTSW.cc:107
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum, unsigned int prev_exprunsubrun_no, unsigned int *cur_exprunsubrun_no)
check the data contents
Definition RawFTSW.h:121
The Raw TLU class Class for data from DAQ PC for TLU(Trigger Logic Unit) It is supposed to be used on...
Definition RawTLU.h:27
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum)
check data contents
Definition RawTLU.h:81
unsigned int GetEveNo(int n)
Get Event #.
Definition RawTLU.h:53
void SetBuffer(const int *hdr)
set buffer
Definition SendHeader.cc:37
int GetNumEventsinPacket()
get contents of Header
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:650
unsigned int GetTTCtimeTRGType(int n)
Check if COPPER Magic words are correct.
Definition RawCOPPER.h:608
int GetExpNo(int n)
get Experimental # from header
Definition RawCOPPER.h:365
unsigned int GetEveNo(int n)
get subrun #(8bit)
Definition RawCOPPER.h:390
unsigned int GetTTUtime(int n)
Check if COPPER Magic words are correct.
Definition RawCOPPER.h:614
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum, unsigned int prev_copper_ctr, unsigned int *cur_copper_ctr, unsigned int prev_exprunsubrun_no, unsigned int *cur_exprunsubrun_no)
check data contents
Definition RawCOPPER.h:699
int GetSubRunNo(int n)
get run # (14bit)
Definition RawCOPPER.h:384
unsigned int GetErrorBitFlag(int n)
get contents of header
Definition RawCOPPER.h:416
int GetRunNo(int n)
Exp# (10bit) run# (14bit) restart # (8bit)
Definition RawCOPPER.h:377
Abstract base class for different kinds of events.
STL namespace.