Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
291 | f9daq | 1 | #include "stdio.h" |
2 | #include "TROOT.h" |
||
3 | #include "TSystem.h" |
||
4 | #include "TFile.h" |
||
5 | #include "TH1F.h" |
||
6 | #include "TH2F.h" |
||
7 | #include "TF1.h" |
||
8 | #include "TMath.h" |
||
9 | #include "TStyle.h" |
||
10 | #include "TCanvas.h" |
||
11 | #include "TLine.h" |
||
12 | |||
13 | //~ #define USE_ZLIB |
||
14 | #ifdef USE_ZLIB |
||
15 | # include "zlib.h" |
||
16 | #endif |
||
17 | // ------------------------------------------------------------------------------ |
||
18 | |||
19 | #define POSMARG 1000 |
||
20 | |||
21 | #define READBUFFERLENGTH 50000 |
||
22 | |||
23 | // data format |
||
24 | #define MAXDATA 40 |
||
25 | |||
26 | #define RUNREC_ID 1 |
||
27 | #define ENDREC_ID 2 |
||
28 | #define POSREC_ID 3 |
||
29 | #define EVTREC_ID 4 |
||
30 | |||
31 | typedef struct { |
||
32 | unsigned long id,len; |
||
33 | unsigned long fver,time; |
||
34 | unsigned long nev,nch,ped,xy; |
||
35 | long nx,x0,dx,ny,y0,dy; |
||
36 | } RUNREC; |
||
37 | RUNREC *runrec; |
||
38 | RUNREC run; |
||
39 | |||
40 | typedef struct { |
||
41 | unsigned long id,len; |
||
42 | unsigned long time; |
||
43 | } ENDREC; |
||
44 | ENDREC *endrec; |
||
45 | |||
46 | typedef struct { |
||
47 | unsigned long id,len; |
||
48 | unsigned long time; |
||
49 | long ix,x,xset,iy,y,yset; |
||
50 | } POSREC; |
||
51 | POSREC *posrec; |
||
52 | POSREC pos; |
||
53 | |||
54 | typedef struct { |
||
55 | unsigned long id,len; |
||
56 | unsigned long nev; |
||
57 | unsigned short data[MAXDATA]; |
||
58 | } EVTREC; |
||
59 | EVTREC *evtrec; |
||
60 | |||
61 | // ------------------------------------------------------------------------------ |
||
62 | |||
63 | int d2time(char* dfile0="test") |
||
64 | { |
||
65 | unsigned long startTime = 0; |
||
66 | unsigned long endTime = 0; |
||
67 | |||
68 | |||
69 | |||
70 | int dbg=0; |
||
71 | printf(" dat 2 root conversion program\nUsage:\nd2r(input file -.dat, TDC min [ps], TDC max [ps], QDC min, QDC max)\n\n"); |
||
72 | |||
73 | char fullname[256], sbuff[256]; |
||
74 | FILE *fp; |
||
75 | |||
76 | |||
77 | //data buffer |
||
78 | char readbuf[READBUFFERLENGTH]; |
||
79 | runrec = (RUNREC *) readbuf; |
||
80 | endrec = (ENDREC *) readbuf; |
||
81 | posrec = (POSREC *) readbuf; |
||
82 | evtrec = (EVTREC *) readbuf; |
||
83 | |||
84 | //data file |
||
85 | #ifdef USE_ZLIB |
||
86 | gzFile dfp; |
||
87 | #else |
||
88 | FILE *dfp; |
||
89 | #endif |
||
90 | char dfile[256]; |
||
91 | int ftype=0, fcount=1; |
||
92 | do { |
||
93 | switch(ftype++) { |
||
94 | case 0: |
||
95 | sprintf(dfile, "data/%s_file%02d.dat", dfile0, fcount); |
||
96 | break; |
||
97 | case 1: |
||
98 | sprintf(dfile, "data/%s_file%02d.dat.gz", dfile0, fcount); |
||
99 | break; |
||
100 | case 2: |
||
101 | sprintf(dfile, "data/%s_file%02d.gz", dfile0, fcount); |
||
102 | break; |
||
103 | default: |
||
104 | printf(" Cannot find data file for %s !!!\n", dfile0); |
||
105 | return -1; |
||
106 | } |
||
107 | #ifdef USE_ZLIB |
||
108 | dfp=gzopen(dfile,"rb"); |
||
109 | #else |
||
110 | dfp=fopen(dfile,"rb"); |
||
111 | #endif |
||
112 | |||
113 | } while(!dfp); |
||
114 | printf("Opened data file %s\n", dfile); |
||
115 | |||
116 | // ----------------------------------------------- |
||
117 | // loop trough records |
||
118 | UInt_t rec_id, rec_len; |
||
119 | unsigned ulsize = 4; |
||
120 | int ceve=0; |
||
121 | int end_of_file = 0; |
||
122 | |||
123 | while(1) { |
||
124 | #ifdef USE_ZLIB |
||
125 | if(gzeof(dfp)) end_of_file = 1; |
||
126 | #else |
||
127 | if(feof(dfp)) end_of_file = 1; |
||
128 | #endif |
||
129 | |||
130 | #ifdef USE_ZLIB |
||
131 | gzread(dfp, (voidp)&readbuf, 2*ulsize); |
||
132 | #else |
||
133 | fread( (void*)&readbuf, 2*ulsize, 1, dfp); |
||
134 | #endif |
||
135 | rec_id=readbuf[0]; |
||
136 | rec_len=readbuf[ulsize]; |
||
137 | |||
138 | if(dbg) printf("-----------------------------------------------\n"); |
||
139 | if(dbg) printf("[%d] rec_id = %d | rec_len = %d\n", ceve, rec_id, rec_len); |
||
140 | |||
141 | switch(rec_id) |
||
142 | { |
||
143 | case RUNREC_ID: |
||
144 | #ifdef USE_ZLIB |
||
145 | gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize)); |
||
146 | #else |
||
147 | fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp); |
||
148 | #endif |
||
149 | run=*runrec; |
||
150 | |||
151 | if(dbg) { |
||
152 | printf("RUNREC_ID\n"); |
||
153 | printf("id = %d, len = %d, time = %d\n", run.id, run.len, run.time); |
||
154 | printf("nev = %d, nch = %d\n", run.nev, run.nch); |
||
155 | printf("nx = %d, x0 = %d, dx = %d\n", run.nx, run.x0, run.dx); |
||
156 | printf("ny = %d, y0 = %d, dy = %d\n", run.ny, run.y0, run.dy); |
||
157 | |||
158 | break; |
||
159 | } |
||
160 | |||
161 | printf(">>>>>>>>>>>> RUNREC time = %u\n", run.time); |
||
162 | startTime = run.time; |
||
163 | |||
164 | break; |
||
165 | |||
166 | case POSREC_ID: |
||
167 | #ifdef USE_ZLIB |
||
168 | gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize)); |
||
169 | #else |
||
170 | fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp); |
||
171 | #endif |
||
172 | |||
173 | pos=*posrec; |
||
174 | |||
175 | if(dbg) { |
||
176 | printf("POSREC_ID\n"); |
||
177 | printf("id = %d, len = %d, time = %d\n", posrec->id, posrec->len, posrec->time); |
||
178 | printf("ix = %d, x = %d, xset = %d\n", posrec->ix, posrec->x, posrec->xset); |
||
179 | printf("iy = %d, y = %d, yset = %d\n", posrec->iy, posrec->y, posrec->yset); |
||
180 | } else printf(" [%d,%d] %d, %d\n", pos.ix, pos.iy, pos.xset, pos.yset); |
||
181 | |||
182 | break; |
||
183 | |||
184 | case EVTREC_ID: |
||
185 | #ifdef USE_ZLIB |
||
186 | gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize)); |
||
187 | #else |
||
188 | fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp); |
||
189 | #endif |
||
190 | |||
191 | |||
192 | if(dbg) { |
||
193 | printf("EVTREC_ID\n"); |
||
194 | printf("id = %d, len = %d, nev = %d\n", evtrec->id, evtrec->len, evtrec->nev); |
||
195 | //for(int datai = 0; datai < NCH; datai++) printf("%u ", evtrec->data[datai]); |
||
196 | //printf("\n"); |
||
197 | //for(int datai = NCH; datai < NCH+NCH; datai++) printf("%u ", evtrec->data[datai]); |
||
198 | //printf("\n"); |
||
199 | //break; |
||
200 | } |
||
201 | |||
202 | break; |
||
203 | |||
204 | case ENDREC_ID: |
||
205 | #ifdef USE_ZLIB |
||
206 | gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize)); |
||
207 | #else |
||
208 | fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp); |
||
209 | #endif |
||
210 | |||
211 | if(dbg) { |
||
212 | printf("ENDREC_ID\n"); |
||
213 | printf("id = %d, len = %d, time = %d\n", endrec->id, endrec->len, endrec->time); |
||
214 | } else printf(" ENDREC\n"); |
||
215 | |||
216 | |||
217 | printf(">>>>>>>>>>>> ENDREC time = %u\n", endrec->time); |
||
218 | endTime = endrec->time; |
||
219 | |||
220 | fcount++; |
||
221 | switch(ftype-1) { |
||
222 | case 0: |
||
223 | sprintf(dfile, "data/%s_file%02d.dat", dfile0, fcount); |
||
224 | break; |
||
225 | case 1: |
||
226 | sprintf(dfile, "data/%s_file%02d.dat.gz", dfile0, fcount); |
||
227 | break; |
||
228 | case 2: |
||
229 | sprintf(dfile, "data/%s_file%02d.gz", dfile0, fcount); |
||
230 | break; |
||
231 | } |
||
232 | |||
233 | #ifdef USE_ZLIB |
||
234 | if(dfp) gzclose(dfp); |
||
235 | dfp=gzopen(dfile,"rb"); |
||
236 | #else |
||
237 | if(dfp) fclose(dfp); |
||
238 | dfp=fopen(dfile,"rb"); |
||
239 | #endif |
||
240 | |||
241 | if(!dfp) { |
||
242 | printf(" Cannot open data file: %s ---> Exiting\n", dfile); |
||
243 | end_of_file = 1; |
||
244 | } else { |
||
245 | printf(" Opened data file: %s\n", dfile); |
||
246 | end_of_file = 0; |
||
247 | } |
||
248 | |||
249 | break; |
||
250 | |||
251 | default: |
||
252 | printf("switch(rec_id): default !!!\n"); |
||
253 | end_of_file = 1; |
||
254 | break; |
||
255 | } |
||
256 | |||
257 | ceve++; |
||
258 | if( (ceve%50000) == 0) printf(" Current event = %d\n", ceve); |
||
259 | if(dbg) if( ceve>dbg ) break; |
||
260 | if(end_of_file) break; |
||
261 | } |
||
262 | |||
263 | |||
264 | #ifdef USE_ZLIB |
||
265 | if(dfp) gzclose(dfp); |
||
266 | #else |
||
267 | if(dfp) fclose(dfp); |
||
268 | #endif |
||
269 | |||
270 | |||
271 | printf(">>>>>>>>>>>> TIME = ENDREC.time - RUNREC.time = %u (s) = %.2lf (min)\n", endTime - startTime, (endTime - startTime)/60.); |
||
272 | |||
273 | if(dbg) return 1; |
||
274 | |||
275 | gSystem->Exit(1); |
||
276 | |||
277 | return 1; |
||
278 | } |