Subversion Repositories f9daq

Rev

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

Rev Author Line No. Line
101 f9daq 1
#include "TH1.h"
2
#include "TH2.h"
3
#include "TGraph.h"
4
#include "TCanvas.h"
5
#include "TStyle.h"
6
#include "TLegend.h"
204 f9daq 7
#include "TFile.h"
101 f9daq 8
//#include "TROOT.h"
9
 
10
#include <stdio.h>
180 f9daq 11
#include <time.h>
101 f9daq 12
 
13
#define DATA_PATH  "./data/"
14
#define CFRAC 0.4
15
#define DWIDTH 1024
180 f9daq 16
#define NCH 4
17
#include "drs.h"
18
 
19
ClassImp (drs)
20
 
208 f9daq 21
 
22
TH1F *h1_chmax[NCH];
23
TH1F *h1_chmaxt[NCH];
24
TH1F *h1_chmin[NCH];
25
TH1F *h1_chmint[NCH];
26
TH1F *h1_letime[NCH];
27
TH1F *h1_cftime[NCH];
28
TH1F *h1_cftdif[NCH];
29
TH1F *h1_qdc[NCH];
30
TH2F *h2_dpo[NCH];
31
TH2F *h2_tdcadc[NCH];
32
TH2F *h2_ctdcadc[NCH];
33
TH2F *h2_ctalk;
34
TGraph *gr_wfm[NCH];
35
TLegend *leg;
36
 
37
 
38
TFile *froot;
39
 
40
int drs::histoinit( int nch, DrsChannel* d,  int trgch){
41
	char histname[128];
42
	leg = new TLegend(0.8, 0.8, 1, 1);
43
	for (int i=0; i<nch; i++) {
44
                sprintf(histname,"chmax_%02d",i);
45
                h1_chmax[i]=new TH1F(histname,histname,1100,-0.88,0.88);
46
                sprintf(histname,"chmaxt_%02d",i);
47
                h1_chmaxt[i]=new TH1F(histname,histname,DWIDTH,-0.1,204.7);
48
                sprintf(histname,"chmin_%02d",i);
49
                h1_chmin[i]=new TH1F(histname,histname,1100,-0.55,0.55);
50
                sprintf(histname,"chmint_%02d",i);
51
                h1_chmint[i]=new TH1F(histname,histname,DWIDTH,-0.1,504.7);
52
                sprintf(histname,"letime_%02d",i);
53
                h1_letime[i]=new TH1F(histname,histname,500,d[i].twin[0],d[i].twin[1]);
54
                sprintf(histname,"cftime_%02d",i);
55
                h1_cftime[i]=new TH1F(histname,histname,500,d[i].twin[0],d[i].twin[1]);
56
                sprintf(histname,"cftdif_%02d",i);
57
                h1_cftdif[i]=new TH1F(histname,histname,2000,d[i].twin[0]-d[trgch].twin[0],d[i].twin[1]-d[trgch].twin[0]);
58
                sprintf(histname,"qdc_%02d",i);
59
                h1_qdc[i]=new TH1F(histname,histname,500,-0.5,4.5);
60
                sprintf(histname,"dpo_%02d",i);
61
                h2_dpo[i]=new TH2F(histname,histname,DWIDTH,-0.1,200.7,3000,-0.9,0.9);
62
                sprintf(histname,"tdcadc_%02d",i);
63
                h2_tdcadc[i]=new TH2F(histname,histname,250,-0.1,0.5,400,d[i].twin[0]-d[trgch].twin[0],d[i].twin[1]-d[trgch].twin[0]);
64
                sprintf(histname,"ctdcadc_%02d",i);
65
                h2_ctdcadc[i]=new TH2F(histname,histname,250,-0.1,0.5,400,d[i].twin[0]-d[trgch].twin[0],d[i].twin[1]-d[trgch].twin[0]);
66
                gr_wfm[i]=new TGraph(DWIDTH);
67
                gr_wfm[i]->SetMinimum(-0.9);
68
                gr_wfm[i]->SetMaximum(0.9);
69
                gr_wfm[i]->SetLineColor(i+1);
70
                gr_wfm[i]->SetMarkerColor(i+1);
71
                sprintf(histname,"ch %d",i);
72
                leg->AddEntry(gr_wfm[i], histname, "l");
73
                if (i) gr_wfm[i]->Draw("LP"); else gr_wfm[i]->Draw("ALP");
74
                gr_wfm[i]->GetHistogram()->SetTitle("WaveForms");
75
        }
76
 
77
 
78
        h2_ctalk=new TH2F("ctalk","ctalk",250,0.,0.5,250,0.,0.5);
79
return 0;
80
}
81
 
82
drs::drs( const char *FileName, int nch, DrsChannel* d, int trgch, int first, int neve, int updfrq)
101 f9daq 83
{
180 f9daq 84
        int debug=0;
85
        int i,ich,iev;
86
        int stat;
208 f9daq 87
        int nev =  first + neve;
101 f9daq 88
//  char FileName[300]="./data/n2.dat";
180 f9daq 89
        FILE *fin;
101 f9daq 90
 
180 f9daq 91
        char recid[5]="SAMO";
92
        unsigned short *srecid = (unsigned short *) &recid[0];
175 f9daq 93
 
180 f9daq 94
        struct evrec {
95
                unsigned long evn;
96
                unsigned short tYear,tMonth,tDay,tHour,tMin,tSec,tmSec,rc;
97
        } evrec;
101 f9daq 98
 
180 f9daq 99
        float dtdata[NCH][DWIDTH];
100
        float tcdata[NCH][DWIDTH];
101
        float tccalib[NCH];
102
        float scaler[NCH];
103
        float *t;
104
        float *y;
105
        float *ay;
101 f9daq 106
 
180 f9daq 107
        short lwfm[NCH];
108
        int nwfm[NCH];
109
        int board;
110
        int version;
175 f9daq 111
 
180 f9daq 112
 
113
        unsigned short ichdat[NCH][DWIDTH];
114
        float chdat[NCH][DWIDTH];
115
        float achdat[NCH][DWIDTH];
116
        float chmax[NCH][2];
117
        float chmin[NCH][2];
118
        float letime[NCH],cftime[NCH],qdc[NCH];
119
 
120
 
101 f9daq 121
//  gROOT->Reset();
122
 
180 f9daq 123
        gStyle->SetOptStat(1111111);
101 f9daq 124
//  gStyle->SetPalette(52);
125
 
208 f9daq 126
        char rootname[0xFF];
127
		sprintf(rootname, "%s_x%d_y%d.root", FileName, 0,0);
128
        TFile *froot = new  TFile(rootname,"RECREATE");
129
		histoinit(nch, d, trgch);
130
		leg->Draw();
180 f9daq 131
        TCanvas *c_wfm= new TCanvas("c_wfm","WFM",50,50,700,700);
208 f9daq 132
 
180 f9daq 133
        c_wfm->Divide(1,1);
134
        c_wfm->cd(1);
101 f9daq 135
 
204 f9daq 136
 
208 f9daq 137
 
180 f9daq 138
        fin=fopen(FileName,"rb");
139
        if (fin==NULL) {
140
                printf("Error opening file %s\n",FileName);
141
                return;
142
        }
143
        printf("%s, %lu\n",recid,sizeof(evrec));
101 f9daq 144
 
145
 
180 f9daq 146
        for (ich=0; ich<nch; ich++) {
147
                nwfm[ich]=0;
148
                for (i=0; i<DWIDTH; i++) {
149
                        achdat[ich][i]=0;
150
                }
151
        }
152
        unsigned int trgcell = 0;
153
        int event = -1;
154
 
155
        iev=0;
156
        time_t t0,told;
157
        time(&t0);
158
        told=t0-1;
206 f9daq 159
		int posrec[9];
180 f9daq 160
        while(!feof(fin)) {
161
                if (iev>=nev) break;
162
                stat=fread(recid,1,4,fin);
163
                if (debug) {
164
                        if (recid[1]=='#') printf("%c%c%u\n",recid[0],recid[1],srecid[1]);
165
                        else printf("%s\n",recid );
166
                }
167
                switch (recid[0]) {
202 f9daq 168
 
205 f9daq 169
				        case 'P':{ // position record
170
									      int len;
206 f9daq 171
					       				  stat=fread(&len,1,4,fin);
172
										  if (len>0 && len<37) {
173
                                                     stat=fread(posrec,1,len,fin);
208 f9daq 174
													 if (recid[3]=='R'){
175
														if (iev>=first){
176
														  froot->Write();
177
														  froot->Close();
178
														  sprintf(rootname, "%s_x%d_y%d.root", FileName, posrec[3], posrec[4]);
179
                                                          froot = new  TFile(rootname,"RECREATE");
180
														  histoinit(nch,d, trgch);
181
													    }
206 f9daq 182
												        printf("Position  len=%d x=%d y=%d ix=%d iy=%d\n", len, posrec[0], posrec[1], posrec[3], posrec[4]);
208 f9daq 183
												     } else
206 f9daq 184
                                                        printf("Position record buffer length %d\n", len);
185
										  } else {
186
													printf("Wrong buffer length %d\n", len);
187
										  }
202 f9daq 188
                        break;
205 f9daq 189
								}
180 f9daq 190
                case 'D': // DRS
191
                        version = atoi(&recid[3]);
192
                        break;
193
                case 'T': // TIME
194
                        switch (recid[1]) {
195
                        case 'I':
196
                                event=0;
197
                                if (version ==0) version++;
198
                                break;
199
                        case '#': trgcell = srecid[1]; break;
200
                        }
201
                        break;
202
                case 'B':
203
                        board = srecid[1];
204
                        break;
205
                case 'C':
206
                        sscanf(recid,"C%d",&ich);
207
                        ich=ich-1;
208
                        if (event) {
209
                                if (version>1) stat=fread(&scaler[ich],1,sizeof(float),fin);
210
                                stat=fread(ichdat[ich],1,sizeof(ichdat[ich]),fin);
211
                        } else {
212
 
213
                                stat=fread(dtdata[ich],1,sizeof(float)*DWIDTH,fin);
214
 
215
                        }
216
                        break;
217
                case 'E':
218
                        stat=fread(&evrec.evn,1,sizeof(evrec),fin);
219
                        iev++;
220
                        if (!version)  {
221
                                stat=fread(dtdata[0],1,sizeof(float)*DWIDTH,fin);
222
                        }
223
                        event=1;
224
                        break;
225
                default:
226
                        printf("Unknown header! %s\n", recid);
227
                        break;
228
                }
208 f9daq 229
                if (iev<first) continue;
180 f9daq 230
 
231
                if (recid[0] == 'E') {
232
                        for ( i=0; i<nch; i++) {
233
                                if (!lwfm[i]) continue;
182 f9daq 234
								double pulseheight =  (d[i].edge)? chmax[i][0]: chmin[i][0];
235
 
236
								if ((pulseheight >d[i].vcut[0])&&
237
								    (pulseheight <d[i].vcut[1]) ) {
238
 
239
                                        h1_cftdif[i]->Fill(cftime[i]-cftime[trgch]);
180 f9daq 240
                                }
182 f9daq 241
                                if (!d[i].edge) pulseheight = - pulseheight;
242
                                h2_tdcadc[i] ->Fill(pulseheight,letime[i]-cftime[trgch]);
243
                                h2_ctdcadc[i]->Fill(pulseheight,cftime[i]-cftime[trgch]);
244
 
245
								//if (i==1) printf("ph=%f %f\n", pulseheight, letime[i]-cftime[trgch]);
180 f9daq 246
                        }
182 f9daq 247
						double ph0 =  (d[0].edge)? chmax[0][0]: -chmin[0][0];
248
						double ph1 =  (d[1].edge)? chmax[1][0]: -chmin[1][0];
249
                        h2_ctalk->Fill(ph0,ph1);
180 f9daq 250
                }
251
                if (event<1) continue;
252
                if (recid[0]!='C') continue;
253
                if (ich>=nch) continue;
101 f9daq 254
 
180 f9daq 255
                lwfm[ich]=0;
256
                qdc[ich]=0;
257
                chmax[ich][0]=-1; chmax[ich][1]=-1;
258
                chmin[ich][0]=1; chmin[ich][1]=-1;
259
                letime[ich]=-1.; letime[ich]=-1.;
260
                cftime[ich]=-1.; cftime[ich]=-1.;
101 f9daq 261
 
262
 
180 f9daq 263
                lwfm[ich]=1;
264
                nwfm[ich]+=1;
101 f9daq 265
 
266
 
180 f9daq 267
 
268
                t  = &tcdata[ich][0];
269
                y  = &chdat[ich][0];
270
                ay = &achdat[ich][0];
271
 
272
                float sum=0;
273
                for (i=0; i<DWIDTH; i++) {
274
                        sum+= dtdata[ich][(i+trgcell)%DWIDTH];
275
                        if (version) t[i]=sum;
276
                        else t[i]=dtdata[0][i]; // old format
277
                }
278
                tccalib[ich]=t[(DWIDTH-trgcell)%DWIDTH];
279
 
280
                for (i=0; i<DWIDTH; i++) {
281
                        if (version) t[i]-=(tccalib[ich]-tccalib[0]); // assume ich 0 is connected and always first in the data
282
                        y[i]=-0.5+(float)ichdat[ich][i]/0xFFFF;
192 f9daq 283
                        if (version) y[i]+=evrec.rc/1000.;
180 f9daq 284
                        h2_dpo[ich]->Fill(t[i],y[i]);
285
                        gr_wfm[ich]->SetPoint(i,t[i],y[i]+0.05*(ich-1));
286
                }
287
 
288
 
289
                for (i=0; i<DWIDTH; i++) {
290
                        // accumulated waveforms
291
                        ay[i]+=y[i];
292
 
293
                        // signal minumum
294
                        if (y[i]>chmax[ich][0]) {
295
                                chmax[ich][0]=y[i];
296
                                chmax[ich][1]=t[i];
297
                        }
298
 
299
                        // signal maximum
300
                        if (y[i]<chmin[ich][0]) {
301
                                chmin[ich][0]=y[i];
302
                                chmin[ich][1]=t[i];
303
                        }
304
                        float t0  = t[i];
305
                        float t0p = t[i+1];
306
                        float t0n = t[i-1];
307
                        // charge in the range
308
                        if ((t0>d[ich].adcgate[0])&&(t0<d[ich].adcgate[1])) {
182 f9daq 309
                                float sign =  (d[ich].edge)? 1: -1;
310
								qdc[ich]+=sign*y[i]*(t0p-t0n)/2.;
180 f9daq 311
                        }
312
                }
313
 
314
                for (i=1; i<DWIDTH-1; i++) {
315
                        float t0  = t[i];
316
                        float t0p = t[i+1];
317
                        float t0n = t[i-1];
318
                        // leading edge in the range
319
                        if ((t0>d[ich].twin[0])&&(t0<d[ich].twin[1])) {
320
                                if ( ((!d[ich].edge)&&(y[i]<d[ich].threshold) ||
321
                                      ( d[ich].edge)&&(y[i]>d[ich].threshold ) )
322
                                     &&(letime[ich]<0.)) {
323
                                        letime[ich]=t0n+(t0-t0n)*(d[ich].threshold-y[i-1])/(y[i]-y[i-1]);
324
                                }
325
                                // constant fraction time in the range
204 f9daq 326
								if (cftime[ich]<0.){
327
								  // negative signals
328
                                  if ((!d[ich].edge) && (chmin[ich][0]<d[ich].threshold)&&(y[i]<chmin[ich][0]*d[ich].cfrac )){
329
									  cftime[ich]=t0n+(t0-t0n)*(chmin[ich][0]*d[ich].cfrac-y[i-1])/(y[i]-y[i-1]);
330
								  }
331
								  // positive signals
332
                                  if (( d[ich].edge) && (chmax[ich][0]>d[ich].threshold)&&(y[i]>chmax[ich][0]*d[ich].cfrac)){
333
									  cftime[ich]=t0n+(t0-t0n)*(chmax[ich][0]*d[ich].cfrac-y[i-1])/(y[i]-y[i-1]);
334
								  }
335
								}
180 f9daq 336
                        }
337
                }
338
 
339
                if ((chmax[ich][1]>d[ich].twin[0])&&(chmax[ich][1]<d[ich].twin[1])) {
340
                        h1_chmax[ich]->Fill(chmax[ich][0]);
341
                        if (chmax[ich][0]>d[ich].threshold) h1_chmaxt[ich]->Fill(chmax[ich][1]);
342
                }
343
                if ((chmin[ich][1]>d[ich].twin[0])&&(chmin[ich][1]<d[ich].twin[1])) {
344
                        h1_chmin[ich]->Fill(chmin[ich][0]);
345
                        if (chmin[ich][0]<d[ich].threshold) h1_chmint[ich]->Fill(chmin[ich][1]);
346
                }
347
                h1_qdc[ich]->Fill(qdc[ich]);
348
                h1_letime[ich]->Fill(letime[ich]);
349
                h1_cftime[ich]->Fill(cftime[ich]);
350
                time(&t0);
351
 
352
                if ((t0!=told) || ((updfrq>0)&&(!(iev%updfrq) ) ) ) {
353
                        c_wfm->Update();
354
                        for (i=0; i<DWIDTH; i++) {
355
                                ay[i]/=nwfm[ich];
356
                                gr_wfm[ich]->SetPoint(i,t[i],ay[i]+0.00*(ich-1));
357
                        }
358
                        printf("processing event %d\n",iev);
359
                        told=t0;
360
                }
175 f9daq 361
        }
101 f9daq 362
 
180 f9daq 363
        printf("number of events: %d\n",iev);
364
        fclose(fin);
101 f9daq 365
 
180 f9daq 366
        TCanvas* c_dpo= new TCanvas("c_dpo","DPO",750,50,700,700);
367
        c_dpo->Divide(2,2);
204 f9daq 368
        for (i=0; i<nch; i++) {c_dpo->cd(i+1); h2_dpo[i]->DrawCopy("colz");}
101 f9daq 369
 
180 f9daq 370
        TCanvas* c_cftdif= new TCanvas("c_cftdif","DPO",750,50,700,700);
371
        c_cftdif->Divide(2,2);
204 f9daq 372
        for (i=0; i<nch; i++) {c_cftdif->cd(i+1); h1_cftdif[i]->DrawCopy();}
373
		froot->Write();
374
		froot->Close();
180 f9daq 375
        return;
376
}
101 f9daq 377
 
180 f9daq 378
 
379
void drsana(int nev=1000, int updfrq=20, char *FileName="./data/x1.dat", int trgch=0){
380
 
381
//  float threshold[nch][2]={{0.,-0.02},{0.,0.},{0.,0.},{0.,-0.25}};
382
//  float twin[nch][2]={{120.,130.},{100.,150.},{100.,150.},{70.,90.}};
383
//  float adcgate[nch][2]={{12nch.,136.},{120.,140.},{120.,140.},{70.,90.}};
384
//  float threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
385
//  float twin[4][2]={{80.,100.},{80.,100.},{90.,110.},{50.,70.}};
386
//  float adcgate[4][2]={{80.,110.},{80.,110.},{90.,110.},{50.,70.}};
182 f9daq 387
        double threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
180 f9daq 388
        float twin[4][2]={{100.,120.},{100.,120.},{110.,130.},{60.,80.}};
389
        float adcgate[4][2]={{105.,125.},{105.,125.},{110.,130.},{60.,80.}};
182 f9daq 390
        double vcut[4][2]={{-0.49,-0.07},{-1,1},{-1,1},{-1,1}};
180 f9daq 391
        int edge[4]={1,1,1,1};// 0 -- rising, 1 -- falling
392
 
393
        DrsChannel c[2];
394
 
395
        c[0].cfrac = CFRAC;
396
        c[0].threshold = threshold[0][0];
397
        c[0].twin[0] = twin[0][0];
398
        c[0].twin[1] = twin[0][1];
399
 
400
        c[0].adcgate[0] = adcgate[0][0];
401
        c[0].adcgate[1] = adcgate[0][1];
402
 
403
        c[0].vcut[0] = vcut[0][0];
404
        c[0].vcut[1] = vcut[0][1];
405
 
406
        c[0].edge = edge[0];
407
 
408
 
409
        c[1].cfrac = CFRAC;
410
        c[1].threshold = threshold[1][0];
411
        c[1].twin[0] = twin[1][0];
412
        c[1].twin[1] = twin[1][1];
413
 
414
        c[1].adcgate[0] = adcgate[1][0];
415
        c[1].adcgate[1] = adcgate[1][1];
416
 
417
        c[1].vcut[0] = vcut[1][0];
418
        c[1].vcut[1] = vcut[1][1];
419
 
420
        c[1].edge = edge[1];
421
        drs * d = new drs(FileName, 2, c, trgch, nev, updfrq );
101 f9daq 422
}