Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
298 f9daq 1
#include <formatio.h>
2
#include "H1D.h"
3
#include "H2D.h"
4
#include <utility.h>
5
#include <ansi_c.h>
6
#include <cvirte.h>             
7
#include <userint.h>
8
#include "vmusb_ctrl.h"
9
#include "daq_cvi.h"
10
static int p1, p2, p3;
11
 
12
 
13
static int daq_on;
14
static int plothandle[4]= {0,0,0, 0};
15
static int tfID;
16
static int controlID;
17
static int verbose;
18
static int timeout;
19
extern int ctrlcflag;
20
 
21
#define MAX_THREADS 10
22
 
23
static CmtThreadPoolHandle poolHandle = 0;
24
 
25
#define MAXCH 72
26
  float gSum[6];
27
  float gRawSum[6];
28
  float gMax[6];
29
  float gSumCluster[6];
30
  float gNtdata[MAXCH*3];
31
  int   gNabove[5];
32
  double gData[MAXCH]; // korigirani ADC ji
33
  double gAdc[MAXCH];  // raw ADC ji
34
 
35
  double gPedestals[MAXCH];
36
    double gPeak[MAXCH];
37
 
38
    double gPeakScaling;    
39
    double gThreshold;    
40
 
41
 
42
typedef struct Channel {
43
  int ix;
44
  int iy;
45
  int idx;
46
 
47
};
48
 
49
struct Channel m_geo_ch[16];
50
 
51
 
52
char strbuf[0xFF];
53
 
54
int gLog=0;
55
int printf(const char *format, ...) {
56
  va_list aptr;
57
  int ret;
58
  FILE *flog;
59
 
60
  va_start(aptr, format);
61
  ret = vsprintf(strbuf, format, aptr);
62
  va_end(aptr);
63
  SetCtrlVal(p1,P1_IO,strbuf);
64
 
65
  if (gLog) {
66
    flog = fopen ("stdio.log", "a");
67
    fprintf (flog, "%s", strbuf);
68
    fclose (flog);
69
  }
70
  return(ret);
71
}
72
 
73
 
74
 
75
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
76
                       LPSTR lpszCmdLine, int nCmdShow) {
77
  if (InitCVIRTE (hInstance, 0, 0) == 0)
78
    return -1;  /* out of memory */
79
  if ((p1 = LoadPanel (0, "vmusb_ctrl.uir", P1)) < 0)
80
    return -1;
81
  if ((p2 = LoadPanel (0, "vmusb_ctrl.uir", P2)) < 0)
82
    return -1;
83
  if ((p3 = LoadPanel (0, "vmusb_ctrl.uir", P3)) < 0)
84
    return -1;
85
  SetStdioPort (CVI_STDIO_WINDOW);
86
  SetSleepPolicy(VAL_SLEEP_MORE);
87
  CmtNewThreadPool (MAX_THREADS,  &poolHandle);
88
  DisplayPanel (p1);
89
 
90
  RunUserInterface ();
91
  DiscardPanel (p1);
92
  DiscardPanel (p2);  
93
  DiscardPanel (p3);  
94
  return 0;
95
}
96
 
97
static void start_timer (double tout) {
98
        timeout = 0;
99
  SetCtrlAttribute (p1, P1_TIMER, ATTR_INTERVAL, tout);
100
  SetCtrlAttribute (p1, P1_TIMER, ATTR_ENABLED, 1);
101
}
102
 
103
static void stop_timer ( void ) {
104
  SetCtrlAttribute (p1, P1_TIMER, ATTR_ENABLED, 0);
105
  //DRSSetTimeout();
106
}
107
 
108
 
109
 
110
 
111
 
112
int georead(){
113
int i;
114
for (i=0; i<64; i++) {
115
  int row=i+1;
116
  double val;
117
  unsigned short sval;
118
  GetTableCellVal (p3, P3_CGEO, MakePoint (3,row), &sval);
119
  fThreshold[i]=sval;
120
  GetTableCellVal (p3, P3_CGEO, MakePoint (4,row), &val);
121
  gPeak[i]=val;
122
  GetTableCellVal (p3, P3_CGEO, MakePoint (5,row), &val);
123
  gPedestals[i]=val;
124
}
125
 
126
GetCtrlVal(p3, P3_PEAKSCALING, &gPeakScaling );
127
GetCtrlVal(p3, P3_GTHRESHOLD, &gThreshold );
128
 
129
for (i=0; i<16; i++) {
130
  int row=i+1;
131
  double val;
132
  unsigned short sval, id;
133
  GetTableCellVal (p3, P3_CGEO, MakePoint (1,row), &id);
134
  GetTableCellVal (p3, P3_CGEO, MakePoint (2,row), &sval);
135
  m_geo_ch[id].ix=sval;
136
  GetTableCellVal (p3, P3_CGEO, MakePoint (3,row), &sval);
137
  m_geo_ch[id].iy=sval;    
138
  m_geo_ch[id].idx=id;    
139
}
140
 
141
return 0;
142
}
143
 
144
void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
145
                               CmtThreadFunctionID functionID, unsigned int event,
146
                               int value, void *callbackData  ) {
147
 
148
  daq_on=0;
149
  //SetDimming(0);
150
  printf("End of Thread \n");
151
  return ;
152
 
153
}
154
 
155
 
156
int CVICALLBACK RefreshGraphs (int panel, int control, int event,
157
                               void *callbackData, int eventData1, int eventData2) {
158
  switch (event) {
159
    case EVENT_TIMER_TICK:
160
//      if (!daq_on ) return 0;
161
    case EVENT_COMMIT: {
162
 
163
      int ch=0;
164
      int logy=0;
165
      int pmt =0;
166
      int updateplots=0;
167
      GetCtrlVal(p1,P1_RECO, &updateplots);
168
      if (!updateplots) return 0;
169
      GetCtrlVal(p2,P2_PMT, &pmt);
170
      GetCtrlVal(p2,P2_ADC, &ch);
171
 
172
 
173
      H1D_Draw(10,p2,P2_GRAPHADC,&plothandle[1]);
174
      H2D_Draw(0,p2,P2_GRAPH2D,&plothandle[2]);
175
 
176
 
177
    }
178
 
179
 
180
    break;
181
 
182
  }
183
  return 0;
184
}
185
 
186
 
187
 
188
int vmusb_init(){
189
 
190
  int range = 2056;
191
  for (int i=0; i<MAXCH; i++) {
192
    char name[0xFF];
193
    sprintf(name,"adc%d", i);
194
 
195
    H1D_Init(i, name,name, range, 0 , range);
196
    H1D_SetTitleX(0,"ADC");
197
    H1D_SetTitleY(0,"N");
198
    sprintf(name,"adc%d", i);
199
 
200
    sprintf(name,"energy%d", i);  
201
    H1D_Init(100+i, name,name, range, 0 , range);
202
    H1D_SetTitleX(0,"Energy");
203
    H1D_SetTitleY(0,"N");
204
  }
205
 
206
 
207
  H2D_Init(0, "cog","Center of grg_peakscalingavity", 256, 0 ,1, 256, 0 ,1);
208
  H2D_SetTitleX(0,"x");
209
  H2D_SetTitleY(0,"y");
210
 
211
  return 0;
212
}
213
 
214
double GetEnergy(int ch, int adc){
215
   return (adc-gPedestals[ch])/(gPeak[ch]-gPedestals[ch])*gPeakScaling;
216
}
217
 
218
int FillHistograms() {
219
 
220
  int npmts = 4;
221
  for (int ipmt=0; ipmt<npmts; ipmt++) { // zanka preko pmtjev
222
    int j2= (ipmt/2)*2+1-ipmt%2;  // sosednja fotopomnozevalka
223
 
224
    float posx[2]= {0,0};
225
    float posy[2]= {0,0};
226
    float sum[2]= {0,0};
227
    float m_threshold =100;
228
    for (int ich=0; ich<16; ich++) { // zanka preko elektronskih kanalov na fotopomnozevalki
229
      int ch= ich+ipmt*16;
230
      if (gMax[ipmt]>m_threshold) {
231
        posx[0]+= gData[ch]*m_geo_ch[ich].ix;
232
        posy[0]+= gData[ch]*m_geo_ch[ich].iy;
233
        sum[0] += gData[ch];
234
 
235
        if (gData[ch]> 0.2*gMax[ipmt]) { // pri racunanju pozicije upostevaj le kanale, ki imajo vrednost vecjo od ratio*maksimalna na tisti fotopomnozevalki
236
          posx[1]+= gData[ch]*m_geo_ch[ich].ix;
237
          posy[1]+= gData[ch]*m_geo_ch[ich].iy;
238
          sum[1] += gData[ch];
239
        }
240
      }
241
    }
242
 
243
    if ( sum[0] > 0  ) {
244
      float px=posx[0]/sum[0];
245
      float py=posy[0]/sum[0];
246
      H2D_Fill(0, px,py,1);
247
    }
248
  }
249
 
250
 
251
  return 0;
252
};
253
 
254
 
255
int DecodeData(int n, unsigned int *buf){
256
 
257
        int idx=1;
258
      int neve=buf[0]/2;
259
      //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
260
      for (int nev=0;nev<neve;nev++){
261
        int len=buf[idx];
262
        int sb =buf[idx+1];
263
        unsigned int *pbuf=&buf[idx+2];
264
        if (sb!=0xffab) {
265
          printf("0x%04x!0xffab len=%d\n",sb,len);
266
          break;
267
        }
268
        // postavi na nic
269
#define InitArrayWithAValue(arr,n,x) {for (int i=0;i<n;i++) arr[i]=x;}      
270
        InitArrayWithAValue( gSum       , 4    , 0);
271
        InitArrayWithAValue( gRawSum    , 4    , 0);
272
        InitArrayWithAValue( gMax       , 4    , 0);
273
        InitArrayWithAValue( gSumCluster, 4    , 0);
274
        InitArrayWithAValue( gNabove    , 4    , 0);
275
        InitArrayWithAValue( gData      , MAXCH, 0);
276
        InitArrayWithAValue( gAdc       , MAXCH, 0);    
277
        //------------------------------------------------------------
278
        for (int i0=0;i0<len-2;i0+=2) {
279
 
280
          int data0  = pbuf[i0];
281
          int data1  = pbuf[i0+1];
282
          int geo    = (data1 >> 11) & 0x1f;
283
          int ch     = (data1&0x1f)  | (geo<<5);
284
          int dtype  = (data1>>9)&0x3;
285
          int adc    =  data0&0xfff;
286
 
287
          switch (dtype) {
288
          case 0x0:
289
            if (ch<MAXCH) {              
290
              H1D_Fill(ch,adc,1);
291
 
292
              int ipmt = ch/16;
293
 
294
              gAdc[ch]=adc;
295
              gRawSum[ipmt]+=adc;
296
 
297
              if (ch<64) gData[ch]= GetEnergy(ch,adc); else gData[ch]=adc;
298
              H1D_Fill(1+ch,gData[ch],1);
299
              gSum[ipmt]+=gData[ch];
300
              if (gData[ch]   >gMax[ipmt] )     gMax[ipmt]= gData[ch];
301
              if (gData[ch]   >gThreshold )    gNabove[ipmt]++;
302
            }
303
            break;
304
          case 0x10:
305
          case 0x11:
306
          case 0x01:
307
            break;
308
          }
309
 
310
        };// for (int i0=0;i0<len-2;i0+=2) 
311
        //------------------------------------------------------------
312
 
313
        idx+=len+1;
314
        FillHistograms();
315
      } // for (int nev=0;nev<neve;nev++)
316
      //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
317
 
318
 
319
  return 0;
320
}
321
int vmusb_daq(){
322
 
323
 
324
  vmusb_init();
325
 
326
 
327
 
328
 
329
   // print welcome message
330
  time_t t,told=0, tstart, tstop;
331
  time(&t);
332
  printf("#############################################\n");
333
  printf("Program vmusb version\n");
334
  printf("Compiled on %s %s\n",__DATE__, __TIME__);
335
  printf("Runtime  %s \n",ctime(&t));
336
  printf("#############################################\n");
337
 
338
 
339
  int neve=-1;
340
  char cfname[100]="test.dat";
341
  char fname[0xff];
342
  char fpedname[0xff];
343
 
344
#define BSIZE 10000
345
  uint32_t data[10000];
346
  int oldValue;
347
  //oldValue = SetBreakOnLibraryErrors (0);
348
  daq_init(); /* Function calls that may legitimately return errors. */
349
  //SetBreakOnLibraryErrors (oldValue);
350
 
351
  int output=0;
352
  GetCtrlVal(p1,P1_OUTPUT, &output);
353
 
354
  GetCtrlVal(p1,P1_FMASK, fpedname);
355
  int fsize=0;
356
  georead();
357
 
358
  GetCtrlVal(p1,P1_NEVE, &neve);
359
  // negative argument time ( in s )limited event loop
360
  GetCtrlVal(p1,P1_FNAME, fname);
361
 
362
  if (GetFileInfo(fname,&fsize)==1) {
363
    printf("Error !\n");
364
    printf( "File %s already exist. Appending ....\n",fname);
365
    //fprintf(stdout,"Remove the file and restart !!!\n");
366
    //exit(0);
367
  }
368
 
369
 
370
  FILE *fp=NULL;
371
  if (output) fp = fopen(fname,"a");
372
  //gzFile fp=gzopen(fname,"a");
373
 
374
  init();
375
  clear();
376
 
377
  int hdr[4]={2}; // recid od run 11 naprej
378
  int i=0;
379
  int ntotal=0;
380
  int counters[30]={0,0,0,0,0, 0,0,0,0,0,0,0};
381
  char names[10][20]={"TRG","CAEN V965"};
382
  time(&t);
383
  tstart=t;
384
  tstop=tstart+360000;
385
  int evetype=0;
386
  GetCtrlVal(p1,P1_EVE, &evetype);    
387
  if (evetype) {
388
    double ntime=0;
389
    GetCtrlVal(p1,P1_NTIME, &ntime);
390
    tstop=tstart+ntime;
391
    neve=-1;
392
  }
393
  int reco=0;
394
  GetCtrlVal(p1,P1_RECO, &reco);
395
  for (i=0;i!=neve && !ctrlcflag && t<tstop;i++){
396
     time(&t);
397
     if (t!=told ) {
398
       printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t));
399
       GetCtrlVal(p1,P1_RECO, &reco);
400
       RefreshGraphs(p2,P2_PMT,EVENT_COMMIT,NULL,0,0);
401
     }  
402
     int nb=event(data,BSIZE, counters,t!=told);
403
     SetCtrlVal(p1,P1_CEVE,i);
404
     if (nb>0){
405
 
406
      if (reco) DecodeData(nb, data);
407
      // zapis v datoteko   
408
      hdr[1]=nb+4*sizeof(int);
409
      hdr[2]=time(NULL);
410
      hdr[3]=i;
411
      if (fp) fwrite(hdr,   sizeof(int),4 , fp);
412
      if (fp) ntotal += fwrite(data, 1,nb, fp);
413
 
414
     } else i--;
415
     told=t;
416
  }
417
 
418
  end();
419
 
420
  printf("Number of Events: %d\n",i);
421
  if (ctrlcflag) printf("User Program termination CTRL-C\n");
422
  if (t>tstop  ) printf("Timeout termination tstart# t>tstop: %d# %d >%d\n",(int)t, (int)tstart, (int) tstop);
423
 
424
 
425
  if (fp) fclose(fp);  
426
  //gzclose(fp);  
427
  printf("%d bytes written to %s\nCounts:\n", (int) (ntotal*sizeof(int)),fname);
428
  for (i=0;i<2;i++) printf("%s\t%d\t%d\n",names[i],counters[2*i],counters[2*i+1]) ;
429
 
430
 
431
 
432
  printf("Usage: vmusb [filename] [number of events] [thresholdfile]\n negative number of events = acq time in seconds\n");
433
 
434
  disconnect();
435
 
436
  return 0;
437
}
438
 
439
int CVICALLBACK StartCB (int panel, int control, int event,
440
                         void *callbackData, int eventData1, int eventData2) {
441
  ThreadFunctionPtr mythread = NULL;
442
  switch (event) {
443
 
444
    case EVENT_COMMIT:
445
 
446
      controlID= control;
447
      if (panel == p1 && control == P1_START) {
448
        mythread = vmusb_daq;
449
 
450
      }
451
      //if (panel == xyscan && control == SCAN_SCAN)   mythread = scan;  
452
      if (mythread!=NULL) {
453
        printf("New Thread panel=%d button=%d\n", panel, control);
454
 
455
        // SetDimming(1);
456
 
457
 
458
        CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID,
459
                                          DEFAULT_THREAD_PRIORITY,
460
                                          EndOfThread,
461
                                          EVENT_TP_THREAD_FUNCTION_END,
462
                                          NULL, RUN_IN_SCHEDULED_THREAD,
463
                                          &tfID);
464
      }
465
      break;
466
  }
467
  return 0;
468
}
469
 
470
int CVICALLBACK StopCB (int panel, int control, int event,
471
                        void *callbackData, int eventData1, int eventData2) {
472
  switch (event) {
473
    case EVENT_COMMIT:
474
      ctrlcflag=1;
475
      break;
476
  }
477
  return 0;
478
}
479
 
480
int CVICALLBACK ExitCB (int panel, int control, int event,
481
                        void *callbackData, int eventData1, int eventData2) {
482
  switch (event) {
483
    case EVENT_COMMIT:
484
      QuitUserInterface (0);
485
      break;
486
  }
487
  return 0;
488
}
489
 
490
int CVICALLBACK ShowHistoCB (int panel, int control, int event,
491
                             void *callbackData, int eventData1, int eventData2) {
492
  switch (event) {
493
    case EVENT_COMMIT:
494
      DisplayPanel (p2);
495
      break;
496
  }
497
  return 0;
498
}
499
 
500
int CVICALLBACK SetLogZCB (int panel, int control, int event,
501
                           void *callbackData, int eventData1, int eventData2) {
502
  switch (event) {
503
    case EVENT_COMMIT:
504
      GetCtrlVal(panel,control, &log);
505
 
506
      if (log) {
507
        SetCtrlAttribute (p2, P2_GRAPH2D, ATTR_YMAP_MODE, VAL_LOG);
508
 
509
      } else {
510
        SetCtrlAttribute (p2, P2_GRAPH2D, ATTR_YMAP_MODE, VAL_LINEAR);
511
      }
512
 
513
      break;
514
  }
515
  return 0;
516
}
517
 
518
int CVICALLBACK SetLogYCB (int panel, int control, int event,
519
                           void *callbackData, int eventData1, int eventData2) {
520
 
521
  switch (event) {
522
    case EVENT_COMMIT:{
523
      int log=0;  
524
      GetCtrlVal(panel,control, &log);
525
 
526
      if (log) {
527
        SetCtrlAttribute (p2, P2_GRAPHADC, ATTR_YMAP_MODE, VAL_LOG);
528
 
529
      } else {
530
        SetCtrlAttribute (p2, P2_GRAPHADC, ATTR_YMAP_MODE, VAL_LINEAR);
531
      }
532
 
533
    }
534
      break;
535
  }
536
  return 0;
537
}
538
 
539
 
540
int CVICALLBACK ShowMainCB (int panel, int control, int event,
541
                            void *callbackData, int eventData1, int eventData2) {
542
  switch (event) {
543
    case EVENT_COMMIT:
544
      DisplayPanel (p1);
545
      break;
546
  }
547
  return 0;
548
}
549
 
550
int CVICALLBACK ShowSettingsCB (int panel, int control, int event,
551
                                void *callbackData, int eventData1, int eventData2) {
552
  switch (event) {
553
    case EVENT_COMMIT:
554
      DisplayPanel (p3);  
555
      break;
556
  }
557
  return 0;
558
}