CMS 3D CMS Logo

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

#include <TracksterInferenceByCNNv4.h>

Inheritance diagram for ticl::TracksterInferenceByCNNv4:
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
 
 TracksterInferenceByCNNv4 (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_
 
std::vector< std::vector< float > > input_Data_
 
std::vector< std::vector< int64_t > > input_shapes_
 
const std::vector< std::string > inputNames_
 
const std::string modelPath_
 
const cms::Ort::ONNXRuntimeonnxSession_
 
const std::vector< std::string > outputNames_
 
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 TracksterInferenceByCNNv4.h.

Constructor & Destructor Documentation

◆ TracksterInferenceByCNNv4()

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

Definition at line 14 of file TracksterInferenceByCNNv4.cc.

References modelPath_, and onnxSession_.

16  modelPath_(conf.getParameter<edm::FileInPath>("onnxModelPath").fullPath()), // Path to the PID model CLU3D
17  inputNames_(conf.getParameter<std::vector<std::string>>("inputNames")), // Define input names for inference
18  outputNames_(conf.getParameter<std::vector<std::string>>("outputNames")), // Define output names for inference
19  eidMinClusterEnergy_(conf.getParameter<double>("eid_min_cluster_energy")), // Minimum cluster energy
20  eidNLayers_(conf.getParameter<int>("eid_n_layers")), // Number of layers
21  eidNClusters_(conf.getParameter<int>("eid_n_clusters")), // Number of clusters
22  doPID_(conf.getParameter<int>("doPID")), // Number of clusters
23  doRegression_(conf.getParameter<int>("doRegression")) // Number of clusters
24  {
25  // Initialize ONNX Runtime sessions for PID and Energy models
26  static std::unique_ptr<cms::Ort::ONNXRuntime> onnxRuntimeInstance =
27  std::make_unique<cms::Ort::ONNXRuntime>(modelPath_.c_str());
28  onnxSession_ = onnxRuntimeInstance.get();
29  }
TracksterInferenceAlgoBase(const edm::ParameterSet &conf)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const std::vector< std::string > outputNames_
const cms::Ort::ONNXRuntime * onnxSession_
const std::string & fullPath() const
Definition: FileInPath.cc:144
const std::vector< std::string > inputNames_

Member Function Documentation

◆ fillPSetDescription()

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

Definition at line 122 of file TracksterInferenceByCNNv4.cc.

References edm::ParameterSetDescription::add().

122  {
123  iDesc.add<int>("algo_verbosity", 0);
124  iDesc
125  .add<edm::FileInPath>("onnxModelPath",
126  edm::FileInPath("RecoHGCal/TICL/data/ticlv4/onnx_models/energy_id_v0.onnx"))
127  ->setComment("Path to ONNX PID model CLU3D");
128  iDesc.add<std::vector<std::string>>("inputNames", {"input:0"});
129  iDesc.add<std::vector<std::string>>("outputNames", {"output/regressed_energy:0", "output/id_probabilities:0"});
130  iDesc.add<double>("eid_min_cluster_energy", 1.0);
131  iDesc.add<int>("eid_n_layers", 50);
132  iDesc.add<int>("eid_n_clusters", 10);
133  iDesc.add<int>("doPID", 1);
134  iDesc.add<int>("doRegression", 0);
135  }
ParameterDescriptionBase * add(U const &iLabel, T const &value)

◆ inputData()

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

Implements ticl::TracksterInferenceAlgoBase.

Definition at line 32 of file TracksterInferenceByCNNv4.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().

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

◆ runInference()

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

Implements ticl::TracksterInferenceAlgoBase.

Definition at line 91 of file TracksterInferenceByCNNv4.cc.

References batchSize_, doPID_, doRegression_, HBHEDarkening_cff::energy, mps_fire::i, input_Data_, input_shapes_, inputNames_, onnxSession_, outputNames_, cms::Ort::ONNXRuntime::run(), tracksterIndices_, and HitToTracksterAssociation_cfi::tracksters.

91  {
92  if (batchSize_ == 0)
93  return; // Exit if no batch
94 
95  std::vector<std::vector<float>> outputTensors;
97  if (doPID_ and doRegression_) {
98  // Run energy model inference
99  if (!outputNames_.empty()) {
100  for (int i = 0; i < static_cast<int>(batchSize_); i++) {
101  const float energy = outputTensors[0][i];
102  tracksters[tracksterIndices_[i]].setRegressedEnergy(energy); // Update energy
103  }
104  }
105  }
106 
107  if (doPID_) {
108  // Run PID model inference
109  if (!outputNames_.empty()) {
110  int probsIdx = outputNames_.empty() ? 0 : 1;
111  std::vector<float> vec = outputTensors[probsIdx];
112  float* probs = vec.data();
113  for (int i = 0; i < batchSize_; i++) {
114  tracksters[tracksterIndices_[i]].setProbabilities(probs); // Update probabilities
115  probs += tracksters[tracksterIndices_[i]].id_probabilities().size(); // Move to next set of probabilities
116  }
117  }
118  }
119  }
const std::vector< std::string > outputNames_
const cms::Ort::ONNXRuntime * onnxSession_
std::vector< std::vector< int64_t > > input_shapes_
std::vector< std::vector< float > > input_Data_
const std::vector< std::string > inputNames_
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::TracksterInferenceByCNNv4::batchSize_
private

Definition at line 34 of file TracksterInferenceByCNNv4.h.

Referenced by inputData(), and runInference().

◆ doPID_

int ticl::TracksterInferenceByCNNv4::doPID_
private

Definition at line 27 of file TracksterInferenceByCNNv4.h.

Referenced by runInference().

◆ doRegression_

int ticl::TracksterInferenceByCNNv4::doRegression_
private

Definition at line 28 of file TracksterInferenceByCNNv4.h.

Referenced by runInference().

◆ eidMinClusterEnergy_

const float ticl::TracksterInferenceByCNNv4::eidMinClusterEnergy_
private

Definition at line 23 of file TracksterInferenceByCNNv4.h.

Referenced by inputData().

◆ eidNClusters_

const int ticl::TracksterInferenceByCNNv4::eidNClusters_
private

Definition at line 25 of file TracksterInferenceByCNNv4.h.

Referenced by inputData().

◆ eidNFeatures_

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

Definition at line 26 of file TracksterInferenceByCNNv4.h.

Referenced by inputData().

◆ eidNLayers_

const int ticl::TracksterInferenceByCNNv4::eidNLayers_
private

Definition at line 24 of file TracksterInferenceByCNNv4.h.

Referenced by inputData().

◆ input_Data_

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

Definition at line 33 of file TracksterInferenceByCNNv4.h.

Referenced by inputData(), and runInference().

◆ input_shapes_

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

Definition at line 31 of file TracksterInferenceByCNNv4.h.

Referenced by inputData(), and runInference().

◆ inputNames_

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

Definition at line 21 of file TracksterInferenceByCNNv4.h.

Referenced by runInference().

◆ modelPath_

const std::string ticl::TracksterInferenceByCNNv4::modelPath_
private

Definition at line 20 of file TracksterInferenceByCNNv4.h.

Referenced by TracksterInferenceByCNNv4().

◆ onnxSession_

const cms::Ort::ONNXRuntime* ticl::TracksterInferenceByCNNv4::onnxSession_
private

Definition at line 18 of file TracksterInferenceByCNNv4.h.

Referenced by runInference(), and TracksterInferenceByCNNv4().

◆ outputNames_

const std::vector<std::string> ticl::TracksterInferenceByCNNv4::outputNames_
private

Definition at line 22 of file TracksterInferenceByCNNv4.h.

Referenced by runInference().

◆ rhtools_

hgcal::RecHitTools ticl::TracksterInferenceByCNNv4::rhtools_
private

Definition at line 30 of file TracksterInferenceByCNNv4.h.

Referenced by inputData().

◆ tracksterIndices_

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

Definition at line 32 of file TracksterInferenceByCNNv4.h.

Referenced by inputData(), and runInference().