14 #include "Minuit2/VariableMetricMinimizer.h" 15 #include "Minuit2/FunctionMinimum.h" 16 #include "Minuit2/MnPrint.h" 17 #include "Minuit2/MnMigrad.h" 18 #include "Minuit2/MnUserParameterState.h" 33 #include "TMatrixDSym.h" 34 #include "TDecompBK.h" 37 #include "TMinuitMinimizer.h" 48 TMinuitMinimizer::UseStaticMinuit(
false);
55 TMinuitMinimizer::UseStaticMinuit(
false);
57 ffit_type =
"default";
58 ffit_variable =
"default";
65 fpar_name[1] =
"SigmaZ0 ";
68 fpar_name[4] =
"dxdz ";
69 fpar_name[5] =
"dydz ";
70 fpar_name[6] =
"SigmaBeam ";
79 theFitter =
new VariableMetricMinimizer();
84 fapplychi2cut =
false;
94 h1z =
new TH1F(
"h1z",
"z distribution",200,-fMaxZ, fMaxZ);
110 return this->
Fit(
nullptr);
117 if ( ffit_variable ==
"z" ) {
119 if ( ffit_type ==
"chi2" ) {
121 return Fit_z_chi2(inipar);
123 }
else if ( ffit_type ==
"likelihood" ) {
125 return Fit_z_likelihood(inipar);
127 }
else if ( ffit_type ==
"combined" ) {
130 double tmp_par[2] = {tmp_beamspot.
z0(), tmp_beamspot.
sigmaZ()};
131 return Fit_z_likelihood(tmp_par);
136 <<
"Error in BeamSpotProducer/BSFitter: " 137 <<
"Illegal fit type, options are chi2,likelihood or combined(ie. first chi2 then likelihood)";
140 }
else if ( ffit_variable ==
"d" ) {
142 if ( ffit_type ==
"d0phi" ) {
146 }
else if ( ffit_type ==
"likelihood" ) {
148 return Fit_d_likelihood(inipar);
150 }
else if ( ffit_type ==
"combined" ) {
154 double tmp_par[4] = {tmp_beamspot.
x0(), tmp_beamspot.
y0(), tmp_beamspot.
dxdz(), tmp_beamspot.
dydz()};
155 return Fit_d_likelihood(tmp_par);
159 <<
"Error in BeamSpotProducer/BSFitter: " 160 <<
"Illegal fit type, options are d0phi, likelihood or combined";
162 }
else if ( ffit_variable ==
"d*z" || ffit_variable ==
"default" ) {
164 if ( ffit_type ==
"likelihood" || ffit_type ==
"default" ) {
179 this->Setd0Cut_d0phi(4.0);
211 if (ffit_type ==
"likelihood") {
212 double tmp_par[7] = {tmp_d0phi.
x0(), tmp_d0phi.
y0(), tmp_d0phi.
z0(),
215 double tmp_error_par[7];
217 tmp_error_par[6]=0.0;
219 reco::BeamSpot tmp_lh = Fit_d_z_likelihood(tmp_par,tmp_error_par);
222 edm::LogWarning(
"BSFitter") <<
"BSFitter: Result is non physical. Log-Likelihood fit to extract beam width did not converge." << std::endl;
230 edm::LogInfo(
"BSFitter") <<
"default track-based fit does not extract beam width." << std::endl;
235 }
else if ( ffit_type ==
"resolution" ) {
241 double tmp_par[7] = {tmp_d0phi.
x0(), tmp_d0phi.
y0(), tmp_z.
z0(),
243 double tmp_error_par[7];
244 for(
int s=0;
s<6;
s++){ tmp_error_par[
s] =
pow(tmp_par[
s],0.5);}
245 tmp_error_par[6]=0.0;
247 reco::BeamSpot tmp_beam = Fit_d_z_likelihood(tmp_par,tmp_error_par);
249 double tmp_par2[7] = {tmp_beam.
x0(), tmp_beam.
y0(), tmp_beam.
z0(),
257 edm::LogWarning(
"BSFitter") <<
"Result is non physical. Log-Likelihood fit did not converge." << std::endl;
266 <<
"Error in BeamSpotProducer/BSFitter: " 267 <<
"Illegal fit type, options are likelihood or resolution";
272 <<
"Error in BeamSpotProducer/BSFitter: " 273 <<
"Illegal variable type, options are \"z\", \"d\", or \"d*z\"";
286 std::vector<double> par(2,0);
287 std::vector<double> err(2,0);
291 err.push_back(0.0001);
292 err.push_back(0.0001);
296 thePDF->SetPDFs(
"PDFGauss_z");
297 thePDF->SetData(fBSvector);
301 MnUserParameters upar;
302 upar.Add(
"X0", 0.,0.);
303 upar.Add(
"Y0", 0.,0.);
304 upar.Add(
"Z0", inipar[0],0.001);
305 upar.Add(
"sigmaZ",inipar[1],0.001);
307 MnMigrad migrad(*thePDF, upar);
309 FunctionMinimum fmin = migrad();
310 ff_minimum = fmin.Fval();
331 for (
int j = 2 ; j < 4 ; ++j) {
332 for(
int k = j ;
k < 4 ; ++
k) {
333 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
339 fmin.Parameters().Vec()(2)),
340 fmin.Parameters().Vec()(3),
359 h1z =
new TH1F(
"h1z",
"z distribution",200,-fMaxZ, fMaxZ);
361 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
365 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam) {
367 h1z->Fill( iparam->z0() );
373 TF1 fgaus(
"fgaus",
"gaus",0.,1.,TF1::EAddToList::kNo);
374 h1z->Fit(&fgaus,
"QLMN0");
378 double fpar[2] = {fgaus.GetParameter(1), fgaus.GetParameter(2) };
382 matrix(2,2) = fgaus.GetParError(1) * fgaus.GetParError(1);
383 matrix(3,3) = fgaus.GetParError(2) * fgaus.GetParError(2);
404 edm::LogInfo(
"BSFitter") <<
"number of total input tracks: " << fBSvector.size() << std::endl;
408 if ( (
int)fBSvector.size() <= fminNtrks ) {
409 edm::LogWarning(
"BSFitter") <<
"need at least " << fminNtrks <<
" tracks to run beamline fitter." << std::endl;
415 theanswer = Fit_d0phi();
416 if ( goodfit ) fnthite++;
422 ftmprow > fconvergence * fBSvector.size() &&
423 ftmprow > fminNtrks ) {
425 theanswer = Fit_d0phi();
429 ftmprow > fconvergence * fBSvector.size() &&
430 ftmprow > fminNtrks ) {
431 preanswer = theanswer;
438 theanswer = preanswer;
442 edm::LogInfo(
"BSFitter") <<
"Total number of successful iterations = " << ( goodfit ? (fnthite+1) : fnthite ) << std::endl;
460 if (fnthite > 0)
edm::LogInfo(
"BSFitter") <<
" number of tracks used: " << ftmprow << std::endl;
470 TMatrixD x_result(4,1);
471 TMatrixDSym V_result(4);
484 std::vector<BSTrkParameters>::iterator iparam = fBSvector.begin();
492 for( iparam = fBSvector.begin() ;
493 iparam != fBSvector.end() ; ++iparam) {
503 g(0,0) =
sin(iparam->phi0());
504 g(1,0) = -
cos(iparam->phi0());
505 g(2,0) = iparam->z0() *
g(0,0);
506 g(3,0) = iparam->z0() *
g(1,0);
510 double sigmabeam2 = 0.006 * 0.006;
511 if (finputBeamWidth > 0 ) sigmabeam2 = finputBeamWidth * finputBeamWidth;
518 double sigma2 = sigmabeam2 + (iparam->sigd0())* (iparam->sigd0());
520 TMatrixD ftmptrans(1,4);
521 ftmptrans = ftmptrans.Transpose(ftmp);
522 TMatrixD dcor = ftmptrans *
g;
523 double chi2tmp = (iparam->d0() - dcor(0,0)) * (iparam->d0() - dcor(0,0))/sigma2;
524 (*iparam) =
BSTrkParameters(iparam->z0(),iparam->sigz0(),iparam->d0(),iparam->sigd0(),
525 iparam->phi0(), iparam->pt(),dcor(0,0),chi2tmp);
528 if (fapplyd0cut && fnthite>0 ) {
529 if (
std::abs(iparam->d0() - dcor(0,0)) > fd0cut ) pass =
false;
532 if (fapplychi2cut && fnthite>0 ) {
533 if ( chi2tmp > fchi2cut ) pass =
false;
539 for(
int j = 0 ; j < 4 ; ++j) {
540 for(
int k = j ;
k < 4 ; ++
k) {
546 Vint += (temp * (1 / sigma2));
547 b += (iparam->d0() / sigma2 *
g);
550 h1z->Fill( iparam->z0() );
555 Double_t determinant;
558 if (!bk.Decompose()) {
561 <<
"Decomposition failed, matrix singular ?" << std::endl
562 <<
"condition number = " << bk.Condition() << std::endl;
565 V_result = Vint.InvertFast(&determinant);
566 x_result = V_result *
b;
581 TF1 fgaus(
"fgaus",
"gaus",0.,1.,TF1::EAddToList::kNo);
584 auto status = h1z->Fit(&fgaus,
"QLN0",
"",h1z->GetMean() -2.*h1z->GetRMS(),h1z->GetMean() +2.*h1z->GetRMS());
594 double fpar[2] = {fgaus.GetParameter(1), fgaus.GetParameter(2) };
598 for (
int j = 0 ; j < 2 ; ++j) {
599 for(
int k = j ;
k < 2 ; ++
k) {
604 for (
int j = 4 ; j < 6 ; ++j) {
605 for(
int k = j ;
k < 6 ; ++
k) {
611 matrix(2,2) = fgaus.GetParError(1) * fgaus.GetParError(1);
612 matrix(3,3) = fgaus.GetParError(2) * fgaus.GetParError(2);
620 double x0tmp = x_result(0,0);
621 double y0tmp = x_result(1,0);
623 x0tmp += x_result(2,0)*fpar[0];
624 y0tmp += x_result(3,0)*fpar[0];
652 fapplychi2cut =
true;
662 thePDF->SetPDFs(
"PDFGauss_d");
663 thePDF->SetData(fBSvector);
665 MnUserParameters upar;
666 upar.Add(
"X0", inipar[0],0.001);
667 upar.Add(
"Y0", inipar[1],0.001);
668 upar.Add(
"Z0", 0.,0.001);
669 upar.Add(
"sigmaZ",0.,0.001);
670 upar.Add(
"dxdz",inipar[2],0.001);
671 upar.Add(
"dydz",inipar[3],0.001);
674 MnMigrad migrad(*thePDF, upar);
676 FunctionMinimum fmin = migrad();
677 ff_minimum = fmin.Fval();
680 for (
int j = 0 ; j < 6 ; ++j) {
681 for(
int k = j ;
k < 6 ; ++
k) {
682 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
687 fmin.Parameters().Vec()(1),
690 fmin.Parameters().Vec()(4),
691 fmin.Parameters().Vec()(5),
699 if(option==1)init_pars[6]=0.0005;
710 double last_minvalue=1.0e+10;
711 double init_bw=-99.99;
715 double DeltadCut=0.1000;
716 if(init_pars[6]<0.0200){DeltadCut=0.0900; }
717 if(init_pars[6]<0.0100){DeltadCut=0.0700;}
720 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
723 if(option==1)iters=500;
724 if(option==2)iters=1;
726 for(
int p=0;
p<iters;
p++){
728 if(iters==500)init_pars[6]+=0.0002;
731 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam)
734 d_sig =
sqrt(init_pars[6]*init_pars[6] + (iparam->sigd0())*(iparam->sigd0()));
735 d_dprime = iparam->d0() - ( ( (init_pars[0] + iparam->z0()*(init_pars[4]))*
sin(iparam->phi0()) )
736 - ( (init_pars[1] + iparam->z0()*(init_pars[5]))*
cos(iparam->phi0()) ) );
739 if(
std::abs(d_dprime)<DeltadCut && option==2){ fBSvectorBW.push_back(*iparam);}
741 d_result = (
exp(-(d_dprime*d_dprime)/(2.0*d_sig*d_sig)))/(d_sig*fsqrt2pi);
742 z_sig =
sqrt(iparam->sigz0() * iparam->sigz0() + init_pars[3]*init_pars[3]);
743 z_result = (
exp(-((iparam->z0() - init_pars[2])*(iparam->z0() - init_pars[2]))/(2.0*z_sig*z_sig)))/(z_sig*fsqrt2pi);
744 tot_pdf=z_result*d_result;
750 if(d_result < 1.0
e-05){ tot_pdf=z_result*1.0e-05;
754 function =
function +
log(tot_pdf);
760 function= -2.0*
function;
761 if(
function<last_minvalue){init_bw=init_pars[6];
762 last_minvalue=
function; }
767 init_bw=init_bw+(0.20*init_bw);
774 <<
"scanPDF:====>>>> WARNING***: The initial guess value of Beam width is negative!!!!!!"<<std::endl
775 <<
"scanPDF:====>>>> Assigning beam width a starting value of "<<init_bw<<
" cm"<<std::endl;
792 inipar[6]=scanPDF(inipar,tracksFailed,1);
793 error_par[6]=(inipar[6])*0.20;
798 scanPDF(inipar,tracksFailed,2);
803 std::vector<BSTrkParameters>::const_iterator iparamBW = fBSvectorBW.begin();
804 for( iparamBW = fBSvectorBW.begin(); iparamBW != fBSvectorBW.end(); ++iparamBW)
805 { fBSvector.push_back(*iparamBW);
809 thePDF->SetPDFs(
"PDFGauss_d*PDFGauss_z");
810 thePDF->SetData(fBSvector);
811 MnUserParameters upar;
813 upar.Add(
"X0", inipar[0],error_par[0]);
814 upar.Add(
"Y0", inipar[1],error_par[1]);
815 upar.Add(
"Z0", inipar[2],error_par[2]);
816 upar.Add(
"sigmaZ",inipar[3],error_par[3]);
817 upar.Add(
"dxdz",inipar[4],error_par[4]);
818 upar.Add(
"dydz",inipar[5],error_par[5]);
819 upar.Add(
"BeamWidthX",inipar[6],error_par[6]);
822 MnMigrad migrad(*thePDF, upar);
824 FunctionMinimum fmin = migrad();
829 ff_minimum = fmin.Fval();
832 bool ff_nfcn=fmin.HasReachedCallLimit();
833 bool ff_cov=fmin.HasCovariance();
834 bool testing=fmin.IsValid();
840 edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: MINUIT DID NOT CONVERGES PROPERLY !!!!!!"<<std::endl;
841 if(ff_nfcn)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: No. of Calls Exhausted"<<std::endl;
842 if(!ff_cov)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: Covariance did not found"<<std::endl;
845 edm::LogInfo(
"BSFitter") <<
"The Total # Tracks used for beam width fit = "<<(fBSvectorBW.size())<<std::endl;
849 double lastIter_pars[7];
851 for(
int ip=0;ip<7;ip++){ lastIter_pars[ip]=fmin.Parameters().Vec()(ip);
857 scanPDF(lastIter_pars,tracksFailed,2);
860 edm::LogWarning(
"BSFitter") <<
"WARNING: # of tracks which have very low pdf value (pdf_d < 1.0e-05) are = "<<tracksFailed<<std::endl;
869 for (
int j = 0 ; j < 7 ; ++j) {
870 for(
int k = j ;
k < 7 ; ++
k) {
871 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
877 fmin.Parameters().Vec()(1),
878 fmin.Parameters().Vec()(2)),
879 fmin.Parameters().Vec()(3),
880 fmin.Parameters().Vec()(4),
881 fmin.Parameters().Vec()(5),
882 fmin.Parameters().Vec()(6),
893 thePDF->SetPDFs(
"PDFGauss_d_resolution*PDFGauss_z");
894 thePDF->SetData(fBSvector);
896 MnUserParameters upar;
897 upar.Add(
"X0", inipar[0],0.001);
898 upar.Add(
"Y0", inipar[1],0.001);
899 upar.Add(
"Z0", inipar[2],0.001);
900 upar.Add(
"sigmaZ",inipar[3],0.001);
901 upar.Add(
"dxdz",inipar[4],0.001);
902 upar.Add(
"dydz",inipar[5],0.001);
903 upar.Add(
"BeamWidthX",inipar[6],0.0001);
904 upar.Add(
"c0",0.0010,0.0001);
905 upar.Add(
"c1",0.0090,0.0001);
908 upar.Fix(
"BeamWidthX");
911 MnMigrad migrad(*thePDF, upar);
913 FunctionMinimum fmin = migrad();
914 ff_minimum = fmin.Fval();
918 for (
int j = 0 ; j < 6 ; ++j) {
919 for(
int k = j ;
k < 6 ; ++
k) {
920 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
929 fresolution_c0 = fmin.Parameters().Vec()(6);
930 fresolution_c1 = fmin.Parameters().Vec()(7);
931 fres_c0_err =
sqrt( fmin.Error().Matrix()(6,6) );
932 fres_c1_err =
sqrt( fmin.Error().Matrix()(7,7) );
934 for (
int j = 6 ; j < 8 ; ++j) {
935 for(
int k = 6 ;
k < 8 ; ++
k) {
936 fres_matrix(j-6,
k-6) = fmin.Error().Matrix()(j,
k);
941 fmin.Parameters().Vec()(1),
942 fmin.Parameters().Vec()(2)),
943 fmin.Parameters().Vec()(3),
944 fmin.Parameters().Vec()(4),
945 fmin.Parameters().Vec()(5),
reco::BeamSpot Fit_d_likelihood(double *inipar)
math::Error< dimension >::type CovarianceMatrix
double z0() const
z coordinate
reco::BeamSpot Fit_z_likelihood(double *inipar)
Sin< T >::type sin(const T &t)
math::XYZPoint Point
point in the space
double scanPDF(double *init_pars, int &tracksFailed, int option)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
reco::BeamSpot Fit_dres_z_likelihood(double *inipar)
void setType(BeamType type)
set beam type
double dydz() const
dydz slope
Cos< T >::type cos(const T &t)
Abs< T >::type abs(const T &t)
double BeamWidthX() const
beam width X
double dxdz() const
dxdz slope
reco::BeamSpot Fit_ited0phi()
double sigmaZ() const
sigma z
reco::BeamSpot Fit_z_chi2(double *inipar)
void Setd0Cut_d0phi(double d0cut)
double covariance(int i, int j) const
(i,j)-th element of error matrix
double y0() const
y coordinate
void SetChi2Cut_d0phi(double chi2cut)
reco::BeamSpot Fit_d0phi()
reco::BeamSpot Fit_d_z_likelihood(double *inipar, double *error_par)
Power< A, B >::type pow(const A &a, const B &b)
double x0() const
x coordinate