19 template <
typename TILES>
23 oneTracksterPerTrackSeed_(conf.getParameter<
bool>(
"oneTracksterPerTrackSeed")),
24 promoteEmptyRegionToTrackster_(conf.getParameter<
bool>(
"promoteEmptyRegionToTrackster")),
25 out_in_dfs_(conf.getParameter<
bool>(
"out_in_dfs")),
26 max_out_in_hops_(conf.getParameter<
int>(
"max_out_in_hops")),
27 min_cos_theta_(conf.getParameter<double>(
"min_cos_theta")),
28 min_cos_pointing_(conf.getParameter<double>(
"min_cos_pointing")),
29 root_doublet_max_distance_from_seed_squared_(
30 conf.getParameter<double>(
"root_doublet_max_distance_from_seed_squared")),
31 etaLimitIncreaseWindow_(conf.getParameter<double>(
"etaLimitIncreaseWindow")),
32 skip_layers_(conf.getParameter<
int>(
"skip_layers")),
33 max_missing_layers_in_trackster_(conf.getParameter<
int>(
"max_missing_layers_in_trackster")),
34 check_missing_layers_(max_missing_layers_in_trackster_ < 100),
35 shower_start_max_layer_(conf.getParameter<
int>(
"shower_start_max_layer")),
36 min_layers_per_trackster_(conf.getParameter<
int>(
"min_layers_per_trackster")),
37 filter_on_categories_(conf.getParameter<
std::vector<
int>>(
"filter_on_categories")),
38 pid_threshold_(conf.getParameter<double>(
"pid_threshold")),
39 energy_em_over_total_threshold_(conf.getParameter<double>(
"energy_em_over_total_threshold")),
40 max_longitudinal_sigmaPCA_(conf.getParameter<double>(
"max_longitudinal_sigmaPCA")),
41 min_clusters_per_ntuplet_(min_layers_per_trackster_),
42 max_delta_time_(conf.getParameter<double>(
"max_delta_time")),
43 eidInputName_(conf.getParameter<
std::
string>(
"eid_input_name")),
44 eidOutputNameEnergy_(conf.getParameter<
std::
string>(
"eid_output_name_energy")),
45 eidOutputNameId_(conf.getParameter<
std::
string>(
"eid_output_name_id")),
46 eidMinClusterEnergy_(conf.getParameter<double>(
"eid_min_cluster_energy")),
47 eidNLayers_(conf.getParameter<
int>(
"eid_n_layers")),
48 eidNClusters_(conf.getParameter<
int>(
"eid_n_clusters")),
49 eidSession_(nullptr) {
52 if (trackstersCache ==
nullptr || trackstersCache->
eidGraphDef ==
nullptr) {
54 <<
"PatternRecognitionbyCA received an empty graph definition from the global cache";
59 template <
typename TILES>
62 template <
typename TILES>
65 std::vector<Trackster> &
result,
66 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation) {
68 if (
input.regions.empty())
74 rhtools_.setGeometry(*
geom);
79 LogDebug(
"HGCPatternRecoByCA") <<
"Making Tracksters with CA" << std::endl;
86 bool isRegionalIter = (
input.regions[0].index != -1);
87 std::vector<HGCDoublet::HGCntuplet> foundNtuplets;
88 std::vector<int> seedIndices;
89 std::vector<uint8_t> layer_cluster_usage(
input.layerClusters.size(), 0);
90 theGraph_->makeAndConnectDoublets(
input.tiles,
96 input.layerClustersTime,
101 root_doublet_max_distance_from_seed_squared_,
102 etaLimitIncreaseWindow_,
104 rhtools_.lastLayer(
type),
107 theGraph_->findNtuplets(foundNtuplets, seedIndices, min_clusters_per_ntuplet_, out_in_dfs_, max_out_in_hops_);
109 const auto &
doublets = theGraph_->getAllDoublets();
110 int tracksterId = -1;
113 std::vector<Trackster> tmpTracksters;
114 tmpTracksters.reserve(foundNtuplets.size());
116 for (
auto const &ntuplet : foundNtuplets) {
119 std::set<unsigned int> effective_cluster_idx;
120 std::pair<std::set<unsigned int>::iterator,
bool> retVal;
122 std::vector<float> times;
123 std::vector<float> timeErrors;
125 for (
auto const &doublet : ntuplet) {
126 auto innerCluster =
doublets[doublet].innerClusterId();
127 auto outerCluster =
doublets[doublet].outerClusterId();
129 retVal = effective_cluster_idx.insert(innerCluster);
131 float time =
input.layerClustersTime.get(innerCluster).first;
133 times.push_back(
time);
134 timeErrors.push_back(1. /
pow(
input.layerClustersTime.get(innerCluster).second, 2));
138 retVal = effective_cluster_idx.insert(outerCluster);
140 float time =
input.layerClustersTime.get(outerCluster).first;
142 times.push_back(
time);
143 timeErrors.push_back(1. /
pow(
input.layerClustersTime.get(outerCluster).second, 2));
148 LogDebug(
"HGCPatternRecoByCA") <<
" New doublet " << doublet <<
" for trackster: " <<
result.size()
149 <<
" InnerCl " << innerCluster <<
" " <<
input.layerClusters[innerCluster].x()
150 <<
" " <<
input.layerClusters[innerCluster].y() <<
" "
151 <<
input.layerClusters[innerCluster].z() <<
" OuterCl " << outerCluster <<
" "
152 <<
input.layerClusters[outerCluster].x() <<
" "
153 <<
input.layerClusters[outerCluster].y() <<
" "
154 <<
input.layerClusters[outerCluster].z() <<
" " << tracksterId << std::endl;
157 unsigned showerMinLayerId = 99999;
158 std::vector<unsigned int> uniqueLayerIds;
159 uniqueLayerIds.reserve(effective_cluster_idx.size());
160 std::vector<std::pair<unsigned int, unsigned int>> lcIdAndLayer;
161 lcIdAndLayer.reserve(effective_cluster_idx.size());
162 for (
auto const i : effective_cluster_idx) {
163 auto const &haf =
input.layerClusters[
i].hitsAndFractions();
164 auto layerId = rhtools_.getLayerWithOffset(haf[0].
first);
165 showerMinLayerId =
std::min(layerId, showerMinLayerId);
166 uniqueLayerIds.push_back(layerId);
167 lcIdAndLayer.emplace_back(
i, layerId);
169 std::sort(uniqueLayerIds.begin(), uniqueLayerIds.end());
170 uniqueLayerIds.erase(
std::unique(uniqueLayerIds.begin(), uniqueLayerIds.end()), uniqueLayerIds.end());
171 unsigned int numberOfLayersInTrackster = uniqueLayerIds.size();
172 if (check_missing_layers_) {
173 int numberOfMissingLayers = 0;
174 unsigned int j = showerMinLayerId;
175 unsigned int indexInVec = 0;
176 for (
const auto &layer : uniqueLayerIds) {
178 numberOfMissingLayers++;
180 if (numberOfMissingLayers > max_missing_layers_in_trackster_) {
181 numberOfLayersInTrackster = indexInVec;
182 for (
auto &llpair : lcIdAndLayer) {
183 if (llpair.second >= layer) {
184 effective_cluster_idx.erase(llpair.first);
195 if ((numberOfLayersInTrackster >= min_layers_per_trackster_) and (showerMinLayerId <= shower_start_max_layer_)) {
198 tmp.vertices().reserve(effective_cluster_idx.size());
199 tmp.vertex_multiplicity().resize(effective_cluster_idx.size(), 1);
202 tmp.setSeed(
input.regions[0].collectionID, seedIndices[tracksterId]);
204 std::pair<float, float> timeTrackster(-99., -1.);
207 tmp.setTimeAndError(timeTrackster.first, timeTrackster.second);
209 tmpTracksters.push_back(
tmp);
213 tmpTracksters,
input.layerClusters, rhtools_.getPositionLayer(rhtools_.lastLayerEE(
type)).z());
216 energyRegressionAndID(
input.layerClusters, tmpTracksters);
223 auto filter_on_pids = [&](
Trackster &
t) ->
bool {
224 auto cumulative_prob = 0.;
225 for (
auto index : filter_on_categories_) {
226 cumulative_prob +=
t.id_probabilities(
index);
228 return (cumulative_prob <= pid_threshold_) &&
229 (
t.raw_em_energy() < energy_em_over_total_threshold_ *
t.raw_energy());
232 std::vector<unsigned int> selectedTrackstersIds;
233 for (
unsigned i = 0;
i < tmpTracksters.size(); ++
i) {
234 if (!filter_on_pids(tmpTracksters[
i]) and tmpTracksters[
i].sigmasPCA()[0] < max_longitudinal_sigmaPCA_) {
235 selectedTrackstersIds.push_back(
i);
239 result.reserve(selectedTrackstersIds.size());
241 for (
unsigned i = 0;
i < selectedTrackstersIds.size(); ++
i) {
242 const auto &
t = tmpTracksters[selectedTrackstersIds[
i]];
243 for (
auto const lcId :
t.vertices()) {
244 layer_cluster_usage[lcId]++;
246 LogDebug(
"HGCPatternRecoByCA") <<
"LayerID: " << lcId <<
" count: " << (
int)layer_cluster_usage[lcId]
249 if (isRegionalIter) {
250 seedToTracksterAssociation[
t.seedIndex()].push_back(
i);
255 for (
auto &trackster :
result) {
256 assert(trackster.vertices().size() <= trackster.vertex_multiplicity().size());
257 for (
size_t i = 0;
i < trackster.vertices().size(); ++
i) {
258 trackster.vertex_multiplicity()[
i] = layer_cluster_usage[trackster.vertices(
i)];
260 LogDebug(
"HGCPatternRecoByCA") <<
"LayerID: " << trackster.vertices(
i)
261 <<
" count: " << (
int)trackster.vertex_multiplicity(
i) << std::endl;
265 if (oneTracksterPerTrackSeed_) {
266 std::vector<Trackster>
tmp;
267 mergeTrackstersTRK(
result,
input.layerClusters,
tmp, seedToTracksterAssociation);
273 energyRegressionAndID(
input.layerClusters,
result);
277 if (promoteEmptyRegionToTrackster_) {
278 emptyTrackstersFromSeedsTRK(
result, seedToTracksterAssociation,
input.regions[0].collectionID);
282 for (
auto &trackster :
result) {
283 LogDebug(
"HGCPatternRecoByCA") <<
"Trackster characteristics: " << std::endl;
284 LogDebug(
"HGCPatternRecoByCA") <<
"Size: " << trackster.vertices().size() << std::endl;
286 for (
auto const &
p : trackster.id_probabilities()) {
294 template <
typename TILES>
296 const std::vector<Trackster> &
input,
298 std::vector<Trackster> &
output,
299 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation)
const {
301 for (
auto &thisSeed : seedToTracksterAssociation) {
302 auto &tracksters = thisSeed.second;
303 if (!tracksters.empty()) {
304 auto numberOfTrackstersInSeed = tracksters.size();
306 auto &outTrackster =
output.back();
307 tracksters[0] =
output.size() - 1;
308 auto updated_size = outTrackster.vertices().size();
309 for (
unsigned int j = 1;
j < numberOfTrackstersInSeed; ++
j) {
310 auto &thisTrackster =
input[tracksters[
j]];
311 updated_size += thisTrackster.vertices().size();
313 LogDebug(
"HGCPatternRecoByCA") <<
"Updated size: " << updated_size << std::endl;
315 outTrackster.vertices().reserve(updated_size);
316 outTrackster.vertex_multiplicity().reserve(updated_size);
319 std::back_inserter(outTrackster.vertices()));
321 std::end(thisTrackster.vertex_multiplicity()),
322 std::back_inserter(outTrackster.vertex_multiplicity()));
324 tracksters.resize(1);
330 template <
typename TILES>
332 std::vector<Trackster> &tracksters,
333 std::unordered_map<
int, std::vector<int>> &seedToTracksterAssociation,
335 for (
auto &thisSeed : seedToTracksterAssociation) {
336 if (thisSeed.second.empty()) {
338 t.setRegressedEnergy(0.
f);
339 t.zeroProbabilities();
341 t.setSeed(collectionID, thisSeed.first);
342 tracksters.emplace_back(
t);
343 thisSeed.second.emplace_back(tracksters.size() - 1);
348 template <
typename TILES>
350 std::vector<Trackster> &tracksters) {
374 std::vector<int> tracksterIndices;
375 for (
int i = 0;
i < (
int)tracksters.size();
i++) {
379 float sumClusterEnergy = 0.;
383 if (sumClusterEnergy >= eidMinClusterEnergy_) {
385 tracksters[
i].setRegressedEnergy(0.
f);
386 tracksters[
i].zeroProbabilities();
387 tracksterIndices.push_back(
i);
394 int batchSize = (
int)tracksterIndices.size();
395 if (batchSize == 0) {
400 tensorflow::TensorShape shape({batchSize, eidNLayers_, eidNClusters_, eidNFeatures_});
401 tensorflow::Tensor
input(tensorflow::DT_FLOAT, shape);
404 std::vector<tensorflow::Tensor>
outputs;
406 if (!eidOutputNameEnergy_.empty()) {
409 if (!eidOutputNameId_.empty()) {
414 for (
int i = 0;
i < batchSize;
i++) {
415 const Trackster &trackster = tracksters[tracksterIndices[
i]];
420 std::vector<int> clusterIndices(trackster.
vertices().size());
422 clusterIndices[
k] =
k;
424 sort(clusterIndices.begin(), clusterIndices.end(), [&
layerClusters, &trackster](
const int &
a,
const int &
b) {
429 std::vector<int> seenClusters(eidNLayers_);
432 for (
const int &
k : clusterIndices) {
436 if (
j < eidNLayers_ && seenClusters[
j] < eidNClusters_) {
451 for (
int j = 0;
j < eidNLayers_;
j++) {
452 for (
int k = seenClusters[
j];
k < eidNClusters_;
k++) {
454 for (
int l = 0;
l < eidNFeatures_;
l++) {
465 if (!eidOutputNameEnergy_.empty()) {
469 for (
const int &
i : tracksterIndices) {
470 tracksters[
i].setRegressedEnergy(*(
energy++));
475 if (!eidOutputNameId_.empty()) {
477 int probsIdx = eidOutputNameEnergy_.empty() ? 0 : 1;
478 float *probs =
outputs[probsIdx].flat<
float>().
data();
480 for (
const int &
i : tracksterIndices) {
481 tracksters[
i].setProbabilities(probs);
482 probs += tracksters[
i].id_probabilities().size();