Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
56 | f9daq | 1 | /*CMZ : 23/02/2008 09.47.19 by Samo Korpar*/ |
2 | /*CMZ : 0.02/00 04/07/2001 00.38.08 by Samo Korpar*/ |
||
3 | /*CMZ : 0.01/00 30/11/99 16.50.35 by Samo Korpar*/ |
||
4 | /*-- Author : Samo Korpar 18/10/99*/ |
||
5 | #include <stdlib.h> |
||
6 | #include <stdio.h> |
||
7 | #include <stdint.h> |
||
8 | #include <string.h> |
||
9 | #include <time.h> |
||
10 | |||
11 | |||
12 | /*KEEP,evdp,T=C++.*/ |
||
13 | #include "evdp.h" |
||
14 | /*KEND.*/ |
||
15 | |||
16 | #define swap_4(x) swap_long((int32_t *) x) |
||
17 | #define swap_2(x) swap_short((short *) x) |
||
18 | |||
19 | |||
20 | int isLE, isLOG; |
||
21 | char datafilename[300]; |
||
22 | |||
23 | EVT_DATA evdata; |
||
24 | #define EVT_DATA_SIZE (sizeof(EVT_DATA)) |
||
25 | LOG_header logh; |
||
26 | EVT_header evh; |
||
27 | EVT_trailer evt; |
||
28 | FILE *evfid; |
||
29 | FILE *oldfid; |
||
30 | |||
31 | int is_little_endian() |
||
32 | { |
||
33 | int test=1; |
||
34 | |||
35 | isLE = (int) *((char *) &test); |
||
36 | return isLE; |
||
37 | } |
||
38 | |||
39 | void swap_short(short *shtosw) |
||
40 | { |
||
41 | short stmp; |
||
42 | char *b1 = (char *) shtosw, *b2 = (char *) &stmp; |
||
43 | |||
44 | stmp=*shtosw; |
||
45 | b1[0]=b2[1]; |
||
46 | b1[1]=b2[0]; |
||
47 | return; |
||
48 | } |
||
49 | |||
50 | void swap_long(int32_t *lotosw) |
||
51 | { |
||
52 | int32_t ltmp; |
||
53 | char *b1 = (char *) lotosw, *b2 = (char *) <mp; |
||
54 | |||
55 | ltmp=*lotosw; |
||
56 | b1[0]=b2[3]; |
||
57 | b1[1]=b2[2]; |
||
58 | b1[2]=b2[1]; |
||
59 | b1[3]=b2[0]; |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | int open_data_file(char *dfn) |
||
64 | { |
||
65 | if (dfn != NULL) |
||
66 | { |
||
67 | strcpy(datafilename, dfn); |
||
68 | isLOG=0; |
||
69 | if (strstr(datafilename, ".daq")!=NULL) isLOG=1; |
||
70 | } |
||
71 | |||
72 | if ((evfid=fopen(datafilename, "r"))==NULL) return 1; |
||
73 | return 0; |
||
74 | } |
||
75 | |||
76 | int close_data_file() |
||
77 | { |
||
78 | if (fclose(evfid)==EOF) return 1; |
||
79 | return 0; |
||
80 | } |
||
81 | |||
82 | int reopen_data_file() |
||
83 | { |
||
84 | { |
||
85 | oldfid=evfid; |
||
86 | } |
||
87 | if (open_data_file((char *) NULL)) return 1; |
||
88 | return 0; |
||
89 | } |
||
90 | |||
91 | int reclose_data_file() |
||
92 | { |
||
93 | if (close_data_file()) return 1; |
||
94 | { |
||
95 | evfid=oldfid; |
||
96 | } |
||
97 | return 0; |
||
98 | } |
||
99 | |||
100 | int next_buf(int len, void *to) |
||
101 | { |
||
102 | if (fread( to, 1, len, evfid) != len) return -1; |
||
103 | return len; |
||
104 | } |
||
105 | |||
106 | int skip_buf(int len) |
||
107 | { |
||
108 | if (fseek(evfid, len, SEEK_CUR)) return -1; |
||
109 | return len; |
||
110 | } |
||
111 | |||
112 | int goto_event(int evgoto) |
||
113 | { |
||
114 | int pos, fpos, bc; |
||
115 | |||
116 | pos=0; |
||
117 | rewind(evfid); |
||
118 | while ( ++pos != evgoto ) |
||
119 | { |
||
120 | if (fread((void *) &bc, 1, 4, evfid) == 4) |
||
121 | { |
||
122 | if (isLE) swap_4(&bc); |
||
123 | if (fseek(evfid, bc-4, SEEK_CUR)) return pos-1; |
||
124 | } |
||
125 | } |
||
126 | return pos; |
||
127 | } |
||
128 | |||
129 | int get_nextev(FED_record *fdrp, unsigned short cid, int prmes) |
||
130 | { |
||
131 | int nr,i; |
||
132 | |||
133 | SLB_header slbh; |
||
134 | SLB_trailer slbt; |
||
135 | |||
136 | nr=0; |
||
137 | if (isLOG) |
||
138 | { |
||
139 | if (next_buf(20, (void *) &logh) != 20) return 0; |
||
140 | if (isLE) |
||
141 | { |
||
142 | swap_4(&logh.bc); |
||
143 | swap_4(&logh.exp); |
||
144 | swap_4(&logh.run); |
||
145 | swap_4(&logh.evt); |
||
146 | swap_4(&logh.dtm); |
||
147 | } |
||
148 | } |
||
149 | if (next_buf(20, (void *) &evh) != 20) return 0; |
||
150 | if (isLE) |
||
151 | { |
||
152 | swap_4(&evh.bc); |
||
153 | swap_2(&evh.recid); |
||
154 | swap_2(&evh.sp); |
||
155 | swap_4(&evh.slt); |
||
156 | swap_4(&evh.cm); |
||
157 | swap_4(&evh.em); |
||
158 | } |
||
159 | if (prmes) |
||
160 | printf("EVT: bc=%u, recid=0x%X, sp=0x%X, slt=%u, cm=0x%X, em=0x%X\n", |
||
161 | evh.bc, evh.recid, evh.sp, evh.slt, evh.cm, evh.em); |
||
162 | evh.bc-=20; |
||
163 | while (evh.bc>4) |
||
164 | { |
||
165 | if (next_buf(16, (void *) &slbh) != 16) return 0; |
||
166 | if (isLE) |
||
167 | { |
||
168 | swap_4(&slbh.bc); |
||
169 | swap_2(&slbh.recid); |
||
170 | swap_2(&slbh.fltev); |
||
171 | swap_2(&slbh.compid); |
||
172 | swap_2(&slbh.slbn); |
||
173 | swap_4(&slbh.dest); |
||
174 | } |
||
175 | if (prmes) |
||
176 | printf(" >SLB bc=%u, recid=0x%X, fltev=%d," |
||
177 | " compid=0x%X, slbn=%d, dest=0x%X\n", slbh.bc, slbh.recid, |
||
178 | slbh.fltev, slbh.compid, slbh.slbn, slbh.dest); |
||
179 | evh.bc-=slbh.bc; |
||
180 | slbh.bc-=16; |
||
181 | |||
182 | if (slbh.compid==0) |
||
183 | { |
||
184 | if (next_buf(EVT_DATA_SIZE, (void *) &evdata) != EVT_DATA_SIZE) return 0; |
||
185 | slbh.bc-=EVT_DATA_SIZE; |
||
186 | if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0; |
||
187 | if (isLE) |
||
188 | { |
||
189 | swap_4(&evdata.run_number); |
||
190 | swap_4(&evdata.event_time); |
||
191 | swap_4(&evdata.event_status); |
||
192 | swap_4(&evdata.flt_status); |
||
193 | swap_4(&evdata.slp_node); |
||
194 | swap_4(&evdata.slt_status); |
||
195 | swap_4(&evdata.exp_number); |
||
196 | swap_4(&evdata.farm_node); |
||
197 | swap_4(&evdata.BX_long); |
||
198 | swap_2(&evdata.BX_upper); |
||
199 | swap_2(&evdata.FCS_tcode); |
||
200 | swap_2(&evdata.physBX); |
||
201 | swap_2(&evdata.FLT_BX); |
||
202 | swap_2(&evdata.fltevt); |
||
203 | } |
||
204 | } |
||
205 | else if (slbh.compid==cid) |
||
206 | { |
||
207 | while (slbh.bc>4) |
||
208 | { |
||
209 | if (next_buf(8, (void *) fdrp) != 8) return 0; |
||
210 | if (isLE) |
||
211 | { |
||
212 | swap_2(&fdrp->bc); |
||
213 | swap_2(&fdrp->fedid); |
||
214 | swap_2(&fdrp->recid); |
||
215 | swap_2(&fdrp->bxn); |
||
216 | } |
||
217 | if (prmes) |
||
218 | printf(" >FED bc=%d, fedid=0x%X, recid=0x%X, bxn=%d\n", |
||
219 | fdrp->bc, fdrp->fedid, fdrp->recid, fdrp->bxn); |
||
220 | slbh.bc-=fdrp->bc; |
||
221 | fdrp->bc-=8; |
||
222 | if (0x8000&fdrp->recid) |
||
223 | { |
||
224 | if (skip_buf(fdrp->bc) != fdrp->bc) return 0; |
||
225 | } |
||
226 | else |
||
227 | { |
||
228 | if (next_buf(fdrp->bc, (void *) fdrp->data) != fdrp->bc) return 0; |
||
229 | nr++; |
||
230 | fdrp++; |
||
231 | } |
||
232 | |||
233 | /* fdrp->bc-=8; |
||
234 | switch (fdrp->recid) |
||
235 | { |
||
236 | case 0x1002: |
||
237 | if (next_buf(128, (void *) fdrp->data) != 128) return 0; |
||
238 | nr++; |
||
239 | fdrp++; |
||
240 | break; |
||
241 | case 0x100a: |
||
242 | if (next_buf(fdrp->bc, (void *) fdr.data) != fdrp->bc) return 0; |
||
243 | memset((void *) fdrp->data, 0xff, 128); |
||
244 | for (i=0;i<fdrp->bc;i+=2) |
||
245 | fdrp->data[fdr.data[i]]=fdr.data[i+1]; |
||
246 | nr++; |
||
247 | fdrp++; |
||
248 | break; |
||
249 | default: |
||
250 | if (skip_buf(fdrp->bc) != fdrp->bc) return 0; |
||
251 | } |
||
252 | */ |
||
253 | } |
||
254 | } |
||
255 | else if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0; |
||
256 | |||
257 | /* |
||
258 | else if (cid==0x400) |
||
259 | { |
||
260 | if (prmes) |
||
261 | { |
||
262 | while (slbh.bc>4) |
||
263 | { |
||
264 | if (next_buf(8, (void *) &fdr) != 8) return 0; |
||
265 | if (isLE) |
||
266 | { |
||
267 | swap_2(&fdr.bc); |
||
268 | swap_2(&fdr.fedid); |
||
269 | swap_2(&fdr.recid); |
||
270 | swap_2(&fdr.bxn); |
||
271 | } |
||
272 | otfid[nf]=fdr.fedid; |
||
273 | printf(" >FED%d bc=%d, fedid=0x%X, recid=0x%X, bxn=%d", |
||
274 | ++nf, fdr.bc, fdr.fedid, fdr.recid, fdr.bxn); |
||
275 | slbh.bc-=fdr.bc; |
||
276 | fdr.bc-=8; |
||
277 | if (next_buf(fdr.bc, (void *) fdr.data) != fdr.bc) return 0; |
||
278 | for (i=0;i<fdr.bc;i+=4) |
||
279 | { |
||
280 | if (! (i%16)) printf("\n"); |
||
281 | printf("0x%02X 0x%02X 0x%02X 0x%02X ", |
||
282 | fdr.data[i], fdr.data[i+1], fdr.data[i+2], fdr.data[i+3]); |
||
283 | } |
||
284 | printf("\n"); |
||
285 | } |
||
286 | } |
||
287 | else |
||
288 | { |
||
289 | if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0; |
||
290 | } |
||
291 | } |
||
292 | else if (cid==0x600) |
||
293 | { |
||
294 | if (prmes) |
||
295 | { |
||
296 | while (slbh.bc>4) |
||
297 | { |
||
298 | if (next_buf(8, (void *) &fdr) != 8) return 0; |
||
299 | if (isLE) |
||
300 | { |
||
301 | swap_2(&fdr.bc); |
||
302 | swap_2(&fdr.fedid); |
||
303 | swap_2(&fdr.recid); |
||
304 | swap_2(&fdr.bxn); |
||
305 | } |
||
306 | printf(" >FED bc=%d, fedid=0x%X, recid=0x%X, bxn=%d\n", |
||
307 | fdr.bc, fdr.fedid, fdr.recid, fdr.bxn); |
||
308 | slbh.bc-=fdr.bc; |
||
309 | fdr.bc-=8; |
||
310 | if (skip_buf(fdr.bc) != fdr.bc ) return 0; |
||
311 | } |
||
312 | } |
||
313 | else |
||
314 | { |
||
315 | if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0; |
||
316 | } |
||
317 | */ |
||
318 | |||
319 | if (next_buf( 4, (void *) &slbt) != 4) return 0; |
||
320 | if (isLE) |
||
321 | { |
||
322 | swap_2(&slbt.recid); |
||
323 | swap_2(&slbt.fltev); |
||
324 | } |
||
325 | if (prmes) |
||
326 | printf(" >SLB recid=0x%X, fltev=%d\n", slbt.recid, slbt.fltev); |
||
327 | } |
||
328 | if (next_buf( 4, (void *) &evt) != 4) return 0; |
||
329 | if (isLE) swap_4(&evt.slt); |
||
330 | if (prmes) |
||
331 | { |
||
332 | printf("EVT: slt=%u, tcode=0x%x, flt_status=0x%x\n", |
||
333 | evh.slt, evdata.FCS_tcode, evdata.flt_status); |
||
334 | } |
||
335 | if (prmes && isLOG) |
||
336 | printf("LOG: bc=%u, exp=%u, run=%u, evt=%u, dtm=%s", |
||
337 | logh.bc, logh.exp, logh.run, logh.evt, ctime((time_t *)&logh.dtm)); |
||
338 | return nr; |
||
339 | } |
||
340 | |||
341 | int dump_cmp(FILE *fout, unsigned short cid) |
||
342 | { |
||
343 | int pos; |
||
344 | int evbc, slbbc, compid; |
||
345 | char evbf[10000]; |
||
346 | |||
347 | LOG_header *loghb; |
||
348 | EVT_header *evhb; |
||
349 | |||
350 | SLB_header *slbh; |
||
351 | |||
352 | |||
353 | if (isLOG) |
||
354 | { |
||
355 | if (skip_buf(20) != 20) return 0; |
||
356 | } |
||
357 | |||
358 | pos=0; |
||
359 | evhb=(EVT_header *) &evbf[pos]; |
||
360 | if (next_buf(20, (void *) evhb) != 20) return 0; |
||
361 | pos+=20; |
||
362 | evbc=evhb->bc; |
||
363 | if (isLE) swap_4(&evbc); |
||
364 | evbc-=20; |
||
365 | |||
366 | while (evbc>4) |
||
367 | { |
||
368 | slbh=(SLB_header *) &evbf[pos]; |
||
369 | if (next_buf(16, (void *) slbh) != 16) return 0; |
||
370 | slbbc=slbh->bc; |
||
371 | compid=slbh->compid; |
||
372 | if (isLE) |
||
373 | { |
||
374 | swap_4(&slbbc); |
||
375 | swap_2(&compid); |
||
376 | } |
||
377 | evbc-=slbbc; |
||
378 | slbbc-=16; |
||
379 | |||
380 | if ((compid==cid)||(compid==0x0)) |
||
381 | { |
||
382 | pos+=16; |
||
383 | if (next_buf(slbbc, (void *) &evbf[pos]) != slbbc) return 0; |
||
384 | pos+=slbbc; |
||
385 | } |
||
386 | else |
||
387 | { |
||
388 | if (skip_buf(slbbc) != slbbc) return 0; |
||
389 | } |
||
390 | } |
||
391 | |||
392 | if (next_buf( 4, (void *) &evbf[pos]) != 4) return 0; |
||
393 | pos+=4; |
||
394 | evhb->bc=pos; |
||
395 | if (isLE) swap_4(&evhb->bc); |
||
396 | if (fwrite((void *) evbf, 1, pos, fout) != pos) return 0; |
||
397 | return 1; |
||
398 | } |