File indexing completed on 2024-09-07 04:34:29
0001 #include "Alignment/CommonAlignmentProducer/interface/AlignmentTracksFromVertexSelector.h"
0002
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "DataFormats/TrackReco/interface/Track.h"
0008
0009 #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
0010 #include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h"
0011
0012
0013 #include "RecoVertex/VertexTools/interface/VertexDistanceXY.h"
0014 #include "RecoVertex/VertexTools/interface/VertexDistance3D.h"
0015
0016
0017 AlignmentTrackFromVertexSelector::AlignmentTrackFromVertexSelector(const edm::ParameterSet& cfg,
0018 edm::ConsumesCollector& iC)
0019 : ttbESToken_(
0020 iC.esConsumes<TransientTrackBuilder, TransientTrackRecord>(edm::ESInputTag("", "TransientTrackBuilder"))),
0021 vertexToken_(iC.consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("vertices"))),
0022 diLeptonToken_(iC.consumes<reco::TrackCollection>(cfg.getParameter<edm::InputTag>("leptonTracks"))),
0023 useClosestVertex_(cfg.getParameter<bool>("useClosestVertexToDilepton")),
0024 vertexIndex_(cfg.getParameter<unsigned int>("vertexIndex")) {}
0025
0026
0027 AlignmentTrackFromVertexSelector::~AlignmentTrackFromVertexSelector() {}
0028
0029
0030 const reco::Vertex* AlignmentTrackFromVertexSelector::findClosestVertex(const reco::TrackCollection& leptonTracks,
0031 const reco::VertexCollection* vertices,
0032 const edm::EventSetup& setup) const {
0033 reco::Vertex* defaultVtx = nullptr;
0034
0035
0036 const TransientTrackBuilder* theB = &setup.getData(ttbESToken_);
0037 std::vector<reco::TransientTrack> tks;
0038 for (const auto& track : leptonTracks) {
0039 reco::TransientTrack trajectory = theB->build(track);
0040 tks.push_back(trajectory);
0041 }
0042
0043
0044 TransientVertex aTransVtx;
0045 KalmanVertexFitter kalman(true);
0046 aTransVtx = kalman.vertex(tks);
0047
0048 if (!aTransVtx.isValid())
0049 return defaultVtx;
0050
0051
0052 VertexDistance3D vertTool3D;
0053 float minD = 9999.;
0054 int closestVtxIndex = 0;
0055 int counter = 0;
0056 for (const auto& vtx : *vertices) {
0057 double dist3D = vertTool3D.distance(aTransVtx, vtx).value();
0058 if (dist3D < minD) {
0059 minD = dist3D;
0060 closestVtxIndex = counter;
0061 }
0062 counter++;
0063 }
0064 if ((*vertices).at(closestVtxIndex).isValid()) {
0065 return &(vertices->at(closestVtxIndex));
0066 } else {
0067 return defaultVtx;
0068 }
0069 }
0070
0071
0072 AlignmentTrackFromVertexSelector::Tracks AlignmentTrackFromVertexSelector::select(
0073 const edm::Handle<reco::TrackCollection>& tc, const edm::Event& evt, const edm::EventSetup& setup) const {
0074 Tracks result;
0075
0076 std::vector<unsigned int> thePVkeys;
0077
0078
0079 edm::Handle<reco::VertexCollection> vertexHandle = evt.getHandle(vertexToken_);
0080
0081
0082 const auto& leptonTracks = evt.get(diLeptonToken_);
0083
0084
0085 if (vertexHandle.isValid()) {
0086 const reco::VertexCollection* vertices = vertexHandle.product();
0087 const reco::Vertex* theVtx = nullptr;
0088
0089 if (useClosestVertex_) {
0090 theVtx = findClosestVertex(leptonTracks, vertices, setup);
0091 } else {
0092 if ((*vertices).at(vertexIndex_).isValid()) {
0093 theVtx = &(vertices->at(vertexIndex_));
0094 }
0095 }
0096
0097 if (theVtx) {
0098 for (auto tv = theVtx->tracks_begin(); tv != theVtx->tracks_end(); tv++) {
0099 if (tv->isNonnull()) {
0100 const reco::TrackRef trackRef = tv->castTo<reco::TrackRef>();
0101 thePVkeys.push_back(trackRef.key());
0102 }
0103 }
0104 }
0105 }
0106
0107 std::sort(thePVkeys.begin(), thePVkeys.end());
0108
0109 LogDebug("AlignmentTrackFromVertexSelector")
0110 << "after collecting the PV keys: thePVkeys.size()" << thePVkeys.size() << std::endl;
0111 for (const auto& key : thePVkeys) {
0112 LogDebug("AlignmentTrackFromVertexSelector") << key << ", ";
0113 }
0114 LogDebug("AlignmentTrackFromVertexSelector") << std::endl;
0115
0116 if (tc.isValid()) {
0117 int indx(0);
0118
0119 for (reco::TrackCollection::const_iterator tk = tc->begin(); tk != tc->end(); ++tk, ++indx) {
0120 reco::TrackRef trackRef = reco::TrackRef(tc, indx);
0121 if (std::find(thePVkeys.begin(), thePVkeys.end(), trackRef.key()) != thePVkeys.end()) {
0122 LogDebug("AlignmentTrackFromVertexSelector") << "track index: " << indx << "filling result vector" << std::endl;
0123 result.push_back(&(*tk));
0124 }
0125 }
0126 }
0127 return result;
0128 }