Belle II Software  release-08-01-10
RawDataBlockFormat.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 <rawdata/dataobjects/RawDataBlockFormat.h>
10 
11 using namespace std;
12 using namespace Belle2;
13 
14 RawDataBlockFormat::RawDataBlockFormat()
15 {
16  m_nwords = 0;
17  m_use_prealloc_buf = 0;
18  m_buffer = NULL;
19  m_num_nodes = 0;
20  m_num_events = 0;
21 }
22 
23 RawDataBlockFormat::~RawDataBlockFormat()
24 {
25  if (!m_use_prealloc_buf && m_buffer != NULL) {
26  delete[] m_buffer;
27  }
28 }
29 
30 int RawDataBlockFormat::GetBufferPos(int n)
31 {
32  if (m_buffer == NULL || m_nwords <= 0) {
33  char err_buf[500];
34  sprintf(err_buf, "[FATAL] RawPacket buffer(%p) is not available or length(%d) is not set.\n %s %s %d\n",
35  m_buffer, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
36  printf("%s", err_buf); fflush(stdout);
37  B2FATAL(err_buf);
38  }
39 
40  if (n >= (m_num_events * m_num_nodes)) {
41  char err_buf[500];
42  sprintf(err_buf, "[FATAL] Invalid COPPER block No. (%d : max %d ) is specified. Exiting... \n %s %s %d\n",
43  n, (m_num_events * m_num_nodes), __FILE__, __PRETTY_FUNCTION__, __LINE__);
44  printf("%s", err_buf); fflush(stdout);
45  B2FATAL(err_buf);
46  }
47 
48  int pos_nwords = 0;
49  for (int i = 1; i <= n ; i++) {
50  if (m_buffer[ pos_nwords ] <= 0) {
51  char err_buf[500];
52  sprintf(err_buf,
53  "[FATAL] ERROR_EVENT : length of this data block is strange ( %d words ). Maybe data is corrupted or RawHeader info has not been filled yet. Exiting...",
54  m_buffer[ pos_nwords ]);
55  printf("%s", err_buf);
56  B2FATAL(err_buf);
57  } else {
58  pos_nwords += m_buffer[ pos_nwords ];
59  }
60  if (pos_nwords >= m_nwords) {
61  char err_buf[500];
62  sprintf(err_buf, "[FATAL] ERROR_EVENT : value of pos_nwords(%d) is larger than m_nwords(%d). Exiting...\n %s %s %d\n",
63  pos_nwords, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
64  printf("%s", err_buf); fflush(stdout);
65  B2FATAL(err_buf); // to reduce multiple error messages
66  }
67  }
68  return pos_nwords;
69 
70 }
71 
72 
73 int RawDataBlockFormat::CheckFTSWID(int n)
74 {
75  int pos = POS_NODE_ID;
76  if (m_buffer[ GetBufferPos(n) + TEMP_POS_NWORDS_HEADER ] == OLD_FTSW_NWORDS_HEADER) {
77  pos = POS_FTSW_ID_OLD;
78  }
79 
80  if ((m_buffer[ GetBufferPos(n) + pos ] & 0xffffff00) == 0x54544400) { // "TTD" + format version ( 0x20=DESY, 0x31=2018/7/11)
81  return 1;
82  } else {
83  return 0;
84  }
85 }
86 
87 
88 int RawDataBlockFormat::CheckTLUID(int n)
89 {
90  int pos = POS_NODE_ID;
91  if (m_buffer[ GetBufferPos(n) + TEMP_POS_NWORDS_HEADER ] == OLD_FTSW_NWORDS_HEADER) {
92  pos = POS_FTSW_ID_OLD;
93  }
94  if (m_buffer[ GetBufferPos(n) + pos ] == 0x544c5520) { // "TLU "
95  return 1;
96  } else {
97  return 0;
98  }
99 }
100 
101 int RawDataBlockFormat::TotalBufNwords()
102 {
103  return m_nwords;
104 }
105 
106 
107 int RawDataBlockFormat::GetBlockNwords(int n)
108 {
109  int size;
110  if (n == (m_num_events * m_num_nodes) - 1) {
111  size = m_nwords - GetBufferPos(n);
112  } else {
113  size = GetBufferPos(n + 1) - GetBufferPos(n);
114  }
115  return size;
116 }
117 
118 
119 int* RawDataBlockFormat::GetWholeBuffer()
120 {
121  return m_buffer;
122 }
123 
124 int* RawDataBlockFormat::GetBuffer(int n)
125 {
126  int pos_nwords = GetBufferPos(n);
127  return &(m_buffer[ pos_nwords ]);
128 }
129 
130 
131 void RawDataBlockFormat::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes)
132 {
133 
134  if (bufin == NULL) {
135  char err_buf[500];
136  sprintf(err_buf, "[DEBUG] bufin is NULL. Exting...\n");
137  printf("%s", err_buf); fflush(stdout);
138  B2FATAL(err_buf);
139  }
140 
141  if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer;
142 
143  if (delete_flag == 0) {
144  m_use_prealloc_buf = true;
145  } else {
146  m_use_prealloc_buf = false;
147  }
148 
149  m_nwords = nwords;
150  m_buffer = bufin;
151 
152  m_num_nodes = num_nodes;
153  m_num_events = num_events;
154 
155  return;
156 
157 }
158 
159 
160 
161 void RawDataBlockFormat::PrintData(int* buf, int nwords)
162 {
163  printf("[DEBUG] ");
164  for (int i = 0; i < nwords; i++) {
165  printf("%.8x ", buf[ i ]);
166  if (i % 10 == 9) printf("\n[DEBUG] ");
167  }
168  printf("\n[DEBUG] ");
169  printf("\n");
170  return;
171 }
172 
173 void RawDataBlockFormat::CopyBlock(int n, int* buf_to)
174 {
175  memcpy(buf_to, GetBuffer(n), GetBlockNwords(n) * sizeof(int));
176  return;
177 }
Abstract base class for different kinds of events.