Subversion Repositories f9daq

Rev

Rev 29 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
29 f9daq 1
//////////////////////////////////////////
2
// Data to root conversion root script
3
//
4
// Contributors: Rok Pestotnik, Rok Dolenec, Dino Tahirovic
5
//
6
// 3/1/2014 TDC cut relative to tdc offset
7
//
8
 
9
#include "stdio.h"
10
#include "TROOT.h"
11
#include "TFile.h"
12
#include "TNtuple.h"
13
#include "TH1F.h"
14
#include "TH2F.h"
15
#include "TF1.h"
16
#include "TMath.h"
17
#include "TStyle.h"
18
#include "TCanvas.h"
19
#include "TLine.h"
20
#include "zlib.h"
21
 
22
// ------------------------------------------------------------------------------
23
 
24
#define POSMARG 1000
25
 
26
#define READBUFFERLENGTH 10000
27
 
28
// data format
29
#define MAXDATA 16
30
#define NCH 64
31
#define TDC_BIN 1.0416 // 1 TDC bin in ns
32
#define MIKRO_BIN 0.49609/1000. //1 mikro step in mm; stage MM3MF
33
#define OFFSETX 5100  // Right edge of SiPM+Lightguide
34
#define OFFSETY 5200 // Lower edge of SiPM+Lightguide
35
 
36
#define RUNREC_ID 1
37
#define ENDREC_ID 2
38
#define POSREC_ID 3
39
#define EVTREC_ID 4
40
#define THRREC_ID 5
41
 
42
typedef struct {
43
  unsigned int id,len;
44
  unsigned int fver,time;
45
  unsigned int nev,nch,ped,xy;
46
  unsigned int thLow, thUp, thStep;
47
  int nx,x0,dx,ny,y0,dy;
48
} RUNREC;
49
RUNREC *runrec;
50
RUNREC run;
51
 
52
typedef struct {
53
  unsigned int id,len;
54
  unsigned int time;
55
} ENDREC;
56
ENDREC *endrec;
57
 
58
typedef struct {
59
  unsigned int id,len;
60
  unsigned int time;
61
  int ix;
62
  int x;
63
  int xset;
64
  int iy;
65
  int y;
66
  int yset;
67
} POSREC;
68
POSREC *posrec;
69
POSREC pos;
70
 
71
typedef struct {
72
    unsigned int id;
73
    unsigned int len;
74
    unsigned int nev;
75
    } EVTREC;
76
EVTREC *evtrec;
77
 
78
typedef struct {
79
  unsigned int id;
80
  unsigned int len;
81
  unsigned int threshold;
82
} THRREC;
83
THRREC *thrrec;
84
THRREC thr;
85
 
86
double padCenter[NCH][2];
87
 
88
int position(int, int, int);
89
 
90
// ------------------------------------------------------------------------------
91
 
92
int d2r(char* dfile0="test", int dbg=0, double tdcCut=5.0)
93
{
94
 
95
  const double c_tdcOffset = +2.5; // ns
96
 
97
  printf(" Data to root conversion program\nUsage:\nd2r(input file name <without .dat>, debug on/off, TDC cut +-[ns])\n\n");
98
 
99
  char fullname[256];
100
  char sbuff[256];
101
  FILE *fp;
102
 
103
  //Chanel information
104
 
105
  double tdcOffset[NCH];
106
  sprintf(fullname, "d2r.ini");
107
  if( (fp=fopen(fullname, "rt")) == NULL )
108
    printf("Cannot open pad centers file %s !!!\n", fullname);
109
  else {
110
    printf("Opened pad centers file %s\n", fullname);
111
    char* result = fgets(sbuff,256, fp);
112
    if (dbg) printf("Read buffer %s\n", result);
113
    printf("%s", sbuff);
114
    for(int i=0; i<NCH; i++) {
115
      int channel;
116
      int message = fscanf(fp, "%d %lf %lf %lf\n", &channel, &padCenter[i][0], &padCenter[i][1], &tdcOffset[i]);
117
      if (dbg) printf("Read d2r.ini returned %d\n", message);
118
    }
119
    fclose(fp);
120
  }
121
  for(int i=0; i<NCH; i++) {
122
    tdcOffset[i] += c_tdcOffset;
123
    printf("%.2lf %.2lf %.2lf\n", padCenter[i][0], padCenter[i][1], tdcOffset[i]);
124
    }
125
 
126
  //TDC correction parameters
127
/*
128
  double corpar[NCH][3]={ {-0.8, 25, 0},
129
                          {-0.8, 25, 0},
130
                          {-0.8, 25, 0},
131
                          {-0.8, 25, 0},
132
                          {-0.8, 25, 0},
133
                          {-0.8, 25, 0},
134
                          {-0.8, 25, 0},
135
                          {-0.8, 25, 0} };
136
  sprintf(fullname, "data/%s_cor.txt", dfile0);
137
  if( (fp=fopen(fullname, "rt")) == NULL )
138
    printf("Cannot open parameter file %s !!!\n", fullname);
139
  else {
140
    printf("Opened parameter file %s\n", fullname);
141
    for(int i=0; i<NCH; i++) {
142
      fscanf(fp, "%lf %lf %lf\n", &corpar[i][0], &corpar[i][1], &corpar[i][2]);*/
143
      /*// check if parameters make sense
144
      if( (corpar[i][0] < (tdcmi-0.2*tdcmi)) || ((tdcma+0.2*tdcma) < corpar[i][0]) ||
145
          (corpar[i][1] < 0) || (1e4*TDC_BIN < corpar[i][1]) ||
146
          (1e4 < TMath::Abs(corpar[i][2])) ) {
147
        printf("Warning: parameters for ch%d out of limits -> using default!\n", i);
148
        corpar[i][0]=2200*TDC_BIN; corpar[i][1]=1000*TDC_BIN; corpar[i][2]=-100*TDC_BIN;
149
      }*/
150
/*    }
151
    fclose(fp);
152
  }*/
153
  //for(int i=0; i<NCH; i++) printf("%.2lf %.2lf %.2lf\n", corpar[i][0], corpar[i][1], corpar[i][2]);
154
 
155
 
156
        //histograms
157
        char hname[256];
158
        //double tdc;
159
        TH2F *htdc;
160
  TH2F* h_correctedTDC;
161
        TH1F *hnhitsx[NCH], *hnhitsy[NCH];
162
        TH2F *h2d[NCH];
163
        TH2F *h_threshold;
164
        TH2F *h_ch33;
165
 
166
        TNtuple *nt;
167
 
168
        //data buffer
169
        unsigned int readbuf[READBUFFERLENGTH];
170
        unsigned int buf[READBUFFERLENGTH];
171
 
172
 
173
 
174
 
175
 
176
        //data file
177
        gzFile dfp;
178
        char dfile[256];
179
        int ftype=0;
180
        int fcount=1;
181
        do {
182
          switch(ftype++) {
183
            case 0:
184
              sprintf(dfile, "./data/%s_file%02d.dat", dfile0, fcount);
185
              break;
186
            case 1:
187
              sprintf(dfile, "./data/%s_file%02d.dat.gz", dfile0, fcount);
188
              break;
189
            case 2:
190
              sprintf(dfile, "./data/%s_file%02d.gz", dfile0, fcount);
191
              break;
192
            default:
193
        printf("  Cannot find data file for %s !!!\n", dfile0);
194
        return -1;
195
          }
196
          dfp=gzopen(dfile,"rb");
197
        } while(!dfp);
198
        printf("Opened data file %s\n", dfile);
199
 
200
        //opens ROOT file
201
        //TFile *rootfile; 
202
        char fnameroot[256];
203
        sprintf(fnameroot, "root/%s.root", dfile0);
204
        //rootfile = (TFile *) gROOT->FindObjectAny(dfile0); 
205
        //if (rootfile!=NULL) {printf("!!!\n");rootfile->Close();}
206
        //rootfile = new TFile(fnameroot);
207
        //if(rootfile) rootfile->Close();
208
        TFile* rootfile = new TFile(fnameroot,"RECREATE",dfile0);
209
 
210
// -----------------------------------------------
211
        // loop trough records
212
        unsigned int rec_id, rec_len;
213
        unsigned int ulsize = sizeof(unsigned int);
214
        //unsigned int ulsize = 4;
215
        if (dbg) printf("Size of unsigned int: %lu\n", sizeof(unsigned int));
216
        int ceve=0;
217
        int end_of_file = 0;
218
 
219
        int ii;
220
        int nint;
221
        int nb;
222
        int nSteps;
223
        int status;
224
 
225
        while(1) {     
226
                if(gzeof(dfp)) end_of_file = 1;        
227
 
228
                gzread(dfp, (voidp)&readbuf, 2*ulsize);
229
                rec_id=readbuf[0];
230
                rec_len=readbuf[1];
231
 
232
 
233
                if(dbg) printf("-----------------------------------------------\n");
234
                if(dbg) printf("[%d] rec_id = %d | rec_len = %u\n", ceve, rec_id, rec_len);
235
 
236
                switch(rec_id)
237
                {              
238
                        case RUNREC_ID:
239
                                gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
240
                                runrec = (RUNREC*) readbuf;
241
                          run = *runrec;
242
 
243
                                if(dbg) {
244
                                        printf("RUNREC_ID\n");
245
                                        printf("id = %d, len = %d, time = %d\n", run.id, run.len, run.time);
246
                                        printf("nev = %d, nch = %d\n", run.nev, run.nch);
247
                                        printf("nx = %d, x0 = %d, dx = %d\n", run.nx, run.x0, run.dx);
248
                                        printf("ny = %d, y0 = %d, dy = %d\n", run.ny, run.y0, run.dy);
249
                                        printf("thLow = %d, thUp = %d, thStep = %d\n", run.thLow, run.thUp, run.thStep);
250
                                }
251
 
252
                                //create histograms
253
                                nt = new TNtuple("nt", "nt", "ch:x:y:tdc");
254
                                sprintf(hname, "htdc");
255
                                htdc = (TH2F*) gROOT->FindObject(hname);
256
                                if (htdc) delete htdc;
257
                                htdc = new TH2F("htdc","Raw TDC;TDC channel;SiPM channel",512,0,512,NCH,0,NCH);
258
                                h_correctedTDC = (TH2F*) gROOT->FindObject("h_correctedTDC");
259
                                if (h_correctedTDC) delete h_correctedTDC;
260
                                h_correctedTDC = new TH2F("h_correctedTDC","Corrected TDC;t [ns];SiPM channel",33, -16.5*TDC_BIN,16.5*TDC_BIN,NCH,0,NCH);
261
        //TH1F* gsumV673A[NCH/16] = new TH1F(hn,hname,256,-0.5,255.5);
262
                                for(int i=0; i<NCH; i++) {
263
                                  /*   
264
                                        sprintf(hname, "htdcpos%d", i);
265
                                        htdcpos[i] = (TH1F*)gROOT->FindObject(hname);
266
                                        if(htdcpos[i]) delete htdcpos[i];
267
                                        htdcpos[i] = new TH1F(hname, hname, 512, 0, 512*TDC_BIN);
268
 
269
                                        sprintf(hname, "htdc%d", i);
270
                                        htdc[i] = (TH1F*)gROOT->FindObject(hname);
271
                                        if(htdc[i]) delete htdc[i];
272
                                        htdc[i] = new TH1F(hname, hname, 512, 0*TDC_BIN, 512*TDC_BIN);
273
                                        */
274
 
275
 
276
                                        sprintf(hname, "hnhitsx%d", i);
277
                                        hnhitsx[i] = (TH1F*)gROOT->FindObject(hname);
278
                                        if(hnhitsx[i]) delete hnhitsx[i];
279
                                        hnhitsx[i] = new TH1F(hname, hname, run.nx,
280
                                                                            (run.x0 - OFFSETX - 0.5*run.dx)*MIKRO_BIN,
281
                                                                            (run.x0 - OFFSETX + (run.nx-0.5)*run.dx)*MIKRO_BIN);
282
 
283
                                        sprintf(hname, "hnhitsy%d", i);
284
                                        hnhitsy[i] = (TH1F*)gROOT->FindObject(hname);
285
                                        if(hnhitsy[i]) delete hnhitsy[i];
286
                                        //hnhitsy[i] = new TH1F(hname, hname, run.ny, (run.y0-0.5*run.dy)*MIKRO_BIN, (run.y0+(run.ny-0.5)*run.dy)*MIKRO_BIN);
287
                                        hnhitsy[i] = new TH1F(hname, hname, run.ny,
288
                                                                            (run.y0 - 0.5*run.dy - OFFSETY)*MIKRO_BIN,
289
                                                                            (run.y0 + (run.ny-0.5)*run.dy - OFFSETY)*MIKRO_BIN);
290
                                        //hnhitsy[i] = new TH1F(hname, hname, 100, 0,100);
291
 
292
                                        sprintf(hname, "h2d%d", i);
293
                                        h2d[i] = (TH2F*)gROOT->FindObject(hname);
294
                                        if(h2d[i]) delete h2d[i];
295
                                        h2d[i] = new TH2F(hname, hname, run.nx,
296
                                                                        (run.x0 - OFFSETX - 0.5*run.dx)*MIKRO_BIN,
297
                                                                        (run.x0 - OFFSETX + (run.nx-0.5)*run.dx)*MIKRO_BIN,
298
                                                                        run.ny,
299
                                                                        (run.y0 - OFFSETY - 0.5*run.dy)*MIKRO_BIN,
300
                                                                        (run.y0 - OFFSETY + (run.ny-0.5)*run.dy)*MIKRO_BIN);
301
                                }
302
 
303
                                if (h_threshold) delete h_threshold;
304
                                nSteps = (run.thUp - run.thLow)/double(run.thStep) + 1;
305
                                if (dbg) printf("nSteps %d\n", nSteps);
306
                                h_threshold = new TH2F("h_threshold","Threshold scan;SiPM ch;Threshold[mV]",64,-0.5,63.5,
307
                                                                      nSteps,
308
                                                                      run.thLow - 0.5*run.thStep,
309
                                                                      run.thUp  + 0.5*run.thStep);
310
 
311
                                //h_threshold = new TH2F("h_threshold","Threshold scan;SiPM ch;Threshold[mV]",64,-0.5,63.5,
312
                                //                                      101,995,2005);
313
 
314
                                if (h_ch33) delete h_ch33;
315
                                h_ch33 = new TH2F("h_ch33","ch. 33;x;y",100,20000,30000,100,0,10000);
316
 
317
                                break;
318
 
319
                        case POSREC_ID:
320
                                gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
321
                                posrec = (POSREC *) readbuf;
322
                                pos=*posrec;
323
 
324
                                if(dbg) {
325
                                        printf("POSREC_ID\n");
326
                                        printf("id = %d, len = %d, time = %d\n", posrec->id, posrec->len, posrec->time);
327
                                        printf("ix = %d, x = %d, xset = %d\n", posrec->ix, posrec->x, posrec->xset);
328
                                        printf("iy = %d, y = %d, yset = %d\n", posrec->iy, posrec->y, posrec->yset);
329
                                } else  printf("        [%d,%d] %d, %d\n", pos.ix, pos.iy, pos.xset, pos.yset);
330
 
331
                                break;
332
 
333
                        case EVTREC_ID:
334
                          gzread(dfp, (voidp)&readbuf[2], ulsize); // last field of event record
335
                          evtrec = (EVTREC *) readbuf;
336
                          //evtrec->nev = buf[0];
337
                          //if (rec_len < 0 || rec_len > 10000) {
338
                          if (rec_len > READBUFFERLENGTH) {
339
                            printf("Len %u\n", rec_len);
340
                            return(0);
341
                            }
342
                          nb = rec_len - 3*ulsize; // no. of bytes to read
343
                                gzread(dfp, (voidp)&buf, nb);
344
 
345
                                if(dbg) {
346
                                        printf("EVTREC_ID\n");
347
                                        printf("id = %d, len = %d, nev = %d\n", evtrec->id, evtrec->len, evtrec->nev);
348
                                        //for(int datai = 0; datai < NCH; datai++) printf("%u ", evtrec->data[datai]);
349
                                        //printf("\n");
350
                                        //for(int datai = NCH; datai < NCH+NCH; datai++) printf("%u ", evtrec->data[datai]);
351
                                        //printf("\n");
352
                                        break;
353
                                }
354
 
355
                                nint = nb / ulsize; // no. of subrecords
356
                                if (dbg) printf("No. of subrecords %d \n", nint);
357
                                ii=0;
358
        while (ii<nint){
359
          int recid = buf[ii++];
360
          int len   = buf[ii++];
361
          if (dbg) printf("Buffer pointer %d\n", ii);    
362
          unsigned int *dbuf = (unsigned int *)&buf[ii];
363
          //if (n%1000==0) 
364
                if (dbg) printf("%d 0x%03x Len=%d\n",evtrec->nev,recid,len);
365
          //unsigned short edge;
366
          //int nhits;
367
                                  if (recid==0x140 || recid==0x141) {
368
            for (int i=0; i<len; i++) {
369
              int data      =  dbuf[i] & 0xFFFF ;
370
              int edge_type = (dbuf[i]>>16)&0x1  ;
371
              int overflow  = (dbuf[i]>>17)&0x1  ;
372
              int tdc_num   = (dbuf[i]>>25)&0x1  ;
373
              int channel   = ((dbuf[i]>>18)&0x1F) | tdc_num<<5 ;
374
              int ev_dat    = (dbuf[i]>>23)&0x1  ;    
375
              int last_dat  = (dbuf[i]>>30)&0x1  ;
376
              int nval_dat  = (dbuf[i]>>31)&0x1  ;
377
              if (dbg){
378
                if (ev_dat) printf("Event %d\n",data);
379
                else printf("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",
380
                channel, edge_type,ev_dat,data,last_dat,nval_dat);
381
              }
382
              if (!ev_dat){
383
                if (!edge_type && !overflow) {
384
                  htdc->Fill(data, channel);
385
                  if(dbg) printf("ch: %d tdc: %d\n", channel, data);
386
                  if (dbg) nt->Fill(channel, pos.ix, pos.iy, data);
387
                  double tdcmin=tdcOffset[channel] - tdcCut;
388
                  double tdcmax=tdcOffset[channel] + tdcCut;
389
                  double time = data*TDC_BIN;
390
                  if(time >= tdcmin && time <= tdcmax) {
391
                    h_correctedTDC->Fill((time - tdcOffset[channel]), channel);
392
                    hnhitsx[channel]->Fill((pos.xset - OFFSETX) * MIKRO_BIN);
393
                    hnhitsy[channel]->Fill((pos.yset - OFFSETY) * MIKRO_BIN);
394
                    h2d[channel]->Fill((pos.xset - OFFSETX) * MIKRO_BIN, (pos.yset - OFFSETY) * MIKRO_BIN);
395
                    //h_threshold->Fill(channel, thr.threshold);
396
                    if (position(pos.xset-OFFSETX, pos.yset-OFFSETY, channel)) {
397
                      h_ch33->Fill(pos.xset-OFFSETX, pos.yset-OFFSETY);
398
                      h_threshold->Fill(channel, thr.threshold);
399
                    }
400
                  }
401
                  //gV673A->Fill(data,channel);
402
                  //gsumV673A[channel/16]->Fill(data);
403
                }
404
              }
405
              if (last_dat) break;
406
            }        
407
          } // if (recid== 0x140 || recid== 0x141)
408
          ii += len;
409
        } //while
410
 
411
// events ------------------------------------------------------------------------------------------
412
// fill histograms
413
/*  
414
        for(int i=0; i<NCH; i++) {
415
          //tdc=((double)evtrec->data[i])*TDC_BIN - tdcoffset[i];
416
          //adc=(double)evtrec->data[i+NCH]-adcoffset[i];
417
          if (gNtWrite) nt->Fill(i,pos.ix,pos.iy,tdc);
418
          //if( (qdcmi < adc) && (adc < qdcma) ) {
419
            htdc[i]->Fill(tdc);
420
            //}
421
          //hadc[i]->Fill(adc);
422
          //hcor[i]->Fill(adc,tdc);    
423
          //if(adc > corpar[i][2])
424
            //hctdc[i]->Fill( tdc - (corpar[i][0] + corpar[i][1]/TMath::Sqrt(adc - corpar[i][2])) );            
425
 
426
          if( (abs(padcenter[i][0] - pos.xset) < POSMARG) && (abs(padcenter[i][1] - pos.yset) < POSMARG) ) {
427
            htdcpos[i]->Fill(tdc);
428
            //hadcpos[i]->Fill(adc);
429
            //hcorpos[i]->Fill(adc,tdc);    
430
            //if(adc > corpar[i][2])
431
              //hctdcpos[i]->Fill( tdc - (corpar[i][0] + corpar[i][1]/TMath::Sqrt(adc - corpar[i][2])) );            
432
          }
433
 
434
          if((tdcmi < tdc) && (tdc < tdcma)) {            
435
            //hadc_cut[i]->Fill(adc);    
436
            hnhitsx[i]->Fill((pos.xset - OFFSETX) * MIKRO_BIN);
437
            hnhitsy[i]->Fill((pos.yset - OFFSETY) * MIKRO_BIN);
438
            h2d[i]->Fill((pos.xset - OFFSETX) * MIKRO_BIN, (pos.yset - OFFSETY) * MIKRO_BIN);
439
 
440
            //if(i==4) adc1=adc;
441
            //if(i==5) adc2=adc;
442
          }  
443
        } */
444
                                break;
445
 
446
                  case THRREC_ID:
447
                    status = gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
448
                    thrrec = (THRREC*) readbuf;
449
                    thr = *thrrec;
450
                    if (dbg) printf("THRREC id = %d len = %d threshold %d\n",
451
                                    thrrec->id, thrrec->len, thrrec->threshold);
452
                    break;
453
 
454
                        case ENDREC_ID:
455
                                gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
456
                                endrec = (ENDREC *) readbuf;
457
 
458
                                if(dbg) {
459
                                        printf("ENDREC_ID\n");
460
                                        printf("id = %d, len = %d, time = %d\n", endrec->id, endrec->len, endrec->time);
461
                                } else printf(" ENDREC\n");
462
 
463
        fcount++;
464
        switch(ftype-1) {
465
            case 0:
466
              sprintf(dfile, "./data/%s_file%02d.dat", dfile0, fcount);
467
              break;
468
            case 1:
469
              sprintf(dfile, "./data/%s_file%02d.dat.gz", dfile0, fcount);
470
              break;
471
            case 2:
472
              sprintf(dfile, "./data/%s_file%02d.gz", dfile0, fcount);
473
              break;
474
          }
475
 
476
        if(dfp) gzclose(dfp);
477
 
478
        dfp=gzopen(dfile,"rb");
479
        if(!dfp) {
480
          printf("      Cannot open data file: %s ---> Exiting\n", dfile);
481
                end_of_file = 1;
482
        } else {
483
          printf("      Opened data file: %s\n", dfile);
484
          end_of_file = 0;
485
        }
486
 
487
                                break;
488
 
489
                        default:
490
                                printf("switch(rec_id): default !!!\n");
491
                                end_of_file = 1;
492
                                break;
493
                }
494
 
495
                ceve++;
496
                if( (ceve%50000) == 0) printf(" Current event = %d\n", ceve);
497
                //if(dbg) if( ceve>dbg ) break;
498
                if(end_of_file) break;
499
        }
500
 
501
        if(dfp) {
502
          gzclose(dfp);
503
          delete dfp;
504
        }
505
        if(dbg) return 1;      
506
        if(rootfile) {
507
          nt->Write();
508
                rootfile->Write();
509
                printf("Saved to %s\n", fnameroot);
510
                rootfile->Close();
511
                delete rootfile;
512
        }              
513
 
514
        return 1;
515
}
516
 
517
int position(int x, int y, int channel)
518
{
519
  int flag = 0;
520
  if ( (x > (padCenter[channel][0] - 5000)) && (x < (padCenter[channel][0] + 5000)) &&
521
      (y > (padCenter[channel][1] - 5000)) && (y < (padCenter[channel][1] + 5000)) ) flag = 1;
522
  return flag;
523
}