Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 //
0002 // oncsEventIterator   mlp 4/19/1997
0003 //
0004 // this iterator reads events froma data file. 
0005 
0006 
0007 
0008 #include "oncsEventiterator.h"
0009 #include <stdio.h>
0010 #include "oncsEvent.h"
0011 #include <stddef.h>
0012 #include <string.h>
0013 #include <sys/types.h>
0014 #include <sys/stat.h>
0015 #include <fcntl.h>
0016 #include <unistd.h>
0017 
0018 
0019 // there are two similar constructors, one with just the
0020 // filename, the other with an additional status value
0021 // which is non-zero on return if anything goes wrong. 
0022 
0023 
0024 oncsEventiterator::~oncsEventiterator()
0025 {
0026      if (fd) close (fd);
0027      if (thefilename != NULL) delete [] thefilename;
0028      if (bp != NULL ) delete [] bp;
0029      if (bptr != NULL ) delete bptr;
0030 }  
0031 
0032 
0033 oncsEventiterator::oncsEventiterator(const char *filename)
0034 {
0035   fd  = open (filename, O_RDONLY | O_LARGEFILE);
0036   bptr = 0;
0037   bp = 0;
0038   allocatedsize = 0;
0039   if (fd > 0) 
0040     {
0041       thefilename = new char[strlen(filename)+1];
0042       strcpy (thefilename, filename);
0043       last_read_status = 0;
0044       current_index = 0;
0045     }
0046   else
0047     last_read_status = 1;
0048 
0049 }  
0050 
0051 oncsEventiterator::oncsEventiterator(const char *filename, int &status)
0052 {
0053   fd  = open (filename, O_RDONLY | O_LARGEFILE);
0054   bptr = 0;
0055   bp = 0;
0056   allocatedsize = 0;
0057   if (fd >0 ) 
0058     {
0059       thefilename = new char[strlen(filename)+1];
0060       strcpy (thefilename, filename);
0061       status = 0;
0062       last_read_status = 0;
0063       current_index = 0;
0064     }
0065   else
0066     {
0067       status = 1;
0068       last_read_status = 1;
0069     }
0070 }  
0071 
0072 void  
0073 oncsEventiterator::identify (OSTREAM &os) const
0074 { 
0075   os << getIdTag() << std::endl;
0076 
0077 };
0078 
0079 const char * oncsEventiterator::getIdTag () const
0080 { 
0081   static char line[180];
0082   strcpy (line, " -- oncsEventiterator reading from ");
0083   strcat (line, thefilename);
0084   return line;
0085 };
0086 
0087 
0088 // and, finally, the only non-constructor member function to
0089 // retrieve events from the iterator.
0090 
0091 Event * oncsEventiterator::getNextEvent()
0092 {
0093   Event *evt = 0;
0094 
0095 
0096   // if we had a read error before, we just return
0097   if (last_read_status) return NULL;
0098 
0099   // see if we have a buffer to read
0100   if (bptr == 0) 
0101     {
0102       if ( (last_read_status = read_next_buffer()) !=0 )
0103     {
0104       return NULL;
0105     }
0106     }
0107 
0108   while (last_read_status == 0)
0109     {
0110       if (bptr) evt =  bptr->getEvent();
0111       if (evt) return evt;
0112 
0113       last_read_status = read_next_buffer();
0114     }
0115 
0116   return NULL;
0117 
0118 }
0119 
0120 // -----------------------------------------------------
0121 // this is a private function to read the next buffer
0122 // if needed. 
0123 
0124 int oncsEventiterator::read_next_buffer()
0125 {
0126   if (bptr) 
0127     {
0128       delete bptr;
0129       bptr = 0;
0130     }
0131 
0132   //  COUT << "reading next buffer" << std::endl; 
0133 
0134   // set the pointer to char to the destination buffer
0135   char *cp = (char *) initialbuffer;
0136 
0137   unsigned int xc;
0138 
0139   // read the first record
0140   xc = read ( fd, cp, 8192);
0141 
0142   // error of EoF?
0143   if ( xc < 8192 ) return -1;
0144 
0145   // get the length into a dedicated variable
0146   if (initialbuffer[1] == ONCSBUFFERID || initialbuffer[1] == PRDFBUFFERID ) // we check for both for legacy data
0147     {
0148       buffer_size = initialbuffer[0];
0149     }
0150   else
0151     {
0152       unsigned int id = oncsBuffer::i4swap(initialbuffer[1]);
0153       if (id == ONCSBUFFERID || id == PRDFBUFFERID ) // we check for both for legacy data
0154     {
0155       buffer_size = oncsBuffer::i4swap(initialbuffer[0]);
0156     }
0157       else
0158     {
0159       return 1;
0160     }
0161     }
0162   int i;
0163   if (bp) 
0164     {
0165       if  (buffer_size > allocatedsize*4)
0166     {
0167       delete [] bp;
0168       i = (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE;
0169       allocatedsize = i * 2048;
0170       bp = new int[allocatedsize];
0171     }
0172     }
0173   else
0174     {
0175       i = (buffer_size + BUFFERBLOCKSIZE) /BUFFERBLOCKSIZE;
0176       allocatedsize = i * BUFFERBLOCKSIZE/4;
0177       bp = new int[allocatedsize];
0178     }
0179 
0180   // for (i = 0; i<2048; i++ ) bp[i] = initialbuffer[i];
0181   memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
0182 
0183   unsigned int read_so_far =  BUFFERBLOCKSIZE;
0184   
0185   cp = (char *) bp;
0186 
0187   // and update the destination buffer pointer
0188   cp += BUFFERBLOCKSIZE;
0189 
0190   // we calculate how many BUFFERBLOCKSIZE-sized records we need to read
0191   // we have already one, so this is the number of records -1.
0192   // normally we would round up  (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE
0193   int records_to_read =  (buffer_size -1) /BUFFERBLOCKSIZE;
0194   unsigned int bytes_to_read   =  records_to_read * BUFFERBLOCKSIZE;
0195   
0196   xc = read ( fd, cp, bytes_to_read);
0197   if ( xc < bytes_to_read )
0198     {
0199       COUT << "error in buffer, salvaging" << std::endl;
0200       bp[0] = read_so_far + xc; 
0201     }
0202 
0203   // and initialize the current_index to be the first event
0204   bptr = new oncsBuffer ( (PHDWORD *) bp, (PHDWORD) allocatedsize );
0205   return 0;
0206 }
0207