ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packet_fvtx_dcm0.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file packet_fvtx_dcm0.cc
1 #include <packet_fvtx_dcm0.h>
2 
4 {
5 
8  number_of_hits = 0;
9  error_code = 0;
10 }
11 
13  : Packet_w4 (data)
14 {
17  number_of_hits = 0;
18  error_code = 0;
19 
20 }
21 
23 {
24 }
25 
26 // ------------------------------------------------------
27 
28 int Packet_fvtx_dcm0::iValue(const int ich, const int what)
29 {
30  // we check that "row" is within range
31  if ( what < 0 || what >3 ) return 0;
32 
33  // now we must make sure we have decoded first so that the number_of_hits used
34  // in a moment is valid
35 
36  if (decoded_data1 == NULL )
37  {
38  if ( (decoded_data1 = decode(&data1_length))==NULL)
39  return 0;
40  }
41 
42  if ( ich < 0 || ich >= number_of_hits) return 0;
43  if ( error_code ) return 0;
44 
45  switch (what)
46  {
47  case 0: // FEMID
48  return decoded_data2[ich];
49  break;
50 
51  case 1: // CHIPID
52  return decoded_data3[ich];
53  break;
54 
55  case 2: // CHANNNEL
56  return decoded_data4[ich];
57  break;
58 
59  case 3: // ADC
60  return decoded_data1[ich];
61  break;
62 
63  default:
64  break;
65 
66  }
67 
68  return 0;
69 }
70 
71 int Packet_fvtx_dcm0::iValue(const int ich, const char *what)
72 {
73  // now let's derefence the proxy array. If we didn't decode
74  // the data until now, we do it now
75 
76 
77  if ( strcmp(what,"HITS")==0)
78  {
79  //we need to decode the main data structures now
80  if (decoded_data1 == NULL )
81  {
82  if ( (decoded_data1 = decode(&data1_length))==NULL)
83  return 0;
84  }
85  if ( error_code ) return 0;
86  return number_of_hits;
87  }
88 
89  else if ( strcmp(what,"FLAG")==0)
90  {
91  if (decoded_data5 == NULL )
92  {
93  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
94  return 0;
95  }
96 
97  return decoded_data5[1];
98  }
99 
100  else if ( strcmp(what,"EVTNR")==0)
101  {
102  if (decoded_data5 == NULL )
103  {
104  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
105  return 0;
106  }
107 
108  return decoded_data5[0];
109  }
110 
111  else if ( strcmp(what,"DETID")==0)
112  {
113  if (decoded_data5 == NULL )
114  {
115  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
116  return 0;
117  }
118 
119  return decoded_data5[2];
120  }
121 
122  else if ( strcmp(what,"ADC")==0)
123  {
124  if (decoded_data1 == NULL )
125  {
126  if ( (decoded_data1 = decode(&data1_length))==NULL)
127  return 0;
128  }
129  if ( ich < 0 || ich >= number_of_hits) return 0;
130  return decoded_data1[ich];
131  }
132 
133 
134  else if ( strcmp(what,"FEMID")==0)
135  {
136  if (decoded_data1 == NULL )
137  {
138  if ( (decoded_data1 = decode(&data1_length))==NULL)
139  return 0;
140  }
141 
142  if ( ich < 0 || ich >= number_of_hits) return 0;
143  return decoded_data2[ich];
144  }
145 
146  else if ( strcmp(what,"CHIPID")==0)
147  {
148  if (decoded_data1 == NULL )
149  {
150  if ( (decoded_data1 = decode(&data1_length))==NULL)
151  return 0;
152  }
153 
154  if ( ich < 0 || ich >= number_of_hits) return 0;
155  return decoded_data3[ich];
156  }
157 
158  else if ( strcmp(what,"CHANNEL")==0)
159  {
160  if (decoded_data1 == NULL )
161  {
162  if ( (decoded_data1 = decode(&data1_length))==NULL)
163  return 0;
164  }
165 
166  if ( ich < 0 || ich >= number_of_hits) return 0;
167  return decoded_data4[ich];
168  }
169 
170 
171  else if ( strcmp(what,"MODADDR")==0)
172  {
173  if (decoded_data5 == NULL )
174  {
175  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
176  return 0;
177  }
178 
179  return decoded_data5[3];
180  }
181 
182 
183  else if ( strcmp(what,"BCLCK")==0)
184  {
185  if (decoded_data5 == NULL )
186  {
187  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
188  return 0;
189  }
190 
191  return decoded_data5[4];
192  }
193 
194  else if ( strcmp(what,"PARITY")==0)
195  {
196  if (decoded_data5 == NULL )
197  {
198  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
199  return 0;
200  }
201 
202  return decoded_data5[5];
203  }
204 
205  else if ( strcmp(what,"FEMERROR")==0)
206  {
207  if (decoded_data5 == NULL )
208  {
209  if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
210  return 0;
211  }
212 
213  return decoded_data5[6];
214  }
215 
216  else if ( strcmp(what,"CHECKPARITY")==0)
217  {
218  if (! parity_comparison )
219  {
221  }
222  return parity_comparison;
223  }
224 
225  else if ( strcmp(what,"ERRORCODE")==0)
226  {
227  return error_code;
228  }
229 
230 
231  return 0;
232 }
233 
234 
235 
236 // ------------------------------------------------------
237 
238 int *Packet_fvtx_dcm0::decode (int *nwout)
239 {
240  int *p,*k;
241 
242 
243  int dlength = getDataLength();
244 
245  p = (int *) findPacketDataStart(packet);
246  if (p == 0)
247  {
248  *nwout = 0;
249  return 0;
250  }
251  k = &p[5]; // skip the header info
252  int payloadlength = dlength -5;
253 
254  // this routine is designed to return the "main" array of values which gets
255  // hooked up to decoded_data1 internally.
256  // in addition, we fill decoded_data{2,3,4} here as well. In this way, we don't
257  // need to bother with deleting the allocated memory in the end.
258 
259  // we use
260  // decoded_data1 -> adc values
261  // decoded_data2 -> FEM ID
262  // decoded_data3 -> CHIP ID
263  // decoded_data4 -> CHANNEL NUMBER
264 
265  p = new int [ 4*512]; // 4 FEM ids (wedges), 512 max hits per wedge
266  decoded_data2 = new int [ 4*512]; // 4 FEM ids (wedges), 512 max hits per wedge
267  decoded_data3 = new int [ 4*512]; // 4 FEM ids (wedges), 512 max hits per wedge
268  decoded_data4 = new int [ 4*512]; // 4 FEM ids (wedges), 512 max hits per wedge
269 
270  data2_length = 4*512;
271  data3_length = 4*512;
272  data4_length = 4*512;
273 
274  memset (p, 0, 4*512*sizeof(int));
275  memset (decoded_data2, 0, 4*512*sizeof(int));
276  memset (decoded_data3, 0, 4*512*sizeof(int));
277  memset (decoded_data4, 0, 4*512*sizeof(int));
278 
279  // let's confirm that k[payloadlength-1] has the MSB set (this holds the CRS)
280  if ( (k[payloadlength-1] & 0x80000000) == 0)
281  {
282  std::cout << __FILE__ << " " << __LINE__ << " Error in trailer word " << std::hex << k[payloadlength-1] << std::dec << " ";
283  this->identify();
284  // for now we let this pass
285  }
286 
287  // and let's confirm that k[0] is an actual FEMID field ( must have all 12 LSBs 0)
288  if ( (k[0] & 0xFFF) != 0)
289  {
290  std::cout << __FILE__ << " " << __LINE__ << " Error: 1st word is not a FEM ID " << std::hex << k[0] << std::dec << " ";
291  this->identify();
292  // for now we let this pass
293  }
294 
295  int i;
296  int last_femid = -1;
297  int number_of_datawords = 0;
298  int adc, channelnumber, chipid;
299  int array_pos = 0;
300 
301  for ( i = 0; i < payloadlength -2; i++)
302  {
303 
304  if ( (k[i] & 0xFFF) == 0) // found a FEM ID
305  {
306  // the simulated data have thee femid encoded differently
307  if ( getHitFormat() == IDFVTX_SIM)
308  {
309  last_femid = ((k[i] >> 12) & 0xf) -1;
310  }
311 
312  else
313  {
314  last_femid = -1; // we reset to "invalid"
315  int f = k[i] >> 12;
316  if ( f & 1 ) last_femid = 0;
317  if ( f & 2 ) last_femid = 1;
318  if ( f & 4 ) last_femid = 2;
319  if ( f & 8 ) last_femid = 3;
320  }
321  number_of_datawords = 0; // we reset the dataword count, just a sanity check if we get more than 512
322  }
323 
324  else // now this should be a data word
325  {
326 
327  // raise hell if either
328  // - we still don't have a valid FEM ID
329  // - the number_of_datawords has reached 512
330  // - the array_pos has reached 2048
331 
332 
333  if ( last_femid < 0)
334  {
335  error_code = 1;
336  // std::cout << __FILE__ << " " << __LINE__ << " Error: got data words without FEM ID at position " << i+5 << " ";
337  //this->identify();
338  }
339  else if ( number_of_datawords >= 512)
340  {
341  error_code = 2;
342  // std::cout << __FILE__ << " " << __LINE__ << " Error: we aleady have reached the max hit count for FEMID " << last_femid << " ";
343  //this->identify();
344  }
345  else if ( array_pos >= 4*512)
346  {
347  error_code = 3;
348  // std::cout << __FILE__ << " " << __LINE__ << " Error: we aleady have reached the max total hit count " ;
349  //this->identify();
350  }
351  else
352  {
353 
354  adc = k[i] & 0x07;
355  chipid = (k[i]>>3) & 0x3f;
356  channelnumber = (k[i]>>9) & 0x7f;
357 
358  p[array_pos] = adc;
359  decoded_data2[array_pos] = last_femid;
360  decoded_data3[array_pos] = chipid;
361  decoded_data4[array_pos] = channelnumber;
362  array_pos++;
363  number_of_datawords++;
364  }
365  }
366  }
367  number_of_hits = array_pos; // we remember how many hits we actually got
368  *nwout = 4*512;
369  return p;
370 }
371 
372 // ------------------------------------------------------
373 
375 {
376  int *k;
377 
378  k = (int *) findPacketDataStart(packet);
379  if (k == 0)
380  {
381  return 0;
382  }
383 
384  int dlength = getDataLength();
385 
386  int i;
387  int p = 0;
388 
389  // the simulated data calculate the hitformat
390  // from word 0; real data from word 5
391  if ( getHitFormat() == IDFVTX_SIM)
392  {
393  i = 0;
394  }
395  else
396  {
397  i = 5;
398  }
399  for (; i< dlength-1; i++)
400  {
401  p = p ^ (k[i] & 0xffff);
402  }
403 
405 
406  if( p == iValue(0, "PARITY") )
407  {
408  parity_comparison = 1;
409  }
410  else
411  {
412  parity_comparison = -1;
413  }
414 
415  return 0;
416 }
417 
418 
419 
420 // ------------------------------------------------------
421 
423 {
424  int *p,*k;
425 
426 
427  k = (int *) findPacketDataStart(packet);
428  if (k == 0)
429  {
430  *nwout = 0;
431  return 0;
432  }
433 
434  int dlength = getDataLength();
435 
436  p = new int[7];
437 
438  p[0] = k[0] & 0xffff; // event number
439  p[1] = k[1] & 0xffff; // Flag word
440  p[2] = k[2] & 0xffff; // DET ID
441  p[3] = k[3] & 0xffff; // module address
442  p[4] = k[4] & 0xffff; // beam clock counter
443  p[5] = k[dlength-1] & 0xffff; // PARITY
444  p[6] = k[dlength-2] & 0xffff; // FEM_ERROR_WORD
445 
446 
447  *nwout = 7;
448  return p;
449 
450 }
451 
452 // ------------------------------------------------------
453 
455 {
456 
457  this->identify(os);
458 
459  os << " Number of hits " << iValue(0,"HITS") << std::endl;
460  os << " Event number: " << iValue(0,"EVTNR") << std::endl;
461  os << " Det id " << iValue(0,"DETID") << std::endl;
462  if ( iValue(0,"ERRORCODE"))
463  {
464  os << " Error Code ********* " << iValue(0,"ERRORCODE");
465  switch ( iValue(0,"ERRORCODE") )
466  {
467  case 1:
468  os << " (no FEM ID found)";
469  break;
470 
471  case 2:
472  os << " (Max hit count per FEM ID reached)";
473  break;
474 
475  case 3:
476  os << " (Max hit count per packet reached)";
477  break;
478 
479  default :
480  break;
481  }
482  os<< std::endl;
483  }
484  else
485  {
486  os << " Error Code " << iValue(0,"ERRORCODE") << std::endl;
487  }
488  os << " module address " << iValue(0,"MODADDR") << std::endl;
489  os << " Flag word 0x" << std::hex << iValue(0,"FLAG") <<std::dec << std::endl;
490  os << " beam clock counter " << iValue(0,"BCLCK") << std::endl;
491  os << " FEM Error word " << iValue(0,"FEMERROR") << std::endl;
492  os << " Parity " << std::hex << iValue(0,"PARITY") << std::dec <<std::endl;
493  os << " Parity Ok ";
494 
495  if ( iValue(0,"CHECKPARITY") ==1 )
496  {
497  os << "Yes" <<std::endl;
498  }
499  else
500  {
501  os << "No (0x" << std::hex << calculated_parity << ")" << std::dec << std::endl;
502  }
503 
504  int hit;
505 
506  if ( iValue(0,"HITS") ) os << " hit # | FemId Chip Chan ADC" << std::endl;
507 
508  for ( hit = 0; hit < iValue(0,"HITS"); hit++)
509  {
510  os << std::setw(6) << hit << " |"
511  << std::setw(5) << iValue(hit, 0)
512  << std::setw(7) << iValue(hit, 1)
513  << std::setw(7) << iValue(hit, 2)
514  << std::setw(7) << iValue(hit, 3)
515  << std::endl;
516 
517 
518 
519  }
520  std::cout << std::endl;
521 
522  // dumpErrorBlock(os);
523  // dumpDebugBlock(os);
524 
525 }
526 
527 
528 
529 
530 
531 
532 
533