CMS 3D CMS Logo

PATRefitVertexProducer.cc
Go to the documentation of this file.
1 
26 
27 #include <memory>
28 
30 public:
33 
34  void produce(edm::Event&, const edm::EventSetup&) override;
36 
37 private:
38  //--- utility methods
39 
40  //--- configuration parameters
48 };
49 
51  : srcCands_(consumes<std::vector<pat::PackedCandidate> >(cfg.getParameter<edm::InputTag>("srcCands"))),
52  srcLostTracks_(consumes<std::vector<pat::PackedCandidate> >(cfg.getParameter<edm::InputTag>("srcLostTracks"))),
53  srcEleKfTracks_(consumes<std::vector<pat::PackedCandidate> >(cfg.getParameter<edm::InputTag>("srcEleKfTracks"))),
54  srcVertices_(consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("srcVertices"))),
55  srcBeamSpot_(consumes<reco::BeamSpot>(cfg.getParameter<edm::InputTag>("srcBeamSpot"))),
56  transTrackBuilderToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
57  useBeamSpot_(cfg.getParameter<bool>("useBeamSpot")),
58  useLostTracks_(cfg.getParameter<bool>("useLostTracks")),
59  useEleKfTracks_(cfg.getParameter<bool>("useEleKfTracks")) {
60  produces<reco::VertexCollection>();
61 }
62 
64  // Obtain collections
67 
69  if (useLostTracks_)
70  evt.getByToken(srcLostTracks_, lostTrackCands);
71 
73  if (useEleKfTracks_)
74  evt.getByToken(srcEleKfTracks_, eleKfTrackCands);
75 
78  const reco::Vertex& pv = vertices->front();
79  size_t vtxIdx = 0;
80 
82  if (useBeamSpot_)
84 
85  // Get transient track builder
86  const TransientTrackBuilder& transTrackBuilder = es.getData(transTrackBuilderToken_);
87 
88  // Output collection
89  auto outputVertices = std::make_unique<reco::VertexCollection>();
90  outputVertices->reserve(1);
91 
92  // Create a new track collection for vertex refit
93  std::vector<reco::TransientTrack> transTracks;
94 
95  // loop over the PFCandidates
96  for (const auto& cand : (*cands)) {
97  if (cand.charge() == 0 || cand.vertexRef().isNull())
98  continue;
99  if (cand.bestTrack() == nullptr)
100  continue;
101  auto key = cand.vertexRef().key();
102  auto quality = cand.pvAssociationQuality();
103  if (key != vtxIdx ||
105  continue;
106  if (useEleKfTracks_ && std::abs(cand.pdgId()) == 11)
107  continue;
108  transTracks.push_back(transTrackBuilder.build(cand.bestTrack()));
109  }
110 
111  // loop over the lostTracks
112  if (useLostTracks_) {
113  for (const auto& cand : (*lostTrackCands)) {
114  if (cand.charge() == 0 || cand.vertexRef().isNull())
115  continue;
116  if (cand.bestTrack() == nullptr)
117  continue;
118  auto key = cand.vertexRef().key();
119  auto quality = cand.pvAssociationQuality();
120  if (key != vtxIdx ||
122  continue;
123  transTracks.push_back(transTrackBuilder.build(cand.bestTrack()));
124  }
125  }
126 
127  // loop over the electronKfTracks
128  if (useEleKfTracks_) {
129  for (const auto& cand : (*eleKfTrackCands)) {
130  if (cand.charge() == 0 || cand.vertexRef().isNull())
131  continue;
132  if (cand.bestTrack() == nullptr)
133  continue;
134  auto key = cand.vertexRef().key();
135  auto quality = cand.pvAssociationQuality();
136  if (key != vtxIdx ||
138  continue;
139  transTracks.push_back(transTrackBuilder.build(cand.bestTrack()));
140  }
141  }
142 
143  // Refit the vertex
144  TransientVertex transVtx;
145  reco::Vertex refitPV(pv); // initialized to the original PV
146 
147  bool fitOK = true;
148  if (transTracks.size() >= 3) {
150  avf.setWeightThreshold(0.1); // weight per track, allow almost every fit, else --> exception
151  if (!useBeamSpot_) {
152  transVtx = avf.vertex(transTracks);
153  } else {
154  transVtx = avf.vertex(transTracks, *beamSpot);
155  }
156  if (!transVtx.isValid()) {
157  fitOK = false;
158  } else {
159  //MB: protect against rare cases when transVtx is valid but its postion is ill-defined
160  if (!std::isfinite(transVtx.position().z())) //MB: it is enough to check one coordinate (?)
161  fitOK = false;
162  }
163  } else
164  fitOK = false;
165  if (fitOK) {
166  refitPV = transVtx;
167  }
168 
169  outputVertices->push_back(refitPV);
170 
171  evt.put(std::move(outputVertices));
172 }
173 
175  // patRefitVertexProducer
177 
178  desc.add<edm::InputTag>("srcVertices", edm::InputTag("offlineSlimmedPrimaryVertices"));
179  desc.add<edm::InputTag>("srcCands", edm::InputTag("packedPFCandidates"));
180  desc.add<edm::InputTag>("srcLostTracks", edm::InputTag("lostTracks"));
181  desc.add<edm::InputTag>("srcEleKfTracks", edm::InputTag("lostTracks:eleTracks"));
182  desc.add<edm::InputTag>("srcBeamSpot", edm::InputTag("offlineBeamSpot"));
183  desc.add<bool>("useBeamSpot", true)->setComment("Refit PV with beam-spot constraint");
184  desc.add<bool>("useLostTracks", true)
185  ->setComment("Use collection of tracks not used by PF-candidates, aka lost-tracks");
186  desc.add<bool>("useEleKfTracks", true)
187  ->setComment("Use collection of electron KF-tracks instead of GSF-tracks of electron PF-candidates");
188 
189  descriptions.addWithDefaultLabel(desc);
190 }
191 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
void produce(edm::Event &, const edm::EventSetup &) override
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
GlobalPoint position() const
edm::EDGetTokenT< reco::BeamSpot > srcBeamSpot_
T z() const
Definition: PV3DBase.h:61
edm::EDGetTokenT< std::vector< pat::PackedCandidate > > srcLostTracks_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:526
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &) const override
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
Definition: HeavyIon.h:7
reco::TransientTrack build(const reco::Track *p) const
string quality
bool isValid() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
key
prepare the HTCondor submission files and eventually submit them
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< std::vector< pat::PackedCandidate > > srcCands_
static void fillDescriptions(edm::ConfigurationDescriptions &)
edm::EDGetTokenT< reco::VertexCollection > srcVertices_
fixed size matrix
HLT enums.
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > transTrackBuilderToken_
edm::EDGetTokenT< std::vector< pat::PackedCandidate > > srcEleKfTracks_
def move(src, dest)
Definition: eostools.py:511
PATRefitVertexProducer(const edm::ParameterSet &)