Rev 175 | Rev 182 | 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); |
||
96 | h1_cftdif[i]=new TH1F(histname,histname,2000,d[i].twin[0],d[i].twin[1]); |
||
97 | sprintf(histname,"qdc_%02d",i); |
||
98 | h1_qdc[i]=new TH1F(histname,histname,500,-0.1,4.9); |
||
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); |
||
102 | h2_tdcadc[i]=new TH2F(histname,histname,250,0.,0.5,400,d[i].twin[0],d[i].twin[1]); |
||
103 | sprintf(histname,"ctdcadc_%02d",i); |
||
104 | h2_ctdcadc[i]=new TH2F(histname,histname,250,0.,0.5,400,d[i].twin[0],d[i].twin[1]); |
||
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; |
||
192 | if ((chmin[i][0]>d[i].vcut[0])&&(chmin[i][0]<d[i].vcut[1])) { |
||
193 | h1_cftdif[i]->Fill(cftime[i]-cftime[trgch]+110.); |
||
194 | } |
||
101 | f9daq | 195 | // h2_tdcadc[i]->Fill(-chmin[i][0],letime[i][1]-cftime[trgch][1]+72); |
196 | // h2_ctdcadc[i]->Fill(-chmin[i][0],cftime[i][1]-cftime[trgch][1]+72); |
||
180 | f9daq | 197 | h2_tdcadc[i]->Fill(-chmin[0][0],letime[i]-cftime[trgch]+72); |
198 | h2_ctdcadc[i]->Fill(-chmin[0][0],cftime[i]-cftime[trgch]+72); |
||
199 | } |
||
200 | h2_ctalk->Fill(-chmin[0][0],-chmin[1][0]); |
||
201 | } |
||
202 | if (event<1) continue; |
||
203 | if (recid[0]!='C') continue; |
||
204 | if (ich>=nch) continue; |
||
101 | f9daq | 205 | |
180 | f9daq | 206 | lwfm[ich]=0; |
207 | qdc[ich]=0; |
||
208 | chmax[ich][0]=-1; chmax[ich][1]=-1; |
||
209 | chmin[ich][0]=1; chmin[ich][1]=-1; |
||
210 | letime[ich]=-1.; letime[ich]=-1.; |
||
211 | cftime[ich]=-1.; cftime[ich]=-1.; |
||
101 | f9daq | 212 | |
213 | |||
180 | f9daq | 214 | lwfm[ich]=1; |
215 | nwfm[ich]+=1; |
||
101 | f9daq | 216 | |
217 | |||
180 | f9daq | 218 | |
219 | t = &tcdata[ich][0]; |
||
220 | y = &chdat[ich][0]; |
||
221 | ay = &achdat[ich][0]; |
||
222 | |||
223 | float sum=0; |
||
224 | for (i=0; i<DWIDTH; i++) { |
||
225 | sum+= dtdata[ich][(i+trgcell)%DWIDTH]; |
||
226 | if (version) t[i]=sum; |
||
227 | else t[i]=dtdata[0][i]; // old format |
||
228 | } |
||
229 | tccalib[ich]=t[(DWIDTH-trgcell)%DWIDTH]; |
||
230 | |||
231 | for (i=0; i<DWIDTH; i++) { |
||
232 | if (version) t[i]-=(tccalib[ich]-tccalib[0]); // assume ich 0 is connected and always first in the data |
||
233 | y[i]=-0.5+(float)ichdat[ich][i]/0xFFFF; |
||
234 | if (version) y[i]+=evrec.rc; |
||
235 | h2_dpo[ich]->Fill(t[i],y[i]); |
||
236 | gr_wfm[ich]->SetPoint(i,t[i],y[i]+0.05*(ich-1)); |
||
237 | } |
||
238 | |||
239 | |||
240 | for (i=0; i<DWIDTH; i++) { |
||
241 | // accumulated waveforms |
||
242 | ay[i]+=y[i]; |
||
243 | |||
244 | // signal minumum |
||
245 | if (y[i]>chmax[ich][0]) { |
||
246 | chmax[ich][0]=y[i]; |
||
247 | chmax[ich][1]=t[i]; |
||
248 | } |
||
249 | |||
250 | // signal maximum |
||
251 | if (y[i]<chmin[ich][0]) { |
||
252 | chmin[ich][0]=y[i]; |
||
253 | chmin[ich][1]=t[i]; |
||
254 | } |
||
255 | float t0 = t[i]; |
||
256 | float t0p = t[i+1]; |
||
257 | float t0n = t[i-1]; |
||
258 | // charge in the range |
||
259 | if ((t0>d[ich].adcgate[0])&&(t0<d[ich].adcgate[1])) { |
||
260 | qdc[ich]+=-y[i]*(t0p-t0n)/2; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | for (i=1; i<DWIDTH-1; i++) { |
||
265 | float t0 = t[i]; |
||
266 | float t0p = t[i+1]; |
||
267 | float t0n = t[i-1]; |
||
268 | // leading edge in the range |
||
269 | if ((t0>d[ich].twin[0])&&(t0<d[ich].twin[1])) { |
||
270 | if ( ((!d[ich].edge)&&(y[i]<d[ich].threshold) || |
||
271 | ( d[ich].edge)&&(y[i]>d[ich].threshold ) ) |
||
272 | &&(letime[ich]<0.)) { |
||
273 | letime[ich]=t0n+(t0-t0n)*(d[ich].threshold-y[i-1])/(y[i]-y[i-1]); |
||
274 | } |
||
275 | // constant fraction time in the range |
||
276 | if ( ( ((!d[ich].edge) && (chmin[ich][0]<d[ich].threshold)&&(y[i]<chmin[ich][0]*d[ich].cfrac)) || |
||
277 | (( d[ich].edge) && (chmax[ich][0]>d[ich].threshold)&&(y[i]>chmax[ich][0]*d[ich].cfrac)) ) |
||
278 | &&(cftime[ich]<0.) |
||
279 | |||
280 | ) { |
||
281 | cftime[ich]=t0n+(t0-t0n)*(chmin[ich][0]*d[ich].cfrac-y[i-1])/(y[i]-y[i-1]); |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | if ((chmax[ich][1]>d[ich].twin[0])&&(chmax[ich][1]<d[ich].twin[1])) { |
||
287 | h1_chmax[ich]->Fill(chmax[ich][0]); |
||
288 | if (chmax[ich][0]>d[ich].threshold) h1_chmaxt[ich]->Fill(chmax[ich][1]); |
||
289 | } |
||
290 | if ((chmin[ich][1]>d[ich].twin[0])&&(chmin[ich][1]<d[ich].twin[1])) { |
||
291 | h1_chmin[ich]->Fill(chmin[ich][0]); |
||
292 | if (chmin[ich][0]<d[ich].threshold) h1_chmint[ich]->Fill(chmin[ich][1]); |
||
293 | } |
||
294 | h1_qdc[ich]->Fill(qdc[ich]); |
||
295 | h1_letime[ich]->Fill(letime[ich]); |
||
296 | h1_cftime[ich]->Fill(cftime[ich]); |
||
297 | time(&t0); |
||
298 | |||
299 | if ((t0!=told) || ((updfrq>0)&&(!(iev%updfrq) ) ) ) { |
||
300 | c_wfm->Update(); |
||
301 | for (i=0; i<DWIDTH; i++) { |
||
302 | ay[i]/=nwfm[ich]; |
||
303 | gr_wfm[ich]->SetPoint(i,t[i],ay[i]+0.00*(ich-1)); |
||
304 | } |
||
305 | printf("processing event %d\n",iev); |
||
306 | told=t0; |
||
307 | } |
||
175 | f9daq | 308 | } |
101 | f9daq | 309 | |
180 | f9daq | 310 | printf("number of events: %d\n",iev); |
311 | fclose(fin); |
||
101 | f9daq | 312 | |
180 | f9daq | 313 | TCanvas* c_dpo= new TCanvas("c_dpo","DPO",750,50,700,700); |
314 | c_dpo->Divide(2,2); |
||
315 | for (i=0; i<nch; i++) {c_dpo->cd(i+1); h2_dpo[i]->Draw("colz");} |
||
101 | f9daq | 316 | |
180 | f9daq | 317 | TCanvas* c_cftdif= new TCanvas("c_cftdif","DPO",750,50,700,700); |
318 | c_cftdif->Divide(2,2); |
||
319 | for (i=0; i<nch; i++) {c_cftdif->cd(i+1); h1_cftdif[i]->Draw();} |
||
320 | return; |
||
321 | } |
||
101 | f9daq | 322 | |
180 | f9daq | 323 | |
324 | void drsana(int nev=1000, int updfrq=20, char *FileName="./data/x1.dat", int trgch=0){ |
||
325 | |||
326 | // float threshold[nch][2]={{0.,-0.02},{0.,0.},{0.,0.},{0.,-0.25}}; |
||
327 | // float twin[nch][2]={{120.,130.},{100.,150.},{100.,150.},{70.,90.}}; |
||
328 | // float adcgate[nch][2]={{12nch.,136.},{120.,140.},{120.,140.},{70.,90.}}; |
||
329 | // float threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}}; |
||
330 | // float twin[4][2]={{80.,100.},{80.,100.},{90.,110.},{50.,70.}}; |
||
331 | // float adcgate[4][2]={{80.,110.},{80.,110.},{90.,110.},{50.,70.}}; |
||
332 | float threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}}; |
||
333 | float twin[4][2]={{100.,120.},{100.,120.},{110.,130.},{60.,80.}}; |
||
334 | float adcgate[4][2]={{105.,125.},{105.,125.},{110.,130.},{60.,80.}}; |
||
335 | float vcut[4][2]={{-0.49,-0.07},{-1,1},{-1,1},{-1,1}}; |
||
336 | int edge[4]={1,1,1,1};// 0 -- rising, 1 -- falling |
||
337 | |||
338 | DrsChannel c[2]; |
||
339 | |||
340 | c[0].cfrac = CFRAC; |
||
341 | c[0].threshold = threshold[0][0]; |
||
342 | c[0].twin[0] = twin[0][0]; |
||
343 | c[0].twin[1] = twin[0][1]; |
||
344 | |||
345 | c[0].adcgate[0] = adcgate[0][0]; |
||
346 | c[0].adcgate[1] = adcgate[0][1]; |
||
347 | |||
348 | c[0].vcut[0] = vcut[0][0]; |
||
349 | c[0].vcut[1] = vcut[0][1]; |
||
350 | |||
351 | c[0].edge = edge[0]; |
||
352 | |||
353 | |||
354 | c[1].cfrac = CFRAC; |
||
355 | c[1].threshold = threshold[1][0]; |
||
356 | c[1].twin[0] = twin[1][0]; |
||
357 | c[1].twin[1] = twin[1][1]; |
||
358 | |||
359 | c[1].adcgate[0] = adcgate[1][0]; |
||
360 | c[1].adcgate[1] = adcgate[1][1]; |
||
361 | |||
362 | c[1].vcut[0] = vcut[1][0]; |
||
363 | c[1].vcut[1] = vcut[1][1]; |
||
364 | |||
365 | c[1].edge = edge[1]; |
||
366 | drs * d = new drs(FileName, 2, c, trgch, nev, updfrq ); |
||
101 | f9daq | 367 | } |