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"
38 using namespace ROOT::Minuit2;
49 ffit_type =
"default";
50 ffit_variable =
"default";
57 fpar_name[1] =
"SigmaZ0 ";
60 fpar_name[4] =
"dxdz ";
61 fpar_name[5] =
"dydz ";
62 fpar_name[6] =
"SigmaBeam ";
71 theFitter =
new VariableMetricMinimizer();
76 fapplychi2cut =
false;
86 h1z =
new TH1F(
"h1z",
"z distribution",200,-fMaxZ, fMaxZ);
109 if ( ffit_variable ==
"z" ) {
111 if ( ffit_type ==
"chi2" ) {
113 return Fit_z_chi2(inipar);
115 }
else if ( ffit_type ==
"likelihood" ) {
117 return Fit_z_likelihood(inipar);
119 }
else if ( ffit_type ==
"combined" ) {
122 double tmp_par[2] = {tmp_beamspot.
z0(), tmp_beamspot.
sigmaZ()};
123 return Fit_z_likelihood(tmp_par);
128 <<
"Error in BeamSpotProducer/BSFitter: "
129 <<
"Illegal fit type, options are chi2,likelihood or combined(ie. first chi2 then likelihood)";
132 }
else if ( ffit_variable ==
"d" ) {
134 if ( ffit_type ==
"d0phi" ) {
138 }
else if ( ffit_type ==
"likelihood" ) {
140 return Fit_d_likelihood(inipar);
142 }
else if ( ffit_type ==
"combined" ) {
146 double tmp_par[4] = {tmp_beamspot.
x0(), tmp_beamspot.
y0(), tmp_beamspot.
dxdz(), tmp_beamspot.
dydz()};
147 return Fit_d_likelihood(tmp_par);
151 <<
"Error in BeamSpotProducer/BSFitter: "
152 <<
"Illegal fit type, options are d0phi, likelihood or combined";
154 }
else if ( ffit_variable ==
"d*z" || ffit_variable ==
"default" ) {
156 if ( ffit_type ==
"likelihood" || ffit_type ==
"default" ) {
171 this->Setd0Cut_d0phi(4.0);
203 if (ffit_type ==
"likelihood") {
204 double tmp_par[7] = {tmp_d0phi.
x0(), tmp_d0phi.
y0(), tmp_d0phi.
z0(),
207 double tmp_error_par[7];
209 tmp_error_par[6]=0.0;
211 reco::BeamSpot tmp_lh = Fit_d_z_likelihood(tmp_par,tmp_error_par);
214 edm::LogWarning(
"BSFitter") <<
"BSFitter: Result is non physical. Log-Likelihood fit to extract beam width did not converge." << std::endl;
222 edm::LogInfo(
"BSFitter") <<
"default track-based fit does not extract beam width." << std::endl;
227 }
else if ( ffit_type ==
"resolution" ) {
233 double tmp_par[7] = {tmp_d0phi.
x0(), tmp_d0phi.
y0(), tmp_z.
z0(),
235 double tmp_error_par[7];
236 for(
int s=0;
s<6;
s++){ tmp_error_par[
s] =
pow(tmp_par[
s],0.5);}
237 tmp_error_par[6]=0.0;
239 reco::BeamSpot tmp_beam = Fit_d_z_likelihood(tmp_par,tmp_error_par);
241 double tmp_par2[7] = {tmp_beam.
x0(), tmp_beam.
y0(), tmp_beam.
z0(),
249 edm::LogWarning(
"BSFitter") <<
"Result is non physical. Log-Likelihood fit did not converge." << std::endl;
258 <<
"Error in BeamSpotProducer/BSFitter: "
259 <<
"Illegal fit type, options are likelihood or resolution";
264 <<
"Error in BeamSpotProducer/BSFitter: "
265 <<
"Illegal variable type, options are \"z\", \"d\", or \"d*z\"";
278 std::vector<double> par(2,0);
279 std::vector<double> err(2,0);
283 err.push_back(0.0001);
284 err.push_back(0.0001);
288 thePDF->SetPDFs(
"PDFGauss_z");
289 thePDF->SetData(fBSvector);
293 MnUserParameters upar;
294 upar.Add(
"X0", 0.,0.);
295 upar.Add(
"Y0", 0.,0.);
296 upar.Add(
"Z0", inipar[0],0.001);
297 upar.Add(
"sigmaZ",inipar[1],0.001);
299 MnMigrad migrad(*thePDF, upar);
301 FunctionMinimum fmin = migrad();
302 ff_minimum = fmin.Fval();
323 for (
int j = 2 ;
j < 4 ; ++
j) {
324 for(
int k =
j ;
k < 4 ; ++
k) {
331 fmin.Parameters().Vec()(2)),
332 fmin.Parameters().Vec()(3),
351 h1z =
new TH1F(
"h1z",
"z distribution",200,-fMaxZ, fMaxZ);
353 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
357 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam) {
359 h1z->Fill( iparam->z0() );
363 h1z->Fit(
"gaus",
"QLM0");
366 TF1 *fgaus = h1z->GetFunction(
"gaus");
368 double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
372 matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
373 matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
394 edm::LogInfo(
"BSFitter") <<
"number of total input tracks: " << fBSvector.size() << std::endl;
398 if ( (
int)fBSvector.size() <= fminNtrks ) {
399 edm::LogWarning(
"BSFitter") <<
"need at least " << fminNtrks <<
" tracks to run beamline fitter." << std::endl;
405 theanswer = Fit_d0phi();
406 if ( goodfit ) fnthite++;
412 ftmprow > fconvergence * fBSvector.size() &&
413 ftmprow > fminNtrks ) {
415 theanswer = Fit_d0phi();
419 ftmprow > fconvergence * fBSvector.size() &&
420 ftmprow > fminNtrks ) {
421 preanswer = theanswer;
428 theanswer = preanswer;
432 edm::LogInfo(
"BSFitter") <<
"Total number of successful iterations = " << ( goodfit ? (fnthite+1) : fnthite ) << std::endl;
450 if (fnthite > 0)
edm::LogInfo(
"BSFitter") <<
" number of tracks used: " << ftmprow << std::endl;
460 TMatrixD x_result(4,1);
461 TMatrixDSym V_result(4);
474 std::vector<BSTrkParameters>::iterator iparam = fBSvector.begin();
482 for( iparam = fBSvector.begin() ;
483 iparam != fBSvector.end() ; ++iparam) {
493 g(0,0) =
sin(iparam->phi0());
494 g(1,0) = -
cos(iparam->phi0());
495 g(2,0) = iparam->z0() *
g(0,0);
496 g(3,0) = iparam->z0() *
g(1,0);
500 double sigmabeam2 = 0.006 * 0.006;
501 if (finputBeamWidth > 0 ) sigmabeam2 = finputBeamWidth * finputBeamWidth;
502 else {
edm::LogWarning(
"BSFitter") <<
"using in fit beam width = " <<
sqrt(sigmabeam2) << std::endl; }
506 double sigma2 = sigmabeam2 + (iparam->sigd0())* (iparam->sigd0());
508 TMatrixD ftmptrans(1,4);
509 ftmptrans = ftmptrans.Transpose(ftmp);
510 TMatrixD dcor = ftmptrans *
g;
511 double chi2tmp = (iparam->d0() - dcor(0,0)) * (iparam->d0() - dcor(0,0))/sigma2;
512 (*iparam) =
BSTrkParameters(iparam->z0(),iparam->sigz0(),iparam->d0(),iparam->sigd0(),
513 iparam->phi0(), iparam->pt(),dcor(0,0),chi2tmp);
516 if (fapplyd0cut && fnthite>0 ) {
517 if (
std::abs(iparam->d0() - dcor(0,0)) > fd0cut ) pass =
false;
520 if (fapplychi2cut && fnthite>0 ) {
521 if ( chi2tmp > fchi2cut ) pass =
false;
527 for(
int j = 0 ;
j < 4 ; ++
j) {
528 for(
int k =
j ;
k < 4 ; ++
k) {
534 Vint += (temp * (1 / sigma2));
535 b += (iparam->d0() / sigma2 *
g);
538 h1z->Fill( iparam->z0() );
543 Double_t determinant;
546 if (!bk.Decompose()) {
549 <<
"Decomposition failed, matrix singular ?" << std::endl
550 <<
"condition number = " << bk.Condition() << std::endl;
553 V_result = Vint.InvertFast(&determinant);
554 x_result = V_result *
b;
567 h1z->Fit(
"gaus",
"QLM0",
"",h1z->GetMean() -2.*h1z->GetRMS(),h1z->GetMean() +2.*h1z->GetRMS());
570 TF1 *fgaus = h1z->GetFunction(
"gaus");
573 edm::LogError(
"NoBeamSpotFit")<<
"gaussian fit failed. no BS d0 fit";
576 double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
580 for (
int j = 0 ;
j < 2 ; ++
j) {
581 for(
int k =
j ;
k < 2 ; ++
k) {
586 for (
int j = 4 ;
j < 6 ; ++
j) {
587 for(
int k =
j ;
k < 6 ; ++
k) {
593 matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
594 matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
602 double x0tmp = x_result(0,0);
603 double y0tmp = x_result(1,0);
605 x0tmp += x_result(2,0)*fpar[0];
606 y0tmp += x_result(3,0)*fpar[0];
634 fapplychi2cut =
true;
644 thePDF->SetPDFs(
"PDFGauss_d");
645 thePDF->SetData(fBSvector);
647 MnUserParameters upar;
648 upar.Add(
"X0", inipar[0],0.001);
649 upar.Add(
"Y0", inipar[1],0.001);
650 upar.Add(
"Z0", 0.,0.001);
651 upar.Add(
"sigmaZ",0.,0.001);
652 upar.Add(
"dxdz",inipar[2],0.001);
653 upar.Add(
"dydz",inipar[3],0.001);
656 MnMigrad migrad(*thePDF, upar);
658 FunctionMinimum fmin = migrad();
659 ff_minimum = fmin.Fval();
662 for (
int j = 0 ;
j < 6 ; ++
j) {
663 for(
int k =
j ;
k < 6 ; ++
k) {
669 fmin.Parameters().Vec()(1),
672 fmin.Parameters().Vec()(4),
673 fmin.Parameters().Vec()(5),
681 if(option==1)init_pars[6]=0.0005;
692 double last_minvalue=1.0e+10;
693 double init_bw=-99.99;
697 double DeltadCut=0.1000;
698 if(init_pars[6]<0.0200){DeltadCut=0.0900; }
699 if(init_pars[6]<0.0100){DeltadCut=0.0700;}
702 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
705 if(option==1)iters=500;
706 if(option==2)iters=1;
708 for(
int p=0;
p<iters;
p++){
710 if(iters==500)init_pars[6]+=0.0002;
713 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam)
716 d_sig =
sqrt(init_pars[6]*init_pars[6] + (iparam->sigd0())*(iparam->sigd0()));
717 d_dprime = iparam->d0() - ( ( (init_pars[0] + iparam->z0()*(init_pars[4]))*
sin(iparam->phi0()) )
718 - ( (init_pars[1] + iparam->z0()*(init_pars[5]))*
cos(iparam->phi0()) ) );
721 if(
std::abs(d_dprime)<DeltadCut && option==2){ fBSvectorBW.push_back(*iparam);}
723 d_result = (
exp(-(d_dprime*d_dprime)/(2.0*d_sig*d_sig)))/(d_sig*fsqrt2pi);
724 z_sig =
sqrt(iparam->sigz0() * iparam->sigz0() + init_pars[3]*init_pars[3]);
725 z_result = (
exp(-((iparam->z0() - init_pars[2])*(iparam->z0() - init_pars[2]))/(2.0*z_sig*z_sig)))/(z_sig*fsqrt2pi);
726 tot_pdf=z_result*d_result;
732 if(d_result < 1.0
e-05){ tot_pdf=z_result*1.0e-05;
736 function =
function +
log(tot_pdf);
743 function= -2.0*
function;
744 if(
function<last_minvalue){init_bw=init_pars[6];
745 last_minvalue=
function; }
750 init_bw=init_bw+(0.20*init_bw);
757 <<
"scanPDF:====>>>> WARNING***: The initial guess value of Beam width is negative!!!!!!"<<std::endl
758 <<
"scanPDF:====>>>> Assigning beam width a starting value of "<<init_bw<<
" cm"<<std::endl;
775 inipar[6]=scanPDF(inipar,tracksFailed,1);
776 error_par[6]=(inipar[6])*0.20;
781 scanPDF(inipar,tracksFailed,2);
786 std::vector<BSTrkParameters>::const_iterator iparamBW = fBSvectorBW.begin();
787 for( iparamBW = fBSvectorBW.begin(); iparamBW != fBSvectorBW.end(); ++iparamBW)
788 { fBSvector.push_back(*iparamBW);
792 thePDF->SetPDFs(
"PDFGauss_d*PDFGauss_z");
793 thePDF->SetData(fBSvector);
794 MnUserParameters upar;
796 upar.Add(
"X0", inipar[0],error_par[0]);
797 upar.Add(
"Y0", inipar[1],error_par[1]);
798 upar.Add(
"Z0", inipar[2],error_par[2]);
799 upar.Add(
"sigmaZ",inipar[3],error_par[3]);
800 upar.Add(
"dxdz",inipar[4],error_par[4]);
801 upar.Add(
"dydz",inipar[5],error_par[5]);
802 upar.Add(
"BeamWidthX",inipar[6],error_par[6]);
805 MnMigrad migrad(*thePDF, upar);
807 FunctionMinimum fmin = migrad();
812 ff_minimum = fmin.Fval();
815 bool ff_nfcn=fmin.HasReachedCallLimit();
816 bool ff_cov=fmin.HasCovariance();
817 bool testing=fmin.IsValid();
823 edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: MINUIT DID NOT CONVERGES PROPERLY !!!!!!"<<std::endl;
824 if(ff_nfcn)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: No. of Calls Exhausted"<<std::endl;
825 if(!ff_cov)
edm::LogWarning(
"BSFitter") <<
"===========>>>>>** WARNING: Covariance did not found"<<std::endl;
828 edm::LogInfo(
"BSFitter") <<
"The Total # Tracks used for beam width fit = "<<(fBSvectorBW.size())<<std::endl;
832 double lastIter_pars[7];
834 for(
int ip=0;ip<7;ip++){ lastIter_pars[ip]=fmin.Parameters().Vec()(ip);
840 scanPDF(lastIter_pars,tracksFailed,2);
843 edm::LogWarning(
"BSFitter") <<
"WARNING: # of tracks which have very low pdf value (pdf_d < 1.0e-05) are = "<<tracksFailed<<std::endl;
852 for (
int j = 0 ;
j < 7 ; ++
j) {
853 for(
int k =
j ;
k < 7 ; ++
k) {
860 fmin.Parameters().Vec()(1),
861 fmin.Parameters().Vec()(2)),
862 fmin.Parameters().Vec()(3),
863 fmin.Parameters().Vec()(4),
864 fmin.Parameters().Vec()(5),
865 fmin.Parameters().Vec()(6),
876 thePDF->SetPDFs(
"PDFGauss_d_resolution*PDFGauss_z");
877 thePDF->SetData(fBSvector);
879 MnUserParameters upar;
880 upar.Add(
"X0", inipar[0],0.001);
881 upar.Add(
"Y0", inipar[1],0.001);
882 upar.Add(
"Z0", inipar[2],0.001);
883 upar.Add(
"sigmaZ",inipar[3],0.001);
884 upar.Add(
"dxdz",inipar[4],0.001);
885 upar.Add(
"dydz",inipar[5],0.001);
886 upar.Add(
"BeamWidthX",inipar[6],0.0001);
887 upar.Add(
"c0",0.0010,0.0001);
888 upar.Add(
"c1",0.0090,0.0001);
891 upar.Fix(
"BeamWidthX");
894 MnMigrad migrad(*thePDF, upar);
896 FunctionMinimum fmin = migrad();
897 ff_minimum = fmin.Fval();
901 for (
int j = 0 ;
j < 6 ; ++
j) {
902 for(
int k =
j ;
k < 6 ; ++
k) {
912 fresolution_c0 = fmin.Parameters().Vec()(6);
913 fresolution_c1 = fmin.Parameters().Vec()(7);
914 fres_c0_err =
sqrt( fmin.Error().Matrix()(6,6) );
915 fres_c1_err =
sqrt( fmin.Error().Matrix()(7,7) );
917 for (
int j = 6 ;
j < 8 ; ++
j) {
918 for(
int k = 6 ;
k < 8 ; ++
k) {
919 fres_matrix(
j-6,
k-6) = fmin.Error().Matrix()(
j,
k);
924 fmin.Parameters().Vec()(1),
925 fmin.Parameters().Vec()(2)),
926 fmin.Parameters().Vec()(3),
927 fmin.Parameters().Vec()(4),
928 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