Belle II Software  release-05-01-25
RawCOPPER.cc
1 //+
2 // File : RawCOPPER.cc
3 // Description : Module to handle raw data from COPPER.
4 //
5 // Author : Satoru Yamada, IPNS, KEK
6 // Date : 2 - Aug - 2013
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  switch (m_version) {
50  case LATEST_POSTREDUCTION_FORMAT_VER :
51  m_access = new PostRawCOPPERFormat_latest;
52  // printf("Calling PostRawCOPPERFormat_latest\n");
53  break;
54  case (0x80 + LATEST_POSTREDUCTION_FORMAT_VER) :
55  m_access = new PreRawCOPPERFormat_latest;
56  // printf("Calling PreRawCOPPERFormat_latest\n");
57  break;
58  case 0x1 :
59  m_access = new PostRawCOPPERFormat_v1;
60  // printf("Calling RawCOPPERFormat_v1\n");
61  break;
62  case (0x80 + 0x1) :
63  m_access = new PreRawCOPPERFormat_v1;
64  // printf("Calling PreRawCOPPERFormat_latest\n");
65  break;
66  case 0 :
67  m_access = new RawCOPPERFormat_v0;
68  // printf("Calling RawCOPPERFormat_v0\n");
69  break;
70  default : {
71  char err_buf[500];
72  sprintf(err_buf, "[FATAL] ERROR_EVENT : Invalid version of a data format(0x%.8x = 0x7f7f**..). Exiting...\n %s %s %d\n",
73  m_buffer[ POS_FORMAT_VERSION ], __FILE__, __PRETTY_FUNCTION__, __LINE__);
74  printf("%s", err_buf); fflush(stdout);
75  B2FATAL(err_buf); // to reduce multiple error messages
76  }
77  }
78 
79  m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes);
80 
81 
82 
83 }
84 
85 void RawCOPPER::SetVersion(string class_name)
86 {
87 
88  if (m_access != NULL) {
89  delete m_access;
90  }
91 
92  if (class_name == "PostRawCOPPERFormat_latest") {
93  m_access = new PostRawCOPPERFormat_latest;
94  m_version = (0 << 7) | LATEST_POSTREDUCTION_FORMAT_VER;
95  } else if (class_name == "PreRawCOPPERFormat_latest") {
96  m_access = new PreRawCOPPERFormat_latest;
97  m_version = (1 << 7) | LATEST_POSTREDUCTION_FORMAT_VER;
98  } else if (class_name == "PostRawCOPPERFormat_v1") {
99  m_access = new PostRawCOPPERFormat_v1;
100  m_version = (0 << 7) | 1;
101  } else if (class_name == "PreRawCOPPERFormat_v1") {
102  m_access = new PreRawCOPPERFormat_v1;
103  m_version = (1 << 7) | 1;
104  } else if (class_name == "RawCOPPERFormat_v0") {
105  m_access = new RawCOPPERFormat_v0;
106  m_version = (0 << 7) | 0;
107  } else {
108  char err_buf[500];
109  sprintf(err_buf, "Invalid name of a data format class (%s). Exiting...\n %s %s %d\n",
110  class_name.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__);
111  printf("%s", err_buf); fflush(stdout);
112  B2FATAL(err_buf);
113  }
114 
115 // if( class_name == "RawCOPPERFormat_v0" ){
116 // m_access = new RawCOPPERFormat_v0;
117 // m_version = ( 0 << 7 ) | 0;
118 // }else if( class_name == "PreRawCOPPERFormat_v0" ){
119 // m_access = new PreRawCOPPERFormat_v0;
120 // m_version = ( 1 << 7 ) | 0;
121 // }else if( class_name == "RawCOPPERFormat_v2" ){
122 // m_access = new RawCOPPERFormat_v2;
123 // m_version = ( 0 << 7 ) | 2;
124 // }else if( class_name == "PreRawCOPPERFormat_v2" ){
125 // m_access = new PreRawCOPPERFormat_v2;
126 // m_version = ( 1 << 7 ) | 2;
127 // }
128  m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes);
129 
130 }
131 
132 void RawCOPPER::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes)
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  SetVersion();
156 
157 }
158 
159 
160 void RawCOPPER::ShowBuffer()
161 {
162 
163  printf("\n");
164  printf("POINTER %p\n", m_buffer);
165  for (int k = 0; k < 100; k++) {
166  printf("%.8x ", m_buffer[ k ]);
167  if ((k + 1) % 10 == 0) printf("\n%.8x : ", k);
168  }
169  printf("\n");
170 
171 }
172 
173 
174 void RawCOPPER::PackDetectorBuf(
175  int* detector_buf_1st, int nwords_1st,
176  int* detector_buf_2nd, int nwords_2nd,
177  int* detector_buf_3rd, int nwords_3rd,
178  int* detector_buf_4th, int nwords_4th,
179  RawCOPPERPackerInfo rawcprpacker_info)
180 {
181 
182  if (m_access != NULL) {
183  delete m_access;
184  }
185  m_access = new PostRawCOPPERFormat_latest;
186  m_version = LATEST_POSTREDUCTION_FORMAT_VER;
187  m_num_events = 1;
188  m_num_nodes = 1;
189 
190  int* packed_buf = NULL;
191  packed_buf = m_access->PackDetectorBuf(&m_nwords,
192  detector_buf_1st, nwords_1st,
193  detector_buf_2nd, nwords_2nd,
194  detector_buf_3rd, nwords_3rd,
195  detector_buf_4th, nwords_4th,
196  rawcprpacker_info);
197 
198  int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called.
199  SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes);
200 
201  delete_flag = 0; // For m_access, need not to delete m_buffer
202  m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes);
203 
204  return;
205 }
206 
207 
208 void RawCOPPER::PackDetectorBuf4DummyData(
209  int* detector_buf_1st, int nwords_1st,
210  int* detector_buf_2nd, int nwords_2nd,
211  int* detector_buf_3rd, int nwords_3rd,
212  int* detector_buf_4th, int nwords_4th,
213  RawCOPPERPackerInfo rawcprpacker_info)
214 {
215 
216  if (m_access != NULL) {
217  delete m_access;
218  }
219  m_access = new PreRawCOPPERFormat_latest;
220  m_version = LATEST_POSTREDUCTION_FORMAT_VER;
221  m_num_events = 1;
222  m_num_nodes = 1;
223 
224  int* packed_buf = NULL;
225  packed_buf = m_access->PackDetectorBuf(&m_nwords,
226  detector_buf_1st, nwords_1st,
227  detector_buf_2nd, nwords_2nd,
228  detector_buf_3rd, nwords_3rd,
229  detector_buf_4th, nwords_4th,
230  rawcprpacker_info);
231 
232  int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called.
233  SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes);
234 
235  delete_flag = 0; // For m_access, need not to delete m_buffer
236  m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes);
237 
238  return;
239 }
240 
241 std::string RawCOPPER::getInfoHTML() const
242 {
243  std::stringstream s;
244  //TODO: All these methods should be const instead.
245  RawCOPPER* nonconst_this = const_cast<RawCOPPER*>(this);
246  nonconst_this->CheckVersionSetBuffer();
247 
248  const int nEntries = nonconst_this->GetNumEntries();
249  s << "Entries: " << nEntries;
250  s << ", Total size (32bit words): " << m_nwords << "<br>";
251 
252  s << "COPPER format version: " << m_version;
253 
254  const char a = 'A'; //finesses are counted from A to D
255  for (int iEntry = 0; iEntry < nEntries; ++iEntry) {
256  s << "<h4>Entry " << iEntry << "</h4>";
257  s << "Node ID: 0x" << std::hex << nonconst_this->GetNodeID(iEntry) << std::dec;
258  for (int iFinesse = 0; iFinesse < 4; iFinesse++) {
259  const int nWords = nonconst_this->GetDetectorNwords(iEntry, iFinesse);
260  const int* buf = nonconst_this->GetDetectorBuffer(iEntry, iFinesse);
261  s << "<p>Finesse " << char(a + iFinesse) << " (Size: " << nWords << ")</p>";
262  s << HTML::getHexDump(buf, nWords) << "<br>";
263  }
264  }
265 
266  return s.str();
267 }
Belle2::RawCOPPER
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition: RawCOPPER.h:50
Belle2::RawCOPPERPackerInfo
struct to contain header information used by RawCOPPERFormat::Packer()
Definition: RawCOPPERPackerInfo.h:12
Belle2::RawCOPPER::GetDetectorBuffer
int * GetDetectorBuffer(int n, int finesse_num)
get Detector buffer
Definition: RawCOPPER.h:667
Belle2::PostRawCOPPERFormat_latest
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
Definition: PostRawCOPPERFormat_latest.h:40
Belle2::PreRawCOPPERFormat_latest
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
Definition: PreRawCOPPERFormat_latest.h:30
Belle2::RawDataBlock::GetNumEntries
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
Definition: RawDataBlock.h:67
Belle2::RawCOPPER::GetDetectorNwords
int GetDetectorNwords(int n, int finesse_num)
get Detector buffer length
Definition: RawCOPPER.h:643
Belle2::RawCOPPER::CheckVersionSetBuffer
void CheckVersionSetBuffer()
Check the version number of data format.
Definition: RawCOPPER.h:733
Belle2::RawCOPPERFormat_v0
The Raw COPPER class ver.0 ( from August, 2013 to April, 2014 ) This class stores data received by CO...
Definition: RawCOPPERFormat_v0.h:35
Belle2::PreRawCOPPERFormat_v1
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
Definition: PreRawCOPPERFormat_v1.h:33
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RawCOPPER::GetNodeID
unsigned int GetNodeID(int n)
get node-ID from data
Definition: RawCOPPER.h:383
Belle2::PostRawCOPPERFormat_v1
The Raw COPPER class ver.1 ( the latest version since May, 2014 ) This class stores data received by ...
Definition: PostRawCOPPERFormat_v1.h:35