Subversion Repositories f9daq

Rev

Rev 37 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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