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 
4 #include <atomic>
5 #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),
39  packedPhi_(0), packedM_(0),
45  hcalFraction_(0),
46  packedTime_(0),
49  p4_(new PolarLorentzVector(0,0,0,0)), p4c_( new LorentzVector(0,0,0,0)),
50  vertex_(new Point(0,0,0)), dphi_(0), deta_(0), dtrkpt_(0),track_(nullptr), pdgId_(0),
51  qualityFlags_(0), pvRefKey_(reco::VertexRef::invalidKey()),
53 
54  explicit PackedCandidate( const reco::Candidate & c,
55  const reco::VertexRefProd &pvRefProd,
56  reco::VertexRef::key_type pvRefKey) :
59  p4_( new PolarLorentzVector(c.pt(), c.eta(), c.phi(), c.mass())),
60  p4c_( new LorentzVector(*p4_)), vertex_( new Point(c.vertex())),
61  dphi_(0), deta_(0), dtrkpt_(0),
62  track_(nullptr), pdgId_(c.pdgId()), qualityFlags_(0), pvRefProd_(pvRefProd),
63  pvRefKey_(pvRefKey),m_(nullptr), packedHits_(0), packedLayers_(0),
65  packBoth();
66  }
67 
68  explicit PackedCandidate( const PolarLorentzVector &p4, const Point &vtx,
69  float trkPt,float etaAtVtx,float phiAtVtx,int pdgId,
70  const reco::VertexRefProd &pvRefProd,
71  reco::VertexRef::key_type pvRefKey) :
74  p4_( new PolarLorentzVector(p4) ), p4c_( new LorentzVector(*p4_)),
75  vertex_( new Point(vtx) ),
76  dphi_(reco::deltaPhi(phiAtVtx,p4_.load()->phi())),
77  deta_(std::abs(etaAtVtx-p4_.load()->eta())>=kMinDEtaToStore_ ? etaAtVtx-p4_.load()->eta() : 0.),
78  dtrkpt_(std::abs(trkPt-p4_.load()->pt())>=kMinDTrkPtToStore_ ? trkPt-p4_.load()->pt() : 0.),
79  track_(nullptr), pdgId_(pdgId),
80  qualityFlags_(0), pvRefProd_(pvRefProd), pvRefKey_(pvRefKey),
82  packBoth();
83  }
84 
85  explicit PackedCandidate( const LorentzVector &p4, const Point &vtx,
86  float trkPt, float etaAtVtx, float phiAtVtx, int pdgId,
87  const reco::VertexRefProd &pvRefProd,
88  reco::VertexRef::key_type pvRefKey) :
91  p4_(new PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M())),
92  p4c_( new LorentzVector(p4)), vertex_( new Point(vtx) ) ,
93  dphi_(reco::deltaPhi(phiAtVtx,p4_.load()->phi())),
94  deta_(std::abs(etaAtVtx-p4_.load()->eta())>=kMinDEtaToStore_ ? etaAtVtx-p4_.load()->eta() : 0.),
95  dtrkpt_(std::abs(trkPt-p4_.load()->pt())>=kMinDTrkPtToStore_ ? trkPt-p4_.load()->pt() : 0.),
96  track_(nullptr), pdgId_(pdgId), qualityFlags_(0),
97  pvRefProd_(pvRefProd), pvRefKey_(pvRefKey),
99  packBoth();
100  }
101 
103  packedPt_(iOther.packedPt_), packedEta_(iOther.packedEta_),
104  packedPhi_(iOther.packedPhi_), packedM_(iOther.packedM_),
105  packedDxy_(iOther.packedDxy_), packedDz_(iOther.packedDz_),
112  packedTime_(iOther.packedTime_),
115  //Need to trigger unpacking in iOther
116  p4_( new PolarLorentzVector(iOther.polarP4() ) ),
117  p4c_( new LorentzVector(iOther.p4())), vertex_( new Point(iOther.vertex())),
118  dxy_(iOther.dxy_), dz_(iOther.dz_),
119  dphi_(iOther.dphi_), deta_(iOther.deta_), dtrkpt_(iOther.dtrkpt_),
120  track_( iOther.track_ ? new reco::Track(*iOther.track_) : nullptr),
121  pdgId_(iOther.pdgId_), qualityFlags_(iOther.qualityFlags_),
122  pvRefProd_(iOther.pvRefProd_),pvRefKey_(iOther.pvRefKey_),
123  m_(iOther.m_? new reco::TrackBase::CovarianceMatrix(*iOther.m_) : nullptr),
126  }
127 
129  packedPt_(iOther.packedPt_), packedEta_(iOther.packedEta_),
130  packedPhi_(iOther.packedPhi_), packedM_(iOther.packedM_),
131  packedDxy_(iOther.packedDxy_), packedDz_(iOther.packedDz_),
138  packedTime_(iOther.packedTime_),
141  p4_( iOther.p4_.exchange(nullptr) ) ,
142  p4c_( iOther.p4c_.exchange(nullptr)), vertex_(iOther.vertex_.exchange(nullptr)),
143  dxy_(iOther.dxy_), dz_(iOther.dz_),
144  dphi_(iOther.dphi_), deta_(iOther.deta_), dtrkpt_(iOther.dtrkpt_),
145  track_( iOther.track_.exchange(nullptr) ),
146  pdgId_(iOther.pdgId_), qualityFlags_(iOther.qualityFlags_),
147  pvRefProd_(std::move(iOther.pvRefProd_)),pvRefKey_(iOther.pvRefKey_),
148  m_( iOther.m_.exchange(nullptr)),
151  }
152 
153 
155  if(this == &iOther) {
156  return *this;
157  }
158  packedPt_ =iOther.packedPt_;
159  packedEta_=iOther.packedEta_;
160  packedPhi_=iOther.packedPhi_;
161  packedM_=iOther.packedM_;
162  packedDxy_=iOther.packedDxy_;
163  packedDz_=iOther.packedDz_;
164  packedDPhi_=iOther.packedDPhi_;
165  packedDEta_=iOther.packedDEta_;
172  packedTime_=iOther.packedTime_;
175  //Need to trigger unpacking in iOther
176  if(p4_) {
177  *p4_ = iOther.polarP4();
178  } else {
179  p4_.store( new PolarLorentzVector(iOther.polarP4() ) ) ;
180  }
181  if(p4c_) {
182  *p4c_ = iOther.p4();
183  } else {
184  p4c_.store( new LorentzVector(iOther.p4()));
185  }
186  if(vertex_) {
187  *vertex_ = iOther.vertex();
188  } else {
189  vertex_.store( new Point(iOther.vertex()));
190  }
191  dxy_=iOther.dxy_;
192  dz_ = iOther.dz_;
193  dphi_=iOther.dphi_;
194  deta_=iOther.deta_;
195  dtrkpt_=iOther.dtrkpt_;
196 
197  if(!iOther.track_) {
198  delete track_.exchange(nullptr);
199  } else {
200  if(!track_) {
201  track_.store( new reco::Track(*iOther.track_));
202  } else {
203  *track_ = *(iOther.track_);
204  }
205  }
206 
207  pdgId_=iOther.pdgId_;
209  pvRefProd_=iOther.pvRefProd_;
210  pvRefKey_=iOther.pvRefKey_;
211  if(!iOther.m_) {
212  delete m_.exchange(nullptr);
213  } else {
214  if(!m_) {
215  m_.store( new reco::Track::CovarianceMatrix(*iOther.m_));
216  } else {
217  *m_ = *(iOther.m_);
218  }
219  }
220 
221 
222  packedHits_=iOther.packedHits_;
227  firstHit_=iOther.firstHit_;
228  return *this;
229  }
230 
232  if(this == &iOther) {
233  return *this;
234  }
235  packedPt_ =iOther.packedPt_;
236  packedEta_=iOther.packedEta_;
237  packedPhi_=iOther.packedPhi_;
238  packedM_=iOther.packedM_;
239  packedDxy_=iOther.packedDxy_;
240  packedDz_=iOther.packedDz_;
241  packedDPhi_=iOther.packedDPhi_;
242  packedDEta_=iOther.packedDEta_;
243  packedDTrkPt_=iOther.packedDTrkPt_;
244  packedCovariance_=iOther.packedCovariance_;
245  packedPuppiweight_=iOther.packedPuppiweight_;
246  packedPuppiweightNoLepDiff_=iOther.packedPuppiweightNoLepDiff_;
247  rawCaloFraction_=iOther.rawCaloFraction_;
248  hcalFraction_=iOther.hcalFraction_;
249  packedTime_=iOther.packedTime_;
250  packedTimeError_=iOther.packedTimeError_;
251  isIsolatedChargedHadron_=iOther.isIsolatedChargedHadron_;
252  delete p4_.exchange(iOther.p4_.exchange(nullptr));
253  delete p4c_.exchange(iOther.p4c_.exchange(nullptr));
254  delete vertex_.exchange(iOther.vertex_.exchange(nullptr));
255  dxy_=iOther.dxy_;
256  dz_ = iOther.dz_;
257  dphi_=iOther.dphi_;
258  deta_=iOther.deta_;
259  dtrkpt_=iOther.dtrkpt_;
260  delete track_.exchange(iOther.track_.exchange( nullptr));
261  pdgId_=iOther.pdgId_;
262  qualityFlags_=iOther.qualityFlags_;
263  pvRefProd_=iOther.pvRefProd_;
264  pvRefKey_=iOther.pvRefKey_;
265  delete m_.exchange(iOther.m_.exchange(nullptr));
266  packedHits_=iOther.packedHits_;
267  packedLayers_=iOther.packedLayers_;
268  normalizedChi2_=iOther.normalizedChi2_;
269  covarianceVersion_=iOther.covarianceVersion_;
270  covarianceSchema_=iOther.covarianceSchema_;
271  firstHit_=iOther.firstHit_;
272  return *this;
273  }
274 
276  ~PackedCandidate() override;
278  size_t numberOfDaughters() const override;
280  const reco::Candidate * daughter( size_type ) const override;
282  size_t numberOfMothers() const override;
284  const reco::Candidate * mother( size_type ) const override;
286  reco::Candidate * daughter( size_type ) override;
288  reco::Candidate * daughter(const std::string& s ) override;
290  const reco::Candidate * daughter(const std::string& s ) const override;
293  size_t numberOfSourceCandidatePtrs() const override {return 0;}
297  return reco::CandidatePtr();
298  }
299 
301  int charge() const override {
302  switch (abs(pdgId_)) {
303  case 211: return (pdgId_>0)-(pdgId_<0);
304  case 11: return (-1)*(pdgId_>0)+(pdgId_<0); //e
305  case 13: return (-1)*(pdgId_>0)+(pdgId_<0); //mu
306  case 15: return (-1)*(pdgId_>0)+(pdgId_<0); //tau
307  case 24: return (pdgId_>0)-(pdgId_<0); //W
308  default: return 0; //FIXME: charge is not defined
309  }
310  }
312  void setCharge( int charge) override {}
314  int threeCharge() const override {return charge()*3;}
316  void setThreeCharge( int threecharge) override {}
318  const LorentzVector & p4() const override { if (!p4c_) unpack(); return *p4c_; }
320  const PolarLorentzVector & polarP4() const override { if (!p4c_) unpack(); return *p4_; }
322  Vector momentum() const override { if (!p4c_) unpack(); return p4c_.load()->Vect(); }
325  Vector boostToCM() const override { if (!p4c_) unpack(); return p4c_.load()->BoostToCM(); }
327  double p() const override { if (!p4c_) unpack(); return p4c_.load()->P(); }
329  double energy() const override { if (!p4c_) unpack(); return p4c_.load()->E(); }
331  double et() const override { return (pt()<=0) ? 0 : p4c_.load()->Et(); }
333  double et2() const override { return (pt()<=0) ? 0 : p4c_.load()->Et2(); }
335  double mass() const override { if (!p4c_) unpack(); return p4_.load()->M(); }
337  double massSqr() const override { if (!p4c_) unpack(); auto m = p4_.load()->M(); return m*m;}
338 
340  double mt() const override { if (!p4c_) unpack(); return p4_.load()->Mt(); }
342  double mtSqr() const override { if (!p4c_) unpack(); return p4_.load()->Mt2(); }
344  double px() const override { if (!p4c_) unpack(); return p4c_.load()->Px(); }
346  double py() const override { if (!p4c_) unpack(); return p4c_.load()->Py(); }
348  double pz() const override { if (!p4c_) unpack(); return p4c_.load()->Pz(); }
350  double pt() const override { if (!p4c_) unpack(); return p4_.load()->Pt();}
352  double phi() const override { if (!p4c_) unpack(); return p4_.load()->Phi(); }
353 
355  virtual double ptTrk() const {
356  maybeUnpackBoth();
357  return p4_.load()->pt() + dtrkpt_;
358  }
360  virtual float phiAtVtx() const {
361  maybeUnpackBoth();
362  float ret = p4_.load()->Phi() + dphi_;
363  while (ret > float(M_PI)) ret -= 2*float(M_PI);
364  while (ret < -float(M_PI)) ret += 2*float(M_PI);
365  return ret;
366  }
368  virtual float etaAtVtx() const {
369  maybeUnpackBoth();
370  return p4_.load()->eta() + deta_;
371  }
372 
374  double theta() const override { if (!p4c_) unpack(); return p4_.load()->Theta(); }
376  double eta() const override { if (!p4c_) unpack(); return p4_.load()->Eta(); }
378  double rapidity() const override { if (!p4c_) unpack(); return p4_.load()->Rapidity(); }
380  double y() const override { if (!p4c_) unpack(); return p4_.load()->Rapidity(); }
382  void setP4( const LorentzVector & p4 ) override {
383  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz and x,y,z
384  dphi_+=polarP4().Phi()-p4.Phi();
385  deta_+=polarP4().Eta()-p4.Eta();
386  dtrkpt_+=polarP4().Pt()-p4.Pt();
387  *p4_ = PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), p4.M());
388  packBoth();
389  }
391  void setP4( const PolarLorentzVector & p4 ) override {
392  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz and x,y,z
393  dphi_+=polarP4().Phi()-p4.Phi();
394  deta_+=polarP4().Eta()-p4.Eta();
395  dtrkpt_+=polarP4().Pt()-p4.Pt();
396  *p4_ = p4;
397  packBoth();
398  }
400  void setMass( double m ) override {
401  if (!p4c_) unpack();
402  *p4_ = PolarLorentzVector(p4_.load()->Pt(), p4_.load()->Eta(), p4_.load()->Phi(), m);
403  pack();
404  }
405  void setPz( double pz ) override {
406  maybeUnpackBoth(); // changing px,py,pz changes also mapping between dxy,dz and x,y,z
407  *p4c_ = LorentzVector(p4c_.load()->Px(), p4c_.load()->Py(), pz, p4c_.load()->E());
408  dphi_+=polarP4().Phi()- p4c_.load()->Phi();
409  deta_+=polarP4().Eta()- p4c_.load()->Eta();
410  dtrkpt_+=polarP4().Pt()- p4c_.load()->Pt();
411  *p4_ = PolarLorentzVector(p4c_.load()->Pt(), p4c_.load()->Eta(), p4c_.load()->Phi(), p4c_.load()->M());
412  packBoth();
413  }
415 
416  // Note: mask is also the maximum value
420  };
421 
422  // set number of tracker hits and layers
423  virtual void setHits( const reco::Track & tk) {
424  // first we count the number of layers with hits
425  int numberOfPixelLayers_ = tk.hitPattern().pixelLayersWithMeasurement();
426  if (numberOfPixelLayers_ > trackPixelHitsMask) numberOfPixelLayers_ = trackPixelHitsMask;
427  int numberOfStripLayers_ = tk.hitPattern().stripLayersWithMeasurement();
428  if (numberOfStripLayers_ > trackStripHitsMask) numberOfStripLayers_ = trackStripHitsMask;
429  packedLayers_ = (numberOfPixelLayers_&trackPixelHitsMask) | (numberOfStripLayers_ << trackStripHitsShift);
430  // now we count number of additional hits, beyond the one-per-layer implied by packedLayers_
431  int numberOfPixelHits_ = tk.hitPattern().numberOfValidPixelHits() - numberOfPixelLayers_;
432  if (numberOfPixelHits_ > trackPixelHitsMask) numberOfPixelHits_ = trackPixelHitsMask;
433  int numberOfStripHits_ = tk.hitPattern().numberOfValidHits() - numberOfPixelHits_ - numberOfPixelLayers_ - numberOfStripLayers_;
434  if (numberOfStripHits_ > trackStripHitsMask) numberOfStripHits_ = trackStripHitsMask;
435 
436  packedHits_ = (numberOfPixelHits_&trackPixelHitsMask) | (numberOfStripHits_ << trackStripHitsShift);
437  }
438 
439  virtual void setTrackProperties( const reco::Track & tk, const reco::Track::CovarianceMatrix & covariance,int quality,int covarianceVersion) {
443  setHits(tk);
444  packBoth();
445  packCovariance(covariance,false);
446  }
447 
448  // set track properties using quality and covarianceVersion to define the level of details in the cov. matrix
449  virtual void setTrackProperties( const reco::Track & tk,int quality,int covarianceVersion ) {
451  }
452 
459  int covarianceVersion() const { return covarianceVersion_;}
460  int covarianceSchema() const { return covarianceSchema_;}
461 
462 
464  const Point & vertex() const override { maybeUnpackBoth(); return *vertex_; }//{ if (fromPV_) return Point(0,0,0); else return Point(0,0,100); }
466  double vx() const override { maybeUnpackBoth(); return vertex_.load()->X(); }//{ return 0; }
468  double vy() const override { maybeUnpackBoth(); return vertex_.load()->Y(); }//{ return 0; }
470  double vz() const override { maybeUnpackBoth(); return vertex_.load()->Z(); }//{ if (fromPV_) return 0; else return 100; }
472  void setVertex( const Point & vertex ) override { maybeUnpackBoth(); *vertex_ = vertex; packVtx(); }
473 
475  enum PVAssoc { NoPV=0, PVLoose=1, PVTight=2, PVUsedInFit=3 } ;
476  const PVAssoc fromPV(size_t ipv=0) const {
477  reco::VertexRef pvRef = vertexRef();
478  if(pvAssociationQuality()==UsedInFitTight and pvRef.key()==ipv) return PVUsedInFit;
479  if(pvRef.key()==ipv or abs(pdgId())==13 or abs(pdgId())==11 ) return PVTight;
480  if(pvAssociationQuality() == CompatibilityBTag and std::abs(dzAssociatedPV()) > std::abs(dz(ipv))) return PVTight; // it is not closest, but at least prevents the B assignment stealing
481  if(pvAssociationQuality() < UsedInFitLoose or pvRef->ndof() < 4.0 ) return PVLoose;
482  return NoPV;
483  }
484 
490 
492 
494  virtual float dxy() const { maybeUnpackBoth(); return dxy_; }
496  virtual float dz(size_t ipv=0) const { maybeUnpackBoth(); return dz_ + (*pvRefProd_)[pvRefKey_].position().z()-(*pvRefProd_)[ipv].position().z(); }
498  virtual float dzAssociatedPV() const { maybeUnpackBoth(); return dz_; }
500  virtual float dxy(const Point &p) const ;
502  virtual float dz(const Point &p) const ;
503 
505  float dzError() const override { maybeUnpackCovariance(); return sqrt((*m_.load())(4,4)); }
507  float dxyError() const override { maybeUnpackCovariance(); return sqrt((*m_.load())(3,3)); }
508 
509 
511  virtual const reco::Track & pseudoTrack() const { if (!track_) unpackTrk(); return *track_; }
512 
514  const reco::Track * bestTrack() const override {
515  if (packedHits_!=0 || packedLayers_ !=0) {
517  return track_.load();
518  }
519  else
520  return nullptr;
521  }
523  bool hasTrackDetails() const {return (packedHits_!=0 || packedLayers_ !=0); }
524 
525 
530 
534  noLostInnerHits=0, // it could still not have a hit in the first layer, e.g. if it crosses an inactive sensor
537  };
540  }
542  int lost = hits; if (lost > 2) lost = 2; // protection against misuse
543  lost++; // shift so it's 0 .. 3 instead of (-1) .. 2
545  }
546 
548  void setFirstHit(uint16_t pattern) {
550  }
552  uint16_t firstHit() const { return firstHit_;}
553 
554  void setMuonID(bool isStandAlone, bool isGlobal) {
555  int16_t muonFlags = isStandAlone | (2*isGlobal);
557  }
558 
559  void setGoodEgamma(bool isGoodEgamma = true) {
560  int16_t egFlags = (isGoodEgamma << egammaFlagsShift) & egammaFlagsMask;
562  }
563 
565  int pdgId() const override { return pdgId_; }
566  // set PDG identifier
567  void setPdgId( int pdgId ) override { pdgId_ = pdgId; }
569  int status() const override { return qualityFlags_; } /*FIXME*/
571  void setStatus( int status ) override {} /*FIXME*/
573  static const unsigned int longLivedTag = 0; /*FIXME*/
575  void setLongLived() override {} /*FIXME*/
577  bool longLived() const override;
579  static const unsigned int massConstraintTag = 0; /*FIXME*/
581  void setMassConstraint() override {} /*FIXME*/
583  bool massConstraint() const override;
584 
586  PackedCandidate * clone() const override {
587  return new PackedCandidate( *this );
588  }
589 
591  double vertexChi2() const override;
598  double vertexNdof() const override;
600  double vertexNormalizedChi2() const override;
602  double vertexCovariance(int i, int j) const override;
606  void fillVertexCovariance(CovarianceMatrix & v) const override;
609  bool hasMasterClone() const override;
612  const reco::CandidateBaseRef & masterClone() const override;
615  bool hasMasterClonePtr() const override;
618 
619  const reco::CandidatePtr & masterClonePtr() const override;
620 
622  template<typename Ref>
623  Ref masterRef() const { return masterClone().template castTo<Ref>(); }
624 
625  bool isElectron() const override { return false; }
626  bool isMuon() const override { return false; }
627  bool isStandAloneMuon() const override { return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 1; }
628  bool isGlobalMuon() const override { return ((qualityFlags_ & muonFlagsMask) >> muonFlagsShift) & 2; }
629  bool isTrackerMuon() const override { return false; }
630  bool isCaloMuon() const override { return false; }
631  bool isPhoton() const override { return false; }
632  bool isConvertedPhoton() const override { return false; }
633  bool isJet() const override { return false; }
634  bool isGoodEgamma() const { return (qualityFlags_ & egammaFlagsMask) !=0; }
635 
636  // puppiweights
637  void setPuppiWeight(float p, float p_nolep = 0.0);
638  float puppiWeight() const;
639  float puppiWeightNoLep() const;
640 
641  // for the neutral fractions
642  void setRawCaloFraction(float p);
643  float rawCaloFraction() const { return (rawCaloFraction_/100.); }
644  void setHcalFraction(float p);
645  float hcalFraction() const { return (hcalFraction_/100.); }
646 
647  // isolated charged hadrons
648  void setIsIsolatedChargedHadron(bool p);
650 
653  //3D IP covariance
654  uint16_t dxydxy;
655  uint16_t dxydz;
656  uint16_t dzdz;
657  //other IP relevant elements
658  uint16_t dlambdadz;
659  uint16_t dphidxy;
660  //other diag elements
661  uint16_t dptdpt;
662  uint16_t detadeta;
663  uint16_t dphidphi;
664  };
665 
667  virtual float time() const { return vertexRef()->t() + dtimeAssociatedPV(); }
669  virtual float dtime(size_t ipv=0) const { return dtimeAssociatedPV() + (*pvRefProd_)[pvRefKey_].t()-(*pvRefProd_)[ipv].t(); }
671  virtual float dtimeAssociatedPV() const {
672  if (packedTime_ == 0) return 0.f;
674  else return unpackTimeNoError(packedTime_);
675  }
677  virtual float timeError() const { return unpackTimeError(packedTimeError_); }
679  void setDTimeAssociatedPV(float aTime, float aTimeError=0) ;
681  void setTime(float aTime, float aTimeError=0) { setDTimeAssociatedPV(aTime - vertexRef()->t(), aTimeError); }
682 
683  private:
684  void unpackCovarianceElement(reco::TrackBase::CovarianceMatrix & m, uint16_t packed, int i,int j) const {
686  }
687  uint16_t packCovarianceElement(const reco::TrackBase::CovarianceMatrix &m,int i,int j) const {
689  }
690 
691  protected:
692  friend class ::testPackedCandidate;
693  static constexpr float kMinDEtaToStore_=0.001;
694  static constexpr float kMinDTrkPtToStore_=0.001;
695 
696 
700 
701  void pack(bool unpackAfterwards=true) ;
702  void unpack() const ;
703  void packVtx(bool unpackAfterwards=true) ;
704  void unpackVtx() const ;
705  void packCovariance(const reco::TrackBase::CovarianceMatrix & m,bool unpackAfterwards=true) ;
706  void unpackCovariance() const;
707  void maybeUnpackBoth() const { if (!p4c_) unpack(); if (!vertex_) unpackVtx(); }
708  void maybeUnpackTrack() const { if (!track_) unpackTrk(); }
709  void maybeUnpackCovariance() const { if (!m_) unpackCovariance(); }
710  void packBoth() { pack(false); packVtx(false); delete p4_.exchange(nullptr); delete p4c_.exchange(nullptr); delete vertex_.exchange(nullptr); unpack(); unpackVtx(); } // do it this way, so that we don't loose precision on the angles before computing dxy,dz
711  void unpackTrk() const ;
712 
714  int8_t packedPuppiweightNoLepDiff_; // storing the DIFFERENCE of (all - "no lep") for compression optimization
717  int16_t packedTime_;
719 
721 
723  mutable std::atomic<PolarLorentzVector*> p4_;
724  mutable std::atomic<LorentzVector*> p4c_;
726  mutable std::atomic<Point*> vertex_;
727  CMS_THREAD_GUARD(vertex_) mutable float dxy_, dz_, dphi_, deta_, dtrkpt_;
729  mutable std::atomic<reco::Track*> track_;
731  int pdgId_;
732  uint16_t qualityFlags_;
736 
738  mutable std::atomic<reco::TrackBase::CovarianceMatrix *> m_;
739  uint8_t packedHits_, packedLayers_; // packedLayers_ -> layers with valid hits; packedHits_ -> extra hits beyond the one-per-layer implied by packedLayers_
740 
742  uint8_t normalizedChi2_;
746  //static std::atomic<CovarianceParameterization*> covarianceParameterization_;
747  static std::once_flag covariance_load_flag;
749  if (!hasTrackDetails()) throw edm::Exception(edm::errors::InvalidReference, "Trying to access covariance matrix for a PackedCandidate for which it's not available. Check hasTrackDetails() before!\n");
750  std::call_once(covariance_load_flag,[](int v) { covarianceParameterization_.load(v); } ,covarianceVersion_ );
751  if(covarianceParameterization_.loadedVersion() != covarianceVersion_ )
752  {
754  << "Attempting to load multiple covariance version in same process. This is not supported.";
755  }
757  }
758 
760  bool overlap( const reco::Candidate & ) const override;
761  template<typename, typename, typename> friend struct component;
762  friend class ::OverlapChecker;
763  friend class ShallowCloneCandidate;
765 
772  };
773 
775  static uint8_t packTimeError(float timeError) ;
776  static float unpackTimeError(uint8_t timeError) ;
777  static float unpackTimeNoError(int16_t time) ;
778  static int16_t packTimeNoError(float time) ;
779  static float unpackTimeWithError(int16_t time, uint8_t timeError) ;
780  static int16_t packTimeWithError(float time, float timeError) ;
781  static constexpr float MIN_TIMEERROR = 0.002f; // 2 ps, smallest storable non-zero uncertainty
782  static constexpr float MIN_TIME_NOERROR = 0.0002f; // 0.2 ps, smallest non-zero time that can be stored by packTimeNoError
783  static constexpr int EXPO_TIMEERROR = 5; // power of 2 used in encoding timeError
784  static constexpr int EXPO_TIME_NOERROR = 6; // power of 2 used in encoding time without timeError
785  static constexpr int EXPO_TIME_WITHERROR = -6; // power of 2 used in encoding time with timeError
786  public:
787  uint16_t firstHit_;
788 
789  };
790 
791  typedef std::vector<pat::PackedCandidate> PackedCandidateCollection;
794 }
795 
796 #endif
float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
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
static int EXPO_TIME_NOERROR
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:169
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
std::atomic< Point * > vertex_
vertex position
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:561
uint16_t packCovarianceElement(const reco::TrackBase::CovarianceMatrix &m, int i, int j) const
float hcalFraction() const
Set the fraction of Hcal needed for HF and neutral hadrons and isolated charged hadrons.
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:823
PackedCandidate & operator=(const PackedCandidate &iOther)
static const unsigned int massConstraintTag
do mass constraint flag
double vertexChi2() const override
chi-squares
bool isMuon() const override
bool longLived() const override
is long lived?
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:265
virtual float dtime(size_t ipv=0) const
dtime with respect to the PV[ipv]
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:499
static int16_t packTimeWithError(float time, float timeError)
Vector momentum() const override
spatial momentum vector
#define nullptr
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.
#define constexpr
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
friend struct component
Vector boostToCM() const override
std::atomic< reco::Track * > track_
reco::Track
double rapidity() const override
rapidity
const PVAssociationQuality pvAssociationQuality() const
void setHcalFraction(float p)
Raw ECAL+HCAL energy over candidate energy for isolated charged hadrons.
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
CovarianceMatrix covariance() const
return track covariance matrix
Definition: TrackBase.h:731
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
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
Return reference to a pseudo track made with candidate kinematics, parameterized error for eta...
std::atomic< PolarLorentzVector * > p4_
the four vector
#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
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
static int EXPO_TIME_WITHERROR
int trackerLayersWithMeasurement() const
std::atomic< LorentzVector * > p4c_
#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
PVAssoc
This refers to the association to PV=ipv. >=PVLoose corresponds to JME definition, >=PVTight to isolation definition.
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:446
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:546
int stripLayersWithMeasurement() const
Definition: HitPattern.h:1035
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
Set the raw ECAL+HCAL energy over candidate energy for isolated charged hadrons.
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
Flag isolation (as in particle flow, i.e. at calorimeter surface rather than at PV) flag for charged ...
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:838
void setIsIsolatedChargedHadron(bool p)
Fraction of Ecal and Hcal for HF and neutral hadrons and isolated charged hadrons.
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
Set isolation (as in particle flow, i.e. at calorimeter surface rather than at PV) flat for charged h...
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:510
static float unpackTimeError(uint8_t timeError)
double vx() const override
x coordinate of vertex position
static float kMinDTrkPtToStore_
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77
reco::VertexRefProd pvRefProd_
Use these to build a Ref to primary vertex.
void setAssociationQuality(PVAssociationQuality q)