17 #define PRECISION_COEFF 1./64. // Precision of the filter coefficients. 1/64 is good enough
21 #define SMAL_UNDEF 1.0e-99
22 #define ALevel 100.0 // Amplitude level
39 Double_t
pars[] = {1.,0.,0,0};
43 Double_t
grf_x[
grf_NPTS] = {2000.,1000.,500.,200.,80.,60.,40.,30.,20.,10.,7.,5.,4.,2.5,2.0};
53 "1/(Systematic Error) of Time",
54 "1/(Systematic Error) of Amplitude",
61 #define NCorr 256 //number of correction points
72 #define STRIANGLE 4 //symmetric triangle
73 #define RTRIANGLE 5 //rigth triangle
75 #define UNKNOWN_SHAPE 7
77 #define NEED_FILTER_NOISE // define when you are interested in the noise output of the filter
97 gCanv[0] =
new TCanvas();
98 gCanv[1] =
new TCanvas();
100 gCanv[2] =
new TCanvas();
101 gCanv[3] =
new TCanvas();
102 gCanv[4] =
new TCanvas();
103 gCanv[5] =
new TCanvas();
110 {cout<<
"The filter length too long, >= "<<
NN<<
"\n"; exit(1);}
114 cout<<
"Preparing simulation of ";
117 case UNIPOLAR: cout<<
"UNIPOLAR";
break;
118 case BIPOLAR: cout<<
"BIPOLAR";
break;
120 case GAUSS: cout<<
"GAUSS";
break;
122 cout<<
" shape["<<
N<<
"]\n";
124 for(ii=0;ii<
NX;ii++)
ni[ii] = (Double_t) ii;
137 #ifdef NEED_FILTER_NOISE
138 cout<<
"Attention, NEED_FILTER_NOISE is active, it slows down the calculations\n";
140 cout<<
"The filter coefficients g[i] can be modified now."<<endl;
141 cout<<
"If they have been modified, execute: showShape(); getCorrections();"<<endl;
142 cout<<
"To simulate, execute: doSeries(pos,ntry)\n";
153 Double_t
fshape(Double_t *xx, Double_t *par)
156 Double_t x1 = xx[0] - par[1],
v1,
v;
162 return par[0]*pow(x1,4.)*(exp(-x1*16./
fN));
165 return par[0]*pow(x1,3.)*exp(-x1*16./
fN) * (4.-16./
fN*x1);
168 if(x1<v1) v = par[0]*x1/
v1;
169 else if(x1<=
fN-v1) v = par[0];
170 else v = par[0]*(
fN-x1)/v1;
174 return par[0]*exp(-(x1*x1)/1.);
176 return (x1<
fN/2.) ? par[0]*x1 : par[0]*(
fN-x1);
179 return (x1<v1) ? par[0]*(
fN-
v1)*x1/v1 : par[0]*(
fN-x1);
182 return (x1<v1) ? par[0]*(
fN-
v1)*x1/v1 : par[0]*(
fN-x1);
184 cout<<
"Unknown shape\n";
193 Double_t
par0[10],vv;
194 for(Int_t ii=0;ii<10;ii++) par0[ii] = par[ii];
206 Double_t
difup(Double_t *xx, Double_t *par)
208 Double_t x1 = xx[0] - par[1];
210 return par[0]*pow(x1,3.)*exp(-x1*16./
fN) * (4.-16./
fN*x1);
217 void show(Double_t *xx, Int_t nn=200, Char_t *xname=NULL, Char_t *yname=NULL, Int_t lcolor=kBlue, Int_t mstyle=kStar, Double_t msize=0.75)
219 gr =
new TGraph(nn,
ni,xx);
220 gr->SetLineColor(lcolor);
222 gr->SetMarkerColor(kBlack);
223 gr->SetMarkerStyle(mstyle);
224 gr->SetMarkerSize(msize);
226 if (xname) {
gr->GetXaxis()->SetTitle(xname);}
227 else gr->GetXaxis()->SetTitle(
"Sample");
229 if (yname) {
gr->SetTitle(yname);}
234 gCnv->SetCrosshair();
gCnv->SetTickx();
gCnv->SetTicky();
243 cout<<
"drawing "<<
fname<<
"\n";
244 TF1 *tf =
new TF1(fname,
fshape,position,position+length,2);
245 tf->SetParameter(0,ampl);
246 tf->SetParameter(1,position);
254 Double_t
rms(Double_t *xx, Int_t nn=
NX)
256 Double_t
sum=0., sum2=0.,
xmax=0.,
fN=(Double_t)nn;
257 for(Int_t ii=0;ii<nn;ii++)
261 sum2 += xx[ii]*xx[ii];
263 sum2 = sqrt(sum2/
fN);
264 cout<<
"srsq="<<sum2*sqrt(
fN)<<
", rms="<<sum2<<
", mean="<<sum/
fN<<
", max="<<
xmax<<
"\n";
273 Double_t sumw=0.,sumwin=0.;
274 for(Int_t ii=pos-nn+1;ii<=
pos;ii++)
277 sumw += xx[ii]*((Double_t)ii);
288 Int_t ishift = (Int_t) shift;
293 v[ii] =
grand.Gaus(0.,noise_rms);
296 if(ii>=shift && ii<shift+
N)
305 vv =
rms((
s+ishift),
N);
307 vv =
rms(
v+ishift,
N);
309 vv =
rms(
x+ishift,
N);
326 for (ii=
N;ii<
NX;ii++) {
y[ii] = 0.;
yma[ii] = 0.;}
327 #ifdef NEED_FILTER_NOISE
328 for (ii=
N;ii<
NX;ii++)
330 for (ii=
N;ii<=((Int_t)
gpos+1+3*
N);ii++)
342 if(
dy[imax]-
dy[imax+1] <= 0.) {cout<<
"Computational error!\n";exit();}
351 tau += (Double_t)imax -
fN;
353 tauw =
meanw(
y,imax+timewin/2.,timewin);
357 cout<<
"ymax["<<
tau<<
"]= "<<
ymax<<
"\n";
359 cout<<
"weigthed mean = "<<tauw<<
"\n";
373 Double_t
series(Double_t
pos, Double_t noise, Int_t
ntry, Int_t nscans=1, Int_t acorr=1)
375 cout<<
"series("<<pos<<
","<<noise<<
","<<ntry<<
","<<nscans<<
","<<acorr<<
")"<<endl;
377 Double_t sumt,sumt2,ty[4];
379 Double_t sumw=0.,sumw2=0.;
381 Double_t suma, suma2;
382 Double_t sumdevt2,sumdeva2;
391 for(iscan=0;iscan<nscans;iscan++)
393 poss = pos + (Double_t)iscan / (Double_t)nscans;
394 suma = 0.; suma2=0.; sumt=0.; sumt2=0.; sumdevt2=0.; sumdeva2=0.;
395 for(ii = 0; ii<
ntry; ii++)
404 sumw += tauw; sumw2 += tauw*tauw;
408 i1 = TMath::Nint((tau-floor(tau))*
NCorr);
409 if(i1>=NCorr) {cout<<
"Index error "<<i1<<endl;
continue;}
411 v2 = tau -
tCorr[i1];
415 v1 = ymax/(1.+
aCorr[i1]) - meana;
424 mean = sumt/
ns; sigma = sqrt(fabs(sumt2/ns - mean*mean));
425 cout<<
"["<<ii<<
"] mean="<<mean<<
", s="<<
sigma;
426 mean = suma/
ns; sigma = sqrt(fabs(suma2/ns - mean*mean));
427 cout<<
", F="<<mean<<
" sF="<<sigma
428 <<
", A="<<mean*
gmax<<
" sA="<<sigma*
gmax<<
"\n";
437 ty[0] = sqrt(fabs(sumt2/ns-ty[2]*ty[2]));
439 ty[1] = sqrt(fabs(suma2/ns - (ty[3]*ty[3])));
440 if(ty[0]!=0.) ty[0] = 1./ty[0];
else ty[0] = 1.e100;
441 if(ty[1]!=0.) ty[1] = 1./ty[1];
else ty[1] = 1.e100;
447 else v1 = (ty[2] - floor(ty[2]))*(Double_t)
NCorr;
448 i1 = TMath::Nint(v1);
if(i1>=
NCorr) i1=
NCorr-1;
if(i1<0) i1=0;
459 serY[6] = 1./sqrt(sumdevt2/ns);
460 serY[7] = meana/sqrt(sumdeva2/ns);
466 i1 = TMath::Nint((ty[2]-floor(ty[2]))*
NCorr);
467 cout<<
"Corr["<<i1<<
"]="<<
tCorr[i1]<<
","<<
aCorr[i1]<<
"\n";
469 cout<<
"time correction["<<i1<<
"]="<<
tCorr[i1]<<
" 1/stdev="<<1./(
gpos-
serY[2])<<
" from "<<ty[2]<<
" to "<<
serY[2]<<
"\n";
470 serY[3] /= (1.+aCorr[i1]);
471 cout<<
"ampl. correction="<<aCorr[i1]<<
" from "<<ty[3]<<
" to "<<
serY[3]<<
"\n";
481 mean = sumw/
ns; sigma = sqrt(fabs(sumw2/ns-mean*mean));
482 cout<<
"mean="<<mean<<
", "<<sigma<<
"\n";
492 Double_t sumt=0.,sumt2=0.,
sum=0.,sum2=0.;
496 for(Int_t ii=0;ii<
ntry;ii++)
500 gf->SetParameter(1,pos);
502 gr =
new TGraph(200,
ni,
x);
504 v =
gf->GetParameter(0);
505 sum +=
v; sum2 += v*
v;
506 v =
gf->GetParameter(1);
507 sumt +=
v; sumt2 += v*
v;
510 vi = (Double_t)(ii+1.);
511 mean = sumt/vi; sigma = sqrt(fabs(sumt2/vi - mean*mean));
512 cout<<
"["<<ii<<
"] Pos="<<mean<<
", sigma="<<sigma
513 <<
". ParError="<<
gf->GetParError(1)<<
"\n";
516 mean = sumt/
ns; sigma = sqrt(fabs(sumt2/ns - mean*mean));
517 cout<<
"Pos="<<mean<<
", sPos="<<sigma<<
", ePos="<<
gf->GetParError(1);
518 mean =
sum/
ns; sigma = sqrt(fabs(sum2/ns - mean*mean));
519 cout<<
". A="<<mean*ca<<
", sA="<<sigma*ca<<
", eA="<<ca*
gf->GetParError(0)<<
"\n";
529 for(kk=0;kk<
NCorr;kk++)
550 v1 = fabs(TMath::MinElement(NCorr,
serCorr[0]));
551 v2 = fabs(TMath::MaxElement(NCorr,
serCorr[0]));
552 serY[4] = 1./TMath::Max(v1,v2);
553 v1 = fabs(TMath::MinElement(NCorr,
serCorr[1]));
554 v2 = fabs(TMath::MaxElement(NCorr,
serCorr[1]));
555 serY[5] = 1./TMath::Max(v1,v2);
556 cout<<
"1/syserr for t,A = "<<
serY[4]<<
", "<<
serY[5]<<
"\n";
571 cout<<
"Series "<<ii<<
" out of "<<grf_NPTS
572 <<
" for "<<
ntry<<
" points with S/N="<<
grf_x[ii]<<
"\n";
585 Int_t lcolor,lwidth,lstyle,lmcolor,lmstyle;
591 if(
fc[kk]==NULL)
fc[kk] =
new TCanvas();
592 else fc[kk]->Clear();
595 lcolor = (kk<2) ? kBlack : kBlue;
597 lwidth=2; lmcolor=lcolor; lmsize=1.;
608 lcolor=kRed; lwidth=4; lstyle=7; lmcolor=2; lmstyle=kDot; lmsize=.75;
613 lcolor=kBlue; lwidth=2; lstyle=2; lmcolor=lcolor; lmstyle=kCircle; lmsize=1.;
617 fg[kk]->SetLineColor(lcolor);
618 fg[kk]->SetLineWidth(lwidth);
619 fg[kk]->SetLineStyle(lstyle);
620 fg[kk]->SetMarkerColor(lmcolor);
621 fg[kk]->SetMarkerStyle(lmstyle);
622 fg[kk]->SetMarkerSize(lmsize);
624 (
fg[kk]->GetXaxis())->
SetTitle(
"A/RMS(noise)");
633 fc[kk]->SetCrosshair();
638 else fg[kk]->Draw(
"PL");
651 gf->SetParameter(0,
pars[0]);
654 cout<<
"Filter max["<<
gmaxx<<
"]="<<
gmax<<
"\n";
669 cout<<
"normalization = "<<
norm<<
", "<<
norm_ma<<
"\n";
674 y[ii]=0.;
dy[ii]=0.;
dyma[ii]=0.;
678 gf->SetParameter(0,
pars[0]);
679 gmax =
gf->GetMaximum();
681 cout<<
"Filter max["<<
gmaxx<<
"]="<<gmax<<
"\n";
688 for(Int_t ii=0;ii<
N;ii++)
700 show(
g,
N,0,0,0,21,2.);
gr->GetYaxis()->SetTitle(
"Filter Coefficients");
705 Double_t rmsnoise =
ALevel/10.;
707 TString txt =
"Signal (A=";
708 txt+=
ALevel; txt+=
" plus noise (RMS="; txt+=rmsnoise; txt+=
")";
711 gr->GetYaxis()->SetTitle(txt);
716 show(
y);
gr->GetYaxis()->SetTitle(
"Filter output");
738 cout<<
"Collecting corrections...\n";
744 TGraph *corrGraph0,*corrGraph1;
745 for(ii=0;ii<
NCorr;ii++) t1[ii] = (Double_t)ii/(Double_t)NCorr;
746 corrGraph0 =
new TGraph(NCorr,t1,
tCorr);
747 corrGraph0->SetLineColor(kBlue);corrGraph0->SetLineWidth(2);
748 corrGraph0->SetMarkerColor(kBlack);corrGraph0->SetMarkerStyle(kDot);
749 corrGraph0->GetXaxis()->SetTitle(
"t/T");
750 corrGraph0->SetTitle(
"Time Corrections");
753 corrGraph0->Draw(
"APL");
756 corrGraph1 =
new TGraph(NCorr,t1,
aCorr);
757 corrGraph1->SetLineColor(kRed);corrGraph1->SetLineWidth(2);
758 corrGraph1->SetMarkerColor(kBlack);corrGraph1->SetMarkerStyle(kDot);
759 corrGraph1->GetXaxis()->SetTitle(
"t/T");
760 corrGraph1->SetTitle(
"Amplitude Corrections");
761 canv->SetGridx(); canv->SetGridy();
762 canv->SetTickx(); canv->SetTicky();
763 corrGraph1->Draw(
"APL");
772 cout<<
"Drawing the autocorrelation function in the interval [0:1]\n";
776 facrl->SetParameter(1,1.);
777 facrl->SetParameter(0,0.);
778 vg = facrl->Integral(0.,
fN);
779 for(ii=0;ii<
NCorr;ii++)
781 xacrl[ii] = (Double_t)ii/(Double_t)
NCorr;
782 facrl->SetParameter(0,xacrl[ii]);
783 yacrl[ii] = facrl->Integral(0.,
fN)/vg;
786 TGraph *gACorr =
new TGraph(NCorr,xacrl,yacrl);
787 gACorr->SetLineStyle(1);
788 gACorr->SetLineWidth(2);
789 gACorr->SetTitle(
"AutoCorrelation");
790 gACorr->GetXaxis()->SetTitle(
"t/T");