Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packet_A.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file packet_A.cc
1 #include "packet_A.h"
2 #include "packetHeaders.h"
3 #include <string.h>
4 #include "buffer.h"
5 
7 {
8 
9  data1_length = 0;
10  data2_length = 0;
11  data3_length = 0;
12  data4_length = 0;
13  data5_length = 0;
14  data6_length = 0;
15  data7_length = 0;
16  data8_length = 0;
17  data9_length = 0;
18  data10_length = 0;
19 
20  decoded_data1 = NULL;
21  decoded_data2 = NULL;
22  decoded_data3 = NULL;
23  decoded_data4 = NULL;
24  decoded_data5 = NULL;
25  decoded_data6 = NULL;
26  decoded_data7 = NULL;
27  decoded_data8 = NULL;
28  decoded_data9 = NULL;
29  decoded_data10 = NULL;
30 
31  is_data_type = 0; //assume "p" type first.
32 }
33 
35 {
36 
37  packet = packet_ptr;
38  // packetHdr = ( PACKETHDR_ptr ) packet;
39 
40  data1_length = 0;
41  data2_length = 0;
42  data3_length = 0;
43  data4_length = 0;
44  data5_length = 0;
45  data6_length = 0;
46  data7_length = 0;
47  data8_length = 0;
48  data9_length = 0;
49  data10_length = 0;
50 
51  decoded_data1 = NULL;
52  decoded_data2 = NULL;
53  decoded_data3 = NULL;
54  decoded_data4 = NULL;
55  decoded_data5 = NULL;
56  decoded_data6 = NULL;
57  decoded_data7 = NULL;
58  decoded_data8 = NULL;
59  decoded_data9 = NULL;
60  decoded_data10 = NULL;
61 
62 
63  is_data_type = 0; //assume "p" type first.
64 }
65 
66 // ----------------------------------------------
67 
69 {
70 
71  if (decoded_data1 != NULL)
72  {
73  delete [] decoded_data1;
74  decoded_data1 = 0;
75  }
76  if (decoded_data2 != NULL)
77  {
78  delete [] decoded_data2;
79  decoded_data2 = 0;
80  }
81  if (decoded_data3 != NULL)
82  {
83  delete [] decoded_data3;
84  decoded_data3 = 0;
85  }
86  if (decoded_data4 != NULL)
87  {
88  delete [] decoded_data4;
89  decoded_data4 = 0;
90  }
91  if (decoded_data5 != NULL)
92  {
93  delete [] decoded_data5;
94  decoded_data5 = 0;
95  }
96  if (decoded_data6 != NULL)
97  {
98  delete [] decoded_data6;
99  decoded_data6 = 0;
100  }
101  if (decoded_data7 != NULL)
102  {
103  delete [] decoded_data7;
104  decoded_data7 = 0;
105  }
106  if (decoded_data8 != NULL)
107  {
108  delete [] decoded_data8;
109  decoded_data8 = 0;
110  }
111  if (decoded_data9 != NULL)
112  {
113  delete [] decoded_data9;
114  decoded_data9 = 0;
115  }
116  if (decoded_data10 != NULL)
117  {
118  delete [] decoded_data10;
119  decoded_data10 = 0;
120  }
121 
122  if (is_data_type) delete [] packet;
123 }
124 
125 
127 {
128  if (is_data_type) return 0;
129  else return 1;
130 }
131 
132 // ----------------------------------------------
133 
135 {
136  if (is_data_type) return -1;
137 
138  PHDWORD *tmp;
139 
140  tmp = new PHDWORD[getLength()];
141  PHDWORD *from= packet;
142  PHDWORD *to=tmp;
143  for (int k=0; k< getLength(); k++)
144  {
145  *to++ = *from++;
146  }
147  packet = tmp;
148  is_data_type = 1;
149  return 0;
150 
151 }
152 
153 // ------------------------------------------------------
155 {
156  return getPacketLength (packet) ;
157 }
158 
159 // ------------------------------------------------------
160 int Packet_A::getErrorLength() const { return
162 
163 //------------------------------------------------------
164 int Packet_A::getDebugLength() const { return
166 
167 //------------------------------------------------------
168 int Packet_A::getIdentifier() const { return getPacketId (packet); }
169 
170 //------------------------------------------------------
171 int Packet_A::setIdentifier(const int newid)
172 {
173  // check that we have short range; we want the parameter to be int
174  if ( newid & 0xffff0000) return -1;
175  setPacketId (packet, newid);
176  return 0;
177 }
178 
179 
180 //------------------------------------------------------
181 int Packet_A::getPadding() const {
182 return
184 }
185 
186 // ------------------------------------------------------
188 {
189  return getPacketStructure (packet);
190 }
191 
192 // ------------------------------------------------------
194 {
196 }
197 
198 // ------------------------------------------------------
199 //int Packet_A::getWordSize() const
200 //{
201 // return ??? // ** action
202 //
203 //}
204 
205 // ------------------------------------------------------
207 {
208  return getPacketDataLength (packet);
209 }
210 
211 // ------------------------------------------------------
212 
214 {
215  out << std::dec;
216  out << "*** Packet id " << getPacketId (packet) << std::endl;
217  out << " Hdr version " << getPacketHdrVersion (packet) << std::endl;
218  out << " Hdr length " << getPacketHdrLength (packet) << std::endl;
219  out << " Packet Length " << getPacketLength (packet) << std::endl;
220  out << " Packet Status " << getPacketStatus (packet) << std::endl;
221  out << " Debug Length " << getPacketDebugLength (packet) << std::endl;
222  out << " Error Length " << getPacketErrorLength (packet) << std::endl;
223  out << " Structure " << getStructure() << " (";
224 
225  switch (getStructure())
226  {
227 
228  case Unstructured:
229  out << "Unformatted) ";
230  break;
231 
232  case Packets:
233  out << "Unformatted) ";
234  out << "Packets ) ";
235  break;
236 
237  case hitArray:
238  out << "HitArray ) ";
239  break;
240 
241  case hitList:
242  out << "HitList ) ";
243  break;
244  }
245  out << std::endl;
246 
247  out << " Descriptor Words: " << getPacketDataDescrLength (packet) << std::endl;
248  out << " Endianism: " << getPacketEndianism (packet);
249 
250  switch (getPacketEndianism (packet) )
251  {
252 
253  case 1:
254  out << " (Little Endian) " << std::endl;
255  break;
256 
257  case 2:
258  out << " (Big Endian) " << std::endl;
259  break;
260 
261  default:
262  out << " (Unknown) " << std::endl;
263  break;
264  }
265 
266  out << " Padding: " << getPacketPadding (packet) << std::endl;
267 
268 
269 }
270 
272 {
273  // out << "identify packet type A" << std::endl;
274  out << std::dec
275  << "Packet " << SETW(6) << getIdentifier()
276  << " " << SETW(5) << getLength()
277  << " " << SETW(2) << getStructure() << " (";
278 
279  switch (getStructure())
280  {
281 
282  case Unstructured:
283  out << "Unformatted) ";
284  break;
285 
286  case Packets:
287  out << "Unformatted) ";
288  out << "Packets ) ";
289  break;
290 
291  case hitArray:
292  out << "HitArray ) ";
293  break;
294 
295  case hitList:
296  out << "HitList ) ";
297  break;
298  }
299 
300  out << SETW(4) << getHitFormat()
301  << " (" << get_mnemonic( getStructure(), getHitFormat()) << ")";
302 
303  if (getErrorLength() ) out << " ** Error Block";
304 
305  out << std::endl;
306 }
307 
308 
309 // ------------------------------------------------------
310 
311 int Packet_A::iValue(const int ich)
312 {
313  // now let's derefence the proxy array. If we didn't decode
314  // the data until now, we do it now
315  if (decoded_data1 == NULL )
316  {
317  if ( (decoded_data1 = decode(&data1_length))==NULL)
318  return 0;
319  }
320 
321  // see if our array is long enough
322  if (ich < 0 || ich >= data1_length) return 0;
323 
324  return decoded_data1[ich];
325 }
326 
327 // ------------------------------------------------------
328 
329 int Packet_A::iValue(const int ich, const char *what)
330 {
331  // now let's derefence the proxy array. If we didn't decode
332  // the data until now, we do it now
333  if (decoded_data1 == NULL )
334  {
335  if ( (decoded_data1 = decode(&data1_length))==NULL)
336  return 0;
337  }
338 
339  // see if our array is long enough
340  if (ich >= data1_length) return 0;
341 
342  return decoded_data1[ich];
343 }
344 
345 // ------------------------------------------------------
346 
347 int Packet_A::iValue(const int ich, const int iy)
348 {
349  // now let's derefence the proxy array. If we didn't decode
350  // the data until now, we do it now
351  if (decoded_data1 == NULL )
352  {
353  if ( (decoded_data1 = decode(&data1_length))==NULL)
354  return 0;
355  }
356 
357  // see if our array is long enough
358  if (ich > data1_length) return 0;
359 
360  return decoded_data1[ich];
361 }
362 
363 // ------------------------------------------------------
364 
365 float Packet_A::rValue(const int ich)
366 {
367  // now let's derefence the proxy array. If we didn't decode
368  // the data until now, we do it now
369  if (decoded_data1 == NULL )
370  {
371  if ( (decoded_data1 = decode(&data1_length))==NULL)
372  return 0;
373  }
374 
375  // see if our array is long enough
376  if (ich > data1_length) return 0;
377 
378  return float(decoded_data1[ich]);
379 }
380 
381 // ------------------------------------------------------
382 
383 float Packet_A::rValue(const int ich, const char *what)
384 {
385  // now let's derefence the proxy array. If we didn't decode
386  // the data until now, we do it now
387  if (decoded_data1 == NULL )
388  {
389  if ( (decoded_data1 = decode(&data1_length))==NULL)
390  return 0;
391  }
392 
393  // see if our array is long enough
394  if (ich > data1_length) return 0;
395 
396  return float(decoded_data1[ich]);
397 }
398 
399 // ------------------------------------------------------
400 
401 float Packet_A::rValue(const int ich, const int iy)
402 {
403  // now let's derefence the proxy array. If we didn't decode
404  // the data until now, we do it now
405  if (decoded_data1 == NULL )
406  {
407  if ( (decoded_data1 = decode(&data1_length))==NULL)
408  return 0;
409  }
410 
411  // see if our array is long enough
412  if (ich > data1_length) return 0;
413 
414  return float(decoded_data1[ich]);
415 }
416 
417 // ----------------------------------------------
418 
419 int
420 Packet_A::getArraylength(const char *what)
421 {
422  // now let's derefence the proxy array. If we didn't decode
423  // the data until now, we do it now
424  if (decoded_data1 == NULL )
425  {
426  if ( (decoded_data1 = decode(&data1_length))==NULL)
427  return -1;
428  }
429 
430  return data1_length;
431 }
432 
433 // ----------------------------------------------
434 
435 int
437  const int nlen, int *nwout,
438  const char *what)
439 {
440  return standardIntArray(iarr, nlen, nwout, what);
441 
442 }
443 
444 
445 
446 // ----------------------------------------------
447 
448 int
450  const int nlen, int *nwout,
451  const char *what)
452 {
453 
454  int *from;
455  int howmuch;
456 
457  *nwout = 0;
458 
459  if (strcmp(what,"") == 0)
460  {
461  // now let's derefence the proxy array. If we didn't decode
462  // the data until now, we do it now
463  if (decoded_data1 == NULL )
464  {
465  if ( (decoded_data1 = decode(&data1_length))==NULL)
466  {
467  *nwout=0;
468  return -1;
469  }
470  }
471  howmuch = data1_length;
472  from = decoded_data1;
473  }
474 
475  else if (strcmp(what,"RAW") == 0)
476  {
477  howmuch = getLength();
478  from = (int *) packet;
479  }
480 
481  else if (strcmp(what,"DATA") == 0)
482  {
483  howmuch = getDataLength();
484  from = (int *) findPacketDataStart(packet);
485  }
486 
487  else
488  {
489  *nwout = 0;
490  return 0;
491  }
492 
493  // see if by any chance we got a negative length (happens)
494  if (howmuch < 0)
495  {
496  *nwout = 0;
497  return -3;
498  }
499 
500  // see if our array is long enough
501  if (nlen < howmuch)
502  {
503  *nwout = 0;
504  return -2;
505  }
506 
507  if ( from == 0)
508  {
509  *nwout = 0;
510  return -1;
511  }
512 
513  // and copy the data to the output array
514  memcpy(iarr, from, 4*howmuch);
515 
516  // tell how much we copied
517  *nwout = howmuch;
518  return 0;
519 }
520 
521 // ------------------------------------------------------
522 
523 
524 int Packet_A::copyMe(int dest[], const int maxlength) const
525 {
526 
527  if ( getLength() > maxlength )
528  {
529  return 0;
530  }
531 
532  memcpy((void *) dest, (void *) packet, 4*getLength() );
533  return getLength();
534 }
535 
536 
537 
538 // ------------------------------------------------------
539 
540 
541 
542 int
544  const int nlen, int *nwout,
545  const char *what)
546 {
547 
548  // now let's derefence the proxy array. If we didn't decode
549  // the data until now, we do it now
550  if (decoded_data1 == NULL )
551  {
552  if ( (decoded_data1 = decode(&data1_length))==NULL)
553  {
554  *nwout=0;
555  return -1;
556  }
557  }
558 
559  // see if our array is long enough
560  if (nlen < data1_length) return -2;
561 
562  // and copy the data to the output array
563  int *from = decoded_data1;
564  for (int i=0; i<data1_length; i++) *rarr++ = float (*from++);
565 
566  // tell how much we copied
567  *nwout = data1_length;
568  return 0;
569 }
570 
571 // ------------------------------------------------------
572 int*
573 Packet_A::getIntArray (int *nwout, const char *what)
574 {
575 
576  // now let's derefence the proxy array. If we didn't decode
577  // the data until now, we do it now
578  if (decoded_data1 == NULL )
579  {
580  if ( (decoded_data1 = decode(&data1_length))==NULL)
581  {
582  *nwout=0;
583  return NULL;
584  }
585  }
586 
587  int *temp=new int[data1_length];
588  int is = fillIntArray(temp, data1_length, nwout);
589  if (is)
590  {
591  *nwout=0;
592  return NULL;
593  }
594  return temp;
595 }
596 
597 // ------------------------------------------------------
598 float* Packet_A::getFloatArray (int *nwout, const char *what)
599 {
600  // now let's derefence the proxy array. If we didn't decode
601  // the data until now, we do it now
602  if (decoded_data1 == NULL )
603  {
604  if ( (decoded_data1 = decode(&data1_length))==NULL)
605  {
606  *nwout=0;
607  return NULL;
608  }
609  }
610 
611  float *temp=new float[data1_length];
612  int is = fillFloatArray(temp, data1_length, nwout);
613  if (is)
614  {
615  *nwout=0;
616  return NULL;
617  }
618  return temp;
619 }
620 
621 // ------------------------------------------------------
623 {
624  int el = getErrorLength();
625  if (!el )
626  {
627  // out << "--- No error block" << std::endl;
628  return;
629  }
630 
631  int j,l;
632 
633  int * data = (int *) findPacketErrorStart (packet);
634 
635  // here we make sure that the packet isn't obviously bogus,
636  // although it's not a comprehensive check.
637  if ( el > getLength() || data < (int *) packet ) return;
638 
639 
640  j = 0;
641  COUT << "--- Error block:" << std::endl;
642  while (1)
643  {
644  out << SETW(5) << j << " | ";
645  for (l=0;l<4;l++)
646  {
647  out << std::hex << SETW(8) << data[j++] << " " ;
648  if (j >= el ) break;
649  }
650  if (j >= el ) break;
651  out << std::dec<< std::endl;
652  }
653  out << std::dec << std::endl;
654 }
655 
656 
657 
658 // ------------------------------------------------------
660 {
661  int el = getDebugLength();
662  if (!el )
663  {
664  // out << "--- No Debug block" << std::endl;
665  return;
666  }
667 
668  int j,l;
669 
670  int * data = (int *) findPacketDebugStart (packet);
671 
672  if ( el > getLength() || data < (int *) packet ) return;
673 
674  j = 0;
675  COUT << "--- Debug block:" << std::endl;
676  while (1)
677  {
678  out << SETW(5) << j << " | ";
679  for (l=0;l<4;l++)
680  {
681  out << std::hex << SETW(8) << data[j++] << " " ;
682  if (j >= el ) break;
683  }
684  if (j >= el ) break;
685  out << std::dec<< std::endl;
686  }
687  out << std::dec<< std::endl;
688 
689  out << "--- DCM Checksum: " << getCheckSumStatus() << std::endl;
690 
691 }
692 
693 #ifdef LVL2_WINNT
694  void fix_endianess ( LONGLONG *x)
695 #else
696  void fix_endianess ( long long *x)
697 #endif
698 {
699  unsigned int *i = (unsigned int *) x;
700  unsigned int r = i[0];
701  i[0] = i[1];
702  i[1] = r;
703 }
704 
705 
706 void Packet_A::fix_endianess ( double *x)
707 {
708  unsigned int *i = (unsigned int *) x;
709  unsigned int r = i[0];
710  i[0] = i[1];
711  i[1] = r;
712 }
713 
714 void Packet_A::fix_endianess ( char *str, const int length)
715 {
716  int j;
717  unsigned int *i = (unsigned int *) str;
718  for (j = 0; j< length/4; j++) i[j] = buffer::i4swap(i[j]);
719 
720 }
721 
722 
723 
725 {
726 
727  // if we don't have a debug block, wearedone (but don't
728  // know what the answer is...
729 
730  if ( getDebugLength() == 0 ) return 0;
731  int i;
732 
733  int *d = (int *) packet;
734 
735  int checksum_recalc = 0x0;
736  for (i = 0;i < getLength() - 2;i++)
737  {
738  checksum_recalc ^= *d++;
739  }
740  d++;
741 
742  // COUT << "Checksum : " << std::hex << checksum_recalc << " " << *d << std::dec << ENDL;
743 
744  if (checksum_recalc != *d) return -1;
745  return 1;
746 }
747 
748