CMS 3D CMS Logo

PF_PU_AssoMapAlgos.cc
Go to the documentation of this file.
1 
3 
5 
7 
12 
14 
18 
19 using namespace edm;
20 using namespace std;
21 using namespace reco;
22 
23 /*************************************************************************************/
24 /* dedicated constructor for the algorithms */
25 /*************************************************************************************/
26 
28  : token_bField_(iC.esConsumes()),
29  token_TrackingGeometry_(iC.esConsumes()),
30  maxNumWarnings_(3),
31  numWarnings_(0)
32 
33 {
34  input_MaxNumAssociations_ = iConfig.getParameter<int>("MaxNumberOfAssociations");
35 
36  token_VertexCollection_ = iC.consumes<VertexCollection>(iConfig.getParameter<InputTag>("VertexCollection"));
37 
38  token_BeamSpot_ = iC.consumes<BeamSpot>(iConfig.getParameter<InputTag>("BeamSpot"));
39 
40  input_doReassociation_ = iConfig.getParameter<bool>("doReassociation");
41  cleanedColls_ = iConfig.getParameter<bool>("GetCleanedCollections");
42 
44  iC.consumes<ConversionCollection>(iConfig.getParameter<InputTag>("ConversionsCollection"));
45 
47  iC.consumes<VertexCompositeCandidateCollection>(iConfig.getParameter<InputTag>("V0KshortCollection"));
49  iC.consumes<VertexCompositeCandidateCollection>(iConfig.getParameter<InputTag>("V0LambdaCollection"));
50 
52  iC.consumes<PFDisplacedVertexCollection>(iConfig.getParameter<InputTag>("NIVertexCollection"));
53 
54  input_FinalAssociation_ = iConfig.getUntrackedParameter<int>("FinalAssociation", 0);
55 
56  ignoremissingpfcollection_ = iConfig.getParameter<bool>("ignoreMissingCollection");
57 
58  input_nTrack_ = iConfig.getParameter<double>("nTrackWeight");
59 }
60 
61 /*************************************************************************************/
62 /* get all needed collections at the beginning */
63 /*************************************************************************************/
64 
66  //get the offline beam spot
67  iEvent.getByToken(token_BeamSpot_, beamspotH);
68 
69  //get the conversion collection for the gamma conversions
72 
73  //get the vertex composite candidate collection for the Kshort's
76 
77  //get the vertex composite candidate collection for the Lambda's
80 
81  //get the displaced vertex collection for nuclear interactions
82  //create a new bool, true if no displaced vertex collection is in the event, mostly for AOD
83  missingColls = false;
86  missingColls = true;
87 
89  LogWarning("PF_PU_AssoMapAlgos::GetInputCollections")
90  << "No Extra objects available in input file --> skipping reconstruction of displaced vertices !!" << endl;
91  ++numWarnings_;
92  }
93  }
94  } else {
96  }
97 
98  //get the input vertex collection
100 
101  bFieldH = iSetup.getHandle(token_bField_);
103 }
104 
105 /*************************************************************************************/
106 /* create the track-to-vertex and vertex-to-track maps in one go */
107 /*************************************************************************************/
108 std::pair<std::unique_ptr<TrackToVertexAssMap>, std::unique_ptr<VertexToTrackAssMap>>
110  unique_ptr<TrackToVertexAssMap> track2vertex(new TrackToVertexAssMap(vtxcollH, trkcollH));
111  unique_ptr<VertexToTrackAssMap> vertex2track(new VertexToTrackAssMap(trkcollH, vtxcollH));
112 
113  int num_vertices = vtxcollH->size();
114  if (num_vertices < input_MaxNumAssociations_)
115  input_MaxNumAssociations_ = num_vertices;
116  vector<VertexRef> vtxColl_help;
117  if (input_MaxNumAssociations_ == 1)
118  vtxColl_help = CreateVertexVector(vtxcollH);
119 
120  //loop over all tracks of the track collection
121  for (size_t idxTrack = 0; idxTrack < trkcollH->size(); ++idxTrack) {
122  TrackRef trackref = TrackRef(trkcollH, idxTrack);
123 
124  TransientTrack transtrk(trackref, &(*bFieldH));
125  transtrk.setBeamSpot(*beamspotH);
127 
129  vtxColl_help = CreateVertexVector(vtxcollH);
130 
131  for (int assoc_ite = 0; assoc_ite < input_MaxNumAssociations_; ++assoc_ite) {
132  VertexStepPair assocVtx =
133  FindAssociation(trackref, vtxColl_help, bFieldH, trackingGeometryH, beamspotH, assoc_ite);
134  int step = assocVtx.second;
135  double distance = (IPTools::absoluteImpactParameter3D(transtrk, *(assocVtx.first))).second.value();
136 
137  int quality = DefineQuality(assoc_ite, step, distance);
138 
139  //std::cout << "associating track: Pt = " << trackref->pt() << ","
140  // << " eta = " << trackref->eta() << ", phi = " << trackref->phi()
141  // << " to vertex: z = " << associatedVertex.first->position().z() << " with quality q = " << quality << std::endl;
142 
143  // Insert the best vertex and the pair of track and the quality of this association in the map
144  track2vertex->insert(assocVtx.first, make_pair(trackref, quality));
145  vertex2track->insert(trackref, make_pair(assocVtx.first, quality));
146 
147  //cleanup only if multiple iterations are made
149  PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, assocVtx.first);
150  }
151  }
152 
153  return {std::move(track2vertex), std::move(vertex2track)};
154 }
155 
156 /*************************************************************************************/
157 /* create the track to vertex association map */
158 /*************************************************************************************/
159 
160 std::unique_ptr<TrackToVertexAssMap> PF_PU_AssoMapAlgos::CreateTrackToVertexMap(
162  return createMappings(trkcollH).first;
163 }
164 
165 /*************************************************************************************/
166 /* create the vertex to track association map */
167 /*************************************************************************************/
168 
169 std::unique_ptr<VertexToTrackAssMap> PF_PU_AssoMapAlgos::CreateVertexToTrackMap(
171  return createMappings(trkcollH).second;
172 }
173 
174 /*****************************************************************************************/
175 /* function to sort the vertices in the AssociationMap by the sum of (pT - pT_Error)**2 */
176 /*****************************************************************************************/
177 
178 unique_ptr<TrackToVertexAssMap> PF_PU_AssoMapAlgos::SortAssociationMap(TrackToVertexAssMap* trackvertexassInput,
180  //create a new TrackVertexAssMap for the Output which will be sorted
181  unique_ptr<TrackToVertexAssMap> trackvertexassOutput(new TrackToVertexAssMap(vtxcollH, trkcollH));
182 
183  //Create and fill a vector of pairs of vertex and the summed (pT-pT_Error)**2 of the tracks associated to the vertex
184  VertexPtsumVector vertexptsumvector;
185 
186  //loop over all vertices in the association map
187  for (TrackToVertexAssMap::const_iterator assomap_ite = trackvertexassInput->begin();
188  assomap_ite != trackvertexassInput->end();
189  assomap_ite++) {
190  const VertexRef assomap_vertexref = assomap_ite->key;
191  const TrackQualityPairVector trckcoll = assomap_ite->val;
192 
193  float ptsum = 0;
194 
195  TrackRef trackref;
196 
197  //get the tracks associated to the vertex and calculate the manipulated pT**2
198  for (unsigned int trckcoll_ite = 0; trckcoll_ite < trckcoll.size(); trckcoll_ite++) {
199  trackref = trckcoll[trckcoll_ite].first;
200  int quality = trckcoll[trckcoll_ite].second;
201 
202  if (quality <= 2)
203  continue;
204 
205  double man_pT = trackref->pt() - trackref->ptError();
206  if (man_pT > 0.)
207  ptsum += man_pT * man_pT;
208  }
209 
210  vertexptsumvector.push_back(make_pair(assomap_vertexref, ptsum));
211  }
212 
213  while (!vertexptsumvector.empty()) {
214  VertexRef vertexref_highestpT;
215  float highestpT = 0.;
216  int highestpT_index = 0;
217 
218  for (unsigned int vtxptsumvec_ite = 0; vtxptsumvec_ite < vertexptsumvector.size(); vtxptsumvec_ite++) {
219  if (vertexptsumvector[vtxptsumvec_ite].second > highestpT) {
220  vertexref_highestpT = vertexptsumvector[vtxptsumvec_ite].first;
221  highestpT = vertexptsumvector[vtxptsumvec_ite].second;
222  highestpT_index = vtxptsumvec_ite;
223  }
224  }
225 
226  //loop over all vertices in the association map
227  for (TrackToVertexAssMap::const_iterator assomap_ite = trackvertexassInput->begin();
228  assomap_ite != trackvertexassInput->end();
229  assomap_ite++) {
230  const VertexRef assomap_vertexref = assomap_ite->key;
231  const TrackQualityPairVector trckcoll = assomap_ite->val;
232 
233  //if the vertex from the association map the vertex with the highest manipulated pT
234  //insert all associated tracks in the output Association Map
235  if (assomap_vertexref == vertexref_highestpT)
236  for (unsigned int trckcoll_ite = 0; trckcoll_ite < trckcoll.size(); trckcoll_ite++)
237  trackvertexassOutput->insert(assomap_vertexref, trckcoll[trckcoll_ite]);
238  }
239 
240  vertexptsumvector.erase(vertexptsumvector.begin() + highestpT_index);
241  }
242 
243  return trackvertexassOutput;
244 }
245 
246 /********************/
247 /* */
248 /* Member Functions */
249 /* */
250 /********************/
251 
252 /*************************************************************************************/
253 /* create helping vertex vector to remove associated vertices */
254 /*************************************************************************************/
255 
257  vector<VertexRef> output;
258  output.reserve(vtxcollH->size());
259  auto const nVtx = vtxcollH->size();
260  for (unsigned int i = 0; i < nVtx; ++i)
261  output.emplace_back(vtxcollH, i);
262 
263  return output;
264 }
265 
266 /****************************************************************************/
267 /* erase one vertex from the vertex vector */
268 /****************************************************************************/
269 
270 void PF_PU_AssoMapAlgos::EraseVertex(std::vector<reco::VertexRef>& vtxcollV, reco::VertexRef toErase) {
271  for (unsigned int index_vtx = 0; index_vtx < vtxcollV.size(); ++index_vtx) {
272  VertexRef vertexref = vtxcollV.at(index_vtx);
273 
274  if (vertexref == toErase) {
275  vtxcollV.erase(vtxcollV.begin() + index_vtx);
276  break;
277  }
278  }
279 }
280 
281 /*************************************************************************************/
282 /* function to find the closest vertex in 3D for a certain track */
283 /*************************************************************************************/
284 
286  const std::vector<reco::VertexRef>& vtxcollV,
287  double tWeight) {
288  double ztrack = trkref->vertex().z();
289 
290  VertexRef foundVertexRef = vtxcollV.at(0);
291 
292  double dzmin = 1e5;
293 
294  //loop over all vertices with a good quality in the vertex collection
295  for (unsigned int index_vtx = 0; index_vtx < vtxcollV.size(); ++index_vtx) {
296  const VertexRef& vertexref = vtxcollV.at(index_vtx);
297 
298  double nTracks = sqrt(vertexref->tracksSize());
299 
300  double z_distance = fabs(ztrack - vertexref->z());
301 
302  double weightedDistance = z_distance - tWeight * nTracks;
303 
304  if (weightedDistance < dzmin) {
305  dzmin = weightedDistance;
306  foundVertexRef = vertexref;
307  }
308  }
309 
310  return foundVertexRef;
311 }
312 
313 /*************************************************************************************/
314 /* function to find the closest vertex in 3D for a certain track */
315 /*************************************************************************************/
316 
318  const std::vector<reco::VertexRef>& vtxcollV,
319  double tWeight) {
320  VertexRef foundVertexRef = vtxcollV.at(0);
321 
322  double d3min = 1e5;
323 
324  //loop over all vertices with a good quality in the vertex collection
325  for (unsigned int index_vtx = 0; index_vtx < vtxcollV.size(); ++index_vtx) {
326  const VertexRef& vertexref = vtxcollV.at(index_vtx);
327 
328  double nTracks = sqrt(vertexref->tracksSize());
329 
330  double distance = 1e5;
331  pair<bool, Measurement1D> IpPair = IPTools::absoluteImpactParameter3D(transtrk, *vertexref);
332 
333  if (IpPair.first)
334  distance = IpPair.second.value();
335 
336  double weightedDistance = distance - tWeight * nTracks;
337 
338  if (weightedDistance < d3min) {
339  d3min = weightedDistance;
340  foundVertexRef = vertexref;
341  }
342  }
343 
344  return foundVertexRef;
345 }
346 
347 /****************************************************************************************/
348 /* function to calculate the deltaR between a vector and a vector connecting two points */
349 /****************************************************************************************/
350 
352  const math::XYZVector& vtx_mom,
354  double bs_x = bsH->x0();
355  double bs_y = bsH->y0();
356  double bs_z = bsH->z0();
357 
358  double connVec_x = vtx_pos.x() - bs_x;
359  double connVec_y = vtx_pos.y() - bs_y;
360  double connVec_z = vtx_pos.z() - bs_z;
361 
362  double connVec_r = sqrt(connVec_x * connVec_x + connVec_y * connVec_y + connVec_z * connVec_z);
363  double connVec_theta = acos(connVec_z * 1. / connVec_r);
364 
365  double connVec_eta = -1. * log(tan(connVec_theta * 1. / 2.));
366  double connVec_phi = atan2(connVec_y, connVec_x);
367 
368  return deltaR(vtx_mom.eta(), vtx_mom.phi(), connVec_eta, connVec_phi);
369 }
370 
371 /*************************************************************************************/
372 /* function to filter the conversion collection */
373 /*************************************************************************************/
374 
375 unique_ptr<ConversionCollection> PF_PU_AssoMapAlgos::GetCleanedConversions(
376  edm::Handle<reco::ConversionCollection> convCollH, Handle<BeamSpot> bsH, bool cleanedColl) {
377  unique_ptr<ConversionCollection> cleanedConvColl(new ConversionCollection());
378 
379  for (unsigned int convcoll_idx = 0; convcoll_idx < convCollH->size(); convcoll_idx++) {
380  ConversionRef convref(convCollH, convcoll_idx);
381 
382  if (!cleanedColl) {
383  cleanedConvColl->push_back(*convref);
384  continue;
385  }
386 
387  if ((convref->nTracks() == 2) && (fabs(convref->pairInvariantMass()) <= 0.1)) {
388  cleanedConvColl->push_back(*convref);
389  }
390  }
391 
392  return cleanedConvColl;
393 }
394 
395 /*************************************************************************************/
396 /* function to find out if the track comes from a gamma conversion */
397 /*************************************************************************************/
398 
400  const ConversionCollection& cleanedConvColl,
401  Conversion* gamma) {
402  for (unsigned int convcoll_ite = 0; convcoll_ite < cleanedConvColl.size(); convcoll_ite++) {
403  if (ConversionTools::matchesConversion(trackref, cleanedConvColl.at(convcoll_ite))) {
404  *gamma = cleanedConvColl.at(convcoll_ite);
405  return true;
406  }
407  }
408 
409  return false;
410 }
411 
412 /********************************************************************************/
413 /* function to find the closest vertex for a track from a conversion */
414 /********************************************************************************/
415 
417  const reco::Conversion& gamma,
421  const std::vector<reco::VertexRef>& vtxcollV,
422  double tWeight) {
423  math::XYZPoint conv_pos = gamma.conversionVertex().position();
424 
425  math::XYZVector conv_mom(
426  gamma.refittedPair4Momentum().x(), gamma.refittedPair4Momentum().y(), gamma.refittedPair4Momentum().z());
427 
428  Track photon(trackref->chi2(), trackref->ndof(), conv_pos, conv_mom, 0, trackref->covariance());
429 
430  TransientTrack transpho(photon, &(*bfH));
431  transpho.setBeamSpot(*bsH);
432  transpho.setTrackingGeometry(tgH);
433 
434  return FindClosest3D(transpho, vtxcollV, tWeight);
435 }
436 
437 /*************************************************************************************/
438 /* function to filter the Kshort collection */
439 /*************************************************************************************/
440 
441 unique_ptr<VertexCompositeCandidateCollection> PF_PU_AssoMapAlgos::GetCleanedKshort(
442  Handle<VertexCompositeCandidateCollection> KshortsH, Handle<BeamSpot> bsH, bool cleanedColl) {
443  unique_ptr<VertexCompositeCandidateCollection> cleanedKaonColl(new VertexCompositeCandidateCollection());
444 
445  for (unsigned int kscoll_idx = 0; kscoll_idx < KshortsH->size(); kscoll_idx++) {
446  VertexCompositeCandidateRef ksref(KshortsH, kscoll_idx);
447 
448  if (!cleanedColl) {
449  cleanedKaonColl->push_back(*ksref);
450  continue;
451  }
452 
453  VertexDistance3D distanceComputer;
454 
455  GlobalPoint dec_pos = RecoVertex::convertPos(ksref->vertex());
456 
457  GlobalError decayVertexError = GlobalError(ksref->vertexCovariance(0, 0),
458  ksref->vertexCovariance(0, 1),
459  ksref->vertexCovariance(1, 1),
460  ksref->vertexCovariance(0, 2),
461  ksref->vertexCovariance(1, 2),
462  ksref->vertexCovariance(2, 2));
463 
464  math::XYZVector dec_mom(ksref->momentum().x(), ksref->momentum().y(), ksref->momentum().z());
465 
466  GlobalPoint bsPosition = RecoVertex::convertPos(bsH->position());
468 
469  double kaon_significance =
470  (distanceComputer.distance(VertexState(bsPosition, bsError), VertexState(dec_pos, decayVertexError)))
471  .significance();
472 
473  if ((ksref->vertex().rho() >= 3.) && (ksref->vertexNormalizedChi2() <= 3.) &&
474  (fabs(ksref->mass() - kMass) <= 0.01) && (kaon_significance > 15.) &&
475  (PF_PU_AssoMapAlgos::dR(ksref->vertex(), dec_mom, bsH) <= 0.3)) {
476  cleanedKaonColl->push_back(*ksref);
477  }
478  }
479 
480  return cleanedKaonColl;
481 }
482 
483 /*************************************************************************************/
484 /* function to filter the Lambda collection */
485 /*************************************************************************************/
486 
487 unique_ptr<VertexCompositeCandidateCollection> PF_PU_AssoMapAlgos::GetCleanedLambda(
488  Handle<VertexCompositeCandidateCollection> LambdasH, Handle<BeamSpot> bsH, bool cleanedColl) {
489  unique_ptr<VertexCompositeCandidateCollection> cleanedLambdaColl(new VertexCompositeCandidateCollection());
490 
491  for (unsigned int lambdacoll_idx = 0; lambdacoll_idx < LambdasH->size(); lambdacoll_idx++) {
492  VertexCompositeCandidateRef lambdaref(LambdasH, lambdacoll_idx);
493 
494  if (!cleanedColl) {
495  cleanedLambdaColl->push_back(*lambdaref);
496  continue;
497  }
498 
499  VertexDistance3D distanceComputer;
500 
501  GlobalPoint dec_pos = RecoVertex::convertPos(lambdaref->vertex());
502 
503  GlobalError decayVertexError = GlobalError(lambdaref->vertexCovariance(0, 0),
504  lambdaref->vertexCovariance(0, 1),
505  lambdaref->vertexCovariance(1, 1),
506  lambdaref->vertexCovariance(0, 2),
507  lambdaref->vertexCovariance(1, 2),
508  lambdaref->vertexCovariance(2, 2));
509 
510  math::XYZVector dec_mom(lambdaref->momentum().x(), lambdaref->momentum().y(), lambdaref->momentum().z());
511 
512  GlobalPoint bsPosition = RecoVertex::convertPos(bsH->position());
514 
515  double lambda_significance =
516  (distanceComputer.distance(VertexState(bsPosition, bsError), VertexState(dec_pos, decayVertexError)))
517  .significance();
518 
519  if ((lambdaref->vertex().rho() >= 3.) && (lambdaref->vertexNormalizedChi2() <= 3.) &&
520  (fabs(lambdaref->mass() - lamMass) <= 0.005) && (lambda_significance > 15.) &&
521  (PF_PU_AssoMapAlgos::dR(lambdaref->vertex(), dec_mom, bsH) <= 0.3)) {
522  cleanedLambdaColl->push_back(*lambdaref);
523  }
524  }
525 
526  return cleanedLambdaColl;
527 }
528 
529 /*************************************************************************************/
530 /* function to find out if the track comes from a V0 decay */
531 /*************************************************************************************/
532 
534  const VertexCompositeCandidateCollection& cleanedKshort,
535  const VertexCompositeCandidateCollection& cleanedLambda,
537  //the part for the reassociation of particles from Kshort decays
538  for (VertexCompositeCandidateCollection::const_iterator iKS = cleanedKshort.begin(); iKS != cleanedKshort.end();
539  iKS++) {
540  const RecoChargedCandidate* dauCand1 = dynamic_cast<const RecoChargedCandidate*>(iKS->daughter(0));
541  TrackRef dauTk1 = dauCand1->track();
542  const RecoChargedCandidate* dauCand2 = dynamic_cast<const RecoChargedCandidate*>(iKS->daughter(1));
543  TrackRef dauTk2 = dauCand2->track();
544 
545  if ((trackref == dauTk1) || (trackref == dauTk2)) {
546  *V0 = *iKS;
547  return true;
548  }
549  }
550 
551  //the part for the reassociation of particles from Lambda decays
552  for (VertexCompositeCandidateCollection::const_iterator iLambda = cleanedLambda.begin();
553  iLambda != cleanedLambda.end();
554  iLambda++) {
555  const RecoChargedCandidate* dauCand1 = dynamic_cast<const RecoChargedCandidate*>(iLambda->daughter(0));
556  TrackRef dauTk1 = dauCand1->track();
557  const RecoChargedCandidate* dauCand2 = dynamic_cast<const RecoChargedCandidate*>(iLambda->daughter(1));
558  TrackRef dauTk2 = dauCand2->track();
559 
560  if ((trackref == dauTk1) || (trackref == dauTk2)) {
561  *V0 = *iLambda;
562  return true;
563  }
564  }
565 
566  return false;
567 }
568 
569 /*************************************************************************************/
570 /* function to find the closest vertex in z for a track from a V0 */
571 /*************************************************************************************/
572 
574  const VertexCompositeCandidate& V0_vtx,
575  ESHandle<MagneticField> bFieldH,
576  edm::ESHandle<GlobalTrackingGeometry> trackingGeometryH,
577  Handle<BeamSpot> bsH,
578  const std::vector<reco::VertexRef>& vtxcollV,
579  double tWeight) {
580  const math::XYZPoint& dec_pos = V0_vtx.vertex();
581 
582  math::XYZVector dec_mom(V0_vtx.momentum().x(), V0_vtx.momentum().y(), V0_vtx.momentum().z());
583 
584  Track V0(trackref->chi2(), trackref->ndof(), dec_pos, dec_mom, 0, trackref->covariance());
585 
586  TransientTrack transV0(V0, &(*bFieldH));
587  transV0.setBeamSpot(*bsH);
588  transV0.setTrackingGeometry(trackingGeometryH);
589 
590  return FindClosest3D(transV0, vtxcollV, tWeight);
591 }
592 
593 /*************************************************************************************/
594 /* function to filter the nuclear interaction collection */
595 /*************************************************************************************/
596 
597 unique_ptr<PFDisplacedVertexCollection> PF_PU_AssoMapAlgos::GetCleanedNI(Handle<PFDisplacedVertexCollection> NuclIntH,
598  Handle<BeamSpot> bsH,
599  bool cleanedColl) {
600  unique_ptr<PFDisplacedVertexCollection> cleanedNIColl(new PFDisplacedVertexCollection());
601 
602  for (PFDisplacedVertexCollection::const_iterator niref = NuclIntH->begin(); niref != NuclIntH->end(); niref++) {
603  if ((niref->isFake()) || !(niref->isNucl()))
604  continue;
605 
606  if (!cleanedColl) {
607  cleanedNIColl->push_back(*niref);
608  continue;
609  }
610 
611  VertexDistance3D distanceComputer;
612 
613  GlobalPoint ni_pos = RecoVertex::convertPos(niref->position());
614  GlobalError interactionVertexError = RecoVertex::convertError(niref->error());
615 
616  math::XYZVector ni_mom(niref->primaryMomentum().x(), niref->primaryMomentum().y(), niref->primaryMomentum().z());
617 
618  GlobalPoint bsPosition = RecoVertex::convertPos(bsH->position());
620 
621  double nuclint_significance =
622  (distanceComputer.distance(VertexState(bsPosition, bsError), VertexState(ni_pos, interactionVertexError)))
623  .significance();
624 
625  if ((niref->position().rho() >= 3.) && (nuclint_significance > 15.) &&
626  (PF_PU_AssoMapAlgos::dR(niref->position(), ni_mom, bsH) <= 0.3)) {
627  cleanedNIColl->push_back(*niref);
628  }
629  }
630 
631  return cleanedNIColl;
632 }
633 
634 /*************************************************************************************/
635 /* function to find out if the track comes from a nuclear interaction */
636 /*************************************************************************************/
637 
639  const PFDisplacedVertexCollection& cleanedNI,
640  PFDisplacedVertex* displVtx) {
641  //the part for the reassociation of particles from nuclear interactions
642  for (PFDisplacedVertexCollection::const_iterator iDisplV = cleanedNI.begin(); iDisplV != cleanedNI.end(); iDisplV++) {
643  if (iDisplV->trackWeight(trackref) > 1.e-5) {
644  *displVtx = *iDisplV;
645  return true;
646  }
647  }
648 
649  return false;
650 }
651 
652 /*************************************************************************************/
653 /* function to find the closest vertex in z for a track from a nuclear interaction */
654 /*************************************************************************************/
655 
657  const PFDisplacedVertex& displVtx,
658  ESHandle<MagneticField> bFieldH,
659  edm::ESHandle<GlobalTrackingGeometry> trackingGeometryH,
660  Handle<BeamSpot> bsH,
661  const std::vector<reco::VertexRef>& vtxcollV,
662  double tWeight) {
664 
665  if ((displVtx.isTherePrimaryTracks()) || (displVtx.isThereMergedTracks())) {
666  for (TrackCollection::const_iterator trkcoll_ite = refittedTracks.begin(); trkcoll_ite != refittedTracks.end();
667  trkcoll_ite++) {
668  const TrackBaseRef retrackbaseref = displVtx.originalTrack(*trkcoll_ite);
669 
670  if (displVtx.isIncomingTrack(retrackbaseref)) {
671  VertexRef VOAssociation = TrackWeightAssociation(retrackbaseref, vtxcollV);
672 
673  if (VOAssociation->trackWeight(retrackbaseref) >= 1.e-5) {
674  return VOAssociation;
675  }
676 
677  TransientTrack transIncom(*retrackbaseref, &(*bFieldH));
678  transIncom.setBeamSpot(*bsH);
680 
681  return FindClosest3D(transIncom, vtxcollV, tWeight);
682  }
683  }
684  }
685 
686  const math::XYZPoint& ni_pos = displVtx.position();
687 
688  math::XYZVector ni_mom(
689  displVtx.primaryMomentum().x(), displVtx.primaryMomentum().y(), displVtx.primaryMomentum().z());
690 
691  Track incom(trackref->chi2(), trackref->ndof(), ni_pos, ni_mom, 0, trackref->covariance());
692 
693  TransientTrack transIncom(incom, &(*bFieldH));
694  transIncom.setBeamSpot(*bsH);
695  transIncom.setTrackingGeometry(trackingGeometryH);
696 
697  return FindClosest3D(transIncom, vtxcollV, tWeight);
698 }
699 
700 /*************************************************************************************/
701 /* function to find the vertex with the highest TrackWeight for a certain track */
702 /*************************************************************************************/
703 template <typename TREF>
705  const std::vector<reco::VertexRef>& vtxcollV) {
706  VertexRef bestvertexref = vtxcollV.at(0);
707  float bestweight = 0.;
708 
709  //loop over all vertices in the vertex collection
710  for (auto const& vertexref : vtxcollV) {
711  //get the most probable vertex for the track
712  float weight = vertexref->trackWeight(trackRef);
713  if (weight > bestweight) {
714  bestweight = weight;
715  bestvertexref = vertexref;
716  }
717  }
718 
719  return bestvertexref;
720 }
721 
722 /*************************************************************************************/
723 /* find an association for a certain track */
724 /*************************************************************************************/
725 
727  const std::vector<reco::VertexRef>& vtxColl,
731  int assocNum) {
732  VertexRef foundVertex;
733 
734  //if it is not the first try of an association jump to the final association
735  //to avoid multiple (secondary) associations and/or unphysical (primary and secondary) associations
736  if (assocNum > 0)
737  goto finalStep;
738 
739  // Step 1: First round of association:
740  // Find the vertex with the highest track-to-vertex association weight
741  foundVertex = TrackWeightAssociation(trackref, vtxColl);
742 
743  if (foundVertex->trackWeight(trackref) >= 1.e-5) {
744  return make_pair(foundVertex, 0.);
745  }
746 
747  // Step 2: Reassociation
748  // Second round of association:
749  // In case no vertex with track-to-vertex association weight > 1.e-5 is found,
750  // check the track originates from a neutral hadron decay, photon conversion or nuclear interaction
751 
753  // Test if the track comes from a photon conversion:
754  // If so, try to find the vertex of the mother particle
756  if (ComesFromConversion(trackref, *cleanedConvCollP, &gamma)) {
757  foundVertex = FindConversionVertex(trackref, gamma, bfH, tgH, bsH, vtxColl, input_nTrack_);
758  return make_pair(foundVertex, 1.);
759  }
760 
761  // Test if the track comes from a Kshort or Lambda decay:
762  // If so, reassociate the track to the vertex of the V0
764  if (ComesFromV0Decay(trackref, *cleanedKshortCollP, *cleanedLambdaCollP, &V0)) {
765  foundVertex = FindV0Vertex(trackref, V0, bfH, tgH, bsH, vtxColl, input_nTrack_);
766  return make_pair(foundVertex, 1.);
767  }
768 
769  if (!missingColls) {
770  // Test if the track comes from a nuclear interaction:
771  // If so, reassociate the track to the vertex of the incoming particle
772  PFDisplacedVertex displVtx;
773  if (ComesFromNI(trackref, *cleanedNICollP, &displVtx)) {
774  foundVertex = FindNIVertex(trackref, displVtx, bfH, tgH, bsH, vtxColl, input_nTrack_);
775  return make_pair(foundVertex, 1.);
776  }
777  }
778  }
779 
780  // Step 3: Final association
781  // If no vertex is found with track-to-vertex association weight > 1.e-5
782  // and no reassociation was done do the final association
783  // look for the closest vertex in 3D or in z/longitudinal distance
784  // or associate the track always to the first vertex (default)
785 
786 finalStep:
787 
788  switch (input_FinalAssociation_) {
789  case 1: {
790  // closest in z
791  foundVertex = FindClosestZ(trackref, vtxColl, input_nTrack_);
792  break;
793  }
794 
795  case 2: {
796  // closest in 3D
797  TransientTrack transtrk(trackref, &(*bfH));
798  transtrk.setBeamSpot(*bsH);
800 
801  foundVertex = FindClosest3D(transtrk, vtxColl, input_nTrack_);
802  break;
803  }
804 
805  default: {
806  // allways first vertex
807  foundVertex = vtxColl.at(0);
808  break;
809  }
810  }
811 
812  return make_pair(foundVertex, 2.);
813 }
814 
815 /*************************************************************************************/
816 /* get the quality for a certain association */
817 /*************************************************************************************/
818 
819 int PF_PU_AssoMapAlgos::DefineQuality(int assoc_ite, int step, double distance) {
820  int quality = 0;
821 
822  switch (step) {
823  case 0: {
824  //TrackWeight association
825  if (distance <= tw_90) {
826  quality = 5;
827  } else {
828  if (distance <= tw_70) {
829  quality = 4;
830  } else {
831  if (distance <= tw_50) {
832  quality = 3;
833  } else {
834  quality = 2;
835  }
836  }
837  }
838  break;
839  }
840 
841  case 1: {
842  //Secondary association
843  if (distance <= sec_70) {
844  quality = 4;
845  } else {
846  if (distance <= sec_50) {
847  quality = 3;
848  } else {
849  quality = 2;
850  }
851  }
852  break;
853  }
854 
855  case 2: {
856  //Final association
857  if (assoc_ite == 1) {
858  quality = 1;
859  } else {
860  if (assoc_ite >= 2) {
861  quality = 0;
862  } else {
863  if (distance <= fin_70) {
864  quality = 4;
865  } else {
866  if (distance <= fin_50) {
867  quality = 3;
868  } else {
869  quality = 2;
870  }
871  }
872  }
873  }
874  break;
875  }
876 
877  default: {
878  quality = -1;
879  break;
880  }
881  }
882 
883  return quality;
884 }
edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > LambdaCollectionToken_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
reco::Vertex::Point convertPos(const GlobalPoint &p)
static std::unique_ptr< reco::VertexCompositeCandidateCollection > GetCleanedLambda(edm::Handle< reco::VertexCompositeCandidateCollection >, edm::Handle< reco::BeamSpot >, bool)
std::vector< PFDisplacedVertex > PFDisplacedVertexCollection
collection of PFDisplacedVertex objects
static bool matchesConversion(const reco::GsfElectron &ele, const reco::Conversion &conv, bool allowCkfMatch=true, bool allowAmbiguousGsfMatch=false)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::ESHandle< MagneticField > bFieldH
edm::EDGetTokenT< reco::BeamSpot > token_BeamSpot_
std::unique_ptr< VertexToTrackAssMap > CreateVertexToTrackMap(edm::Handle< reco::TrackCollection >)
static reco::VertexRef FindNIVertex(const reco::TrackRef, const reco::PFDisplacedVertex &, edm::ESHandle< MagneticField >, edm::ESHandle< GlobalTrackingGeometry >, edm::Handle< reco::BeamSpot >, const std::vector< reco::VertexRef > &, double)
Vector momentum() const final
spatial momentum vector
const double lamMass
std::vector< VertexCompositeCandidate > VertexCompositeCandidateCollection
collection of Candidate objects
const math::XYZTLorentzVector primaryMomentum(std::string massHypo="PI", bool useRefitted=true, double mass=0.0) const
Momentum of primary or merged track calculated with a mass hypothesis.
static std::unique_ptr< reco::PFDisplacedVertexCollection > GetCleanedNI(edm::Handle< reco::PFDisplacedVertexCollection >, edm::Handle< reco::BeamSpot >, bool)
std::unique_ptr< TrackToVertexAssMap > SortAssociationMap(TrackToVertexAssMap *, edm::Handle< reco::TrackCollection >)
const Point & position() const
position
Definition: BeamSpot.h:59
const bool isTherePrimaryTracks() const
--—— Provide useful information --—— ///
void setBeamSpot(const reco::BeamSpot &beamSpot)
const double kMass
static bool ComesFromNI(const reco::TrackRef, const reco::PFDisplacedVertexCollection &, reco::PFDisplacedVertex *)
static std::unique_ptr< reco::VertexCompositeCandidateCollection > GetCleanedKshort(edm::Handle< reco::VertexCompositeCandidateCollection >, edm::Handle< reco::BeamSpot >, bool)
int DefineQuality(int, int, double)
const Point & position() const
position
Definition: Vertex.h:127
static reco::VertexRef TrackWeightAssociation(const TREF &, const std::vector< reco::VertexRef > &)
VertexStepPair FindAssociation(const reco::TrackRef &, const std::vector< reco::VertexRef > &, edm::ESHandle< MagneticField >, edm::ESHandle< GlobalTrackingGeometry >, edm::Handle< reco::BeamSpot >, int)
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
std::pair< bool, Measurement1D > absoluteImpactParameter3D(const reco::TransientTrack &transientTrack, const reco::Vertex &vertex)
Definition: IPTools.cc:38
const double tw_70
GlobalErrorBase< double, ErrorMatrixTag > GlobalError
Definition: GlobalError.h:13
const double fin_70
edm::ESHandle< GlobalTrackingGeometry > trackingGeometryH
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
Definition: weight.py:1
const bool isIncomingTrack(const reco::TrackBaseRef &originalTrack) const
Is primary or merged track.
const Point & vertex() const override
vertex position (overwritten by PF...)
std::unique_ptr< reco::PFDisplacedVertexCollection > cleanedNICollP
std::unique_ptr< reco::VertexCompositeCandidateCollection > cleanedLambdaCollP
static reco::VertexRef FindConversionVertex(const reco::TrackRef, const reco::Conversion &, edm::ESHandle< MagneticField >, edm::ESHandle< GlobalTrackingGeometry >, edm::Handle< reco::BeamSpot >, const std::vector< reco::VertexRef > &, double)
std::pair< reco::VertexRef, int > VertexStepPair
key_type key() const
Accessor for product key.
Definition: Ref.h:250
std::vector< Conversion > ConversionCollection
collectin of Conversion objects
Definition: ConversionFwd.h:9
std::vector< VertexPtsumPair > VertexPtsumVector
std::unique_ptr< TrackToVertexAssMap > CreateTrackToVertexMap(edm::Handle< reco::TrackCollection >)
const_iterator end() const
last iterator over the map (read only)
T getUntrackedParameter(std::string const &, T const &) const
std::pair< std::unique_ptr< TrackToVertexAssMap >, std::unique_ptr< VertexToTrackAssMap > > createMappings(edm::Handle< reco::TrackCollection > trkcollH)
U second(std::pair< T, U > const &p)
double x0() const
x coordinate
Definition: BeamSpot.h:61
string quality
int iEvent
Definition: GenABIO.cc:224
static std::unique_ptr< reco::ConversionCollection > GetCleanedConversions(edm::Handle< reco::ConversionCollection >, edm::Handle< reco::BeamSpot >, bool)
Covariance3DMatrix covariance3D() const
return only 3D position covariance matrix
Definition: BeamSpot.h:112
const edm::ESGetToken< GlobalTrackingGeometry, GlobalTrackingGeometryRecord > token_TrackingGeometry_
T sqrt(T t)
Definition: SSEVec.h:19
reco::TrackRef track() const override
reference to a track
PF_PU_AssoMapAlgos(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
edm::Handle< reco::PFDisplacedVertexCollection > displVertexCollH
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
virtual void GetInputCollections(edm::Event &, const edm::EventSetup &)
double y0() const
y coordinate
Definition: BeamSpot.h:63
static bool ComesFromV0Decay(const reco::TrackRef, const reco::VertexCompositeCandidateCollection &, const reco::VertexCompositeCandidateCollection &, reco::VertexCompositeCandidate *)
edm::AssociationMap< edm::OneToManyWithQuality< reco::VertexCollection, reco::TrackCollection, int > > TrackToVertexAssMap
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
static double dR(const math::XYZPoint &, const math::XYZVector &, edm::Handle< reco::BeamSpot >)
const bool isThereMergedTracks() const
If a merged track was identified.
edm::EDGetTokenT< reco::PFDisplacedVertexCollection > NIVertexCollectionToken_
edm::Handle< reco::VertexCollection > vtxcollH
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
const double sec_50
static reco::VertexRef FindClosest3D(reco::TransientTrack, const std::vector< reco::VertexRef > &, double tWeight=0.)
std::vector< reco::VertexRef > CreateVertexVector(edm::Handle< reco::VertexCollection >)
Block of elements.
edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > KshortCollectionToken_
std::vector< TrackQualityPair > TrackQualityPairVector
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
const std::vector< Track > & refittedTracks() const
Returns the container of refitted tracks.
Definition: Vertex.h:196
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
edm::EDGetTokenT< reco::ConversionCollection > ConversionsCollectionToken_
edm::Handle< reco::VertexCompositeCandidateCollection > vertCompCandCollLambdaH
edm::EDGetTokenT< reco::VertexCollection > token_VertexCollection_
static reco::VertexRef FindV0Vertex(const reco::TrackRef, const reco::VertexCompositeCandidate &, edm::ESHandle< MagneticField >, edm::ESHandle< GlobalTrackingGeometry >, edm::Handle< reco::BeamSpot >, const std::vector< reco::VertexRef > &, double)
std::unique_ptr< reco::VertexCompositeCandidateCollection > cleanedKshortCollP
void EraseVertex(std::vector< reco::VertexRef > &, reco::VertexRef)
significance
Definition: met_cff.py:15
double z0() const
z coordinate
Definition: BeamSpot.h:65
const_iterator begin() const
first iterator over the map (read only)
fixed size matrix
HLT enums.
const double tw_50
void setTrackingGeometry(const edm::ESHandle< GlobalTrackingGeometry > &tg)
const double sec_70
static bool ComesFromConversion(const reco::TrackRef, const reco::ConversionCollection &, reco::Conversion *)
step
Definition: StallMonitor.cc:98
edm::Handle< reco::BeamSpot > beamspotH
Log< level::Warning, false > LogWarning
edm::Handle< reco::ConversionCollection > convCollH
static reco::VertexRef FindClosestZ(const reco::TrackRef, const std::vector< reco::VertexRef > &, double tWeight=0.)
edm::AssociationMap< edm::OneToManyWithQuality< reco::TrackCollection, reco::VertexCollection, int > > VertexToTrackAssMap
const double tw_90
def move(src, dest)
Definition: eostools.py:511
edm::Handle< reco::VertexCompositeCandidateCollection > vertCompCandCollKshortH
TrackBaseRef originalTrack(const Track &refTrack) const
Definition: Vertex.cc:81
const double fin_50
std::unique_ptr< reco::ConversionCollection > cleanedConvCollP
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > token_bField_