CMS 3D CMS Logo

deep_helpers.cc
Go to the documentation of this file.
3 
4 namespace btagbtvdeep {
5 
6  constexpr static int qualityMap[8] = {1, 0, 1, 1, 4, 4, 5, 6};
7 
15  muonFlagsMask = 0x0600,
17  };
18 
19  // remove infs and NaNs with value (adapted from DeepNTuples)
20  const float catch_infs(const float in, const float replace_value) {
21  if (in == in) { // check if NaN
22  if (std::isinf(in))
23  return replace_value;
24  else if (in < -1e32 || in > 1e32)
25  return replace_value;
26  return in;
27  }
28  return replace_value;
29  }
30 
31  // remove infs/NaN and bound (adapted from DeepNTuples)
32  const float catch_infs_and_bound(const float in,
33  const float replace_value,
34  const float lowerbound,
35  const float upperbound,
36  const float offset,
37  const bool use_offsets) {
38  float withoutinfs = catch_infs(in, replace_value);
39  if (withoutinfs + offset < lowerbound)
40  return lowerbound;
41  if (withoutinfs + offset > upperbound)
42  return upperbound;
43  if (use_offsets)
44  withoutinfs += offset;
45  return withoutinfs;
46  }
47 
48  // 2D distance between SV and PV (adapted from DeepNTuples)
50  VertexDistanceXY dist;
52  svcand.fillVertexCovariance(csv);
53  reco::Vertex svtx(svcand.vertex(), csv);
54  return dist.distance(svtx, pv);
55  }
56 
57  //3D distance between SV and PV (adapted from DeepNTuples)
59  VertexDistance3D dist;
61  svcand.fillVertexCovariance(csv);
62  reco::Vertex svtx(svcand.vertex(), csv);
63  return dist.distance(svtx, pv);
64  }
65 
66  // dot product between SV and PV (adapted from DeepNTuples)
69  reco::Candidate::Vector d(sv.vx() - pv.x(), sv.vy() - pv.y(), sv.vz() - pv.z());
70  return p.Unit().Dot(d.Unit());
71  }
72 
73  // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic)
74  float mindrsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs,
75  const reco::Candidate *cand,
76  float mindr) {
77  for (unsigned int i0 = 0; i0 < svs.size(); ++i0) {
78  float tempdr = reco::deltaR(svs[i0], *cand);
79  if (tempdr < mindr) {
80  mindr = tempdr;
81  }
82  }
83  return mindr;
84  }
85 
86  // instantiate template
87  template bool sv_vertex_comparator<reco::VertexCompositePtrCandidate, reco::Vertex>(
88  const reco::VertexCompositePtrCandidate &, const reco::VertexCompositePtrCandidate &, const reco::Vertex &);
89 
90  float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv) {
91  float vtx_ass = pat::PackedCandidate::PVAssociationQuality(qualityMap[pv_ass_quality]);
92  if (pfcand.trackRef().isNonnull() && pv->trackWeight(pfcand.trackRef()) > 0.5 && pv_ass_quality == 7) {
94  }
95  return vtx_ass;
96  }
97 
99  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
100  // conditions from PackedCandidate producer
101  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
102  // do same bit operations than in PackedCandidate
103  uint16_t qualityFlags = 0;
104  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
105  bool isHighPurity = (qualityFlags & trackHighPurityMask) >> trackHighPurityShift;
106  // to do as in TrackBase
107  uint8_t quality = (1 << reco::TrackBase::loose);
108  if (isHighPurity) {
109  quality |= (1 << reco::TrackBase::highPurity);
110  }
111  return quality;
112  }
113 
115  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
116  // conditions from PackedCandidate producer
117  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
118  // do same bit operations than in PackedCandidate
119  uint16_t qualityFlags = 0;
120  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
121  return int16_t((qualityFlags & lostInnerHitsMask) >> lostInnerHitsShift) - 1;
122  }
123 
124 } // namespace btagbtvdeep
float quality_from_pfcand(const reco::PFCandidate &pfcand)
Definition: deep_helpers.cc:98
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv)
Definition: deep_helpers.cc:67
Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:58
math::XYZVector Vector
point in the space
Definition: Candidate.h:43
void fillVertexCovariance(CovarianceMatrix &v) const override
fill SMatrix
const float catch_infs(const float in, const float replace_value=0.)
Definition: deep_helpers.cc:20
double vy() const override
y coordinate of vertex position
double y() const
y coordinate
Definition: Vertex.h:117
math::Error< dimension >::type CovarianceMatrix
covariance error matrix (3x3)
Definition: Vertex.h:45
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:408
const float catch_infs_and_bound(const float in, const float replace_value, const float lowerbound, const float upperbound, const float offset=0., const bool use_offsets=true)
Definition: deep_helpers.cc:32
Vector momentum() const final
spatial momentum vector
qualityFlagsShiftsAndMasks
Definition: deep_helpers.cc:8
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
float mindrsvpfcand(const std::vector< reco::VertexCompositePtrCandidate > &svs, const reco::Candidate *cand, float mindr=0.4)
Definition: deep_helpers.cc:74
Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:49
const Point & vertex() const override
vertex position (overwritten by PF...)
def pv(vc)
Definition: MetAnalyzer.py:7
double z() const
z coordinate
Definition: Vertex.h:119
float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv)
Definition: deep_helpers.cc:90
static int qualityMap[8]
Definition: deep_helpers.cc:6
d
Definition: ztail.py:151
double vz() const override
z coordinate of vertex position
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
double x() const
x coordinate
Definition: Vertex.h:115
float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
const reco::Track * bestTrack() const override
Definition: PFCandidate.h:149
#define constexpr
double vx() const override
x coordinate of vertex position