ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
A_Event.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file A_Event.cc
1 #include "A_Event.h"
2 
3 #include "EventTypes.h"
4 
5 #include "packet_collection.h"
6 
7 #include "packet.h"
8 #include "Cframe.h"
9 #include "framePackets.h"
10 #include "dataBlock.h"
11 
12 #include "frameRoutines.h"
13 #include "frameHdr.h"
14 
15 
16 // the constructor first ----------------
18 {
19  // set the framlist pointer to 0 just in case.
20  framelist = 0;
21 
22 
23  hasMap = 0;
24  // we always make a pointer-based event.
25  is_data_type = 0;
26  errorcode = 0;
27 
28  // cast the pointer to the EventData pointer
29  EventData = (evtdata_ptr) data;
31  {
32  errorcode=1;
33  }
34  else
35  {
37  }
38 }
39 
41 {
42  // set the framlist pointer to 0 just in case.
43  framelist = 0;
44 
45  hasMap = 0;
46  // we always make a pointer-based event.
47  errorcode = 0;
48  is_data_type = 0;
49 
50  // cast the pointer to the EventData pointer
51  EventData = (evtdata_ptr) data;
53  {
54  errorcode=1;
55  }
56  else
57  {
59  }
60 
61 }
62 
64 {
65  delete [] framelist;
66  if (is_data_type) delete [] (PHDWORD *) EventData;
67  pmap.clear();
68 
69 }
70 
71 
72 // the info-type calls
73 unsigned int
75 {
76  return EventData->evt_length;
77 }
78 
79 int
81 {
82  return EventData->evt_type;
83 }
84 
85 int
87 {
88  return EventData->evt_sequence;
89 }
90 
91 int
93 {
94  return EventData->run_number;
95 }
96 
97 #ifdef WIN32
98 const ULONGLONG ticOffset = 35067168000000000UL;
99 const ULONGLONG ticFactor = 10000000;
100 #else
101 const unsigned long long ticOffset = 35067168000000000ULL;
102 const unsigned long long ticFactor = 10000000;
103 #endif
104 
105 
106 time_t A_Event::getTime() const
107 {
108  if ( EventData->time == -1)
109  {
110  return (time_t) EventData->date;
111  }
112  else
113  {
114 
115  unsigned long long x,y,z;
116  x = (unsigned int) EventData->time;
117  y = (unsigned int) EventData->date;
118  z = y | ( x<<32 );
119  time_t t = (z - ticOffset) / ticFactor;
120  return t;
121  }
122 }
123 
124 
125 void A_Event::listFrame ( const int id, OSTREAM &os) const
126 {
127  PHDWORD *fp;
128  PHDWORD *pp;
129  int i = 0;
130  int j = 0;
131 
132  if (id == 0)
133  {
134 
135  while ( ( fp = framelist[i++]) )
136  {
137  dumpFrame(fp, os);
138  //os << "Frame " << ++j << "align length: "<< getFrameAlignLength(fp) << std::endl;
139  }
140  }
141  else // find the frame for a given packet
142  {
143 
144  while ( (fp = framelist[i++]) )
145  {
146  j++;
147  if ( ( pp = findFramePacketId (fp, id) ) != ptrFailure)
148  {
149  dumpFrame(fp, os);
150  //os << "Frame " << j << "align length: "<< getFrameAlignLength(fp) << std::endl;
151  return;
152  }
153  }
154  }
155 }
156 
157 void
158 A_Event::listHistory(const int id, OSTREAM &os) const
159 {
160  os << "History Block: " << std::endl;
161 
162  int i = 0;
163  if (id == 0)
164  {
165  while ( PHDWORD* fp = framelist[i++] )
166  {
167  PHDWORD* h_ptr = findFrameHistoryStart(fp);
169  dumpBlock(h_ptr, len, os);
170  }
171  }
172  else // find the frame for a given packet
173  {
174  int j = 0;
175  while ( PHDWORD* fp = framelist[i++] )
176  {
177  j++;
178  if ( findFramePacketId(fp, id) != ptrFailure )
179  {
180  PHDWORD* h_ptr = findFrameHistoryStart(fp);
182  dumpBlock(h_ptr, len, os);
183  return;
184  }
185  }
186  }
187  return;
188 }
189 
190 void
191 A_Event::listError( const int id, OSTREAM& os ) const
192 {
193  os << "Error Block: " << std::endl;
194  int i = 0;
195  if (id == 0)
196  {
197  while ( PHDWORD* fp = framelist[i++] )
198  {
201  dumpBlock(ptr, len, os);
202  dumpErrorBlock(fp,os);
203  }
204  }
205  else // find the frame for a given packet
206  {
207  int j = 0;
208  while ( PHDWORD* fp = framelist[i++] )
209  {
210  j++;
211  if ( findFramePacketId(fp, id) != ptrFailure )
212  {
215  dumpBlock(ptr, len, os);
216  return;
217  }
218  }
219  }
220 }
221 
223 {
224  // DLW: for SEQ number and code, there is no corresponding routine in Cframe.h,
225  // so I directly unpack the field. This will break if the frame format ever changes...
226  //
227  os << "Frame length: " << std::dec << getFrameLength(fp) << std::endl;
228  os << "Frame mark: " << std::hex << getFrameMark(fp) << std::dec << std::endl;
229  os << "Frame Hdr version: " << getFrameHdrVersion(fp) << std::endl;
230  os << "Frame Hdr length: " << getFrameHdrLength(fp) << std::endl ;
231  os << "Frame Status: " << getFrameStatus(fp) << std::endl;
232  os << "Frame Seq Number: " << (((*(fp+3))&0xff000000)>>24) << std::endl;
233  os << "Frame Seq Code: " << (((*(fp+3))&0x00ff0000)>>24) << std::endl;
234  os << "Frame Source id: " << getFrameSourceId(fp) << std::endl;
235  os << "Frame data type: " << getFrameDataType(fp) << std::endl;
236  os << "Frame type: " << getFrameType(fp) << std::endl;
237  os << "Frame Error Length: " << getFrameErrorLength(fp) << std::endl;
238  os << "Frame Hist Length: " << getFrameHistoryLength(fp) << std::endl;
239  os << "Frame align length: " << getFrameAlignLength(fp) << std::endl;
240  os << "Frame padding: " << getFramePadding(fp) << std::endl;
241  unsigned int i = 0;
243  for (i = 0; i< getFrameAlignLength(fp); i++ )
244  {
245  os << " - Alignment word " << SETW(2) << i << ": 0x" ;
246 
247  os.fill('0');
248  os << SETW(8) << std::hex << *p++ << std::dec << std::endl;
249  os.fill (' ');
250  }
251  os << std::endl;
252 }
253 
254 void
256 {
257  PHDWORD* ptr = findFrameErrorStart(fp);
259  UINT nerr = calcNumErrorsV1(len);
260  if ( nerr == 0 ) return;
261 
262  errorEntryV1* p = reinterpret_cast<errorEntryV1*>(ptr);
263  for (UINT i=0; i<nerr; ++i)
264  {
265  errorEntryV1& e = *p;
266  os << "ErrorEntry " << i << ": ";
267  os << "severity: " << (int) e.severity << " "
268  << "deviceType: " << (int)e.deviceType << " "
269  << "deviceId: " << std::dec << e.deviceId << " "
270  << "errorCode: " << e.errorCode << " "
271  << "detectCode: " << e.detectCode << " "
272  << "addData: (" << std::hex << e.addData[0] << "," << std::hex << e.addData[1] << ")"
273  << std::dec
274  << std::endl;
275  p++;
276  }
277  os << std::endl;
278 }
279 
280 void
281 A_Event::dumpBlock(PHDWORD* p, UINT len, OSTREAM& os, const int how)
282 {
283  if ( len == 0 ) {
284  os << " (empty)\n" << std::endl;
285  return;
286  }
287 
288  unsigned int j;
289  switch (how)
290  {
291  case (EVT_HEXADECIMAL):
292  j = 0;
293  while (1)
294  {
295  os << SETW(5) << j << " | ";
296  for ( UINT l=0; l<4; l++ )
297  {
298  if ( j >= len ) break;
299  os << std::hex << SETW(8) << p[j++] << " " ;
300  }
301  if ( j >= len ) break;
302  os << std::dec << std::endl;
303  }
304  break;
305 
306  case (EVT_DECIMAL):
307  j = 0;
308  while (1)
309  {
310  os << std::dec << SETW(5) << j << " | ";
311 
312  for ( UINT l=0; l<6; l++ )
313  {
314  os << SETW(10) << p[j++] << " ";
315  if ( j >= len ) break;
316  }
317  if ( j >= len ) break;
318  os << std::endl;
319  }
320  break;
321 
322  default:
323  break;
324  }
325  os << std::endl << std::endl;
326 
327 }
328 
329 unsigned int A_Event::getFrameEntry(const char *what, const int id, const int index) const
330 {
331 
332  PHDWORD *fp;
333  PHDWORD *pp;
334  int i = 0;
335  int j = 0;
336 
337  if (id == 0)
338  {
339 
340  while ( ( fp = framelist[i++]) )
341  {
342  return getFrameValue( what,fp,index);
343  }
344  }
345  else // find the frame for a given packet
346  {
347 
348  while ( (fp = framelist[i++]) )
349  {
350  j++;
351  if ( ( pp = findFramePacketId (fp, id) ) != ptrFailure)
352  {
353  return getFrameValue( what,fp,index);
354  }
355  }
356  }
357  return 0;
358 }
359 
360 
361 
362 
363 unsigned int A_Event::getFrameValue(const char *what, PHDWORD *fp, const unsigned int index) const
364 {
365 
366  if ( strcmp(what,"FRAMELENGTH") == 0) return getFrameLength(fp);
367  else if ( strcmp(what,"FRAMEMARK") ==0) return getFrameMark(fp);
368  else if ( strcmp(what,"FRAMEHDRVERSION") == 0) return getFrameHdrVersion(fp);
369  else if ( strcmp(what,"FRAMEHDRLENGTH") == 0) return getFrameHdrLength(fp);
370  else if ( strcmp(what,"FRAMESTATUS") == 0) return getFrameStatus(fp);
371  else if ( strcmp(what,"FRAMESOURCEID") ==0 )return getFrameSourceId(fp);
372  else if ( strcmp(what,"FRAMEDATATYPE") == 0) return getFrameDataType(fp);
373  else if ( strcmp(what,"FRAMETYPE") == 0) return getFrameType(fp);
374  else if ( strcmp(what,"FRAMEALIGNLENGTH") == 0) return getFrameAlignLength(fp);
375  else if ( strcmp(what,"FRAMEALIGNMENTWORD") == 0)
376  {
378  if ( index >= getFrameAlignLength(fp) ) return 0;
379  return p[index];
380  }
381 
382  return 0;
383 }
384 
385 
386 // getSubevent (int)
387 Packet*
388 A_Event::getPacket (const int id)
389 {
390  return getPacket(id,0);
391 }
392 
393 #if !defined(SunOS) && !defined(OSF1)
394 
396 {
397  int i = 0;
398  PHDWORD *fp;
399  PHDWORD *pp;
400 
401  if ( ! framelist )
402  {
403  errorcode = -3;
404  }
405 
406  if (errorcode) return 0;
407 
408  unsigned int pos_in_event;
409 
410  if (!hasMap)
411  {
412 
413  while ( (fp = framelist[i++]) )
414  {
415  pp = findFramePacketIndex (fp, 0);
416 
417 
418  while ( pp != ptrFailure)
419  {
420 
421  pos_in_event = ( int )( pp - ( PHDWORD *) EventData );
422 
423  // std::cout << "pos in event = " << pos_in_event <<
424  // " packet length = " << *pp << " ev. length= " << getEvtLength() << std::endl;
425 
426  if (pp && *pp > getEvtLength() - pos_in_event )
427  {
428  std::cout << "Found wrong packet length " << *pp
429  << std::hex << "(0x" << *pp << ")" << std::dec
430  << " packet Id: " << getPacketId(pp)
431  << " Event: " << getEvtSequence()
432  << " EvtLength: " << getEvtLength()
433  << " PosInEvent: " << pos_in_event
434  << std::endl;
435  errorcode =-2;
436  break;
437  }
438  if ( pp != 0 && *pp == 0)
439  {
440  std::cout << "found 0-length packet" << std::endl;
441  errorcode =-1;
442  break;
443  }
444 
445 
446  pmap[getPacketId(pp)] = pp;
447  // std::cout << "Packet id " << getPacketId(pp) << std::endl;
448 
449  pp = findNextFramePacket(fp, pp);
450 
451  }
452  }
453  hasMap = 1;
454  }
455  return 0;
456 }
457 
458 
459 Packet*
460 A_Event::getPacket (const int id, const int hitFormat)
461 {
462 
463  PHDWORD *pp;
464 
465 
466  if (!hasMap) createMap();
467  if ( errorcode) return 0;
468 
469 
470  pp = pmap[id];
471  if (!pp) return 0;
472 
473  return makePacket(pp,hitFormat);
474 }
475 
476 #else
477 
478 // the STL-Free solaris version
479 
480 Packet*
481 A_Event::getPacket (const int id, const int hitFormat)
482 {
483 
484  int i = 0;
485  PHDWORD *fp;
486  PHDWORD *pp;
487  UINT ids = id;
488  int wanted_hitformat;
489 
490  while ( fp = framelist[i++] )
491  {
492  if ( ( pp = findFramePacketId (fp, ids) ) != ptrFailure)
493  {
494  return makePacket(pp,hitFormat);
495  }
496  }
497  return 0;
498 }
499 #endif
500 
501 int
502 A_Event::getPacketList( Packet* sl[], const int ne)
503 {
504  int i = 0;
505  PHDWORD *fp;
506  PHDWORD *pp;
507 
508  if (!hasMap) createMap();
509  if ( errorcode) return 0;
510 
511  int entries = 0;
512 
513  while ( (fp = framelist[i++]) )
514  {
515 
516  pp = findFramePacketIndex (fp, 0);
517 
518  while ( pp != ptrFailure)
519  {
520  if (getPacketStructure(pp) == Unstructured)
521  {
522  sl[entries++] = makePacket(pp,0);
523  // sl[entries-1]->identify();
524 
525  }
526 
527  if (entries >= ne) return ne;
528  if ( (pp = findNextFramePacket(fp, pp)) == ptrFailure)
529  {
530  break;
531  }
532  if (*pp > getEvtLength())
533  {
534  std::cout << "Found wrong packet length " << *pp << std::endl;
535  break;
536  }
537  if ( pp != 0 && *pp == 0)
538  {
539  std::cout << "found 0-length packet" << std::endl;
540  // PHDWORD *x = pp - 10;
541  // std::cout << "--------------------------" << std::endl;
542  // for (i=0; i< 20; i++)
543  // {
544  // std::cout << i << " " << x << " " << std::hex <<*x++ << std::dec << std::endl;
545  // }
546  // std::cout << "--------------------------" << std::endl;
547 
548 
549  break;
550  }
551  }
552  }
553  return entries;
554 }
555 
556 
557 
558 
559 Packet *A_Event::makePacket(PHDWORD *pp, const int hitFormat)
560 {
561 
562  int wanted_hitformat;
563 
564  if (getPacketStructure(pp) != Unstructured) return 0;
565 
566 
567  if (hitFormat) wanted_hitformat = hitFormat;
568  else wanted_hitformat = getUnstructPacketHitFormat(pp);
569 
570  switch (wanted_hitformat)
571  {
572 
573  // pbsc "32 channel format"
574 
575  case 50400:
576  case IDHBD_FPGA:
577  case IDHBD_FPGA0SUP:
578  case IDFOCAL_FPGATEST:
579  return new Packet_hbd_fpga(pp);
580  break;
581 
582  case IDHBD_FPGASHORT:
583  case IDHBD_FPGASHORT0SUP:
584  return new Packet_hbd_fpgashort(pp);
585  break;
586 
587  case IDFVTX_DCM0:
588  case IDFVTX_SIM:
589  return new Packet_fvtx_dcm0(pp);
590  break;
591 
592  case IDCDEVPOLARIMETER:
593  return new Packet_cdevpolarimeter(pp);
594  break;
595 
597  return new Packet_cdevpoltarget(pp);
598  break;
599 
600  case IDCDEVIR:
601  return new Packet_cdevir(pp);
602  break;
603 
604  case IDCDEVWCMHISTORY:
605  return new Packet_cdevwcm(pp);
606  break;
607 
608  case IDCDEVBPM:
609  return new Packet_cdevbpm(pp);
610  break;
611 
612  case IDCDEVDVM:
613  return new Packet_cdevdvm(pp);
614  break;
615 
616  case IDCDEVRING:
617  return new Packet_cdevring(pp);
618  break;
619 
620  case IDCDEVRINGPOL:
621  return new Packet_cdevring(pp);
622  break;
623 
624  case IDCDEVRINGFILL:
625  return new Packet_cdevring(pp);
626  break;
627 
628  case IDCDEVRINGNOPOL:
629  return new Packet_cdevringnopol(pp);
630  break;
631 
632  case IDCDEVBUCKETS:
633  return new Packet_cdevbuckets(pp);
634  break;
635 
636  //mlp 10/27/03 added this - the SIS is a straight array of numbers.
637  case IDCDEVSIS:
638  return new Packet_id4evt(pp);
639  break;
640 
641  case IDCDEVMADCH:
642  return new Packet_cdevmadch(pp);
643  break;
644 
645  case ID4SCALER:
646  return new Packet_id4scaler(pp);
647  break;
648 
649  case IDDIGITIZER_12S:
650  case IDDIGITIZER_16S:
651  case IDDIGITIZER_31S:
652  return new Packet_iddigitizerv2(pp);
653  break;
654 
655  case IDGL1P:
656  return new Packet_gl1p(pp);
657  break;
658 
659  case IDGL1_EVCLOCK:
660  return new Packet_gl1_evclocks(pp);
661  break;
662 
663  case IDGL1PSUM:
664  case IDGL1PSUMOBS:
665  return new Packet_gl1psum(pp);
666  break;
667 
668  case ID4EVT:
669  return new Packet_id4evt(pp);
670  break;
671 
672  case ID2EVT:
673  return new Packet_id2evt(pp);
674  break;
675 
676  case IDCSTR:
677  return new Packet_idcstr(pp);
678  break;
679 
680  case IDSTARSCALER:
681  return new Packet_starscaler(pp);
682  break;
683 
684  case IDCDEVDESCR:
685  return new Packet_idcdevdescr(pp);
686  break;
687 
688 
689  default:
690  switch (getUnstructPacketWordSize (pp))
691  {
692  case 1:
693  return new Packet_w1(pp);
694  break;
695  case 2:
696  return new Packet_w2(pp);
697  break;
698  case 4:
699  return new Packet_w4(pp);
700  break;
701  default:
702  return new Packet_w4(pp);
703  }
704  }
705 
706  return 0;
707 }
708 
709 
710 
711 void
713 {
714  os << " -- Event " << EventData->evt_sequence;
715 
716  os << " Run: " << SETW(5) << EventData->run_number;
717 
718  os << " length: " << SETW(5) <<EventData->evt_length;
719 
720  os << " frames: " << SETW(3) << NumberFrames;
721 
722  os << " type: " << SETW(2) << EventData->evt_type ;
723 
724  os << " (" << get_evt_mnemonic(EventData->evt_type );
725 
726 
727  if ( ( EventData->evt_type & CORRUPTEVENTMASK ) )
728  {
729  os << " *Corrupt* ";
730  }
731  os << ") ";
732 
733  time_t x = getTime();
734  os << x;
735 
736  os << std::endl;
737 
738 };
739 
740 
741 
742 int
743 A_Event::existPacket (const int id)
744 {
745 #if !defined(SunOS) && !defined(OSF1)
746 
747  PHDWORD *pp;
748 
749  if (!hasMap)
750  {
751  createMap();
752  }
753 
754  pp = pmap[id];
755 
756  if (!pp)
757  {
758  return 0;
759  }
760  return 1;
761 #else
762  return 0;
763 #endif
764 }
765 
766 
767 
768 // the Copy routine
769 int
770 A_Event::Copy (int * array, const unsigned int length, int *nw, const char *what)
771 {
772  if (length< getEvtLength() )
773  {
774  *nw = 0;
775  return -1;
776  }
777  char *to = (char *) array;
778  char *from;
779  unsigned int l;
780  if ( strcmp (what, "DATA") ==0 )
781  {
782  from= (char *) &EventData->data[0];
784  }
785  else
786  {
787  from= (char *) EventData;
788  l = getEvtLength();
789  }
790  // for (i=0; i<l ; i++) *to++ = *from++;
791  //
792  *nw = l;
793  memcpy (to, from, l*4);
794  return 0;
795 }
796 
798 {
799  if (is_data_type) return -1;
800 
801  PHDWORD *tmp;
802 
803  tmp = new PHDWORD[getEvtLength()];
804  PHDWORD *from= (PHDWORD *) EventData;
805  PHDWORD *to=tmp;
806  for (unsigned int k=0; k< getEvtLength(); k++)
807  {
808  *to++ = *from++;
809  }
810 
811  delete [] framelist;
812  EventData = (evtdata_ptr) tmp;
813  updateFramelist();
814 
815  is_data_type = 1;
816  pmap.clear();
817  hasMap = 0;
818  return 0;
819 
820 }
821 
823 {
824  if (is_data_type) return 0;
825  else return 1;
826 }
827 
828 
830 {
831  // go through the data and see how may,
832  // if any, frames we have got.
833  int max_index = EventData->evt_length - EVTHEADERLENGTH;
834  int index = 0;
835  int number_of_frames = 0;
836  int flength;
837  int cont;
838  int i;
839  // count the number of frames, and allocate a properly-sized vector
840  // with pointers to it.
841  cont = 1;
842  while (index < max_index && cont)
843  {
844  PHDWORD *f = &EventData->data[index];
845  // so here we believe that we point to a frame start
846  if ( validFrameHdr(f) )
847  {
848  number_of_frames++;
849  flength = getFrameLength (f);
850  index += flength;
851  }
852  else
853  {
854  COUT << "invalid frame header, frame nr " << number_of_frames << " index = " << index << std::endl; //mlpd
855  COUT << " -- Event*" << EventData->evt_sequence;
856 
857  COUT << " Run: " << SETW(5) << EventData->run_number;
858 
859  COUT << " length: " << SETW(5) <<EventData->evt_length;
860 
861  COUT << " type: " << SETW(2) << EventData->evt_type
862  << " (" << get_evt_mnemonic(EventData->evt_type) << ") ";
863  COUT << std::endl;
864 
865  for (i=0; i< 8; i++)
866  COUT << i << " " << std::hex << f[i] << std::dec << std::endl;
867 
868  return -1;
869 
870  // COUT << "I will continue anyway" << std::endl;
871  // cont = 0;
872  }
873  }
874 
875  // ok, so many frames. get the vector.
876  framelist = new PHDWORD *[number_of_frames +1];
877 
878  // NumberFrames is a class data member.
879  NumberFrames = number_of_frames;
880 
881  // now we go through once more and remember the pointers to where the
882  // frames start
883  index = 0;
884  int ifn = 0;
885  while (index < max_index && cont)
886  {
887  PHDWORD *f = &EventData->data[index];
888  if ( validFrameHdr(f) )
889  {
890  framelist[ifn++] = f;
891  flength = getFrameLength (f);
892  index += flength;
893  }
894  else
895  {
896  COUT << "invalid frame header, frame nr " << ifn << " index = " << index << std::endl; //mlpd
897  for (i=0; i< 8; i++)
898  COUT << i << " " << std::hex << f[i] << std::dec << std::endl;
899 
900 
901  cont = 0;
902  }
903  }
904 
905  //we terminate the list of frames with a 0.
906  framelist[ifn] = 0;
907  return 0;
908 }
909 
911 {
912 #if !defined(SunOS) && !defined(OSF1)
913  createMap();
914 #endif
915  return errorcode;
916 }