Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include <prdfBuffer.h>
0002 #include <EventTypes.h>
0003 #include <A_Event.h>
0004 #include <Cframe.h>
0005 #include <stdio.h>
0006 
0007 // the constructor first ----------------
0008 
0009 prdfBuffer::prdfBuffer ()
0010 {
0011   is_good =1;
0012 }
0013 
0014 prdfBuffer::prdfBuffer (PHDWORD *array , const int length )
0015 {
0016   is_good =1;
0017   bptr =  (buffer_ptr) array;
0018   data_ptr = &(bptr->data[0]);
0019   max_length = length;
0020   current_index = 0;
0021 
0022   if (bptr->ID != BUFFERMARKER) 
0023   {
0024     //COUT << " will swap the buffer " << std::endl;
0025     unsigned int id = u4swap(bptr->ID);
0026     if (id != BUFFERMARKER) 
0027       {
0028     COUT << " wrong buffer" << std::endl;
0029     is_good =0;
0030     return;
0031       }
0032     if ( buffer_swap())
0033       {
0034     COUT << "problem in buffer swap" << std::endl;
0035     is_good = 0;
0036       }
0037   }
0038   buffer_size = array[0];
0039   //COUT << " done... " << std::endl;
0040  
0041 }
0042 
0043 
0044 prdfBuffer::~prdfBuffer()
0045 {}
0046 
0047 int prdfBuffer::buffer_swap()
0048 {
0049  
0050   unsigned int i;
0051   unsigned int evtindex, frameindex;
0052   evtdata_ptr evtptr;
0053   PHDWORD *frameptr;
0054 
0055   //   swap the buffer header
0056   bptr->Length =  i4swap ( bptr->Length);
0057   bptr->ID =  i4swap ( bptr->ID);
0058   bptr->Bufseq =  i4swap ( bptr->Bufseq);
0059   bptr->Runnr =  i4swap ( bptr->Runnr);
0060 
0061   evtindex = 0;
0062 
0063   while (evtindex*4  < bptr->Length - BUFFERHEADERLENGTH)
0064     {
0065       //      COUT << "evt index " << evtindex << "  buffer length = " <<   bptr->Length << std::endl;
0066 
0067       // map event header on data
0068       evtptr = (  evtdata_ptr ) &bptr->data[evtindex];
0069 
0070       evtptr->evt_length = i4swap(evtptr->evt_length);
0071       evtptr->evt_type = i4swap(evtptr->evt_type);
0072 
0073       int written_length = (bptr->Length  + 8191)/8192;
0074       written_length *= 8192;
0075 
0076       if ( ( evtptr->evt_length + evtindex)*4 > written_length - BUFFERHEADERLENGTH  )
0077     {
0078       std::cout << "Error: next event exceeds buffer length " << bptr->Length 
0079             << " current index " << evtindex 
0080             << "next event length " <<  evtptr->evt_length << std::endl;
0081  
0082       // shit. that's wrong. terminate the buffer here.
0083       bptr->data[evtindex] = 2;
0084       bptr->data[evtindex] = 0;
0085       return -1;
0086     }
0087 
0088       // see if we have got the end of buffer event
0089       if (evtptr->evt_length == 2 && evtptr->evt_type ==0) break;
0090 
0091       // swap the rest of the event header
0092       for (i=2; i<EVTHEADERLENGTH; i++) 
0093     bptr->data[evtindex+i] = i4swap(bptr->data[evtindex+i]);
0094 
0095       // mark first subevent
0096       frameindex = 0;
0097 
0098       int fl;
0099       
0100       while (frameindex < evtptr->evt_length - EVTHEADERLENGTH)
0101     {
0102       // here we have a frame
0103       frameptr =  &evtptr->data[frameindex];
0104       i = frame_swap(frameptr, evtptr->evt_length - EVTHEADERLENGTH );   /* byte swap if wrong endianism */
0105       if ( i )
0106         {
0107           evtptr->evt_type |= CORRUPTEVENTMASK;  
0108           //  COUT << "about to call getFrameLength " << std::endl;
0109           break;
0110         }
0111       fl =  getFrameLength(frameptr);
0112       //  COUT << "framelength " << fl  << "  frameindex = " << frameindex << "  evt length = " <<  evtptr->evt_length << std::endl;
0113 
0114       if (fl <=0 )
0115         {
0116           break;
0117         }
0118       frameindex += fl;
0119     }
0120       evtindex += evtptr->evt_length;
0121       if ( evtptr->evt_length ==0) 
0122     {
0123       COUT << "0-length event found at " << evtindex << "  " << bptr->Length << std::endl; 
0124       return -1;
0125     }
0126     }
0127   return 0;
0128 }
0129 
0130 
0131 int prdfBuffer::getBufferSequence() const
0132 {
0133   if ( !bptr) return 0;
0134   return bptr->Bufseq;
0135 }
0136 
0137 int prdfBuffer::frame_swap(PHDWORD * fp, const int eventlength)
0138 {
0139   int swapped_length = i4swap(*fp);
0140   if ( swapped_length > eventlength ||  swapped_length <0 )
0141     {
0142       //      std::cout << __FILE__ << "  " << __LINE__ << " corrupt frame length " << std::endl;
0143       return -1;
0144     }
0145   int i;
0146   for ( i = 0; i < swapped_length; i++)
0147     {
0148       fp[i] = i4swap(fp[i]);
0149     }
0150 
0151   return 0;
0152 }
0153 
0154 
0155 // ---------------------------------------------------------
0156 Event * prdfBuffer::getEvent()
0157 
0158 {
0159 
0160   if ( current_index < 0 ) return 0;
0161   if ( ! is_good ) return 0;
0162  
0163   Event *evt;
0164   evt =  0;
0165 
0166   // now is the new index pointing outside the allocated memory?
0167   if (current_index < 0 || current_index > max_length)
0168     {
0169       //COUT << "end of buffer r0 " << current_index << std::endl;
0170       current_index = -1;
0171       return evt;
0172     }
0173 
0174 
0175   int len = bptr->data[current_index];
0176 
0177   // maybe there is something wrong with it?
0178   if (len <= 0) 
0179     {
0180       //COUT << "end of buffer r1 " << std::endl;
0181       current_index = -1;
0182       return evt;
0183     }
0184 
0185   //  current_index += len;
0186 
0187   // are we pointing beyond the logical end of buffer?
0188   if (current_index >= (buffer_size/4) -6 )  //6 is 2 end-of-buffer + 4 buffer header
0189     {
0190       //COUT << "end of buffer r2 " << std::endl;
0191       current_index = -1;
0192       return evt;
0193     }
0194 
0195   // are we pointing to an end-of-buffer event? 
0196   if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
0197     {
0198       //COUT << "end of buffer r3 " << std::endl;
0199       current_index = -1;
0200       return evt;
0201     }
0202 
0203   // none of the above, just return
0204   evt =  new A_Event( &bptr->data[current_index]);
0205   evt->setOriginBuffer(getBufferSequence());
0206   current_index += len;
0207   return evt;
0208 
0209 }
0210 
0211 // ---------------------------------------------------------
0212 int * prdfBuffer::getEventData()
0213 
0214 {
0215 
0216   if ( current_index < 0 ) return 0;
0217  
0218 
0219   int *evtData = 0;
0220 
0221   // now is the new index pointing outside the allocated memory?
0222   if (current_index < 0 || current_index > max_length)
0223     {
0224       //COUT << "end of buffer r1 " << current_index << std::endl;
0225       current_index = -1;
0226       return evtData;
0227     }
0228 
0229 
0230   int len = bptr->data[current_index];
0231 
0232   // maybe there is something wrong with it?
0233   if (len <= 0) 
0234     {
0235       current_index = -1;
0236       return evtData;
0237     }
0238 
0239 
0240   // are we pointing beyond the logical end of buffer?
0241   if (current_index >= (buffer_size/4) -6 )  //6 is 2 end-of-buffer + 4 buffer header
0242     {
0243       //COUT << "end of buffer r2 " << std::endl;
0244       current_index = -1;
0245       return evtData;
0246     }
0247 
0248   // are we pointing to an end-of-buffer event? 
0249   if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
0250     {
0251       //COUT << "end of buffer r3 " << std::endl;
0252       current_index = -1;
0253       return evtData;
0254     }
0255 
0256   // none of the above, just return
0257   evtData = (int *) &bptr->data[current_index];
0258   current_index += len;
0259   return evtData;
0260 
0261 }
0262 
0263