Subversion Repositories f9daq

Rev

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

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