Subversion Repositories f9daq

Rev

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