Subversion Repositories f9daq

Rev

Rev 36 | Rev 40 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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