Attachment 'df_decode.c'
Download 1 /****************************************************************************
2 * *
3 * df_decode.c: Sample decode program *
4 * *
5 * Compile: gcc -Wall -O2 df_decode.c -o df_decode *
6 * *
7 ****************************************************************************/
8
9
10 #define _LARGEFILE64_SOURCE
11 #define _FILE_OFFSET_BITS 64
12
13 #define _GNU_SOURCE
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <sys/time.h>
25 #include <sys/mman.h>
26
27 #include "adcm_df.h"
28
29
30 void print_adcm_counters (struct adcm_counters_t * cnt)
31 {
32
33 int i;
34
35 if (!cnt)
36 return;
37 printf ("Counters run time:%e\n", cnt->time);
38 for (i = 0; i < ADC_NCH; i++)
39 if (cnt->rawhits[i])
40 printf ("Hits [%2d]: %10u\n", i, cnt->rawhits[i]);
41 }
42
43 /*****************************************************************************/
44 int process_raw_buf (unsigned char *buf, size_t len)
45 {
46
47 size_t p = 0, dp = 0;
48 struct adcm_cmap_t cmap;
49 struct adcm_counters_t counters;
50 int nev = 0, nmap = 0, ncnt = 0;
51
52 memset (&counters, 0, sizeof (struct adcm_counters_t));
53 memset (&cmap, 0, sizeof (struct adcm_cmap_t));
54
55 while (((dp = p + sizeof (struct stor_packet_hdr_t)) < len)) {
56
57 struct stor_packet_hdr_t *hdr = (struct stor_packet_hdr_t *) & buf[p];
58
59 switch (hdr->id) {
60 case STOR_ID_CMAP:
61 nmap++;
62 memcpy (&cmap, &buf[dp], sizeof (struct adcm_cmap_t));
63 break;
64 case STOR_ID_EVNT:{
65 struct stor_ev_hdr_t *eh = (struct stor_ev_hdr_t *) & buf[dp];
66 int n;
67 // float t1 = 0, t2 = 0;
68 nev++;
69 printf ("TS %08X\n", eh->ts);
70 for (n = 0; n < eh->np; n++) {
71 struct stor_puls_t *hit = (struct stor_puls_t *) & buf[dp + sizeof (struct stor_ev_hdr_t) + n * sizeof (struct stor_puls_t)];
72 printf ("%d\t%f\t%f\t%f\n", hit->ch, hit->a, hit->t, hit->w);
73 /*
74 if (hit->ch == 0)
75 t1 = hit->t;
76 if (hit->ch == 1)
77 t2 = hit->t;
78 */
79 }
80 /*
81 if ((t1 > 0) && (t2 > 0))
82 printf ("%f\n", t2 - t1);
83 */
84 break;
85 }
86 case STOR_ID_CNTR:
87 ncnt++;
88 memcpy (&counters, &buf[dp], sizeof (struct adcm_counters_t));
89 break;
90 default:
91 fprintf (stderr, "Bad block ID %04X at pos %zu\n", hdr->id, p);
92 exit (1);
93 }
94 p += hdr->size;
95 }
96 fprintf (stderr, "Complete %zu bytes, %u events, %u counters, %u maps\n", p, nev, ncnt, nmap);
97 print_adcm_counters (&counters);
98 return (0);
99 }
100
101 /*****************************************************************************/
102
103 int main (int argc, char *argv[])
104 {
105 int fd;
106 struct stat statbuf;
107 size_t maplen, maxmaplen;
108 off_t nb, mapoff;
109 void *mapaddr;
110 double partdone;
111 char dumpfilename[1024];
112
113
114 if (argc == 2) {
115 strncpy (dumpfilename, argv[1], 1023);
116 } else {
117 fprintf (stderr, "USAGE: %s datafile\n", argv[0]);
118 exit (1);
119 }
120
121 if ((stat (dumpfilename, &statbuf))) {
122 fprintf (stderr, "stat('%s') failed: %s\n", dumpfilename, strerror (errno));
123 exit (1);
124 }
125 fd = open (dumpfilename, O_RDONLY);
126 if (fd < 0) {
127 fprintf (stderr, "open('%s') failed: %s\n", dumpfilename, strerror (errno));
128 exit (1);
129 }
130 nb = statbuf.st_size;
131
132 mapoff = 0;
133 // maxmaplen = 1073741824L; // 1G
134 maxmaplen = 10485760L; // 10M
135 while (mapoff < nb) {
136 maplen = nb - mapoff;
137 if (maplen > maxmaplen)
138 maplen = maxmaplen;
139 mapaddr = mmap (0, maplen, PROT_READ, MAP_SHARED, fd, mapoff);
140 if (mapaddr == MAP_FAILED) {
141 fprintf (stderr, "mmap('%s') failed: %s\n", dumpfilename, strerror (errno));
142 exit (1);
143 }
144 printf ("Mapped %zu bytes from %lld to addr %p\n", maplen, mapoff, mapaddr);
145 process_raw_buf ((unsigned char *)mapaddr, maplen);
146 munmap (mapaddr, maplen);
147 mapoff += maplen;
148 partdone = 1. * mapoff / nb;
149 printf ("%lld MB done\n", mapoff / 1048576L);
150 //break;
151 }
152 close (fd);
153 return 0;
154 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.