CMS 3D CMS Logo

PackedCandidate.h
Go to the documentation of this file.
1 #ifndef __DataFormats_PatCandidates_PackedCandidate_h__
2 #define __DataFormats_PatCandidates_PackedCandidate_h__
3 
13 #include <atomic>
14 #include <mutex>
15 
16 /* #include "DataFormats/Math/interface/PtEtaPhiMass.h" */
17 
18 // forward declare testing structure
19 class testPackedCandidate;
20 
21 namespace pat {
23 public:
34 
35  typedef unsigned int index;
38  : packedPt_(0), packedEta_(0), packedPhi_(0), packedM_(0), packedDxy_(0),
44  p4_(new PolarLorentzVector(0, 0, 0, 0)),
45  p4c_(new LorentzVector(0, 0, 0, 0)), vertex_(new Point(0, 0, 0)),
46  dphi_(0), deta_(0), dtrkpt_(0), track_(nullptr), pdgId_(0),
47  qualityFlags_(0), pvRefKey_(reco::VertexRef::invalidKey()), m_(nullptr),
50 
52  const reco::VertexRefProd &pvRefProd,
58  p4_(new PolarLorentzVector(c.pt(), c.eta(), c.phi(), c.mass())),
59  p4c_(new LorentzVector(*p4_)), vertex_(new Point(c.vertex())), dphi_(0),
60  deta_(0), dtrkpt_(0), track_(nullptr), pdgId_(c.pdgId()),
61  qualityFlags_(0), pvRefProd_(pvRefProd), pvRefKey_(pvRefKey),
64  packBoth();
65  }
66 
67  explicit PackedCandidate(const PolarLorentzVector &p4, const Point &vtx,
68  float trkPt, float etaAtVtx, float phiAtVtx,
69  int pdgId, const reco::VertexRefProd &pvRefProd,
74  isIsolatedChargedHadron_(false), p4_(new PolarLorentzVector(p4)),
75  p4c_(new LorentzVector(*p4_)), vertex_(new Point(vtx)),
76  dphi_(reco::deltaPhi(phiAtVtx, p4_.load()->phi())),
77  deta_(std::abs(etaAtVtx - p4_.load()->eta()) >= kMinDEtaToStore_
78  ? etaAtVtx - p4_.load()->eta()
79  : 0.),
80  dtrkpt_(std::abs(trkPt - p4_.load()->pt()) >= kMinDTrkPtToStore_
81  ? trkPt - p4_.load()->pt()
82  : 0.),
83  track_(nullptr), pdgId_(pdgId), qualityFlags_(0), pvRefProd_(pvRefProd),
84  pvRefKey_(pvRefKey), m_(nullptr), packedHits_(0), packedLayers_(0),
86  firstHit_(0) {
87  packBoth();
88  }
89 
90  explicit PackedCandidate(const LorentzVector &p4, const Point &vtx,
91  float trkPt, float etaAtVtx, float phiAtVtx,
92  int pdgId, const reco::VertexRefProd &pvRefProd,
98  p4_(new PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M())),
99  p4c_(new LorentzVector(p4)), vertex_(new Point(vtx)),
100  dphi_(reco::deltaPhi(phiAtVtx, p4_.load()->phi())),
101  deta_(std::abs(etaAtVtx - p4_.load()->eta()) >= kMinDEtaToStore_
102  ? etaAtVtx - p4_.load()->eta()
103  : 0.),
104  dtrkpt_(std::abs(trkPt - p4_.load()->pt()) >= kMinDTrkPtToStore_
105  ? trkPt - p4_.load()->pt()
106  : 0.),
107  track_(nullptr), pdgId_(pdgId), qualityFlags_(0), pvRefProd_(pvRefProd),
108  pvRefKey_(pvRefKey), m_(nullptr), packedHits_(0), packedLayers_(0),
110  firstHit_(0) {
111  packBoth();
112  }
113 
115  : packedPt_(iOther.packedPt_), packedEta_(iOther.packedEta_),
116  packedPhi_(iOther.packedPhi_), packedM_(iOther.packedM_),
117  packedDxy_(iOther.packedDxy_), packedDz_(iOther.packedDz_),
129  // Need to trigger unpacking in iOther
130  p4_(new PolarLorentzVector(iOther.polarP4())),
131  p4c_(new LorentzVector(iOther.p4())),
132  vertex_(new Point(iOther.vertex())), dxy_(iOther.dxy_), dz_(iOther.dz_),
133  dphi_(iOther.dphi_), deta_(iOther.deta_), dtrkpt_(iOther.dtrkpt_),
134  track_(iOther.track_ ? new reco::Track(*iOther.track_) : nullptr),
135  pdgId_(iOther.pdgId_), qualityFlags_(iOther.qualityFlags_),
136  pvRefProd_(iOther.pvRefProd_), pvRefKey_(iOther.pvRefKey_),
137  m_(iOther.m_ ? new reco::TrackBase::CovarianceMatrix(*iOther.m_)
138  : nullptr),
143  firstHit_(iOther.firstHit_) {}
144 
146  : packedPt_(iOther.packedPt_), packedEta_(iOther.packedEta_),
147  packedPhi_(iOther.packedPhi_), packedM_(iOther.packedM_),
148  packedDxy_(iOther.packedDxy_), packedDz_(iOther.packedDz_),
160  p4_(iOther.p4_.exchange(nullptr)), p4c_(iOther.p4c_.exchange(nullptr)),
161  vertex_(iOther.vertex_.exchange(nullptr)), dxy_(iOther.dxy_),
162  dz_(iOther.dz_), dphi_(iOther.dphi_), deta_(iOther.deta_),
163  dtrkpt_(iOther.dtrkpt_), track_(iOther.track_.exchange(nullptr)),
164  pdgId_(iOther.pdgId_), qualityFlags_(iOther.qualityFlags_),
165  pvRefProd_(std::move(iOther.pvRefProd_)), pvRefKey_(iOther.pvRefKey_),
166  m_(iOther.m_.exchange(nullptr)), packedHits_(iOther.packedHits_),
171  firstHit_(iOther.firstHit_) {}
172 
174  if (this == &iOther) {
175  return *this;
176  }
177  packedPt_ = iOther.packedPt_;
178  packedEta_ = iOther.packedEta_;
179  packedPhi_ = iOther.packedPhi_;
180  packedM_ = iOther.packedM_;
181  packedDxy_ = iOther.packedDxy_;
182  packedDz_ = iOther.packedDz_;
183  packedDPhi_ = iOther.packedDPhi_;
184  packedDEta_ = iOther.packedDEta_;
185  packedDTrkPt_ = iOther.packedDTrkPt_;
191  caloFraction_ = iOther.caloFraction_;
192  hcalFraction_ = iOther.hcalFraction_;
193  packedTime_ = iOther.packedTime_;
196  // Need to trigger unpacking in iOther
197  if (p4_) {
198  *p4_ = iOther.polarP4();
199  } else {
200  p4_.store(new PolarLorentzVector(iOther.polarP4()));
201  }
202  if (p4c_) {
203  *p4c_ = iOther.p4();
204  } else {
205  p4c_.store(new LorentzVector(iOther.p4()));
206  }
207  if (vertex_) {
208  *vertex_ = iOther.vertex();
209  } else {
210  vertex_.store(new Point(iOther.vertex()));
211  }
212  dxy_ = iOther.dxy_;
213  dz_ = iOther.dz_;
214  dphi_ = iOther.dphi_;
215  deta_ = iOther.deta_;
216  dtrkpt_ = iOther.dtrkpt_;
217 
218  if (!iOther.track_) {
219  delete track_.exchange(nullptr);
220  } else {
221  if (!track_) {
222  track_.store(new reco::Track(*iOther.track_));
223  } else {
224  *track_ = *(iOther.track_);
225  }
226  }
227 
228  pdgId_ = iOther.pdgId_;
229  qualityFlags_ = iOther.qualityFlags_;
230  pvRefProd_ = iOther.pvRefProd_;
231  pvRefKey_ = iOther.pvRefKey_;
232  if (!iOther.m_) {
233  delete m_.exchange(nullptr);
234  } else {
235  if (!m_) {
236  m_.store(new reco::Track::CovarianceMatrix(*iOther.m_));
237  } else {
238  *m_ = *(iOther.m_);
239  }
240  }
241 
242  packedHits_ = iOther.packedHits_;
243  packedLayers_ = iOther.packedLayers_;
247  firstHit_ = iOther.firstHit_;
248  return *this;
249  }
250 
252  if (this == &iOther) {
253  return *this;
254  }
255  packedPt_ = iOther.packedPt_;
256  packedEta_ = iOther.packedEta_;
257  packedPhi_ = iOther.packedPhi_;
258  packedM_ = iOther.packedM_;
259  packedDxy_ = iOther.packedDxy_;
260  packedDz_ = iOther.packedDz_;
261  packedDPhi_ = iOther.packedDPhi_;
262  packedDEta_ = iOther.packedDEta_;
263  packedDTrkPt_ = iOther.packedDTrkPt_;
264  packedCovariance_ = iOther.packedCovariance_;
265  packedPuppiweight_ = iOther.packedPuppiweight_;
266  packedPuppiweightNoLepDiff_ = iOther.packedPuppiweightNoLepDiff_;
267  rawCaloFraction_ = iOther.rawCaloFraction_;
268  rawHcalFraction_ = iOther.rawHcalFraction_;
269  caloFraction_ = iOther.caloFraction_;
270  hcalFraction_ = iOther.hcalFraction_;
271  packedTime_ = iOther.packedTime_;
272  packedTimeError_ = iOther.packedTimeError_;
273  isIsolatedChargedHadron_ = iOther.isIsolatedChargedHadron_;
274  delete p4_.exchange(iOther.p4_.exchange(nullptr));
275  delete p4c_.exchange(iOther.p4c_.exchange(nullptr));
276  delete vertex_.exchange(iOther.vertex_.exchange(nullptr));
277  dxy_ = iOther.dxy_;
278  dz_ = iOther.dz_;
279  dphi_ = iOther.dphi_;
280  deta_ = iOther.deta_;
281  dtrkpt_ = iOther.dtrkpt_;
282  delete track_.exchange(iOther.track_.exchange(nullptr));
283  pdgId_ = iOther.pdgId_;
284  qualityFlags_ = iOther.qualityFlags_;
285  pvRefProd_ = iOther.pvRefProd_;
286  pvRefKey_ = iOther.pvRefKey_;
287  delete m_.exchange(iOther.m_.exchange(nullptr));
288  packedHits_ = iOther.packedHits_;
289  packedLayers_ = iOther.packedLayers_;
290  normalizedChi2_ = iOther.normalizedChi2_;
291  covarianceVersion_ = iOther.covarianceVersion_;
292  covarianceSchema_ = iOther.covarianceSchema_;
293  firstHit_ = iOther.firstHit_;
294  return *this;
295  }
296 
298  ~PackedCandidate() override;
300  size_t numberOfDaughters() const override;
302  const reco::Candidate *daughter(size_type) const override;
304  size_t numberOfMothers() const override;
306  const reco::Candidate *mother(size_type) const override;
310  reco::Candidate *daughter(const std::string &s) override;
312  const reco::Candidate *daughter(const std::string &s) const override;
315  size_t numberOfSourceCandidatePtrs() const override { return 0; }
319  return reco::CandidatePtr();
320  }
321 
323  int charge() const override {
324  switch (abs(pdgId_)) {
325  case 211:
326  return (pdgId_ > 0) - (pdgId_ < 0);
327  case 11:
328  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // e
329  case 13:
330  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // mu
331  case 15:
332  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // tau
333  case 24:
334  return (pdgId_ > 0) - (pdgId_ < 0); // W
335  default:
336  return 0; // FIXME: charge is not defined
337  }
338  }
340  void setCharge(int charge) override {}
342  int threeCharge() const override { return charge() * 3; }
344  void setThreeCharge(int threecharge) override {}
346  const LorentzVector &p4() const override {
347  if (!p4c_)
348  unpack();
349  return *p4c_;
350  }
352  const PolarLorentzVector &polarP4() const override {
353  if (!p4c_)
354  unpack();
355  return *p4_;
356  }
358  Vector momentum() const override {
359  if (!p4c_)
360  unpack();
361  return p4c_.load()->Vect();
362  }
365  Vector boostToCM() const override {
366  if (!p4c_)
367  unpack();
368  return p4c_.load()->BoostToCM();
369  }
371  double p() const override {
372  if (!p4c_)
373  unpack();
374  return p4c_.load()->P();
375  }
377  double energy() const override {
378  if (!p4c_)
379  unpack();
380  return p4c_.load()->E();
381  }
383  double et() const override { return (pt() <= 0) ? 0 : p4c_.load()->Et(); }
385  double et2() const override { return (pt() <= 0) ? 0 : p4c_.load()->Et2(); }
387  double mass() const override {
388  if (!p4c_)
389  unpack();
390  return p4_.load()->M();
391  }
393  double massSqr() const override {
394  if (!p4c_)
395  unpack();
396  auto m = p4_.load()->M();
397  return m * m;
398  }
399 
401  double mt() const override {
402  if (!p4c_)
403  unpack();
404  return p4_.load()->Mt();
405  }
407  double mtSqr() const override {
408  if (!p4c_)
409  unpack();
410  return p4_.load()->Mt2();
411  }
413  double px() const override {
414  if (!p4c_)
415  unpack();
416  return p4c_.load()->Px();
417  }
419  double py() const override {
420  if (!p4c_)
421  unpack();
422  return p4c_.load()->Py();
423  }
425  double pz() const override {
426  if (!p4c_)
427  unpack();
428  return p4c_.load()->Pz();
429  }
431  double pt() const override {
432  if (!p4c_)
433  unpack();
434  return p4_.load()->Pt();
435  }
437  double phi() const override {
438  if (!p4c_)
439  unpack();
440  return p4_.load()->Phi();
441  }
442 
444  virtual double ptTrk() const {
445  maybeUnpackBoth();
446  return p4_.load()->pt() + dtrkpt_;
447  }
449  virtual float phiAtVtx() const {
450  maybeUnpackBoth();
451  float ret = p4_.load()->Phi() + dphi_;
452  while (ret > float(M_PI))
453  ret -= 2 * float(M_PI);
454  while (ret < -float(M_PI))
455  ret += 2 * float(M_PI);
456  return ret;
457  }
459  virtual float etaAtVtx() const {
460  maybeUnpackBoth();
461  return p4_.load()->eta() + deta_;
462  }
463 
465  double theta() const override {
466  if (!p4c_)
467  unpack();
468  return p4_.load()->Theta();
469  }
471  double eta() const override {
472  if (!p4c_)
473  unpack();
474  return p4_.load()->Eta();
475  }
477  double rapidity() const override {
478  if (!p4c_)
479  unpack();
480  return p4_.load()->Rapidity();
481  }
483  double y() const override {
484  if (!p4c_)
485  unpack();
486  return p4_.load()->Rapidity();
487  }
489  void setP4(const LorentzVector &p4) override {
490  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
491  // and x,y,z
492  dphi_ += polarP4().Phi() - p4.Phi();
493  deta_ += polarP4().Eta() - p4.Eta();
494  dtrkpt_ += polarP4().Pt() - p4.Pt();
495  *p4_ = PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M());
496  packBoth();
497  }
499  void setP4(const PolarLorentzVector &p4) override {
500  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
501  // and x,y,z
502  dphi_ += polarP4().Phi() - p4.Phi();
503  deta_ += polarP4().Eta() - p4.Eta();
504  dtrkpt_ += polarP4().Pt() - p4.Pt();
505  *p4_ = p4;
506  packBoth();
507  }
509  void setMass(double m) override {
510  if (!p4c_)
511  unpack();
512  *p4_ = PolarLorentzVector(p4_.load()->Pt(), p4_.load()->Eta(),
513  p4_.load()->Phi(), m);
514  pack();
515  }
516  void setPz(double pz) override {
517  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
518  // and x,y,z
519  *p4c_ = LorentzVector(p4c_.load()->Px(), p4c_.load()->Py(), pz,
520  p4c_.load()->E());
521  dphi_ += polarP4().Phi() - p4c_.load()->Phi();
522  deta_ += polarP4().Eta() - p4c_.load()->Eta();
523  dtrkpt_ += polarP4().Pt() - p4c_.load()->Pt();
524  *p4_ = PolarLorentzVector(p4c_.load()->Pt(), p4c_.load()->Eta(),
525  p4c_.load()->Phi(), p4c_.load()->M());
526  packBoth();
527  }
529 
530  // Note: mask is also the maximum value
535  };
536 
537  // set number of tracker hits and layers
538  virtual void setHits(const reco::Track &tk) {
539  // first we count the number of layers with hits
540  int numberOfPixelLayers_ = tk.hitPattern().pixelLayersWithMeasurement();
541  if (numberOfPixelLayers_ > trackPixelHitsMask)
542  numberOfPixelLayers_ = trackPixelHitsMask;
543  int numberOfStripLayers_ = tk.hitPattern().stripLayersWithMeasurement();
544  if (numberOfStripLayers_ > trackStripHitsMask)
545  numberOfStripLayers_ = trackStripHitsMask;
546  packedLayers_ = (numberOfPixelLayers_ & trackPixelHitsMask) |
547  (numberOfStripLayers_ << trackStripHitsShift);
548  // now we count number of additional hits, beyond the one-per-layer implied
549  // by packedLayers_
550  int numberOfPixelHits_ =
551  tk.hitPattern().numberOfValidPixelHits() - numberOfPixelLayers_;
552  if (numberOfPixelHits_ > trackPixelHitsMask)
553  numberOfPixelHits_ = trackPixelHitsMask;
554  int numberOfStripHits_ = tk.hitPattern().numberOfValidHits() -
555  numberOfPixelHits_ - numberOfPixelLayers_ -
556  numberOfStripLayers_;
557  if (numberOfStripHits_ > trackStripHitsMask)
558  numberOfStripHits_ = trackStripHitsMask;
559 
560  packedHits_ = (numberOfPixelHits_ & trackPixelHitsMask) |
561  (numberOfStripHits_ << trackStripHitsShift);
562  }
563 
564  virtual void
566  const reco::Track::CovarianceMatrix &covariance,
567  int quality, int covarianceVersion) {
571  setHits(tk);
572  packBoth();
573  packCovariance(covariance, false);
574  }
575 
576  // set track properties using quality and covarianceVersion to define the
577  // level of details in the cov. matrix
578  virtual void setTrackProperties(const reco::Track &tk, int quality,
579  int covarianceVersion) {
581  }
582 
583  int numberOfPixelHits() const {
585  }
586  int numberOfHits() const {
589  }
592  }
595  }
598  }
599  virtual void setCovarianceVersion(int v) { covarianceVersion_ = v; }
600  int covarianceVersion() const { return covarianceVersion_; }
601  int covarianceSchema() const { return covarianceSchema_; }
602 
604  const Point &vertex() const override {
605  maybeUnpackBoth();
606  return *vertex_;
607  } //{ if (fromPV_) return Point(0,0,0); else return Point(0,0,100); }
609  double vx() const override {
610  maybeUnpackBoth();
611  return vertex_.load()->X();
612  } //{ return 0; }
614  double vy() const override {
615  maybeUnpackBoth();
616  return vertex_.load()->Y();
617  } //{ return 0; }
619  double vz() const override {
620  maybeUnpackBoth();
621  return vertex_.load()->Z();
622  } //{ if (fromPV_) return 0; else return 100; }
624  void setVertex(const Point &vertex) override {
625  maybeUnpackBoth();
626  *vertex_ = vertex;
627  packVtx();
628  }
629 
632  enum PVAssoc { NoPV = 0, PVLoose = 1, PVTight = 2, PVUsedInFit = 3 };
633  const PVAssoc fromPV(size_t ipv = 0) const {
634  reco::VertexRef pvRef = vertexRef();
635  if (pvAssociationQuality() == UsedInFitTight and pvRef.key() == ipv)
636  return PVUsedInFit;
637  if (pvRef.key() == ipv or abs(pdgId()) == 13 or abs(pdgId()) == 11)
638  return PVTight;
640  std::abs(dzAssociatedPV()) > std::abs(dz(ipv)))
641  return PVTight; // it is not closest, but at least prevents the B
642  // assignment stealing
643  if (pvAssociationQuality() < UsedInFitLoose or pvRef->ndof() < 4.0)
644  return PVLoose;
645  return NoPV;
646  }
647 
658  };
662  }
666  }
667 
668  const reco::VertexRef vertexRef() const {
670  }
671 
673  virtual float dxy() const {
674  maybeUnpackBoth();
675  return dxy_;
676  }
678  virtual float dz(size_t ipv = 0) const {
679  maybeUnpackBoth();
680  return dz_ + (*pvRefProd_)[pvRefKey_].position().z() -
681  (*pvRefProd_)[ipv].position().z();
682  }
684  virtual float dzAssociatedPV() const {
685  maybeUnpackBoth();
686  return dz_;
687  }
689  virtual float dxy(const Point &p) const;
691  virtual float dz(const Point &p) const;
692 
694  float dzError() const override {
696  return sqrt((*m_.load())(4, 4));
697  }
699  float dxyError() const override {
701  return sqrt((*m_.load())(3, 3));
702  }
703 
706  virtual const reco::Track &pseudoTrack() const {
707  if (!track_)
708  unpackTrk();
709  return *track_;
710  }
711 
713  const reco::Track *bestTrack() const override {
714  if (packedHits_ != 0 || packedLayers_ != 0) {
716  return track_.load();
717  } else
718  return nullptr;
719  }
721  bool hasTrackDetails() const {
722  return (packedHits_ != 0 || packedLayers_ != 0);
723  }
724 
726  bool trackHighPurity() const {
728  }
731  qualityFlags_ =
733  ((highPurity << trackHighPurityShift) & trackHighPurityMask);
734  }
735 
739  noLostInnerHits = 0, // it could still not have a hit in the first layer,
740  // e.g. if it crosses an inactive sensor
743  };
745  return LostInnerHits(
747  }
749  int lost = hits;
750  if (lost > 2)
751  lost = 2; // protection against misuse
752  lost++; // shift so it's 0 .. 3 instead of (-1) .. 2
755  }
756 
758  void setFirstHit(uint16_t pattern) { firstHit_ = pattern; }
760  uint16_t firstHit() const { return firstHit_; }
761 
762  void setMuonID(bool isStandAlone, bool isGlobal) {
763  int16_t muonFlags = isStandAlone | (2 * isGlobal);
765  ((muonFlags << muonFlagsShift) & muonFlagsMask);
766  }
767 
768  void setGoodEgamma(bool isGoodEgamma = true) {
769  int16_t egFlags = (isGoodEgamma << egammaFlagsShift) & egammaFlagsMask;
771  }
772 
774  int pdgId() const override { return pdgId_; }
775  // set PDG identifier
776  void setPdgId(int pdgId) override { pdgId_ = pdgId; }
778  int status() const override { return qualityFlags_; } /*FIXME*/
780  void setStatus(int status) override {} /*FIXME*/
782  static const unsigned int longLivedTag = 0; /*FIXME*/
784  void setLongLived() override {} /*FIXME*/
786  bool longLived() const override;
788  static const unsigned int massConstraintTag = 0; /*FIXME*/
790  void setMassConstraint() override {} /*FIXME*/
792  bool massConstraint() const override;
793 
795  PackedCandidate *clone() const override { return new PackedCandidate(*this); }
796 
798  double vertexChi2() const override;
805  double vertexNdof() const override;
807  double vertexNormalizedChi2() const override;
809  double vertexCovariance(int i, int j) const override;
814  return m;
815  }
817  void fillVertexCovariance(CovarianceMatrix &v) const override;
820  bool hasMasterClone() const override;
824  const reco::CandidateBaseRef &masterClone() const override;
828  bool hasMasterClonePtr() const override;
832 
833  const reco::CandidatePtr &masterClonePtr() const override;
834 
836  template <typename Ref> Ref masterRef() const {
837  return masterClone().template castTo<Ref>();
838  }
839 
840  bool isElectron() const override { return false; }
841  bool isMuon() const override { return false; }
842  bool isStandAloneMuon() const override {
843  return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 1;
844  }
845  bool isGlobalMuon() const override {
846  return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 2;
847  }
848  bool isTrackerMuon() const override { return false; }
849  bool isCaloMuon() const override { return false; }
850  bool isPhoton() const override { return false; }
851  bool isConvertedPhoton() const override { return false; }
852  bool isJet() const override { return false; }
853  bool isGoodEgamma() const { return (qualityFlags_ & egammaFlagsMask) != 0; }
854 
855  // puppiweights
856  void setPuppiWeight(float p,
857  float p_nolep = 0.0);
858  float puppiWeight() const;
860  float puppiWeightNoLep() const;
861 
862  // for the neutral fractions
863  void
864  setRawCaloFraction(float p);
865  float rawCaloFraction() const {
867  return (rawCaloFraction_ / 100.);
868  }
869  void setRawHcalFraction(
870  float p);
871  float rawHcalFraction() const {
872  return (rawHcalFraction_ / 100.);
873  }
874  void setCaloFraction(
875  float p);
876  float caloFraction() const {
877  return (caloFraction_ / 100.);
878  }
879  void setHcalFraction(float p);
880  float hcalFraction() const {
882  return (hcalFraction_ / 100.);
883  }
884 
885  // isolated charged hadrons
887  bool p);
888  bool isIsolatedChargedHadron() const {
891  }
892 
896  : dxydxy(0), dxydz(0), dzdz(0), dlambdadz(0), dphidxy(0), dptdpt(0),
897  detadeta(0), dphidphi(0) {}
898  // 3D IP covariance
899  uint16_t dxydxy;
900  uint16_t dxydz;
901  uint16_t dzdz;
902  // other IP relevant elements
903  uint16_t dlambdadz;
904  uint16_t dphidxy;
905  // other diag elements
906  uint16_t dptdpt;
907  uint16_t detadeta;
908  uint16_t dphidphi;
909  };
910 
912  virtual float time() const { return vertexRef()->t() + dtimeAssociatedPV(); }
914  virtual float dtime(size_t ipv = 0) const {
915  return dtimeAssociatedPV() + (*pvRefProd_)[pvRefKey_].t() -
916  (*pvRefProd_)[ipv].t();
917  }
919  virtual float dtimeAssociatedPV() const {
920  if (packedTime_ == 0)
921  return 0.f;
922  if (packedTimeError_ > 0)
924  else
926  }
928  virtual float timeError() const { return unpackTimeError(packedTimeError_); }
930  void setDTimeAssociatedPV(float aTime, float aTimeError = 0);
932  void setTime(float aTime, float aTimeError = 0) {
933  setDTimeAssociatedPV(aTime - vertexRef()->t(), aTimeError);
934  }
935 
936 private:
938  uint16_t packed, int i, int j) const {
940  packed, covarianceSchema_, i, j, pt(), eta(), numberOfHits(),
942  }
944  int i, int j) const {
945  return covarianceParameterization().pack(m(i, j), covarianceSchema_, i, j,
946  pt(), eta(), numberOfHits(),
948  }
949 
950 protected:
951  friend class ::testPackedCandidate;
952  static constexpr float kMinDEtaToStore_ = 0.001;
953  static constexpr float kMinDTrkPtToStore_ = 0.001;
954 
958 
959  void pack(bool unpackAfterwards = true);
960  void unpack() const;
961  void packVtx(bool unpackAfterwards = true);
962  void unpackVtx() const;
964  bool unpackAfterwards = true);
965  void unpackCovariance() const;
966  void maybeUnpackBoth() const {
967  if (!p4c_)
968  unpack();
969  if (!vertex_)
970  unpackVtx();
971  }
972  void maybeUnpackTrack() const {
973  if (!track_)
974  unpackTrk();
975  }
976  void maybeUnpackCovariance() const {
977  if (!m_)
979  }
980  void packBoth() {
981  pack(false);
982  packVtx(false);
983  delete p4_.exchange(nullptr);
984  delete p4c_.exchange(nullptr);
985  delete vertex_.exchange(nullptr);
986  unpack();
987  unpackVtx();
988  } // do it this way, so that we don't loose precision on the angles before
989  // computing dxy,dz
990  void unpackTrk() const;
991 
993  int8_t packedPuppiweightNoLepDiff_; // storing the DIFFERENCE of (all - "no
994  // lep") for compression optimization
997  uint8_t caloFraction_;
999  int16_t packedTime_;
1001 
1003 
1005  mutable std::atomic<PolarLorentzVector *> p4_;
1006  mutable std::atomic<LorentzVector *> p4c_;
1008  mutable std::atomic<Point *> vertex_;
1009  CMS_THREAD_GUARD(vertex_) mutable float dxy_, dz_, dphi_, deta_, dtrkpt_;
1011  mutable std::atomic<reco::Track *> track_;
1013  int pdgId_;
1014  uint16_t qualityFlags_;
1018 
1020  mutable std::atomic<reco::TrackBase::CovarianceMatrix *> m_;
1021  uint8_t packedHits_,
1022  packedLayers_; // packedLayers_ -> layers with valid hits; packedHits_ ->
1023  // extra hits beyond the one-per-layer implied by
1024  // packedLayers_
1025 
1031  // static std::atomic<CovarianceParameterization*>
1032  // covarianceParameterization_;
1033  static std::once_flag covariance_load_flag;
1035  if (!hasTrackDetails())
1037  "Trying to access covariance matrix for a "
1038  "PackedCandidate for which it's not available. "
1039  "Check hasTrackDetails() before!\n");
1040  std::call_once(covariance_load_flag,
1041  [](int v) { covarianceParameterization_.load(v); },
1043  if (covarianceParameterization_.loadedVersion() != covarianceVersion_) {
1045  << "Attempting to load multiple covariance version in same process. "
1046  "This is not supported.";
1047  }
1049  }
1050 
1052  bool overlap(const reco::Candidate &) const override;
1053  template <typename, typename, typename> friend struct component;
1054  friend class ::OverlapChecker;
1057 
1065  muonFlagsMask = 0x0600,
1069  };
1070 
1072  static uint8_t packTimeError(float timeError);
1073  static float unpackTimeError(uint8_t timeError);
1074  static float unpackTimeNoError(int16_t time);
1075  static int16_t packTimeNoError(float time);
1076  static float unpackTimeWithError(int16_t time, uint8_t timeError);
1077  static int16_t packTimeWithError(float time, float timeError);
1078  static constexpr float MIN_TIMEERROR =
1079  0.002f; // 2 ps, smallest storable non-zero uncertainty
1081  0.0002f; // 0.2 ps, smallest non-zero time that can be stored by
1082  // packTimeNoError
1084  5; // power of 2 used in encoding timeError
1086  6; // power of 2 used in encoding time without timeError
1088  -6; // power of 2 used in encoding time with timeError
1089 public:
1090  uint16_t firstHit_;
1091 };
1092 
1093 typedef std::vector<pat::PackedCandidate> PackedCandidateCollection;
1096 } // namespace pat
1097 
1098 #endif
float puppiWeight() const
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
static float MIN_TIME_NOERROR
void setMuonID(bool isStandAlone, bool isGlobal)
void setStatus(int status) override
set status word
double theta() const override
momentum polar angle
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:167
float dxyError() const override
uncertainty on dxy
const reco::Candidate * daughter(size_type) const override
return daughter at a given position (throws an exception)
double px() const override
x coordinate of momentum vector
virtual float etaAtVtx() const
eta from the track (normally identical to eta())
void fillVertexCovariance(CovarianceMatrix &v) const override
fill SMatrix
bool isTrackerMuon() const override
int covarianceSchema() const
float puppiWeightNoLep() const
Weight from full PUPPI.
void setPuppiWeight(float p, float p_nolep=0.0)
void setThreeCharge(int threecharge) override
set electric charge
int numberOfHits() const
size_t size_type
Definition: Candidate.h:30
float unpack(uint16_t packed, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
CovarianceMatrix vertexCovariance() const override
return SMatrix
int pdgId() const override
PDG identifier.
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:594
uint16_t packCovarianceElement(const reco::TrackBase::CovarianceMatrix &m, int i, int j) const
float hcalFraction() const
bool isGoodEgamma() const
bool hasMasterClonePtr() const override
reco::VertexRef::key_type pvRefKey_
int pixelLayersWithMeasurement() const
void maybeUnpackBoth() const
static const unsigned int longLivedTag
long lived flag
int numberOfValidHits() const
Definition: HitPattern.h:896
PackedCandidate & operator=(const PackedCandidate &iOther)
static const unsigned int massConstraintTag
do mass constraint flag
std::atomic< LorentzVector * > p4c_
double vertexChi2() const override
chi-squares
bool isMuon() const override
bool longLived() const override
is long lived?
#define nullptr
static float unpackTimeNoError(int16_t time)
double vertexNdof() const override
std::vector< pat::PackedCandidate > PackedCandidateCollection
const LorentzVector & p4() const override
four-momentum Lorentz vecto r
math::XYZPoint Point
point in the space
virtual void setCovarianceVersion(int v)
void setRawCaloFraction(float p)
Weight from PUPPI removing leptons.
const reco::CandidatePtr & masterClonePtr() const override
float dzError() const override
uncertainty on dz
static int16_t packTimeNoError(float time)
PackedCandidate(const PackedCandidate &iOther)
double massSqr() const override
mass squared
bool isPhoton() const override
virtual float time() const
time (wrt nominal zero of the collision)
void unpackCovarianceElement(reco::TrackBase::CovarianceMatrix &m, uint16_t packed, int i, int j) const
PackedCovariance packedCovariance_
key_type key() const
Accessor for product key.
Definition: Ref.h:263
virtual float dtime(size_t ipv=0) const
dtime with respect to the PV[ipv]
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:538
static int16_t packTimeWithError(float time, float timeError)
Vector momentum() const override
spatial momentum vector
double vz() const override
z coordinate of vertex position
bool isConvertedPhoton() const override
bool isGlobalMuon() const override
const reco::Track * bestTrack() const override
return a pointer to the track if present. otherwise, return a null pointer
static uint8_t packTimeError(float timeError)
static to allow unit testing
const reco::VertexRef vertexRef() const
void setMassConstraint() override
set mass constraint flag
PackedCandidate(PackedCandidate &&iOther)
double pz() const override
z coordinate of momentum vector
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
PackedCandidate(const reco::Candidate &c, const reco::VertexRefProd &pvRefProd, reco::VertexRef::key_type pvRefKey)
float pack(float value, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
double py() const override
y coordinate of momentum vector
Ref masterRef() const
cast master clone reference to a concrete type
Definition: HeavyIon.h:7
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
reco::CandidateCollection daughters
collection of daughter candidates
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
bool massConstraint() const override
do mass constraint?
static float MIN_TIMEERROR
int status() const override
status word
int charge() const override
electric charge
void setP4(const LorentzVector &p4) override
set 4-momentum
size_t numberOfMothers() const override
number of mothers
double vy() const override
y coordinate of vertex position
Vector boostToCM() const override
double rapidity() const override
rapidity
const PVAssociationQuality pvAssociationQuality() const
void setHcalFraction(float p)
Fraction of ECAL+HCAL energy over candidate energy.
virtual float timeError() const
time measurement uncertainty (-1 if not available)
math::XYZTLorentzVector LorentzVector
Lorentz vector.
virtual void setTrackProperties(const reco::Track &tk, int quality, int covarianceVersion)
void maybeUnpackCovariance() const
const PVAssoc fromPV(size_t ipv=0) const
std::atomic< PolarLorentzVector * > p4_
the four vector
CovarianceMatrix covariance() const
return track covariance matrix
Definition: TrackBase.h:776
T sqrt(T t)
Definition: SSEVec.h:18
void setMass(double m) override
set particle mass
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< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
bool trackHighPurity() const
true if the track had the highPurity quality bit
const Point & vertex() const override
vertex position
std::atomic< Point * > vertex_
vertex position
void setPz(double pz) override
LostInnerHits lostInnerHits() const
void setLongLived() override
set long lived flag
PackedCandidate()
default constructor
edm::Ref< pat::PackedCandidateCollection > PackedCandidateRef
friend class ShallowCloneCandidate
void setP4(const PolarLorentzVector &p4) override
set 4-momentum
double mtSqr() const override
transverse mass squared
static CovarianceParameterization covarianceParameterization_
virtual double ptTrk() const
pt from the track (normally identical to pt())
#define CMS_THREAD_GUARD(_var_)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int stripLayersWithMeasurement() const
double energy() const override
energy
double vertexNormalizedChi2() const override
chi-squared divided by n.d.o.f.
virtual const reco::Track & pseudoTrack() const
#define CMS_THREAD_SAFE
~PackedCandidate() override
destructor
PackedCandidate(const PolarLorentzVector &p4, const Point &vtx, float trkPt, float etaAtVtx, float phiAtVtx, int pdgId, const reco::VertexRefProd &pvRefProd, reco::VertexRef::key_type pvRefKey)
double pt() const override
transverse momentum
virtual float dtimeAssociatedPV() const
dtime with respect to the PV ref
double p() const override
magnitude of momentum vector
double et() const override
transverse energy
float caloFraction() const
Set the fraction of ECAL+HCAL energy over candidate energy.
int threeCharge() const override
electric charge
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
virtual float dzAssociatedPV() const
dz with respect to the PV ref
int numberOfPixelHits() const
float rawHcalFraction() const
Set the fraction of Hcal needed isolated charged hadrons.
std::atomic< reco::Track * > track_
reco::Track
static int EXPO_TIME_WITHERROR
int trackerLayersWithMeasurement() const
#define M_PI
bool hasTrackDetails() const
Return true if a bestTrack can be extracted from this Candidate.
bool isJet() const override
std::atomic< reco::TrackBase::CovarianceMatrix * > m_
IP covariance.
LostInnerHits
Enumerator specifying the.
double eta() const override
momentum pseudorapidity
bool isCaloMuon() const override
double phi() const override
momentum azimuthal angle
void setGoodEgamma(bool isGoodEgamma=true)
uint8_t normalizedChi2_
track quality information
void setRawHcalFraction(float p)
Raw ECAL+HCAL energy over candidate energy for isolated charged hadrons.
edm::Ptr< Candidate > CandidatePtr
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:25
bool isStandAloneMuon() const override
void setTime(float aTime, float aTimeError=0)
set time measurement
size_t numberOfSourceCandidatePtrs() const override
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:479
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::RefVector< pat::PackedCandidateCollection > PackedCandidateRefVector
def load(fileName)
Definition: svgfig.py:547
int stripLayersWithMeasurement() const
Definition: HitPattern.h:1138
const reco::Candidate * mother(size_type) const override
return mother at a given position (throws an exception)
static std::once_flag covariance_load_flag
void setPdgId(int pdgId) override
int covarianceVersion() const
static float kMinDEtaToStore_
virtual float phiAtVtx() const
momentum azimuthal angle from the track (normally identical to phi())
void setFirstHit(uint16_t pattern)
Set first hit from HitPattern.
int pdgId_
PDG identifier.
double et2() const override
transverse energy squared (use this for cuts)!
edm::RefProd< VertexCollection > VertexRefProd
persistent reference to a Vertex
Definition: VertexFwd.h:15
double mass() const override
mass
float rawCaloFraction() const
void unpackCovariance() const
void setDTimeAssociatedPV(float aTime, float aTimeError=0)
set time measurement
friend class ShallowClonePtrCandidate
uint16_t firstHit() const
Return first hit from HitPattern for tracks with high level details.
fixed size matrix
PackedCandidate * clone() const override
returns a clone of the Candidate object
PackedCandidate & operator=(PackedCandidate &&iOther)
size_t numberOfDaughters() const override
number of daughters
void packVtx(bool unpackAfterwards=true)
void setLostInnerHits(LostInnerHits hits)
reco::CandidatePtr sourceCandidatePtr(size_type i) const override
PackedCandidate(const LorentzVector &p4, const Point &vtx, float trkPt, float etaAtVtx, float phiAtVtx, int pdgId, const reco::VertexRefProd &pvRefProd, reco::VertexRef::key_type pvRefKey)
trackHitShiftsAndMasks
set impact parameters covariance
void setVertex(const Point &vertex) override
set vertex
int numberOfValidPixelHits() const
Definition: HitPattern.h:916
void setIsIsolatedChargedHadron(bool p)
Fraction of Hcal for HF, neutral hadrons, and charged particles.
double mt() const override
transverse mass
void pack(bool unpackAfterwards=true)
double y() const override
rapidity
void packCovariance(const reco::TrackBase::CovarianceMatrix &m, bool unpackAfterwards=true)
const reco::CandidateBaseRef & masterClone() const override
virtual float dxy() const
dxy with respect to the PV ref
bool overlap(const reco::Candidate &) const override
check overlap with another Candidate
void setTrackHighPurity(bool highPurity)
set to true if the track had the highPurity quality bit
virtual void setTrackProperties(const reco::Track &tk, const reco::Track::CovarianceMatrix &covariance, int quality, int covarianceVersion)
static float unpackTimeWithError(int16_t time, uint8_t timeError)
void maybeUnpackTrack() const
virtual void setHits(const reco::Track &tk)
bool isIsolatedChargedHadron() const
const CovarianceParameterization & covarianceParameterization() const
bool isElectron() const override
bool hasMasterClone() const override
void setCharge(int charge) override
set electric charge
math::XYZVector Vector
point in the space
const PolarLorentzVector & polarP4() const override
four-momentum Lorentz vector
def move(src, dest)
Definition: eostools.py:511
static float unpackTimeError(uint8_t timeError)
double vx() const override
x coordinate of vertex position
#define constexpr
static float kMinDTrkPtToStore_
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77
void setCaloFraction(float p)
Fraction of Hcal for isolated charged hadrons.
reco::VertexRefProd pvRefProd_
Use these to build a Ref to primary vertex.
void setAssociationQuality(PVAssociationQuality q)