CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes
DeepMETProducer Class Reference
Inheritance diagram for DeepMETProducer:
edm::stream::EDProducer< edm::GlobalCache< DeepMETCache > >

Public Member Functions

 DeepMETProducer (const edm::ParameterSet &, const DeepMETCache *)
 
void produce (edm::Event &event, const edm::EventSetup &setup) override
 
- Public Member Functions inherited from edm::stream::EDProducer< edm::GlobalCache< DeepMETCache > >
 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 (DeepMETCache *)
 
static std::unique_ptr
< DeepMETCache
initializeGlobalCache (const edm::ParameterSet &)
 

Private Attributes

const bool ignore_leptons_
 
tensorflow::Tensor input_
 
tensorflow::Tensor input_cat0_
 
tensorflow::Tensor input_cat1_
 
tensorflow::Tensor input_cat2_
 
const unsigned int max_n_pf_
 
const float norm_
 
const edm::EDGetTokenT
< std::vector
< pat::PackedCandidate > > 
pf_token_
 
tensorflow::Session * session_
 

Static Private Attributes

static const
std::unordered_map< int,
int32_t > 
charge_embedding_ {{-1, 0}, {0, 1}, {1, 2}}
 
static const
std::unordered_map< int,
int32_t > 
pdg_id_embedding_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer< edm::GlobalCache< DeepMETCache > >
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

Definition at line 15 of file DeepMETProducer.cc.

Constructor & Destructor Documentation

DeepMETProducer::DeepMETProducer ( const edm::ParameterSet cfg,
const DeepMETCache cache 
)
explicit

Definition at line 52 of file DeepMETProducer.cc.

References input_, input_cat0_, input_cat1_, input_cat2_, and max_n_pf_.

53  : pf_token_(consumes<std::vector<pat::PackedCandidate> >(cfg.getParameter<edm::InputTag>("pf_src"))),
54  norm_(cfg.getParameter<double>("norm_factor")),
55  ignore_leptons_(cfg.getParameter<bool>("ignore_leptons")),
56  max_n_pf_(cfg.getParameter<unsigned int>("max_n_pf")),
58  produces<pat::METCollection>();
59 
60  const tensorflow::TensorShape shape({1, max_n_pf_, 8});
61  const tensorflow::TensorShape cat_shape({1, max_n_pf_, 1});
62 
63  input_ = tensorflow::Tensor(tensorflow::DT_FLOAT, shape);
64  input_cat0_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
65  input_cat1_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
66  input_cat2_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
67 }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:85
const bool ignore_leptons_
std::atomic< tensorflow::GraphDef * > graph_def
tensorflow::Tensor input_cat0_
tensorflow::Tensor input_cat1_
tensorflow::Tensor input_
tensorflow::Session * session_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
tensorflow::Tensor input_cat2_
const unsigned int max_n_pf_
const edm::EDGetTokenT< std::vector< pat::PackedCandidate > > pf_token_
const float norm_

Member Function Documentation

void DeepMETProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 151 of file DeepMETProducer.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::add(), submitPVResolutionJobs::desc, HLT_FULL_cff::InputTag, and AlCaHLTBitMon_QueryRunRegistry::string.

151  {
153  desc.add<edm::InputTag>("pf_src", edm::InputTag("packedPFCandidates"));
154  desc.add<bool>("ignore_leptons", false);
155  desc.add<double>("norm_factor", 50.);
156  desc.add<unsigned int>("max_n_pf", 4500);
157  desc.add<std::string>("graph_path", "RecoMET/METPUSubtraction/data/deepmet/deepmet_v1_2018.pb");
158  descriptions.add("deepMETProducer", desc);
159 }
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void DeepMETProducer::globalEndJob ( DeepMETCache cache)
static

Definition at line 149 of file DeepMETProducer.cc.

References DeepMETCache::graph_def.

149 { delete cache->graph_def; }
std::atomic< tensorflow::GraphDef * > graph_def
std::unique_ptr< DeepMETCache > DeepMETProducer::initializeGlobalCache ( const edm::ParameterSet params)
static

Definition at line 135 of file DeepMETProducer.cc.

References utilities::cache(), edm::FileInPath::fullPath(), edm::ParameterSet::getParameter(), HLT_FULL_cff::graphPath, tensorflow::loadGraphDef(), and AlCaHLTBitMon_QueryRunRegistry::string.

135  {
136  // this method is supposed to create, initialize and return a DeepMETCache instance
137  std::unique_ptr<DeepMETCache> cache = std::make_unique<DeepMETCache>();
138 
139  // load the graph def and save it
140  std::string graphPath = params.getParameter<std::string>("graph_path");
141  if (!graphPath.empty()) {
142  graphPath = edm::FileInPath(graphPath).fullPath();
143  cache->graph_def = tensorflow::loadGraphDef(graphPath);
144  }
145 
146  return cache;
147 }
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::string fullPath() const
Definition: FileInPath.cc:161
def cache
Definition: utilities.py:3
void DeepMETProducer::produce ( edm::Event event,
const edm::EventSetup setup 
)
override

Definition at line 69 of file DeepMETProducer.cc.

References funct::abs(), charge_embedding_, ignore_leptons_, input_, input_cat0_, input_cat1_, input_cat2_, max_n_pf_, eostools::move(), norm_, HLT_FULL_cff::output_names, pdg_id_embedding_, pf_token_, tensorflow::run(), pileupReCalc_HLTpaths::scale, and session_.

69  {
70  auto const& pfs = event.get(pf_token_);
71 
72  const tensorflow::NamedTensorList input_list = {
73  {"input", input_}, {"input_cat0", input_cat0_}, {"input_cat1", input_cat1_}, {"input_cat2", input_cat2_}};
74 
75  // Set all inputs to zero
76  input_.flat<float>().setZero();
77  input_cat0_.flat<float>().setZero();
78  input_cat1_.flat<float>().setZero();
79  input_cat2_.flat<float>().setZero();
80 
81  size_t i_pf = 0;
82  float px_leptons = 0.;
83  float py_leptons = 0.;
84  const float scale = 1. / norm_;
85  for (const auto& pf : pfs) {
86  if (ignore_leptons_) {
87  int pdg_id = std::abs(pf.pdgId());
88  if (pdg_id == 11 || pdg_id == 13) {
89  px_leptons += pf.px();
90  py_leptons += pf.py();
91  continue;
92  }
93  }
94 
95  // fill the tensor
96  // PF keys [b'PF_dxy', b'PF_dz', b'PF_eta', b'PF_mass', b'PF_pt', b'PF_puppiWeight', b'PF_px', b'PF_py']
97  float* ptr = &input_.tensor<float, 3>()(0, i_pf, 0);
98  *ptr = pf.dxy();
99  *(++ptr) = pf.dz();
100  *(++ptr) = pf.eta();
101  *(++ptr) = pf.mass();
102  *(++ptr) = scale_and_rm_outlier(pf.pt(), scale);
103  *(++ptr) = pf.puppiWeight();
104  *(++ptr) = scale_and_rm_outlier(pf.px(), scale);
105  *(++ptr) = scale_and_rm_outlier(pf.py(), scale);
106  input_cat0_.tensor<float, 3>()(0, i_pf, 0) = charge_embedding_.at(pf.charge());
107  input_cat1_.tensor<float, 3>()(0, i_pf, 0) = pdg_id_embedding_.at(pf.pdgId());
108  input_cat2_.tensor<float, 3>()(0, i_pf, 0) = pf.fromPV();
109 
110  ++i_pf;
111  if (i_pf == max_n_pf_) {
112  break; // output a warning?
113  }
114  }
115 
116  std::vector<tensorflow::Tensor> outputs;
117  const std::vector<std::string> output_names = {"output/BiasAdd"};
118 
119  // run the inference and return met
120  tensorflow::run(session_, input_list, output_names, &outputs);
121 
122  // The DNN directly estimates the missing px and py, not the recoil
123  float px = outputs[0].tensor<float, 2>()(0, 0) * norm_;
124  float py = outputs[0].tensor<float, 2>()(0, 1) * norm_;
125 
126  px -= px_leptons;
127  py -= py_leptons;
128 
129  auto pf_mets = std::make_unique<pat::METCollection>();
130  const reco::Candidate::LorentzVector p4(px, py, 0., std::hypot(px, py));
131  pf_mets->emplace_back(reco::MET(p4, {}));
132  event.put(std::move(pf_mets));
133 }
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:30
const bool ignore_leptons_
tensorflow::Tensor input_cat0_
tensorflow::Tensor input_cat1_
tensorflow::Tensor input_
Definition: MET.h:41
def move
Definition: eostools.py:511
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:213
static const std::unordered_map< int, int32_t > pdg_id_embedding_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
tensorflow::Session * session_
tensorflow::Tensor input_cat2_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
const unsigned int max_n_pf_
static const std::unordered_map< int, int32_t > charge_embedding_
const edm::EDGetTokenT< std::vector< pat::PackedCandidate > > pf_token_
const float norm_

Member Data Documentation

const std::unordered_map<int, int32_t> DeepMETProducer::charge_embedding_ {{-1, 0}, {0, 1}, {1, 2}}
inlinestaticprivate

Definition at line 38 of file DeepMETProducer.cc.

Referenced by produce().

const bool DeepMETProducer::ignore_leptons_
private

Definition at line 28 of file DeepMETProducer.cc.

Referenced by produce().

tensorflow::Tensor DeepMETProducer::input_
private

Definition at line 33 of file DeepMETProducer.cc.

Referenced by DeepMETProducer(), and produce().

tensorflow::Tensor DeepMETProducer::input_cat0_
private

Definition at line 34 of file DeepMETProducer.cc.

Referenced by DeepMETProducer(), and produce().

tensorflow::Tensor DeepMETProducer::input_cat1_
private

Definition at line 35 of file DeepMETProducer.cc.

Referenced by DeepMETProducer(), and produce().

tensorflow::Tensor DeepMETProducer::input_cat2_
private

Definition at line 36 of file DeepMETProducer.cc.

Referenced by DeepMETProducer(), and produce().

const unsigned int DeepMETProducer::max_n_pf_
private

Definition at line 29 of file DeepMETProducer.cc.

Referenced by DeepMETProducer(), and produce().

const float DeepMETProducer::norm_
private

Definition at line 27 of file DeepMETProducer.cc.

Referenced by produce().

const std::unordered_map<int, int32_t> DeepMETProducer::pdg_id_embedding_
inlinestaticprivate
Initial value:
{
{-211, 0}, {-13, 1}, {-11, 2}, {0, 3}, {1, 4}, {2, 5}, {11, 6}, {13, 7}, {22, 8}, {130, 9}, {211, 10}}

Definition at line 39 of file DeepMETProducer.cc.

Referenced by produce().

const edm::EDGetTokenT<std::vector<pat::PackedCandidate> > DeepMETProducer::pf_token_
private

Definition at line 26 of file DeepMETProducer.cc.

Referenced by produce().

tensorflow::Session* DeepMETProducer::session_
private

Definition at line 31 of file DeepMETProducer.cc.

Referenced by produce().