Subversion Repositories f9daq

Rev

Rev 90 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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