Analysis Software
Documentation for sPHENIX simulation 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 
8 #include "oncsEventiterator.h"
9 #include <stdio.h>
10 #include "oncsEvent.h"
11 #include <stddef.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 
18 
19 // there are two similar constructors, one with just the
20 // filename, the other with an additional status value
21 // which is non-zero on return if anything goes wrong.
22 
23 
25 {
26  if (fd) close (fd);
27  if (thefilename != NULL) delete [] thefilename;
28  if (bp != NULL ) delete [] bp;
29  if (bptr != NULL ) delete bptr;
30 }
31 
32 
34 {
35  fd = open (filename, O_RDONLY | O_LARGEFILE);
36  bptr = 0;
37  bp = 0;
38  allocatedsize = 0;
39  if (fd > 0)
40  {
41  thefilename = new char[strlen(filename)+1];
42  strcpy (thefilename, filename);
43  last_read_status = 0;
44  current_index = 0;
45  }
46  else
47  last_read_status = 1;
48 
49 }
50 
52 {
53  fd = open (filename, O_RDONLY | O_LARGEFILE);
54  bptr = 0;
55  bp = 0;
56  allocatedsize = 0;
57  if (fd >0 )
58  {
59  thefilename = new char[strlen(filename)+1];
60  strcpy (thefilename, filename);
61  status = 0;
62  last_read_status = 0;
63  current_index = 0;
64  }
65  else
66  {
67  status = 1;
68  last_read_status = 1;
69  }
70 }
71 
72 void
74 {
75  os << getIdTag() << std::endl;
76 
77 };
78 
79 const char * oncsEventiterator::getIdTag () const
80 {
81  static char line[180];
82  strcpy (line, " -- oncsEventiterator reading from ");
83  strcat (line, thefilename);
84  return line;
85 };
86 
87 
88 // and, finally, the only non-constructor member function to
89 // retrieve events from the iterator.
90 
92 {
93  Event *evt = 0;
94 
95 
96  // if we had a read error before, we just return
97  if (last_read_status) return NULL;
98 
99  // see if we have a buffer to read
100  if (bptr == 0)
101  {
102  if ( (last_read_status = read_next_buffer()) !=0 )
103  {
104  return NULL;
105  }
106  }
107 
108  while (last_read_status == 0)
109  {
110  if (bptr) evt = bptr->getEvent();
111  if (evt) return evt;
112 
114  }
115 
116  return NULL;
117 
118 }
119 
120 // -----------------------------------------------------
121 // this is a private function to read the next buffer
122 // if needed.
123 
125 {
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;
169  allocatedsize = i * 2048;
170  bp = new int[allocatedsize];
171  }
172  }
173  else
174  {
177  bp = new int[allocatedsize];
178  }
179 
180  // for (i = 0; i<2048; i++ ) bp[i] = initialbuffer[i];
181  memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
182 
183  unsigned int read_so_far = BUFFERBLOCKSIZE;
184 
185  cp = (char *) bp;
186 
187  // and update the destination buffer pointer
188  cp += BUFFERBLOCKSIZE;
189 
190  // we calculate how many BUFFERBLOCKSIZE-sized records we need to read
191  // we have already one, so this is the number of records -1.
192  // normally we would round up (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE
193  int records_to_read = (buffer_size -1) /BUFFERBLOCKSIZE;
194  unsigned int bytes_to_read = records_to_read * BUFFERBLOCKSIZE;
195 
196  xc = read ( fd, cp, bytes_to_read);
197  if ( xc < bytes_to_read )
198  {
199  COUT << "error in buffer, salvaging" << std::endl;
200  bp[0] = read_so_far + xc;
201  }
202 
203  // and initialize the current_index to be the first event
204  bptr = new oncsBuffer ( (PHDWORD *) bp, (PHDWORD) allocatedsize );
205  return 0;
206 }
207