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),
39  packedEta_(0),
40  packedPhi_(0),
41  packedM_(0),
42  packedDxy_(0),
43  packedDz_(0),
44  packedDPhi_(0),
45  packedDEta_(0),
46  packedDTrkPt_(0),
52  caloFraction_(0),
53  hcalFraction_(0),
54  packedTime_(0),
57  p4_(new PolarLorentzVector(0, 0, 0, 0)),
58  p4c_(new LorentzVector(0, 0, 0, 0)),
59  vertex_(new Point(0, 0, 0)),
60  dphi_(0),
61  deta_(0),
62  dtrkpt_(0),
63  track_(nullptr),
64  pdgId_(0),
65  qualityFlags_(0),
66  pvRefKey_(reco::VertexRef::invalidKey()),
67  m_(nullptr),
68  packedHits_(0),
69  packedLayers_(0),
70  normalizedChi2_(0),
73  firstHit_(0) {}
74 
76  const reco::VertexRefProd &pvRefProd,
78  : packedPuppiweight_(0),
82  caloFraction_(0),
83  hcalFraction_(0),
84  packedTime_(0),
87  p4_(new PolarLorentzVector(c.pt(), c.eta(), c.phi(), c.mass())),
88  p4c_(new LorentzVector(*p4_)),
89  vertex_(new Point(c.vertex())),
90  dphi_(0),
91  deta_(0),
92  dtrkpt_(0),
93  track_(nullptr),
94  pdgId_(c.pdgId()),
95  qualityFlags_(0),
96  pvRefProd_(pvRefProd),
97  pvRefKey_(pvRefKey),
98  m_(nullptr),
99  packedHits_(0),
100  packedLayers_(0),
101  normalizedChi2_(0),
104  firstHit_(0) {
105  packBoth();
106  }
107 
109  const Point &vtx,
110  float trkPt,
111  float etaAtVtx,
112  float phiAtVtx,
113  int pdgId,
114  const reco::VertexRefProd &pvRefProd,
115  reco::VertexRef::key_type pvRefKey)
116  : packedPuppiweight_(0),
118  rawCaloFraction_(0),
119  rawHcalFraction_(0),
120  caloFraction_(0),
121  hcalFraction_(0),
122  packedTime_(0),
123  packedTimeError_(0),
125  p4_(new PolarLorentzVector(p4)),
126  p4c_(new LorentzVector(*p4_)),
127  vertex_(new Point(vtx)),
129  deta_(std::abs(etaAtVtx - p4_.load()->eta()) >= kMinDEtaToStore_ ? etaAtVtx - p4_.load()->eta() : 0.),
130  dtrkpt_(std::abs(trkPt - p4_.load()->pt()) >= kMinDTrkPtToStore_ ? trkPt - p4_.load()->pt() : 0.),
131  track_(nullptr),
132  pdgId_(pdgId),
133  qualityFlags_(0),
134  pvRefProd_(pvRefProd),
135  pvRefKey_(pvRefKey),
136  m_(nullptr),
137  packedHits_(0),
138  packedLayers_(0),
139  normalizedChi2_(0),
142  firstHit_(0) {
143  packBoth();
144  }
145 
147  const Point &vtx,
148  float trkPt,
149  float etaAtVtx,
150  float phiAtVtx,
151  int pdgId,
152  const reco::VertexRefProd &pvRefProd,
153  reco::VertexRef::key_type pvRefKey)
154  : packedPuppiweight_(0),
156  rawCaloFraction_(0),
157  rawHcalFraction_(0),
158  caloFraction_(0),
159  hcalFraction_(0),
160  packedTime_(0),
161  packedTimeError_(0),
163  p4_(new PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M())),
164  p4c_(new LorentzVector(p4)),
165  vertex_(new Point(vtx)),
167  deta_(std::abs(etaAtVtx - p4_.load()->eta()) >= kMinDEtaToStore_ ? etaAtVtx - p4_.load()->eta() : 0.),
168  dtrkpt_(std::abs(trkPt - p4_.load()->pt()) >= kMinDTrkPtToStore_ ? trkPt - p4_.load()->pt() : 0.),
169  track_(nullptr),
170  pdgId_(pdgId),
171  qualityFlags_(0),
172  pvRefProd_(pvRefProd),
173  pvRefKey_(pvRefKey),
174  m_(nullptr),
175  packedHits_(0),
176  packedLayers_(0),
177  normalizedChi2_(0),
180  firstHit_(0) {
181  packBoth();
182  }
183 
185  : packedPt_(iOther.packedPt_),
186  packedEta_(iOther.packedEta_),
187  packedPhi_(iOther.packedPhi_),
188  packedM_(iOther.packedM_),
189  packedDxy_(iOther.packedDxy_),
190  packedDz_(iOther.packedDz_),
191  packedDPhi_(iOther.packedDPhi_),
192  packedDEta_(iOther.packedDEta_),
201  packedTime_(iOther.packedTime_),
204  // Need to trigger unpacking in iOther
205  p4_(new PolarLorentzVector(iOther.polarP4())),
206  p4c_(new LorentzVector(iOther.p4())),
207  vertex_((iOther.vertex_ ? new Point(iOther.vertex()) : nullptr)),
208  dxy_(vertex_ ? iOther.dxy_ : 0),
209  dz_(vertex_ ? iOther.dz_ : 0),
210  dphi_(vertex_ ? iOther.dphi_ : 0),
211  deta_(vertex_ ? iOther.deta_ : 0),
212  dtrkpt_(vertex_ ? iOther.dtrkpt_ : 0),
213  track_(iOther.track_ ? new reco::Track(*iOther.track_) : nullptr),
214  pdgId_(iOther.pdgId_),
216  pvRefProd_(iOther.pvRefProd_),
217  pvRefKey_(iOther.pvRefKey_),
218  m_(iOther.m_ ? new reco::TrackBase::CovarianceMatrix(*iOther.m_) : nullptr),
219  packedHits_(iOther.packedHits_),
224  firstHit_(iOther.firstHit_),
225  trkAlgoPacked_(iOther.trkAlgoPacked_) {}
226 
228  : packedPt_(iOther.packedPt_),
229  packedEta_(iOther.packedEta_),
230  packedPhi_(iOther.packedPhi_),
231  packedM_(iOther.packedM_),
232  packedDxy_(iOther.packedDxy_),
233  packedDz_(iOther.packedDz_),
234  packedDPhi_(iOther.packedDPhi_),
235  packedDEta_(iOther.packedDEta_),
244  packedTime_(iOther.packedTime_),
247  p4_(iOther.p4_.exchange(nullptr)),
248  p4c_(iOther.p4c_.exchange(nullptr)),
249  vertex_(iOther.vertex_.exchange(nullptr)),
250  dxy_(iOther.dxy_),
251  dz_(iOther.dz_),
252  dphi_(iOther.dphi_),
253  deta_(iOther.deta_),
254  dtrkpt_(iOther.dtrkpt_),
255  track_(iOther.track_.exchange(nullptr)),
256  pdgId_(iOther.pdgId_),
258  pvRefProd_(iOther.pvRefProd_),
259  pvRefKey_(iOther.pvRefKey_),
260  m_(iOther.m_.exchange(nullptr)),
261  packedHits_(iOther.packedHits_),
266  firstHit_(iOther.firstHit_),
267  trkAlgoPacked_(iOther.trkAlgoPacked_) {}
268 
270  if (this == &iOther) {
271  return *this;
272  }
273  packedPt_ = iOther.packedPt_;
274  packedEta_ = iOther.packedEta_;
275  packedPhi_ = iOther.packedPhi_;
276  packedM_ = iOther.packedM_;
277  packedDxy_ = iOther.packedDxy_;
278  packedDz_ = iOther.packedDz_;
279  packedDPhi_ = iOther.packedDPhi_;
280  packedDEta_ = iOther.packedDEta_;
281  packedDTrkPt_ = iOther.packedDTrkPt_;
287  caloFraction_ = iOther.caloFraction_;
288  hcalFraction_ = iOther.hcalFraction_;
289  packedTime_ = iOther.packedTime_;
292  // Need to trigger unpacking in iOther
293  if (p4_) {
294  *p4_ = iOther.polarP4();
295  } else {
296  p4_.store(new PolarLorentzVector(iOther.polarP4()));
297  }
298  if (p4c_) {
299  *p4c_ = iOther.p4();
300  } else {
301  p4c_.store(new LorentzVector(iOther.p4()));
302  }
303  if (vertex_) {
304  *vertex_ = iOther.vertex();
305  } else {
306  vertex_.store(new Point(iOther.vertex()));
307  }
308  dxy_ = iOther.dxy_;
309  dz_ = iOther.dz_;
310  dphi_ = iOther.dphi_;
311  deta_ = iOther.deta_;
312  dtrkpt_ = iOther.dtrkpt_;
313 
314  if (!iOther.track_) {
315  delete track_.exchange(nullptr);
316  } else {
317  if (!track_) {
318  track_.store(new reco::Track(*iOther.track_));
319  } else {
320  *track_ = *(iOther.track_);
321  }
322  }
323 
324  pdgId_ = iOther.pdgId_;
325  qualityFlags_ = iOther.qualityFlags_;
326  pvRefProd_ = iOther.pvRefProd_;
327  pvRefKey_ = iOther.pvRefKey_;
328  if (!iOther.m_) {
329  delete m_.exchange(nullptr);
330  } else {
331  if (!m_) {
332  m_.store(new reco::Track::CovarianceMatrix(*iOther.m_));
333  } else {
334  *m_ = *(iOther.m_);
335  }
336  }
337 
338  packedHits_ = iOther.packedHits_;
339  packedLayers_ = iOther.packedLayers_;
343  firstHit_ = iOther.firstHit_;
345  return *this;
346  }
347 
349  if (this == &iOther) {
350  return *this;
351  }
352  packedPt_ = iOther.packedPt_;
353  packedEta_ = iOther.packedEta_;
354  packedPhi_ = iOther.packedPhi_;
355  packedM_ = iOther.packedM_;
356  packedDxy_ = iOther.packedDxy_;
357  packedDz_ = iOther.packedDz_;
358  packedDPhi_ = iOther.packedDPhi_;
359  packedDEta_ = iOther.packedDEta_;
360  packedDTrkPt_ = iOther.packedDTrkPt_;
361  packedCovariance_ = iOther.packedCovariance_;
362  packedPuppiweight_ = iOther.packedPuppiweight_;
363  packedPuppiweightNoLepDiff_ = iOther.packedPuppiweightNoLepDiff_;
364  rawCaloFraction_ = iOther.rawCaloFraction_;
365  rawHcalFraction_ = iOther.rawHcalFraction_;
366  caloFraction_ = iOther.caloFraction_;
367  hcalFraction_ = iOther.hcalFraction_;
368  packedTime_ = iOther.packedTime_;
369  packedTimeError_ = iOther.packedTimeError_;
370  isIsolatedChargedHadron_ = iOther.isIsolatedChargedHadron_;
371  delete p4_.exchange(iOther.p4_.exchange(nullptr));
372  delete p4c_.exchange(iOther.p4c_.exchange(nullptr));
373  delete vertex_.exchange(iOther.vertex_.exchange(nullptr));
374  dxy_ = iOther.dxy_;
375  dz_ = iOther.dz_;
376  dphi_ = iOther.dphi_;
377  deta_ = iOther.deta_;
378  dtrkpt_ = iOther.dtrkpt_;
379  delete track_.exchange(iOther.track_.exchange(nullptr));
380  pdgId_ = iOther.pdgId_;
381  qualityFlags_ = iOther.qualityFlags_;
382  pvRefProd_ = iOther.pvRefProd_;
383  pvRefKey_ = iOther.pvRefKey_;
384  delete m_.exchange(iOther.m_.exchange(nullptr));
385  packedHits_ = iOther.packedHits_;
386  packedLayers_ = iOther.packedLayers_;
387  normalizedChi2_ = iOther.normalizedChi2_;
388  covarianceVersion_ = iOther.covarianceVersion_;
389  covarianceSchema_ = iOther.covarianceSchema_;
390  firstHit_ = iOther.firstHit_;
391  trkAlgoPacked_ = iOther.trkAlgoPacked_;
392  return *this;
393  }
394 
396  ~PackedCandidate() override;
398  size_t numberOfDaughters() const override;
400  const reco::Candidate *daughter(size_type) const override;
402  size_t numberOfMothers() const override;
404  const reco::Candidate *mother(size_type) const override;
408  reco::Candidate *daughter(const std::string &s) override;
410  const reco::Candidate *daughter(const std::string &s) const override;
413  size_t numberOfSourceCandidatePtrs() const override { return 0; }
417 
419  int charge() const override {
420  switch (abs(pdgId_)) {
421  case 211:
422  return (pdgId_ > 0) - (pdgId_ < 0);
423  case 11:
424  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // e
425  case 13:
426  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // mu
427  case 15:
428  return (-1) * (pdgId_ > 0) + (pdgId_ < 0); // tau
429  case 24:
430  return (pdgId_ > 0) - (pdgId_ < 0); // W
431  default:
432  return 0; // FIXME: charge is not defined
433  }
434  }
436  void setCharge(int charge) override {}
438  int threeCharge() const override { return charge() * 3; }
440  void setThreeCharge(int threecharge) override {}
442  const LorentzVector &p4() const override {
443  if (!p4c_)
444  unpack();
445  return *p4c_;
446  }
448  const PolarLorentzVector &polarP4() const override {
449  if (!p4c_)
450  unpack();
451  return *p4_;
452  }
454  Vector momentum() const override {
455  if (!p4c_)
456  unpack();
457  return p4c_.load()->Vect();
458  }
461  Vector boostToCM() const override {
462  if (!p4c_)
463  unpack();
464  return p4c_.load()->BoostToCM();
465  }
467  double p() const override {
468  if (!p4c_)
469  unpack();
470  return p4c_.load()->P();
471  }
473  double energy() const override {
474  if (!p4c_)
475  unpack();
476  return p4c_.load()->E();
477  }
479  double et() const override { return (pt() <= 0) ? 0 : p4c_.load()->Et(); }
481  double et2() const override { return (pt() <= 0) ? 0 : p4c_.load()->Et2(); }
483  double mass() const override {
484  if (!p4c_)
485  unpack();
486  return p4_.load()->M();
487  }
489  double massSqr() const override {
490  if (!p4c_)
491  unpack();
492  auto m = p4_.load()->M();
493  return m * m;
494  }
495 
497  double mt() const override {
498  if (!p4c_)
499  unpack();
500  return p4_.load()->Mt();
501  }
503  double mtSqr() const override {
504  if (!p4c_)
505  unpack();
506  return p4_.load()->Mt2();
507  }
509  double px() const override {
510  if (!p4c_)
511  unpack();
512  return p4c_.load()->Px();
513  }
515  double py() const override {
516  if (!p4c_)
517  unpack();
518  return p4c_.load()->Py();
519  }
521  double pz() const override {
522  if (!p4c_)
523  unpack();
524  return p4c_.load()->Pz();
525  }
527  double pt() const override {
528  if (!p4c_)
529  unpack();
530  return p4_.load()->Pt();
531  }
533  double phi() const override {
534  if (!p4c_)
535  unpack();
536  return p4_.load()->Phi();
537  }
538 
540  virtual double ptTrk() const {
541  maybeUnpackBoth();
542  return p4_.load()->pt() + dtrkpt_;
543  }
545  virtual float phiAtVtx() const {
546  maybeUnpackBoth();
547  float ret = p4_.load()->Phi() + dphi_;
548  while (ret > float(M_PI))
549  ret -= 2 * float(M_PI);
550  while (ret < -float(M_PI))
551  ret += 2 * float(M_PI);
552  return ret;
553  }
555  virtual float etaAtVtx() const {
556  maybeUnpackBoth();
557  return p4_.load()->eta() + deta_;
558  }
559 
561  double theta() const override {
562  if (!p4c_)
563  unpack();
564  return p4_.load()->Theta();
565  }
567  double eta() const override {
568  if (!p4c_)
569  unpack();
570  return p4_.load()->Eta();
571  }
573  double rapidity() const override {
574  if (!p4c_)
575  unpack();
576  return p4_.load()->Rapidity();
577  }
579  double y() const override {
580  if (!p4c_)
581  unpack();
582  return p4_.load()->Rapidity();
583  }
585  void setP4(const LorentzVector &p4) override {
586  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
587  // and x,y,z
588  dphi_ += polarP4().Phi() - p4.Phi();
589  deta_ += polarP4().Eta() - p4.Eta();
590  dtrkpt_ += polarP4().Pt() - p4.Pt();
591  *p4_ = PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M());
592  packBoth();
593  }
595  void setP4(const PolarLorentzVector &p4) override {
596  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
597  // and x,y,z
598  dphi_ += polarP4().Phi() - p4.Phi();
599  deta_ += polarP4().Eta() - p4.Eta();
600  dtrkpt_ += polarP4().Pt() - p4.Pt();
601  *p4_ = p4;
602  packBoth();
603  }
605  void setMass(double m) override {
606  if (!p4c_)
607  unpack();
608  *p4_ = PolarLorentzVector(p4_.load()->Pt(), p4_.load()->Eta(), p4_.load()->Phi(), m);
609  pack();
610  }
611  void setPz(double pz) override {
612  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz
613  // and x,y,z
614  *p4c_ = LorentzVector(p4c_.load()->Px(), p4c_.load()->Py(), pz, p4c_.load()->E());
615  dphi_ += polarP4().Phi() - p4c_.load()->Phi();
616  deta_ += polarP4().Eta() - p4c_.load()->Eta();
617  dtrkpt_ += polarP4().Pt() - p4c_.load()->Pt();
618  *p4_ = PolarLorentzVector(p4c_.load()->Pt(), p4c_.load()->Eta(), p4c_.load()->Phi(), p4c_.load()->M());
619  packBoth();
620  }
622 
623  // Note: mask is also the maximum value
625 
626  // set number of tracker hits and layers
627  virtual void setHits(const reco::Track &tk) {
628  // first we count the number of layers with hits
629  int numberOfPixelLayers_ = tk.hitPattern().pixelLayersWithMeasurement();
630  if (numberOfPixelLayers_ > trackPixelHitsMask)
631  numberOfPixelLayers_ = trackPixelHitsMask;
632  int numberOfStripLayers_ = tk.hitPattern().stripLayersWithMeasurement();
633  if (numberOfStripLayers_ > trackStripHitsMask)
634  numberOfStripLayers_ = trackStripHitsMask;
635  packedLayers_ = (numberOfPixelLayers_ & trackPixelHitsMask) | (numberOfStripLayers_ << trackStripHitsShift);
636  // now we count number of additional hits, beyond the one-per-layer implied
637  // by packedLayers_
638  int numberOfPixelHits_ = tk.hitPattern().numberOfValidPixelHits() - numberOfPixelLayers_;
639  if (numberOfPixelHits_ > trackPixelHitsMask)
640  numberOfPixelHits_ = trackPixelHitsMask;
641  int numberOfStripHits_ =
642  tk.hitPattern().numberOfValidHits() - numberOfPixelHits_ - numberOfPixelLayers_ - numberOfStripLayers_;
643  if (numberOfStripHits_ > trackStripHitsMask)
644  numberOfStripHits_ = trackStripHitsMask;
645 
646  packedHits_ = (numberOfPixelHits_ & trackPixelHitsMask) | (numberOfStripHits_ << trackStripHitsShift);
647  }
648 
649  virtual void setTrackProperties(const reco::Track &tk,
650  const reco::Track::CovarianceMatrix &covariance,
651  int quality,
652  int covarianceVersion) {
656  setHits(tk);
657  maybeUnpackBoth();
658  packBoth();
659  packCovariance(covariance, false);
660  }
661 
662  // set track properties using quality and covarianceVersion to define the
663  // level of details in the cov. matrix
664  virtual void setTrackProperties(const reco::Track &tk, int quality, int covarianceVersion) {
666  }
667 
668  void setTrackPropertiesLite(unsigned int covSchema,
669  unsigned int covarianceVersion,
670  unsigned int nHits,
671  unsigned int nPixelHits) {
673  covarianceSchema_ = covSchema;
674  packedHits_ =
675  (nPixelHits & trackPixelHitsMask) | (((nHits - nPixelHits) & trackStripHitsMask) << trackStripHitsShift);
676  }
677 
679  int numberOfHits() const {
681  }
685  virtual void setCovarianceVersion(int v) { covarianceVersion_ = v; }
686  int covarianceVersion() const { return covarianceVersion_; }
687  int covarianceSchema() const { return covarianceSchema_; }
688 
690  const Point &vertex() const override {
691  maybeUnpackBoth();
692  return *vertex_;
693  } //{ if (fromPV_) return Point(0,0,0); else return Point(0,0,100); }
695  double vx() const override {
696  maybeUnpackBoth();
697  return vertex_.load()->X();
698  } //{ return 0; }
700  double vy() const override {
701  maybeUnpackBoth();
702  return vertex_.load()->Y();
703  } //{ return 0; }
705  double vz() const override {
706  maybeUnpackBoth();
707  return vertex_.load()->Z();
708  } //{ if (fromPV_) return 0; else return 100; }
710  void setVertex(const Point &vertex) override {
711  maybeUnpackBoth();
712  *vertex_ = vertex;
713  packVtx();
714  }
715 
718  enum PVAssoc { NoPV = 0, PVLoose = 1, PVTight = 2, PVUsedInFit = 3 };
719  const PVAssoc fromPV(size_t ipv = 0) const {
720  reco::VertexRef pvRef = vertexRef();
721  if (pvAssociationQuality() == UsedInFitTight and pvRef.key() == ipv)
722  return PVUsedInFit;
723  if (pvRef.key() == ipv or abs(pdgId()) == 13 or abs(pdgId()) == 11)
724  return PVTight;
726  return PVTight; // it is not closest, but at least prevents the B
727  // assignment stealing
728  if (pvAssociationQuality() < UsedInFitLoose or pvRef->ndof() < 4.0)
729  return PVLoose;
730  return NoPV;
731  }
732 
743  };
746  }
748  qualityFlags_ =
750  }
751 
753 
755  virtual float dxy() const {
756  maybeUnpackBoth();
757  return dxy_;
758  }
759 
761  virtual float dz(size_t ipv = 0) const {
762  maybeUnpackBoth();
763  return dz_ + (*pvRefProd_)[pvRefKey_].position().z() - (*pvRefProd_)[ipv].position().z();
764  }
766  virtual float dzAssociatedPV() const {
767  maybeUnpackBoth();
768  return dz_;
769  }
770 
772  virtual float dxy(const Point &p) const;
774  virtual float dz(const Point &p) const;
775 
777  float dzError() const override {
779  return sqrt((*m_.load())(4, 4));
780  }
782  float dxyError() const override {
784  return sqrt((*m_.load())(3, 3));
785  }
786 
789  virtual const reco::Track &pseudoTrack() const {
790  if (!track_)
791  unpackTrk();
792  return *track_;
793  }
797  virtual const reco::Track pseudoPosDefTrack() const;
798 
800  const reco::Track *bestTrack() const override {
801  if (packedHits_ != 0 || packedLayers_ != 0) {
803  return track_.load();
804  } else
805  return nullptr;
806  }
808  bool hasTrackDetails() const { return (packedHits_ != 0 || packedLayers_ != 0); }
811  bool fromTrackCandidate() const { return (packedDz_ != 0 || (packedDxy_ != 0 && packedDxy_ != 32768)); }
815  void setTrackHighPurity(bool highPurity) {
816  qualityFlags_ =
818  }
819 
823  noLostInnerHits = 0, // it could still not have a hit in the first layer,
824  // e.g. if it crosses an inactive sensor
827  };
830  }
832  int lost = hits;
833  if (lost > 2)
834  lost = 2; // protection against misuse
835  lost++; // shift so it's 0 .. 3 instead of (-1) .. 2
837  }
838 
840  void setFirstHit(uint16_t pattern) { firstHit_ = pattern; }
842  uint16_t firstHit() const { return firstHit_; }
843 
845  void setTrkAlgo(uint8_t algo, uint8_t original) {
846  trkAlgoPacked_ = algo | ((algo == original ? 0 : original) << 8);
847  }
848  uint8_t trkAlgo() const { return trkAlgoPacked_ & 0xff; }
849  uint8_t trkOriginalAlgo() const {
850  return (trkAlgoPacked_ & 0xff00) == 0 ? trkAlgo() : ((trkAlgoPacked_ >> 8) & 0xff);
851  }
852 
853  void setMuonID(bool isStandAlone, bool isGlobal) {
854  int16_t muonFlags = isStandAlone | (2 * isGlobal);
856  }
857 
858  void setGoodEgamma(bool isGoodEgamma = true) {
859  int16_t egFlags = (isGoodEgamma << egammaFlagsShift) & egammaFlagsMask;
861  }
862 
864  int pdgId() const override { return pdgId_; }
865  // set PDG identifier
866  void setPdgId(int pdgId) override { pdgId_ = pdgId; }
868  int status() const override { return qualityFlags_; } /*FIXME*/
870  void setStatus(int status) override {} /*FIXME*/
872  static const unsigned int longLivedTag = 0; /*FIXME*/
874  void setLongLived() override {} /*FIXME*/
876  bool longLived() const override;
878  static const unsigned int massConstraintTag = 0; /*FIXME*/
880  void setMassConstraint() override {} /*FIXME*/
882  bool massConstraint() const override;
883 
885  PackedCandidate *clone() const override { return new PackedCandidate(*this); }
886 
888  double vertexChi2() const override;
895  double vertexNdof() const override;
897  double vertexNormalizedChi2() const override;
899  double vertexCovariance(int i, int j) const override;
904  return m;
905  }
907  void fillVertexCovariance(CovarianceMatrix &v) const override;
910  bool hasMasterClone() const override;
914  const reco::CandidateBaseRef &masterClone() const override;
918  bool hasMasterClonePtr() const override;
922 
923  const reco::CandidatePtr &masterClonePtr() const override;
924 
926  template <typename Ref>
927  Ref masterRef() const {
928  return masterClone().template castTo<Ref>();
929  }
930 
931  bool isElectron() const override { return false; }
932  bool isMuon() const override { return false; }
933  bool isStandAloneMuon() const override { return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 1; }
934  bool isGlobalMuon() const override { return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 2; }
935  bool isTrackerMuon() const override { return false; }
936  bool isCaloMuon() const override { return false; }
937  bool isPhoton() const override { return false; }
938  bool isConvertedPhoton() const override { return false; }
939  bool isJet() const override { return false; }
940  bool isGoodEgamma() const { return (qualityFlags_ & egammaFlagsMask) != 0; }
941 
942  // puppiweights
943  void setPuppiWeight(float p,
944  float p_nolep = 0.0);
945  float puppiWeight() const;
947  float puppiWeightNoLep() const;
948 
949  // for the neutral fractions
950  void setRawCaloFraction(float p);
951  float rawCaloFraction() const {
953  return (rawCaloFraction_ / 100.);
954  }
955  void setRawHcalFraction(float p);
956  float rawHcalFraction() const {
957  return (rawHcalFraction_ / 100.);
958  }
959  void setCaloFraction(float p);
960  float caloFraction() const {
961  return (caloFraction_ / 100.);
962  }
963  void setHcalFraction(float p);
964  float hcalFraction() const {
966  return (hcalFraction_ / 100.);
967  }
968 
969  // isolated charged hadrons
970  void setIsIsolatedChargedHadron(bool p);
971  bool isIsolatedChargedHadron() const {
974  }
975 
979  : dxydxy(0), dxydz(0), dzdz(0), dlambdadz(0), dphidxy(0), dptdpt(0), detadeta(0), dphidphi(0) {}
980  // 3D IP covariance
981  uint16_t dxydxy;
982  uint16_t dxydz;
983  uint16_t dzdz;
984  // other IP relevant elements
985  uint16_t dlambdadz;
986  uint16_t dphidxy;
987  // other diag elements
988  uint16_t dptdpt;
989  uint16_t detadeta;
990  uint16_t dphidphi;
991  };
992 
994  virtual float time() const { return vertexRef()->t() + dtimeAssociatedPV(); }
996  virtual float dtime(size_t ipv = 0) const {
997  return dtimeAssociatedPV() + (*pvRefProd_)[pvRefKey_].t() - (*pvRefProd_)[ipv].t();
998  }
1000  virtual float dtimeAssociatedPV() const {
1001  if (packedTime_ == 0)
1002  return 0.f;
1003  if (packedTimeError_ > 0)
1005  else
1007  }
1009  virtual float timeError() const { return unpackTimeError(packedTimeError_); }
1011  void setDTimeAssociatedPV(float aTime, float aTimeError = 0);
1013  void setTime(float aTime, float aTimeError = 0) { setDTimeAssociatedPV(aTime - vertexRef()->t(), aTimeError); }
1014 
1015  private:
1016  void unpackCovarianceElement(reco::TrackBase::CovarianceMatrix &m, uint16_t packed, int i, int j) const {
1018  packed, covarianceSchema_, i, j, pt(), eta(), numberOfHits(), numberOfPixelHits());
1019  }
1020  uint16_t packCovarianceElement(const reco::TrackBase::CovarianceMatrix &m, int i, int j) const {
1023  }
1024 
1025  protected:
1026  friend class ::testPackedCandidate;
1027  static constexpr float kMinDEtaToStore_ = 0.001;
1028  static constexpr float kMinDTrkPtToStore_ = 0.001;
1029 
1033 
1034  void pack(bool unpackAfterwards = true);
1035  void unpack() const;
1036  void packVtx(bool unpackAfterwards = true);
1037  void unpackVtx() const;
1038  void packCovariance(const reco::TrackBase::CovarianceMatrix &m, bool unpackAfterwards = true);
1039  void unpackCovariance() const;
1040  void maybeUnpackBoth() const {
1041  if (!p4c_)
1042  unpack();
1043  if (!vertex_)
1044  unpackVtx();
1045  }
1046  void maybeUnpackTrack() const {
1047  if (!track_)
1048  unpackTrk();
1049  }
1050  void maybeUnpackCovariance() const {
1051  if (!m_)
1052  unpackCovariance();
1053  }
1054  void packBoth() {
1055  pack(false);
1056  packVtx(false);
1057  delete p4_.exchange(nullptr);
1058  delete p4c_.exchange(nullptr);
1059  delete vertex_.exchange(nullptr);
1060  unpack();
1061  unpackVtx();
1062  } // do it this way, so that we don't loose precision on the angles before
1063  // computing dxy,dz
1064  void unpackTrk() const;
1065 
1067  int8_t packedPuppiweightNoLepDiff_; // storing the DIFFERENCE of (all - "no
1068  // lep") for compression optimization
1071  uint8_t caloFraction_;
1073  int16_t packedTime_;
1075 
1077 
1079  mutable std::atomic<PolarLorentzVector *> p4_;
1080  mutable std::atomic<LorentzVector *> p4c_;
1082  mutable std::atomic<Point *> vertex_;
1085  mutable std::atomic<reco::Track *> track_;
1087  int pdgId_;
1088  uint16_t qualityFlags_;
1092 
1094  mutable std::atomic<reco::TrackBase::CovarianceMatrix *> m_;
1095  uint8_t packedHits_,
1096  packedLayers_; // packedLayers_ -> layers with valid hits; packedHits_ ->
1097  // extra hits beyond the one-per-layer implied by
1098  // packedLayers_
1099 
1105  // static std::atomic<CovarianceParameterization*>
1106  // covarianceParameterization_;
1107  static std::once_flag covariance_load_flag;
1109  if (!hasTrackDetails())
1111  "Trying to access covariance matrix for a "
1112  "PackedCandidate for which it's not available. "
1113  "Check hasTrackDetails() before!\n");
1114  std::call_once(
1118  << "Attempting to load multiple covariance version in same process. "
1119  "This is not supported.";
1120  }
1122  }
1123 
1125  uint16_t firstHit_;
1126 
1128  uint16_t trkAlgoPacked_ = 0;
1129 
1131  bool overlap(const reco::Candidate &) const override;
1132  template <typename, typename, typename>
1133  friend struct component;
1134  friend class ::OverlapChecker;
1137 
1145  muonFlagsMask = 0x0600,
1149  };
1150 
1152  static uint8_t packTimeError(float timeError);
1153  static float unpackTimeError(uint8_t timeError);
1154  static float unpackTimeNoError(int16_t time);
1155  static int16_t packTimeNoError(float time);
1156  static float unpackTimeWithError(int16_t time, uint8_t timeError);
1157  static int16_t packTimeWithError(float time, float timeError);
1158  static constexpr float MIN_TIMEERROR = 0.002f; // 2 ps, smallest storable non-zero uncertainty
1159  static constexpr float MIN_TIME_NOERROR = 0.0002f; // 0.2 ps, smallest non-zero time that can be stored by
1160  // packTimeNoError
1161  static constexpr int EXPO_TIMEERROR = 5; // power of 2 used in encoding timeError
1162  static constexpr int EXPO_TIME_NOERROR = 6; // power of 2 used in encoding time without timeError
1163  static constexpr int EXPO_TIME_WITHERROR = -6; // power of 2 used in encoding time with timeError
1164  };
1165 
1166  typedef std::vector<pat::PackedCandidate> PackedCandidateCollection;
1169 } // namespace pat
1170 
1171 #endif
float puppiWeight() const
bool isGoodEgamma() const
int numberOfValidPixelHits() const
Definition: HitPattern.h:831
double vertexNormalizedChi2() const override
chi-squared divided by n.d.o.f.
void setMuonID(bool isStandAlone, bool isGlobal)
static constexpr int EXPO_TIME_WITHERROR
int covarianceVersion() const
void setStatus(int status) override
set status word
bool massConstraint() const override
do mass constraint?
const Point & vertex() const override
vertex position
float puppiWeightNoLep() const
Weight from full PUPPI.
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:164
double vertexNdof() const override
int numberOfValidHits() const
Definition: HitPattern.h:817
bool overlap(const reco::Candidate &) const override
check overlap with another Candidate
double mtSqr() const override
transverse mass squared
void unpackCovarianceElement(reco::TrackBase::CovarianceMatrix &m, uint16_t packed, int i, int j) const
math::XYZVector Vector
point in the space
Definition: Candidate.h:42
void setPuppiWeight(float p, float p_nolep=0.0)
void setThreeCharge(int threecharge) override
set electric charge
int charge() const override
electric charge
size_t size_type
Definition: Candidate.h:29
double et() const override
transverse energy
void setTrackPropertiesLite(unsigned int covSchema, unsigned int covarianceVersion, unsigned int nHits, unsigned int nPixelHits)
int numberOfPixelHits() const
reco::VertexRef::key_type pvRefKey_
bool isGlobalMuon() const override
ret
prodAgent to be discontinued
bool hasMasterClonePtr() const override
bool isStandAloneMuon() const override
static const unsigned int longLivedTag
long lived flag
PackedCandidate & operator=(const PackedCandidate &iOther)
double px() const override
x coordinate of momentum vector
static const unsigned int massConstraintTag
do mass constraint flag
std::atomic< LorentzVector * > p4c_
virtual float etaAtVtx() const
eta from the track (normally identical to eta())
static float unpackTimeNoError(int16_t time)
std::vector< pat::PackedCandidate > PackedCandidateCollection
int stripLayersWithMeasurement() const
Definition: HitPattern.h:1005
math::XYZPoint Point
point in the space
virtual void setCovarianceVersion(int v)
void setRawCaloFraction(float p)
Weight from PUPPI removing leptons.
static int16_t packTimeNoError(float time)
void maybeUnpackCovariance() const
PackedCandidate(const PackedCandidate &iOther)
bool fromTrackCandidate() const
const PVAssoc fromPV(size_t ipv=0) const
Ref masterRef() const
cast master clone reference to a concrete type
double pz() const override
z coordinate of momentum vector
void setTrkAlgo(uint8_t algo, uint8_t original)
Set/get track algo.
Vector boostToCM() const override
const PolarLorentzVector & polarP4() const override
four-momentum Lorentz vector
bool isJet() const override
PackedCovariance packedCovariance_
reco::CandidatePtr sourceCandidatePtr(size_type i) const override
float rawCaloFraction() const
static int16_t packTimeWithError(float time, float timeError)
float caloFraction() const
Set the fraction of ECAL+HCAL energy over candidate energy.
int pdgId() const override
PDG identifier.
float rawHcalFraction() const
Set the fraction of Hcal needed isolated charged hadrons.
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)
size_t numberOfMothers() const override
number of mothers
const reco::CandidateBaseRef & masterClone() const override
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
double rapidity() const override
rapidity
key_type key() const
Accessor for product key.
Definition: Ref.h:250
uint16_t packCovarianceElement(const reco::TrackBase::CovarianceMatrix &m, int i, int j) const
PackedCandidate(const reco::Candidate &c, const reco::VertexRefProd &pvRefProd, reco::VertexRef::key_type pvRefKey)
double vy() const override
y coordinate of vertex position
virtual float dtime(size_t ipv=0) const
dtime with respect to the PV[ipv]
bool trackHighPurity() const
true if the track had the highPurity quality bit
Definition: HeavyIon.h:7
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
reco::CandidateCollection daughters
collection of daughter candidates
void maybeUnpackBoth() const
double p() const override
magnitude of momentum vector
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
int pixelLayersWithMeasurement() const
const PVAssociationQuality pvAssociationQuality() const
string quality
void setP4(const LorentzVector &p4) override
set 4-momentum
CovarianceMatrix covariance() const
return track covariance matrix
Definition: TrackBase.h:716
const reco::CandidatePtr & masterClonePtr() const override
void unpackCovariance() const
int covarianceSchema() const
virtual float timeError() const
time measurement uncertainty (-1 if not available)
double eta() const override
momentum pseudorapidity
virtual const reco::Track pseudoPosDefTrack() const
bool isPhoton() const override
uint8_t trkAlgo() const
void setHcalFraction(float p)
Fraction of ECAL+HCAL energy over candidate energy.
virtual float dtimeAssociatedPV() const
dtime with respect to the PV ref
math::XYZTLorentzVector LorentzVector
Lorentz vector.
virtual void setTrackProperties(const reco::Track &tk, int quality, int covarianceVersion)
std::atomic< PolarLorentzVector * > p4_
the four vector
T sqrt(T t)
Definition: SSEVec.h:23
void setMass(double m) override
set particle mass
float dzError() const override
uncertainty on dz
static constexpr float kMinDTrkPtToStore_
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
std::atomic< Point * > vertex_
vertex position
void setPz(double pz) override
void setLongLived() override
set long lived flag
PackedCandidate()
default constructor
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
edm::Ref< pat::PackedCandidateCollection > PackedCandidateRef
uint16_t firstHit() const
Return first hit from HitPattern for tracks with high level details.
friend class ShallowCloneCandidate
void setP4(const PolarLorentzVector &p4) override
set 4-momentum
size_t numberOfDaughters() const override
number of daughters
static CovarianceParameterization covarianceParameterization_
void fillVertexCovariance(CovarianceMatrix &v) const override
fill SMatrix
#define CMS_THREAD_GUARD(_var_)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual double ptTrk() const
pt from the track (normally identical to pt())
#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)
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
float dxyError() const override
uncertainty on dxy
double vz() const override
z coordinate of vertex position
double massSqr() const override
mass squared
bool isElectron() const override
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
Vector momentum() const override
spatial momentum vector
std::atomic< reco::Track * > track_
reco::Track
void maybeUnpackTrack() const
double mt() const override
transverse mass
#define M_PI
static constexpr int EXPO_TIME_NOERROR
int status() const override
status word
PackedCandidate * clone() const override
returns a clone of the Candidate object
const reco::Track * bestTrack() const override
return a pointer to the track if present. otherwise, return a null pointer
int stripLayersWithMeasurement() const
std::atomic< reco::TrackBase::CovarianceMatrix * > m_
IP covariance.
LostInnerHits
Enumerator specifying the.
bool isConvertedPhoton() const override
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
double mass() const override
mass
void setTime(float aTime, float aTimeError=0)
set time measurement
double pt() const override
transverse momentum
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:504
static constexpr int EXPO_TIMEERROR
virtual float phiAtVtx() const
momentum azimuthal angle from the track (normally identical to phi())
double py() const override
y coordinate of momentum vector
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
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
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:593
static std::once_flag covariance_load_flag
uint8_t trkOriginalAlgo() const
void setPdgId(int pdgId) override
int trackerLayersWithMeasurement() const
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
void setFirstHit(uint16_t pattern)
Set first hit from HitPattern.
int pdgId_
PDG identifier.
bool isIsolatedChargedHadron() const
void setDTimeAssociatedPV(float aTime, float aTimeError=0)
set time measurement
friend class ShallowClonePtrCandidate
float hcalFraction() const
fixed size matrix
than at PV) flag for charged hadrons
PackedCandidate & operator=(PackedCandidate &&iOther)
Structure Point Contains parameters of Gaussian fits to DMRs.
double vx() const override
x coordinate of vertex position
const reco::Candidate * mother(size_type) const override
return mother at a given position (throws an exception)
LostInnerHits lostInnerHits() const
double energy() const override
energy
bool isCaloMuon() const override
void packVtx(bool unpackAfterwards=true)
void setLostInnerHits(LostInnerHits hits)
PackedCandidate(const LorentzVector &p4, const Point &vtx, float trkPt, float etaAtVtx, float phiAtVtx, int pdgId, const reco::VertexRefProd &pvRefProd, reco::VertexRef::key_type pvRefKey)
const reco::Candidate * daughter(size_type) const override
return daughter at a given position (throws an exception)
trackHitShiftsAndMasks
set impact parameters covariance
void setVertex(const Point &vertex) override
set vertex
void setIsIsolatedChargedHadron(bool p)
Fraction of Hcal for HF, neutral hadrons, and charged particles.
static constexpr float kMinDEtaToStore_
double vertexChi2() const override
chi-squares
int threeCharge() const override
electric charge
size_t numberOfSourceCandidatePtrs() const override
CovarianceMatrix vertexCovariance() const override
return SMatrix
virtual float dxy() const
dxy with respect to the PV ref
void pack(bool unpackAfterwards=true)
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:514
double y() const override
rapidity
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
void packCovariance(const reco::TrackBase::CovarianceMatrix &m, bool unpackAfterwards=true)
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
virtual float time() const
time (wrt nominal zero of the collision)
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)
double theta() const override
momentum polar angle
double et2() const override
transverse energy squared (use this for cuts)!
virtual void setHits(const reco::Track &tk)
const LorentzVector & p4() const override
four-momentum Lorentz vecto r
bool hasMasterClone() const override
bool isTrackerMuon() const override
uint16_t trkAlgoPacked_
track algorithm details
uint16_t firstHit_
details (hit pattern) of the first hit on track
void setCharge(int charge) override
set electric charge
const CovarianceParameterization & covarianceParameterization() const
virtual float dzAssociatedPV() const
dz with respect to the PV ref
math::XYZVector Vector
point in the space
static constexpr float MIN_TIMEERROR
static float unpackTimeError(uint8_t timeError)
double phi() const override
momentum azimuthal angle
bool isMuon() const override
virtual const reco::Track & pseudoTrack() const
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:74
void setCaloFraction(float p)
Fraction of Hcal for isolated charged hadrons.
bool longLived() const override
is long lived?
reco::VertexRefProd pvRefProd_
Use these to build a Ref to primary vertex.
static constexpr float MIN_TIME_NOERROR
void setAssociationQuality(PVAssociationQuality q)
bool hasTrackDetails() const
Return true if a bestTrack can be extracted from this Candidate.
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:38