CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BSFitter.cc
Go to the documentation of this file.
1 
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"
19 //#include "CLHEP/config/CLHEP.h"
20 
21 // C++ standard
22 #include <vector>
23 #include <cmath>
24 
25 // CMS
30 
31 // ROOT
32 #include "TMatrixD.h"
33 #include "TMatrixDSym.h"
34 #include "TDecompBK.h"
35 #include "TH1.h"
36 #include "TF1.h"
37 
38 using namespace ROOT::Minuit2;
39 
40 
41 //_____________________________________________________________________
43  fbeamtype = reco::BeamSpot::Unknown;
44 }
45 
46 //_____________________________________________________________________
47 BSFitter::BSFitter( const std:: vector< BSTrkParameters > &BSvector ) {
48 
49  ffit_type = "default";
50  ffit_variable = "default";
51 
52  fBSvector = BSvector;
53 
54  fsqrt2pi = sqrt(2.* TMath::Pi());
55 
56  fpar_name[0] = "z0 ";
57  fpar_name[1] = "SigmaZ0 ";
58  fpar_name[2] = "X0 ";
59  fpar_name[3] = "Y0 ";
60  fpar_name[4] = "dxdz ";
61  fpar_name[5] = "dydz ";
62  fpar_name[6] = "SigmaBeam ";
63 
64  //if (theGausszFcn == 0 ) {
65  thePDF = new BSpdfsFcn();
66 
67 
68 //}
69  //if (theFitter == 0 ) {
70 
71  theFitter = new VariableMetricMinimizer();
72 
73  //}
74 
75  fapplyd0cut = false;
76  fapplychi2cut = false;
77  ftmprow = 0;
78  ftmp.ResizeTo(4,1);
79  ftmp.Zero();
80  fnthite=0;
81  fMaxZ = 50.; //cm
82  fconvergence = 0.5; // stop fit when 50% of the input collection has been removed.
83  fminNtrks = 100;
84  finputBeamWidth = -1; // no input
85 
86  h1z = new TH1F("h1z","z distribution",200,-fMaxZ, fMaxZ);
87 
88 }
89 
90 //______________________________________________________________________
92 {
93  //delete fBSvector;
94  delete thePDF;
95  delete theFitter;
96 }
97 
98 
99 //______________________________________________________________________
101 
102  return this->Fit(0);
103 
104 }
105 
106 //______________________________________________________________________
107 reco::BeamSpot BSFitter::Fit(double *inipar = 0) {
108  fbeamtype = reco::BeamSpot::Unknown;
109  if ( ffit_variable == "z" ) {
110 
111  if ( ffit_type == "chi2" ) {
112 
113  return Fit_z_chi2(inipar);
114 
115  } else if ( ffit_type == "likelihood" ) {
116 
117  return Fit_z_likelihood(inipar);
118 
119  } else if ( ffit_type == "combined" ) {
120 
121  reco::BeamSpot tmp_beamspot = Fit_z_chi2(inipar);
122  double tmp_par[2] = {tmp_beamspot.z0(), tmp_beamspot.sigmaZ()};
123  return Fit_z_likelihood(tmp_par);
124 
125  } else {
126 
127  throw cms::Exception("LogicError")
128  << "Error in BeamSpotProducer/BSFitter: "
129  << "Illegal fit type, options are chi2,likelihood or combined(ie. first chi2 then likelihood)";
130 
131  }
132  } else if ( ffit_variable == "d" ) {
133 
134  if ( ffit_type == "d0phi" ) {
135  this->d0phi_Init();
136  return Fit_d0phi();
137 
138  } else if ( ffit_type == "likelihood" ) {
139 
140  return Fit_d_likelihood(inipar);
141 
142  } else if ( ffit_type == "combined" ) {
143 
144  this->d0phi_Init();
145  reco::BeamSpot tmp_beamspot = Fit_d0phi();
146  double tmp_par[4] = {tmp_beamspot.x0(), tmp_beamspot.y0(), tmp_beamspot.dxdz(), tmp_beamspot.dydz()};
147  return Fit_d_likelihood(tmp_par);
148 
149  } else {
150  throw cms::Exception("LogicError")
151  << "Error in BeamSpotProducer/BSFitter: "
152  << "Illegal fit type, options are d0phi, likelihood or combined";
153  }
154  } else if ( ffit_variable == "d*z" || ffit_variable == "default" ) {
155 
156  if ( ffit_type == "likelihood" || ffit_type == "default" ) {
157 
159  // we are now fitting Z inside d0phi fitter
160  // first fit z distribution using a chi2 fit
161  //reco::BeamSpot tmp_z = Fit_z_chi2(inipar);
162  //for (int j = 2 ; j < 4 ; ++j) {
163  //for(int k = j ; k < 4 ; ++k) {
164  // matrix(j,k) = tmp_z.covariance()(j,k);
165  //}
166  //}
167 
168  // use d0-phi algorithm to extract transverse position
169  this->d0phi_Init();
170  //reco::BeamSpot tmp_d0phi= Fit_d0phi(); // change to iterative procedure:
171  this->Setd0Cut_d0phi(4.0);
172  reco::BeamSpot tmp_d0phi= Fit_ited0phi();
173 
174  //for (int j = 0 ; j < 2 ; ++j) {
175  // for(int k = j ; k < 2 ; ++k) {
176  // matrix(j,k) = tmp_d0phi.covariance()(j,k);
177  //}
178  //}
179  // slopes
180  //for (int j = 4 ; j < 6 ; ++j) {
181  // for(int k = j ; k < 6 ; ++k) {
182  // matrix(j,k) = tmp_d0phi.covariance()(j,k);
183  // }
184  //}
185 
186 
187  // put everything into one object
188  reco::BeamSpot spot(reco::BeamSpot::Point(tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_d0phi.z0()),
189  tmp_d0phi.sigmaZ(),
190  tmp_d0phi.dxdz(),
191  tmp_d0phi.dydz(),
192  0.,
193  tmp_d0phi.covariance(),
194  fbeamtype );
195 
196 
197 
198  //reco::BeamSpot tmp_z = Fit_z_chi2(inipar);
199 
200  //reco::BeamSpot tmp_d0phi = Fit_d0phi();
201 
202  // log-likelihood fit
203  if (ffit_type == "likelihood") {
204  double tmp_par[7] = {tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_d0phi.z0(),
205  tmp_d0phi.sigmaZ(), tmp_d0phi.dxdz(), tmp_d0phi.dydz(),0.0};
206 
207  double tmp_error_par[7];
208  for(int s=0;s<6;s++){ tmp_error_par[s] = pow( tmp_d0phi.covariance()(s,s),0.5);}
209  tmp_error_par[6]=0.0;
210 
211  reco::BeamSpot tmp_lh = Fit_d_z_likelihood(tmp_par,tmp_error_par);
212 
213  if (edm::isNotFinite(ff_minimum)) {
214  edm::LogWarning("BSFitter") << "BSFitter: Result is non physical. Log-Likelihood fit to extract beam width did not converge." << std::endl;
216  return tmp_lh;
217  }
218  return tmp_lh;
219 
220  } else {
221 
222  edm::LogInfo("BSFitter") << "default track-based fit does not extract beam width." << std::endl;
223  return spot;
224  }
225 
226 
227  } else if ( ffit_type == "resolution" ) {
228 
229  reco::BeamSpot tmp_z = Fit_z_chi2(inipar);
230  this->d0phi_Init();
231  reco::BeamSpot tmp_d0phi = Fit_d0phi();
232 
233  double tmp_par[7] = {tmp_d0phi.x0(), tmp_d0phi.y0(), tmp_z.z0(),
234  tmp_z.sigmaZ(), tmp_d0phi.dxdz(), tmp_d0phi.dydz(),0.0};
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;
238 
239  reco::BeamSpot tmp_beam = Fit_d_z_likelihood(tmp_par,tmp_error_par);
240 
241  double tmp_par2[7] = {tmp_beam.x0(), tmp_beam.y0(), tmp_beam.z0(),
242  tmp_beam.sigmaZ(), tmp_beam.dxdz(), tmp_beam.dydz(),
243  tmp_beam.BeamWidthX()};
244 
245  reco::BeamSpot tmp_lh = Fit_dres_z_likelihood(tmp_par2);
246 
247  if (edm::isNotFinite(ff_minimum)) {
248 
249  edm::LogWarning("BSFitter") << "Result is non physical. Log-Likelihood fit did not converge." << std::endl;
251  return tmp_lh;
252  }
253  return tmp_lh;
254 
255  } else {
256 
257  throw cms::Exception("LogicError")
258  << "Error in BeamSpotProducer/BSFitter: "
259  << "Illegal fit type, options are likelihood or resolution";
260  }
261  } else {
262 
263  throw cms::Exception("LogicError")
264  << "Error in BeamSpotProducer/BSFitter: "
265  << "Illegal variable type, options are \"z\", \"d\", or \"d*z\"";
266  }
267 
268 
269 }
270 
271 //______________________________________________________________________
273 
274  //std::cout << "Fit_z(double *) called" << std::endl;
275  //std::cout << "inipar[0]= " << inipar[0] << std::endl;
276  //std::cout << "inipar[1]= " << inipar[1] << std::endl;
277 
278  std::vector<double> par(2,0);
279  std::vector<double> err(2,0);
280 
281  par.push_back(0.0);
282  par.push_back(7.0);
283  err.push_back(0.0001);
284  err.push_back(0.0001);
285  //par[0] = 0.0; err[0] = 0.0;
286  //par[1] = 7.0; err[1] = 0.0;
287 
288  thePDF->SetPDFs("PDFGauss_z");
289  thePDF->SetData(fBSvector);
290  //std::cout << "data loaded"<< std::endl;
291 
292  //FunctionMinimum fmin = theFitter->Minimize(*theGausszFcn, par, err, 1, 500, 0.1);
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);
298 
299  MnMigrad migrad(*thePDF, upar);
300 
301  FunctionMinimum fmin = migrad();
302  ff_minimum = fmin.Fval();
303  //std::cout << " eval= " << ff_minimum
304  // << "/n params[0]= " << fmin.Parameters().Vec()(0) << std::endl;
305 
306  /*
307  TMinuit *gmMinuit = new TMinuit(2);
308 
309  //gmMinuit->SetFCN(z_fcn);
310  gmMinuit->SetFCN(myFitz_fcn);
311 
312 
313  int ierflg = 0;
314  double step[2] = {0.001,0.001};
315 
316  for (int i = 0; i<2; i++) {
317  gmMinuit->mnparm(i,fpar_name[i].c_str(),inipar[i],step[i],0,0,ierflg);
318  }
319  gmMinuit->Migrad();
320  */
322 
323  for (int j = 2 ; j < 4 ; ++j) {
324  for(int k = j ; k < 4 ; ++k) {
325  matrix(j,k) = fmin.Error().Matrix()(j,k);
326  }
327  }
328 
330  0.,
331  fmin.Parameters().Vec()(2)),
332  fmin.Parameters().Vec()(3),
333  0.,
334  0.,
335  0.,
336  matrix,
337  fbeamtype );
338 }
339 
340 //______________________________________________________________________
342 
343  // N.B. this fit is not performed anymore but now
344  // Z is fitted in the same track set used in the d0-phi fit after
345  // each iteration
346 
347 
348  //std::cout << "Fit_z_chi2() called" << std::endl;
349  // FIXME: include whole tracker z length for the time being
350  // ==> add protection and z0 cut
351  h1z = new TH1F("h1z","z distribution",200,-fMaxZ, fMaxZ);
352 
353  std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
354 
355  // HERE check size of track vector
356 
357  for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam) {
358 
359  h1z->Fill( iparam->z0() );
360  //std::cout<<"z0="<<iparam->z0()<<"; sigZ0="<<iparam->sigz0()<<std::endl;
361  }
362 
363  h1z->Fit("gaus","QLM0");
364  //std::cout << "fitted "<< std::endl;
365 
366  TF1 *fgaus = h1z->GetFunction("gaus");
367  //std::cout << "got function" << std::endl;
368  double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
369  //std::cout<<"Debug fpar[2] = (" <<fpar[0]<<","<<fpar[1]<<")"<<std::endl;
371  // add matrix values.
372  matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
373  matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
374 
375  //delete h1z;
376 
378  0.,
379  fpar[0]),
380  fpar[1],
381  0.,
382  0.,
383  0.,
384  matrix,
385  fbeamtype );
386 
387 
388 }
389 
390 //______________________________________________________________________
392 
393  this->d0phi_Init();
394  edm::LogInfo("BSFitter") << "number of total input tracks: " << fBSvector.size() << std::endl;
395 
396  reco::BeamSpot theanswer;
397 
398  if ( (int)fBSvector.size() <= fminNtrks ) {
399  edm::LogWarning("BSFitter") << "need at least " << fminNtrks << " tracks to run beamline fitter." << std::endl;
400  fbeamtype = reco::BeamSpot::Fake;
401  theanswer.setType(fbeamtype);
402  return theanswer;
403  }
404 
405  theanswer = Fit_d0phi(); //get initial ftmp and ftmprow
406  if ( goodfit ) fnthite++;
407  //std::cout << "Initial tempanswer (iteration 0): " << theanswer << std::endl;
408 
409  reco::BeamSpot preanswer = theanswer;
410 
411  while ( goodfit &&
412  ftmprow > fconvergence * fBSvector.size() &&
413  ftmprow > fminNtrks ) {
414 
415  theanswer = Fit_d0phi();
416  fd0cut /= 1.5;
417  fchi2cut /= 1.5;
418  if ( goodfit &&
419  ftmprow > fconvergence * fBSvector.size() &&
420  ftmprow > fminNtrks ) {
421  preanswer = theanswer;
422  //std::cout << "Iteration " << fnthite << ": " << preanswer << std::endl;
423  fnthite++;
424  }
425  }
426  // FIXME: return fit results from previous iteration for both bad fit and for >50% tracks thrown away
427  //std::cout << "The last iteration, theanswer: " << theanswer << std::endl;
428  theanswer = preanswer;
429  //std::cout << "Use previous results from iteration #" << ( fnthite > 0 ? fnthite-1 : 0 ) << std::endl;
430  //if ( fnthite > 1 ) std::cout << theanswer << std::endl;
431 
432  edm::LogInfo("BSFitter") << "Total number of successful iterations = " << ( goodfit ? (fnthite+1) : fnthite ) << std::endl;
433  if (goodfit) {
434  fbeamtype = reco::BeamSpot::Tracker;
435  theanswer.setType(fbeamtype);
436  }
437  else {
438  edm::LogWarning("BSFitter") << "Fit doesn't converge!!!" << std::endl;
439  fbeamtype = reco::BeamSpot::Unknown;
440  theanswer.setType(fbeamtype);
441  }
442  return theanswer;
443 }
444 
445 
446 //______________________________________________________________________
448 
449  //LogDebug ("BSFitter") << " we will use " << fBSvector.size() << " tracks.";
450  if (fnthite > 0) edm::LogInfo("BSFitter") << " number of tracks used: " << ftmprow << std::endl;
451  //std::cout << " ftmp = matrix("<<ftmp.GetNrows()<<","<<ftmp.GetNcols()<<")"<<std::endl;
452  //std::cout << " ftmp(0,0)="<<ftmp(0,0)<<std::endl;
453  //std::cout << " ftmp(1,0)="<<ftmp(1,0)<<std::endl;
454  //std::cout << " ftmp(2,0)="<<ftmp(2,0)<<std::endl;
455  //std::cout << " ftmp(3,0)="<<ftmp(3,0)<<std::endl;
456 
457  h1z->Reset();
458 
459 
460  TMatrixD x_result(4,1);
461  TMatrixDSym V_result(4);
462 
463  TMatrixDSym Vint(4);
464  TMatrixD b(4,1);
465 
466  //Double_t weightsum = 0;
467 
468  Vint.Zero();
469  b.Zero();
470 
471  TMatrixD g(4,1);
472  TMatrixDSym temp(4);
473 
474  std::vector<BSTrkParameters>::iterator iparam = fBSvector.begin();
475  ftmprow=0;
476 
477 
478  //edm::LogInfo ("BSFitter") << " test";
479 
480  //std::cout << "BSFitter: fit" << std::endl;
481 
482  for( iparam = fBSvector.begin() ;
483  iparam != fBSvector.end() ; ++iparam) {
484 
485 
486  //if(i->weight2 == 0) continue;
487 
488  //if (ftmprow==0) {
489  //std::cout << "d0=" << iparam->d0() << " sigd0=" << iparam->sigd0()
490  //<< " phi0="<< iparam->phi0() << " z0=" << iparam->z0() << std::endl;
491  //std::cout << "d0phi_d0=" << iparam->d0phi_d0() << " d0phi_chi2="<<iparam->d0phi_chi2() << std::endl;
492  //}
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);
497 
498 
499  // average transverse beam width
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; }
503 
504  //double sigma2 = sigmabeam2 + (iparam->sigd0())* (iparam->sigd0()) / iparam->weight2;
505  // this should be 2*sigmabeam2?
506  double sigma2 = sigmabeam2 + (iparam->sigd0())* (iparam->sigd0());
507 
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);
514 
515  bool pass = true;
516  if (fapplyd0cut && fnthite>0 ) {
517  if ( std::abs(iparam->d0() - dcor(0,0)) > fd0cut ) pass = false;
518 
519  }
520  if (fapplychi2cut && fnthite>0 ) {
521  if ( chi2tmp > fchi2cut ) pass = false;
522 
523  }
524 
525  if (pass) {
526  temp.Zero();
527  for(int j = 0 ; j < 4 ; ++j) {
528  for(int k = j ; k < 4 ; ++k) {
529  temp(j,k) += g(j,0) * g(k,0);
530  }
531  }
532 
533 
534  Vint += (temp * (1 / sigma2));
535  b += (iparam->d0() / sigma2 * g);
536  //weightsum += sqrt(i->weight2);
537  ftmprow++;
538  h1z->Fill( iparam->z0() );
539  }
540 
541 
542  }
543  Double_t determinant;
544  TDecompBK bk(Vint);
545  bk.SetTol(1e-11); //FIXME: find a better way to solve x_result
546  if (!bk.Decompose()) {
547  goodfit = false;
548  edm::LogWarning("BSFitter")
549  << "Decomposition failed, matrix singular ?" << std::endl
550  << "condition number = " << bk.Condition() << std::endl;
551  }
552  else {
553  V_result = Vint.InvertFast(&determinant);
554  x_result = V_result * b;
555  }
556  // for(int j = 0 ; j < 4 ; ++j) {
557  // for(int k = 0 ; k < 4 ; ++k) {
558  // std::cout<<"V_result("<<j<<","<<k<<")="<<V_result(j,k)<<std::endl;
559  // }
560  // }
561  // for (int j=0;j<4;++j){
562  // std::cout<<"x_result("<<j<<",0)="<<x_result(j,0)<<std::endl;
563  // }
564  //LogDebug ("BSFitter") << " d0-phi fit done.";
565  //std::cout<< " d0-phi fit done." << std::endl;
566 
567  h1z->Fit("gaus","QLM0","",h1z->GetMean() -2.*h1z->GetRMS(),h1z->GetMean() +2.*h1z->GetRMS());
568 
569  //std::cout << "fitted "<< std::endl;
570  TF1 *fgaus = h1z->GetFunction("gaus");
571  //std::cout << "got function" << std::endl;
572  if (!fgaus){
573  edm::LogError("NoBeamSpotFit")<<"gaussian fit failed. no BS d0 fit";
574  return reco::BeamSpot();
575  }
576  double fpar[2] = {fgaus->GetParameter(1), fgaus->GetParameter(2) };
577 
579  // first two parameters
580  for (int j = 0 ; j < 2 ; ++j) {
581  for(int k = j ; k < 2 ; ++k) {
582  matrix(j,k) = V_result(j,k);
583  }
584  }
585  // slope parameters
586  for (int j = 4 ; j < 6 ; ++j) {
587  for(int k = j ; k < 6 ; ++k) {
588  matrix(j,k) = V_result(j-2,k-2);
589  }
590  }
591 
592  // Z0 and sigmaZ
593  matrix(2,2) = fgaus->GetParError(1) * fgaus->GetParError(1);
594  matrix(3,3) = fgaus->GetParError(2) * fgaus->GetParError(2);
595 
596  ftmp = x_result;
597 
598  // x0 and y0 are *not* x,y at z=0, but actually at z=0
599  // to correct for this, we need to translate them to z=z0
600  // using the measured slopes
601  //
602  double x0tmp = x_result(0,0);
603  double y0tmp = x_result(1,0);
604 
605  x0tmp += x_result(2,0)*fpar[0];
606  y0tmp += x_result(3,0)*fpar[0];
607 
608 
610  y0tmp,
611  fpar[0]),
612  fpar[1],
613  x_result(2,0),
614  x_result(3,0),
615  0.,
616  matrix,
617  fbeamtype );
618 
619 }
620 
621 
622 //______________________________________________________________________
623 void BSFitter::Setd0Cut_d0phi(double d0cut) {
624 
625  fapplyd0cut = true;
626 
627  //fBSforCuts = BSfitted;
628  fd0cut = d0cut;
629 }
630 
631 //______________________________________________________________________
632 void BSFitter::SetChi2Cut_d0phi(double chi2cut) {
633 
634  fapplychi2cut = true;
635 
636  //fBSforCuts = BSfitted;
637  fchi2cut = chi2cut;
638 }
639 
640 //______________________________________________________________________
642 
643 
644  thePDF->SetPDFs("PDFGauss_d");
645  thePDF->SetData(fBSvector);
646 
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);
654 
655 
656  MnMigrad migrad(*thePDF, upar);
657 
658  FunctionMinimum fmin = migrad();
659  ff_minimum = fmin.Fval();
660 
662  for (int j = 0 ; j < 6 ; ++j) {
663  for(int k = j ; k < 6 ; ++k) {
664  matrix(j,k) = fmin.Error().Matrix()(j,k);
665  }
666  }
667 
668  return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
669  fmin.Parameters().Vec()(1),
670  0.),
671  0.,
672  fmin.Parameters().Vec()(4),
673  fmin.Parameters().Vec()(5),
674  0.,
675  matrix,
676  fbeamtype );
677 }
678 //______________________________________________________________________
679 double BSFitter::scanPDF(double *init_pars, int & tracksfixed, int option){
680 
681  if(option==1)init_pars[6]=0.0005; //starting value for any given configuration
682 
683  //local vairables with initial values
684  double fsqrt2pi=0.0;
685  double d_sig=0.0;
686  double d_dprime=0.0;
687  double d_result=0.0;
688  double z_sig=0.0;
689  double z_result=0.0;
690  double function=0.0;
691  double tot_pdf=0.0;
692  double last_minvalue=1.0e+10;
693  double init_bw=-99.99;
694  int iters=0;
695 
696  //used to remove tracks if far away from bs by this
697  double DeltadCut=0.1000;
698  if(init_pars[6]<0.0200){DeltadCut=0.0900; } //worked for high 2.36TeV
699  if(init_pars[6]<0.0100){DeltadCut=0.0700;} //just a guesss for 7 TeV but one should scan for actual values
700 
701 
702 std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin();
703 
704 
705 if(option==1)iters=500;
706 if(option==2)iters=1;
707 
708 for(int p=0;p<iters;p++){
709 
710  if(iters==500)init_pars[6]+=0.0002;
711  tracksfixed=0;
712 
713 for( iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam)
714  {
715  fsqrt2pi = sqrt(2.* TMath::Pi());
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()) ) );
719 
720  //***Remove tracks before the fit which gives low pdf values to blow up the pdf
721  if(std::abs(d_dprime)<DeltadCut && option==2){ fBSvectorBW.push_back(*iparam);}
722 
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;
727 
728  //for those trcks which gives problems due to very tiny pdf_d values.
729  //Update: This protection will NOT be used with the dprime cut above but still kept here to get
730  // the intial value of beam width reasonably
731  //A warning will appear if there were any tracks with < 10^-5 for pdf_d so that (d-dprime) cut can be lowered
732  if(d_result < 1.0e-05){ tot_pdf=z_result*1.0e-05;
733  //if(option==2)std::cout<<"last Iter d-d' = "<<(std::abs(d_dprime))<<std::endl;
734  tracksfixed++; }
735 
736  function = function + log(tot_pdf);
737  tot_pdf=0.0;
738 
739 
740  }//loop over tracks
741 
742 
743  function= -2.0*function;
744  if(function<last_minvalue){init_bw=init_pars[6];
745  last_minvalue=function; }
746  function=0.0;
747  }//loop over beam width
748 
749  if(init_bw>0) {
750  init_bw=init_bw+(0.20*init_bw); //start with 20 % more
751 
752  }
753  else{
754 
755  if(option==1){
756  edm::LogWarning("BSFitter")
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;
759  init_bw=0.0200;
760 
761  }
762  }
763 
764 
765  return init_bw;
766 
767 }
768 
769 //________________________________________________________________________________
770 reco::BeamSpot BSFitter::Fit_d_z_likelihood(double *inipar, double *error_par) {
771 
772  int tracksFailed=0;
773 
774  //estimate first guess of beam width and tame 20% extra of it to start
775  inipar[6]=scanPDF(inipar,tracksFailed,1);
776  error_par[6]=(inipar[6])*0.20;
777 
778 
779  //Here remove the tracks which give low pdf and fill into a new vector
780  //std::cout<<"Size of Old vector = "<<(fBSvector.size())<<std::endl;
781  /* double junk= */ scanPDF(inipar,tracksFailed,2);
782  //std::cout<<"Size of New vector = "<<(fBSvectorBW.size())<<std::endl;
783 
784  //Refill the fBSVector again with new sets of tracks
785  fBSvector.clear();
786  std::vector<BSTrkParameters>::const_iterator iparamBW = fBSvectorBW.begin();
787  for( iparamBW = fBSvectorBW.begin(); iparamBW != fBSvectorBW.end(); ++iparamBW)
788  { fBSvector.push_back(*iparamBW);
789  }
790 
791 
792  thePDF->SetPDFs("PDFGauss_d*PDFGauss_z");
793  thePDF->SetData(fBSvector);
794  MnUserParameters upar;
795 
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]);
803 
804 
805  MnMigrad migrad(*thePDF, upar);
806 
807  FunctionMinimum fmin = migrad();
808 
809  // std::cout<<"-----how the fit evoves------"<<std::endl;
810  // std::cout<<fmin<<std::endl;
811 
812  ff_minimum = fmin.Fval();
813 
814 
815  bool ff_nfcn=fmin.HasReachedCallLimit();
816  bool ff_cov=fmin.HasCovariance();
817  bool testing=fmin.IsValid();
818 
819 
820  //Print WARNINGS if minimum did not converged
821  if( ! testing )
822  {
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;
826  }
827 
828  edm::LogInfo("BSFitter") <<"The Total # Tracks used for beam width fit = "<<(fBSvectorBW.size())<<std::endl;
829 
830 
831  //Checks after fit is performed
832  double lastIter_pars[7];
833 
834  for(int ip=0;ip<7;ip++){ lastIter_pars[ip]=fmin.Parameters().Vec()(ip);
835  }
836 
837 
838 
839  tracksFailed=0;
840  /* double lastIter_scan= */ scanPDF(lastIter_pars,tracksFailed,2);
841 
842 
843  edm::LogWarning("BSFitter") <<"WARNING: # of tracks which have very low pdf value (pdf_d < 1.0e-05) are = "<<tracksFailed<<std::endl;
844 
845 
846 
847  //std::cout << " eval= " << ff_minimum
848  // << "/n params[0]= " << fmin.Parameters().Vec()(0) << std::endl;
849 
851 
852  for (int j = 0 ; j < 7 ; ++j) {
853  for(int k = j ; k < 7 ; ++k) {
854  matrix(j,k) = fmin.Error().Matrix()(j,k);
855  }
856  }
857 
858 
859  return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
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),
866 
867  matrix,
868  fbeamtype );
869 }
870 
871 
872 //______________________________________________________________________
874 
875 
876  thePDF->SetPDFs("PDFGauss_d_resolution*PDFGauss_z");
877  thePDF->SetData(fBSvector);
878 
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);
889 
890  // fix beam width
891  upar.Fix("BeamWidthX");
892  // number of parameters in fit are 9-1 = 8
893 
894  MnMigrad migrad(*thePDF, upar);
895 
896  FunctionMinimum fmin = migrad();
897  ff_minimum = fmin.Fval();
898 
900 
901  for (int j = 0 ; j < 6 ; ++j) {
902  for(int k = j ; k < 6 ; ++k) {
903  matrix(j,k) = fmin.Error().Matrix()(j,k);
904  }
905  }
906 
907  //std::cout << " fill resolution values" << std::endl;
908  //std::cout << " matrix size= " << fmin.Error().Matrix().size() << std::endl;
909  //std::cout << " vec(6)="<< fmin.Parameters().Vec()(6) << std::endl;
910  //std::cout << " vec(7)="<< fmin.Parameters().Vec()(7) << std::endl;
911 
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) );
916 
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);
920  }
921  }
922 
923  return reco::BeamSpot( reco::BeamSpot::Point(fmin.Parameters().Vec()(0),
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),
929  inipar[6],
930  matrix,
931  fbeamtype );
932 }
933 
934 
935 
reco::BeamSpot Fit_d_likelihood(double *inipar)
Definition: BSFitter.cc:641
const double Pi
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:31
double z0() const
z coordinate
Definition: BeamSpot.h:68
virtual ~BSFitter()
Definition: BSFitter.cc:91
reco::BeamSpot Fit()
Definition: BSFitter.cc:100
reco::BeamSpot Fit_z_likelihood(double *inipar)
Definition: BSFitter.cc:272
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:29
double scanPDF(double *init_pars, int &tracksFailed, int option)
Definition: BSFitter.cc:679
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
Definition: Activities.doc:4
reco::BeamSpot Fit_dres_z_likelihood(double *inipar)
Definition: BSFitter.cc:873
void setType(BeamType type)
set beam type
Definition: BeamSpot.h:131
double dydz() const
dydz slope
Definition: BeamSpot.h:84
bool isNotFinite(T x)
Definition: isFinite.h:10
Definition: Fit.h:34
T sqrt(T t)
Definition: SSEVec.h:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
BSFitter()
Definition: BSFitter.cc:42
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:86
double dxdz() const
dxdz slope
Definition: BeamSpot.h:82
int k[5][pyjets_maxn]
reco::BeamSpot Fit_ited0phi()
Definition: BSFitter.cc:391
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
double b
Definition: hdecay.h:120
reco::BeamSpot Fit_z_chi2(double *inipar)
Definition: BSFitter.cc:341
void Setd0Cut_d0phi(double d0cut)
Definition: BSFitter.cc:623
double covariance(int i, int j) const
(i,j)-th element of error matrix
Definition: BeamSpot.h:112
double y0() const
y coordinate
Definition: BeamSpot.h:66
void SetChi2Cut_d0phi(double chi2cut)
Definition: BSFitter.cc:632
reco::BeamSpot Fit_d0phi()
Definition: BSFitter.cc:447
reco::BeamSpot Fit_d_z_likelihood(double *inipar, double *error_par)
Definition: BSFitter.cc:770
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
double x0() const
x coordinate
Definition: BeamSpot.h:64