CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
deep_helpers.cc
Go to the documentation of this file.
4 
5 namespace btagbtvdeep {
6 
7  constexpr static int qualityMap[8] = {1, 0, 1, 1, 4, 4, 5, 6};
8 
16  muonFlagsMask = 0x0600,
18  };
19 
20  // remove infs and NaNs with value (adapted from DeepNTuples)
21  const float catch_infs(const float in, const float replace_value) {
22  if (edm::isNotFinite(in))
23  return replace_value;
24  if (in < -1e32 || in > 1e32)
25  return replace_value;
26  return in;
27  }
28 
29  // remove infs/NaN and bound (adapted from DeepNTuples)
30  const float catch_infs_and_bound(const float in,
31  const float replace_value,
32  const float lowerbound,
33  const float upperbound,
34  const float offset,
35  const bool use_offsets) {
36  float withoutinfs = catch_infs(in, replace_value);
37  if (withoutinfs + offset < lowerbound)
38  return lowerbound;
39  if (withoutinfs + offset > upperbound)
40  return upperbound;
41  if (use_offsets)
42  withoutinfs += offset;
43  return withoutinfs;
44  }
45 
46  // 2D distance between SV and PV (adapted from DeepNTuples)
48  VertexDistanceXY dist;
50  svcand.fillVertexCovariance(csv);
51  reco::Vertex svtx(svcand.vertex(), csv);
52  return dist.distance(svtx, pv);
53  }
54 
55  //3D distance between SV and PV (adapted from DeepNTuples)
57  VertexDistance3D dist;
59  svcand.fillVertexCovariance(csv);
60  reco::Vertex svtx(svcand.vertex(), csv);
61  return dist.distance(svtx, pv);
62  }
63 
64  // dot product between SV and PV (adapted from DeepNTuples)
67  reco::Candidate::Vector d(sv.vx() - pv.x(), sv.vy() - pv.y(), sv.vz() - pv.z());
68  return p.Unit().Dot(d.Unit());
69  }
70 
71  // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic)
72  float mindrsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs,
73  const reco::Candidate *cand,
74  float mindr) {
75  for (unsigned int i0 = 0; i0 < svs.size(); ++i0) {
76  float tempdr = reco::deltaR(svs[i0], *cand);
77  if (tempdr < mindr) {
78  mindr = tempdr;
79  }
80  }
81  return mindr;
82  }
83 
84  // instantiate template
85  template bool sv_vertex_comparator<reco::VertexCompositePtrCandidate, reco::Vertex>(
87 
88  float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv) {
89  float vtx_ass = pat::PackedCandidate::PVAssociationQuality(qualityMap[pv_ass_quality]);
90  if (pfcand.trackRef().isNonnull() && pv->trackWeight(pfcand.trackRef()) > 0.5 && pv_ass_quality == 7) {
92  }
93  return vtx_ass;
94  }
95 
96  float quality_from_pfcand(const reco::PFCandidate &pfcand) {
97  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
98  // conditions from PackedCandidate producer
99  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
100  // do same bit operations than in PackedCandidate
101  uint16_t qualityFlags = 0;
102  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
103  bool isHighPurity = (qualityFlags & trackHighPurityMask) >> trackHighPurityShift;
104  // to do as in TrackBase
105  uint8_t quality = (1 << reco::TrackBase::loose);
106  if (isHighPurity) {
107  quality |= (1 << reco::TrackBase::highPurity);
108  }
109  return quality;
110  }
111 
113  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
114  // conditions from PackedCandidate producer
115  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
116  // do same bit operations than in PackedCandidate
117  uint16_t qualityFlags = 0;
118  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
119  return int16_t((qualityFlags & lostInnerHitsMask) >> lostInnerHitsShift) - 1;
120  }
121 
122  std::pair<float, float> getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand) {
123  const auto *patJet = dynamic_cast<const pat::Jet *>(&jet);
124  std::pair<float, float> features;
125  // Do Subjets
126  if (patJet) {
127  if (patJet->nSubjetCollections() > 0) {
128  auto subjets = patJet->subjets();
129  std::nth_element(
130  subjets.begin(),
131  subjets.begin() + 1,
132  subjets.end(),
133  [](const edm::Ptr<pat::Jet> &p1, const edm::Ptr<pat::Jet> &p2) { return p1->pt() > p2->pt(); });
134  features.first = !subjets.empty() ? reco::deltaR(*cand, *subjets[0]) : -1;
135  features.second = subjets.size() > 1 ? reco::deltaR(*cand, *subjets[1]) : -1;
136  } else {
137  features.first = -1;
138  features.second = -1;
139  }
140  } else {
141  features.first = -1;
142  features.second = -1;
143  }
144  return features;
145  }
146 } // namespace btagbtvdeep
float quality_from_pfcand(const reco::PFCandidate &pfcand)
Definition: deep_helpers.cc:96
Vector momentum() const final
spatial momentum vector
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:65
Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:56
math::XYZVector Vector
point in the space
Definition: Candidate.h:42
double vz() const override
z coordinate of vertex position
const float catch_infs(const float in, const float replace_value=0.)
Definition: deep_helpers.cc:21
const TString p2
Definition: fwPaths.cc:13
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
void fillVertexCovariance(CovarianceMatrix &v) const override
fill SMatrix
const reco::Track * bestTrack() const override
Definition: PFCandidate.h:150
double y() const
y coordinate
Definition: Vertex.h:131
Base class for all types of Jets.
Definition: Jet.h:20
double vy() const override
y coordinate of vertex position
string quality
const Point & vertex() const override
vertex position (overwritten by PF...)
std::pair< float, float > getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand)
math::Error< dimension >::type CovarianceMatrix
covariance error matrix (3x3)
Definition: Vertex.h:46
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:430
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:30
tuple d
Definition: ztail.py:151
qualityFlagsShiftsAndMasks
Definition: deep_helpers.cc:9
float mindrsvpfcand(const std::vector< reco::VertexCompositePtrCandidate > &svs, const reco::Candidate *cand, float mindr=0.4)
Definition: deep_helpers.cc:72
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:47
const TString p1
Definition: fwPaths.cc:12
double z() const
z coordinate
Definition: Vertex.h:133
float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv)
Definition: deep_helpers.cc:88
double vx() const override
x coordinate of vertex position
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
double x() const
x coordinate
Definition: Vertex.h:129
Analysis-level calorimeter jet class.
Definition: Jet.h:77
float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
static constexpr int qualityMap[8]
Definition: deep_helpers.cc:7