22 #include <TDirectory.h>
26 #include <TGraphErrors.h>
114 rthist.resize (nbinsr*nbinst+200);
121 npoints = binvar==0 ? rtHist->GetNbinsX() : rtHist->GetNbinsY() ;
132 TH1D** hslizes =
new TH1D*[
npoints];
150 TDirectory* binhistdir =
dir->mkdir(
"binhist");
152 std::cout <<
" Calibrator::RtGraph: " <<
" Directory " << std::string(binhistdir->GetPath()) <<
" binlabel " << std::string(binlabel) << std::endl;
155 std::cout <<
" Calibrator::RtGraph: " <<
" number of points " <<
npoints <<
" binvar " << binvar << std::endl;
159 m_chname = std::string(Form(
"%s slize_%i",binlabel,
i));
160 m_chtit = binvar==0 ? std::string(Form(
"bin %i : %4.2f < %s < %4.2f [ns]",
i,rtHist->GetXaxis()->GetBinLowEdge(
i+1),binlabel,rtHist->GetXaxis()->GetBinUpEdge(
i+1))) :
161 std::string(Form(
"bin %i : %4.2f < %s < %4.2f [mm]",
i,rtHist->GetYaxis()->GetBinLowEdge(
i+1),binlabel,rtHist->GetYaxis()->GetBinUpEdge(
i+1))) ;
162 hslizes[
i] = binvar==0 ? rtHist->ProjectionY(
m_chname.data(),
i+1,
i+1) :
164 hslizes[
i]->SetTitle(
m_chtit.data());
166 hslizes[
i]->SetDirectory(
nullptr);
170 for (
int j=1;j<=hslizes[
i]->GetNbinsX();j++) {
171 float val=hslizes[
i]->GetBinContent(j);
195 int rtEntries = rtHist->GetEntries();
196 if(rtEntries==0) rtEntries=1;
200 float frmin=0,frmax=0;
205 if (
m_btype[
i]==
LOW) {frmin = hslizes[
i]->GetBinCenter(1); frmax = hslizes[
i]->GetBinCenter(
m_maxbin[
i]+4);}
209 std::unique_ptr<TF1>
ff(
new TF1(
"dtfitfunc",
"gaus"));
211 ff->SetRange(frmin,frmax);
213 m_t = binvar==0 ? rtHist->GetXaxis()->GetBinCenter(
i+1) : rtHist->GetYaxis()->GetBinCenter(
i+1);
214 m_et = binvar==0 ? rtHist->GetXaxis()->GetBinWidth(1)/std::sqrt(12.0) : rtHist->GetYaxis()->GetBinWidth(1)/std::sqrt(12.0);
235 m_d =
ff->GetParameter(1);
236 m_ed =
ff->GetParError(1);
268 rtgr->SetTitle((std::string(
"binning in ") + binlabel).
data());
270 trgr->SetTitle((std::string(
"binning in ") + binlabel).
data());
272 rtgr->SetMarkerStyle(1) ;
273 rtgr->SetMarkerColor(2) ;
274 rtgr->SetLineColor(2) ;
275 rtgr->SetName(
"rtgraph") ;
276 trgr->SetMarkerStyle(1) ;
277 trgr->SetMarkerColor(2) ;
278 trgr->SetLineColor(2) ;
279 trgr->SetName(
"trgraph") ;
317 double rmin0 =
par[0];
320 double t_const =
par[3];
322 double t = t_const + 2*
rho/
v*std::asin(std::sqrt(rmin0*rmin0*(1-0.25*
r*
r)+
r*
r)/(2*
rho));
327 double rmin0 =
par[0];
330 double t_const =
par[3];
332 double r_squared = (4*
rho*
rho*
std::sin(
v*(
t-t_const)/(2*
rho))-rmin0*rmin0)/(1-0.25*rmin0*rmin0);
333 double r=r_squared>0 ? std::sqrt(r_squared) : 0.0;
352 useshortstraws(true),
355 m_rtbinning (
"None"),
384 Calibrator::Calibrator(
int lev,
const std::string& nme,
int mint0,
int minrt,
const std::string& rtr,
const std::string& rtb,
float t0sft)
397 useshortstraws(true),
423 m_isdines ( rtr.
find(
"dines")!=std::string::npos)
434 if (
max-
min == 0 )
return -1;
440 if ( (valuex<minx) || (valuex>maxx) || (valuey<miny) || (valuey>maxy) )
return -1;
441 if(maxx-minx == 0 || maxy-miny==0)
return -1;
442 int binnox=(
int)(nbinsx*((valuex-minx)/(maxx-minx)));
443 int binnoy=(
int)(nbinsy*((valuey-miny)/(maxy-miny)));
444 return binnoy*nbinsx+binnox;
448 if((
int)
n==0)
return oldmean;
449 return oldmean*((
n-1)/
n)+newvalue/
n;
468 std::string yn[2]={
"NO",
"YES"};
469 std::string
info = std::string(Form(
"CONFIGURATION %-16s: dort=%-3s, dot0=%-3s, dores=%-3s, selection=",
m_name.data(),yn[
dort].data(),yn[
dot0].data(),yn[
dores].data()));
471 if (isel==-3)
info += std::string(
"all");
472 else if (isel==-4)
info += std::string(
"none");
473 else info += std::string(Form(
"%2i,",isel));
486 std::string optstr=
"";
487 if (
dort) optstr +=
'R';
488 if (
dot0) optstr +=
'T';
507 std::string selstr=
"";
510 else for (
int isel :
selection) selstr += std::string(Form(
"%i",isel));
518 double mint=
data[
key].rtgraph->tval[0];
520 std::string brt =
"";
521 brt += Form(
"%f %f %i ",mint,maxt,
nbins);
524 brt += Form(
"%f ",
data[
key].rtgraph->rval[
ip]);
532 std::ifstream oldconstfile(
"calib_constants_in.txt");
536 if (oldconstfile.is_open())
538 while (!oldconstfile.eof() )
540 oldconstfile >>
key >>
t0;
542 std::cout <<
"UPDATED OLD T0: " <<
key <<
" " <<
data[
key].oldt02 <<
" -> " <<
t0 << std::endl;
546 oldconstfile.close();
550 std::cout <<
"NO OLD T0S FILE FOUND. USING AVERAGE VALUES!" << std::endl;
560 RtGraph* rtg = m_rtbinning.find(
't')==std::string::npos ?
new RtGraph(rtHist,1,std::string(
"abs(rtrack)").
data(),!bequiet,
dir) :
new RtGraph(rtHist,0,std::string(
"t-t0").
data(),!bequiet,
dir);
562 TF1 dtfitfunc(
"dtfitfunc",
"gaus(0)");
564 gStyle->SetOptFit(1111);
565 gStyle->SetPalette(1);
568 TF1 *trfunc,*rtfunc,*oldrtfunc;
573 trfunc->SetParameters(0.3, 1.0, 0.05, -3.);
574 double rmin0Limits[2]={0.0,2.0};
575 double rhoLimits[2]={0.0,10.};
576 double vLimits[2]={0.0,1.0};
577 trfunc->SetParLimits(0,rmin0Limits[0],rmin0Limits[1]);
578 trfunc->SetParLimits(1,rhoLimits[0],rhoLimits[1]);
579 trfunc->SetParLimits(2,vLimits[0],vLimits[1]);
580 rtg->
trgr->SetTitle(
"Dines' R-t sqrt(..)");
583 trfunc =
new TF1(
"trfunc",
pol3deg,0,3,4);
586 trfunc->SetParameters(0.0, 0.0, 0.0, 0.0);
587 rtg->
trgr->SetTitle(
"Polynomial R-t");
591 trfunc->SetRange(0,2);
592 if (
opt.find(
'3')==std::string::npos) trfunc->FixParameter(3,0);
593 trfunc->SetLineColor(4) ;
594 rtg->
trgr->Fit(trfunc,
"QR");
595 if (!bequiet) rtg->
trgr->Write();
599 rtfunc->SetRange(0,45);
600 if (
opt.find(
'3')==std::string::npos) rtfunc->FixParameter(3,0);
601 rtfunc->SetLineColor(4) ;
603 rtfunc->SetParameters(trfunc->GetParameter(0),trfunc->GetParameter(1),trfunc->GetParameter(2),trfunc->GetParameter(3));
607 rtfunc->SetParameters(0.000000
e+00, 6.269950
e-02, -3.370054
e-04, -1.244642
e-07);
608 if(rtg->
rtgr !=
nullptr) {
609 rtg->
rtgr->Fit(rtfunc,
"QR");
610 for (
int ipnt=0; ipnt<rtg->
rtgr->GetN(); ipnt++){
611 float deriv = rtfunc->Derivative(rtg->
rtgr->GetX()[ipnt]);
612 if(fabs(deriv)<1.0
e-24) deriv=0.05;
613 rtg->
rtgr->SetPointError(ipnt , rtg->
rtgr->GetEY()[ipnt]/deriv , rtg->
rtgr->GetEY()[ipnt]);
615 rtg->
rtgr->Fit(rtfunc,
"R");
619 if (!bequiet and (rtg->
rtgr !=
nullptr)) rtg->
rtgr->Write();
622 oldrtfunc->SetRange(0,45);
623 oldrtfunc->SetLineColor(1) ;
624 oldrtfunc->SetLineStyle(2) ;
625 oldrtfunc->SetLineWidth(1) ;
628 if (!bequiet) oldrtfunc->Write();
630 rtpars[0] = rtfunc->GetParameter(0);
631 rtpars[1] = rtfunc->GetParameter(1);
632 rtpars[2] = rtfunc->GetParameter(2);
633 rtpars[3] = rtfunc->GetParameter(3);
641 float precision = 0.0001;
644 float driftradius = rtpars[0]+tdrift*(rtpars[1]+tdrift*(rtpars[2]));
645 float residual = std::abs(rdrift) - driftradius;
646 while (std::abs(
residual) > precision) {
648 drdt = rtpars[1]+tdrift*(2*rtpars[2]);
649 if(fabs(drdt)<1.0e-24) drdt=0.05;
652 driftradius = rtpars[0]+tdrift*(rtpars[1]+tdrift*(rtpars[2]));
656 if (ntries>maxtries){
662 if (
opt.find(
'0')==std::string::npos) {
664 data[
key].rtpar[0] = rtpars[0];
665 data[
key].rtpar[1] = rtpars[1];
666 data[
key].rtpar[2] = rtpars[2];
670 data[
key].rtpar[1] = rtpars[1] + 2*tdrift*rtpars[2] + 3*tdrift*tdrift*rtpars[3];
671 data[
key].rtpar[2] = rtpars[2] + 3*tdrift*rtpars[3];
672 data[
key].rtpar[3] = rtpars[3];
676 data[
key].rtpar[0] = rtpars[0];
677 data[
key].rtpar[1] = rtpars[1];
678 data[
key].rtpar[2] = rtpars[2];
679 data[
key].rtpar[3] = rtpars[3];
694 float mean = tresHist->GetMean();
695 float rms = tresHist->GetRMS();
698 for (
int j=1; j<=tresHist->GetNbinsX()+1;j++){
699 val=tresHist->GetBinContent(j);
708 float fitmean=tresHist->GetBinCenter(maxbin);
709 if (std::abs(fitmean)>5.0){
717 if (fitrms>5.0) fitrms=5.0;
720 if (tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2) < m_mintres ||
721 tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2) > m_maxtres) {
728 tresHist->Fit(
"gaus",
"QR",
"",tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2),tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2));
729 if (tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2) < m_mintres ||
730 tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2) > m_maxtres) {
731 data[
key].tres = tresHist->GetFunction(
"gaus")->GetParameter(2);
732 data[
key].tresMean = tresHist->GetFunction(
"gaus")->GetParameter(1);
733 data[
key].t0err = tresHist->GetFunction(
"gaus")->GetParError(1);
735 return tresHist->GetFunction(
"gaus")->GetParameter(1);
738 tresHist->Fit(
"gaus",
"QR",
"",tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2),tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2));
739 if (tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2) < m_mintres ||
740 tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2) > m_maxtres) {
741 data[
key].tres = tresHist->GetFunction(
"gaus")->GetParameter(2);
742 data[
key].tresMean = tresHist->GetFunction(
"gaus")->GetParameter(1);
743 data[
key].t0err = tresHist->GetFunction(
"gaus")->GetParError(1);
745 return tresHist->GetFunction(
"gaus")->GetParameter(1);
748 tresHist->Fit(
"gaus",
"QR",
"",tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2),tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2));
749 if (tresHist->GetFunction(
"gaus")->GetParameter(1) - tresHist->GetFunction(
"gaus")->GetParameter(2) < m_mintres ||
750 tresHist->GetFunction(
"gaus")->GetParameter(1) + tresHist->GetFunction(
"gaus")->GetParameter(2) > m_maxtres) {
757 data[
key].tres = tresHist->GetFunction(
"gaus")->GetParameter(2);
758 data[
key].tresMean = tresHist->GetFunction(
"gaus")->GetParameter(1);
759 data[
key].t0err = tresHist->GetFunction(
"gaus")->GetParError(1);
763 if ( std::abs(tresHist->GetFunction(
"gaus")->GetParameter(2))>10 || std::abs(tresHist->GetFunction(
"gaus")->GetParameter(1))>10 ) {
765 data[
key].tres = tresHist->GetRMS();
766 data[
key].tresMean = tresHist->GetMean();
767 data[
key].t0err = tresHist->GetRMS();
769 return tresHist->GetMean();
773 gStyle->SetOptFit(1111);
775 return tresHist->GetFunction(
"gaus")->GetParameter(1);
781 float mean = resHist->GetMean();
783 resHist->Fit(
"gaus",
"QRI",
"",
mean-0.3,
mean+0.3);
784 resHist->Fit(
"gaus",
"QRI",
"",resHist->GetFunction(
"gaus")->GetParameter(1) - 1.5*resHist->GetFunction(
"gaus")->GetParameter(2),resHist->GetFunction(
"gaus")->GetParameter(1) + 1.5*resHist->GetFunction(
"gaus")->GetParameter(2));
785 resHist->Fit(
"gaus",
"QRI",
"",resHist->GetFunction(
"gaus")->GetParameter(1) - 1.5*resHist->GetFunction(
"gaus")->GetParameter(2),resHist->GetFunction(
"gaus")->GetParameter(1) + 1.5*resHist->GetFunction(
"gaus")->GetParameter(2));
786 resHist->Fit(
"gaus",
"QRI",
"",resHist->GetFunction(
"gaus")->GetParameter(1) - 1.5*resHist->GetFunction(
"gaus")->GetParameter(2),resHist->GetFunction(
"gaus")->GetParameter(1) + 1.5*resHist->GetFunction(
"gaus")->GetParameter(2));
788 gStyle->SetOptFit(1111);
790 data[
key].res=resHist->GetFunction(
"gaus")->GetParameter(2);
791 data[
key].resMean=resHist->GetFunction(
"gaus")->GetParameter(1);
792 data[
key].reserr=resHist->GetFunction(
"gaus")->GetParError(2);
794 return resHist->GetFunction(
"gaus")->GetParameter(2);
801 bool calrt=
opt.find(
'R')!=std::string::npos;
802 bool calt0=
opt.find(
'T')!=std::string::npos;
803 bool donothing=
opt.find(
'N')!=std::string::npos;
804 bool prnt=
opt.find(
'P')!=std::string::npos;
805 bool useref=
opt.find(
'B')!=std::string::npos;
806 bool useshortstw=
opt.find(
'S')!=std::string::npos;
808 if (donothing)
return dir;
812 bool enough_t0 =
data[
key].ntres>=m_mint0stat;
813 bool enough_rt =
data[
key].nrt>=m_minrtstat;
815 if ((enough_rt && calrt) || (enough_t0 && calt0)) {
816 m_hdirs[
key] =
dir->mkdir(Form(
"%s%s",m_name.data(),
key.data()));
819 std::cout <<
" Calibrator::Calibrate Parent Directory: " << std::string(
dir->GetPath()) << std::endl;
820 std::cout <<
" Calibrator::Calibrate key " <<
key <<
" name " << m_name <<
" entries for rt " <<
data[
key].nrt <<
" min req " << m_minrtstat << std::endl;
821 std::cout <<
" Calibrator::Calibrate key " <<
key <<
" entries for t0 " <<
data[
key].ntres <<
" min req " << m_mint0stat << std::endl;
822 std::cout <<
" Calibrator::Calibrate Directory: " << std::string(m_hdirs[
key]->GetPath()) << std::endl;
828 if ((enough_rt && calrt) || (enough_t0 && calt0)) {
830 m_resHists[
key] =
new TH1F(
"residual",
"residual",m_nbinsres,m_minres,m_maxres);
832 for (
int i=0;
i<100;
i++) {
833 m_resHists[
key]->SetBinContent(
i+1,
data[
key].reshist[
i]);
836 m_resHists[
key]->SetEntries((
int)
data[
key].nres);
837 if (bequiet) m_resHists[
key]->SetDirectory(
nullptr);
839 std::cout <<
" No bin content " << std::endl;
841 FitResidual(
key,m_resHists[
key]);
847 if (prnt) printf(
"Calibrator::calibrate %8s %-14s: \n",m_name.data(),
key.data());
861 if ((calrt && enough_rt) ||
level==0) {
863 m_rtHists[
key] =
new TH2F(
"rt-relation",
"rt-relation",m_nbinst,m_mint,m_maxt,m_nbinsr,m_minr,m_maxr);
864 for (
int i=0;
i<m_nbinst;
i++) {
865 for (
int j=0;j<m_nbinsr;j++) {
866 m_rtHists[
key]->SetBinContent(
i+1,j+1,
data[
key].rthist[
i+m_nbinst*j]);
870 if(
level<2) std::cout <<
" Calibrator::calibrate do rt fit for key " <<
key <<
" with number of entries " << m_rtHists[
key]->GetEntries() << std::endl;
872 m_rtHists[
key]->SetDirectory(
nullptr);
873 if(
level<2) std::cout <<
" remove this level from calout.root file " << std::endl;
900 if (useref &&
level==5){
912 if ((calt0 && enough_t0) ||
level==0) {
914 m_tresHists[
key] =
new TH1F(
"timeresidual",
"time residual",m_nbinstres,m_mintres,m_maxtres);
916 for (
int i=0;
i<100;
i++) {
917 m_tresHists[
key]->SetBinContent(
i+1,
data[
key].m_treshist[
i]);
919 m_tresHists[
key]->SetEntries(
data[
key].ntres);
920 if(
level<2) std::cout <<
" Calibrator::calibrate do t0 fit for key " <<
key <<
" Numb. entries " << m_rtHists[
key]->GetEntries() << std::endl;
922 if(
level<2) std::cout <<
" Calibrator::calibrate: remove this level from calibout.root " << std::endl;
923 m_tresHists[
key]->SetDirectory(
nullptr);
947 if (prnt) printf(
"T0 /\\ %7i %05.2f%+05.2f%+05.2f=%05.2f \n",
data[
key].ntres, caldata_above->
t0, caldata_above->
oldt02,
data[
key].oldt02 ,
data[
key].t0);
953 if (prnt && !bequiet) std::cout <<
" H";
955 if (prnt) std::cout << std::endl;
978 if (
hist ==
nullptr){
979 std::cout <<
"OUT OF MEMORY!" << std::endl;
990 if (makehist)
hist->rthist[
i]=0;
992 if (makehist)
hist->m_treshist[
i]=0;
993 if (makehist)
hist->reshist[
i]=0;
997 if (binhist==
nullptr){
1000 if (makehist)
hist->m_treshist[tresbin]=
d.weight;
1005 if (makehist)
hist->reshist[resbin]=1;
1009 if (makehist)
hist->rthist[rtbin]=1;
1018 if(
level<2) std::cout <<
" Calibrator::AddHit called for key " <<
key <<
" add histogram with "<< npop <<
" non-zero bins " << std::endl;
1020 for(
int ipop=2;ipop<2*npop+2;ipop=ipop+2) {
1023 value=binhist[ipop+1];
1032 else if (ibin>=100 && ibin<200) {
1072 hist->oldrtpar[0]=
d.rtpar[0];
1073 hist->oldrtpar[1]=
d.rtpar[1];
1074 hist->oldrtpar[2]=
d.rtpar[2];
1075 hist->oldrtpar[3]=
d.rtpar[3];
1083 hist->calflag=
false;
1087 for (
unsigned int i =0;
i < 4;
i++){
1088 hist->rtpar[
i]=-10.0;
1099 if(
level<2) std::cout <<
" First hit for det " <<
data[
key].det <<
" lay " <<
data[
key].lay <<
" t0 " <<
data[
key].t0 <<
" upd t0 " <<
data[
key].oldt02 <<
" nhits: " <<
data[
key].nhits << std::endl;
1108 if (binhist==
nullptr){
1111 if (makehist)
data[
key].m_treshist[tresbin]=
data[
key].m_treshist[tresbin]+
d.weight;
1116 if (makehist)
data[
key].reshist[resbin]++;
1120 if (makehist)
data[
key].rthist[rtbin]++;
1132 for(
int ipop=2;ipop<2*npop+2;ipop=ipop+2) {
1135 value=binhist[ipop+1];
1144 else if (ibin>=100 && ibin<200) {
1182 TNtuple* stattup =
new TNtuple(Form(
"%stuple",
m_name.data()),
"statistics",
"det:lay:mod:brd:chp:sid:stl:stw:rtflag:t0flag:t0:oldt0:rt0:dt0:t0offset:ftype:nhits:nt0:nrt:res:resMean:dres:tres:tresMean:x:y:z");
1184 if ((ihist->second).calflag) {
1186 float const ntvar[27]={
1187 float((ihist->second).det),
1188 float((ihist->second).lay),
1189 float((ihist->second).mod),
1190 float((ihist->second).brd),
1191 float((ihist->second).chp),
1192 float((ihist->second).sid),
1193 float((ihist->second).stl),
1194 float((ihist->second).stw),
1195 float((
int)(ihist->second).rtflag),
1196 float((
int)(ihist->second).t0flag),
1198 float((ihist->second).oldt02),
1200 (ihist->second).reft0,
1201 (ihist->second).t0err,
1202 (ihist->second).t0off,
1203 float((ihist->second).t0fittype),
1204 (ihist->second).nhits,
1205 (
float)(ihist->second).ntres,
1206 (
float)(ihist->second).nrt,
1207 (
float)(ihist->second).
res,
1208 (
float)(ihist->second).resMean,
1209 (ihist->second).reserr,
1210 (ihist->second).
tres,
1211 (ihist->second).tresMean,
1216 stattup->Fill(ntvar);
1229 for (
const std::pair<const std::string, caldata>&
p :
data) {
1230 dumpfile <<
p.first <<
" " <<
p.second.t0 <<
" " << std::endl;