CMS 3D CMS Logo

PATLostTracks.cc
Go to the documentation of this file.
14 
15 namespace {
16  bool passesQuality(const reco::Track& trk, const std::vector<reco::TrackBase::TrackQuality>& allowedQuals) {
17  for (const auto& qual : allowedQuals) {
18  if (trk.quality(qual))
19  return true;
20  }
21  return false;
22  }
23 } // namespace
24 
25 namespace pat {
27  public:
28  explicit PATLostTracks(const edm::ParameterSet&);
29  ~PATLostTracks() override;
30 
31  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
32 
33  private:
35  bool passTrkCuts(const reco::Track& tr) const;
36  void addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
37  const reco::TrackRef& trk,
38  const reco::VertexRef& pvSlimmed,
39  const reco::VertexRefProd& pvSlimmedColl,
40  const TrkStatus& trkStatus,
41  const pat::PackedCandidate::PVAssociationQuality& pvAssocQuality,
43  std::pair<int, pat::PackedCandidate::PVAssociationQuality> associateTrkToVtx(const reco::VertexCollection& vertices,
44  const reco::TrackRef& trk) const;
45 
46  private:
55  const double minPt_;
56  const double minHits_;
57  const double minPixelHits_;
58  const double minPtToStoreProps_;
59  const int covarianceVersion_;
60  const std::vector<int> covariancePackingSchemas_;
61  std::vector<reco::TrackBase::TrackQuality> qualsToAutoAccept_;
64  const bool allowMuonId_;
70  const bool useLegacySetup_;
71  };
72 } // namespace pat
73 
75  : cands_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("inputCandidates"))),
77  iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
78  tracks_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("inputTracks"))),
79  vertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("secondaryVertices"))),
80  kshorts_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("kshorts"))),
81  lambdas_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("lambdas"))),
82  pv_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("primaryVertices"))),
83  pvOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
84  minPt_(iConfig.getParameter<double>("minPt")),
85  minHits_(iConfig.getParameter<uint32_t>("minHits")),
86  minPixelHits_(iConfig.getParameter<uint32_t>("minPixelHits")),
87  minPtToStoreProps_(iConfig.getParameter<double>("minPtToStoreProps")),
88  covarianceVersion_(iConfig.getParameter<int>("covarianceVersion")),
89  covariancePackingSchemas_(iConfig.getParameter<std::vector<int>>("covariancePackingSchemas")),
90  muons_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
91  passThroughCut_(iConfig.getParameter<std::string>("passThroughCut")),
92  allowMuonId_(iConfig.getParameter<bool>("allowMuonId")),
94  iConfig.getParameter<edm::ParameterSet>("pvAssignment").getParameter<double>("maxDzForPrimaryAssignment")),
96  iConfig.getParameter<edm::ParameterSet>("pvAssignment").getParameter<double>("maxDzSigForPrimaryAssignment")),
97  maxDzErrorForPrimaryAssignment_(iConfig.getParameter<edm::ParameterSet>("pvAssignment")
98  .getParameter<double>("maxDzErrorForPrimaryAssignment")),
99  maxDxyForNotReconstructedPrimary_(iConfig.getParameter<edm::ParameterSet>("pvAssignment")
100  .getParameter<double>("maxDxyForNotReconstructedPrimary")),
101  maxDxySigForNotReconstructedPrimary_(iConfig.getParameter<edm::ParameterSet>("pvAssignment")
102  .getParameter<double>("maxDxySigForNotReconstructedPrimary")),
103  useLegacySetup_(iConfig.getParameter<bool>("useLegacySetup")) {
104  std::vector<std::string> trkQuals(iConfig.getParameter<std::vector<std::string>>("qualsToAutoAccept"));
106  trkQuals.begin(), trkQuals.end(), std::back_inserter(qualsToAutoAccept_), reco::TrackBase::qualityByName);
107 
109  qualsToAutoAccept_.end()) {
110  std::ostringstream msg;
111  msg << " PATLostTracks has a quality requirement which resolves to undefQuality. This usually means a typo and is "
112  "therefore treated a config error\nquality requirements:\n ";
113  for (const auto& trkQual : trkQuals)
114  msg << trkQual << " ";
115  throw cms::Exception("Configuration") << msg.str();
116  }
117 
118  produces<std::vector<reco::Track>>();
119  produces<std::vector<pat::PackedCandidate>>();
120  produces<std::vector<pat::PackedCandidate>>("eleTracks");
121  produces<edm::Association<pat::PackedCandidateCollection>>();
122 }
123 
125 
128  iEvent.getByToken(cands_, cands);
129 
131  iEvent.getByToken(map_, pf2pc);
132 
134  iEvent.getByToken(tracks_, tracks);
135 
137  iEvent.getByToken(vertices_, vertices);
138 
140  iEvent.getByToken(muons_, muons);
141 
143  iEvent.getByToken(kshorts_, kshorts);
145  iEvent.getByToken(lambdas_, lambdas);
146 
148  iEvent.getByToken(pv_, pvs);
149  reco::VertexRef pv(pvs.id());
150  reco::VertexRefProd pvRefProd(pvs);
152  iEvent.getByToken(pvOrigs_, pvOrigs);
153 
154  auto outPtrTrks = std::make_unique<std::vector<reco::Track>>();
155  auto outPtrTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
156  auto outPtrEleTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
157 
158  std::vector<TrkStatus> trkStatus(tracks->size(), TrkStatus::NOTUSED);
159  //Mark all tracks used in candidates
160  //check if packed candidates are storing the tracks by seeing if number of hits >0
161  //currently we dont use that information though
162  //electrons will never store their track (they store the GSF track)
163  for (unsigned int ic = 0, nc = cands->size(); ic < nc; ++ic) {
165  const reco::PFCandidate& cand = (*cands)[ic];
166  if (cand.charge() && cand.trackRef().isNonnull() && cand.trackRef().id() == tracks.id()) {
167  if (cand.pdgId() == 11)
168  trkStatus[cand.trackRef().key()] = TrkStatus::PFELECTRON;
169  else if (cand.pdgId() == -11)
170  trkStatus[cand.trackRef().key()] = TrkStatus::PFPOSITRON;
171  else if ((*pf2pc)[r]->numberOfHits() > 0)
172  trkStatus[cand.trackRef().key()] = TrkStatus::PFCAND;
173  else
174  trkStatus[cand.trackRef().key()] = TrkStatus::PFCANDNOTRKPROPS;
175  }
176  }
177 
178  //Mark all tracks used in secondary vertices
179  for (const auto& secVert : *vertices) {
180  for (auto trkIt = secVert.tracks_begin(); trkIt != secVert.tracks_end(); trkIt++) {
181  if (trkStatus[trkIt->key()] == TrkStatus::NOTUSED)
182  trkStatus[trkIt->key()] = TrkStatus::VTX;
183  }
184  }
185  for (const auto& v0 : *kshorts) {
186  for (size_t dIdx = 0; dIdx < v0.numberOfDaughters(); dIdx++) {
187  size_t key = (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
188  if (trkStatus[key] == TrkStatus::NOTUSED)
189  trkStatus[key] = TrkStatus::VTX;
190  }
191  }
192  for (const auto& v0 : *lambdas) {
193  for (size_t dIdx = 0; dIdx < v0.numberOfDaughters(); dIdx++) {
194  size_t key = (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
195  if (trkStatus[key] == TrkStatus::NOTUSED)
196  trkStatus[key] = TrkStatus::VTX;
197  }
198  }
199  std::vector<int> mapping(tracks->size(), -1);
200  int lostTrkIndx = 0;
201  for (unsigned int trkIndx = 0; trkIndx < tracks->size(); trkIndx++) {
202  reco::TrackRef trk(tracks, trkIndx);
203  if (trkStatus[trkIndx] == TrkStatus::VTX || (trkStatus[trkIndx] == TrkStatus::NOTUSED && passTrkCuts(*trk))) {
204  outPtrTrks->emplace_back(*trk);
205  //association to PV
206  std::pair<int, pat::PackedCandidate::PVAssociationQuality> pvAsso = associateTrkToVtx(*pvOrigs, trk);
207  const reco::VertexRef& pvOrigRef = reco::VertexRef(pvOrigs, pvAsso.first);
208  if (pvOrigRef.isNonnull()) {
209  pv = reco::VertexRef(pvs, pvOrigRef.key()); // WARNING: assume the PV slimmer is keeping same order
210  } else if (!pvs->empty()) {
211  pv = reco::VertexRef(pvs, 0);
212  }
213  addPackedCandidate(*outPtrTrksAsCands, trk, pv, pvRefProd, trkStatus[trkIndx], pvAsso.second, muons);
214 
215  //for creating the reco::Track -> pat::PackedCandidate map
216  //not done for the lostTrack:eleTracks collection
217  mapping[trkIndx] = lostTrkIndx;
218  lostTrkIndx++;
219  } else if ((trkStatus[trkIndx] == TrkStatus::PFELECTRON || trkStatus[trkIndx] == TrkStatus::PFPOSITRON) &&
220  passTrkCuts(*trk)) {
221  //association to PV
222  std::pair<int, pat::PackedCandidate::PVAssociationQuality> pvAsso = associateTrkToVtx(*pvOrigs, trk);
223  const reco::VertexRef& pvOrigRef = reco::VertexRef(pvOrigs, pvAsso.first);
224  if (pvOrigRef.isNonnull()) {
225  pv = reco::VertexRef(pvs, pvOrigRef.key()); // WARNING: assume the PV slimmer is keeping same order
226  } else if (!pvs->empty()) {
227  pv = reco::VertexRef(pvs, 0);
228  }
229  addPackedCandidate(*outPtrEleTrksAsCands, trk, pv, pvRefProd, trkStatus[trkIndx], pvAsso.second, muons);
230  }
231  }
232 
233  iEvent.put(std::move(outPtrTrks));
234  iEvent.put(std::move(outPtrEleTrksAsCands), "eleTracks");
235  edm::OrphanHandle<pat::PackedCandidateCollection> oh = iEvent.put(std::move(outPtrTrksAsCands));
236  auto tk2pc = std::make_unique<edm::Association<pat::PackedCandidateCollection>>(oh);
238  tk2pcFiller.insert(tracks, mapping.begin(), mapping.end());
239  tk2pcFiller.fill();
240  iEvent.put(std::move(tk2pc));
241 }
242 
244  const bool passTrkHits = tr.pt() > minPt_ && tr.numberOfValidHits() >= minHits_ &&
246  const bool passTrkQual = passesQuality(tr, qualsToAutoAccept_);
247 
248  return passTrkHits || passTrkQual || passThroughCut_(tr);
249 }
250 
251 void pat::PATLostTracks::addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
252  const reco::TrackRef& trk,
253  const reco::VertexRef& pvSlimmed,
254  const reco::VertexRefProd& pvSlimmedColl,
255  const pat::PATLostTracks::TrkStatus& trkStatus,
256  const pat::PackedCandidate::PVAssociationQuality& pvAssocQuality,
258  const float mass = 0.13957018;
259 
260  int id = 211 * trk->charge();
261  if (trkStatus == TrkStatus::PFELECTRON)
262  id = 11;
263  else if (trkStatus == TrkStatus::PFPOSITRON)
264  id = -11;
265 
266  // assign the proper pdgId for tracks that are reconstructed as a muon
267  if (allowMuonId_) {
268  for (auto& mu : *muons) {
269  if (reco::TrackRef(mu.innerTrack()) == trk) {
270  id = -13 * trk->charge();
271  break;
272  }
273  }
274  }
275 
276  reco::Candidate::PolarLorentzVector p4(trk->pt(), trk->eta(), trk->phi(), mass);
277  cands.emplace_back(
278  pat::PackedCandidate(p4, trk->vertex(), trk->pt(), trk->eta(), trk->phi(), id, pvSlimmedColl, pvSlimmed.key()));
279 
280  cands.back().setTrackHighPurity(trk->quality(reco::TrackBase::highPurity));
281 
282  if (trk->pt() > minPtToStoreProps_ || trkStatus == TrkStatus::VTX) {
283  if (useLegacySetup_ || std::abs(id) == 11 || trkStatus == TrkStatus::VTX) {
284  cands.back().setTrackProperties(*trk, covariancePackingSchemas_[4], covarianceVersion_);
285  } else {
286  if (trk->hitPattern().numberOfValidPixelHits() > 0) {
287  cands.back().setTrackProperties(
288  *trk, covariancePackingSchemas_[0], covarianceVersion_); // high quality with pixels
289  } else {
290  cands.back().setTrackProperties(
291  *trk, covariancePackingSchemas_[1], covarianceVersion_); // high quality without pixels
292  }
293  }
294  } else if (!useLegacySetup_ && trk->pt() > 0.5) {
295  if (trk->hitPattern().numberOfValidPixelHits() > 0) {
296  cands.back().setTrackProperties(
297  *trk, covariancePackingSchemas_[2], covarianceVersion_); // low quality with pixels
298  } else {
299  cands.back().setTrackProperties(
300  *trk, covariancePackingSchemas_[3], covarianceVersion_); // low quality without pixels
301  }
302  }
303  cands.back().setAssociationQuality(pvAssocQuality);
304 }
305 
306 std::pair<int, pat::PackedCandidate::PVAssociationQuality> pat::PATLostTracks::associateTrkToVtx(
307  const reco::VertexCollection& vertices, const reco::TrackRef& trk) const {
308  //For legacy setup check only if the track is used in fit of the PV, i.e. vertices[0],
309  //and associate quality if weight > 0.5. Otherwise return invalid vertex index (-1)
310  //and default quality flag (NotReconstructedPrimary = 0)
311  if (useLegacySetup_) {
312  float w = vertices[0].trackWeight(trk);
313  if (w > 0.5) {
314  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(0, pat::PackedCandidate::UsedInFitTight);
315  } else if (w > 0.) {
316  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(0,
318  } else {
319  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(-1,
321  }
322  }
323 
324  //Inspired by CommonTools/RecoAlgos/interface/PrimaryVertexAssignment.h
325  //but without specific association for secondaries in jets and option to use timing
326 
327  int iVtxMaxWeight = -1;
328  int iVtxMinDzDist = -1;
329  size_t idx = 0;
330  float maxWeight = 0;
332  double minDzSig = std::numeric_limits<double>::max();
333  for (auto const& vtx : vertices) {
334  float w = vtx.trackWeight(trk);
335  double dz = std::abs(trk->dz(vtx.position()));
336  double dzSig = dz / trk->dzError();
337  if (w > maxWeight) {
338  maxWeight = w;
339  iVtxMaxWeight = idx;
340  }
341  if (dzSig < minDzSig) {
342  minDzSig = dzSig;
343  minDz = dz;
344  iVtxMinDzDist = idx;
345  }
346  idx++;
347  }
348  // vertex in which fit the track was used
349  if (iVtxMaxWeight >= 0) {
350  if (maxWeight > 0.5) {
351  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(iVtxMaxWeight,
353  } else {
354  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(iVtxMaxWeight,
356  }
357  }
358  // vertex "closest in Z" with tight cuts (targetting primary particles)
359  if (minDz < maxDzForPrimaryAssignment_) {
360  const double add_cov = vertices[iVtxMinDzDist].covariance(2, 2);
361  const double dzErr = sqrt(trk->dzError() * trk->dzError() + add_cov);
362  if (minDz / dzErr < maxDzSigForPrimaryAssignment_ && trk->dzError() < maxDzErrorForPrimaryAssignment_) {
363  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(iVtxMinDzDist,
365  }
366  }
367  // if the track is not compatible with other PVs but is compatible with the BeamSpot, we may simply have not reco'ed the PV!
368  // we still point it to the closest in Z, but flag it as possible orphan-primary
369  if (!vertices.empty() && std::abs(trk->dxy(vertices[0].position())) < maxDxyForNotReconstructedPrimary_ &&
370  std::abs(trk->dxy(vertices[0].position()) / trk->dxyError()) < maxDxySigForNotReconstructedPrimary_)
371  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(iVtxMinDzDist,
373  // for tracks not associated to any PV return the closest in dz
374  return std::pair<int, pat::PackedCandidate::PVAssociationQuality>(iVtxMinDzDist, pat::PackedCandidate::OtherDeltaZ);
375 }
376 
377 using pat::PATLostTracks;
T getParameter(std::string const &) const
int pdgId() const final
PDG identifier.
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
std::vector< VertexCompositeCandidate > VertexCompositeCandidateCollection
collection of Candidate objects
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
const double minPt_
const double minPtToStoreProps_
const double w
Definition: UKUtility.cc:23
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
~PATLostTracks() override
std::vector< pat::PackedCandidate > PackedCandidateCollection
const double maxDzSigForPrimaryAssignment_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
int charge() const final
electric charge
Definition: LeafCandidate.h:91
key_type key() const
Accessor for product key.
Definition: Ref.h:263
PATLostTracks(const edm::ParameterSet &)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const double maxDxyForNotReconstructedPrimary_
const int covarianceVersion_
ProductID id() const
Accessor for product ID.
Definition: Ref.h:257
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
const double minHits_
const edm::EDGetTokenT< reco::PFCandidateCollection > cands_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const double maxDzErrorForPrimaryAssignment_
const edm::EDGetTokenT< reco::MuonCollection > muons_
const bool useLegacySetup_
T sqrt(T t)
Definition: SSEVec.h:18
double p4[4]
Definition: TauolaWrapper.h:92
double pt() const
track transverse momentum
Definition: TrackBase.h:660
const bool allowMuonId_
const edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > lambdas_
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:901
const int mu
Definition: Constants.h:22
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
std::pair< int, pat::PackedCandidate::PVAssociationQuality > associateTrkToVtx(const reco::VertexCollection &vertices, const reco::TrackRef &trk) const
const double maxDzForPrimaryAssignment_
const double maxDxySigForNotReconstructedPrimary_
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:134
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > map_
void addPackedCandidate(std::vector< pat::PackedCandidate > &cands, const reco::TrackRef &trk, const reco::VertexRef &pvSlimmed, const reco::VertexRefProd &pvSlimmedColl, const TrkStatus &trkStatus, const pat::PackedCandidate::PVAssociationQuality &pvAssocQuality, edm::Handle< reco::MuonCollection > muons) const
bool passTrkCuts(const reco::Track &tr) const
const edm::EDGetTokenT< reco::TrackCollection > tracks_
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:485
const edm::EDGetTokenT< reco::VertexCollection > vertices_
const std::vector< int > covariancePackingSchemas_
tuple msg
Definition: mps_check.py:285
bool quality(const TrackQuality) const
Track quality.
Definition: TrackBase.h:549
const edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > kshorts_
std::vector< Muon > MuonCollection
Definition: Muon.h:35
std::vector< reco::TrackBase::TrackQuality > qualsToAutoAccept_
StringCutObjectSelector< reco::Track, false > passThroughCut_
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
int numberOfValidPixelHits() const
Definition: HitPattern.h:916
const double minPixelHits_
const edm::EDGetTokenT< reco::VertexCollection > pv_
def move(src, dest)
Definition: eostools.py:511
const edm::EDGetTokenT< reco::VertexCollection > pvOrigs_
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:39