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