Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-30 23:17:12

0001 #ifndef RecoBTag_FeatureTools_deep_helpers_h
0002 #define RecoBTag_FeatureTools_deep_helpers_h
0003 
0004 #include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h"
0005 #include "DataFormats/BTauReco/interface/TaggingVariable.h"
0006 
0007 #include "TrackingTools/IPTools/interface/IPTools.h"
0008 
0009 #include "DataFormats/BTauReco/interface/CandIPTagInfo.h"
0010 
0011 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0012 #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h"
0013 //#include "RecoVertex/VertexTools/interface/VertexDistanceXY.h"
0014 
0015 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0016 #include "DataFormats/PatCandidates/interface/Jet.h"
0017 
0018 #include "FWCore/Framework/interface/Event.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 
0021 #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
0022 
0023 #include <iostream>
0024 #include <fstream>
0025 #include <algorithm>
0026 #include <numeric>
0027 #include <nlohmann/json.hpp>
0028 
0029 namespace btagbtvdeep {
0030 
0031   // remove infs and NaNs with value  (adapted from DeepNTuples)
0032   const float catch_infs(const float in, const float replace_value = 0.);
0033 
0034   // remove infs/NaN and bound (adapted from DeepNTuples)
0035   const float catch_infs_and_bound(const float in,
0036                                    const float replace_value,
0037                                    const float lowerbound,
0038                                    const float upperbound,
0039                                    const float offset = 0.,
0040                                    const bool use_offsets = true);
0041 
0042   // 2D distance between SV and PV (adapted from DeepNTuples)
0043   Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv);
0044 
0045   //3D distance between SV and PV (adapted from DeepNTuples)
0046   Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv);
0047 
0048   // dot product between SV and PV (adapted from DeepNTuples)
0049   float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv);
0050 
0051   // helper to order vertices by significance (adapted from DeepNTuples)
0052   template <typename SVType, typename PVType>
0053   bool sv_vertex_comparator(const SVType &sva, const SVType &svb, const PVType &pv) {
0054     auto adxy = vertexDxy(sva, pv);
0055     auto bdxy = vertexDxy(svb, pv);
0056     float aval = adxy.value();
0057     float bval = bdxy.value();
0058     float aerr = adxy.error();
0059     float berr = bdxy.error();
0060 
0061     float asig = catch_infs(aval / aerr, 0.);
0062     float bsig = catch_infs(bval / berr, 0.);
0063     return bsig < asig;
0064   }
0065 
0066   // write tagging variables to vector (adapted from DeepNTuples)
0067   template <typename T>
0068   int dump_vector(reco::TaggingVariableList &from, T *to, reco::btau::TaggingVariableName name, const size_t max) {
0069     std::vector<T> vals = from.getList(name, false);
0070     size_t size = std::min(vals.size(), max);
0071     if (size > 0) {
0072       for (size_t i = 0; i < vals.size(); i++) {
0073         to[i] = catch_infs(vals[i], -0.1);
0074       }
0075     }
0076     return size;
0077   }
0078 
0079   // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic)
0080   float mindrsvpfcand(const std::vector<reco::VertexCompositePtrCandidate> &svs,
0081                       const reco::Candidate *cand,
0082                       float mindr = 0.4);
0083 
0084   // mimic the calculation in PackedCandidate
0085   float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef &pv);
0086   float quality_from_pfcand(const reco::PFCandidate &pfcand);
0087   float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand);
0088 
0089   std::pair<float, float> getDRSubjetFeatures(const reco::Jet &jet, const reco::Candidate *cand);
0090 
0091   // struct to hold preprocessing parameters
0092   struct PreprocessParams {
0093     struct VarInfo {
0094       VarInfo() {}
0095       VarInfo(float median, float norm_factor, float replace_inf_value, float lower_bound, float upper_bound, float pad)
0096           : center(median),
0097             norm_factor(norm_factor),
0098             replace_inf_value(replace_inf_value),
0099             lower_bound(lower_bound),
0100             upper_bound(upper_bound),
0101             pad(pad) {}
0102       float center = 0;
0103       float norm_factor = 1;
0104       float replace_inf_value = 0;
0105       float lower_bound = -5;
0106       float upper_bound = 5;
0107       float pad = 0;
0108     };
0109 
0110     unsigned min_length = 0;
0111     unsigned max_length = 0;
0112     std::vector<std::string> var_names;
0113     std::unordered_map<std::string, VarInfo> var_info_map;
0114 
0115     VarInfo info(const std::string &name) const { return var_info_map.at(name); }
0116   };
0117 
0118   int center_norm_pad(const std::vector<float> &input,
0119                       float center,
0120                       float scale,
0121                       unsigned min_length,
0122                       unsigned max_length,
0123                       std::vector<float> &datavec,
0124                       int startval,
0125                       float pad_value = 0,
0126                       float replace_inf_value = 0,
0127                       float min = 0,
0128                       float max = -1);
0129 
0130   int center_norm_pad_halfRagged(const std::vector<float> &input,
0131                                  float center,
0132                                  float scale,
0133                                  unsigned target_length,
0134                                  std::vector<float> &datavec,
0135                                  int startval,
0136                                  float pad_value = 0,
0137                                  float replace_inf_value = 0,
0138                                  float min = 0,
0139                                  float max = -1);
0140 
0141   void ParticleNetConstructor(const edm::ParameterSet &Config_,
0142                               bool doExtra,
0143                               std::vector<std::string> &input_names_,
0144                               std::unordered_map<std::string, PreprocessParams> &prep_info_map_,
0145                               std::vector<std::vector<int64_t>> &input_shapes_,
0146                               std::vector<unsigned> &input_sizes_,
0147                               cms::Ort::FloatArrays *data_);
0148 
0149 }  // namespace btagbtvdeep
0150 #endif  //RecoBTag_FeatureTools_deep_helpers_h