CMS 3D CMS Logo

PatternRecognitionbyFastJet.cc
Go to the documentation of this file.
1 // Author: Marco Rovere - marco.rovere@cern.ch
2 // Date: 10/2021
3 #include <algorithm>
4 #include <set>
5 #include <vector>
6 
7 #include "tbb/task_arena.h"
8 #include "tbb/tbb.h"
9 
17 
18 #include "TrackstersPCA.h"
22 
23 #include "fastjet/ClusterSequence.hh"
24 
25 using namespace ticl;
26 using namespace fastjet;
27 
28 template <typename TILES>
31  : PatternRecognitionAlgoBaseT<TILES>(conf, iC),
32  caloGeomToken_(iC.esConsumes<CaloGeometry, CaloGeometryRecord>()),
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 
42 template <typename TILES>
43 void PatternRecognitionbyFastJet<TILES>::buildJetAndTracksters(std::vector<PseudoJet> &fjInputs,
44  std::vector<ticl::Trackster> &result) {
46  edm::LogVerbatim("PatternRecogntionbyFastJet")
47  << "Creating FastJet with " << fjInputs.size() << " LayerClusters in input";
48  }
49  fastjet::ClusterSequence sequence(fjInputs, JetDefinition(antikt_algorithm, antikt_radius_));
50  auto jets = fastjet::sorted_by_pt(sequence.inclusive_jets(0));
52  edm::LogVerbatim("PatternRecogntionbyFastJet") << "FastJet produced " << jets.size() << " jets/trackster";
53  }
54 
55  auto trackster_idx = result.size();
56  result.resize(trackster_idx + jets.size());
57  for (const auto &pj : jets) {
58  if (pj.constituents().size() > static_cast<unsigned int>(minNumLayerCluster_)) {
59  for (const auto &component : pj.constituents()) {
60  result[trackster_idx].vertices().push_back(component.user_index());
61  result[trackster_idx].vertex_multiplicity().push_back(1);
63  edm::LogVerbatim("PatternRecogntionbyFastJet")
64  << "Jet has " << pj.constituents().size() << " components that are stored in trackster " << trackster_idx;
65  }
66  }
67  trackster_idx++;
68  } else {
70  edm::LogVerbatim("PatternRecogntionbyFastJet")
71  << "Jet with " << pj.constituents().size() << " constituents discarded since too small wrt "
72  << minNumLayerCluster_;
73  }
74  }
75  }
76  fjInputs.clear();
77 }
78 
79 template <typename TILES>
82  std::vector<Trackster> &result,
83  std::unordered_map<int, std::vector<int>> &seedToTracksterAssociation) {
84  // Protect from events with no seeding regions
85  if (input.regions.empty())
86  return;
87 
88  edm::EventSetup const &es = input.es;
89  const CaloGeometry &geom = es.getData(caloGeomToken_);
90  rhtools_.setGeometry(geom);
91 
92  constexpr auto isHFnose = std::is_same<TILES, TICLLayerTilesHFNose>::value;
93  constexpr int nEtaBin = TILES::constants_type_t::nEtaBins;
94  constexpr int nPhiBin = TILES::constants_type_t::nPhiBins;
95 
96  // We need to partition the two sides of the HGCAL detector
97  auto lastLayerPerSide = static_cast<unsigned int>(rhtools_.lastLayer(isHFnose)) - 1;
98  unsigned int maxLayer = 2 * lastLayerPerSide - 1;
99  std::vector<fastjet::PseudoJet> fjInputs;
100  fjInputs.clear();
101  for (unsigned int currentLayer = 0; currentLayer <= maxLayer; ++currentLayer) {
102  if (currentLayer == lastLayerPerSide) {
103  buildJetAndTracksters(fjInputs, result);
104  }
105  const auto &tileOnLayer = input.tiles[currentLayer];
106  for (int ieta = 0; ieta <= nEtaBin; ++ieta) {
107  auto offset = ieta * nPhiBin;
109  edm::LogVerbatim("PatternRecogntionbyFastJet") << "offset: " << offset;
110  }
111  for (int iphi = 0; iphi <= nPhiBin; ++iphi) {
113  edm::LogVerbatim("PatternRecogntionbyFastJet") << "iphi: " << iphi;
114  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Entries in tileBin: " << tileOnLayer[offset + iphi].size();
115  }
116  for (auto clusterIdx : tileOnLayer[offset + iphi]) {
117  // Skip masked layer clusters
118  if (input.mask[clusterIdx] == 0.) {
120  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Skipping masked layerIdx " << clusterIdx;
121  }
122  continue;
123  }
124  // Should we correct for the position of the PV?
125  auto const &cl = input.layerClusters[clusterIdx];
126  math::XYZVector direction(cl.x(), cl.y(), cl.z());
127  direction = direction.Unit();
128  direction *= cl.energy();
129  auto fpj = fastjet::PseudoJet(direction.X(), direction.Y(), direction.Z(), cl.energy());
130  fpj.set_user_index(clusterIdx);
131  fjInputs.push_back(fpj);
132  } // End of loop on the clusters on currentLayer
133  } // End of loop over phi-bin region
134  } // End of loop over eta-bin region
135  } // End of loop over layers
136 
137  // Collect the jet from the other side wrt to the one taken care of inside the main loop above.
138  buildJetAndTracksters(fjInputs, result);
139 
141  input.layerClusters,
142  input.layerClustersTime,
143  rhtools_.getPositionLayer(rhtools_.lastLayerEE(isHFnose), isHFnose).z());
144 
145  // run energy regression and ID
146  energyRegressionAndID(input.layerClusters, input.tfSession, result);
148  for (auto const &t : result) {
149  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Barycenter: " << t.barycenter();
150  edm::LogVerbatim("PatternRecogntionbyFastJet") << "LCs: " << t.vertices().size();
151  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Energy: " << t.raw_energy();
152  edm::LogVerbatim("PatternRecogntionbyFastJet") << "Regressed: " << t.regressed_energy();
153  }
154  }
155 }
156 
157 template <typename TILES>
159  const tensorflow::Session *eidSession,
160  std::vector<Trackster> &tracksters) {
161  // Energy regression and particle identification strategy:
162  //
163  // 1. Set default values for regressed energy and particle id for each trackster.
164  // 2. Store indices of tracksters whose total sum of cluster energies is above the
165  // eidMinClusterEnergy_ (GeV) treshold. Inference is not applied for soft tracksters.
166  // 3. When no trackster passes the selection, return.
167  // 4. Create input and output tensors. The batch dimension is determined by the number of
168  // selected tracksters.
169  // 5. Fill input tensors with layer cluster features. Per layer, clusters are ordered descending
170  // by energy. Given that tensor data is contiguous in memory, we can use pointer arithmetic to
171  // fill values, even with batching.
172  // 6. Zero-fill features for empty clusters in each layer.
173  // 7. Batched inference.
174  // 8. Assign the regressed energy and id probabilities to each trackster.
175  //
176  // Indices used throughout this method:
177  // i -> batch element / trackster
178  // j -> layer
179  // k -> cluster
180  // l -> feature
181 
182  // set default values per trackster, determine if the cluster energy threshold is passed,
183  // and store indices of hard tracksters
184  std::vector<int> tracksterIndices;
185  for (int i = 0; i < static_cast<int>(tracksters.size()); i++) {
186  // calculate the cluster energy sum (2)
187  // note: after the loop, sumClusterEnergy might be just above the threshold which is enough to
188  // decide whether to run inference for the trackster or not
189  float sumClusterEnergy = 0.;
190  for (const unsigned int &vertex : tracksters[i].vertices()) {
191  sumClusterEnergy += static_cast<float>(layerClusters[vertex].energy());
192  // there might be many clusters, so try to stop early
193  if (sumClusterEnergy >= eidMinClusterEnergy_) {
194  // set default values (1)
195  tracksters[i].setRegressedEnergy(0.f);
196  tracksters[i].zeroProbabilities();
197  tracksterIndices.push_back(i);
198  break;
199  }
200  }
201  }
202 
203  // do nothing when no trackster passes the selection (3)
204  int batchSize = static_cast<int>(tracksterIndices.size());
205  if (batchSize == 0) {
206  return;
207  }
208 
209  // create input and output tensors (4)
210  tensorflow::TensorShape shape({batchSize, eidNLayers_, eidNClusters_, eidNFeatures_});
211  tensorflow::Tensor input(tensorflow::DT_FLOAT, shape);
212  tensorflow::NamedTensorList inputList = {{eidInputName_, input}};
213 
214  std::vector<tensorflow::Tensor> outputs;
215  std::vector<std::string> outputNames;
216  if (!eidOutputNameEnergy_.empty()) {
217  outputNames.push_back(eidOutputNameEnergy_);
218  }
219  if (!eidOutputNameId_.empty()) {
220  outputNames.push_back(eidOutputNameId_);
221  }
222 
223  // fill input tensor (5)
224  for (int i = 0; i < batchSize; i++) {
225  const Trackster &trackster = tracksters[tracksterIndices[i]];
226 
227  // per layer, we only consider the first eidNClusters_ clusters in terms of energy, so in order
228  // to avoid creating large / nested structures to do the sorting for an unknown number of total
229  // clusters, create a sorted list of layer cluster indices to keep track of the filled clusters
230  std::vector<int> clusterIndices(trackster.vertices().size());
231  for (int k = 0; k < (int)trackster.vertices().size(); k++) {
232  clusterIndices[k] = k;
233  }
234  sort(clusterIndices.begin(), clusterIndices.end(), [&layerClusters, &trackster](const int &a, const int &b) {
235  return layerClusters[trackster.vertices(a)].energy() > layerClusters[trackster.vertices(b)].energy();
236  });
237 
238  // keep track of the number of seen clusters per layer
239  std::vector<int> seenClusters(eidNLayers_);
240 
241  // loop through clusters by descending energy
242  for (const int &k : clusterIndices) {
243  // get features per layer and cluster and store the values directly in the input tensor
244  const reco::CaloCluster &cluster = layerClusters[trackster.vertices(k)];
245  int j = rhtools_.getLayerWithOffset(cluster.hitsAndFractions()[0].first) - 1;
246  if (j < eidNLayers_ && seenClusters[j] < eidNClusters_) {
247  // get the pointer to the first feature value for the current batch, layer and cluster
248  float *features = &input.tensor<float, 4>()(i, j, seenClusters[j], 0);
249 
250  // fill features
251  *(features++) = float(cluster.energy() / float(trackster.vertex_multiplicity(k)));
252  *(features++) = float(std::abs(cluster.eta()));
253  *(features) = float(cluster.phi());
254 
255  // increment seen clusters
256  seenClusters[j]++;
257  }
258  }
259 
260  // zero-fill features of empty clusters in each layer (6)
261  for (int j = 0; j < eidNLayers_; j++) {
262  for (int k = seenClusters[j]; k < eidNClusters_; k++) {
263  float *features = &input.tensor<float, 4>()(i, j, k, 0);
264  for (int l = 0; l < eidNFeatures_; l++) {
265  *(features++) = 0.f;
266  }
267  }
268  }
269  }
270 
271  // run the inference (7)
272  tensorflow::run(const_cast<tensorflow::Session *>(eidSession), inputList, outputNames, &outputs);
273 
274  // store regressed energy per trackster (8)
275  if (!eidOutputNameEnergy_.empty()) {
276  // get the pointer to the energy tensor, dimension is batch x 1
277  float *energy = outputs[0].flat<float>().data();
278 
279  for (const int &i : tracksterIndices) {
280  tracksters[i].setRegressedEnergy(*(energy++));
281  }
282  }
283 
284  // store id probabilities per trackster (8)
285  if (!eidOutputNameId_.empty()) {
286  // get the pointer to the id probability tensor, dimension is batch x id_probabilities.size()
287  int probsIdx = eidOutputNameEnergy_.empty() ? 0 : 1;
288  float *probs = outputs[probsIdx].flat<float>().data();
289 
290  for (const int &i : tracksterIndices) {
291  tracksters[i].setProbabilities(probs);
292  probs += tracksters[i].id_probabilities().size();
293  }
294  }
295 }
296 
297 template <typename TILES>
299  iDesc.add<int>("algo_verbosity", 0);
300  iDesc.add<double>("antikt_radius", 0.09)->setComment("Radius to be used while running the Anti-kt clustering");
301  iDesc.add<int>("minNumLayerCluster", 5)->setComment("Not Inclusive");
302  iDesc.add<std::string>("eid_input_name", "input");
303  iDesc.add<std::string>("eid_output_name_energy", "output/regressed_energy");
304  iDesc.add<std::string>("eid_output_name_id", "output/id_probabilities");
305  iDesc.add<double>("eid_min_cluster_energy", 1.);
306  iDesc.add<int>("eid_n_layers", 50);
307  iDesc.add<int>("eid_n_clusters", 10);
308 }
309 
Log< level::Info, true > LogVerbatim
void setComment(std::string const &value)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:30
const std::vector< std::pair< DetId, float > > & hitsAndFractions() const
Definition: CaloCluster.h:210
void energyRegressionAndID(const std::vector< reco::CaloCluster > &layerClusters, const tensorflow::Session *, std::vector< Trackster > &result)
double phi() const
azimuthal angle of cluster centroid
Definition: CaloCluster.h:184
static std::string const input
Definition: EdmProvDump.cc:47
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool energyWeight=true)
PatternRecognitionbyFastJet(const edm::ParameterSet &conf, edm::ConsumesCollector)
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
static void fillPSetDescription(edm::ParameterSetDescription &iDesc)
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
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
bool getData(T &iHolder) const
Definition: EventSetup.h:122
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void makeTracksters(const typename PatternRecognitionAlgoBaseT< TILES >::Inputs &input, std::vector< Trackster > &result, std::unordered_map< int, std::vector< int >> &seedToTracksterAssociation) override
double energy() const
cluster energy
Definition: CaloCluster.h:149
void buildJetAndTracksters(std::vector< fastjet::PseudoJet > &, std::vector< ticl::Trackster > &)
std::vector< unsigned int > & vertices()
Definition: Trackster.h:57
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
std::vector< float > & vertex_multiplicity()
Definition: Trackster.h:58
double b
Definition: hdecay.h:118
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
double a
Definition: hdecay.h:119
Definition: Common.h:8
double eta() const
pseudorapidity of cluster centroid
Definition: CaloCluster.h:181