45 #include <EventTypes.h>
65 using namespace TMath;
79 int cosmicRN[
cosmicruns] = {212, 215, 216, 218, 223, 337, 338, 339, 405, 406, 229, 240};
93 Double_t
par0Min[] = {-50000., 0., 2., 0., -50000., -0.3};
102 Double_t
ps( Double_t *chadc, Double_t *par)
104 Double_t
x = chadc[0];
106 Double_t
mu = par[0];
108 Double_t
sigma = par[2];
109 Double_t
norm = par[3];
112 for (Double_t
n = 1.0;
n < 100.0;
n++ ) {
113 sum += TMath::Poisson(
n,mu)*TMath::Gaus(x,
n*g,sigma,kTRUE);
134 Double_t result = par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
136 result += par[4]*TMath::Gaus(x[0],par[5],par[6]);
138 Double_t
norm = par[4 +
p*3];
139 Double_t
mean = par[5 +
p*3];
143 Double_t
sigma = par[6 +
p*3];
144 result += norm*TMath::Gaus(x[0],mean,sigma);
156 TH1F *
data = (TH1F*)(gROOT->FindObject(hName));
158 cout<<
"spmCalib Data Histogramm "<<hName<<
" not found"<<endl;
161 cout<<hName<<
" calibration mode "<<mode<<endl;
164 Double_t range(150.);
166 Double_t bckgr0(100.), bckgr1(-1./range), bckgr2(0.);
167 Double_t pNorm(1000.), pLoc(0.), pRMS(1.);
168 Double_t par[4+maxPeaks*3];
178 for (
int p=1;
p<= maxPeaks;
p++) {
179 par[4 +
p*3] = pNorm;
180 par[5 +
p*3] = par[3]*
p;
183 TH1F * dataCl = (TH1F*)(data->Clone(
"dataCl"));
184 dataCl -> SetAxisRange(0., range);
185 TH1F * dataCl2 = (TH1F*)data->Clone(
"dataCl2");
186 dataCl2 -> SetAxisRange(0., range);
187 TString cTitle =
"Peaks from "; cTitle += hName;
188 TCanvas *
peaks =
new TCanvas(
"peaks",cTitle,10,10,1000,900);
189 peaks -> Divide(1,2);
194 TSpectrum *
s =
new TSpectrum(maxPeaks);
195 s->SetResolution(2.);
196 Int_t nfound = s->Search(dataCl, 1.5,
"nobackground ", 0.10);
197 printf(
"TSpectrum: Found %d candidate peaks to fit\n",nfound);
200 TH1 *hb = s->Background(dataCl, 20,
"same");
201 if (hb) peaks->Update();
204 cout<<
"sipmCalib One or No peaks were found - EXITING"<<endl;
213 TF1 *fline =
new TF1(
"fline",
"pol2",0.,range);
215 dataCl2 -> Fit(
"fline",
"qn");
217 par[0] = fline->GetParameter(0);
218 par[1] = fline->GetParameter(1);
219 par[2] = fline->GetParameter(2);
221 Float_t * xpeaks = s->GetPositionX();
223 Int_t pindex[nfound];
224 TMath::Sort(nfound, xpeaks, pindex, kFALSE);
226 Double_t sep[nfound-1];
229 for (
int ip = 1; ip<nfound; ip++){
230 sep[ip-1] = xpeaks[pindex[ip]];
231 if(ip>1) sep[ip-1] -= xpeaks[pindex[ip-1]];
232 cout<<
"Separation "<<ip<<
" val "<<sep[ip-1]<<endl;
236 Int_t cont(0); Double_t avsep(0.);
237 for (
int ip = 0; ip < nfound-1; ip++){
244 if(abs((avsep- sep[ip])/(avsep+ sep[ip]))<0.2) {
245 avsep = (avsep*cont+sep[ip])/(cont+1);
249 cout<<
"ip "<<ip<<
" cont "<<cont<<
" avsep "<<avsep<<
" var "<<(ip!=0? (avsep- sep[ip])/(avsep+ sep[ip]) : 0.)<<endl;
251 cout<<
"Zero approximation to sSingle Pixel Calib = "<<avsep<<
" Contributors "<<cont<<endl;
255 for (
int p=0;
p<nfound;
p++) {
256 Double_t xp = xpeaks[pindex[
p]];
257 Int_t bin = dataCl2->GetXaxis()->FindBin(xp);
258 Double_t yp = dataCl2->GetBinContent(bin);
259 cout<<
"Testing Peak (LFit) "<<
p<<
" index "<<pindex[
p]<<
" xp "<<xp<<
" bin "<<bin<<
" yP "<<yp<<
" BCKG "<<fline->Eval(xp)<<endl;
261 Double_t bckgr = hb->GetBinContent(bin);
262 cout<<
"Testing Peak (TSp ) "<<
p<<
" xp "<<xp<<
" bin "<<bin<<
" Bcgrd "<<bckgr<<
" Signal "<<yp-bckgr<<endl;
263 if (yp-TMath::Sqrt(yp) < bckgr)
continue;
271 printf(
"Now fitting: Be patient\n");
274 TVirtualFitter::Fitter(dataCl2,10+
calibPeaks*3);
275 fit->SetParameters(par);
278 fit->FixParameter(5+ip*3, par[5+ip*3]);
279 fit->SetParLimits(3, 0.5*par[3], 1.5*par[3]);
280 }
else fit->FixParameter(3, par[3]);
282 for(
int ip=0; ip<
calibPeaks; ip++) fit->SetParLimits(6+ip*3, 0.5, par[3]/2.);
286 fit->GetParameters(par);
288 char p5[10], p6[10], p3[10];
289 sprintf(p5,
"%.2f ", par[5]);
290 sprintf(p6,
"%.2f ", par[6]);
291 sprintf(p3,
"%.2f ", par[3]);
293 TString ped =
"Pedestal at "; ped += p5; ped +=
" RMS = "; ped += p6;
294 TString cal =
"Pixel Calibtion "; cal += p3; cal +=
" ADC counts per pixel";
296 Double_t
ymax = data-> GetMaximum();
297 label.DrawText(30, ymax*0.8,ped);
298 label.DrawText(30, ymax*0.6,cal);
304 return 12.5*(1 + ax/0.19);
314 pedestal = par[4]+x[0]*par[5];
326 signal = par[0]*pow((x[0]-par[1]),par[2])*exp(-(x[0]-par[1])*par[3]);
342 return par[3]*(TMath::Erf((x[0]-par[0])/par[1])+1.)*TMath::Exp(par[2]*x[0]);
346 Double_t
erf_g(Double_t *
x, Double_t * par){
347 float g = par[0]/(sqrt(6.2830)*par[2])*TMath::Exp(-(x[0]-par[1])*(x[0]-par[1])/(2.*par[2]*par[2]));
348 return g+par[6]*(TMath::Erf((x[0]-par[3])/par[4])+1.)*TMath::Exp(par[5]*x[0]);
354 return par[0]*pow(x[0],par[1])*pow((1-x[0]),par[2]);
361 float g = par[0]/(sqrt(6.2830)*par[2])*TMath::Exp(-(x[0]-par[1])*(x[0]-par[1])/(2.*par[2]*par[2]));
362 float p = par[3]*pow(x[0],par[4])*pow((1-x[0]),par[5]);
375 char *
s = getenv(
"BASE_DIR");
376 TString basedir = (s==0||strlen(s)==0)?
"/home/kistenev/workarea/" : s;
389 float y1 = f->Derivative(x1, 0, 0.002);
390 float y2 = f->Derivative(x2, 0, 0.002);
392 return (f->Eval(x1)>f->Eval(x2))? x1 : x2;
396 float dx = fabs(xx2-xx1);
398 while(y1*y2<0&&dx>0.001){
400 float y = f->Derivative(x, 0, 0.01);
401 if(y1*y>0) xx1 =
x;
else xx2 =
x;
411 float yhm = f->Eval(peak)/2;
416 float xm = (xr+xl)/2.;
417 float ym = f->Eval(xm);
419 while(abs(ym-yhm)>0.01*yhm && xr-xl>0.001){
420 if(ym>yhm) xr = xm;
else xl = xm;
425 bool lok = (abs(ym-yhm)>0.1*yhm)?
false :
true;
431 while(abs(ym-yhm)>0.01*yhm&&xr-xl>0.001){
432 if(ym>yhm) xl = xm;
else xr = xm;
438 bool rok = (abs(ym-yhm)>0.1*yhm)?
false :
true;
441 if(rok) w = xmr-xml;
else w = 2*(peak-xml);
453 int bMax = proj->GetMaximumBin();
454 float pMax = proj->GetBinCenter(bMax);
457 x = proj->GetXaxis();
458 double xmin = x->GetXmin();
459 double xmax = x->GetXmax();
461 pE[1] = proj->GetRMS();
463 pE[3] = proj->GetBinContent(bMax)/TMath::Exp(pE[2]*pE[0]);
464 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
466 fE =
new TF1(
"fE",&
erfunc,xmin,xmax,4);
467 fE->SetParameters(pE);
468 proj->Fit(
"fE",
"NQL0");
469 fE->GetParameters(pE);
470 maxE = xPeak(fE,xmin,xmax);
472 if(maxE<=xmin||maxE>=xmax||pE[0]<=xmin) erfOK =
false;
473 float scale = (fine? 1. : 1.5);
475 pG[0] = proj->GetBinContent(bMax);
478 pG[2] =
width(fE,xmin,xmax,maxE);
480 pG[1] = proj->GetMean();
481 pG[2] = proj->GetRMS();
483 fG = (TF1*)(gROOT->FindObject(
"fG"));
486 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-0.3*scale),TMath::Min(xmax,pG[1]+0.075*scale));
487 fG->SetParameters(pG);
488 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-0.1*scale),TMath::Min(xmax,pG[1]+0.05*scale));
490 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-1.5*scale),TMath::Min(xmax,pG[1]+1.5*scale));
491 fG->SetParameters(pG);
492 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-2.*scale),TMath::Min(xmax,pG[1]+2.*scale));
494 proj->Fit(
"fG",
"NLQR0");
495 fG->GetParameters(pG);
496 return ((!erfOK||pG[1]<xmin+0.1||pG[1]>xmax-0.05)?
false :
true);
501 bool hcalUtil::emcFit(TH1D *
proj,
bool fine,
bool sing, Double_t * pE,
float & maxE, Double_t * pG,
bool MIP){
504 int bMax = proj->GetMaximumBin();
505 float pMax = proj->GetBinCenter(bMax);
508 x = proj->GetXaxis();
509 double xmin = x->GetXmin();
510 double xmax = x->GetXmax();
512 pE[1] = proj->GetRMS();
514 pE[3] = proj->GetBinContent(bMax)/TMath::Exp(pE[2]*pE[0]);
515 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
517 fE =
new TF1(
"fE",&
erfunc,xmin,xmax,4);
518 fE->SetParameters(pE);
519 proj->Fit(
"fE",
"NQL0");
520 fE->GetParameters(pE);
521 maxE = xPeak(fE,xmin,xmax);
523 if(maxE<=xmin||maxE>=xmax||pE[0]<=xmin) erfOK =
false;
525 float scale = (fine? 1. : 1.5);
527 pG[0] = proj->GetBinContent(bMax);
530 pG[2] =
width(fE,xmin,xmax,maxE);
532 pG[1] = proj->GetMean();
533 pG[2] = proj->GetRMS();
535 fG = (TF1*)(gROOT->FindObject(
"fG"));
538 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-0.3*scale),TMath::Min(xmax,pG[1]+0.075*scale));
539 fG->SetParameters(pG);
540 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-0.1*scale),TMath::Min(xmax,pG[1]+0.05*scale));
542 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-1.5*scale),TMath::Min(xmax,pG[1]+1.5*scale));
543 fG->SetParameters(pG);
544 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-2.*scale),TMath::Min(xmax,pG[1]+2.*scale));
546 proj->Fit(
"fG",
"NLQR0");
547 fG->GetParameters(pG);
549 return ((!erfOK||pG[1]<xmin+0.1||pG[1]>xmax-0.05)?
false :
true);
554 bool hcalUtil::emcFit(TH1D * proj,
bool sing, Double_t * pE,
float & maxE, Double_t * pG){
557 int bMax = proj->GetMaximumBin();
558 float pMax = proj->GetBinCenter(bMax);
561 x = proj->GetXaxis();
562 double xmin = x->GetXmin();
563 double xmax = x->GetXmax();
565 pE[1] = proj->GetRMS();
567 pE[3] = proj->GetBinContent(bMax)/TMath::Exp(pE[2]*pE[0]);
568 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
570 fE =
new TF1(
"fE",&
erfunc,xmin,xmax,4);
571 fE->SetParameters(pE);
572 proj->Fit(
"fE",
"QNL0");
573 fE->GetParameters(pE);
574 maxE = xPeak(fE,xmin,xmax);
576 if(maxE<=xmin||maxE>=xmax||pE[0]<=xmin) erfOK =
false;
578 pG[0] = proj->GetBinContent(bMax);
581 pG[2] =
width(fE,xmin,xmax,maxE) ;
583 pG[1] = proj->GetMean();
584 pG[2] = proj->GetRMS();
586 fG = (TF1*)(gROOT->FindObject(
"fG"));
588 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-pG[2]),TMath::Min(xmax,pG[1]+pG[2]));
589 fG->SetParameters(pG);
590 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-pG[2]),TMath::Min(xmax,pG[1]+pG[2]));
591 proj->Fit(
"fG",
"QNLR0");
592 fG->GetParameters(pG);
594 return ((!erfOK||pG[1]<xmin+0.1||pG[1]>xmax-0.05)?
false :
true);
598 bool hcalUtil::emcFit(TH1D * proj,
bool sing, Double_t * pE,
float & maxE, Double_t * pG,
float minx,
float maxx){
601 int bMax = proj->GetMaximumBin();
602 float pMax = proj->GetBinCenter(bMax);
606 double xmax = x->GetXmax();
607 xmin = TMath::Max((
double)minx,xmin);
608 xmax = TMath::Min((
double)maxx,xmax);
610 pE[1] = proj->GetRMS();
612 pE[3] = proj->GetBinContent(bMax)/TMath::Exp(pE[2]*pE[0]);
613 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
615 fE =
new TF1(
"fE",&
erfunc,xmin,xmax,4);
616 fE->SetParameters(pE);
617 proj->Fit(
"fE",
"QNL0");
618 fE->GetParameters(pE);
619 maxE = xPeak(fE,xmin,xmax);
621 if(maxE<=xmin||maxE>=xmax||pE[0]<=xmin) erfOK =
false;
623 pG[0] = proj->GetBinContent(bMax);
626 pG[2] =
width(fE,xmin,xmax,maxE) ;
629 pG[1] = proj->GetMean();
630 pG[2] = proj->GetRMS();
632 fG = (TF1*)(gROOT->FindObject(
"fG"));
634 fG =
new TF1(
"fG",
"gaus",TMath::Max(xmin,pG[1]-pG[2]),TMath::Min(xmax,pG[1]+pG[2]));
635 fG->SetParameters(pG);
636 fG->SetParLimits(1,TMath::Max(xmin,pG[1]-pG[2]),TMath::Min(xmax,pG[1]+pG[2]));
637 proj->Fit(
"fG",
"QNLR0");
638 fG->GetParameters(pG);
640 return ((!erfOK||pG[1]<xmin+0.1||pG[1]>xmax-0.05)?
false :
true);
648 int bMax = pr->GetMaximumBin();
651 TH1D *
p=(TH1D*)(pr->Clone());
652 float xmean = p->GetMean();
654 int b1 = x->FindBin(xmean-0.07);
655 int b2 = x->FindBin(xmean+0.07);
656 for (
int bin = b1; bin<=b2; bin++){
657 p->SetBinError(bin,p->GetBinContent(bin));
659 double xmin = x->GetXmin();
660 if(xmin<=0.) xmin = 0.05;
662 double xmax = x->GetXmax();
663 pE[0] = pr->Integral()*x->GetBinWidth(0)/(log(xmax)-log(xmin));;
666 cout<<
"Initial par. values"<<pE[0]<<
" "<<pE[1]<<
" "<< pE[2]<<endl;
667 if(pE[0]<=0.)
return false;
669 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
672 fE =
new TF1(
"fE",&
powerFun,xmin,xmax,3);
673 fE->SetParameters(pE);
678 fE->GetParameters(pE);
679 pG[0] = pr->GetBinContent(bMax)*sqrt(6.28)*0.03;
685 fG = (TF1*)(gROOT->FindObject(
"fG"));
687 fG =
new TF1(
"fG",&
power_g,xmin,xmax,6);
688 fG->SetParameters(pG);
689 fG->SetParLimits(3,pE[0],pE[0]);
690 fG->SetParLimits(4,pE[1],pE[1]);
691 fG->SetParLimits(5,pE[2],pE[2]);
699 fG->GetParameters(pG);
700 maxE = xPeak(fG,pG[1]-0.1,pG[1]+0.1);
703 return ((pG[1]<xmin+0.05||pG[1]>xmax-0.05)?
false :
true);
712 int bMax = pr->GetMaximumBin();
715 TH1D *
p=(TH1D*)(pr->Clone());
716 float xmean = p->GetMean();
718 int b1 = x->FindBin(xmean-0.07);
719 int b2 = x->FindBin(xmean+0.07);
720 for (
int bin = b1; bin<=b2; bin++){
721 p->SetBinError(bin,p->GetBinContent(bin));
723 double xmin = x->GetXmin();
726 double xmax = x->GetXmax();
727 pE[0] = pr->Integral()*x->GetBinWidth(0)/(log(xmax)-log(xmin));;
730 fE = (TF1*)(gROOT->GetListOfFunctions()->FindObject(
"fE"));
733 fE =
new TF1(
"fE",&
powerFun,xmin,xmax,3);
734 fE->SetParameters(pE);
739 fE->GetParameters(pE);
740 pG[0] = pr->GetBinContent(bMax)*sqrt(6.28)*0.03;
746 fG = (TF1*)(gROOT->FindObject(
"fG"));
748 fG =
new TF1(
"fG",&
power_g,xmin,xmax,6);
749 fG->SetParameters(pG);
750 fG->SetParLimits(3,pE[0],pE[0]);
751 fG->SetParLimits(4,pE[1],pE[1]);
752 fG->SetParLimits(5,pE[2],pE[2]);
760 fG->GetParameters(pG);
761 maxE = xPeak(fG,pG[1]-0.1,pG[1]+0.1);
764 return ((pG[1]<xmin+0.05||pG[1]>xmax-0.05)?
false :
true);