-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheventcombiner.cc
323 lines (256 loc) · 6.83 KB
/
eventcombiner.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
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
318
319
320
321
322
323
#include "fileEventiterator.h"
#include "testEventiterator.h"
#include "phenixTypes.h"
#include "A_Event.h"
#include "ogzBuffer.h"
#include "ophBuffer.h"
#include "ospBuffer.h"
#include "EventTypes.h"
#include "oEvent.h"
#include <cstdlib>
#include <unistd.h>
#include <cstdio>
#include <string>
#include <vector>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#define DDEVENTITERATOR 1
#define FILEEVENTITERATOR 2
#define TESTEVENTITERATOR 3
#define DDPOOL 4
#define DFILE 5
#define DNULL 6
#if defined(SunOS) || defined(Linux) || defined(OSF1)
#include <cstring>
#else
#include <bstring.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
#if defined(SunOS) || defined(Linux) || defined(OSF1)
void sig_handler(int);
#else
void sig_handler(...);
#endif
void exitmsg()
{
COUT << "** usage: eventcombiner outputfile inputfile1 inputfile2 ..." << std::endl;
COUT << " eventcombiner -h for more help" << std::endl;
exit(0);
}
void exithelp()
{
COUT << std::endl;
COUT << " Syntax:"<< std::endl;
COUT << std::endl;
COUT << " eventcombiner [-v] [-i] [-n number] [-u] [-p] [-h] inputfile1 inputfile2 ..."<< std::endl;
COUT << " e.g eventcombiner -v /export/rcfdata/dcm_data/built_evt/rc_3612.prdfz /export/rcfdata/dcm_data/rc/*3612*" << std::endl;
COUT << " will combine all the *3612* (from one run number) together in the file. "<< std::endl;
COUT << " Options:" << std::endl;
COUT << " -v verbose, without that it does its work silently" << std::endl;
COUT << " -i identify the events as they are processed, good for debugging" << std::endl;
COUT << " -e <event number> start with event number (number in evt. header)" << std::endl;
COUT << " -c <event number> start with nth event" << std::endl;
COUT << " -n <number> stop after so many events" << std::endl;
COUT << " -p read and write legacy PHENIX format data, default is sPHENIX "<< std::endl;
// COUT << " -u write uncompressed data, default is compressed "<< std::endl;
COUT << " -f force output file overwrite, normally you cannot overwrite an existing file (safety belt)"<< std::endl;
COUT << " -x ignore event numbers (allow non-matching evt nrs to be combined, DANGEROUS)"<< std::endl;
COUT << " -h this message" << std::endl;
exit(0);
}
void evtcountexitmsg()
{
COUT << "** cannot specify both -e and -c!" << std::endl;
COUT << " type eventcombiner -h for more help" << std::endl;
exit(0);
}
oBuffer *ob;
int fd;
int file_open = 0;
int
main(int argc, char *argv[])
{
int c;
int status;
int eventnumber =0;
int countnumber =0;
int forceflag =0;
int verbose = 0;
int identify = 0;
int maxevents = 0;
int eventnr = 0;
int ignoreeventnr = 0;
// int gzipcompress = 0;
int legacyphenix = 0;
extern char *optarg;
extern int optind;
PHDWORD *buffer;
// initialize the pointers to 0;
fd = 0;
ob = 0;
// COUT << "parsing input" << std::endl;
int buffer_size = 256*1024*64; // makes 32MB (specifies how many dwords, so *4)
while ((c = getopt(argc, argv, "n:c:e:vipfhx")) != EOF)
{
switch (c)
{
case 'v': // verbose
verbose = 1;
break;
case 'i': // identify
identify = 1;
break;
case 'f': // force
forceflag = 1;
break;
// case 'g': // do not gzip-compress
// gzipcompress = 1;
// break;
case 'p': // do not gzip-compress
legacyphenix = 1;
break;
case 'x': // do not gzip-compress
ignoreeventnr = 1;
break;
case 'e':
if ( !sscanf(optarg, "%d", &eventnumber) ) exitmsg();
break;
case 'c':
if ( !sscanf(optarg, "%d", &countnumber) ) exitmsg();
break;
case 'n': // number of events
if ( !sscanf(optarg, "%d", &maxevents) ) exitmsg();
break;
case 'h':
exithelp();
break;
default:
break;
}
}
if (argc < 3) exitmsg();
if ( eventnumber && countnumber) evtcountexitmsg();
vector<Eventiterator *> it;
vector<Event *>evt;
int index;
string filename;
filename =argv[optind];
// try if the output file exists
fd = open(filename.c_str(), O_RDONLY | O_LARGEFILE);
if (fd > 0 )
{
if ( ! forceflag)
{
COUT << "file " << filename << " exists - I won't override it" << std::endl;
COUT << " use -f to force" << std::endl;
exit(1);
}
close (fd);
}
for ( index = optind+1; index < argc; index++ )
{
COUT << "reading from file " << argv[index] << std::endl;
Eventiterator *itx = new fileEventiterator(argv[index], status);
if (status)
{
COUT << "could not open " << argv[index] << std::endl;
exit(1);
}
it.push_back(itx);
}
buffer = new PHDWORD [buffer_size];
int go_on = 1;
unlink (filename.c_str());
fd = open(filename.c_str(),
O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
S_IRWXU | S_IROTH | S_IRGRP );
if ( fd < 0 )
{
COUT << "Could not open file: " << filename << std::endl;
exit (1);
}
if (verbose) COUT << "Opened file: " << filename << std::endl;
if (legacyphenix)
{
ob = new ophBuffer (fd, buffer, buffer_size);
}
else
{
ob = new ospBuffer (fd, buffer, buffer_size);
}
int count = 0;
while ( ( maxevents == 0 || eventnr < maxevents) && go_on)
{
int total_length = 0;
int enr = 0;
vector<Eventiterator *>::iterator it_itr;
Event *e = 0;
for (it_itr = it.begin(); it_itr != it.end(); ++it_itr)
{
e = (*it_itr)->getNextEvent();
if ( !e )
{
go_on = 0;
}
else
{
total_length += e->getEvtLength();
}
evt.push_back(e);
}
if (! go_on ) break;
int *out = new int[total_length];
int nwout;
int current = 0;
int take_this = 1;
if ( eventnumber && e != 0 && e->getEvtSequence() < eventnumber) take_this = 0;
if ( countnumber && count+1 < countnumber) take_this = 0;
vector<Event *>::iterator evt_itr;
for (evt_itr = evt.begin(); evt_itr != evt.end(); ++evt_itr)
{
if (evt_itr == evt.begin() ) // event from 1st input file
{
enr = (*evt_itr)->getEvtSequence();
(*evt_itr)->Copy ( out , total_length , &nwout);
current = nwout;
delete (*evt_itr);
}
else
{
if (take_this ==0 || (ignoreeventnr ==0 && (*evt_itr)->getEvtSequence() != enr ))
{
take_this = 0;
}
else
{
(*evt_itr)->Copy ( &out[current] , total_length-current , &nwout, "DATA");
current += nwout;
out[0] += nwout;
delete (*evt_itr);
}
}
}
if (take_this)
{
Event *E = new oncsEvent(out);
if (identify) E->identify();
ob->addEvent(E);
delete E;
eventnr++;
}
count++;
delete [] out;
evt.clear();
}
vector<Eventiterator *>::iterator it_itr;
for (it_itr = it.begin(); it_itr != it.end(); ++it_itr)
{
delete ( (*it_itr) ) ;
}
delete ob;
close (fd);
}