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 SERIAL");
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 SERIAL",
"",h1z->GetMean() -2.*h1z->GetRMS(),h1z->GetMean() +2.*h1z->GetRMS());
595 double fpar[2] = {fgaus.GetParameter(1), fgaus.GetParameter(2) };
599 for (
int j = 0 ; j < 2 ; ++j) {
600 for(
int k = j ;
k < 2 ; ++
k) {
605 for (
int j = 4 ; j < 6 ; ++j) {
606 for(
int k = j ;
k < 6 ; ++
k) {
612 matrix(2,2) = fgaus.GetParError(1) * fgaus.GetParError(1);
613 matrix(3,3) = fgaus.GetParError(2) * fgaus.GetParError(2);
621 double x0tmp = x_result(0,0);
622 double y0tmp = x_result(1,0);
624 x0tmp += x_result(2,0)*fpar[0];
625 y0tmp += x_result(3,0)*fpar[0];
653 fapplychi2cut =
true;
663 thePDF->SetPDFs(
"PDFGauss_d");
664 thePDF->SetData(fBSvector);
666 MnUserParameters upar;
667 upar.Add(
"X0", inipar[0],0.001);
668 upar.Add(
"Y0", inipar[1],0.001);
669 upar.Add(
"Z0", 0.,0.001);
670 upar.Add(
"sigmaZ",0.,0.001);
671 upar.Add(
"dxdz",inipar[2],0.001);
672 upar.Add(
"dydz",inipar[3],0.001);
675 MnMigrad migrad(*thePDF, upar);
677 FunctionMinimum fmin = migrad();
678 ff_minimum = fmin.Fval();
681 for (
int j = 0 ; j < 6 ; ++j) {
682 for(
int k = j ;
k < 6 ; ++
k) {
683 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
688 fmin.Parameters().Vec()(1),
691 fmin.Parameters().Vec()(4),
692 fmin.Parameters().Vec()(5),
700 if(option==1)init_pars[6]=0.0005;
711 double last_minvalue=1.0e+10;
712 double init_bw=-99.99;
716 double DeltadCut=0.1000;
717 if(init_pars[6]<0.0200){DeltadCut=0.0900; }
718 if(init_pars[6]<0.0100){DeltadCut=0.0700;}
721 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
724 if(option==1)iters=500;
725 if(option==2)iters=1;
727 for(
int p=0;
p<iters;
p++){
729 if(iters==500)init_pars[6]+=0.0002;
732 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam)
735 d_sig =
sqrt(init_pars[6]*init_pars[6] + (iparam->sigd0())*(iparam->sigd0()));
736 d_dprime = iparam->d0() - ( ( (init_pars[0] + iparam->z0()*(init_pars[4]))*
sin(iparam->phi0()) )
737 - ( (init_pars[1] + iparam->z0()*(init_pars[5]))*
cos(iparam->phi0()) ) );
740 if(
std::abs(d_dprime)<DeltadCut && option==2){ fBSvectorBW.push_back(*iparam);}
742 d_result = (
exp(-(d_dprime*d_dprime)/(2.0*d_sig*d_sig)))/(d_sig*fsqrt2pi);
743 z_sig =
sqrt(iparam->sigz0() * iparam->sigz0() + init_pars[3]*init_pars[3]);
744 z_result = (
exp(-((iparam->z0() - init_pars[2])*(iparam->z0() - init_pars[2]))/(2.0*z_sig*z_sig)))/(z_sig*fsqrt2pi);
745 tot_pdf=z_result*d_result;
751 if(d_result < 1.0
e-05){ tot_pdf=z_result*1.0e-05;
755 function =
function +
log(tot_pdf);
761 function= -2.0*
function;
762 if(
function<last_minvalue){init_bw=init_pars[6];
763 last_minvalue=
function; }
768 init_bw=init_bw+(0.20*init_bw);
775 <<
"scanPDF:====>>>> WARNING***: The initial guess value of Beam width is negative!!!!!!"<<std::endl
776 <<
"scanPDF:====>>>> Assigning beam width a starting value of "<<init_bw<<
" cm"<<std::endl;
793 inipar[6]=scanPDF(inipar,tracksFailed,1);
794 error_par[6]=(inipar[6])*0.20;
799 scanPDF(inipar,tracksFailed,2);
804 std::vector<BSTrkParameters>::const_iterator iparamBW = fBSvectorBW.begin();
805 for( iparamBW = fBSvectorBW.begin(); iparamBW != fBSvectorBW.end(); ++iparamBW)
806 { fBSvector.push_back(*iparamBW);
810 thePDF->SetPDFs(
"PDFGauss_d*PDFGauss_z");
811 thePDF->SetData(fBSvector);
812 MnUserParameters upar;
814 upar.Add(
"X0", inipar[0],error_par[0]);
815 upar.Add(
"Y0", inipar[1],error_par[1]);
816 upar.Add(
"Z0", inipar[2],error_par[2]);
817 upar.Add(
"sigmaZ",inipar[3],error_par[3]);
818 upar.Add(
"dxdz",inipar[4],error_par[4]);
819 upar.Add(
"dydz",inipar[5],error_par[5]);
820 upar.Add(
"BeamWidthX",inipar[6],error_par[6]);
823 MnMigrad migrad(*thePDF, upar);
825 FunctionMinimum fmin = migrad();
830 ff_minimum = fmin.Fval();
833 bool ff_nfcn=fmin.HasReachedCallLimit();
834 bool ff_cov=fmin.HasCovariance();
835 bool testing=fmin.IsValid();
841 edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: MINUIT DID NOT CONVERGES PROPERLY !!!!!!"<<std::endl;
842 if(ff_nfcn)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: No. of Calls Exhausted"<<std::endl;
843 if(!ff_cov)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: Covariance did not found"<<std::endl;
846 edm::LogInfo(
"BSFitter") <<
"The Total # Tracks used for beam width fit = "<<(fBSvectorBW.size())<<std::endl;
850 double lastIter_pars[7];
852 for(
int ip=0;ip<7;ip++){ lastIter_pars[ip]=fmin.Parameters().Vec()(ip);
858 scanPDF(lastIter_pars,tracksFailed,2);
861 edm::LogWarning(
"BSFitter") <<
"WARNING: # of tracks which have very low pdf value (pdf_d < 1.0e-05) are = "<<tracksFailed<<std::endl;
870 for (
int j = 0 ; j < 7 ; ++j) {
871 for(
int k = j ;
k < 7 ; ++
k) {
872 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
878 fmin.Parameters().Vec()(1),
879 fmin.Parameters().Vec()(2)),
880 fmin.Parameters().Vec()(3),
881 fmin.Parameters().Vec()(4),
882 fmin.Parameters().Vec()(5),
883 fmin.Parameters().Vec()(6),
894 thePDF->SetPDFs(
"PDFGauss_d_resolution*PDFGauss_z");
895 thePDF->SetData(fBSvector);
897 MnUserParameters upar;
898 upar.Add(
"X0", inipar[0],0.001);
899 upar.Add(
"Y0", inipar[1],0.001);
900 upar.Add(
"Z0", inipar[2],0.001);
901 upar.Add(
"sigmaZ",inipar[3],0.001);
902 upar.Add(
"dxdz",inipar[4],0.001);
903 upar.Add(
"dydz",inipar[5],0.001);
904 upar.Add(
"BeamWidthX",inipar[6],0.0001);
905 upar.Add(
"c0",0.0010,0.0001);
906 upar.Add(
"c1",0.0090,0.0001);
909 upar.Fix(
"BeamWidthX");
912 MnMigrad migrad(*thePDF, upar);
914 FunctionMinimum fmin = migrad();
915 ff_minimum = fmin.Fval();
919 for (
int j = 0 ; j < 6 ; ++j) {
920 for(
int k = j ;
k < 6 ; ++
k) {
921 matrix(j,
k) = fmin.Error().Matrix()(j,
k);
930 fresolution_c0 = fmin.Parameters().Vec()(6);
931 fresolution_c1 = fmin.Parameters().Vec()(7);
932 fres_c0_err =
sqrt( fmin.Error().Matrix()(6,6) );
933 fres_c1_err =
sqrt( fmin.Error().Matrix()(7,7) );
935 for (
int j = 6 ; j < 8 ; ++j) {
936 for(
int k = 6 ;
k < 8 ; ++
k) {
937 fres_matrix(j-6,
k-6) = fmin.Error().Matrix()(j,
k);
942 fmin.Parameters().Vec()(1),
943 fmin.Parameters().Vec()(2)),
944 fmin.Parameters().Vec()(3),
945 fmin.Parameters().Vec()(4),
946 fmin.Parameters().Vec()(5),
reco::BeamSpot Fit_d_likelihood(double *inipar)
math::Error< dimension >::type CovarianceMatrix
double z0() const
z coordinate
constexpr bool isNotFinite(T x)
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