Rev 34 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
29 | f9daq | 1 | #define USE_DAQ |
2 | #define USE_MIKRO |
||
3 | |||
4 | // Izberi ustrezni interface v meniju projektnega okna |
||
5 | // Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME) |
||
6 | #ifdef USE_DAQ |
||
7 | # define USE_CAMAC |
||
32 | f9daq | 8 | #include "c:\HOME\dino\sipmScan\include\camac.h" |
29 | f9daq | 9 | # ifdef SISVME |
10 | # include "sisvme_dll.h" |
||
11 | # endif |
||
12 | # ifdef WIENVME |
||
13 | # include "wienvme_dll.h" |
||
14 | # endif |
||
15 | # include "CAENV673A.h" |
||
34 | f9daq | 16 | # include "CAENV462.h" |
17 | |||
29 | f9daq | 18 | #endif |
19 | |||
20 | #ifdef USE_MIKRO |
||
21 | # include "MIKRO.h" |
||
22 | #endif |
||
23 | |||
24 | #include <userint.h> |
||
25 | #include <ansi_c.h> |
||
26 | #include <utility.h> |
||
27 | #include <analysis.h> |
||
28 | |||
33 | f9daq | 29 | #include "c:\HOME\dino\sipmScan\include\sipmBias_ui.h" |
29 | f9daq | 30 | |
31 | #ifdef USE_DAQ |
||
32 | # define USE_CAMAC |
||
33 | # include "camac.h" |
||
34 | f9daq | 34 | # define NDAC 1 // CAEN C221 |
35 | # define ASD8 0 // channels |
||
36 | # define AMPDISSH 1 |
||
90 | f9daq | 37 | # define SIPM 15 |
29 | f9daq | 38 | # define CAEN_V673A 0x22220000 // IJS |
39 | //#define CAEN_V673A 0x10110000 // FMF |
||
40 | # define CAEN_V462 0x100300 |
||
41 | #endif |
||
42 | |||
43 | #ifdef USE_MIKRO |
||
90 | f9daq | 44 | # define MIKRO_COM 3 |
29 | f9daq | 45 | # define MIKRO_X 1 |
46 | # define USE_MIKRO_Y |
||
47 | # define MIKRO_Y 2 |
||
48 | # define USE_MIKRO_Z |
||
49 | # define MIKRO_Z 3 |
||
90 | f9daq | 50 | # define STEP_TOLERANCE 1 |
29 | f9daq | 51 | #endif |
52 | |||
53 | #define MAXCH 512 |
||
54 | #define MAX_THREADS 10 |
||
55 | |||
56 | #define IWAIT 200 |
||
57 | |||
58 | #define NCH 64 |
||
34 | f9daq | 59 | #define POWERSUPPLY 66 // Power supply voltage for SiPM bias |
90 | f9daq | 60 | #define OFFSETX 5600 // position of the lower right corner |
61 | #define OFFSETY 5200 |
||
29 | f9daq | 62 | |
63 | static int p1h, pID, rID, tfID; |
||
64 | static int ph_tdc, ph_adc; |
||
65 | static int dtdc[NCH][2][MAXCH]; |
||
66 | static int something[NCH][2][MAXCH]; |
||
67 | |||
68 | static int daq_on; |
||
69 | static int poolHandle = 0; |
||
70 | static int ntics,dummy; |
||
71 | |||
72 | /************************************************************/ |
||
73 | void wait_loop(unsigned long iloop) |
||
74 | |||
75 | { |
||
76 | int i; |
||
77 | |||
78 | for (i=0;i<iloop;i++); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
83 | int eventData1, int eventData2) |
||
84 | { |
||
85 | QueueUserEvent (9000, p1h, P1_TIMER); |
||
86 | return (0); |
||
87 | } |
||
88 | |||
89 | int update_plots (void) |
||
90 | { |
||
91 | int irange, ch; |
||
92 | |||
93 | GetCtrlVal (p1h, P1_PLCH, &ch); |
||
94 | |||
95 | if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW); |
||
96 | GetCtrlVal (p1h, P1_TDCHL, &irange); |
||
97 | ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH, VAL_INTEGER, |
||
98 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); |
||
99 | |||
100 | if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW); |
||
101 | GetCtrlVal (p1h, P1_ADCHL, &irange); |
||
102 | ph_adc = PlotY (p1h, P1_ADC, &something[ch][irange], MAXCH, VAL_INTEGER, |
||
103 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE); |
||
104 | return (0); |
||
105 | } |
||
106 | |||
107 | |||
108 | int module_header(int recid,unsigned long *data,int len){ |
||
109 | data[0] = recid; |
||
110 | data[1] = (len >0)? len : 0 ; |
||
111 | return data[1]+2; |
||
112 | } |
||
113 | |||
114 | int SetDac(int, double); |
||
115 | |||
116 | int CVICALLBACK daq_run(void *functionData) |
||
117 | { |
||
118 | // *** |
||
119 | // variable declarations |
||
120 | int i,j; |
||
121 | int dtype,ch,rg,adc,cres,bsr; |
||
122 | |||
123 | //unsigned short aa[NCH][4]; |
||
124 | unsigned long tdc[NCH]; |
||
125 | unsigned long step_minutes, end_time_s, cur_time_s; |
||
126 | int start_hours, start_minutes, start_seconds; |
||
127 | |||
128 | int status,fmax,fcount,fev; |
||
129 | int popupret; |
||
130 | int mikroX; // check the position |
||
131 | int mikroY; // check the position |
||
132 | |||
133 | char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN]; |
||
134 | int supr0,tdcmin,fseed; |
||
135 | float frac; |
||
136 | double fracg; |
||
137 | unsigned short dum16; |
||
138 | int count; |
||
139 | int print = 0; |
||
140 | int timer_out = 0; |
||
141 | //time_t t,told, tstart; |
||
142 | int ntrig; |
||
143 | int chip, len; |
||
144 | unsigned long *pdata; |
||
145 | |||
146 | FILE *fp; |
||
34 | f9daq | 147 | #define RUNREC_ID 1 |
148 | #define ENDREC_ID 2 |
||
149 | #define POSREC_ID 3 |
||
150 | #define EVTREC_ID 4 |
||
151 | #define THRREC_ID 5 |
||
152 | #define BIASREC_ID 6 |
||
29 | f9daq | 153 | |
154 | typedef struct { |
||
155 | unsigned long id,len; |
||
156 | unsigned long fver,time; |
||
34 | f9daq | 157 | unsigned long thLow, thUp, thStep; |
158 | unsigned long biasLow, biasUp, biasStep; |
||
29 | f9daq | 159 | unsigned long nev,nch,ped,xy; |
160 | long nx,x0,dx,ny,y0,dy; |
||
161 | } RUNREC; |
||
162 | RUNREC runrec; |
||
163 | |||
164 | typedef struct { |
||
165 | unsigned long id,len; |
||
166 | unsigned long time; |
||
167 | } ENDREC; |
||
168 | ENDREC endrec; |
||
169 | |||
170 | typedef struct { |
||
171 | unsigned long id,len; |
||
172 | unsigned long time; |
||
173 | long ix,x,xset,iy,y,yset; |
||
174 | } POSREC; |
||
175 | POSREC posrec; |
||
176 | |||
177 | typedef struct { |
||
178 | unsigned long id,len; |
||
179 | unsigned long nev; |
||
180 | //unsigned short data[NCH]; |
||
181 | } EVTREC; |
||
182 | EVTREC evtrec; |
||
183 | |||
184 | typedef struct { |
||
185 | unsigned long id; |
||
186 | unsigned long len; |
||
187 | unsigned long threshold; |
||
188 | } THRREC; |
||
189 | THRREC thrrec; |
||
34 | f9daq | 190 | |
191 | typedef struct { |
||
192 | unsigned long id; |
||
193 | unsigned long len; |
||
194 | unsigned long bias; |
||
195 | } BIASREC; |
||
196 | BIASREC biasrec; |
||
29 | f9daq | 197 | |
198 | double thresholdLow; |
||
199 | double thresholdUp; |
||
200 | double thresholdStep; |
||
34 | f9daq | 201 | double biasLow; |
202 | double biasUp; |
||
203 | double biasStep; |
||
29 | f9daq | 204 | |
205 | #define BSIZE 20000 |
||
206 | |||
207 | int maxn = BSIZE-4; // 2 words x 2 headers |
||
208 | int tdcdata, edge_type, overflow, tdc_num, channel, ev_dat, last_dat, nval_dat; |
||
209 | // end of declarations |
||
210 | // *** |
||
211 | |||
212 | runrec.id = RUNREC_ID; |
||
213 | runrec.len = sizeof(runrec); |
||
214 | //runrec.fver = 0x10000; |
||
215 | runrec.nch = NCH; |
||
216 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1; |
||
217 | GetCtrlVal(p1h, P1_DEBUG, &print); |
||
218 | endrec.id = ENDREC_ID; |
||
219 | endrec.len = sizeof(endrec); |
||
220 | posrec.id = POSREC_ID; |
||
221 | posrec.len = sizeof(posrec); |
||
222 | evtrec.id = EVTREC_ID; |
||
223 | evtrec.len = sizeof(evtrec); |
||
224 | thrrec.id = THRREC_ID; |
||
225 | thrrec.len = sizeof(thrrec); |
||
34 | f9daq | 226 | biasrec.id = BIASREC_ID; |
227 | biasrec.len = sizeof(biasrec); |
||
29 | f9daq | 228 | |
229 | |||
230 | cres = 0; |
||
231 | |||
232 | GetCtrlVal (p1h, P1_NEVE, &runrec.nev); |
||
233 | GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped); |
||
234 | |||
235 | GetCtrlVal (p1h, P1_NX, &runrec.nx); |
||
236 | GetCtrlVal (p1h, P1_XSTEP, &runrec.dx); |
||
237 | GetCtrlVal (p1h, P1_XMIN, &runrec.x0); |
||
238 | GetCtrlVal (p1h, P1_NY, &runrec.ny); |
||
239 | GetCtrlVal (p1h, P1_YSTEP, &runrec.dy); |
||
240 | GetCtrlVal (p1h, P1_YMIN, &runrec.y0); |
||
241 | |||
242 | GetCtrlVal (p1h, P1_NMIN, &step_minutes); |
||
243 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
244 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
245 | time(&cur_time_s); |
||
246 | end_time_s = cur_time_s + step_minutes*60; |
||
247 | printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, cur_time_s, end_time_s); |
||
248 | |||
249 | //GetCtrlVal (p1h, P1_DSAVE, &dsave); |
||
250 | //if (dsave) { |
||
251 | GetCtrlVal (p1h, P1_DFILE, dfile0); |
||
252 | |||
253 | fev=0; |
||
254 | fcount=1; |
||
255 | GetCtrlVal (p1h, P1_NEWF, &fmax); |
||
256 | fmax*=1000000;//fmax in Mega Bytes |
||
257 | //} |
||
258 | GetCtrlVal (p1h, P1_SUPR, &supr0); |
||
259 | if (supr0) { |
||
260 | GetCtrlVal (p1h, P1_TDCMIN, &tdcmin); |
||
261 | GetCtrlVal (p1h, P1_FRAC, &frac); |
||
262 | } |
||
263 | |||
34 | f9daq | 264 | GetCtrlVal (p1h, P1_SASD8THR, &thresholdLow); |
29 | f9daq | 265 | runrec.thLow = thresholdLow*1000; // mV |
266 | GetCtrlVal (p1h, P1_STHRUP, &thresholdUp); |
||
267 | runrec.thUp = thresholdUp*1000; // mV |
||
268 | GetCtrlVal (p1h, P1_STEP, &thresholdStep); |
||
269 | runrec.thStep = thresholdStep*1000; // mV |
||
270 | |||
34 | f9daq | 271 | GetCtrlVal (p1h, P1_SBIAS, &biasLow); |
272 | GetCtrlVal (p1h, P1_SBIASUP, &biasUp); |
||
273 | GetCtrlVal (p1h, P1_SBIASSTEP, &biasStep); |
||
274 | runrec.biasLow = biasLow*1000; |
||
275 | runrec.biasUp = biasUp*1000; |
||
276 | runrec.biasStep = biasStep*1000; |
||
29 | f9daq | 277 | |
278 | #ifdef USE_DAQ |
||
279 | V673A_map(0,CAEN_V673A,0); |
||
280 | V673A_init(0); |
||
281 | |||
282 | V462_map(0,CAEN_V462,0); |
||
283 | V462_set0(0,1); |
||
284 | #endif |
||
285 | |||
286 | //if (dsave) { |
||
287 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
90 | f9daq | 288 | SetCtrlVal(p1h, P1_FCOUNT, fcount); |
29 | f9daq | 289 | |
290 | fp = fopen (dfile, "wb"); |
||
291 | time (&runrec.time); |
||
292 | status = fwrite (&runrec, 1, runrec.len, fp); |
||
293 | //} |
||
294 | if (supr0) { |
||
295 | fseed = runrec.time & 0x7fffffff; |
||
296 | Uniform (1, fseed, &fracg); |
||
297 | } |
||
34 | f9daq | 298 | |
299 | // Set SiPM bias voltage |
||
300 | //SetDac(SIPM, 1.0); |
||
301 | biasrec.bias = runrec.biasLow; |
||
302 | while (biasrec.bias <= runrec.biasUp) { |
||
303 | SetDac(SIPM, biasrec.bias/1000.0 - POWERSUPPLY); |
||
304 | SetCtrlVal(p1h, P1_SBIAS, biasrec.bias/1000.0); |
||
305 | status = fwrite(&biasrec, 1, biasrec.len, fp); |
||
306 | if (print) printf("BIASREC status %d len %d bias %d\n", status, biasrec.len, biasrec.bias); |
||
90 | f9daq | 307 | wait_loop(10000); |
34 | f9daq | 308 | |
309 | // Set ASD threshold voltage |
||
29 | f9daq | 310 | thrrec.threshold = (unsigned long) ceil(thresholdLow*1000); // threshold in mV |
311 | while (thrrec.threshold <= thresholdUp*1000) { |
||
34 | f9daq | 312 | SetDac(ASD8, thrrec.threshold/1000.0); |
313 | SetCtrlVal(p1h, P1_SASD8THR, thrrec.threshold/1000.0); |
||
29 | f9daq | 314 | status = fwrite(&thrrec, 1, thrrec.len, fp); |
315 | if (print) printf("THRREC status %d len %d threshold %d\n", status, thrrec.len, thrrec.threshold); |
||
90 | f9daq | 316 | wait_loop(10000); |
29 | f9daq | 317 | |
318 | for (posrec.ix=0; posrec.ix<runrec.nx; posrec.ix++) { |
||
319 | posrec.xset = runrec.x0 + posrec.ix*runrec.dx; |
||
320 | #ifdef USE_MIKRO |
||
321 | //printf("MIKRO_MoveTo (1, x);%d\n",posrec.x); |
||
322 | do { |
||
90 | f9daq | 323 | MIKRO_MoveTo (MIKRO_X, OFFSETX + posrec.xset); |
29 | f9daq | 324 | MIKRO_GetPosition(MIKRO_X, &mikroX); |
90 | f9daq | 325 | mikroX -= OFFSETX; |
29 | f9daq | 326 | } while (abs(posrec.xset - mikroX) > STEP_TOLERANCE); |
327 | //printf("->MIKRO_MoveTo (1, x);%d\n",posrec.xset); |
||
328 | #endif |
||
329 | //SetCtrlVal (p1h, P1_X, posrec.xset); |
||
330 | posrec.xset = mikroX; // set the true value |
||
331 | SetCtrlVal(p1h, P1_X, mikroX); |
||
332 | SetCtrlVal (p1h, P1_IX, posrec.ix); |
||
333 | for (posrec.iy=0; posrec.iy<runrec.ny; posrec.iy++) { |
||
334 | posrec.yset = runrec.y0 + posrec.iy*runrec.dy; |
||
335 | #ifdef USE_MIKRO_Y |
||
336 | //printf("MIKRO_MoveTo (2, y);%d\n",y); |
||
337 | do { |
||
90 | f9daq | 338 | MIKRO_MoveTo (MIKRO_Y, OFFSETY + posrec.yset); |
29 | f9daq | 339 | MIKRO_GetPosition(MIKRO_Y, &mikroY); |
90 | f9daq | 340 | mikroY -= OFFSETY; |
29 | f9daq | 341 | } while (abs(posrec.yset - mikroY) > STEP_TOLERANCE); |
342 | //printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset); |
||
343 | #endif |
||
344 | //SetCtrlVal (p1h, P1_Y, posrec.yset); |
||
345 | posrec.yset = mikroY; // set the true value |
||
346 | SetCtrlVal (p1h, P1_Y, mikroY); |
||
347 | SetCtrlVal (p1h, P1_IY, posrec.iy); |
||
348 | //if (dsave) { |
||
349 | if (fmax && (ftell(fp) > fmax)) { |
||
350 | fcount+=1; |
||
351 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
352 | fclose(fp); |
||
353 | fp = fopen (dfile, "wb"); |
||
354 | } |
||
355 | |||
356 | time (&posrec.time); |
||
357 | status = fwrite (&posrec, 1, posrec.len, fp); |
||
358 | if (print) printf("POSREC status %d len %d\n", status, posrec.len); |
||
359 | //} |
||
360 | |||
361 | // clear the plots |
||
362 | for (j=0;j<NCH;j++) { |
||
363 | for (i=0;i<MAXCH;i++){ |
||
364 | dtdc[j][0][i]=0; |
||
365 | dtdc[j][1][i]=0; |
||
366 | } |
||
367 | } |
||
368 | |||
369 | evtrec.nev=1; |
||
370 | //time(&t); |
||
371 | //tstart=t; |
||
372 | if (print) printf("RUN START.\n"); |
||
373 | |||
374 | do { |
||
375 | unsigned long word[BSIZE]; |
||
376 | count=0; |
||
377 | ntics=0; |
||
378 | if (print) printf("Event counter: %d\n", evtrec.nev); |
||
379 | |||
380 | //if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t)); |
||
381 | |||
382 | #ifdef USE_DAQ |
||
383 | #ifdef CAEN_V673A |
||
384 | |||
385 | |||
386 | V462_start0(0); |
||
387 | //tmlnk (tout); |
||
388 | do { |
||
389 | ntrig = V673A_ntrig(0); |
||
390 | } while (ntrig==0 &&(ntics<2)); |
||
391 | //tmulk(); |
||
392 | //if (timer_out) { |
||
393 | // fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
394 | // V673A_clallb(0); |
||
395 | // continue; |
||
396 | //} |
||
397 | |||
398 | if (ntics>=2) { |
||
399 | fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
400 | printf("Timer tics %d\n", ntics); |
||
401 | ntics = 0; |
||
402 | evtrec.nev--; |
||
403 | V673A_clallb(0); |
||
404 | continue; |
||
405 | } |
||
406 | |||
407 | if (ntrig>0){ |
||
408 | // chip readout |
||
409 | for (chip=0;chip<2;chip++){ |
||
410 | if (chip) len = V673A_read1 (0, &word[count+2],maxn-count-2); |
||
411 | else len = V673A_read0 (0, &word[count+2],maxn-count-2); |
||
412 | |||
413 | if (len>16*32*4){ |
||
414 | if (print) printf("Length > 2048 per chip!\n"); |
||
415 | chip=2; |
||
416 | V673A_clallb(0); |
||
417 | evtrec.nev--; |
||
418 | continue; |
||
419 | } |
||
420 | pdata=&word[count+2]; |
||
421 | for(i=0;i< len;i++){ |
||
422 | tdcdata = pdata[i] & 0xFFFF ; |
||
423 | edge_type = (pdata[i]>>16)&0x1 ; |
||
424 | overflow = (pdata[i]>>17)&0x1 ; |
||
425 | tdc_num = (pdata[i]>>25)&0x1 ; |
||
426 | channel = ((pdata[i]>>18)&0x1F) |tdc_num<<5 ; |
||
427 | ev_dat = (pdata[i]>>23)&0x1 ; |
||
428 | last_dat = (pdata[i]>>30)&0x1 ; |
||
429 | nval_dat = (pdata[i]>>31)&0x1 ; |
||
430 | if (ev_dat) { |
||
431 | if(print) printf("Event %d\n",tdcdata); |
||
432 | } else { |
||
433 | dtdc[channel][edge_type][tdcdata]++; |
||
434 | |||
435 | if (print) printf |
||
436 | ("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",channel, edge_type,ev_dat,tdcdata,last_dat,nval_dat); |
||
437 | } |
||
438 | } |
||
439 | if (count+2 < maxn) count+=module_header(0x140+chip,&word[count],len); |
||
440 | } |
||
441 | if (print) printf("Received triggers: %d \n", ntrig); |
||
442 | } else { |
||
443 | if (ntrig==0) { |
||
444 | fprintf(stderr,"-----------------> no trigger"); |
||
445 | break; |
||
446 | } else { |
||
447 | if (print) fprintf(stderr,"-----------------> wrong number of triggers in chips V673a !!!\n"); |
||
448 | } |
||
449 | V673A_clallb(0); |
||
450 | evtrec.nev--; |
||
451 | continue; |
||
452 | } |
||
453 | |||
454 | V673A_clallb(0); |
||
455 | #endif |
||
456 | #endif |
||
457 | |||
458 | |||
459 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
||
460 | evtrec.len = count*sizeof(unsigned long) + sizeof(evtrec); |
||
461 | if (print) printf("EVTREC.len = %d bytes.\n", evtrec.len); |
||
462 | |||
463 | status = fwrite (&evtrec, 1, sizeof(evtrec), fp); |
||
464 | status = fwrite (&word, 1, count*sizeof(unsigned long), fp); |
||
465 | |||
466 | |||
467 | if (!(evtrec.nev%1000)) SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
468 | |||
469 | if (!((evtrec.nev+1)%1000)) { |
||
470 | if (fmax && (ftell(fp) > fmax)) { |
||
471 | time (&endrec.time); |
||
472 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
473 | fcount+=1; |
||
474 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
90 | f9daq | 475 | SetCtrlVal(p1h, P1_FCOUNT, fcount); |
29 | f9daq | 476 | fclose(fp); |
477 | fp = fopen (dfile, "wb"); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | if(step_minutes > 0) { |
||
482 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
483 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
484 | time(&cur_time_s); |
||
485 | if(cur_time_s >= end_time_s) { |
||
486 | end_time_s = cur_time_s + step_minutes*60; |
||
487 | printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours, start_minutes, start_seconds, posrec.xset); |
||
488 | break; |
||
489 | } |
||
490 | } |
||
491 | //told=t; |
||
492 | |||
493 | GetCtrlVal(p1h, P1_DAQ, &daq_on); |
||
494 | |||
495 | } while (evtrec.nev++<runrec.nev && daq_on); |
||
496 | if (!daq_on) break; |
||
497 | } // x loop |
||
498 | if (!daq_on) break; |
||
499 | } // y loop |
||
500 | if (!daq_on) break; |
||
501 | thrrec.threshold += thresholdStep*1000; |
||
90 | f9daq | 502 | wait_loop(10000); |
29 | f9daq | 503 | } // threhsold loop |
34 | f9daq | 504 | if (!daq_on) break; |
505 | biasrec.bias += runrec.biasStep; |
||
90 | f9daq | 506 | wait_loop(10000); |
34 | f9daq | 507 | } // bias loop |
29 | f9daq | 508 | |
34 | f9daq | 509 | SetDac(SIPM, 0); // safety |
510 | |||
29 | f9daq | 511 | //if (dsave) { |
512 | time (&endrec.time); |
||
513 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
514 | if (print) printf("ENDREC status %d len %d\n", status, endrec.len); |
||
515 | fclose(fp); |
||
516 | //} |
||
517 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
518 | printf("STOP:%2d-%2d-%2d (start_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, runrec.time, endrec.time); |
||
519 | printf("Elapsed time: %u s.\n", endrec.time-runrec.time); |
||
520 | |||
521 | daq_on=0; |
||
522 | SetCtrlVal (p1h, P1_DAQ, daq_on); |
||
523 | SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
524 | if (print) printf("RUN END. \n\n"); |
||
525 | |||
526 | return 0; |
||
527 | } |
||
528 | |||
529 | |||
530 | int SetDac(int ch, double val){ |
||
531 | |||
532 | const unsigned int maxdac=0xFFF; |
||
533 | const double RANGE = +9.9976; //V |
||
534 | unsigned int dac; |
||
535 | |||
536 | if ((val > RANGE) || (val < 0)) { |
||
537 | printf("DAC value OUT OF RANGE!!!\n"); |
||
538 | return(-1); |
||
539 | } |
||
540 | |||
541 | dac = (val/RANGE)*maxdac; |
||
542 | CSSA_W(NDAC,ch,16,&dac); |
||
543 | printf("DAC ch %d set to %f V dac=%d\n", ch, val, dac); |
||
544 | return 0; |
||
545 | } |
||
546 | |||
547 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
548 | LPSTR lpszCmdLine, int nCmdShow) |
||
549 | { |
||
550 | int i,j,status,refon; |
||
551 | long int xpos, ypos, zpos; |
||
90 | f9daq | 552 | char dfile[256]; |
553 | char message[128]; |
||
29 | f9daq | 554 | FILE *fp; |
555 | |||
556 | typedef struct { |
||
557 | unsigned long id,len; |
||
558 | unsigned long fver,time; |
||
559 | unsigned long nev,nch,ped,xy; |
||
560 | long nx,x0,dx,ny,y0,dy; |
||
561 | } RUNREC; |
||
562 | RUNREC runrec; |
||
563 | |||
564 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
565 | return -1; /* out of memory */ |
||
566 | |||
567 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
568 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
569 | |||
570 | SetStdioWindowOptions (1000, 0, 0); |
||
571 | SetStdioWindowSize (200, 560); |
||
572 | SetStdioWindowPosition (825, 250); |
||
573 | |||
574 | #ifdef USE_DAQ |
||
575 | VME_START(NULL); |
||
576 | #endif |
||
577 | |||
578 | #ifdef USE_MIKRO |
||
579 | MIKRO_Open (MIKRO_COM); |
||
580 | MIKRO_Init (MIKRO_X,0); |
||
581 | #ifdef USE_MIKRO_Y |
||
582 | MIKRO_Init (MIKRO_Y,0); |
||
583 | #endif |
||
584 | #ifdef USE_MIKRO_Z |
||
585 | MIKRO_Init (MIKRO_Z,0); |
||
586 | #endif |
||
587 | #endif |
||
588 | |||
33 | f9daq | 589 | if ((p1h = LoadPanel (0, "include/sipmBias_ui.uir", P1)) < 0) return -1; |
29 | f9daq | 590 | DisplayPanel (p1h); |
591 | SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1); |
||
592 | |||
90 | f9daq | 593 | sprintf(message, "%d", OFFSETX); |
594 | SetCtrlVal (p1h, P1_OFFSETX, message); |
||
595 | sprintf(message, "%d", OFFSETY); |
||
596 | SetCtrlVal (p1h, P1_OFFSETY, message); |
||
597 | |||
29 | f9daq | 598 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
599 | SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2); |
||
600 | |||
601 | QueueUserEvent (1000, p1h, P1_RESET); |
||
602 | //QueueUserEvent (1000, p1h, P1_ASD8THR); |
||
603 | QueueUserEvent (1000, p1h, P1_AMPDISSHTHR); |
||
34 | f9daq | 604 | QueueUserEvent (1000, p1h, P1_BIAS); |
29 | f9daq | 605 | do { |
606 | GetUserEvent (1, &pID, &rID); |
||
607 | switch (rID) { |
||
608 | case P1_TIMER: |
||
609 | ntics+=1; |
||
610 | GetCtrlVal (p1h, P1_REFON, &refon); |
||
611 | if (refon) update_plots(); |
||
612 | break; |
||
613 | case P1_REFRESH: |
||
614 | update_plots(); |
||
615 | break; |
||
616 | case P1_DAQ: |
||
617 | GetCtrlVal (p1h, P1_DAQ, &daq_on); |
||
618 | if (daq_on) { |
||
619 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
620 | } else { |
||
621 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
622 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
623 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
624 | } |
||
625 | break; |
||
626 | |||
627 | case P1_ZSET: |
||
628 | if (!daq_on) { |
||
629 | GetCtrlVal (p1h, P1_ZSET, &zpos); |
||
630 | #ifdef USE_MIKRO_Z |
||
631 | MIKRO_MoveTo (MIKRO_Z, zpos); |
||
632 | #endif |
||
633 | } |
||
634 | break; |
||
635 | case P1_REREAD: |
||
636 | if (!daq_on) { |
||
637 | status = FileSelectPopup ("", "*.dat", ".dat", |
||
638 | "Choose the previous file", |
||
639 | VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile); |
||
640 | if (status==1) { |
||
641 | fp = fopen (dfile, "rb"); |
||
642 | status = fread (&runrec, 1, sizeof(runrec), fp); |
||
643 | fclose(fp); |
||
644 | if (runrec.id==1) { |
||
645 | SetCtrlVal (p1h, P1_NX, runrec.nx); |
||
646 | SetCtrlVal (p1h, P1_XSTEP, runrec.dx); |
||
647 | SetCtrlVal (p1h, P1_XMIN, runrec.x0); |
||
648 | SetCtrlVal (p1h, P1_NY, runrec.ny); |
||
649 | SetCtrlVal (p1h, P1_YSTEP, runrec.dy); |
||
650 | SetCtrlVal (p1h, P1_YMIN, runrec.y0); |
||
651 | SetCtrlVal (p1h, P1_NEVE, runrec.nev); |
||
652 | } |
||
653 | } |
||
654 | } |
||
655 | break; |
||
656 | case P1_MGET: |
||
657 | #ifdef USE_MIKRO |
||
658 | MIKRO_GetPosition(MIKRO_X,&xpos); |
||
659 | Delay(0.01); |
||
660 | SetCtrlVal (p1h, P1_X, xpos); |
||
661 | #ifdef USE_MIKRO_Y |
||
662 | MIKRO_GetPosition(MIKRO_Y,&ypos); |
||
663 | Delay(0.01); |
||
664 | SetCtrlVal (p1h, P1_Y, ypos); |
||
665 | #endif |
||
666 | #ifdef USE_MIKRO_Z |
||
667 | MIKRO_GetPosition(MIKRO_Z,&zpos); |
||
668 | Delay(0.01); |
||
669 | SetCtrlVal (p1h, P1_Z, zpos); |
||
670 | #endif |
||
671 | #endif |
||
672 | break; |
||
673 | case P1_HO: |
||
674 | if (!daq_on) { |
||
675 | SetWaitCursor (1); |
||
676 | #ifdef USE_MIKRO |
||
677 | MIKRO_ReferenceMove (MIKRO_X); |
||
678 | #ifdef USE_MIKRO_Y |
||
679 | MIKRO_ReferenceMove (MIKRO_Y); |
||
680 | #endif |
||
681 | #ifdef USE_MIKRO_Z |
||
682 | MIKRO_ReferenceMove (MIKRO_Z); |
||
683 | #endif |
||
684 | #endif |
||
685 | SetWaitCursor (0); |
||
686 | } |
||
687 | break; |
||
688 | case P1_RESET: |
||
689 | for (j=0;j<NCH;j++) { |
||
690 | for (i=0;i<MAXCH;i++){ |
||
691 | dtdc[j][0][i]=0; |
||
692 | dtdc[j][1][i]=0; |
||
693 | |||
694 | } |
||
695 | } |
||
696 | update_plots(); |
||
697 | break; |
||
698 | case P1_TDCLOG: |
||
699 | GetCtrlVal (p1h, P1_TDCLOG, &status); |
||
700 | SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status); |
||
701 | update_plots(); |
||
702 | break; |
||
703 | case P1_ADCLOG: |
||
704 | GetCtrlVal (p1h, P1_ADCLOG, &status); |
||
705 | SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status); |
||
706 | update_plots(); |
||
707 | break; |
||
34 | f9daq | 708 | case P1_ASD8THR: |
29 | f9daq | 709 | { |
710 | double value; |
||
711 | GetCtrlVal (p1h, P1_SASD8THR, &value); |
||
34 | f9daq | 712 | SetDac(ASD8, value); |
29 | f9daq | 713 | |
714 | } |
||
34 | f9daq | 715 | break; |
716 | case P1_BIAS: |
||
717 | { |
||
718 | double value; |
||
719 | GetCtrlVal (p1h, P1_SBIAS, &value); |
||
720 | value -= POWERSUPPLY; |
||
721 | SetDac(SIPM, value); |
||
722 | } |
||
723 | break; |
||
29 | f9daq | 724 | case P1_AMPDISSHTHR: |
725 | { |
||
726 | double value; |
||
727 | GetCtrlVal (p1h, P1_SAMPDISSHTHR, &value); |
||
34 | f9daq | 728 | SetDac(AMPDISSH, value); |
29 | f9daq | 729 | |
730 | } |
||
731 | break; |
||
732 | |||
733 | } |
||
734 | |||
735 | } while ((rID != P1_EXIT)||daq_on); |
||
736 | |||
737 | CmtDiscardThreadPool (poolHandle); |
||
738 | DiscardPanel (p1h); |
||
739 | |||
740 | #ifdef USE_MIKRO |
||
741 | MIKRO_Close (); |
||
742 | #endif |
||
743 | |||
744 | #ifdef USE_DAQ |
||
745 | VME_STOP(); |
||
746 | #endif |
||
747 | return 0; |
||
748 | |||
749 | } |