forked from IevgenLavrukhin/e1039_coda_decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHaCodaFile.C
329 lines (285 loc) · 9.12 KB
/
THaCodaFile.C
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/////////////////////////////////////////////////////////////////////
//
// THaCodaFile
// File of CODA data
//
// CODA data file, and facilities to open, close, read,
// write, filter CODA data to disk files, etc.
// A lot of this relies on the "evio.cpp" code from
// DAQ group which is a C++ rendition of evio.c that
// we have used for years, but here are some useful
// added features.
//
// author Robert Michaels (rom@jlab.org)
//
/////////////////////////////////////////////////////////////////////
#include "THaCodaFile.h"
#ifndef STANDALONE
ClassImp(THaCodaFile)
#endif
//Constructors
THaCodaFile::THaCodaFile() { // do nothing (must open file separately)
ffirst = 0;
init(" no name ");
}
THaCodaFile::THaCodaFile(TString fname) {
ffirst = 0;
init(fname);
int status = codaOpen(fname.Data(),"r"); // read only
staterr("open",status);
}
THaCodaFile::THaCodaFile(TString fname, TString readwrite) {
ffirst = 0;
init(fname);
int status = codaOpen(fname.Data(),readwrite.Data()); // pass read or write flag
staterr("open",status);
}
//Destructor
THaCodaFile::~THaCodaFile () {
int status = codaClose();
staterr("close",status);
};
int THaCodaFile::codaOpen(TString fname) {
init(fname);
int status = evOpen((char*)fname.Data(),"r",&handle);
staterr("open",status);
return status;
};
int THaCodaFile::codaOpen(TString fname, TString readwrite) {
init(fname);
int status = evOpen((char*)fname.Data(),(char*)readwrite.Data(),&handle);
staterr("open",status);
return status;
};
int THaCodaFile::codaClose() {
// Close the file. Do nothing if file not opened.
if( handle ) {
int status = evClose(handle);
handle = 0;
return status;
}
return CODA_OK;
}
int THaCodaFile::codaRead() {
// codaRead: Reads data from file, stored in evbuffer.
// Must be called once per event.
int status;
if ( handle ) {
status = evRead(handle, evbuffer, MAXEVLEN);
staterr("read",status);
if (status != S_SUCCESS) {
if (status == EOF) return status; // ok, end of file
status = CODA_ERROR;
}
} else {
if(CODA_VERBOSE) {
cout << "codaRead ERROR: tried to access a file with handle = 0" << endl;
cout << "You need to call codaOpen(filename)" << endl;
cout << "or use the constructor with (filename) arg" << endl;
}
status = CODA_ERROR;
}
return status;
};
int THaCodaFile::codaWrite(unsigned *evbuffer) {
// codaWrite: Writes data to file
int status;
if ( handle ) {
status = evWrite(handle, evbuffer);
staterr("write",status);
} else {
cout << "codaWrite ERROR: tried to access file with handle = 0" << endl;
status = CODA_ERROR;
}
return status;
};
unsigned* THaCodaFile::getEvBuffer() {
// Here's how to get raw event buffer, evbuffer, after codaRead call
return evbuffer;
}
int THaCodaFile::filterToFile(TString output_file) {
// A call to filterToFile filters from present file to output_file
// using filter criteria defined by evtypes, evlist, and max_to_filt
// which are loaded by public methods of this class. If no conditions
// were loaded, it makes a copy of the input file (i.e. no filtering).
int i;
if(output_file == filename) {
if(CODA_VERBOSE) {
cout << "filterToFile: ERROR: ";
cout << "Input and output files cannot be same " << endl;
cout << "This is to protect you against overwriting data" << endl;
}
return CODA_ERROR;
}
FILE *fp;
if ((fp = fopen(output_file.Data(),"r")) != NULL) {
if(CODA_VERBOSE) {
cout << "filterToFile: ERROR: ";
cout << "Output file `" << output_file << "' exists " << endl;
cout << "You must remove it by hand first. " << endl;
cout << "This forces you to think and not overwrite data." << endl;
}
fclose(fp);
return CODA_ERROR;
}
THaCodaFile* fout = new THaCodaFile(output_file.Data(),"w");
int nfilt = 0;
while (codaRead() == S_SUCCESS) {
unsigned* rawbuff = getEvBuffer();
int evtype = rawbuff[1]>>16;
int evnum = rawbuff[4];
int oktofilt = 1;
if (CODA_DEBUG) {
cout << "Input evtype " << dec << evtype;
cout << " evnum " << evnum << endl;
cout << "max_to_filt = " << max_to_filt << endl;
cout << "evtype size = " << evtypes[0] << endl;
cout << "evlist size = " << evlist[0] << endl;
}
if ( evtypes[0] > 0 ) {
oktofilt = 0;
for (i=1; i<=evtypes[0]; i++) {
if (evtype == evtypes[i]) {
oktofilt = 1;
goto Cont1;
}
}
}
Cont1:
if ( evlist[0] > 0 ) {
oktofilt = 0;
for (i=1; i<=evlist[0]; i++) {
if (evnum == evlist[i]) {
oktofilt = 1;
goto Cont2;
}
}
}
Cont2:
if (oktofilt) {
nfilt++;
if (CODA_DEBUG) {
cout << "Filtering event, nfilt " << dec << nfilt << endl;
}
int status = fout->codaWrite(getEvBuffer());
if (status != S_SUCCESS) {
if (CODA_VERBOSE) {
cout << "Error in filterToFile ! " << endl;
cout << "codaWrite returned status " << status << endl;
}
goto Finish;
}
if (max_to_filt > 0) {
if (nfilt == max_to_filt) {
goto Finish;
}
}
}
}
Finish:
delete fout;
return S_SUCCESS;
};
void THaCodaFile::addEvTypeFilt(int evtype_to_filt)
// Function to set up filtering by event type
{
int i;
initFilter();
if (evtypes[0] >= maxftype-1) {
TArrayI temp = evtypes;
maxftype = maxftype + 100;
evtypes.Set(maxftype);
for (i=0; i<=temp[0]; i++) evtypes[i]=temp[i];
temp.~TArrayI();
}
evtypes[0] = evtypes[0] + 1; // 0th element = num elements in list
int n = evtypes[0];
evtypes[n] = evtype_to_filt;
return;
};
void THaCodaFile::addEvListFilt(int event_num_to_filt)
// Function to set up filtering by list of event numbers
{
int i;
initFilter();
if (evlist[0] >= maxflist-1) {
TArrayI temp = evlist;
maxflist = maxflist + 100;
evlist.Set(maxflist);
for (i=0; i<=temp[0]; i++) evlist[i]=temp[i];
temp.~TArrayI();
}
evlist[0] = evlist[0] + 1; // 0th element = num elements in list
int n = evlist[0];
evlist[n] = event_num_to_filt;
return;
};
void THaCodaFile::setMaxEvFilt(int max_event)
// Function to set up the max number of events to filter
{
max_to_filt = max_event;
return;
};
void THaCodaFile::staterr(TString tried_to, int status) {
// staterr gives the non-expert user a reasonable clue
// of what the status returns from evio mean.
// Note: severe errors can cause job to exit(0)
// and the user has to pay attention to why.
if (status == S_SUCCESS) return; // everything is fine.
if (tried_to == "open") {
cout << "THaCodaFile: ERROR opening file = " << filename << endl;
cout << "Most likely errors are: " << endl;
cout << " 1. You mistyped the name of file ?" << endl;
cout << " 2. The file has length zero ? " << endl;
cout << status << " Job aborting !! " << endl;
exit(0);
}
switch (status) {
case S_EVFILE_TRUNC :
cout << "THaCodaFile ERROR: Truncated event on file read" << endl;
cout << "Evbuffer size is too small. Job aborted." << endl;
exit(0); // If this ever happens, recompile with MAXEVLEN
// bigger, and mutter under your breath at the author.
case S_EVFILE_BADBLOCK :
cout << "Bad block number encountered " << endl;
break;
case S_EVFILE_BADHANDLE :
cout << "Bad handle (file/stream not open) " << endl;
break;
case S_EVFILE_ALLOCFAIL :
cout << "Failed to allocate event I/O" << endl;
break;
case S_EVFILE_BADFILE :
cout << "File format error" << endl;
break;
case S_EVFILE_UNKOPTION :
cout << "Unknown option specified" << endl;
break;
case S_EVFILE_UNXPTDEOF :
cout << "Unexpected end of file while reading event" << endl;
break;
case EOF:
/*
if(CODA_VERBOSE) {
cout << "Normal end of file " << filename << " encountered" << endl;
}*/
break;
default:
cout << "Error status 0x" << hex << status << endl;
}
};
void THaCodaFile::init(TString fname) {
handle = 0;
filename = fname;
};
void THaCodaFile::initFilter() {
if (!ffirst) {
ffirst = 1;
maxflist = 100;
maxftype = 100;
evlist.Set(maxflist);
evtypes.Set(maxftype);
evlist[0] = 0;
evtypes[0] = 0;
}
};