CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes
ticl::TracksterInferenceByDNN Class Reference

#include <TracksterInferenceByDNN.h>

Inheritance diagram for ticl::TracksterInferenceByDNN:
ticl::TracksterInferenceAlgoBase

Public Member Functions

void inputData (const std::vector< reco::CaloCluster > &layerClusters, std::vector< Trackster > &tracksters) override
 
void runInference (std::vector< Trackster > &tracksters) override
 
 TracksterInferenceByDNN (const edm::ParameterSet &conf)
 
- Public Member Functions inherited from ticl::TracksterInferenceAlgoBase
 TracksterInferenceAlgoBase (const edm::ParameterSet &conf)
 
virtual ~TracksterInferenceAlgoBase ()
 

Static Public Member Functions

static void fillPSetDescription (edm::ParameterSetDescription &iDesc)
 
- Static Public Member Functions inherited from ticl::TracksterInferenceAlgoBase
static void fillPSetDescription (edm::ParameterSetDescription &desc)
 

Private Attributes

int batchSize_
 
int doPID_
 
int doRegression_
 
const float eidMinClusterEnergy_
 
const int eidNClusters_
 
const int eidNLayers_
 
const std::string en_modelPath_
 
const std::string id_modelPath_
 
std::vector< std::vector< float > > input_Data_
 
std::vector< std::vector< int64_t > > input_shapes_
 
const std::vector< std::string > inputNames_
 
const cms::Ort::ONNXRuntimeonnxEnergySession_
 
const cms::Ort::ONNXRuntimeonnxPIDSession_
 
const std::vector< std::string > output_en_
 
const std::vector< std::string > output_id_
 
hgcal::RecHitTools rhtools_
 
std::vector< int > tracksterIndices_
 

Static Private Attributes

static constexpr int eidNFeatures_ = 3
 

Additional Inherited Members

- Protected Attributes inherited from ticl::TracksterInferenceAlgoBase
int algo_verbosity_
 

Detailed Description

Definition at line 9 of file TracksterInferenceByDNN.h.

Constructor & Destructor Documentation

◆ TracksterInferenceByDNN()

ticl::TracksterInferenceByDNN::TracksterInferenceByDNN ( const edm::ParameterSet conf)
explicit

Definition at line 14 of file TracksterInferenceByDNN.cc.

References en_modelPath_, id_modelPath_, onnxEnergySession_, and onnxPIDSession_.

17  conf.getParameter<edm::FileInPath>("onnxPIDModelPath").fullPath()), // Path to the PID model CLU3D
19  conf.getParameter<edm::FileInPath>("onnxEnergyModelPath").fullPath()), // Path to the Energy model CLU3D
20  inputNames_(conf.getParameter<std::vector<std::string>>("inputNames")), // Define input names for inference
21  output_en_(conf.getParameter<std::vector<std::string>>("output_en")), // Define output energy for inference
22  output_id_(conf.getParameter<std::vector<std::string>>("output_id")), // Define output PID for inference
23  eidMinClusterEnergy_(conf.getParameter<double>("eid_min_cluster_energy")), // Minimum cluster energy
24  eidNLayers_(conf.getParameter<int>("eid_n_layers")), // Number of layers
25  eidNClusters_(conf.getParameter<int>("eid_n_clusters")), // Number of clusters
26  doPID_(conf.getParameter<int>("doPID")), // Number of clusters
27  doRegression_(conf.getParameter<int>("doRegression")) // Number of clusters
28  {
29  // Initialize ONNX Runtime sessions for PID and Energy models
30  static std::unique_ptr<cms::Ort::ONNXRuntime> onnxPIDRuntimeInstance =
31  std::make_unique<cms::Ort::ONNXRuntime>(id_modelPath_.c_str());
32  onnxPIDSession_ = onnxPIDRuntimeInstance.get();
33  static std::unique_ptr<cms::Ort::ONNXRuntime> onnxEnergyRuntimeInstance =
34  std::make_unique<cms::Ort::ONNXRuntime>(en_modelPath_.c_str());
35  onnxEnergySession_ = onnxEnergyRuntimeInstance.get();
36  }
TracksterInferenceAlgoBase(const edm::ParameterSet &conf)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const std::vector< std::string > output_en_
const std::vector< std::string > inputNames_
const cms::Ort::ONNXRuntime * onnxEnergySession_
const cms::Ort::ONNXRuntime * onnxPIDSession_
const std::vector< std::string > output_id_
const std::string & fullPath() const
Definition: FileInPath.cc:144

Member Function Documentation

◆ fillPSetDescription()

void ticl::TracksterInferenceByDNN::fillPSetDescription ( edm::ParameterSetDescription iDesc)
static

Definition at line 128 of file TracksterInferenceByDNN.cc.

References edm::ParameterSetDescription::add().

128  {
129  iDesc.add<int>("algo_verbosity", 0);
130  iDesc
131  .add<edm::FileInPath>("onnxPIDModelPath",
132  edm::FileInPath("RecoHGCal/TICL/data/ticlv5/onnx_models/patternrecognition/id_v0.onnx"))
133  ->setComment("Path to ONNX PID model CLU3D");
134  iDesc
136  "onnxEnergyModelPath",
137  edm::FileInPath("RecoHGCal/TICL/data/ticlv5/onnx_models/patternrecognition/energy_v0.onnx"))
138  ->setComment("Path to ONNX Energy model CLU3D");
139  iDesc.add<std::vector<std::string>>("inputNames", {"input"});
140  iDesc.add<std::vector<std::string>>("output_en", {"enreg_output"});
141  iDesc.add<std::vector<std::string>>("output_id", {"pid_output"});
142  iDesc.add<double>("eid_min_cluster_energy", 1.0);
143  iDesc.add<int>("eid_n_layers", 50);
144  iDesc.add<int>("eid_n_clusters", 10);
145  iDesc.add<int>("doPID", 1);
146  iDesc.add<int>("doRegression", 1);
147  }
ParameterDescriptionBase * add(U const &iLabel, T const &value)

◆ inputData()

void ticl::TracksterInferenceByDNN::inputData ( const std::vector< reco::CaloCluster > &  layerClusters,
std::vector< Trackster > &  tracksters 
)
overridevirtual

Implements ticl::TracksterInferenceAlgoBase.

Definition at line 39 of file TracksterInferenceByDNN.cc.

References a, funct::abs(), b, batchSize_, eidMinClusterEnergy_, eidNClusters_, eidNFeatures_, eidNLayers_, reco::CaloCluster::energy(), reco::CaloCluster::eta(), f, hgcal::RecHitTools::getLayerWithOffset(), reco::CaloCluster::hitsAndFractions(), mps_fire::i, input_Data_, input_shapes_, dqmiolumiharvest::j, isotrackApplyRegressor::k, hltEgammaHGCALIDVarsL1Seeded_cfi::layerClusters, reco::CaloCluster::phi(), rhtools_, jetUpdater_cfi::sort, tracksterIndices_, HitToTracksterAssociation_cfi::tracksters, bphysicsOniaDQM_cfi::vertex, ticl::Trackster::vertex_multiplicity(), AlignmentTracksFromVertexSelector_cfi::vertices, and ticl::Trackster::vertices().

40  {
41  tracksterIndices_.clear(); // Clear previous indices
42  for (int i = 0; i < static_cast<int>(tracksters.size()); i++) {
43  float sumClusterEnergy = 0.;
44  for (const unsigned int& vertex : tracksters[i].vertices()) {
45  sumClusterEnergy += static_cast<float>(layerClusters[vertex].energy());
46  if (sumClusterEnergy >= eidMinClusterEnergy_) {
47  tracksters[i].setRegressedEnergy(0.f); // Set regressed energy to 0
48  tracksters[i].zeroProbabilities(); // Zero out probabilities
49  tracksterIndices_.push_back(i); // Add index to the list
50  break;
51  }
52  }
53  }
54 
55  // Prepare input shapes and data for inference
56  batchSize_ = static_cast<int>(tracksterIndices_.size());
57  if (batchSize_ == 0)
58  return; // Exit if no tracksters
59 
60  std::vector<int64_t> inputShape = {batchSize_, eidNLayers_, eidNClusters_, eidNFeatures_};
61  input_shapes_ = {inputShape};
62 
63  input_Data_.clear();
65 
66  for (int i = 0; i < batchSize_; i++) {
67  const Trackster& trackster = tracksters[tracksterIndices_[i]];
68 
69  // Prepare indices and sort clusters based on energy
70  std::vector<int> clusterIndices(trackster.vertices().size());
71  for (int k = 0; k < static_cast<int>(trackster.vertices().size()); k++) {
72  clusterIndices[k] = k;
73  }
74 
75  std::sort(clusterIndices.begin(), clusterIndices.end(), [&layerClusters, &trackster](const int& a, const int& b) {
76  return layerClusters[trackster.vertices(a)].energy() > layerClusters[trackster.vertices(b)].energy();
77  });
78 
79  std::vector<int> seenClusters(eidNLayers_, 0);
80 
81  // Fill input data with cluster information
82  for (const int& k : clusterIndices) {
83  const reco::CaloCluster& cluster = layerClusters[trackster.vertices(k)];
84  int j = rhtools_.getLayerWithOffset(cluster.hitsAndFractions()[0].first) - 1;
85  if (j < eidNLayers_ && seenClusters[j] < eidNClusters_) {
86  auto index = (i * eidNLayers_ + j) * eidNFeatures_ * eidNClusters_ + seenClusters[j] * eidNFeatures_;
87  input_Data_[0][index] =
88  static_cast<float>(cluster.energy() / static_cast<float>(trackster.vertex_multiplicity(k)));
89  input_Data_[0][index + 1] = static_cast<float>(std::abs(cluster.eta()));
90  input_Data_[0][index + 2] = static_cast<float>(cluster.phi());
91  seenClusters[j]++;
92  }
93  }
94  }
95  }
const std::vector< std::pair< DetId, float > > & hitsAndFractions() const
Definition: CaloCluster.h:210
std::vector< std::vector< int64_t > > input_shapes_
double phi() const
azimuthal angle of cluster centroid
Definition: CaloCluster.h:184
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
double energy() const
cluster energy
Definition: CaloCluster.h:149
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
double eta() const
pseudorapidity of cluster centroid
Definition: CaloCluster.h:181
std::vector< std::vector< float > > input_Data_
unsigned int getLayerWithOffset(const DetId &) const
Definition: RecHitTools.cc:381

◆ runInference()

void ticl::TracksterInferenceByDNN::runInference ( std::vector< Trackster > &  tracksters)
overridevirtual

Implements ticl::TracksterInferenceAlgoBase.

Definition at line 98 of file TracksterInferenceByDNN.cc.

References batchSize_, doPID_, doRegression_, HBHEDarkening_cff::energy, mps_fire::i, input_Data_, input_shapes_, inputNames_, onnxEnergySession_, onnxPIDSession_, output_en_, output_id_, mps_fire::result, cms::Ort::ONNXRuntime::run(), tracksterIndices_, and HitToTracksterAssociation_cfi::tracksters.

98  {
99  if (batchSize_ == 0)
100  return; // Exit if no batch
101 
102  if (doPID_ and doRegression_) {
103  // Run energy model inference
105  auto& energyOutputTensor = result[0];
106  if (!output_en_.empty()) {
107  for (int i = 0; i < static_cast<int>(batchSize_); i++) {
108  const float energy = energyOutputTensor[i];
109  tracksters[tracksterIndices_[i]].setRegressedEnergy(energy); // Update energy
110  }
111  }
112  }
113 
114  if (doPID_) {
115  // Run PID model inference
117  auto pidOutputTensor = pidOutput[0];
118  float* probs = pidOutputTensor.data();
119  if (!output_id_.empty()) {
120  for (int i = 0; i < batchSize_; i++) {
121  tracksters[tracksterIndices_[i]].setProbabilities(probs); // Update probabilities
122  probs += tracksters[tracksterIndices_[i]].id_probabilities().size(); // Move to next set of probabilities
123  }
124  }
125  }
126  }
const std::vector< std::string > output_en_
std::vector< std::vector< int64_t > > input_shapes_
const std::vector< std::string > inputNames_
const cms::Ort::ONNXRuntime * onnxEnergySession_
const cms::Ort::ONNXRuntime * onnxPIDSession_
const std::vector< std::string > output_id_
std::vector< std::vector< float > > input_Data_
FloatArrays run(const std::vector< std::string > &input_names, FloatArrays &input_values, const std::vector< std::vector< int64_t >> &input_shapes={}, const std::vector< std::string > &output_names={}, int64_t batch_size=1) const
Definition: ONNXRuntime.cc:87

Member Data Documentation

◆ batchSize_

int ticl::TracksterInferenceByDNN::batchSize_
private

Definition at line 37 of file TracksterInferenceByDNN.h.

Referenced by inputData(), and runInference().

◆ doPID_

int ticl::TracksterInferenceByDNN::doPID_
private

Definition at line 30 of file TracksterInferenceByDNN.h.

Referenced by runInference().

◆ doRegression_

int ticl::TracksterInferenceByDNN::doRegression_
private

Definition at line 31 of file TracksterInferenceByDNN.h.

Referenced by runInference().

◆ eidMinClusterEnergy_

const float ticl::TracksterInferenceByDNN::eidMinClusterEnergy_
private

Definition at line 26 of file TracksterInferenceByDNN.h.

Referenced by inputData().

◆ eidNClusters_

const int ticl::TracksterInferenceByDNN::eidNClusters_
private

Definition at line 28 of file TracksterInferenceByDNN.h.

Referenced by inputData().

◆ eidNFeatures_

constexpr int ticl::TracksterInferenceByDNN::eidNFeatures_ = 3
staticprivate

Definition at line 29 of file TracksterInferenceByDNN.h.

Referenced by inputData().

◆ eidNLayers_

const int ticl::TracksterInferenceByDNN::eidNLayers_
private

Definition at line 27 of file TracksterInferenceByDNN.h.

Referenced by inputData().

◆ en_modelPath_

const std::string ticl::TracksterInferenceByDNN::en_modelPath_
private

Definition at line 22 of file TracksterInferenceByDNN.h.

Referenced by TracksterInferenceByDNN().

◆ id_modelPath_

const std::string ticl::TracksterInferenceByDNN::id_modelPath_
private

Definition at line 21 of file TracksterInferenceByDNN.h.

Referenced by TracksterInferenceByDNN().

◆ input_Data_

std::vector<std::vector<float> > ticl::TracksterInferenceByDNN::input_Data_
private

Definition at line 36 of file TracksterInferenceByDNN.h.

Referenced by inputData(), and runInference().

◆ input_shapes_

std::vector<std::vector<int64_t> > ticl::TracksterInferenceByDNN::input_shapes_
private

Definition at line 34 of file TracksterInferenceByDNN.h.

Referenced by inputData(), and runInference().

◆ inputNames_

const std::vector<std::string> ticl::TracksterInferenceByDNN::inputNames_
private

Definition at line 23 of file TracksterInferenceByDNN.h.

Referenced by runInference().

◆ onnxEnergySession_

const cms::Ort::ONNXRuntime* ticl::TracksterInferenceByDNN::onnxEnergySession_
private

Definition at line 19 of file TracksterInferenceByDNN.h.

Referenced by runInference(), and TracksterInferenceByDNN().

◆ onnxPIDSession_

const cms::Ort::ONNXRuntime* ticl::TracksterInferenceByDNN::onnxPIDSession_
private

Definition at line 18 of file TracksterInferenceByDNN.h.

Referenced by runInference(), and TracksterInferenceByDNN().

◆ output_en_

const std::vector<std::string> ticl::TracksterInferenceByDNN::output_en_
private

Definition at line 24 of file TracksterInferenceByDNN.h.

Referenced by runInference().

◆ output_id_

const std::vector<std::string> ticl::TracksterInferenceByDNN::output_id_
private

Definition at line 25 of file TracksterInferenceByDNN.h.

Referenced by runInference().

◆ rhtools_

hgcal::RecHitTools ticl::TracksterInferenceByDNN::rhtools_
private

Definition at line 33 of file TracksterInferenceByDNN.h.

Referenced by inputData().

◆ tracksterIndices_

std::vector<int> ticl::TracksterInferenceByDNN::tracksterIndices_
private

Definition at line 35 of file TracksterInferenceByDNN.h.

Referenced by inputData(), and runInference().