CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
PNETLeptonProducer< LeptonType > Class Template Reference
Inheritance diagram for PNETLeptonProducer< LeptonType >:
edm::stream::EDProducer< edm::GlobalCache< cms::Ort::ONNXRuntime > >

Public Member Functions

 PNETLeptonProducer (const edm::ParameterSet &, const cms::Ort::ONNXRuntime *)
 
 ~PNETLeptonProducer () override
 
- Public Member Functions inherited from edm::stream::EDProducer< edm::GlobalCache< cms::Ort::ONNXRuntime > >
 EDProducer ()=default
 
 EDProducer (const EDProducer &)=delete
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
const EDProduceroperator= (const EDProducer &)=delete
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
static void globalEndJob (const ONNXRuntime *)
 
static std::unique_ptr< ONNXRuntimeinitializeGlobalCache (const edm::ParameterSet &)
 

Private Types

using LeptonTagInfoCollection = DeepBoostedJetFeaturesCollection
 

Private Member Functions

void make_inputs (const DeepBoostedJetFeatures &)
 
void produce (edm::Event &, const edm::EventSetup &) override
 

Private Attributes

cms::Ort::FloatArrays data_
 
bool debug_ = false
 
std::vector< std::string > flav_names_
 
std::vector< std::string > input_names_
 
std::vector< std::vector< int64_t > > input_shapes_
 
std::vector< unsigned > input_sizes_
 
edm::EDGetTokenT< edm::View< LeptonType > > leps_
 
std::unordered_map< std::string, btagbtvdeep::PreprocessParamsprep_info_map_
 
edm::EDGetTokenT< LeptonTagInfoCollectionsrc_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer< edm::GlobalCache< cms::Ort::ONNXRuntime > >
using CacheTypes = CacheContexts< T... >
 
using GlobalCache = typename CacheTypes::GlobalCache
 
using HasAbility = AbilityChecker< T... >
 
using InputProcessBlockCache = typename CacheTypes::InputProcessBlockCache
 
using LuminosityBlockCache = typename CacheTypes::LuminosityBlockCache
 
using LuminosityBlockContext = LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCache >
 
using LuminosityBlockSummaryCache = typename CacheTypes::LuminosityBlockSummaryCache
 
using RunCache = typename CacheTypes::RunCache
 
using RunContext = RunContextT< RunCache, GlobalCache >
 
using RunSummaryCache = typename CacheTypes::RunSummaryCache
 

Detailed Description

template<typename LeptonType>
class PNETLeptonProducer< LeptonType >

Definition at line 31 of file PNETLeptonProducer.cc.

Member Typedef Documentation

◆ LeptonTagInfoCollection

template<typename LeptonType >
using PNETLeptonProducer< LeptonType >::LeptonTagInfoCollection = DeepBoostedJetFeaturesCollection
private

Definition at line 42 of file PNETLeptonProducer.cc.

Constructor & Destructor Documentation

◆ PNETLeptonProducer()

template<typename LeptonType >
PNETLeptonProducer< LeptonType >::PNETLeptonProducer ( const edm::ParameterSet iConfig,
const cms::Ort::ONNXRuntime cache 
)
explicit

Definition at line 61 of file PNETLeptonProducer.cc.

References gather_cfg::cout, PNETLeptonProducer< LeptonType >::data_, PNETLeptonProducer< LeptonType >::debug_, PNETLeptonProducer< LeptonType >::flav_names_, mps_fire::i, PNETLeptonProducer< LeptonType >::input_names_, PNETLeptonProducer< LeptonType >::input_shapes_, PNETLeptonProducer< LeptonType >::input_sizes_, btagbtvdeep::ParticleNetConstructor(), PNETLeptonProducer< LeptonType >::prep_info_map_, and x.

62  : src_(consumes<LeptonTagInfoCollection>(iConfig.getParameter<edm::InputTag>("src"))),
63  leps_(consumes<edm::View<LeptonType>>(iConfig.getParameter<edm::InputTag>("srcLeps"))),
64  flav_names_(iConfig.getParameter<std::vector<std::string>>("flav_names")),
65  debug_(iConfig.getUntrackedParameter<bool>("debugMode", false)) {
67 
68  if (debug_) {
69  for (unsigned i = 0; i < input_names_.size(); ++i) {
70  const auto &group_name = input_names_.at(i);
71  std::cout << group_name << std::endl;
72  if (!input_shapes_.empty()) {
73  std::cout << group_name << "\nshapes: ";
74  for (const auto &x : input_shapes_.at(i)) {
75  std::cout << x << ", ";
76  }
77  }
78  std::cout << "\nvariables: ";
79  for (const auto &x : prep_info_map_.at(group_name).var_names) {
80  std::cout << x << ", ";
81  }
82  std::cout << "\n";
83  }
84  std::cout << "\n";
85  }
86 
87  for (const auto &flav_name : flav_names_) {
88  produces<edm::ValueMap<float>>(flav_name);
89  }
90 }
cms::Ort::FloatArrays data_
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::unordered_map< std::string, btagbtvdeep::PreprocessParams > prep_info_map_
T getUntrackedParameter(std::string const &, T const &) const
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_)
edm::EDGetTokenT< edm::View< LeptonType > > leps_
std::vector< std::string > input_names_
std::vector< unsigned > input_sizes_
std::vector< std::string > flav_names_
edm::EDGetTokenT< LeptonTagInfoCollection > src_
std::vector< std::vector< int64_t > > input_shapes_

◆ ~PNETLeptonProducer()

template<typename LeptonType >
PNETLeptonProducer< LeptonType >::~PNETLeptonProducer ( )
inlineoverride

Definition at line 34 of file PNETLeptonProducer.cc.

34 {}

Member Function Documentation

◆ fillDescriptions()

template<typename LeptonType >
void PNETLeptonProducer< LeptonType >::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 137 of file PNETLeptonProducer.cc.

References edm::ConfigurationDescriptions::add(), submitPVResolutionJobs::desc, timingPdfMaker::modname, and AlCaHLTBitMon_QueryRunRegistry::string.

137  {
139  desc.add<edm::InputTag>("src")->setComment("input variables");
140  desc.add<edm::InputTag>("srcLeps")->setComment("input physics object collection. src and srcLeps must be in synch");
141  desc.add<std::vector<std::string>>("flav_names")->setComment("Names of the oputput classes");
142  desc.add<std::string>("preprocess_json", "PhysicsTools/NanoAOD/data/PNetMuonId/preprocess.json");
143  desc.add<edm::FileInPath>("model_path", edm::FileInPath("PhysicsTools/NanoAOD/data/PNetMuonId/model.onnx"));
144  desc.addOptionalUntracked<bool>("debugMode", false);
145 
147  if (typeid(LeptonType) == typeid(pat::Muon))
148  modname += "muon";
149  else if (typeid(LeptonType) == typeid(pat::Electron))
150  modname += "electron";
151  modname += "PNetTags";
152  descriptions.add(modname, desc);
153 }
Analysis-level electron class.
Definition: Electron.h:51
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Analysis-level muon class.
Definition: Muon.h:51

◆ globalEndJob()

template<typename LeptonType >
void PNETLeptonProducer< LeptonType >::globalEndJob ( const ONNXRuntime cache)
static

Definition at line 134 of file PNETLeptonProducer.cc.

134 {}

◆ initializeGlobalCache()

template<typename LeptonType >
std::unique_ptr< cms::Ort::ONNXRuntime > PNETLeptonProducer< LeptonType >::initializeGlobalCache ( const edm::ParameterSet cfg)
static

Definition at line 128 of file PNETLeptonProducer.cc.

References looper::cfg, and contentValuesFiles::fullPath.

129  {
130  return std::make_unique<cms::Ort::ONNXRuntime>(cfg.getParameter<edm::FileInPath>("model_path").fullPath());
131 }

◆ make_inputs()

template<typename LeptonType >
void PNETLeptonProducer< LeptonType >::make_inputs ( const DeepBoostedJetFeatures taginfo)
private

Definition at line 156 of file PNETLeptonProducer.cc.

References btagbtvdeep::center_norm_pad(), EcalPhiSymFlatTableProducers_cfi::fill, btagbtvdeep::DeepBoostedJetFeatures::get(), mps_fire::i, and info().

156  {
157  for (unsigned igroup = 0; igroup < input_names_.size(); ++igroup) {
158  const auto &group_name = input_names_[igroup];
159  const auto &prep_params = prep_info_map_.at(group_name);
160  auto &group_values = data_[igroup];
161  group_values.resize(input_sizes_[igroup]);
162 
163  // first reset group_values to 0
164  std::fill(group_values.begin(), group_values.end(), 0);
165  unsigned curr_pos = 0;
166 
167  // transform/pad
168  for (unsigned i = 0; i < prep_params.var_names.size(); ++i) {
169  const auto &varname = prep_params.var_names[i];
170  const auto &raw_value = taginfo.get(varname);
171  const auto &info = prep_params.info(varname);
172  int insize = btagbtvdeep::center_norm_pad(raw_value,
173  info.center,
174  info.norm_factor,
175  prep_params.min_length,
176  prep_params.max_length,
177  group_values,
178  curr_pos,
179  info.pad,
180  info.replace_inf_value,
181  info.lower_bound,
182  info.upper_bound);
183  curr_pos += insize;
184  if (i == 0 && (!input_shapes_.empty())) {
185  input_shapes_[igroup][2] = insize;
186  }
187  }
188  group_values.resize(curr_pos);
189  }
190 }
cms::Ort::FloatArrays data_
static const TGPicture * info(bool iBackgroundIsBlack)
std::unordered_map< std::string, btagbtvdeep::PreprocessParams > prep_info_map_
const std::vector< float > & get(const std::string &name) const
std::vector< std::string > input_names_
std::vector< unsigned > input_sizes_
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)
std::vector< std::vector< int64_t > > input_shapes_

◆ produce()

template<typename LeptonType >
void PNETLeptonProducer< LeptonType >::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 93 of file PNETLeptonProducer.cc.

References cms::cuda::assert(), SplitLinear::begin, mps_fire::end, trigObjTnPSource_cfi::filler, iEvent, TtSemiLepHitFitProducer_Electrons_cfi::leps, eostools::move(), PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and TrackRefitter_38T_cff::src.

93  {
95  iEvent.getByToken(src_, src);
96 
98  iEvent.getByToken(leps_, leps);
99 
100  std::vector<std::vector<float>> mvaScores(flav_names_.size(), std::vector<float>(leps->size(), -1));
101 
102  // tagInfo src could be empty if the event has no PV
103  if (!src->empty()) {
104  assert(src->size() == leps->size());
105  for (size_t ilep = 0; ilep < src->size(); ilep++) {
106  const auto &taginfo = (*src)[ilep];
107  make_inputs(taginfo);
108  auto outputs = globalCache()->run(input_names_, data_, input_shapes_)[0];
109  // std::cout<<"outputs.size(): "<<outputs.size()<<std::endl;
110  // std::cout<<"flav_names_.size(): "<<flav_names_.size()<<std::endl;
111  assert(outputs.size() == flav_names_.size());
112  for (unsigned int iflav = 0; iflav < flav_names_.size(); ++iflav) {
113  mvaScores[iflav][ilep] = outputs.at(iflav);
114  }
115  }
116  }
117 
118  for (unsigned int iflav = 0; iflav < flav_names_.size(); ++iflav) {
119  std::unique_ptr<edm::ValueMap<float>> pnScore(new edm::ValueMap<float>());
121  filler.insert(leps, mvaScores[iflav].begin(), mvaScores[iflav].end());
122  filler.fill();
123  iEvent.put(std::move(pnScore), flav_names_[iflav]);
124  }
125 }
cms::Ort::FloatArrays data_
assert(be >=bs)
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< edm::View< LeptonType > > leps_
void make_inputs(const DeepBoostedJetFeatures &)
std::vector< std::string > input_names_
std::vector< std::string > flav_names_
edm::EDGetTokenT< LeptonTagInfoCollection > src_
def move(src, dest)
Definition: eostools.py:511
std::vector< std::vector< int64_t > > input_shapes_

Member Data Documentation

◆ data_

template<typename LeptonType >
cms::Ort::FloatArrays PNETLeptonProducer< LeptonType >::data_
private

◆ debug_

template<typename LeptonType >
bool PNETLeptonProducer< LeptonType >::debug_ = false
private

◆ flav_names_

template<typename LeptonType >
std::vector<std::string> PNETLeptonProducer< LeptonType >::flav_names_
private

◆ input_names_

template<typename LeptonType >
std::vector<std::string> PNETLeptonProducer< LeptonType >::input_names_
private

◆ input_shapes_

template<typename LeptonType >
std::vector<std::vector<int64_t> > PNETLeptonProducer< LeptonType >::input_shapes_
private

◆ input_sizes_

template<typename LeptonType >
std::vector<unsigned> PNETLeptonProducer< LeptonType >::input_sizes_
private

◆ leps_

template<typename LeptonType >
edm::EDGetTokenT<edm::View<LeptonType> > PNETLeptonProducer< LeptonType >::leps_
private

Definition at line 48 of file PNETLeptonProducer.cc.

◆ prep_info_map_

template<typename LeptonType >
std::unordered_map<std::string, btagbtvdeep::PreprocessParams> PNETLeptonProducer< LeptonType >::prep_info_map_
private

◆ src_

template<typename LeptonType >
edm::EDGetTokenT<LeptonTagInfoCollection> PNETLeptonProducer< LeptonType >::src_
private

Definition at line 47 of file PNETLeptonProducer.cc.