CMS 3D CMS Logo

deep_helpers.h
Go to the documentation of this file.
1 #ifndef RecoBTag_FeatureTools_deep_helpers_h
2 #define RecoBTag_FeatureTools_deep_helpers_h
3 
6 
8 
10 
13 //#include "RecoVertex/VertexTools/interface/VertexDistanceXY.h"
14 
17 
20 
22 
23 #include <iostream>
24 #include <fstream>
25 #include <algorithm>
26 #include <numeric>
27 #include <nlohmann/json.hpp>
28 
29 namespace btagbtvdeep {
30 
31  // remove infs and NaNs with value (adapted from DeepNTuples)
32  const float catch_infs(const float in, const float replace_value = 0.);
33 
34  // remove infs/NaN and bound (adapted from DeepNTuples)
35  const float catch_infs_and_bound(const float in,
36  const float replace_value,
37  const float lowerbound,
38  const float upperbound,
39  const float offset = 0.,
40  const bool use_offsets = true);
41 
42  // 2D distance between SV and PV (adapted from DeepNTuples)
44 
45  //3D distance between SV and PV (adapted from DeepNTuples)
47 
48  // dot product between SV and PV (adapted from DeepNTuples)
50 
51  // helper to order vertices by significance (adapted from DeepNTuples)
52  template <typename SVType, typename PVType>
53  bool sv_vertex_comparator(const SVType &sva, const SVType &svb, const PVType &pv) {
54  auto adxy = vertexDxy(sva, pv);
55  auto bdxy = vertexDxy(svb, pv);
56  float aval = adxy.value();
57  float bval = bdxy.value();
58  float aerr = adxy.error();
59  float berr = bdxy.error();
60 
61  float asig = catch_infs(aval / aerr, 0.);
62  float bsig = catch_infs(bval / berr, 0.);
63  return bsig < asig;
64  }
65 
66  // write tagging variables to vector (adapted from DeepNTuples)
67  template <typename T>
69  std::vector<T> vals = from.getList(name, false);
70  size_t size = std::min(vals.size(), max);
71  if (size > 0) {
72  for (size_t i = 0; i < vals.size(); i++) {
73  to[i] = catch_infs(vals[i], -0.1);
74  }
75  }
76  return size;
77  }
78 
79  // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic)
80  float mindrsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs,
81  const reco::Candidate *cand,
82  float mindr = 0.4);
83 
84  // mimic the calculation in PackedCandidate
85  float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv);
88 
89  std::pair<float, float> getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand);
90 
91  // struct to hold preprocessing parameters
93  struct VarInfo {
94  VarInfo() {}
95  VarInfo(float median, float norm_factor, float replace_inf_value, float lower_bound, float upper_bound, float pad)
96  : center(median),
101  pad(pad) {}
102  float center = 0;
103  float norm_factor = 1;
104  float replace_inf_value = 0;
105  float lower_bound = -5;
106  float upper_bound = 5;
107  float pad = 0;
108  };
109 
110  unsigned min_length = 0;
111  unsigned max_length = 0;
112  std::vector<std::string> var_names;
113  std::unordered_map<std::string, VarInfo> var_info_map;
114 
115  VarInfo info(const std::string &name) const { return var_info_map.at(name); }
116  };
117 
118  int center_norm_pad(const std::vector<float> &input,
119  float center,
120  float scale,
121  unsigned min_length,
122  unsigned max_length,
123  std::vector<float> &datavec,
124  int startval,
125  float pad_value = 0,
126  float replace_inf_value = 0,
127  float min = 0,
128  float max = -1);
129 
130  int center_norm_pad_halfRagged(const std::vector<float> &input,
131  float center,
132  float scale,
133  unsigned target_length,
134  std::vector<float> &datavec,
135  int startval,
136  float pad_value = 0,
137  float replace_inf_value = 0,
138  float min = 0,
139  float max = -1);
140 
141  void ParticleNetConstructor(const edm::ParameterSet &Config_,
142  bool doExtra,
143  std::vector<std::string> &input_names_,
144  std::unordered_map<std::string, PreprocessParams> &prep_info_map_,
145  std::vector<std::vector<int64_t>> &input_shapes_,
146  std::vector<unsigned> &input_sizes_,
147  cms::Ort::FloatArrays *data_);
148 
149 } // namespace btagbtvdeep
150 #endif //RecoBTag_FeatureTools_deep_helpers_h
size
Write out results.
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)
float quality_from_pfcand(const reco::PFCandidate &pfcand)
Definition: deep_helpers.cc:96
float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv)
Definition: deep_helpers.cc:65
std::vector< std::string > var_names
Definition: deep_helpers.h:112
Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:56
VarInfo info(const std::string &name) const
Definition: deep_helpers.h:115
std::unordered_map< std::string, VarInfo > var_info_map
Definition: deep_helpers.h:113
const float catch_infs(const float in, const float replace_value=0.)
Definition: deep_helpers.cc:21
Base class for all types of Jets.
Definition: Jet.h:20
int dump_vector(reco::TaggingVariableList &from, T *to, reco::btau::TaggingVariableName name, const size_t max)
Definition: deep_helpers.h:68
std::pair< float, float > getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand)
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
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:30
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:72
std::vector< TaggingValue > getList(TaggingVariableName tag, bool throwOnEmptyList=true) const
Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:47
float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv)
Definition: deep_helpers.cc:88
bool sv_vertex_comparator(const SVType &sva, const SVType &svb, const PVType &pv)
Definition: deep_helpers.h:53
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
VarInfo(float median, float norm_factor, float replace_inf_value, float lower_bound, float upper_bound, float pad)
Definition: deep_helpers.h:95
long double T