Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:46

0001 #include "oncsBuffer.h"
0002 #include "oncsStructures.h"
0003 #include "oncsSubConstants.h"
0004 
0005 // the constructor first ----------------
0006 oncsBuffer::oncsBuffer (PHDWORD *array , const PHDWORD length )
0007 {
0008   bptr =  (buffer_ptr) array;
0009   data_ptr = &(bptr->data[0]);
0010   max_length = length;
0011   current_index = 0;
0012 
0013   if (bptr->ID != ONCSBUFFERID && bptr->ID != PRDFBUFFERID) // PRDFBUFFERID is for legacy data 
0014   {
0015     //    COUT << " will swap the buffer " << std::endl;
0016     unsigned int id = i4swap(bptr->ID);
0017     if (id != ONCSBUFFERID && id != PRDFBUFFERID ) 
0018       {
0019     COUT << " wrong buffer" << std::endl;
0020     return;
0021       }
0022     if ( buffer_swap()) 
0023       {
0024     COUT << "problem in buffer swap" << std::endl;
0025       }
0026   }
0027   buffer_size = array[0];
0028   
0029 }
0030 
0031 
0032 // ---------------------------------------------------------
0033 int oncsBuffer::getBufferSequence() const
0034 {
0035   if ( !bptr) return 0;
0036   return bptr->Bufseq;
0037 }
0038 
0039 
0040 int oncsBuffer::buffer_swap()
0041 {
0042 
0043   unsigned int i;
0044   unsigned int evtindex, sevtindex;
0045   oncsevtdata_ptr evtptr;
0046   subevtdata_ptr sevtptr;
0047 
0048   //   swap the buffer header
0049   bptr->Length =  i4swap ( bptr->Length);
0050   bptr->ID =  i4swap ( bptr->ID);
0051   bptr->Bufseq =  i4swap ( bptr->Bufseq);
0052   bptr->Runnr =  i4swap ( bptr->Runnr);
0053 
0054   evtindex = 0;
0055 
0056   while (evtindex < bptr->Length - BUFFERHEADERLENGTH)
0057     {
0058       //  COUT << "evt index " << evtindex << std::endl;
0059 
0060       // map event header on data
0061       evtptr = (  oncsevtdata_ptr ) &bptr->data[evtindex];
0062 
0063       evtptr->evt_length = i4swap(evtptr->evt_length);
0064       evtptr->evt_type = i4swap(evtptr->evt_type);
0065 
0066       // see if we have got the end of buffer event
0067       if (evtptr->evt_length == 2 && evtptr->evt_type ==0) break;
0068 
0069       // swap the rest of the event header
0070       for (i=2; i<EVTHEADERLENGTH; i++) 
0071     bptr->data[evtindex+i] = i4swap(bptr->data[evtindex+i]);
0072 
0073       // mark first subevent
0074       sevtindex = 0;
0075 
0076       while (sevtindex < evtptr->evt_length - EVTHEADERLENGTH)
0077     {
0078       // map subevent structure on data
0079       sevtptr = (subevtdata_ptr) &evtptr->data[sevtindex];
0080 
0081       // swap buffer header
0082       sevtptr->sub_length = i4swap(sevtptr->sub_length);
0083       sevtptr->sub_id = i2swap(sevtptr->sub_id);
0084       sevtptr->sub_type = i2swap(sevtptr->sub_type);
0085       sevtptr->sub_decoding = i2swap(sevtptr->sub_decoding);
0086       sevtptr->sub_padding = i2swap(sevtptr->sub_padding);
0087       sevtptr->reserved[0] = i2swap(sevtptr->reserved[0]);
0088       sevtptr->reserved[1] = i2swap(sevtptr->reserved[1]);
0089       
0090       // now swap the data depending on the type
0091       int *p = &sevtptr->data;
0092 
0093       switch (sevtptr->sub_type)
0094         {
0095         case 1: break;
0096 
0097         case 2: 
0098           for (i=0; i<sevtptr->sub_length - SEVTHEADERLENGTH; i++)
0099         {
0100           *p = i22swap(*p);
0101           p++;
0102         }
0103           break;
0104 
0105         case 4:
0106           for (i=0; i<sevtptr->sub_length - SEVTHEADERLENGTH; i++)
0107         {
0108           *p = i4swap(*p);
0109           p++;
0110         }
0111           break;
0112       
0113         default: 
0114           COUT << "unknown data type " << sevtptr->sub_type << std::endl;
0115           break;
0116         }
0117       sevtindex += sevtptr->sub_length;
0118     }
0119       evtindex += evtptr->evt_length;
0120     }
0121   return 0;
0122 }
0123 
0124 // ---------------------------------------------------------
0125 int oncsBuffer::i4swap(const int in)
0126 {
0127   union 
0128   {
0129     int i4;
0130     char c[4];
0131   } i,o;
0132 
0133   i.i4 = in;
0134   o.c[0] = i.c[3];
0135   o.c[1] = i.c[2];
0136   o.c[2] = i.c[1];
0137   o.c[3] = i.c[0];
0138   return o.i4;
0139 }
0140 // ---------------------------------------------------------
0141 int oncsBuffer::i22swap(const int in)
0142 {
0143   union 
0144   {
0145     int i4;
0146     char c[4];
0147   } i,o;
0148 
0149   i.i4 = in;
0150   o.c[0] = i.c[1];
0151   o.c[1] = i.c[0];
0152   o.c[2] = i.c[3];
0153   o.c[3] = i.c[2];
0154   return o.i4;
0155 }
0156 
0157 // ---------------------------------------------------------
0158 short oncsBuffer::i2swap(const  short in)
0159 {
0160   union 
0161   {
0162     short i2;
0163     char c[2];
0164   } i,o;
0165 
0166   i.i2 = in;
0167   o.c[0] = i.c[1];
0168   o.c[1] = i.c[0];
0169 
0170   return o.i2;
0171 }
0172 
0173 
0174 // ---------------------------------------------------------
0175 Event * oncsBuffer::getEvent()
0176 {
0177   if ( current_index < 0 ) return 0;
0178 
0179   Event *evt;
0180   evt =  new oncsEvent( &bptr->data[current_index]);
0181   evt->setOriginBuffer(getBufferSequence());
0182 
0183   
0184   int l =  evt->getEvtLength();
0185   if ( l<= 0) return 0;
0186   current_index += evt->getEvtLength();
0187 
0188   // now is the new index pointing outside the allocated memory?
0189   //  if (current_index < 0 || current_index > BUFFERSIZE)
0190   if (current_index < 0 || current_index >= buffer_size)
0191     {
0192       //COUT << "end of buffer r1" << current_index << std::endl;
0193       current_index = -1;
0194       return evt;
0195     }
0196 
0197   // are we pointing beyond the logical end of buffer?
0198   if (current_index > buffer_size/4 )
0199     {
0200       //COUT << "end of buffer r2" << std::endl;
0201       current_index = -1;
0202       return evt;
0203     }
0204 
0205   // are we pointing to an end-of-buffer event? 
0206   if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
0207     {
0208       //COUT << "end of buffer r3" << std::endl;
0209       current_index = -1;
0210       return evt;
0211     }
0212 
0213   // none of the above, just return
0214   return evt;
0215 
0216 }
0217 
0218