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