00001
00015 #include "Minuit2/VariableMetricMinimizer.h"
00016 #include "Minuit2/FunctionMinimum.h"
00017 #include "Minuit2/MnPrint.h"
00018 #include "Minuit2/MnMigrad.h"
00019 #include "Minuit2/MnUserParameterState.h"
00020
00021
00022
00023 #include <vector>
00024 #include <cmath>
00025
00026
00027 #include "RecoVertex/BeamSpotProducer/interface/BSFitter.h"
00028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00029 #include "FWCore/Utilities/interface/Exception.h"
00030
00031
00032 #include "TMatrixD.h"
00033 #include "TMatrixDSym.h"
00034 #include "TDecompBK.h"
00035 #include "TH1.h"
00036 #include "TF1.h"
00037
00038 using namespace ROOT::Minuit2;
00039
00040
00041
00042 BSFitter::BSFitter() {
00043 fbeamtype = reco::BeamSpot::Unknown;
00044 }
00045
00046
00047 BSFitter::BSFitter( std:: vector< BSTrkParameters > BSvector ) {
00048
00049 ffit_type = "default";
00050 ffit_variable = "default";
00051
00052 fBSvector = BSvector;
00053
00054 fsqrt2pi = sqrt(2.* TMath::Pi());
00055
00056 fpar_name[0] = "z0 ";
00057 fpar_name[1] = "SigmaZ0 ";
00058 fpar_name[2] = "X0 ";
00059 fpar_name[3] = "Y0 ";
00060 fpar_name[4] = "dxdz ";
00061 fpar_name[5] = "dydz ";
00062 fpar_name[6] = "SigmaBeam ";
00063
00064
00065 thePDF = new BSpdfsFcn();
00066
00067
00068
00069
00070
00071 theFitter = new VariableMetricMinimizer();
00072
00073
00074
00075 fapplyd0cut = false;
00076 fapplychi2cut = false;
00077 ftmprow = 0;
00078 ftmp.ResizeTo(4,1);
00079 ftmp.Zero();
00080 fnthite=0;
00081 fMaxZ = 50.;
00082 fconvergence = 0.5;
00083 fminNtrks = 100;
00084 finputBeamWidth = -1;
00085
00086 h1z = new TH1F("h1z","z distribution",200,-fMaxZ, fMaxZ);
00087
00088 }
00089
00090
00091 BSFitter::~BSFitter()
00092 {
00093
00094 delete thePDF;
00095 delete theFitter;
00096 }
00097
00098
00099
00100 reco::BeamSpot BSFitter::Fit() {
00101
00102 return this->Fit(0);
00103
00104 }
00105
00106
00107 reco::BeamSpot BSFitter::Fit(double *inipar = 0) {
00108 fbeamtype = reco::BeamSpot::Unknown;
00109 if ( ffit_variable == "z" ) {
00110
00111 if ( ffit_type == "chi2" ) {
00112
00113 return Fit_z_chi2(inipar);
00114
00115 } else if ( ffit_type == "likelihood" ) {
00116
00117 return Fit_z_likelihood(inipar);
00118
00119 } else if ( ffit_type == "combined" ) {
00120
00121 reco::BeamSpot tmp_beamspot = Fit_z_chi2(inipar);
00122 double tmp_par[2] = {tmp_beamspot.z0(), tmp_beamspot.sigmaZ()};
00123 return Fit_z_likelihood(tmp_par);
00124
00125 } else {
00126
00127 throw cms::Exception("LogicError")
00128 << "Error in BeamSpotProducer/BSFitter: "
00129 << "Illegal fit type, options are chi2,likelihood or combined(ie. first chi2 then likelihood)";
00130
00131 }
00132 } else if ( ffit_variable == "d" ) {
00133
00134 if ( ffit_type == "d0phi" ) {
00135 this->d0phi_Init();
00136 return Fit_d0phi();
00137
00138 } else if ( ffit_type == "likelihood" ) {
00139
00140 return Fit_d_likelihood(inipar);
00141
00142 } else if ( ffit_type == "combined" ) {
00143
00144 this->d0phi_Init();
00145 reco::BeamSpot tmp_beamspot = Fit_d0phi();
00146 double tmp_par[4] = {tmp_beamspot.x0(), tmp_beamspot.y0(), tmp_beamspot.dxdz(), tmp_beamspot.dydz()};
00147 return Fit_d_likelihood(tmp_par);
00148
00149 } else {
00150 throw cms::Exception("LogicError")
00151 << "Error in BeamSpotProducer/BSFitter: "
00152 << "Illegal fit type, options are d0phi, likelihood or combined";
00153 }
00154 } else if ( ffit_variable == "d*z" || ffit_variable == "default" ) {
00155
00156 if ( ffit_type == "likelihood" || ffit_type == "default" ) {
00157
00158 reco::BeamSpot::CovarianceMatrix matrix;
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 this->d0phi_Init();
00170
00171 this->Setd0Cut_d0phi(4.0);
00172 reco::BeamSpot tmp_d0phi= Fit_ited0phi();
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 reco::BeamSpot spot(reco::BeamSpot::Point(tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_d0phi.z0()),
00189 tmp_d0phi.sigmaZ(),
00190 tmp_d0phi.dxdz(),
00191 tmp_d0phi.dydz(),
00192 0.,
00193 tmp_d0phi.covariance(),
00194 fbeamtype );
00195
00196
00197
00198
00199
00200
00201
00202
00203 if (ffit_type == "likelihood") {
00204 double tmp_par[7] = {tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_d0phi.z0(),
00205 tmp_d0phi.sigmaZ(), tmp_d0phi.dxdz(), tmp_d0phi.dydz(),0.0};
00206
00207 double tmp_error_par[7];
00208 for(int s=0;s<6;s++){ tmp_error_par[s] = pow( tmp_d0phi.covariance()(s,s),0.5);}
00209 tmp_error_par[6]=0.0;
00210
00211 reco::BeamSpot tmp_lh = Fit_d_z_likelihood(tmp_par,tmp_error_par);
00212
00213 if ( isnan(ff_minimum) || std::isinf(ff_minimum) ) {
00214 edm::LogWarning("BSFitter") << "BSFitter: Result is non physical. Log-Likelihood fit to extract beam width did not converge." << std::endl;
00215 tmp_lh.setType(reco::BeamSpot::Unknown);
00216 return tmp_lh;
00217 }
00218 return tmp_lh;
00219
00220 } else {
00221
00222 edm::LogInfo("BSFitter") << "default track-based fit does not extract beam width." << std::endl;
00223 return spot;
00224 }
00225
00226
00227 } else if ( ffit_type == "resolution" ) {
00228
00229 reco::BeamSpot tmp_z = Fit_z_chi2(inipar);
00230 this->d0phi_Init();
00231 reco::BeamSpot tmp_d0phi = Fit_d0phi();
00232
00233 double tmp_par[7] = {tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_z.z0(),
00234 tmp_z.sigmaZ(), tmp_d0phi.dxdz(), tmp_d0phi.dydz(),0.0};
00235 double tmp_error_par[7];
00236 for(int s=0;s<6;s++){ tmp_error_par[s] = pow(tmp_par[s],0.5);}
00237 tmp_error_par[6]=0.0;
00238
00239 reco::BeamSpot tmp_beam = Fit_d_z_likelihood(tmp_par,tmp_error_par);
00240
00241 double tmp_par2[7] = {tmp_beam.x0(), tmp_beam.y0(), tmp_beam.z0(),
00242 tmp_beam.sigmaZ(), tmp_beam.dxdz(), tmp_beam.dydz(),
00243 tmp_beam.BeamWidthX()};
00244
00245 reco::BeamSpot tmp_lh = Fit_dres_z_likelihood(tmp_par2);
00246
00247 if ( isnan(ff_minimum) || std::isinf(ff_minimum) ) {
00248
00249 edm::LogWarning("BSFitter") << "Result is non physical. Log-Likelihood fit did not converge." << std::endl;
00250 tmp_lh.setType(reco::BeamSpot::Unknown);
00251 return tmp_lh;
00252 }
00253 return tmp_lh;
00254
00255 } else {
00256
00257 throw cms::Exception("LogicError")
00258 << "Error in BeamSpotProducer/BSFitter: "
00259 << "Illegal fit type, options are likelihood or resolution";
00260 }
00261 } else {
00262
00263 throw cms::Exception("LogicError")
00264 << "Error in BeamSpotProducer/BSFitter: "
00265 << "Illegal variable type, options are \"z\", \"d\", or \"d*z\"";
00266 }
00267
00268
00269 }
00270
00271
00272 reco::BeamSpot BSFitter::Fit_z_likelihood(double *inipar) {
00273
00274
00275
00276
00277
00278 std::vector<double> par(2,0);
00279 std::vector<double> err(2,0);
00280
00281 par.push_back(0.0);
00282 par.push_back(7.0);
00283 err.push_back(0.0001);
00284 err.push_back(0.0001);
00285
00286
00287
00288 thePDF->SetPDFs("PDFGauss_z");
00289 thePDF->SetData(fBSvector);
00290
00291
00292
00293 MnUserParameters upar;
00294 upar.Add("X0", 0.,0.);
00295 upar.Add("Y0", 0.,0.);
00296 upar.Add("Z0", inipar[0],0.001);
00297 upar.Add("sigmaZ",inipar[1],0.001);
00298
00299 MnMigrad migrad(*thePDF, upar);
00300
00301 FunctionMinimum fmin = migrad();
00302 ff_minimum = fmin.Fval();
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 reco::BeamSpot::CovarianceMatrix matrix;
00322
00323 for (int j = 2 ; j < 4 ; ++j) {
00324 for(int k = j ; k < 4 ; ++k) {
00325 matrix(j,k) = fmin.Error().Matrix()(j,k);
00326 }
00327 }
00328
00329 return reco::BeamSpot( reco::BeamSpot::Point(0.,
00330 0.,
00331 fmin.Parameters().Vec()(2)),
00332 fmin.Parameters().Vec()(3),
00333 0.,
00334 0.,
00335 0.,
00336 matrix,
00337 fbeamtype );
00338 }
00339
00340
00341 reco::BeamSpot BSFitter::Fit_z_chi2(double *inipar) {
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 h1z = new TH1F("h1z","z distribution",200,-fMaxZ, fMaxZ);
00352
00353 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
00354
00355
00356
00357 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam) {
00358
00359 h1z->Fill( iparam->z0() );
00360
00361 }
00362
00363 h1z->Fit("gaus","QLM0");
00364
00365
00366 TF1 *fgaus = h1z->GetFunction("gaus");
00367
00368 double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
00369
00370 reco::BeamSpot::CovarianceMatrix matrix;
00371
00372 matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
00373 matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
00374
00375
00376
00377 return reco::BeamSpot( reco::BeamSpot::Point(0.,
00378 0.,
00379 fpar[0]),
00380 fpar[1],
00381 0.,
00382 0.,
00383 0.,
00384 matrix,
00385 fbeamtype );
00386
00387
00388 }
00389
00390
00391 reco::BeamSpot BSFitter::Fit_ited0phi() {
00392
00393 this->d0phi_Init();
00394 edm::LogInfo("BSFitter") << "number of total input tracks: " << fBSvector.size() << std::endl;
00395
00396 reco::BeamSpot theanswer;
00397
00398 if ( (int)fBSvector.size() <= fminNtrks ) {
00399 edm::LogWarning("BSFitter") << "need at least " << fminNtrks << " tracks to run beamline fitter." << std::endl;
00400 fbeamtype = reco::BeamSpot::Fake;
00401 theanswer.setType(fbeamtype);
00402 return theanswer;
00403 }
00404
00405 theanswer = Fit_d0phi();
00406 if ( goodfit ) fnthite++;
00407
00408
00409 reco::BeamSpot preanswer = theanswer;
00410
00411 while ( goodfit &&
00412 ftmprow > fconvergence * fBSvector.size() &&
00413 ftmprow > fminNtrks ) {
00414
00415 theanswer = Fit_d0phi();
00416 fd0cut /= 1.5;
00417 fchi2cut /= 1.5;
00418 if ( goodfit &&
00419 ftmprow > fconvergence * fBSvector.size() &&
00420 ftmprow > fminNtrks ) {
00421 preanswer = theanswer;
00422
00423 fnthite++;
00424 }
00425 }
00426
00427
00428 theanswer = preanswer;
00429
00430
00431
00432 edm::LogInfo("BSFitter") << "Total number of successful iterations = " << ( goodfit ? (fnthite+1) : fnthite ) << std::endl;
00433 if (goodfit) {
00434 fbeamtype = reco::BeamSpot::Tracker;
00435 theanswer.setType(fbeamtype);
00436 }
00437 else {
00438 edm::LogWarning("BSFitter") << "Fit doesn't converge!!!" << std::endl;
00439 fbeamtype = reco::BeamSpot::Unknown;
00440 theanswer.setType(fbeamtype);
00441 }
00442 return theanswer;
00443 }
00444
00445
00446
00447 reco::BeamSpot BSFitter::Fit_d0phi() {
00448
00449
00450 if (fnthite > 0) edm::LogInfo("BSFitter") << " number of tracks used: " << ftmprow << std::endl;
00451
00452
00453
00454
00455
00456
00457 h1z->Reset();
00458
00459
00460 TMatrixD x_result(4,1);
00461 TMatrixDSym V_result(4);
00462
00463 TMatrixDSym Vint(4);
00464 TMatrixD b(4,1);
00465
00466
00467
00468 Vint.Zero();
00469 b.Zero();
00470
00471 TMatrixD g(4,1);
00472 TMatrixDSym temp(4);
00473
00474 std::vector<BSTrkParameters>::iterator iparam = fBSvector.begin();
00475 ftmprow=0;
00476
00477
00478
00479
00480
00481
00482 for( iparam = fBSvector.begin() ;
00483 iparam != fBSvector.end() ; ++iparam) {
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 g(0,0) = sin(iparam->phi0());
00494 g(1,0) = -cos(iparam->phi0());
00495 g(2,0) = iparam->z0() * g(0,0);
00496 g(3,0) = iparam->z0() * g(1,0);
00497
00498
00499
00500 double sigmabeam2 = 0.006 * 0.006;
00501 if (finputBeamWidth > 0 ) sigmabeam2 = finputBeamWidth * finputBeamWidth;
00502 else { edm::LogWarning("BSFitter") << "using in fit beam width = " << sqrt(sigmabeam2) << std::endl; }
00503
00504
00505
00506 double sigma2 = sigmabeam2 + (iparam->sigd0())* (iparam->sigd0());
00507
00508 TMatrixD ftmptrans(1,4);
00509 ftmptrans = ftmptrans.Transpose(ftmp);
00510 TMatrixD dcor = ftmptrans * g;
00511 double chi2tmp = (iparam->d0() - dcor(0,0)) * (iparam->d0() - dcor(0,0))/sigma2;
00512 (*iparam) = BSTrkParameters(iparam->z0(),iparam->sigz0(),iparam->d0(),iparam->sigd0(),
00513 iparam->phi0(), iparam->pt(),dcor(0,0),chi2tmp);
00514
00515 bool pass = true;
00516 if (fapplyd0cut && fnthite>0 ) {
00517 if ( std::abs(iparam->d0() - dcor(0,0)) > fd0cut ) pass = false;
00518
00519 }
00520 if (fapplychi2cut && fnthite>0 ) {
00521 if ( chi2tmp > fchi2cut ) pass = false;
00522
00523 }
00524
00525 if (pass) {
00526 temp.Zero();
00527 for(int j = 0 ; j < 4 ; ++j) {
00528 for(int k = j ; k < 4 ; ++k) {
00529 temp(j,k) += g(j,0) * g(k,0);
00530 }
00531 }
00532
00533
00534 Vint += (temp * (1 / sigma2));
00535 b += (iparam->d0() / sigma2 * g);
00536
00537 ftmprow++;
00538 h1z->Fill( iparam->z0() );
00539 }
00540
00541
00542 }
00543 Double_t determinant;
00544 TDecompBK bk(Vint);
00545 bk.SetTol(1e-11);
00546 if (!bk.Decompose()) {
00547 goodfit = false;
00548 edm::LogWarning("BSFitter")
00549 << "Decomposition failed, matrix singular ?" << std::endl
00550 << "condition number = " << bk.Condition() << std::endl;
00551 }
00552 else {
00553 V_result = Vint.InvertFast(&determinant);
00554 x_result = V_result * b;
00555 }
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 h1z->Fit("gaus","QLM0","",h1z->GetMean() -2.*h1z->GetRMS(),h1z->GetMean() +2.*h1z->GetRMS());
00568
00569
00570 TF1 *fgaus = h1z->GetFunction("gaus");
00571
00572 if (!fgaus){
00573 edm::LogError("NoBeamSpotFit")<<"gaussian fit failed. no BS d0 fit";
00574 return reco::BeamSpot();
00575 }
00576 double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
00577
00578 reco::BeamSpot::CovarianceMatrix matrix;
00579
00580 for (int j = 0 ; j < 2 ; ++j) {
00581 for(int k = j ; k < 2 ; ++k) {
00582 matrix(j,k) = V_result(j,k);
00583 }
00584 }
00585
00586 for (int j = 4 ; j < 6 ; ++j) {
00587 for(int k = j ; k < 6 ; ++k) {
00588 matrix(j,k) = V_result(j-2,k-2);
00589 }
00590 }
00591
00592
00593 matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
00594 matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
00595
00596 ftmp = x_result;
00597
00598
00599
00600
00601
00602 double x0tmp = x_result(0,0);
00603 double y0tmp = x_result(1,0);
00604
00605 x0tmp += x_result(2,0)*fpar[0];
00606 y0tmp += x_result(3,0)*fpar[0];
00607
00608
00609 return reco::BeamSpot( reco::BeamSpot::Point(x0tmp,
00610 y0tmp,
00611 fpar[0]),
00612 fpar[1],
00613 x_result(2,0),
00614 x_result(3,0),
00615 0.,
00616 matrix,
00617 fbeamtype );
00618
00619 }
00620
00621
00622
00623 void BSFitter::Setd0Cut_d0phi(double d0cut) {
00624
00625 fapplyd0cut = true;
00626
00627
00628 fd0cut = d0cut;
00629 }
00630
00631
00632 void BSFitter::SetChi2Cut_d0phi(double chi2cut) {
00633
00634 fapplychi2cut = true;
00635
00636
00637 fchi2cut = chi2cut;
00638 }
00639
00640
00641 reco::BeamSpot BSFitter::Fit_d_likelihood(double *inipar) {
00642
00643
00644 thePDF->SetPDFs("PDFGauss_d");
00645 thePDF->SetData(fBSvector);
00646
00647 MnUserParameters upar;
00648 upar.Add("X0", inipar[0],0.001);
00649 upar.Add("Y0", inipar[1],0.001);
00650 upar.Add("Z0", 0.,0.001);
00651 upar.Add("sigmaZ",0.,0.001);
00652 upar.Add("dxdz",inipar[2],0.001);
00653 upar.Add("dydz",inipar[3],0.001);
00654
00655
00656 MnMigrad migrad(*thePDF, upar);
00657
00658 FunctionMinimum fmin = migrad();
00659 ff_minimum = fmin.Fval();
00660
00661 reco::BeamSpot::CovarianceMatrix matrix;
00662 for (int j = 0 ; j < 6 ; ++j) {
00663 for(int k = j ; k < 6 ; ++k) {
00664 matrix(j,k) = fmin.Error().Matrix()(j,k);
00665 }
00666 }
00667
00668 return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
00669 fmin.Parameters().Vec()(1),
00670 0.),
00671 0.,
00672 fmin.Parameters().Vec()(4),
00673 fmin.Parameters().Vec()(5),
00674 0.,
00675 matrix,
00676 fbeamtype );
00677 }
00678
00679 double BSFitter::scanPDF(double *init_pars, int & tracksfixed, int option){
00680
00681 if(option==1)init_pars[6]=0.0005;
00682
00683
00684 double fsqrt2pi=0.0;
00685 double d_sig=0.0;
00686 double d_dprime=0.0;
00687 double d_result=0.0;
00688 double z_sig=0.0;
00689 double z_result=0.0;
00690 double function=0.0;
00691 double tot_pdf=0.0;
00692 double last_minvalue=1.0e+10;
00693 double init_bw=-99.99;
00694 int iters=0;
00695
00696
00697 double DeltadCut=0.1000;
00698 if(init_pars[6]<0.0200){DeltadCut=0.0900; }
00699 if(init_pars[6]<0.0100){DeltadCut=0.0700;}
00700
00701
00702 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
00703
00704
00705 if(option==1)iters=500;
00706 if(option==2)iters=1;
00707
00708 for(int p=0;p<iters;p++){
00709
00710 if(iters==500)init_pars[6]+=0.0002;
00711 tracksfixed=0;
00712
00713 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam)
00714 {
00715 fsqrt2pi = sqrt(2.* TMath::Pi());
00716 d_sig = sqrt(init_pars[6]*init_pars[6] + (iparam->sigd0())*(iparam->sigd0()));
00717 d_dprime = iparam->d0() - ( ( (init_pars[0] + iparam->z0()*(init_pars[4]))*sin(iparam->phi0()) )
00718 - ( (init_pars[1] + iparam->z0()*(init_pars[5]))*cos(iparam->phi0()) ) );
00719
00720
00721 if(std::abs(d_dprime)<DeltadCut && option==2){ fBSvectorBW.push_back(*iparam);}
00722
00723 d_result = (exp(-(d_dprime*d_dprime)/(2.0*d_sig*d_sig)))/(d_sig*fsqrt2pi);
00724 z_sig = sqrt(iparam->sigz0() * iparam->sigz0() + init_pars[3]*init_pars[3]);
00725 z_result = (exp(-((iparam->z0() - init_pars[2])*(iparam->z0() - init_pars[2]))/(2.0*z_sig*z_sig)))/(z_sig*fsqrt2pi);
00726 tot_pdf=z_result*d_result;
00727
00728
00729
00730
00731
00732 if(d_result < 1.0e-05){ tot_pdf=z_result*1.0e-05;
00733
00734 tracksfixed++; }
00735
00736 function = function + log(tot_pdf);
00737 tot_pdf=0.0;
00738
00739
00740 }
00741
00742
00743 function= -2.0*function;
00744 if(function<last_minvalue){init_bw=init_pars[6];
00745 last_minvalue=function; }
00746 function=0.0;
00747 }
00748
00749 if(init_bw>0) {
00750 init_bw=init_bw+(0.20*init_bw);
00751
00752 }
00753 else{
00754
00755 if(option==1){
00756 edm::LogWarning("BSFitter")
00757 <<"scanPDF:====>>>> WARNING***: The initial guess value of Beam width is negative!!!!!!"<<std::endl
00758 <<"scanPDF:====>>>> Assigning beam width a starting value of "<<init_bw<<" cm"<<std::endl;
00759 init_bw=0.0200;
00760
00761 }
00762 }
00763
00764
00765 return init_bw;
00766
00767 }
00768
00769
00770 reco::BeamSpot BSFitter::Fit_d_z_likelihood(double *inipar, double *error_par) {
00771
00772 int tracksFailed=0;
00773
00774
00775 inipar[6]=scanPDF(inipar,tracksFailed,1);
00776 error_par[6]=(inipar[6])*0.20;
00777
00778
00779
00780
00781 scanPDF(inipar,tracksFailed,2);
00782
00783
00784
00785 fBSvector.clear();
00786 std::vector<BSTrkParameters>::const_iterator iparamBW = fBSvectorBW.begin();
00787 for( iparamBW = fBSvectorBW.begin(); iparamBW != fBSvectorBW.end(); ++iparamBW)
00788 { fBSvector.push_back(*iparamBW);
00789 }
00790
00791
00792 thePDF->SetPDFs("PDFGauss_d*PDFGauss_z");
00793 thePDF->SetData(fBSvector);
00794 MnUserParameters upar;
00795
00796 upar.Add("X0", inipar[0],error_par[0]);
00797 upar.Add("Y0", inipar[1],error_par[1]);
00798 upar.Add("Z0", inipar[2],error_par[2]);
00799 upar.Add("sigmaZ",inipar[3],error_par[3]);
00800 upar.Add("dxdz",inipar[4],error_par[4]);
00801 upar.Add("dydz",inipar[5],error_par[5]);
00802 upar.Add("BeamWidthX",inipar[6],error_par[6]);
00803
00804
00805 MnMigrad migrad(*thePDF, upar);
00806
00807 FunctionMinimum fmin = migrad();
00808
00809
00810
00811
00812 ff_minimum = fmin.Fval();
00813
00814
00815 bool ff_nfcn=fmin.HasReachedCallLimit();
00816 bool ff_cov=fmin.HasCovariance();
00817 bool testing=fmin.IsValid();
00818
00819
00820
00821 if( ! testing )
00822 {
00823 edm::LogWarning("BSFitter") <<"===========>>>>>** WARNING: MINUIT DID NOT CONVERGES PROPERLY !!!!!!"<<std::endl;
00824 if(ff_nfcn) edm::LogWarning("BSFitter") <<"===========>>>>>** WARNING: No. of Calls Exhausted"<<std::endl;
00825 if(!ff_cov) edm::LogWarning("BSFitter") <<"===========>>>>>** WARNING: Covariance did not found"<<std::endl;
00826 }
00827
00828 edm::LogInfo("BSFitter") <<"The Total # Tracks used for beam width fit = "<<(fBSvectorBW.size())<<std::endl;
00829
00830
00831
00832 double lastIter_pars[7];
00833
00834 for(int ip=0;ip<7;ip++){ lastIter_pars[ip]=fmin.Parameters().Vec()(ip);
00835 }
00836
00837
00838
00839 tracksFailed=0;
00840 scanPDF(lastIter_pars,tracksFailed,2);
00841
00842
00843 edm::LogWarning("BSFitter") <<"WARNING: # of tracks which have very low pdf value (pdf_d < 1.0e-05) are = "<<tracksFailed<<std::endl;
00844
00845
00846
00847
00848
00849
00850 reco::BeamSpot::CovarianceMatrix matrix;
00851
00852 for (int j = 0 ; j < 7 ; ++j) {
00853 for(int k = j ; k < 7 ; ++k) {
00854 matrix(j,k) = fmin.Error().Matrix()(j,k);
00855 }
00856 }
00857
00858
00859 return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
00860 fmin.Parameters().Vec()(1),
00861 fmin.Parameters().Vec()(2)),
00862 fmin.Parameters().Vec()(3),
00863 fmin.Parameters().Vec()(4),
00864 fmin.Parameters().Vec()(5),
00865 fmin.Parameters().Vec()(6),
00866
00867 matrix,
00868 fbeamtype );
00869 }
00870
00871
00872
00873 reco::BeamSpot BSFitter::Fit_dres_z_likelihood(double *inipar) {
00874
00875
00876 thePDF->SetPDFs("PDFGauss_d_resolution*PDFGauss_z");
00877 thePDF->SetData(fBSvector);
00878
00879 MnUserParameters upar;
00880 upar.Add("X0", inipar[0],0.001);
00881 upar.Add("Y0", inipar[1],0.001);
00882 upar.Add("Z0", inipar[2],0.001);
00883 upar.Add("sigmaZ",inipar[3],0.001);
00884 upar.Add("dxdz",inipar[4],0.001);
00885 upar.Add("dydz",inipar[5],0.001);
00886 upar.Add("BeamWidthX",inipar[6],0.0001);
00887 upar.Add("c0",0.0010,0.0001);
00888 upar.Add("c1",0.0090,0.0001);
00889
00890
00891 upar.Fix("BeamWidthX");
00892
00893
00894 MnMigrad migrad(*thePDF, upar);
00895
00896 FunctionMinimum fmin = migrad();
00897 ff_minimum = fmin.Fval();
00898
00899 reco::BeamSpot::CovarianceMatrix matrix;
00900
00901 for (int j = 0 ; j < 6 ; ++j) {
00902 for(int k = j ; k < 6 ; ++k) {
00903 matrix(j,k) = fmin.Error().Matrix()(j,k);
00904 }
00905 }
00906
00907
00908
00909
00910
00911
00912 fresolution_c0 = fmin.Parameters().Vec()(6);
00913 fresolution_c1 = fmin.Parameters().Vec()(7);
00914 fres_c0_err = sqrt( fmin.Error().Matrix()(6,6) );
00915 fres_c1_err = sqrt( fmin.Error().Matrix()(7,7) );
00916
00917 for (int j = 6 ; j < 8 ; ++j) {
00918 for(int k = 6 ; k < 8 ; ++k) {
00919 fres_matrix(j-6,k-6) = fmin.Error().Matrix()(j,k);
00920 }
00921 }
00922
00923 return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
00924 fmin.Parameters().Vec()(1),
00925 fmin.Parameters().Vec()(2)),
00926 fmin.Parameters().Vec()(3),
00927 fmin.Parameters().Vec()(4),
00928 fmin.Parameters().Vec()(5),
00929 inipar[6],
00930 matrix,
00931 fbeamtype );
00932 }
00933
00934
00935