CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Attributes
MLPFProducer Class Reference
Inheritance diagram for MLPFProducer:
edm::stream::EDProducer< edm::GlobalCache< MLPFCache > >

Public Member Functions

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

Private Attributes

const edm::EDGetTokenT< reco::PFBlockCollectioninputTagBlocks_
 
const std::string model_path_
 
const edm::EDPutTokenT< reco::PFCandidateCollectionpfCandidatesPutToken_
 
tensorflow::Session * session_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer< edm::GlobalCache< MLPFCache > >
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 14 of file MLPFProducer.cc.

Constructor & Destructor Documentation

◆ MLPFProducer()

MLPFProducer::MLPFProducer ( const edm::ParameterSet cfg,
const MLPFCache cache 
)
explicit

Definition at line 31 of file MLPFProducer.cc.

32  : pfCandidatesPutToken_{produces<reco::PFCandidateCollection>()},
33  inputTagBlocks_(consumes<reco::PFBlockCollection>(cfg.getParameter<edm::InputTag>("src"))),
34  model_path_(cfg.getParameter<std::string>("model_path")) {
36 }

Member Function Documentation

◆ fillDescriptions()

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

Definition at line 132 of file MLPFProducer.cc.

132  {
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 }

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

◆ globalEndJob()

void MLPFProducer::globalEndJob ( MLPFCache cache)
static

Definition at line 130 of file MLPFProducer.cc.

130 { delete cache->graph_def; }

References utilities::cache().

◆ initializeGlobalCache()

std::unique_ptr< MLPFCache > MLPFProducer::initializeGlobalCache ( const edm::ParameterSet params)
static

Definition at line 116 of file MLPFProducer.cc.

116  {
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 }

References utilities::cache(), contentValuesFiles::fullPath, edm::FileInPath::fullPath(), tensorflow::loadGraphDef(), LogDebug, CalibrationSummaryClient_cfi::params, castor_dqm_sourceclient_file_cfg::path, and AlCaHLTBitMon_QueryRunRegistry::string.

◆ produce()

void MLPFProducer::produce ( edm::Event event,
const edm::EventSetup setup 
)
override

Definition at line 38 of file MLPFProducer.cc.

38  {
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 }

References reco::mlpf::argMax(), cms::cuda::assert(), reco::mlpf::BATCH_SIZE, gather_cfg::blocks, dqmMemoryStats::float, reco::mlpf::getElementProperties(), reco::mlpf::getPFElements(), reco::mlpf::IDX_CHARGE, reco::mlpf::IDX_ENERGY, reco::mlpf::IDX_ETA, reco::mlpf::IDX_PHI, input, inputTagBlocks_, reco::mlpf::LSH_BIN_SIZE, reco::mlpf::makeCandidate(), reco::mlpf::normalize(), reco::mlpf::NUM_CLASS, reco::mlpf::NUM_ELEMENT_FEATURES, reco::mlpf::NUM_MAX_ELEMENTS_BATCH, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, reco::mlpf::pdgid_encoding, pfCandidatesPutToken_, tensorflow::run(), session_, and reco::mlpf::setCandidateRefs().

Member Data Documentation

◆ inputTagBlocks_

const edm::EDGetTokenT<reco::PFBlockCollection> MLPFProducer::inputTagBlocks_
private

Definition at line 26 of file MLPFProducer.cc.

Referenced by produce().

◆ model_path_

const std::string MLPFProducer::model_path_
private

Definition at line 27 of file MLPFProducer.cc.

◆ pfCandidatesPutToken_

const edm::EDPutTokenT<reco::PFCandidateCollection> MLPFProducer::pfCandidatesPutToken_
private

Definition at line 25 of file MLPFProducer.cc.

Referenced by produce().

◆ session_

tensorflow::Session* MLPFProducer::session_
private

Definition at line 28 of file MLPFProducer.cc.

Referenced by produce().

reco::mlpf::IDX_ENERGY
static constexpr unsigned int IDX_ENERGY
Definition: MLPFModel.h:29
tensorflow::createSession
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:85
input
static const std::string input
Definition: EdmProvDump.cc:48
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
reco::mlpf::normalize
float normalize(float in)
Definition: MLPFModel.cc:149
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
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:89301
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
MLPFProducer::model_path_
const std::string model_path_
Definition: MLPFProducer.cc:27
reco::mlpf::makeCandidate
reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi)
Definition: MLPFModel.cc:162
edm::FileInPath
Definition: FileInPath.h:61
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:204
reco::mlpf::NUM_MAX_ELEMENTS_BATCH
static constexpr int NUM_MAX_ELEMENTS_BATCH
Definition: MLPFModel.h:13
utilities.cache
def cache(function)
Definition: utilities.py:3
reco::mlpf::getElementProperties
std::array< float, NUM_ELEMENT_FEATURES > getElementProperties(const reco::PFBlockElement &orig)
Definition: MLPFModel.cc:15
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
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
reco::mlpf::getPFElements
const std::vector< const reco::PFBlockElement * > getPFElements(const reco::PFBlockCollection &blocks)
Definition: MLPFModel.cc:185
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::mlpf::IDX_ETA
static constexpr unsigned int IDX_ETA
Definition: MLPFModel.h:27
looper.cfg
cfg
Definition: looper.py:296
tensorflow::loadGraphDef
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
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:158
gather_cfg.blocks
blocks
Definition: gather_cfg.py:90
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