CMS 3D CMS Logo

DeepMETProducer.cc
Go to the documentation of this file.
5 
8 
11 
12 using namespace deepmet_helper;
13 
14 struct DeepMETCache {
15  std::atomic<tensorflow::GraphDef*> graph_def;
16 };
17 
18 class DeepMETProducer : public edm::stream::EDProducer<edm::GlobalCache<DeepMETCache> > {
19 public:
20  explicit DeepMETProducer(const edm::ParameterSet&, const DeepMETCache*);
21  void produce(edm::Event& event, const edm::EventSetup& setup) override;
22  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
23 
24  // static methods for handling the global cache
25  static std::unique_ptr<DeepMETCache> initializeGlobalCache(const edm::ParameterSet&);
26  static void globalEndJob(DeepMETCache*);
27 
28 private:
30  const float norm_;
31  const bool ignore_leptons_;
32  const unsigned int max_n_pf_;
33 
34  tensorflow::Session* session_;
35 
36  tensorflow::Tensor input_;
37  tensorflow::Tensor input_cat0_;
38  tensorflow::Tensor input_cat1_;
39  tensorflow::Tensor input_cat2_;
40 };
41 
43  : pf_token_(consumes<std::vector<pat::PackedCandidate> >(cfg.getParameter<edm::InputTag>("pf_src"))),
44  norm_(cfg.getParameter<double>("norm_factor")),
45  ignore_leptons_(cfg.getParameter<bool>("ignore_leptons")),
46  max_n_pf_(cfg.getParameter<unsigned int>("max_n_pf")),
47  session_(tensorflow::createSession(cache->graph_def)) {
48  produces<pat::METCollection>();
49 
50  const tensorflow::TensorShape shape({1, max_n_pf_, 8});
51  const tensorflow::TensorShape cat_shape({1, max_n_pf_, 1});
52 
53  input_ = tensorflow::Tensor(tensorflow::DT_FLOAT, shape);
54  input_cat0_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
55  input_cat1_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
56  input_cat2_ = tensorflow::Tensor(tensorflow::DT_FLOAT, cat_shape);
57 }
58 
60  auto const& pfs = event.get(pf_token_);
61 
62  const tensorflow::NamedTensorList input_list = {
63  {"input", input_}, {"input_cat0", input_cat0_}, {"input_cat1", input_cat1_}, {"input_cat2", input_cat2_}};
64 
65  // Set all inputs to zero
66  input_.flat<float>().setZero();
67  input_cat0_.flat<float>().setZero();
68  input_cat1_.flat<float>().setZero();
69  input_cat2_.flat<float>().setZero();
70 
71  size_t i_pf = 0;
72  float px_leptons = 0.;
73  float py_leptons = 0.;
74  const float scale = 1. / norm_;
75  for (const auto& pf : pfs) {
76  if (ignore_leptons_) {
77  int pdg_id = std::abs(pf.pdgId());
78  if (pdg_id == 11 || pdg_id == 13) {
79  px_leptons += pf.px();
80  py_leptons += pf.py();
81  continue;
82  }
83  }
84 
85  // fill the tensor
86  // 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']
87  float* ptr = &input_.tensor<float, 3>()(0, i_pf, 0);
88  *ptr = pf.dxy();
89  *(++ptr) = pf.dz();
90  *(++ptr) = pf.eta();
91  *(++ptr) = pf.mass();
92  *(++ptr) = scale_and_rm_outlier(pf.pt(), scale);
93  *(++ptr) = pf.puppiWeight();
94  *(++ptr) = scale_and_rm_outlier(pf.px(), scale);
95  *(++ptr) = scale_and_rm_outlier(pf.py(), scale);
96  input_cat0_.tensor<float, 3>()(0, i_pf, 0) = charge_embedding.at(pf.charge());
97  input_cat1_.tensor<float, 3>()(0, i_pf, 0) = pdg_id_embedding.at(pf.pdgId());
98  input_cat2_.tensor<float, 3>()(0, i_pf, 0) = pf.fromPV();
99 
100  ++i_pf;
101  if (i_pf == max_n_pf_) {
102  break; // output a warning?
103  }
104  }
105 
106  std::vector<tensorflow::Tensor> outputs;
107  const std::vector<std::string> output_names = {"output/BiasAdd"};
108 
109  // run the inference and return met
111 
112  // The DNN directly estimates the missing px and py, not the recoil
113  float px = outputs[0].tensor<float, 2>()(0, 0) * norm_;
114  float py = outputs[0].tensor<float, 2>()(0, 1) * norm_;
115 
116  px -= px_leptons;
117  py -= py_leptons;
118 
119  LogDebug("produce") << "<DeepMETProducer::produce>:" << std::endl
120  << " MET from DeepMET Producer is MET_x " << px << " and MET_y " << py << std::endl;
121 
122  auto pf_mets = std::make_unique<pat::METCollection>();
123  const reco::Candidate::LorentzVector p4(px, py, 0., std::hypot(px, py));
124  pf_mets->emplace_back(reco::MET(p4, {}));
125  event.put(std::move(pf_mets));
126 }
127 
128 std::unique_ptr<DeepMETCache> DeepMETProducer::initializeGlobalCache(const edm::ParameterSet& params) {
129  // this method is supposed to create, initialize and return a DeepMETCache instance
130  std::unique_ptr<DeepMETCache> cache = std::make_unique<DeepMETCache>();
131 
132  // load the graph def and save it
133  std::string graphPath = params.getParameter<std::string>("graph_path");
134  if (!graphPath.empty()) {
137  }
138 
139  return cache;
140 }
141 
143 
146  desc.add<edm::InputTag>("pf_src", edm::InputTag("packedPFCandidates"));
147  desc.add<bool>("ignore_leptons", false);
148  desc.add<double>("norm_factor", 50.);
149  desc.add<unsigned int>("max_n_pf", 4500);
150  desc.add<std::string>("graph_path", "RecoMET/METPUSubtraction/data/models/deepmet/deepmet_v1_2018/model.graphdef");
151  descriptions.add("deepMETProducer", desc);
152 }
153 
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:85
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:30
const bool ignore_leptons_
std::atomic< tensorflow::GraphDef * > graph_def
std::string fullPath() const
Definition: FileInPath.cc:161
void produce(edm::Event &event, const edm::EventSetup &setup) override
tensorflow::Tensor input_cat0_
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
tensorflow::Tensor input_cat1_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void globalEndJob(DeepMETCache *)
static std::unique_ptr< DeepMETCache > initializeGlobalCache(const edm::ParameterSet &)
tensorflow::Tensor input_
DeepMETProducer(const edm::ParameterSet &, const DeepMETCache *)
static const std::unordered_map< int, int32_t > charge_embedding
Definition: DeepMETHelp.h:10
Definition: HeavyIon.h:7
Definition: MET.h:41
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
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
tensorflow::Session * session_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static const std::unordered_map< int, int32_t > pdg_id_embedding
Definition: DeepMETHelp.h:11
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
tensorflow::Tensor input_cat2_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
HLT enums.
def cache(function)
Definition: utilities.py:3
float scale_and_rm_outlier(float val, float scale)
Definition: DeepMETHelper.cc:4
const unsigned int max_n_pf_
const edm::EDGetTokenT< std::vector< pat::PackedCandidate > > pf_token_
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
const float norm_
#define LogDebug(id)