18 template <
typename TILES>
22 oneTracksterPerTrackSeed_(conf.getParameter<
bool>(
"oneTracksterPerTrackSeed")),
23 promoteEmptyRegionToTrackster_(conf.getParameter<
bool>(
"promoteEmptyRegionToTrackster")),
24 out_in_dfs_(conf.getParameter<
bool>(
"out_in_dfs")),
25 max_out_in_hops_(conf.getParameter<
int>(
"max_out_in_hops")),
26 min_cos_theta_(conf.getParameter<double>(
"min_cos_theta")),
27 min_cos_pointing_(conf.getParameter<double>(
"min_cos_pointing")),
28 root_doublet_max_distance_from_seed_squared_(
29 conf.getParameter<double>(
"root_doublet_max_distance_from_seed_squared")),
30 etaLimitIncreaseWindow_(conf.getParameter<double>(
"etaLimitIncreaseWindow")),
31 skip_layers_(conf.getParameter<
int>(
"skip_layers")),
32 max_missing_layers_in_trackster_(conf.getParameter<
int>(
"max_missing_layers_in_trackster")),
33 check_missing_layers_(max_missing_layers_in_trackster_ < 100),
34 shower_start_max_layer_(conf.getParameter<
int>(
"shower_start_max_layer")),
35 min_layers_per_trackster_(conf.getParameter<
int>(
"min_layers_per_trackster")),
36 filter_on_categories_(conf.getParameter<
std::
vector<
int>>(
"filter_on_categories")),
37 pid_threshold_(conf.getParameter<double>(
"pid_threshold")),
38 energy_em_over_total_threshold_(conf.getParameter<double>(
"energy_em_over_total_threshold")),
39 max_longitudinal_sigmaPCA_(conf.getParameter<double>(
"max_longitudinal_sigmaPCA")),
40 min_clusters_per_ntuplet_(min_layers_per_trackster_),
41 max_delta_time_(conf.getParameter<double>(
"max_delta_time")),
42 eidInputName_(conf.getParameter<
std::
string>(
"eid_input_name")),
43 eidOutputNameEnergy_(conf.getParameter<
std::
string>(
"eid_output_name_energy")),
44 eidOutputNameId_(conf.getParameter<
std::
string>(
"eid_output_name_id")),
45 eidMinClusterEnergy_(conf.getParameter<double>(
"eid_min_cluster_energy")),
46 eidNLayers_(conf.getParameter<
int>(
"eid_n_layers")),
47 eidNClusters_(conf.getParameter<
int>(
"eid_n_clusters")),
48 eidSession_(nullptr) {
51 if (trackstersCache ==
nullptr || trackstersCache->
eidGraphDef ==
nullptr) {
53 <<
"PatternRecognitionbyCA received an empty graph definition from the global cache";
58 template <
typename TILES>
61 template <
typename TILES>
64 std::vector<Trackster> &
result,
65 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation) {
67 if (
input.regions.empty())
73 rhtools_.setGeometry(*
geom);
78 LogDebug(
"HGCPatternRecoByCA") <<
"Making Tracksters with CA" << std::endl;
85 bool isRegionalIter = (
input.regions[0].index != -1);
87 std::vector<int> seedIndices;
88 std::vector<uint8_t> layer_cluster_usage(
input.layerClusters.size(), 0);
89 theGraph_->makeAndConnectDoublets(
input.tiles,
95 input.layerClustersTime,
100 root_doublet_max_distance_from_seed_squared_,
101 etaLimitIncreaseWindow_,
103 rhtools_.lastLayer(
type),
106 theGraph_->findNtuplets(
foundNtuplets, seedIndices, min_clusters_per_ntuplet_, out_in_dfs_, max_out_in_hops_);
108 const auto &
doublets = theGraph_->getAllDoublets();
109 int tracksterId = -1;
112 std::vector<Trackster> tmpTracksters;
118 std::set<unsigned int> effective_cluster_idx;
120 for (
auto const &doublet : ntuplet) {
121 auto innerCluster =
doublets[doublet].innerClusterId();
122 auto outerCluster =
doublets[doublet].outerClusterId();
124 effective_cluster_idx.insert(innerCluster);
125 effective_cluster_idx.insert(outerCluster);
128 LogDebug(
"HGCPatternRecoByCA") <<
" New doublet " << doublet <<
" for trackster: " <<
result.size()
129 <<
" InnerCl " << innerCluster <<
" " <<
input.layerClusters[innerCluster].x()
130 <<
" " <<
input.layerClusters[innerCluster].y() <<
" "
131 <<
input.layerClusters[innerCluster].z() <<
" OuterCl " << outerCluster <<
" "
132 <<
input.layerClusters[outerCluster].x() <<
" "
133 <<
input.layerClusters[outerCluster].y() <<
" "
134 <<
input.layerClusters[outerCluster].z() <<
" " << tracksterId << std::endl;
137 unsigned showerMinLayerId = 99999;
138 std::vector<unsigned int> uniqueLayerIds;
139 uniqueLayerIds.reserve(effective_cluster_idx.size());
140 std::vector<std::pair<unsigned int, unsigned int>> lcIdAndLayer;
141 lcIdAndLayer.reserve(effective_cluster_idx.size());
142 for (
auto const i : effective_cluster_idx) {
143 auto const &haf =
input.layerClusters[
i].hitsAndFractions();
144 auto layerId = rhtools_.getLayerWithOffset(haf[0].
first);
145 showerMinLayerId =
std::min(layerId, showerMinLayerId);
146 uniqueLayerIds.push_back(layerId);
147 lcIdAndLayer.emplace_back(
i, layerId);
149 std::sort(uniqueLayerIds.begin(), uniqueLayerIds.end());
150 uniqueLayerIds.erase(
std::unique(uniqueLayerIds.begin(), uniqueLayerIds.end()), uniqueLayerIds.end());
151 unsigned int numberOfLayersInTrackster = uniqueLayerIds.size();
152 if (check_missing_layers_) {
153 int numberOfMissingLayers = 0;
154 unsigned int j = showerMinLayerId;
155 unsigned int indexInVec = 0;
156 for (
const auto &
layer : uniqueLayerIds) {
158 numberOfMissingLayers++;
160 if (numberOfMissingLayers > max_missing_layers_in_trackster_) {
161 numberOfLayersInTrackster = indexInVec;
162 for (
auto &llpair : lcIdAndLayer) {
163 if (llpair.second >=
layer) {
164 effective_cluster_idx.erase(llpair.first);
175 if ((numberOfLayersInTrackster >= min_layers_per_trackster_) and (showerMinLayerId <= shower_start_max_layer_)) {
178 tmp.vertices().reserve(effective_cluster_idx.size());
179 tmp.vertex_multiplicity().resize(effective_cluster_idx.size(), 1);
182 tmp.setSeed(
input.regions[0].collectionID, seedIndices[tracksterId]);
184 std::copy(std::begin(effective_cluster_idx),
std::end(effective_cluster_idx), std::back_inserter(
tmp.vertices()));
185 tmpTracksters.push_back(
tmp);
190 input.layerClustersTime,
191 rhtools_.getPositionLayer(rhtools_.lastLayerEE(
type),
type).z());
194 energyRegressionAndID(
input.layerClusters, tmpTracksters);
201 auto filter_on_pids = [&](
Trackster &
t) ->
bool {
202 auto cumulative_prob = 0.;
203 for (
auto index : filter_on_categories_) {
204 cumulative_prob +=
t.id_probabilities(
index);
206 return (cumulative_prob <= pid_threshold_) &&
207 (
t.raw_em_energy() < energy_em_over_total_threshold_ *
t.raw_energy());
210 std::vector<unsigned int> selectedTrackstersIds;
211 for (
unsigned i = 0;
i < tmpTracksters.size(); ++
i) {
212 if (!filter_on_pids(tmpTracksters[
i]) and tmpTracksters[
i].sigmasPCA()[0] < max_longitudinal_sigmaPCA_) {
213 selectedTrackstersIds.push_back(
i);
217 result.reserve(selectedTrackstersIds.size());
219 for (
unsigned i = 0;
i < selectedTrackstersIds.size(); ++
i) {
220 const auto &
t = tmpTracksters[selectedTrackstersIds[
i]];
221 for (
auto const lcId :
t.vertices()) {
222 layer_cluster_usage[lcId]++;
224 LogDebug(
"HGCPatternRecoByCA") <<
"LayerID: " << lcId <<
" count: " << (
int)layer_cluster_usage[lcId]
227 if (isRegionalIter) {
228 seedToTracksterAssociation[
t.seedIndex()].push_back(
i);
233 for (
auto &trackster :
result) {
234 assert(trackster.vertices().size() <= trackster.vertex_multiplicity().size());
235 for (
size_t i = 0;
i < trackster.vertices().size(); ++
i) {
236 trackster.vertex_multiplicity()[
i] = layer_cluster_usage[trackster.vertices(
i)];
238 LogDebug(
"HGCPatternRecoByCA") <<
"LayerID: " << trackster.vertices(
i)
239 <<
" count: " << (
int)trackster.vertex_multiplicity(
i) << std::endl;
243 if (oneTracksterPerTrackSeed_) {
244 std::vector<Trackster>
tmp;
245 mergeTrackstersTRK(
result,
input.layerClusters,
tmp, seedToTracksterAssociation);
251 input.layerClustersTime,
252 rhtools_.getPositionLayer(rhtools_.lastLayerEE(
type),
type).z());
255 energyRegressionAndID(
input.layerClusters,
result);
259 if (promoteEmptyRegionToTrackster_) {
260 emptyTrackstersFromSeedsTRK(
result, seedToTracksterAssociation,
input.regions[0].collectionID);
264 for (
auto &trackster :
result) {
265 LogDebug(
"HGCPatternRecoByCA") <<
"Trackster characteristics: " << std::endl;
266 LogDebug(
"HGCPatternRecoByCA") <<
"Size: " << trackster.vertices().size() << std::endl;
268 for (
auto const &
p : trackster.id_probabilities()) {
276 template <
typename TILES>
278 const std::vector<Trackster> &
input,
280 std::vector<Trackster> &
output,
281 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation)
const {
283 for (
auto &thisSeed : seedToTracksterAssociation) {
284 auto &tracksters = thisSeed.second;
285 if (!tracksters.empty()) {
286 auto numberOfTrackstersInSeed = tracksters.size();
288 auto &outTrackster =
output.back();
289 tracksters[0] =
output.size() - 1;
290 auto updated_size = outTrackster.vertices().size();
291 for (
unsigned int j = 1;
j < numberOfTrackstersInSeed; ++
j) {
292 auto &thisTrackster =
input[tracksters[
j]];
293 updated_size += thisTrackster.vertices().size();
295 LogDebug(
"HGCPatternRecoByCA") <<
"Updated size: " << updated_size << std::endl;
297 outTrackster.vertices().reserve(updated_size);
298 outTrackster.vertex_multiplicity().reserve(updated_size);
299 std::copy(std::begin(thisTrackster.vertices()),
301 std::back_inserter(outTrackster.vertices()));
302 std::copy(std::begin(thisTrackster.vertex_multiplicity()),
303 std::end(thisTrackster.vertex_multiplicity()),
304 std::back_inserter(outTrackster.vertex_multiplicity()));
306 tracksters.resize(1);
312 template <
typename TILES>
314 std::vector<Trackster> &tracksters,
315 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation,
317 for (
auto &thisSeed : seedToTracksterAssociation) {
318 if (thisSeed.second.empty()) {
320 t.setRegressedEnergy(0.
f);
321 t.zeroProbabilities();
323 t.setSeed(collectionID, thisSeed.first);
324 tracksters.emplace_back(
t);
325 thisSeed.second.emplace_back(tracksters.size() - 1);
330 template <
typename TILES>
332 std::vector<Trackster> &tracksters) {
356 std::vector<int> tracksterIndices;
357 for (
int i = 0;
i < (
int)tracksters.size();
i++) {
361 float sumClusterEnergy = 0.;
365 if (sumClusterEnergy >= eidMinClusterEnergy_) {
367 tracksters[
i].setRegressedEnergy(0.
f);
368 tracksters[
i].zeroProbabilities();
369 tracksterIndices.push_back(
i);
376 int batchSize = (
int)tracksterIndices.size();
377 if (batchSize == 0) {
382 tensorflow::TensorShape shape({batchSize, eidNLayers_, eidNClusters_, eidNFeatures_});
383 tensorflow::Tensor
input(tensorflow::DT_FLOAT, shape);
386 std::vector<tensorflow::Tensor>
outputs;
388 if (!eidOutputNameEnergy_.empty()) {
391 if (!eidOutputNameId_.empty()) {
396 for (
int i = 0;
i < batchSize;
i++) {
397 const Trackster &trackster = tracksters[tracksterIndices[
i]];
402 std::vector<int> clusterIndices(trackster.
vertices().size());
404 clusterIndices[
k] =
k;
406 sort(clusterIndices.begin(), clusterIndices.end(), [&
layerClusters, &trackster](
const int &
a,
const int &
b) {
411 std::vector<int> seenClusters(eidNLayers_);
414 for (
const int &
k : clusterIndices) {
418 if (
j < eidNLayers_ && seenClusters[
j] < eidNClusters_) {
433 for (
int j = 0;
j < eidNLayers_;
j++) {
434 for (
int k = seenClusters[
j];
k < eidNClusters_;
k++) {
436 for (
int l = 0;
l < eidNFeatures_;
l++) {
447 if (!eidOutputNameEnergy_.empty()) {
451 for (
const int &
i : tracksterIndices) {
452 tracksters[
i].setRegressedEnergy(*(
energy++));
457 if (!eidOutputNameId_.empty()) {
459 int probsIdx = eidOutputNameEnergy_.empty() ? 0 : 1;
460 float *probs =
outputs[probsIdx].flat<
float>().
data();
462 for (
const int &
i : tracksterIndices) {
463 tracksters[
i].setProbabilities(probs);
464 probs += tracksters[
i].id_probabilities().size();