source: sans/utils/Teabag/bin_evt.c @ 1249

Last change on this file since 1249 was 837, checked in by ajj, 11 years ago

Checkin of Teabag code for TISANE.

File size: 8.3 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#ifndef _WIN32
4#include <unistd.h>
5#endif
6#include <getopt.h>
7#include <string.h>
8#include <math.h>
9
10//Write sans data
11#include "sanswrite.h"
12#include "sa_rdwr.h"
13#include "sa_data.h"
14#include "sa_futil.h"
15#include "sa_img.h"
16
17unsigned int * histo;
18
19#define AIMTYPE_XY  0 // XY Event
20#define AIMTYPE_XYM 2 // XY Minor event
21#define AIMTYPE_MIR 1 // Minor rollover event
22#define AIMTYPE_MAR 3 // Major rollover event
23
24#define USECSPERTICK 0.1 // microseconds
25#define TICKSPERUSEC 10
26#define MAXBINS 2000
27#define XBINS   128
28#define YBINS   128
29
30#define MAJOR_VERSION 1
31#define MINOR_VERSION 0
32
33union AIMEvent {
34        unsigned int raw;
35        struct word {
36                unsigned short tildax : 8;
37                unsigned short y : 8;
38                unsigned short time : 13;
39                unsigned short ppto : 1;
40                unsigned short type : 2;
41        } word;
42} AimEvent;
43
44struct counter {
45        int events;
46        int t0;
47        int minor_rollover;
48        int major_rollover;
49        int pp;
50        int dl;
51} counter;
52
53int time_msw, time_lsw, x, y, t, t_longest;
54int t_lookup[MAXBINS], nbins;
55int countsums[MAXBINS];
56unsigned int *histo;
57
58/*
59 static char buf[82];
60 pbin(long unsigned int val,int bits){
61 int j=bits;
62 char *p=buf;
63 if (bits>64) return NULL;
64 while( --j >= 0 ){
65 *p++ = ((val & (1 << j)) ? '1' : '0');
66 if ((j%4) == 0) *p++ =' ';
67 }
68 *--p = '\0';               
69 return buf;
70 }
71 */
72
73void SetupBins(float binwidth, int tbins)
74{
75        int tickwidth, i, boundary;
76        tickwidth = (floor) (binwidth/USECSPERTICK);
77        boundary = 0;
78        memset(&t_lookup[0],0,sizeof(t_lookup));
79        for (i=0;i<tbins;i++) {
80                t_lookup[i] = boundary;
81                printf("%d %d\n",i,boundary);
82                boundary += tickwidth;
83               
84        }
85        nbins = tbins;
86}
87
88int SetupHisto(int tbins) 
89{
90        int size;
91        size = sizeof(unsigned int) * XBINS * YBINS * (tbins+1);
92        printf("Histo size: %u bytes; %u elements\n",size,(XBINS*YBINS*tbins));
93        if ((histo = (unsigned int *) malloc(size)) == NULL) {
94                return -1;
95        }
96        return 0;
97}
98
99int FindBin(int ticks) 
100{
101        int i,whichbin=nbins-1; /* Bogus value for trash events */
102        for (i=0;i<nbins;i++) {
103                if (ticks > t_lookup[i]) {
104                        whichbin = i;
105                } else break;
106        }
107        return whichbin;
108}
109
110int ProcessEvent(unsigned int lword) 
111{
112        char label[10];
113        int  b;
114       
115        AimEvent.raw = lword;
116       
117        switch (AimEvent.word.type) {
118                case AIMTYPE_XY:
119                        sprintf(label,"XY ");
120                        x = ~AimEvent.word.tildax & 127;
121                        y = AimEvent.word.y & 127;
122                        time_lsw = AimEvent.word.time;
123                        t = (int) ((time_msw << 13) + time_lsw);
124                        if (AimEvent.word.ppto) counter.pp++;
125                        //if (AimEvent.word.tildax > 127){
126                        //      printf("%s : %#x :: %u (%u), %u : %u\n",&label,lword,x,AimEvent.word.tildax,y,t);
127                        //}
128                        // Bin XY event
129                        if (t > t_longest) t_longest = t;
130                        b = FindBin(t);
131                        //printf("%s : %#x : %u : %u : %#x\n", &label,AimEvent.raw,t,b,histo);
132                        *(histo + XBINS*YBINS*b + XBINS*y + x) += 1;
133                        countsums[b]++;
134                        counter.events++;
135                        break;
136                case AIMTYPE_XYM:
137                        sprintf(label,"XYM");
138                        x = ~AimEvent.word.tildax & 127;
139                        y = AimEvent.word.y & 127;
140                        //if (AimEvent.word.tildax > 127){
141                        //      printf("%s : %#x :: %u (%u), %u : %u\n",&label,lword,x,AimEvent.word.tildax,y,t);
142                        //}
143                        time_lsw = AimEvent.word.time;
144                        if (AimEvent.word.ppto) counter.pp++;
145                       
146                        break;
147                case AIMTYPE_MIR:
148                        sprintf(label,"MIR");
149                        time_msw = AimEvent.word.time;
150                        t = (int) ((time_msw << 13) + time_lsw);
151                        if (AimEvent.word.ppto) {
152                                counter.t0++;
153                        }
154                        if (t > t_longest) t_longest = t;
155                        // Bin XYM event
156                        b = FindBin(t);
157                        //printf("%s : %#x : %u : %u : %#x\n", &label,AimEvent.raw,t,b,histo);
158                        *(histo + XBINS*YBINS*b + XBINS*y + x) += 1;
159                        countsums[b]++;
160                        counter.events++;
161                        break;
162                case AIMTYPE_MAR:
163                        sprintf(label,"MAR");
164                        if (AimEvent.word.ppto) {
165                                counter.t0++;
166                        }
167        }
168        return 1;
169}
170
171void DumpHistogram(const char * root, int bin)
172{
173        unsigned int * pHist;
174        char outfile[128];
175        FILE * fp;
176        int i, j;
177       
178        pHist = (histo + XBINS*YBINS*bin);
179        sprintf(outfile,"%s_%03d.dat",root,bin);
180        printf("Writing slice %3d to \"%s\"\n",bin,outfile);
181        if ((fp = fopen(outfile,"w")) == NULL) {
182                fprintf(stderr,"Unable to open output file \"%s\" for writing.\n",outfile);
183                return;
184        }
185        for (j=0;j<YBINS;j++) {
186                for (i=0;i<XBINS;i++) {
187                        fprintf(fp," %7d",*(pHist + XBINS*j + i));
188                }
189                fprintf(fp,"\n");
190        }
191        fclose(fp);
192}
193
194void DumpVAXBinary(const char * root, int bin)
195{
196        int retn,i,j,detcounts;
197        int byteorder = 0;
198        char outfile[128];
199        FILE * fp;
200        unsigned int * pHist;
201        SANSHDR header;
202        int hist[128][128];
203       
204    sprintf(outfile,"%.5s%3.3d.SA3_TIS_T%3.3d",root,bin+1,bin+1);
205       
206        retn = initdata(&header,hist, outfile);
207       
208        if ((fp = fopen(outfile,"wb")) == NULL){
209                fprintf(stderr,"Couldn't write output file\n");
210                return;
211        }
212       
213       
214        pHist = (histo + XBINS*YBINS*bin);
215       
216        detcounts = 0;
217       
218        for (j=0;j<YBINS;j++) {
219                for (i=0;i<XBINS;i++) {
220                        hist[j][i] = *(pHist +XBINS*j +i);
221                        detcounts += hist[j][i];
222                }
223        }
224       
225        //Put in correct detector counts
226        header.run.detcnt = detcounts;
227       
228        if ((retn = WriteSansHdr(fp,&header,byteorder)) !=0 ) {
229                fprintf(stderr,"Problem encountered writing data\b");
230                return;
231        }
232       
233        if ((retn = WriteSansHist(fp,hist,128,128,byteorder)) !=0 ) {
234                fprintf(stderr,"Problem encountered writing data\b");
235                return;
236        }
237       
238}
239
240
241void DumpAllHistograms(const char * root, int tbins)
242{
243        int i;
244        for (i=0;i<tbins;i++) {
245                DumpHistogram(root,i);
246        }
247}
248
249void DumpAllVAXBinary(const char * root, int tbins)
250{
251        int i;
252        for (i=0;i<tbins;i++) {
253                DumpVAXBinary(root,i);
254        }
255}
256
257/*void DumpAllGRASP(const char * root, int tbins)
258{
259        int i;
260        for (i=0; i<tbins; i++) {
261                DumpGRASP(root,i);
262        }
263       
264}*/
265
266int main(int argc, char * argv[])
267{
268        FILE * fp;
269        char buf[128],evtfile[128],outprefix[6], root[128],*ptr,ch;
270        unsigned int lword;
271        int n,i;
272        int nwords = 0;
273        int nslices = 40;
274        int binsize = 5000; // 5ms
275        // Flags
276        int verbose=0,dumphisto=0, dumpvax=0, printstats=0, hasprefix=0;
277       
278        strcpy(evtfile,"event.log");
279        memset(&counter,0,sizeof(counter));
280        memset(&countsums,0,sizeof(countsums));
281       
282        while ((ch = getopt(argc, argv, "vf:n:t:dxs:p:h")) != -1)
283                switch (ch) {
284                        case 't':
285                                if ((n = sscanf(optarg,"%d",&binsize)) != 1) {
286                                        fprintf(stderr,"Error obtaining time slice bin size in microseconds\n");
287                                        exit(1);
288                                }
289                                break;
290                        case 'f':
291                                strcpy(evtfile,optarg);
292                                break;
293                        case 'n':
294                                if ((n = sscanf(optarg,"%d",&nslices)) != 1) {
295                                        fprintf(stderr,"Error obtaining number of time slices.\n");
296                                        exit(1);
297                                }
298                                break;
299                               
300                        case 'v':
301                                verbose = 1;
302                                break;
303                        case 'd':
304                                dumphisto = 1;
305                                break;
306                        case 'x':
307                                dumpvax = 1;
308                                break;
309                        case 's':
310                                printstats = 1;
311                                break;
312                        case 'p':
313                                if (dumpvax == 1){
314                                        strcpy(outprefix,optarg);
315                                        hasprefix=1;
316                                } else {
317                                        fprintf(stderr,"-p option is only valid with -x\n");
318                                }
319                                break;
320                        case 'h':
321                        default:
322                                fprintf(stderr,"Version %d.%d\n", MAJOR_VERSION,MINOR_VERSION);
323                                fprintf(stderr,"Usage: Teabag [-vdxs] [-n slices] [-t timeslice(us)] ");
324                                fprintf(stderr,"[-f eventfile] [-p prefix]\n");
325                                fprintf(stderr,"     -v  verbose\n");
326                                fprintf(stderr,"     -d  dump histograms as ascii\n");
327                                fprintf(stderr,"     -x  dump histograms as VAX binary\n");
328                                fprintf(stderr,"     -s  print statistics\n");
329                                fprintf(stderr,"\neventfile\tThe name of the event log file from NISTO\
330                                                \n\t\tNB This should be contain the hexadecimal representation, not ASCII\
331                                                \n\nprefix\t\tThe 5 character prefix to be used for vax output files.\
332                                                \n\t\tThis option is only valid with -v\
333                                                \n\t\tNote that for GRASP compatibility it should be of the format 3 characters+ 2 digits\n");
334                                exit(1);
335                }
336       
337        if (verbose) printf("Opening log file \"%s\"\n",evtfile);
338       
339        SetupBins(binsize, nslices);
340        SetupHisto(nslices);
341       
342        if ((fp = fopen(evtfile,"r")) == NULL) {
343                fprintf(stderr,"Unable to open event log file \"%s\".\n",evtfile);
344                exit(1);
345        }
346       
347        time_msw=0;
348        time_lsw=0;
349        t_longest = 0;
350        //  while(fread(&lword, sizeof(lword), 1, fp) != 0) {
351        while(fgets(buf,sizeof(buf),fp)) {
352                if (sscanf(buf,"%x",&lword)) {
353                        ProcessEvent(lword);
354                        nwords++;
355                }
356                //if (nwords > 10){
357                //      break;
358                //}
359        }
360        printf("Events processed:     %10d\n",counter.events);
361        printf("Longest time (ticks): %10d\n",t_longest);
362        for (i=0;i< nslices;i++) {
363                printf("%2d\t%6d\n",i,countsums[i]);
364        }
365        fclose(fp);
366       
367        if (dumphisto) {
368                strcpy(root,evtfile);
369                ptr = strchr(root,'.');
370                *ptr = 0;
371                DumpAllHistograms(root,nslices);
372        }
373       
374        if (dumpvax) {
375                if (hasprefix){
376                        strncpy(root,outprefix,5);
377                } else {
378                        strncpy(root,evtfile,5);
379                }
380               
381                DumpAllVAXBinary(root,nslices);
382        }
383        return 0;
384}
Note: See TracBrowser for help on using the repository browser.