ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oncsEventiterator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file oncsEventiterator.cc
1 //
2 // oncsEventIterator mlp 4/19/1997
3 //
4 // this iterator reads events froma data file.
5 
6 
7 #include "oncsEventiterator.h"
8 #include <stdio.h>
9 #include "oncsEvent.h"
10 #include <stddef.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 
17 
18 // there are two similar constructors, one with just the
19 // filename, the other with an additional status value
20 // which is non-zero on return if anything goes wrong.
21 
22 
24 {
25  if (fd) close (fd);
26  if (thefilename != NULL) delete [] thefilename;
27  if (bp != NULL ) delete [] bp;
28  if (bptr != NULL ) delete bptr;
29 }
30 
31 
33 {
34  fd = open (filename, O_RDONLY | O_LARGEFILE);
35  bptr = 0;
36  bp = 0;
37  allocatedsize = 0;
38  if (fd > 0)
39  {
40  thefilename = new char[strlen(filename)+1];
41  strcpy (thefilename, filename);
42  last_read_status = 0;
43  current_index = 0;
44  }
45  else
46  last_read_status = 1;
47 
48 }
49 
51 {
52  fd = open (filename, O_RDONLY | O_LARGEFILE);
53  bptr = 0;
54  bp = 0;
55  allocatedsize = 0;
56  if (fd >0 )
57  {
58  thefilename = new char[strlen(filename)+1];
59  strcpy (thefilename, filename);
60  status = 0;
61  last_read_status = 0;
62  current_index = 0;
63  }
64  else
65  {
66  status = 1;
67  last_read_status = 1;
68  }
69 }
70 
71 void
73 {
74  os << getIdTag() << std::endl;
75 
76 };
77 
78 const char * oncsEventiterator::getIdTag () const
79 {
80  static char line[180];
81  strcpy (line, " -- oncsEventiterator reading from ");
82  strcat (line, thefilename);
83  return line;
84 };
85 
86 
87 // and, finally, the only non-constructor member function to
88 // retrieve events from the iterator.
89 
91 {
92  Event *evt = 0;
93 
94 
95  // if we had a read error before, we just return
96  if (last_read_status) return NULL;
97 
98  // see if we have a buffer to read
99  if (bptr == 0)
100  {
101  if ( (last_read_status = read_next_buffer()) !=0 )
102  {
103  return NULL;
104  }
105  }
106 
107  while (last_read_status == 0)
108  {
109  if (bptr) evt = bptr->getEvent();
110  if (evt) return evt;
111 
113  }
114 
115  return NULL;
116 
117 }
118 
119 // -----------------------------------------------------
120 // this is a private function to read the next buffer
121 // if needed.
122 
124 {
125  int ip = 8192;
126  if (bptr)
127  {
128  delete bptr;
129  bptr = 0;
130  }
131 
132  // COUT << "reading next buffer" << std::endl;
133 
134  // set the pointer to char to the destination buffer
135  char *cp = (char *) initialbuffer;
136 
137  unsigned int xc;
138 
139  // read the first record
140  xc = read ( fd, cp, 8192);
141 
142  // error of EoF?
143  if ( xc < 8192 ) return -1;
144 
145  // get the length into a dedicated variable
146  if (initialbuffer[1] == ONCSBUFFERID || initialbuffer[1] == PRDFBUFFERID ) // we check for both for legacy data
147  {
149  }
150  else
151  {
152  unsigned int id = oncsBuffer::i4swap(initialbuffer[1]);
153  if (id == ONCSBUFFERID || id == PRDFBUFFERID ) // we check for both for legacy data
154  {
156  }
157  else
158  {
159  return 1;
160  }
161  }
162  int i;
163  if (bp)
164  {
165  if (buffer_size > allocatedsize*4)
166  {
167  delete [] bp;
168  i = (buffer_size +8191) /8192;
169  allocatedsize = i * 2048;
170  bp = new int[allocatedsize];
171  }
172  }
173  else
174  {
175  i = (buffer_size +8191) /8192;
176  allocatedsize = i * 2048;
177  bp = new int[allocatedsize];
178  }
179  for (i = 0; i<2048; i++ ) bp[i] = initialbuffer[i];
180 
181  cp = (char *) bp;
182 
183  // and update the destination buffer pointer
184  cp += 8192;
185 
186  // now we read records until the whole buffer is read
187  while ( ip < buffer_size)
188  {
189  // read the next record
190  xc = read ( fd, cp, BUFFERBLOCKSIZE);
191  if ( xc < BUFFERBLOCKSIZE ) return -1;
192 
193  // update the pointer and byte count
194  cp += 8192;
195  ip += 8192;
196  }
197 
198  // and initialize the current_index to be the first event
199  bptr = new oncsBuffer ( (PHDWORD *) bp, (PHDWORD) allocatedsize );
200  return 0;
201 }
202