Belle II Software  release-08-01-10
RawCOPPER.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/RawCOPPER.h>
10 
11 #include <framework/utilities/HTML.h>
12 #include <sstream>
13 
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 //#define DESY
19 //#define NO_DATA_CHECK
20 //#define WO_FIRST_EVENUM_CHECK
21 
22 RawCOPPER::RawCOPPER()
23 {
24  m_access = NULL;
25  m_version = -1;
26 }
27 
28 RawCOPPER::~RawCOPPER()
29 {
30  if (m_access != NULL) delete m_access;
31  m_access = NULL;
32 }
33 
34 void RawCOPPER::SetVersion()
35 {
36 
37  if (m_buffer == NULL) {
38  char err_buf[500];
39  sprintf(err_buf, "m_buffer is NULL. Exiting...");
40  printf("%s", err_buf); fflush(stdout);
41  B2FATAL(err_buf);
42  }
43 
44  if (m_access != NULL) {
45  delete m_access;
46  }
47 
48  m_version = ((m_buffer[ POS_FORMAT_VERSION ]) & FORMAT_MASK) >> 8;
49 
50  switch (m_version) {
51  case LATEST_POSTREDUCTION_FORMAT_VER :
52  m_access = new PostRawCOPPERFormat_latest;
53  // printf("Calling PostRawCOPPERFormat_latest\n");
54  break;
55  case (0x80 + LATEST_POSTREDUCTION_FORMAT_VER) :
56  m_access = new PreRawCOPPERFormat_latest;
57  // printf("Calling PreRawCOPPERFormat_latest\n");
58  break;
59  case 0x2 :
60  m_access = new PostRawCOPPERFormat_v2;
61  // printf("Calling PostRawCOPPERFormat_latest\n");
62  break;
63  case (0x80 + 0x2) :
64  m_access = new PreRawCOPPERFormat_v2;
65  // printf("Calling PreRawCOPPERFormat_latest\n");
66  break;
67  case 0x1 :
68  m_access = new PostRawCOPPERFormat_v1;
69  // printf("Calling RawCOPPERFormat_v1\n");
70  break;
71  case (0x80 + 0x1) :
72  m_access = new PreRawCOPPERFormat_v1;
73  // printf("Calling PreRawCOPPERFormat_latest\n");
74  break;
75  case 0 :
76  m_access = new RawCOPPERFormat_v0;
77  // printf("Calling RawCOPPERFormat_v0\n");
78  break;
79  default : {
80  char err_buf[500];
81  sprintf(err_buf, "[FATAL] ERROR_EVENT : Invalid version of a data format(0x%.8x = 0x7f7f**..). Exiting...\n %s %s %d\n",
82  m_buffer[ POS_FORMAT_VERSION ], __FILE__, __PRETTY_FUNCTION__, __LINE__);
83  printf("%s", err_buf); fflush(stdout);
84  B2FATAL(err_buf); // to reduce multiple error messages
85  }
86  }
87 
88  m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes);
89 
90 
91 
92 }
93 
94 void RawCOPPER::SetVersion(string class_name)
95 {
96 
97  if (m_access != NULL) {
98  delete m_access;
99  }
100 
101  if (class_name == "PostRawCOPPERFormat_latest") {
102  m_access = new PostRawCOPPERFormat_latest;
103  m_version = (0 << 7) | LATEST_POSTREDUCTION_FORMAT_VER;
104  } else if (class_name == "PreRawCOPPERFormat_latest") {
105  m_access = new PreRawCOPPERFormat_latest;
106  m_version = (1 << 7) | LATEST_POSTREDUCTION_FORMAT_VER;
107  } else if (class_name == "PostRawCOPPERFormat_v1") {
108  m_access = new PostRawCOPPERFormat_v1;
109  m_version = (0 << 7) | 1;
110  } else if (class_name == "PreRawCOPPERFormat_v1") {
111  m_access = new PreRawCOPPERFormat_v1;
112  m_version = (1 << 7) | 1;
113  } else if (class_name == "RawCOPPERFormat_v0") {
114  m_access = new RawCOPPERFormat_v0;
115  m_version = (0 << 7) | 0;
116  } else {
117  char err_buf[500];
118  sprintf(err_buf, "Invalid name of a data format class (%s). Exiting...\n %s %s %d\n",
119  class_name.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__);
120  printf("%s", err_buf); fflush(stdout);
121  B2FATAL(err_buf);
122  }
123 
124 // if( class_name == "RawCOPPERFormat_v0" ){
125 // m_access = new RawCOPPERFormat_v0;
126 // m_version = ( 0 << 7 ) | 0;
127 // }else if( class_name == "PreRawCOPPERFormat_v0" ){
128 // m_access = new PreRawCOPPERFormat_v0;
129 // m_version = ( 1 << 7 ) | 0;
130 // }else if( class_name == "RawCOPPERFormat_v2" ){
131 // m_access = new RawCOPPERFormat_v2;
132 // m_version = ( 0 << 7 ) | 2;
133 // }else if( class_name == "PreRawCOPPERFormat_v2" ){
134 // m_access = new PreRawCOPPERFormat_v2;
135 // m_version = ( 1 << 7 ) | 2;
136 // }
137  m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes);
138 
139 }
140 
141 void RawCOPPER::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes)
142 {
143  if (bufin == NULL) {
144  char err_buf[500];
145  sprintf(err_buf, "[DEBUG] bufin is NULL. Exting...\n");
146  printf("%s", err_buf); fflush(stdout);
147  B2FATAL(err_buf);
148  }
149 
150  if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer;
151 
152  if (delete_flag == 0) {
153  m_use_prealloc_buf = true;
154  } else {
155  m_use_prealloc_buf = false;
156  }
157 
158  m_nwords = nwords;
159  m_buffer = bufin;
160 
161  m_num_nodes = num_nodes;
162  m_num_events = num_events;
163 
164  SetVersion();
165 
166 }
167 
168 
169 void RawCOPPER::ShowBuffer()
170 {
171 
172  printf("\n");
173  printf("POINTER %p\n", m_buffer);
174  for (int k = 0; k < 100; k++) {
175  printf("%.8x ", m_buffer[ k ]);
176  if ((k + 1) % 10 == 0) printf("\n%.8x : ", k);
177  }
178  printf("\n");
179 
180 }
181 
182 
183 void RawCOPPER::PackDetectorBuf(
184  int* detector_buf_1st, int nwords_1st,
185  int* detector_buf_2nd, int nwords_2nd,
186  int* detector_buf_3rd, int nwords_3rd,
187  int* detector_buf_4th, int nwords_4th,
188  RawCOPPERPackerInfo rawcprpacker_info)
189 {
190  // This function should be used for packing COPPER-format data.
191  if (m_access != NULL) {
192  delete m_access;
193  }
194  m_access = new PostRawCOPPERFormat_v2; // The latest version for COPPER-format
195  m_version = 2; // The latest version for COPPER-format
196  m_num_events = 1;
197  m_num_nodes = 1;
198 
199  int* packed_buf = NULL;
200  packed_buf = m_access->PackDetectorBuf(&m_nwords,
201  detector_buf_1st, nwords_1st,
202  detector_buf_2nd, nwords_2nd,
203  detector_buf_3rd, nwords_3rd,
204  detector_buf_4th, nwords_4th,
205  rawcprpacker_info);
206 
207  int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called.
208  SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes);
209 
210  delete_flag = 0; // For m_access, need not to delete m_buffer
211  m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes);
212 
213  return;
214 }
215 
216 void RawCOPPER::PackDetectorBuf(int* const(&detector_buf_ch)[MAX_PCIE40_CH],
217  int const(&nwords_ch)[MAX_PCIE40_CH],
218  RawCOPPERPackerInfo rawcprpacker_info)
219 {
220  if (LATEST_POSTREDUCTION_FORMAT_VER < 4) {
221  char err_buf[500];
222  sprintf(err_buf, "This function must be used for PCIe40 data(ver.4 or later). Exiting...");
223  printf("%s", err_buf); fflush(stdout);
224  B2FATAL(err_buf);
225  }
226 
227  if (m_access != NULL) {
228  delete m_access;
229  }
230 
231  m_access = new PostRawCOPPERFormat_latest;
232  m_version = LATEST_POSTREDUCTION_FORMAT_VER;
233  m_num_events = 1;
234  m_num_nodes = 1;
235 
236  int* packed_buf = NULL;
237  packed_buf = m_access->PackDetectorBuf(&m_nwords,
238  detector_buf_ch, nwords_ch,
239  rawcprpacker_info);
240 
241  int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called.
242  SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes);
243 
244  delete_flag = 0; // For m_access, need not to delete m_buffer
245  m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes);
246 
247  return;
248 }
249 
250 
251 void RawCOPPER::PackDetectorBuf4DummyData(
252  int* detector_buf_1st, int nwords_1st,
253  int* detector_buf_2nd, int nwords_2nd,
254  int* detector_buf_3rd, int nwords_3rd,
255  int* detector_buf_4th, int nwords_4th,
256  RawCOPPERPackerInfo rawcprpacker_info)
257 {
258  // This function should be used for packing COPPER-format data.
259  if (m_access != NULL) {
260  delete m_access;
261  }
262  m_access = new PostRawCOPPERFormat_v2; // The latest version for COPPER-format
263  m_version = 2; // The latest version for COPPER-format
264 
265  m_num_events = 1;
266  m_num_nodes = 1;
267 
268  int* packed_buf = NULL;
269  packed_buf = m_access->PackDetectorBuf(&m_nwords,
270  detector_buf_1st, nwords_1st,
271  detector_buf_2nd, nwords_2nd,
272  detector_buf_3rd, nwords_3rd,
273  detector_buf_4th, nwords_4th,
274  rawcprpacker_info);
275 
276  int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called.
277  SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes);
278 
279  delete_flag = 0; // For m_access, need not to delete m_buffer
280  m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes);
281 
282  return;
283 }
284 
285 std::string RawCOPPER::getInfoHTML() const
286 {
287  std::stringstream s;
288  //TODO: All these methods should be const instead.
289  RawCOPPER* nonconst_this = const_cast<RawCOPPER*>(this);
290  nonconst_this->CheckVersionSetBuffer();
291 
292  const int nEntries = nonconst_this->GetNumEntries();
293  s << "Entries: " << nEntries;
294  s << ", Total size (32bit words): " << m_nwords << "<br>";
295 
296  s << "COPPER format version: " << m_version;
297 
298  const char a = 'A'; //finesses are counted from A to D
299  for (int iEntry = 0; iEntry < nEntries; ++iEntry) {
300  s << "<h4>Entry " << iEntry << "</h4>";
301  s << "Node ID: 0x" << std::hex << nonconst_this->GetNodeID(iEntry) << std::dec;
302  for (int iFinesse = 0; iFinesse < 4; iFinesse++) {
303  const int nWords = nonconst_this->GetDetectorNwords(iEntry, iFinesse);
304  const int* buf = nonconst_this->GetDetectorBuffer(iEntry, iFinesse);
305  s << "<p>Finesse " << char(a + iFinesse) << " (Size: " << nWords << ")</p>";
306  s << HTML::getHexDump(buf, nWords) << "<br>";
307  }
308  }
309 
310  return s.str();
311 }
312 
313 void RawCOPPER::CompareHeaderValue(int n, const unsigned int (&input_val)[MAX_PCIE40_CH], vector<vector< unsigned int>>& result)
314 {
315  CheckVersionSetBuffer();
316  m_access->CompareHeaderValue(n, input_val, result);
317  return;
318 }
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
The Raw COPPER class ver.2 This class stores data received by COPPER via belle2link Data from all det...
int * PackDetectorBuf(int *packed_buf_nwords, int *detector_buf_1st, int nwords_1st, int *detector_buf_2nd, int nwords_2nd, int *detector_buf_3rd, int nwords_3rd, int *detector_buf_4th, int nwords_4th, RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17
Pack data (format ver. = -1 -> Select the latest format version)
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
The Raw COPPER class ver.0 ( from August, 2013 to April, 2014 ) This class stores data received by CO...
struct to contain header information used by RawCOPPERFormat::Packer()
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition: RawCOPPER.h:52
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
Definition: RawDataBlock.h:67
void CheckVersionSetBuffer()
Check the version number of data format.
Definition: RawCOPPER.h:741
int GetDetectorNwords(int n, int finesse_num)
get Detector buffer length
Definition: RawCOPPER.h:657
int * GetDetectorBuffer(int n, int finesse_num)
get Detector buffer
Definition: RawCOPPER.h:681
unsigned int GetNodeID(int n)
get node-ID from data
Definition: RawCOPPER.h:397
Abstract base class for different kinds of events.