-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprdfoStream.cc
More file actions
150 lines (118 loc) · 3.04 KB
/
Copy pathprdfoStream.cc
File metadata and controls
150 lines (118 loc) · 3.04 KB
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
#include <prdfoStream.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "EvtConstants.h"
using namespace std;
// the constructor first ----------------
prdfoStream::prdfoStream (const char *filename, const int length)
{
buffer_sequence = 1;
_filename = filename;
int *b = new int [length];
bptr = ( buffer_ptr ) b;
data_ptr = &(bptr->data[0]);
max_length = length; // in 32bit units
max_size = max_length;
bptr->ID = -64;
bptr->Bufseq = buffer_sequence;
bptr->Runnr = 0;
has_buffer = 0;
defunct = 0;
// test if the file exists, do not overwrite
fd = open(_filename.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
S_IRWXU | S_IROTH | S_IRGRP );
if (fd < 0)
{
cout << " error opening file " << _filename << endl;
perror ( _filename.c_str());
fd = 0;
defunct = 1;
}
}
// the destructor... ----------------
prdfoStream::~prdfoStream ()
{
writeout();
int *b = (int *) bptr;
delete [] b;
if (fd) close (fd);
}
int prdfoStream::prepare_next( const int iseq
, const int irun)
{
if (defunct)
{
cout << "defunct oStream object " << endl;
return -1;
}
// re-initialize the event header length
bptr->Length = BUFFERHEADERLENGTH*4;
bptr->ID = -64;
bptr->Bufseq = iseq;
if (irun>0) bptr->Runnr = irun;
current_index = 0;
left = max_size - BUFFERHEADERLENGTH - EOBLENGTH;
has_end = 0;
has_buffer =1;
return 0;
}
// ---------------------------------------------------------
int prdfoStream::addEvent(Event * evt)
{
if (defunct)
{
cout << "defunct oStream object " << endl;
return -1;
}
if ( ! has_buffer) prepare_next (buffer_sequence++, evt->getRunNumber() );
int evtsize = evt->getEvtLength();
if (evtsize*4 > left-EOBLENGTH)
{
//cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Prdf buffer being written" << endl;
writeout();
}
int nw;
evt->Copy( &(bptr->data[current_index]), evtsize, &nw);
left -= evtsize;
current_index += evtsize;
bptr->Length += evtsize*4;
return 0;
}
// ----------------------------------------------------------
int prdfoStream::addEoB()
{
if (has_end) return -1;
bptr->data[current_index++] = 2;
bptr->data[current_index++] = 0;
bptr->Length += 2*4;
has_end = 1;
// cout << "addind EOB" << endl;
return 0;
}
unsigned int prdfoStream::writeout ()
{
if ( bptr->Length == BUFFERHEADERLENGTH*4) return 0;
if (!has_end) addEoB();
unsigned int bytes;
int blockcount = ( bptr->Length + 8192 -1)/8192;
int bytecount = blockcount*8192;
bytes = writen ( fd, (char *) bptr , bytecount );
has_buffer = 0;
return bytes;
}
unsigned int prdfoStream::writen (int fd, char *ptr, const unsigned int nbytes)
{
unsigned int nleft, nwritten;
nleft = nbytes;
while ( nleft>0 )
{
nwritten = write (fd, ptr, nleft);
if ( nwritten < 0 )
return nwritten;
nleft -= nwritten;
ptr += nwritten;
}
return (nbytes-nleft);
}