CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
ticl::PatternRecognitionbyFastJet< TILES > Class Template Referencefinal

#include <PatternRecognitionbyFastJet.h>

Inheritance diagram for ticl::PatternRecognitionbyFastJet< TILES >:
ticl::PatternRecognitionAlgoBaseT< TILES >

Public Member Functions

void energyRegressionAndID (const std::vector< reco::CaloCluster > &layerClusters, const tensorflow::Session *, std::vector< Trackster > &result)
 
void makeTracksters (const typename PatternRecognitionAlgoBaseT< TILES >::Inputs &input, std::vector< Trackster > &result, std::unordered_map< int, std::vector< int >> &seedToTracksterAssociation) override
 
 PatternRecognitionbyFastJet (const edm::ParameterSet &conf, edm::ConsumesCollector)
 
 ~PatternRecognitionbyFastJet () override=default
 
- Public Member Functions inherited from ticl::PatternRecognitionAlgoBaseT< TILES >
virtual void makeTracksters (const Inputs &input, std::vector< Trackster > &result, std::unordered_map< int, std::vector< int >> &seedToTracksterAssociation)=0
 
 PatternRecognitionAlgoBaseT (const edm::ParameterSet &conf, edm::ConsumesCollector)
 
virtual ~PatternRecognitionAlgoBaseT ()
 

Static Public Member Functions

static void fillPSetDescription (edm::ParameterSetDescription &iDesc)
 

Private Member Functions

void buildJetAndTracksters (std::vector< fastjet::PseudoJet > &, std::vector< ticl::Trackster > &)
 

Private Attributes

const double antikt_radius_
 
edm::ESGetToken< CaloGeometry, CaloGeometryRecordcaloGeomToken_
 
const bool computeLocalTime_
 
const std::string eidInputName_
 
const float eidMinClusterEnergy_
 
const int eidNClusters_
 
const int eidNLayers_
 
const std::string eidOutputNameEnergy_
 
const std::string eidOutputNameId_
 
tensorflow::Session * eidSession_
 
const int minNumLayerCluster_
 
hgcal::RecHitTools rhtools_
 

Static Private Attributes

static const int eidNFeatures_ = 3
 

Additional Inherited Members

- Protected Attributes inherited from ticl::PatternRecognitionAlgoBaseT< TILES >
int algo_verbosity_
 

Detailed Description

template<typename TILES>
class ticl::PatternRecognitionbyFastJet< TILES >

Definition at line 18 of file PatternRecognitionbyFastJet.h.

Constructor & Destructor Documentation

◆ PatternRecognitionbyFastJet()

template<typename TILES >
PatternRecognitionbyFastJet::PatternRecognitionbyFastJet ( const edm::ParameterSet conf,
edm::ConsumesCollector  iC 
)

Definition at line 29 of file PatternRecognitionbyFastJet.cc.

33  antikt_radius_(conf.getParameter<double>("antikt_radius")),
34  minNumLayerCluster_(conf.getParameter<int>("minNumLayerCluster")),
35  eidInputName_(conf.getParameter<std::string>("eid_input_name")),
36  eidOutputNameEnergy_(conf.getParameter<std::string>("eid_output_name_energy")),
37  eidOutputNameId_(conf.getParameter<std::string>("eid_output_name_id")),
38  eidMinClusterEnergy_(conf.getParameter<double>("eid_min_cluster_energy")),
39  eidNLayers_(conf.getParameter<int>("eid_n_layers")),
40  eidNClusters_(conf.getParameter<int>("eid_n_clusters")),
41  computeLocalTime_(conf.getParameter<bool>("computeLocalTime")){};
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > caloGeomToken_

◆ ~PatternRecognitionbyFastJet()

template<typename TILES >
ticl::PatternRecognitionbyFastJet< TILES >::~PatternRecognitionbyFastJet ( )
overridedefault

Member Function Documentation

◆ buildJetAndTracksters()

template<typename TILES >
void PatternRecognitionbyFastJet::buildJetAndTracksters ( std::vector< fastjet::PseudoJet > &  ,
std::vector< ticl::Trackster > &   
)
private

Definition at line 44 of file PatternRecognitionbyFastJet.cc.

References ticl::Advanced, ticl::Basic, metsig::jet, PDWG_EXODelayedJetMET_cff::jets, and mps_fire::result.

45  {
47  edm::LogVerbatim("PatternRecogntionbyFastJet")
48  << "Creating FastJet with " << fjInputs.size() << " LayerClusters in input";
49  }
50  fastjet::ClusterSequence sequence(fjInputs, JetDefinition(antikt_algorithm, antikt_radius_));
51  auto jets = fastjet::sorted_by_pt(sequence.inclusive_jets(0));
53  edm::LogVerbatim("PatternRecogntionbyFastJet") << "FastJet produced " << jets.size() << " jets/trackster";
54  }
55 
56  auto trackster_idx = result.size();
57  auto jetsSize = std::count_if(jets.begin(), jets.end(), [this](fastjet::PseudoJet jet) {
58  return jet.constituents().size() > static_cast<unsigned int>(minNumLayerCluster_);
59  });
60  result.resize(trackster_idx + jetsSize);
61 
62  for (const auto &pj : jets) {
63  if (pj.constituents().size() > static_cast<unsigned int>(minNumLayerCluster_)) {
64  for (const auto &component : pj.constituents()) {
65  result[trackster_idx].vertices().push_back(component.user_index());
66  result[trackster_idx].vertex_multiplicity().push_back(1);
68  edm::LogVerbatim("PatternRecogntionbyFastJet")
69  << "Jet has " << pj.constituents().size() << " components that are stored in trackster " << trackster_idx;
70  }
71  }
72  trackster_idx++;
73  } else {
75  edm::LogVerbatim("PatternRecogntionbyFastJet")
76  << "Jet with " << pj.constituents().size() << " constituents discarded since too small wrt "
78  }
79  }
80  }
81  fjInputs.clear();
82 }
Log< level::Info, true > LogVerbatim

◆ energyRegressionAndID()

template<typename TILES >
void PatternRecognitionbyFastJet::energyRegressionAndID ( const std::vector< reco::CaloCluster > &  layerClusters,
const tensorflow::Session *  eidSession,
std::vector< Trackster > &  result 
)

Definition at line 164 of file PatternRecognitionbyFastJet.cc.

References a, funct::abs(), b, HLT_FULL_cff::batchSize, data, hcalRecHitTable_cff::energy, reco::CaloCluster::energy(), reco::CaloCluster::eta(), f, lowptgsfeleseed::features(), nano_mu_digi_cff::float, reco::CaloCluster::hitsAndFractions(), mps_fire::i, input, crabTemplate::inputList, createfilelist::int, dqmiolumiharvest::j, dqmdumpme::k, MainPageGenerator::l, hltEgammaHGCALIDVarsL1Seeded_cfi::layerClusters, jetsAK4_CHS_cff::outputNames, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, reco::CaloCluster::phi(), tensorflow::run(), l1trig_cff::shape, jetUpdater_cfi::sort, bphysicsOniaDQM_cfi::vertex, ticl::Trackster::vertex_multiplicity(), AlignmentTracksFromVertexSelector_cfi::vertices, and ticl::Trackster::vertices().

166  {
167  // Energy regression and particle identification strategy:
168  //
169  // 1. Set default values for regressed energy and particle id for each trackster.
170  // 2. Store indices of tracksters whose total sum of cluster energies is above the
171  // eidMinClusterEnergy_ (GeV) threshold. Inference is not applied for soft tracksters.
172  // 3. When no trackster passes the selection, return.
173  // 4. Create input and output tensors. The batch dimension is determined by the number of
174  // selected tracksters.
175  // 5. Fill input tensors with layer cluster features. Per layer, clusters are ordered descending
176  // by energy. Given that tensor data is contiguous in memory, we can use pointer arithmetic to
177  // fill values, even with batching.
178  // 6. Zero-fill features for empty clusters in each layer.
179  // 7. Batched inference.
180  // 8. Assign the regressed energy and id probabilities to each trackster.
181  //
182  // Indices used throughout this method:
183  // i -> batch element / trackster
184  // j -> layer
185  // k -> cluster
186  // l -> feature
187 
188  // set default values per trackster, determine if the cluster energy threshold is passed,
189  // and store indices of hard tracksters
190  std::vector<int> tracksterIndices;
191  for (int i = 0; i < static_cast<int>(tracksters.size()); i++) {
192  // calculate the cluster energy sum (2)
193  // note: after the loop, sumClusterEnergy might be just above the threshold which is enough to
194  // decide whether to run inference for the trackster or not
195  float sumClusterEnergy = 0.;
196  for (const unsigned int &vertex : tracksters[i].vertices()) {
197  sumClusterEnergy += static_cast<float>(layerClusters[vertex].energy());
198  // there might be many clusters, so try to stop early
199  if (sumClusterEnergy >= eidMinClusterEnergy_) {
200  // set default values (1)
201  tracksters[i].setRegressedEnergy(0.f);
202  tracksters[i].zeroProbabilities();
203  tracksterIndices.push_back(i);
204  break;
205  }
206  }
207  }
208 
209  // do nothing when no trackster passes the selection (3)
210  int batchSize = static_cast<int>(tracksterIndices.size());
211  if (batchSize == 0) {
212  return;
213  }
214 
215  // create input and output tensors (4)
216  tensorflow::TensorShape shape({batchSize, eidNLayers_, eidNClusters_, eidNFeatures_});
217  tensorflow::Tensor input(tensorflow::DT_FLOAT, shape);
219 
220  std::vector<tensorflow::Tensor> outputs;
221  std::vector<std::string> outputNames;
222  if (!eidOutputNameEnergy_.empty()) {
224  }
225  if (!eidOutputNameId_.empty()) {
226  outputNames.push_back(eidOutputNameId_);
227  }
228 
229  // fill input tensor (5)
230  for (int i = 0; i < batchSize; i++) {
231  const Trackster &trackster = tracksters[tracksterIndices[i]];
232 
233  // per layer, we only consider the first eidNClusters_ clusters in terms of energy, so in order
234  // to avoid creating large / nested structures to do the sorting for an unknown number of total
235  // clusters, create a sorted list of layer cluster indices to keep track of the filled clusters
236  std::vector<int> clusterIndices(trackster.vertices().size());
237  for (int k = 0; k < (int)trackster.vertices().size(); k++) {
238  clusterIndices[k] = k;
239  }
240  sort(clusterIndices.begin(), clusterIndices.end(), [&layerClusters, &trackster](const int &a, const int &b) {
241  return layerClusters[trackster.vertices(a)].energy() > layerClusters[trackster.vertices(b)].energy();
242  });
243 
244  // keep track of the number of seen clusters per layer
245  std::vector<int> seenClusters(eidNLayers_);
246 
247  // loop through clusters by descending energy
248  for (const int &k : clusterIndices) {
249  // get features per layer and cluster and store the values directly in the input tensor
250  const reco::CaloCluster &cluster = layerClusters[trackster.vertices(k)];
251  int j = rhtools_.getLayerWithOffset(cluster.hitsAndFractions()[0].first) - 1;
252  if (j < eidNLayers_ && seenClusters[j] < eidNClusters_) {
253  // get the pointer to the first feature value for the current batch, layer and cluster
254  float *features = &input.tensor<float, 4>()(i, j, seenClusters[j], 0);
255 
256  // fill features
257  *(features++) = float(cluster.energy() / float(trackster.vertex_multiplicity(k)));
258  *(features++) = float(std::abs(cluster.eta()));
259  *(features) = float(cluster.phi());
260 
261  // increment seen clusters
262  seenClusters[j]++;
263  }
264  }
265 
266  // zero-fill features of empty clusters in each layer (6)
267  for (int j = 0; j < eidNLayers_; j++) {
268  for (int k = seenClusters[j]; k < eidNClusters_; k++) {
269  float *features = &input.tensor<float, 4>()(i, j, k, 0);
270  for (int l = 0; l < eidNFeatures_; l++) {
271  *(features++) = 0.f;
272  }
273  }
274  }
275  }
276 
277  // run the inference (7)
279 
280  // store regressed energy per trackster (8)
281  if (!eidOutputNameEnergy_.empty()) {
282  // get the pointer to the energy tensor, dimension is batch x 1
283  float *energy = outputs[0].flat<float>().data();
284 
285  for (const int &i : tracksterIndices) {
286  tracksters[i].setRegressedEnergy(*(energy++));
287  }
288  }
289 
290  // store id probabilities per trackster (8)
291  if (!eidOutputNameId_.empty()) {
292  // get the pointer to the id probability tensor, dimension is batch x id_probabilities.size()
293  int probsIdx = eidOutputNameEnergy_.empty() ? 0 : 1;
294  float *probs = outputs[probsIdx].flat<float>().data();
295 
296  for (const int &i : tracksterIndices) {
297  tracksters[i].setProbabilities(probs);
298  probs += tracksters[i].id_probabilities().size();
299  }
300  }
301 }
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:31
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
static std::string const input
Definition: EdmProvDump.cc:50
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:281
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
string inputList
Definition: crabTemplate.py:6
double energy() const
cluster energy
Definition: CaloCluster.h:149
std::vector< unsigned int > & vertices()
Definition: Trackster.h:57
std::vector< float > & vertex_multiplicity()
Definition: Trackster.h:58
double b
Definition: hdecay.h:120
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
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

◆ fillPSetDescription()

template<typename TILES >
void PatternRecognitionbyFastJet::fillPSetDescription ( edm::ParameterSetDescription iDesc)
static

Definition at line 304 of file PatternRecognitionbyFastJet.cc.

References edm::ParameterSetDescription::add(), edm::ParameterDescriptionNode::setComment(), and AlCaHLTBitMon_QueryRunRegistry::string.

304  {
305  iDesc.add<int>("algo_verbosity", 0);
306  iDesc.add<double>("antikt_radius", 0.09)->setComment("Radius to be used while running the Anti-kt clustering");
307  iDesc.add<int>("minNumLayerCluster", 5)->setComment("Not Inclusive");
308  iDesc.add<std::string>("eid_input_name", "input");
309  iDesc.add<std::string>("eid_output_name_energy", "output/regressed_energy");
310  iDesc.add<std::string>("eid_output_name_id", "output/id_probabilities");
311  iDesc.add<double>("eid_min_cluster_energy", 1.);
312  iDesc.add<int>("eid_n_layers", 50);
313  iDesc.add<int>("eid_n_clusters", 10);
314  iDesc.add<bool>("computeLocalTime", false);
315 }
void setComment(std::string const &value)
ParameterDescriptionBase * add(U const &iLabel, T const &value)

◆ makeTracksters()

template<typename TILES >
void PatternRecognitionbyFastJet::makeTracksters ( const typename PatternRecognitionAlgoBaseT< TILES >::Inputs input,
std::vector< Trackster > &  result,
std::unordered_map< int, std::vector< int >> &  seedToTracksterAssociation 
)
override

Definition at line 85 of file PatternRecognitionbyFastJet.cc.

References ticl::Advanced, ticl::assignPCAtoTracksters(), ticl::Basic, haddnano::cl, ALPAKA_ACCELERATOR_NAMESPACE::brokenline::constexpr(), relativeConstraints::geom, edm::EventSetup::getData(), hcalRecHitTable_cff::ieta, input, hcalRecHitTable_cff::iphi, L1TMuonDQMOffline_cfi::nEtaBins, ecaldqm::binning::nPhiBins, hltrates_dqm_sourceclient-live_cfg::offset, mps_fire::result, submitPVValidationJobs::t, and relativeConstraints::value.

88  {
89  // Protect from events with no seeding regions
90  if (input.regions.empty())
91  return;
92 
93  edm::EventSetup const &es = input.es;
96 
100 
101  // We need to partition the two sides of the HGCAL detector
102  auto lastLayerPerSide = static_cast<unsigned int>(rhtools_.lastLayer(isHFnose)) - 1;
103  unsigned int maxLayer = 2 * lastLayerPerSide - 1;
104  std::vector<fastjet::PseudoJet> fjInputs;
105  fjInputs.clear();
106  for (unsigned int currentLayer = 0; currentLayer <= maxLayer; ++currentLayer) {
107  if (currentLayer == lastLayerPerSide) {
108  buildJetAndTracksters(fjInputs, result);
109  }
110  const auto &tileOnLayer = input.tiles[currentLayer];
111  for (int ieta = 0; ieta <= nEtaBin; ++ieta) {
112  auto offset = ieta * nPhiBin;
114  edm::LogVerbatim("PatternRecogntionbyFastJet") << "offset: " << offset;
115  }
116  for (int iphi = 0; iphi <= nPhiBin; ++iphi) {
118  edm::LogVerbatim("PatternRecogntionbyFastJet") << "iphi: " << iphi;
119  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Entries in tileBin: " << tileOnLayer[offset + iphi].size();
120  }
121  for (auto clusterIdx : tileOnLayer[offset + iphi]) {
122  // Skip masked layer clusters
123  if (input.mask[clusterIdx] == 0.) {
125  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Skipping masked layerIdx " << clusterIdx;
126  }
127  continue;
128  }
129  // Should we correct for the position of the PV?
130  auto const &cl = input.layerClusters[clusterIdx];
131  math::XYZVector direction(cl.x(), cl.y(), cl.z());
132  direction = direction.Unit();
133  direction *= cl.energy();
134  auto fpj = fastjet::PseudoJet(direction.X(), direction.Y(), direction.Z(), cl.energy());
135  fpj.set_user_index(clusterIdx);
136  fjInputs.push_back(fpj);
137  } // End of loop on the clusters on currentLayer
138  } // End of loop over phi-bin region
139  } // End of loop over eta-bin region
140  } // End of loop over layers
141 
142  // Collect the jet from the other side wrt to the one taken care of inside the main loop above.
143  buildJetAndTracksters(fjInputs, result);
144 
146  input.layerClusters,
147  input.layerClustersTime,
148  rhtools_.getPositionLayer(rhtools_.lastLayerEE(isHFnose), isHFnose).z(),
150 
151  // run energy regression and ID
152  energyRegressionAndID(input.layerClusters, input.tfSession, result);
154  for (auto const &t : result) {
155  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Barycenter: " << t.barycenter();
156  edm::LogVerbatim("PatternRecogntionbyFastJet") << "LCs: " << t.vertices().size();
157  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Energy: " << t.raw_energy();
158  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Regressed: " << t.regressed_energy();
159  }
160  }
161 }
Log< level::Info, true > LogVerbatim
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void energyRegressionAndID(const std::vector< reco::CaloCluster > &layerClusters, const tensorflow::Session *, std::vector< Trackster > &result)
static std::string const input
Definition: EdmProvDump.cc:50
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > caloGeomToken_
void buildJetAndTracksters(std::vector< fastjet::PseudoJet > &, std::vector< ticl::Trackster > &)
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
GlobalPoint getPositionLayer(int layer, bool nose=false) const
Definition: RecHitTools.cc:152
void setGeometry(CaloGeometry const &)
Definition: RecHitTools.cc:79
static constexpr int nPhiBins
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool computeLocalTime=false, bool energyWeight=true)
unsigned int lastLayerEE(bool nose=false) const
Definition: RecHitTools.h:76
unsigned int lastLayer(bool nose=false) const
Definition: RecHitTools.h:80

Member Data Documentation

◆ antikt_radius_

template<typename TILES >
const double ticl::PatternRecognitionbyFastJet< TILES >::antikt_radius_
private

Definition at line 35 of file PatternRecognitionbyFastJet.h.

◆ caloGeomToken_

template<typename TILES >
edm::ESGetToken<CaloGeometry, CaloGeometryRecord> ticl::PatternRecognitionbyFastJet< TILES >::caloGeomToken_
private

Definition at line 34 of file PatternRecognitionbyFastJet.h.

◆ computeLocalTime_

template<typename TILES >
const bool ticl::PatternRecognitionbyFastJet< TILES >::computeLocalTime_
private

Definition at line 43 of file PatternRecognitionbyFastJet.h.

◆ eidInputName_

template<typename TILES >
const std::string ticl::PatternRecognitionbyFastJet< TILES >::eidInputName_
private

Definition at line 37 of file PatternRecognitionbyFastJet.h.

◆ eidMinClusterEnergy_

template<typename TILES >
const float ticl::PatternRecognitionbyFastJet< TILES >::eidMinClusterEnergy_
private

Definition at line 40 of file PatternRecognitionbyFastJet.h.

◆ eidNClusters_

template<typename TILES >
const int ticl::PatternRecognitionbyFastJet< TILES >::eidNClusters_
private

Definition at line 42 of file PatternRecognitionbyFastJet.h.

◆ eidNFeatures_

template<typename TILES >
const int ticl::PatternRecognitionbyFastJet< TILES >::eidNFeatures_ = 3
staticprivate

Definition at line 48 of file PatternRecognitionbyFastJet.h.

◆ eidNLayers_

template<typename TILES >
const int ticl::PatternRecognitionbyFastJet< TILES >::eidNLayers_
private

Definition at line 41 of file PatternRecognitionbyFastJet.h.

◆ eidOutputNameEnergy_

template<typename TILES >
const std::string ticl::PatternRecognitionbyFastJet< TILES >::eidOutputNameEnergy_
private

Definition at line 38 of file PatternRecognitionbyFastJet.h.

◆ eidOutputNameId_

template<typename TILES >
const std::string ticl::PatternRecognitionbyFastJet< TILES >::eidOutputNameId_
private

Definition at line 39 of file PatternRecognitionbyFastJet.h.

◆ eidSession_

template<typename TILES >
tensorflow::Session* ticl::PatternRecognitionbyFastJet< TILES >::eidSession_
private

Definition at line 46 of file PatternRecognitionbyFastJet.h.

◆ minNumLayerCluster_

template<typename TILES >
const int ticl::PatternRecognitionbyFastJet< TILES >::minNumLayerCluster_
private

Definition at line 36 of file PatternRecognitionbyFastJet.h.

◆ rhtools_

template<typename TILES >
hgcal::RecHitTools ticl::PatternRecognitionbyFastJet< TILES >::rhtools_
private

Definition at line 45 of file PatternRecognitionbyFastJet.h.