CMS 3D CMS Logo

MLPFProducer.cc
Go to the documentation of this file.
5 
9 
10 struct MLPFCache {
11  const tensorflow::GraphDef* graph_def;
12 };
13 
14 class MLPFProducer : public edm::stream::EDProducer<edm::GlobalCache<MLPFCache> > {
15 public:
16  explicit MLPFProducer(const edm::ParameterSet&, const MLPFCache*);
17  void produce(edm::Event& event, const edm::EventSetup& setup) override;
18  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
19 
20  // static methods for handling the global cache
21  static std::unique_ptr<MLPFCache> initializeGlobalCache(const edm::ParameterSet&);
22  static void globalEndJob(MLPFCache*);
23 
24 private:
28  tensorflow::Session* session_;
29 };
30 
32  : pfCandidatesPutToken_{produces<reco::PFCandidateCollection>()},
33  inputTagBlocks_(consumes<reco::PFBlockCollection>(cfg.getParameter<edm::InputTag>("src"))),
34  model_path_(cfg.getParameter<std::string>("model_path")) {
35  session_ = tensorflow::createSession(cache->graph_def);
36 }
37 
39  using namespace reco::mlpf;
40 
41  const auto& blocks = event.get(inputTagBlocks_);
42  const auto& all_elements = getPFElements(blocks);
43 
44  const long long int num_elements_total = all_elements.size();
45 
46  //tensor size must be a multiple of the bin size and larger than the number of elements
47  const auto tensor_size = LSH_BIN_SIZE * (num_elements_total / LSH_BIN_SIZE + 1);
48  assert(tensor_size <= NUM_MAX_ELEMENTS_BATCH);
49 
50  //Create the input tensor
51  tensorflow::TensorShape shape({BATCH_SIZE, tensor_size, NUM_ELEMENT_FEATURES});
52  tensorflow::Tensor input(tensorflow::DT_FLOAT, shape);
53  input.flat<float>().setZero();
54 
55  //Fill the input tensor
56  unsigned int ielem = 0;
57  for (const auto* pelem : all_elements) {
58  const auto& elem = *pelem;
59 
60  //prepare the input array from the PFElement
61  const auto& props = getElementProperties(elem);
62 
63  //copy features to the input array
64  for (unsigned int iprop = 0; iprop < NUM_ELEMENT_FEATURES; iprop++) {
65  input.tensor<float, 3>()(0, ielem, iprop) = normalize(props[iprop]);
66  }
67  ielem += 1;
68  }
69 
70  //TF model input and output tensor names
71  const tensorflow::NamedTensorList input_list = {{"x:0", input}};
72  const std::vector<std::string> output_names = {"Identity:0"};
73 
74  //Prepare the output tensor
75  std::vector<tensorflow::Tensor> outputs;
76 
77  //run the GNN inference, given the inputs and the output.
78  //Note that the GNN enables information transfer between the input PFElements,
79  //such that the output ML-PFCandidates are in general combinations of the input PFElements, in the form of
80  //y_out = Adj.x_in, where x_in is input matrix (num_elem, NUM_ELEMENT_FEATURES), y_out is the output matrix (num_elem, NUM_OUTPUT_FEATURES)
81  //and Adj is an adjacency matrix between the elements that is constructed on the fly during model inference.
82  tensorflow::run(session_, input_list, output_names, &outputs);
83 
84  //process the output tensor to ML-PFCandidates.
85  //The output can contain up to num_elem particles, with predicted PDGID=0 corresponding to no particles predicted.
86  const auto out_arr = outputs[0].tensor<float, 3>();
87 
88  std::vector<reco::PFCandidate> pOutputCandidateCollection;
89  for (unsigned int ielem = 0; ielem < all_elements.size(); ielem++) {
90  //get the coefficients in the output corresponding to the class probabilities (raw logits)
91  std::vector<float> pred_id_logits;
92  for (unsigned int idx_id = 0; idx_id <= NUM_CLASS; idx_id++) {
93  pred_id_logits.push_back(out_arr(0, ielem, idx_id));
94  }
95 
96  //get the most probable class PDGID
97  int pred_pid = pdgid_encoding[argMax(pred_id_logits)];
98 
99  //get the predicted momentum components
100  float pred_eta = out_arr(0, ielem, IDX_ETA);
101  float pred_phi = out_arr(0, ielem, IDX_PHI);
102  float pred_charge = out_arr(0, ielem, IDX_CHARGE);
103  float pred_e = out_arr(0, ielem, IDX_ENERGY);
104 
105  //a particle was predicted for this PFElement, otherwise it was a spectator
106  if (pred_pid != 0) {
107  auto cand = makeCandidate(pred_pid, pred_charge, pred_e, pred_eta, pred_phi);
108  setCandidateRefs(cand, all_elements, ielem);
109  pOutputCandidateCollection.push_back(cand);
110  }
111  } //loop over PFElements
112 
113  event.emplace(pfCandidatesPutToken_, pOutputCandidateCollection);
114 }
115 
117  // this method is supposed to create, initialize and return a MLPFCache instance
118  std::unique_ptr<MLPFCache> cache = std::make_unique<MLPFCache>();
119 
120  //load the frozen TF graph of the GNN model
121  std::string path = params.getParameter<std::string>("model_path");
123  LogDebug("MLPFProducer") << "Initializing MLPF model from " << fullPath;
124 
126 
127  return cache;
128 }
129 
130 void MLPFProducer::globalEndJob(MLPFCache* cache) { delete cache->graph_def; }
131 
134  desc.add<edm::InputTag>("src", edm::InputTag("particleFlowBlock"));
135  desc.add<std::string>("model_path", "RecoParticleFlow/PFProducer/data/mlpf/mlpf_2020_11_04.pb");
136  descriptions.addWithDefaultLabel(desc);
137 }
138 
MLPFCache::graph_def
const tensorflow::GraphDef * graph_def
Definition: MLPFProducer.cc:11
reco::mlpf::IDX_ENERGY
static constexpr unsigned int IDX_ENERGY
Definition: MLPFModel.h:29
tensorflow::createSession
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:85
MLPFProducer::produce
void produce(edm::Event &event, const edm::EventSetup &setup) override
Definition: MLPFProducer.cc:38
input
static const std::string input
Definition: EdmProvDump.cc:48
TensorFlow.h
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
MLPFCache
Definition: MLPFProducer.cc:10
PFCandidate.h
reco::mlpf::normalize
float normalize(float in)
Definition: MLPFModel.cc:148
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
edm::EDGetTokenT< reco::PFBlockCollection >
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
edm::EDPutTokenT< reco::PFCandidateCollection >
MLPFProducer::inputTagBlocks_
const edm::EDGetTokenT< reco::PFBlockCollection > inputTagBlocks_
Definition: MLPFProducer.cc:26
PatBasicFWLiteJetAnalyzer_Selector_cfg.outputs
outputs
Definition: PatBasicFWLiteJetAnalyzer_Selector_cfg.py:48
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
reco::mlpf::IDX_CHARGE
static constexpr unsigned int IDX_CHARGE
Definition: MLPFModel.h:30
cms::cuda::assert
assert(be >=bs)
reco::mlpf::LSH_BIN_SIZE
static constexpr int LSH_BIN_SIZE
Definition: MLPFModel.h:14
EDProducer.h
MLPFProducer::model_path_
const std::string model_path_
Definition: MLPFProducer.cc:27
MLPFModel.h
reco::mlpf::makeCandidate
reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi)
Definition: MLPFModel.cc:161
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
edm::FileInPath
Definition: FileInPath.h:64
MakerMacros.h
reco::mlpf::NUM_CLASS
static constexpr unsigned int NUM_CLASS
Definition: MLPFModel.h:26
reco::mlpf::setCandidateRefs
void setCandidateRefs(reco::PFCandidate &cand, const std::vector< const reco::PFBlockElement * > elems, size_t ielem_originator)
Definition: MLPFModel.cc:203
reco::mlpf::NUM_MAX_ELEMENTS_BATCH
static constexpr int NUM_MAX_ELEMENTS_BATCH
Definition: MLPFModel.h:13
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
utilities.cache
def cache(function)
Definition: utilities.py:3
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
reco::mlpf::getElementProperties
std::array< float, NUM_ELEMENT_FEATURES > getElementProperties(const reco::PFBlockElement &orig)
Definition: MLPFModel.cc:14
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::mlpf::NUM_ELEMENT_FEATURES
static constexpr unsigned int NUM_ELEMENT_FEATURES
Definition: MLPFModel.h:10
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
MLPFProducer::session_
tensorflow::Session * session_
Definition: MLPFProducer.cc:28
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
reco::mlpf::BATCH_SIZE
static constexpr int BATCH_SIZE
Definition: MLPFModel.h:17
tensorflow::NamedTensorList
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:30
reco::mlpf::pdgid_encoding
static const std::vector< int > pdgid_encoding
Definition: MLPFModel.h:34
MLPFProducer::pfCandidatesPutToken_
const edm::EDPutTokenT< reco::PFCandidateCollection > pfCandidatesPutToken_
Definition: MLPFProducer.cc:25
cand
Definition: decayParser.h:32
MLPFProducer::MLPFProducer
MLPFProducer(const edm::ParameterSet &, const MLPFCache *)
Definition: MLPFProducer.cc:31
MLPFProducer::initializeGlobalCache
static std::unique_ptr< MLPFCache > initializeGlobalCache(const edm::ParameterSet &)
Definition: MLPFProducer.cc:116
edm::stream::EDProducer
Definition: EDProducer.h:38
MLPFProducer::globalEndJob
static void globalEndJob(MLPFCache *)
Definition: MLPFProducer.cc:130
edm::EventSetup
Definition: EventSetup.h:58
reco::mlpf::getPFElements
const std::vector< const reco::PFBlockElement * > getPFElements(const reco::PFBlockCollection &blocks)
Definition: MLPFModel.cc:184
reco::mlpf::IDX_ETA
static constexpr unsigned int IDX_ETA
Definition: MLPFModel.h:27
looper.cfg
cfg
Definition: looper.py:297
tensorflow::loadGraphDef
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
Frameworkfwd.h
MLPFProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: MLPFProducer.cc:132
tensorflow::run
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
reco::mlpf::IDX_PHI
static constexpr unsigned int IDX_PHI
Definition: MLPFModel.h:28
reco::mlpf
Definition: MLPFModel.h:8
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
reco::mlpf::argMax
int argMax(std::vector< float > const &vec)
Definition: MLPFModel.cc:157
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
gather_cfg.blocks
blocks
Definition: gather_cfg.py:90
MLPFProducer
Definition: MLPFProducer.cc:14
edm::InputTag
Definition: InputTag.h:15
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:161
edm::ConfigurationDescriptions::addWithDefaultLabel
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:87