Analysis Software
Documentation for
sPHENIX
simulation software
Home page
Related Pages
Modules
Namespaces
Classes
Files
Examples
External Links
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
fileEventiterator.cc
Go to the documentation of this file.
Or view
the newest version in sPHENIX GitHub for file fileEventiterator.cc
1
//
2
// fileEventIterator mlp 4/19/1997
3
//
4
// this iterator reads events froma data file.
5
6
7
#include <stddef.h>
8
#include <string.h>
9
10
#include <unistd.h>
11
#include <sys/types.h>
12
#include <sys/stat.h>
13
#include <fcntl.h>
14
15
// there are two similar constructors, one with just the
16
// filename, the other with an additional status value
17
// which is non-zero on return if anything goes wrong.
18
#include "
fileEventiterator.h
"
19
20
//#ifndef LVL2_WINNT
21
#include "
lzobuffer.h
"
22
//#endif
23
24
25
fileEventiterator::~fileEventiterator
()
26
{
27
if
(
fd
) close (
fd
);
28
if
(
thefilename
!= NULL)
delete
[]
thefilename
;
29
if
(
bp
!= NULL )
delete
[]
bp
;
30
if
(
bptr
!= NULL )
delete
bptr
;
31
}
32
33
34
fileEventiterator::fileEventiterator
(
const
char
*
filename
)
35
{
36
open_file
( filename);
37
}
38
39
fileEventiterator::fileEventiterator
(
const
char
*
filename
,
int
&
status
)
40
{
41
status =
open_file
( filename);
42
}
43
44
45
int
fileEventiterator::open_file
(
const
char
*
filename
)
46
{
47
fd
=
open
(filename, O_RDONLY | O_LARGEFILE);
48
bptr
= 0;
49
bp
= 0;
50
allocatedsize
= 0;
51
thefilename
= NULL;
52
events_so_far
= 0;
53
verbosity
=0;
54
_defunct
= 0;
55
56
if
(
fd
> 0)
57
{
58
thefilename
=
new
char
[strlen(filename)+1];
59
strcpy (
thefilename
, filename);
60
last_read_status
= 0;
61
current_index
= 0;
62
return
0;
63
}
64
else
65
{
66
last_read_status
= 1;
67
_defunct
= 1;
68
}
69
return
1;
70
71
}
72
73
74
75
void
fileEventiterator::identify
(
OSTREAM
&
os
)
const
76
{
77
os <<
"fileEventiterator reading from "
<<
thefilename
;
78
if
(
_defunct
) os <<
" *** defunct"
;
79
os<< std::endl;
80
81
};
82
83
84
const
char
*
fileEventiterator::getCurrentFileName
()
const
85
{
86
static
char
namestr[512];
87
if
(
thefilename
== NULL)
88
{
89
return
" "
;
90
}
91
else
92
{
93
strcpy (namestr,
thefilename
);
94
return
namestr;
95
}
96
};
97
98
99
100
101
const
char
*
fileEventiterator::getIdTag
()
const
102
{
103
// sprintf (idline, " -- fileEventiterator reading from %s", thefilename);
104
return
"fileEventiterator"
;
105
};
106
107
108
109
// and, finally, the only non-constructor member function to
110
// retrieve events from the iterator.
111
112
Event
*
fileEventiterator::getNextEvent
()
113
{
114
if
(
_defunct
)
return
0;
115
Event
*evt = 0;
116
117
// if we had a read error before, we just return
118
if
(
last_read_status
)
return
NULL;
119
120
// see if we have a buffer to read
121
if
(
bptr
== 0)
122
{
123
if
( (
last_read_status
=
read_next_buffer
()) !=0 )
124
{
125
return
NULL;
126
}
127
}
128
129
while
(
last_read_status
== 0)
130
{
131
if
(
bptr
) evt =
bptr
->
getEvent
();
132
if
(evt)
133
{
134
events_so_far
++;
135
return
evt;
136
}
137
last_read_status
=
read_next_buffer
();
138
}
139
140
return
NULL;
141
142
}
143
144
// -----------------------------------------------------
145
// this is a private function to read the next buffer
146
// if needed.
147
148
int
fileEventiterator::read_next_buffer
()
149
{
150
PHDWORD
initialbuffer[
BUFFERBLOCKSIZE
/4];
151
152
buffer_size
= 0;
153
154
if
(
bptr
)
155
{
156
delete
bptr
;
157
bptr
= 0;
158
}
159
events_so_far
= 0;
160
161
// we are now reading the first block (8192 bytes) into
162
// initialbuffer. The buffer is at least that long. We
163
// look up the buffersize etc from that, and
164
// start filling the actual buffer. We do not read
165
// the data into the eventual destination since we may
166
// have to delete it to get more room (we might find that
167
// the buffer is larger than what we have allocated).
168
169
170
// set the pointer to char to the destination buffer
171
char
*cp = (
char
*) initialbuffer;
172
173
unsigned
int
xc;
174
175
176
// this while loop implements the skipping of 8k records until
177
// we find a valid buffer marker. (We usually find it right away).
178
179
while
(
buffer_size
== 0 )
180
{
181
// read the first record
182
xc =
read
(
fd
, cp,
BUFFERBLOCKSIZE
);
183
184
// error of EoF?
185
if
( xc <
BUFFERBLOCKSIZE
)
186
{
187
// COUT << "ferror" << std::endl;
188
return
-1;
189
}
190
191
192
// get the buffer length into a dedicated variable
193
if
(initialbuffer[1] ==
BUFFERMARKER
|| initialbuffer[1]==
GZBUFFERMARKER
194
|| initialbuffer[1]==
LZO1XBUFFERMARKER
|| initialbuffer[1]==
ONCSBUFFERMARKER
)
195
{
196
buffer_size
= initialbuffer[0];
197
}
198
else
199
{
200
unsigned
int
marker =
buffer::u4swap
(initialbuffer[1]);
201
if
(marker ==
BUFFERMARKER
|| marker ==
GZBUFFERMARKER
|| marker ==
LZO1XBUFFERMARKER
|| marker ==
ONCSBUFFERMARKER
)
202
{
203
buffer_size
=
buffer::u4swap
(initialbuffer[0]);
204
}
205
}
206
}
207
208
209
int
i
;
210
if
(
bp
)
211
{
212
// this tests if we have enough space in the existing buffer
213
if
(
buffer_size
>
allocatedsize
*4)
// no, we delete and make a bigger one
214
{
215
delete
[]
bp
;
216
i = (
buffer_size
+
BUFFERBLOCKSIZE
-1) /
BUFFERBLOCKSIZE
;
217
allocatedsize
= i *
BUFFERBLOCKSIZE
/4;
218
bp
=
new
PHDWORD
[
allocatedsize
];
219
// std::cout << __FILE__ << " " << __LINE__ << " new bp pointer is " << bp << " length value " << bp[-1]<< std::endl;
220
}
221
}
222
else
223
{
224
i = (
buffer_size
+
BUFFERBLOCKSIZE
-1) /
BUFFERBLOCKSIZE
;
225
allocatedsize
= i *
BUFFERBLOCKSIZE
/4;
226
bp
=
new
PHDWORD
[
allocatedsize
];
227
228
}
229
230
// for (i = 0; i<BUFFERBLOCKSIZE/4; i++ ) bp[i] = initialbuffer[i];
231
memcpy (
bp
, initialbuffer,
BUFFERBLOCKSIZE
);
232
233
cp = (
char
*)
bp
;
234
235
// and update the destination buffer pointer
236
cp +=
BUFFERBLOCKSIZE
;
237
238
PHDWORD
read_so_far =
BUFFERBLOCKSIZE
;
239
240
int
errorinread=0;
241
242
// we calculate how many BUFFERBLOCKSIZE-sized records we need to read
243
// we have already one, so this is the number of records -1.
244
// normally we would round up (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE
245
int
records_to_read = (
buffer_size
-1) /
BUFFERBLOCKSIZE
;
246
unsigned
int
bytes_to_read = records_to_read *
BUFFERBLOCKSIZE
;
247
248
xc =
read
(
fd
, cp, bytes_to_read);
249
if
( xc < bytes_to_read )
250
{
251
COUT
<<
"error in buffer, salvaging"
<< std::endl;
252
bp
[0] = read_so_far + xc;
253
errorinread =1;
254
}
255
256
// and initialize the current_index to be the first event
257
258
if
( ( initialbuffer[1]==
GZBUFFERMARKER
||
259
buffer::u4swap
(initialbuffer[1])==
GZBUFFERMARKER
||
260
initialbuffer[1]==
LZO1XBUFFERMARKER
||
261
buffer::u4swap
(initialbuffer[1])==
LZO1XBUFFERMARKER
)
262
&& errorinread )
263
{
264
bptr
= 0;
265
return
-3;
266
}
267
268
return
buffer::makeBuffer
(
bp
,
allocatedsize
, &
bptr
);
269
270
271
}
272
online_distribution
blob
master
newbasic
fileEventiterator.cc
Built by
Jin Huang
. updated:
Sat Feb 17 2024 22:18:27
using
1.8.2 with
sPHENIX GitHub integration