CMS 3D CMS Logo

deep_helpers.cc
Go to the documentation of this file.
12 #include "TLorentzVector.h"
18 
19 namespace btagbtvdeep {
20 
21  constexpr static int qualityMap[8] = {1, 0, 1, 1, 4, 4, 5, 6};
22 
30  muonFlagsMask = 0x0600,
32  };
33 
34  // remove infs and NaNs with value (adapted from DeepNTuples)
35  const float catch_infs(const float in, const float replace_value) {
36  if (edm::isNotFinite(in))
37  return replace_value;
38  if (in < -1e32 || in > 1e32)
39  return replace_value;
40  return in;
41  }
42 
43  // remove infs/NaN and bound (adapted from DeepNTuples)
44  const float catch_infs_and_bound(const float in,
45  const float replace_value,
46  const float lowerbound,
47  const float upperbound,
48  const float offset,
49  const bool use_offsets) {
50  float withoutinfs = catch_infs(in, replace_value);
51  if (withoutinfs + offset < lowerbound)
52  return lowerbound;
53  if (withoutinfs + offset > upperbound)
54  return upperbound;
55  if (use_offsets)
56  withoutinfs += offset;
57  return withoutinfs;
58  }
59 
60  // 2D distance between SV and PV (adapted from DeepNTuples)
62  VertexDistanceXY dist;
64  svcand.fillVertexCovariance(csv);
65  reco::Vertex svtx(svcand.vertex(), csv);
66  return dist.distance(svtx, pv);
67  }
68 
69  //3D distance between SV and PV (adapted from DeepNTuples)
71  VertexDistance3D dist;
73  svcand.fillVertexCovariance(csv);
74  reco::Vertex svtx(svcand.vertex(), csv);
75  return dist.distance(svtx, pv);
76  }
77 
78  // dot product between SV and PV (adapted from DeepNTuples)
80  reco::Candidate::Vector p = sv.momentum();
81  reco::Candidate::Vector d(sv.vx() - pv.x(), sv.vy() - pv.y(), sv.vz() - pv.z());
82  return p.Unit().Dot(d.Unit());
83  }
84 
85  // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic)
86  float mindrsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs,
87  const reco::Candidate *cand,
88  float mindr) {
89  for (unsigned int i0 = 0; i0 < svs.size(); ++i0) {
90  float tempdr = reco::deltaR(svs[i0], *cand);
91  if (tempdr < mindr) {
92  mindr = tempdr;
93  }
94  }
95  return mindr;
96  }
97 
98  // compute minimum distance between SVs and a candidate (from DeepNTuples, now polymorphic)
99  float mindistsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs, const reco::TransientTrack track) {
100  float mindist_ = 999.999;
101  float out_dist = 0.0;
102  for (unsigned int i = 0; i < svs.size(); ++i) {
103  if (!track.isValid()) {
104  continue;
105  }
107  svs[i].fillVertexCovariance(csv);
108  reco::Vertex vertex(svs[i].vertex(), csv);
109  if (!vertex.isValid()) {
110  continue;
111  }
112 
113  GlobalVector direction(svs[i].px(), svs[i].py(), svs[i].pz());
114 
115  AnalyticalImpactPointExtrapolator extrapolator(track.field());
117  extrapolator.extrapolate(track.impactPointState(), RecoVertex::convertPos(vertex.position()));
118 
119  VertexDistance3D dist;
120 
121  if (!tsos.isValid()) {
122  continue;
123  }
124  GlobalPoint refPoint = tsos.globalPosition();
125  GlobalError refPointErr = tsos.cartesianError().position();
126  GlobalPoint vertexPosition = RecoVertex::convertPos(vertex.position());
127  GlobalError vertexPositionErr = RecoVertex::convertError(vertex.error());
128 
129  std::pair<bool, Measurement1D> result(
130  true, dist.distance(VertexState(vertexPosition, vertexPositionErr), VertexState(refPoint, refPointErr)));
131  if (!result.first) {
132  continue;
133  }
134 
135  GlobalPoint impactPoint = tsos.globalPosition();
136  GlobalVector IPVec(impactPoint.x() - vertex.x(), impactPoint.y() - vertex.y(), impactPoint.z() - vertex.z());
137  double prod = IPVec.dot(direction);
138  double sign = (prod >= 0) ? 1. : -1.;
139 
140  if (result.second.value() < mindist_) {
141  out_dist = sign * result.second.value();
142  mindist_ = result.second.value();
143  }
144  }
145  return out_dist;
146  }
147 
148  // instantiate template
149  template bool sv_vertex_comparator<reco::VertexCompositePtrCandidate, reco::Vertex>(
151 
152  float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv) {
153  float vtx_ass = pat::PackedCandidate::PVAssociationQuality(qualityMap[pv_ass_quality]);
154  if (pfcand.trackRef().isNonnull() && pv->trackWeight(pfcand.trackRef()) > 0.5 && pv_ass_quality == 7) {
156  }
157  return vtx_ass;
158  }
159 
161  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
162  // conditions from PackedCandidate producer
163  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
164  // do same bit operations than in PackedCandidate
165  uint16_t qualityFlags = 0;
166  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
167  bool isHighPurity = (qualityFlags & trackHighPurityMask) >> trackHighPurityShift;
168  // to do as in TrackBase
169  uint8_t quality = (1 << reco::TrackBase::loose);
170  if (isHighPurity) {
172  }
173  return quality;
174  }
175 
177  const auto &pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track();
178  // conditions from PackedCandidate producer
179  bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity);
180  // do same bit operations than in PackedCandidate
181  uint16_t qualityFlags = 0;
182  qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask);
183  return int16_t((qualityFlags & lostInnerHitsMask) >> lostInnerHitsShift) - 1;
184  }
185 
186  std::pair<float, float> getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand) {
187  const auto *patJet = dynamic_cast<const pat::Jet *>(&jet);
188  std::pair<float, float> features;
189  // Do Subjets
190  if (patJet) {
191  if (patJet->nSubjetCollections() > 0) {
192  auto subjets = patJet->subjets();
193  std::nth_element(
194  subjets.begin(),
195  subjets.begin() + 1,
196  subjets.end(),
197  [](const edm::Ptr<pat::Jet> &p1, const edm::Ptr<pat::Jet> &p2) { return p1->pt() > p2->pt(); });
198  features.first = !subjets.empty() ? reco::deltaR(*cand, *subjets[0]) : -1;
199  features.second = subjets.size() > 1 ? reco::deltaR(*cand, *subjets[1]) : -1;
200  } else {
201  features.first = -1;
202  features.second = -1;
203  }
204  } else {
205  features.first = -1;
206  features.second = -1;
207  }
208  return features;
209  }
210 
211  int center_norm_pad(const std::vector<float> &input,
212  float center,
213  float norm_factor,
214  unsigned min_length,
215  unsigned max_length,
216  std::vector<float> &datavec,
217  int startval,
218  float pad_value,
219  float replace_inf_value,
220  float min,
221  float max) {
222  // do variable shifting/scaling/padding/clipping in one go
223 
224  assert(min <= pad_value && pad_value <= max);
225  assert(min_length <= max_length);
226 
227  unsigned target_length = std::clamp((unsigned)input.size(), min_length, max_length);
228  for (unsigned i = 0; i < target_length; ++i) {
229  if (i < input.size()) {
230  datavec[i + startval] = std::clamp((catch_infs(input[i], replace_inf_value) - center) * norm_factor, min, max);
231  } else {
232  datavec[i + startval] = pad_value;
233  }
234  }
235  return target_length;
236  }
237 
238  int center_norm_pad_halfRagged(const std::vector<float> &input,
239  float center,
240  float norm_factor,
241  unsigned target_length,
242  std::vector<float> &datavec,
243  int startval,
244  float pad_value,
245  float replace_inf_value,
246  float min,
247  float max) {
248  // do variable shifting/scaling/padding/clipping in one go
249 
250  assert(min <= pad_value && pad_value <= max);
251 
252  for (unsigned i = 0; i < std::min(static_cast<unsigned int>(input.size()), target_length); ++i) {
253  datavec.push_back(std::clamp((catch_infs(input[i], replace_inf_value) - center) * norm_factor, min, max));
254  }
255  if (input.size() < target_length)
256  datavec.insert(datavec.end(), target_length - input.size(), pad_value);
257 
258  return target_length;
259  }
260 
262  bool doExtra,
263  std::vector<std::string> &input_names_,
264  std::unordered_map<std::string, PreprocessParams> &prep_info_map_,
265  std::vector<std::vector<int64_t>> &input_shapes_,
266  std::vector<unsigned> &input_sizes_,
267  cms::Ort::FloatArrays *data_) {
268  // load preprocessing info
269  auto json_path = Config_.getParameter<std::string>("preprocess_json");
270  if (!json_path.empty()) {
271  // use preprocessing json file if available
272  std::ifstream ifs(edm::FileInPath(json_path).fullPath());
274  js.at("input_names").get_to(input_names_);
275  for (const auto &group_name : input_names_) {
276  const auto &group_pset = js.at(group_name);
277  auto &prep_params = prep_info_map_[group_name];
278  group_pset.at("var_names").get_to(prep_params.var_names);
279  if (group_pset.contains("var_length")) {
280  prep_params.min_length = group_pset.at("var_length");
281  prep_params.max_length = prep_params.min_length;
282  } else {
283  prep_params.min_length = group_pset.at("min_length");
284  prep_params.max_length = group_pset.at("max_length");
285  input_shapes_.push_back({1, (int64_t)prep_params.var_names.size(), -1});
286  }
287  const auto &var_info_pset = group_pset.at("var_infos");
288  for (const auto &var_name : prep_params.var_names) {
289  const auto &var_pset = var_info_pset.at(var_name);
290  double median = var_pset.at("median");
291  double norm_factor = var_pset.at("norm_factor");
292  double replace_inf_value = var_pset.at("replace_inf_value");
293  double lower_bound = var_pset.at("lower_bound");
294  double upper_bound = var_pset.at("upper_bound");
295  double pad = var_pset.contains("pad") ? double(var_pset.at("pad")) : 0;
296  prep_params.var_info_map[var_name] =
298  }
299 
300  if (doExtra && data_ != nullptr) {
301  // create data storage with a fixed size vector initialized w/ 0
302  const auto &len = input_sizes_.emplace_back(prep_params.max_length * prep_params.var_names.size());
303  data_->emplace_back(len, 0);
304  }
305  }
306  } else {
307  // otherwise use the PSet in the python config file
308  const auto &prep_pset = Config_.getParameterSet("preprocessParams");
309  input_names_ = prep_pset.getParameter<std::vector<std::string>>("input_names");
310  for (const auto &group_name : input_names_) {
311  const edm::ParameterSet &group_pset = prep_pset.getParameterSet(group_name);
312  auto &prep_params = prep_info_map_[group_name];
313  prep_params.var_names = group_pset.getParameter<std::vector<std::string>>("var_names");
314  prep_params.min_length = group_pset.getParameter<unsigned>("var_length");
315  prep_params.max_length = prep_params.min_length;
316  const auto &var_info_pset = group_pset.getParameterSet("var_infos");
317  for (const auto &var_name : prep_params.var_names) {
318  const edm::ParameterSet &var_pset = var_info_pset.getParameterSet(var_name);
319  double median = var_pset.getParameter<double>("median");
320  double norm_factor = var_pset.getParameter<double>("norm_factor");
321  double replace_inf_value = var_pset.getParameter<double>("replace_inf_value");
322  double lower_bound = var_pset.getParameter<double>("lower_bound");
323  double upper_bound = var_pset.getParameter<double>("upper_bound");
324  prep_params.var_info_map[var_name] =
326  }
327 
328  if (doExtra && data_ != nullptr) {
329  // create data storage with a fixed size vector initialized w/ 0
330  const auto &len = input_sizes_.emplace_back(prep_params.max_length * prep_params.var_names.size());
331  data_->emplace_back(len, 0);
332  }
333  }
334  }
335  }
336 
337 } // namespace btagbtvdeep
int center_norm_pad_halfRagged(const std::vector< float > &input, float center, float scale, unsigned target_length, std::vector< float > &datavec, int startval, float pad_value=0, float replace_inf_value=0, float min=0, float max=-1)
reco::Vertex::Point convertPos(const GlobalPoint &p)
float quality_from_pfcand(const reco::PFCandidate &pfcand)
vector< string > parse(string line, const string &delimiter)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv)
Definition: deep_helpers.cc:79
Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:70
math::XYZVector Vector
point in the space
Definition: Candidate.h:42
const float catch_infs(const float in, const float replace_value=0.)
Definition: deep_helpers.cc:35
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
nlohmann::json json
T z() const
Definition: PV3DBase.h:61
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
void fillVertexCovariance(CovarianceMatrix &v) const override
fill SMatrix
Base class for all types of Jets.
Definition: Jet.h:20
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
ParameterSet const & getParameterSet(std::string const &) const
const Point & vertex() const override
vertex position (overwritten by PF...)
std::pair< float, float > getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand)
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
assert(be >=bs)
math::Error< dimension >::type CovarianceMatrix
covariance error matrix (3x3)
Definition: Vertex.h:46
static std::string const input
Definition: EdmProvDump.cc:50
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:44
string quality
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
qualityFlagsShiftsAndMasks
Definition: deep_helpers.cc:23
void ParticleNetConstructor(const edm::ParameterSet &Config_, bool doExtra, std::vector< std::string > &input_names_, std::unordered_map< std::string, PreprocessParams > &prep_info_map_, std::vector< std::vector< int64_t >> &input_shapes_, std::vector< unsigned > &input_sizes_, cms::Ort::FloatArrays *data_)
float mindrsvpfcand(const std::vector< reco::VertexCompositePtrCandidate > &svs, const reco::Candidate *cand, float mindr=0.4)
Definition: deep_helpers.cc:86
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:61
float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv)
float mindistsvpfcand(const std::vector< reco::VertexCompositePtrCandidate > &svs, const reco::TransientTrack track)
Definition: deep_helpers.cc:99
d
Definition: ztail.py:151
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
Analysis-level calorimeter jet class.
Definition: Jet.h:77
int center_norm_pad(const std::vector< float > &input, float center, float scale, unsigned min_length, unsigned max_length, std::vector< float > &datavec, int startval, float pad_value=0, float replace_inf_value=0, float min=0, float max=-1)
float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand)
T median(std::vector< T > values)
Definition: median.h:16
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
static constexpr int qualityMap[8]
Definition: deep_helpers.cc:21