20 const auto& tracksters = *tCH.
product();
22 const auto& simClusters = *sCCH.
product();
23 auto nTracksters = tracksters.size();
27 auto nSimClusters = simClusters.size();
28 std::vector<size_t> sCIndices;
29 for (
unsigned int scId = 0; scId < nSimClusters; ++scId) {
31 simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() != 0)) {
32 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
33 <<
"Excluding SimCluster from event: " << simClusters[scId].g4Tracks()[0].eventId().event()
34 <<
" with BX: " << simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() << std::endl;
37 sCIndices.emplace_back(scId);
39 nSimClusters = sCIndices.size();
45 tssInSimCluster.resize(nSimClusters);
46 for (
unsigned int i = 0;
i < nSimClusters; ++
i) {
47 tssInSimCluster[
i].simClusterId =
i;
48 tssInSimCluster[
i].energy = 0.f;
49 tssInSimCluster[
i].hits_and_fractions.clear();
53 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToSimClusterId_Map;
54 for (
const auto& scId : sCIndices) {
55 const auto& hits_and_fractions = simClusters[scId].hits_and_fractions();
56 for (
const auto& it_haf : hits_and_fractions) {
57 const auto hitid = it_haf.first;
58 const auto itcheck =
hitMap_->find(hitid);
59 if (itcheck !=
hitMap_->end()) {
60 const auto hit_find_it = detIdToSimClusterId_Map.find(hitid);
61 if (hit_find_it == detIdToSimClusterId_Map.end()) {
62 detIdToSimClusterId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
64 detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second);
67 tssInSimCluster[scId].energy += it_haf.second *
hit->energy();
68 tssInSimCluster[scId].hits_and_fractions.emplace_back(hitid, it_haf.second);
74 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
75 <<
"tssInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl;
76 for (
size_t sc = 0; sc < tssInSimCluster.size(); ++sc) {
77 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
78 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
79 <<
" SimClusterIdx: " << tssInSimCluster[sc].simClusterId << std::endl;
80 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
81 <<
" Energy: " << tssInSimCluster[sc].energy << std::endl;
82 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
" # of clusters : " << nLayerClusters << std::endl;
83 double tot_energy = 0.;
84 for (
auto const& haf : tssInSimCluster[sc].hits_and_fractions) {
85 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
86 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
87 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
88 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
90 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
91 for (
auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) {
92 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
93 <<
" tsIdx/energy/score: " << ts.first <<
"/" << ts.second.first <<
"/" << ts.second.second << std::endl;
97 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"detIdToSimClusterId_Map INFO" << std::endl;
98 for (
auto const& detId : detIdToSimClusterId_Map) {
99 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
100 <<
"For detId: " << (uint32_t)detId.first
101 <<
" we have found the following connections with SimClusters:" << std::endl;
102 for (
auto const& sc : detId.second) {
103 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
104 <<
" SimCluster Id: " << sc.clusterId <<
" with fraction: " << sc.fraction
105 <<
" and energy: " << sc.fraction *
hitMap_->at(detId.first)->energy() << std::endl;
111 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
116 scsInTrackster.resize(nTracksters);
118 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
119 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
120 const auto lcId = tracksters[tsId].vertices(
i);
121 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
123 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
124 unsigned int numberOfHitsInLC = hits_and_fractions.size();
126 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
127 const auto rh_detid = hits_and_fractions[hitId].first;
128 const auto rhFraction = hits_and_fractions[hitId].second;
130 const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
131 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
132 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
134 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
136 const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
138 if (hit_find_in_SC != detIdToSimClusterId_Map.end()) {
139 const auto itcheck =
hitMap_->find(rh_detid);
144 for (
const auto&
h : hit_find_in_SC->second) {
147 tssInSimCluster[
h.clusterId].tracksterIdToEnergyAndScore[tsId].first +=
148 lcFractionInTs *
h.fraction *
hit->energy();
150 scsInTrackster[tsId].emplace_back(
h.clusterId, 0.f);
158 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
159 for (
const auto& lcId : tracksters[tsId].
vertices()) {
160 const auto& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
161 unsigned int numberOfHitsInLC = hits_and_fractions.size();
171 std::vector<int> hitsToSimClusterId(numberOfHitsInLC);
173 int maxSCId_byNumberOfHits = -1;
175 unsigned int maxSCNumberOfHitsInLC = 0;
177 int maxSCId_byEnergy = -1;
179 float maxEnergySharedLCandSC = 0.f;
181 float energyFractionOfLCinSC = 0.f;
183 float energyFractionOfSCinLC = 0.f;
184 std::unordered_map<unsigned, unsigned> occurrencesSCinLC;
185 unsigned int numberOfNoiseHitsInLC = 0;
186 std::unordered_map<unsigned, float> SCEnergyInLC;
188 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
189 const auto rh_detid = hits_and_fractions[hitId].first;
190 const auto rhFraction = hits_and_fractions[hitId].second;
192 const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
200 if (rhFraction == 0.) {
201 hitsToSimClusterId[hitId] = -2;
204 if (hit_find_in_SC == detIdToSimClusterId_Map.end()) {
205 hitsToSimClusterId[hitId] -= 1;
207 const auto itcheck =
hitMap_->find(rh_detid);
209 auto maxSCEnergyInLC = 0.f;
212 for (
const auto&
h : hit_find_in_SC->second) {
213 SCEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
216 if (SCEnergyInLC[
h.clusterId] > maxSCEnergyInLC) {
217 maxSCEnergyInLC = SCEnergyInLC[
h.clusterId];
218 maxSCId =
h.clusterId;
221 hitsToSimClusterId[hitId] = maxSCId;
225 for (
const auto&
c : hitsToSimClusterId) {
227 numberOfNoiseHitsInLC++;
229 occurrencesSCinLC[
c]++;
233 for (
const auto&
c : occurrencesSCinLC) {
234 if (
c.second > maxSCNumberOfHitsInLC) {
235 maxSCId_byNumberOfHits =
c.first;
236 maxSCNumberOfHitsInLC =
c.second;
240 for (
const auto&
c : SCEnergyInLC) {
241 if (
c.second > maxEnergySharedLCandSC) {
242 maxSCId_byEnergy =
c.first;
243 maxEnergySharedLCandSC =
c.second;
247 float totalSCEnergyOnLayer = 0.f;
248 if (maxSCId_byEnergy >= 0) {
249 totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy].energy;
250 energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer;
251 if (tracksters[tsId].raw_energy() > 0.
f) {
252 energyFractionOfLCinSC = maxEnergySharedLCandSC / tracksters[tsId].raw_energy();
256 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") << std::setw(12) <<
"TracksterID:"
257 <<
"\t" << std::setw(12) <<
"layerCluster"
258 <<
"\t" << std::setw(10) <<
"lc energy"
259 <<
"\t" << std::setw(5) <<
"nhits"
260 <<
"\t" << std::setw(12) <<
"noise hits"
261 <<
"\t" << std::setw(22) <<
"maxSCId_byNumberOfHits"
262 <<
"\t" << std::setw(8) <<
"nhitsSC"
263 <<
"\t" << std::setw(13) <<
"maxSCId_byEnergy"
264 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandSC"
265 <<
"\t" << std::setw(22) <<
"totalSCEnergyOnLayer"
266 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinSC"
267 <<
"\t" << std::setw(25) <<
"energyFractionOfSCinLC"
270 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
271 << std::setw(12) << tsId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
272 << tracksters[tsId].raw_energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
273 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSCId_byNumberOfHits <<
"\t" << std::setw(8)
274 << maxSCNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSCId_byEnergy <<
"\t" << std::setw(20)
275 << maxEnergySharedLCandSC <<
"\t" << std::setw(22) << totalSCEnergyOnLayer <<
"\t" << std::setw(22)
276 << energyFractionOfLCinSC <<
"\t" << std::setw(25) << energyFractionOfSCinLC <<
"\n";
280 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
281 <<
"Improved tssInSimCluster INFO (Now containing the linked tracksters id and energy - score still empty)"
283 for (
size_t sc = 0; sc < tssInSimCluster.size(); ++sc) {
284 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
285 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
286 <<
" SimClusterIdx: " << tssInSimCluster[sc].simClusterId << std::endl;
287 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
288 <<
" Energy: " << tssInSimCluster[sc].energy << std::endl;
289 double tot_energy = 0.;
290 for (
auto const& haf : tssInSimCluster[sc].hits_and_fractions) {
291 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
292 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
293 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
294 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
296 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
297 for (
auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) {
298 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
299 <<
" tsIdx/energy/score: " << ts.first <<
"/" << ts.second.first <<
"/" << ts.second.second << std::endl;
303 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"Improved detIdToSimClusterId_Map INFO" << std::endl;
304 for (
auto const& sc : detIdToSimClusterId_Map) {
305 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
306 <<
"For detId: " << (uint32_t)sc.first
307 <<
" we have found the following connections with SimClusters:" << std::endl;
308 for (
auto const& sclu : sc.second) {
309 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
310 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
311 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
318 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
321 std::sort(scsInTrackster[tsId].begin(), scsInTrackster[tsId].
end());
323 scsInTrackster[tsId].erase(
last, scsInTrackster[tsId].
end());
327 if (tracksters[tsId].raw_energy() == 0. && !scsInTrackster[tsId].
empty()) {
328 for (
auto& scPair : scsInTrackster[tsId]) {
330 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"TracksterId : \t " << tsId <<
"\t SC id : \t"
331 << scPair.first <<
"\t score \t " << scPair.second <<
"\n";
336 float invTracksterEnergyWeight = 0.f;
337 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
338 const auto lcId = tracksters[tsId].vertices(
i);
339 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
341 const auto& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
343 for (
auto const& haf : hits_and_fractions) {
344 invTracksterEnergyWeight += (lcFractionInTs * haf.second *
hitMap_->at(haf.first)->energy()) *
345 (lcFractionInTs * haf.second *
hitMap_->at(haf.first)->energy());
348 invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight;
350 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
351 const auto lcId = tracksters[tsId].vertices(
i);
352 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
354 const auto& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
355 unsigned int numberOfHitsInLC = hits_and_fractions.size();
356 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
357 DetId rh_detid = hits_and_fractions[
i].first;
358 float rhFraction = hits_and_fractions[
i].second * lcFractionInTs;
360 const bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end());
362 const auto itcheck =
hitMap_->find(rh_detid);
364 float hitEnergyWeight =
hit->energy() *
hit->energy();
366 for (
auto& scPair : scsInTrackster[tsId]) {
367 float scFraction = 0.f;
369 const auto findHitIt =
std::find(detIdToSimClusterId_Map[rh_detid].begin(),
370 detIdToSimClusterId_Map[rh_detid].
end(),
372 if (findHitIt != detIdToSimClusterId_Map[rh_detid].
end())
373 scFraction = findHitIt->fraction;
376 (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invTracksterEnergyWeight;
378 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
379 <<
"rh_detid:\t" << (uint32_t)rh_detid <<
"\ttracksterId:\t" << tsId <<
"\t"
380 <<
"rhfraction,scFraction:\t" << rhFraction <<
", " << scFraction <<
"\t"
381 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
382 <<
"current score:\t" << scPair.second <<
"\t"
383 <<
"invTracksterEnergyWeight:\t" << invTracksterEnergyWeight <<
"\n";
390 if (scsInTrackster[tsId].
empty())
391 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"trackster Id: \t" << tsId <<
"\tSC id:\t-1 "
398 for (
const auto& scId : sCIndices) {
399 float invSCEnergyWeight = 0.f;
401 const unsigned int SCNumberOfHits = tssInSimCluster[scId].hits_and_fractions.size();
402 if (SCNumberOfHits == 0)
405 int tsWithMaxEnergyInSC = -1;
407 float maxEnergyTSinSC = 0.f;
408 float SCenergy = tssInSimCluster[scId].energy;
410 float SCEnergyFractionInTS = 0.f;
411 for (
const auto& ts : tssInSimCluster[scId].tracksterIdToEnergyAndScore) {
412 if (ts.second.first > maxEnergyTSinSC) {
413 maxEnergyTSinSC = ts.second.first;
414 tsWithMaxEnergyInSC = ts.first;
418 SCEnergyFractionInTS = maxEnergyTSinSC / SCenergy;
420 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
421 << std::setw(12) <<
"simcluster\t" << std::setw(15) <<
"sc total energy\t" << std::setw(15)
422 <<
"scEnergyOnLayer\t" << std::setw(14) <<
"SCNhitsOnLayer\t" << std::setw(18) <<
"tsWithMaxEnergyInSC\t"
423 << std::setw(15) <<
"maxEnergyTSinSC\t" << std::setw(20) <<
"SCEnergyFractionInTS"
425 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
426 << std::setw(12) << scId <<
"\t" << std::setw(15) << simClusters[scId].energy() <<
"\t" << std::setw(15)
427 << SCenergy <<
"\t" << std::setw(14) << SCNumberOfHits <<
"\t" << std::setw(18) << tsWithMaxEnergyInSC <<
"\t"
428 << std::setw(15) << maxEnergyTSinSC <<
"\t" << std::setw(20) << SCEnergyFractionInTS <<
"\n";
431 for (
auto const& haf : tssInSimCluster[scId].hits_and_fractions) {
432 invSCEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
434 invSCEnergyWeight = 1.f / invSCEnergyWeight;
436 for (
unsigned int i = 0;
i < SCNumberOfHits; ++
i) {
437 auto& sc_hitDetId = tssInSimCluster[scId].hits_and_fractions[
i].first;
438 auto& scFraction = tssInSimCluster[scId].hits_and_fractions[
i].second;
440 bool hitWithLC =
false;
441 if (scFraction == 0.
f)
443 const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId);
444 if (hit_find_in_LC != detIdToLayerClusterId_Map.end())
446 const auto itcheck =
hitMap_->find(sc_hitDetId);
448 float hitEnergyWeight =
hit->energy() *
hit->energy();
449 for (
auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) {
450 unsigned int tsId = tsPair.first;
451 float tsFraction = 0.f;
453 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
454 const auto lcId = tracksters[tsId].vertices(
i);
455 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
458 const auto findHitIt =
std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(),
459 detIdToLayerClusterId_Map[sc_hitDetId].
end(),
461 if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].
end())
462 tsFraction = findHitIt->fraction * lcFractionInTs;
464 tsPair.second.second +=
465 (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight;
467 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
468 <<
"SCDetId:\t" << (uint32_t)sc_hitDetId <<
"\tTracksterId:\t" << tracksterId <<
"\t"
469 <<
"tsFraction, scFraction:\t" << tsFraction <<
", " << scFraction <<
"\t"
470 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
471 <<
"current score:\t" << tsPair.second.second <<
"\t"
472 <<
"invSCEnergyWeight:\t" << invSCEnergyWeight <<
"\n";
478 if (tssInSimCluster[scId].tracksterIdToEnergyAndScore.empty())
479 LogDebug(
"TracksterAssociatorByEnergyScoreImpl") <<
"SC Id: \t" << scId <<
"\tTS id:\t-1 "
483 for (
const auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) {
484 LogDebug(
"TracksterAssociatorByEnergyScoreImpl")
485 <<
"SC Id: \t" << scId <<
"\t TS id: \t" << tsPair.first <<
"\t score \t" << tsPair.second.second
486 <<
"\t shared energy:\t" << tsPair.second.first <<
"\t shared energy fraction:\t"
487 << (tsPair.second.first / SCenergy) <<
"\n";
491 return {scsInTrackster, tssInSimCluster};