18 const auto&
clusters = *cCCH.product();
19 const auto& simClusters = *sCCH.
product();
20 auto nLayerClusters =
clusters.size();
24 auto nSimClusters = simClusters.size();
25 std::vector<size_t> sCIndices;
26 for (
unsigned int scId = 0; scId < nSimClusters; ++scId) {
28 simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() != 0)) {
29 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
30 <<
"Excluding SimCluster from event: " << simClusters[scId].g4Tracks()[0].eventId().event()
31 <<
" with BX: " << simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() << std::endl;
34 sCIndices.emplace_back(scId);
36 nSimClusters = sCIndices.size();
42 lcsInSimCluster.resize(nSimClusters);
43 for (
unsigned int i = 0;
i < nSimClusters; ++
i) {
44 lcsInSimCluster[
i].resize(
layers_ * 2);
45 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
46 lcsInSimCluster[
i][
j].simClusterId =
i;
47 lcsInSimCluster[
i][
j].energy = 0.f;
48 lcsInSimCluster[
i][
j].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 scLayerId =
61 const auto itcheck =
hitMap_->find(hitid);
62 if (itcheck !=
hitMap_->end()) {
63 auto hit_find_it = detIdToSimClusterId_Map.find(hitid);
64 if (hit_find_it == detIdToSimClusterId_Map.end()) {
65 detIdToSimClusterId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
67 detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second);
70 lcsInSimCluster[scId][scLayerId].energy += it_haf.second *
hit->energy();
71 lcsInSimCluster[scId][scLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
77 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
78 <<
"lcsInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl;
79 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
80 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
81 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
82 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
83 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
84 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
85 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
86 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
87 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
88 <<
" # of clusters : " << nLayerClusters << std::endl;
89 double tot_energy = 0.;
90 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
91 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
92 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
93 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
94 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
96 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
97 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
98 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
99 << lc.second.first <<
"/" << lc.second.second << std::endl;
104 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"detIdToSimClusterId_Map INFO" << std::endl;
105 for (
auto const& sc : detIdToSimClusterId_Map) {
106 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
107 <<
"For detId: " << (uint32_t)sc.first
108 <<
" we have found the following connections with SimClusters:" << std::endl;
109 for (
auto const& sclu : sc.second) {
110 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
111 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
112 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
118 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
124 scsInLayerCluster.resize(nLayerClusters);
126 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
127 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
128 unsigned int numberOfHitsInLC = hits_and_fractions.size();
129 const auto firstHitDetId = hits_and_fractions[0].first;
133 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
134 const auto rh_detid = hits_and_fractions[hitId].first;
135 const auto rhFraction = hits_and_fractions[hitId].second;
137 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
138 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
139 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
141 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
143 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
145 if (hit_find_in_SC != detIdToSimClusterId_Map.end()) {
146 const auto itcheck =
hitMap_->find(rh_detid);
151 for (
auto&
h : hit_find_in_SC->second) {
154 lcsInSimCluster[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
155 h.fraction *
hit->energy();
157 scsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
164 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
165 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
166 unsigned int numberOfHitsInLC = hits_and_fractions.size();
167 const auto firstHitDetId = hits_and_fractions[0].first;
179 std::vector<int> hitsToSimClusterId(numberOfHitsInLC);
181 int maxSCId_byNumberOfHits = -1;
183 unsigned int maxSCNumberOfHitsInLC = 0;
185 int maxSCId_byEnergy = -1;
187 float maxEnergySharedLCandSC = 0.f;
189 float energyFractionOfLCinSC = 0.f;
191 float energyFractionOfSCinLC = 0.f;
192 std::unordered_map<unsigned, unsigned> occurrencesSCinLC;
193 unsigned int numberOfNoiseHitsInLC = 0;
194 std::unordered_map<unsigned, float> SCEnergyInLC;
196 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
197 const auto rh_detid = hits_and_fractions[hitId].first;
198 const auto rhFraction = hits_and_fractions[hitId].second;
200 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
208 if (rhFraction == 0.) {
209 hitsToSimClusterId[hitId] = -2;
212 if (hit_find_in_SC == detIdToSimClusterId_Map.end()) {
213 hitsToSimClusterId[hitId] -= 1;
215 const auto itcheck =
hitMap_->find(rh_detid);
217 auto maxSCEnergyInLC = 0.f;
220 for (
auto&
h : hit_find_in_SC->second) {
221 SCEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
224 if (SCEnergyInLC[
h.clusterId] > maxSCEnergyInLC) {
225 maxSCEnergyInLC = SCEnergyInLC[
h.clusterId];
226 maxSCId =
h.clusterId;
229 hitsToSimClusterId[hitId] = maxSCId;
233 for (
const auto&
c : hitsToSimClusterId) {
235 numberOfNoiseHitsInLC++;
237 occurrencesSCinLC[
c]++;
241 for (
const auto&
c : occurrencesSCinLC) {
242 if (
c.second > maxSCNumberOfHitsInLC) {
243 maxSCId_byNumberOfHits =
c.first;
244 maxSCNumberOfHitsInLC =
c.second;
248 for (
const auto&
c : SCEnergyInLC) {
249 if (
c.second > maxEnergySharedLCandSC) {
250 maxSCId_byEnergy =
c.first;
251 maxEnergySharedLCandSC =
c.second;
255 float totalSCEnergyOnLayer = 0.f;
256 if (maxSCId_byEnergy >= 0) {
257 totalSCEnergyOnLayer = lcsInSimCluster[maxSCId_byEnergy][lcLayerId].energy;
258 energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer;
260 energyFractionOfLCinSC = maxEnergySharedLCandSC /
clusters[lcId].energy();
264 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") << std::setw(10) <<
"LayerId:"
265 <<
"\t" << std::setw(12) <<
"layerCluster"
266 <<
"\t" << std::setw(10) <<
"lc energy"
267 <<
"\t" << std::setw(5) <<
"nhits"
268 <<
"\t" << std::setw(12) <<
"noise hits"
269 <<
"\t" << std::setw(22) <<
"maxSCId_byNumberOfHits"
270 <<
"\t" << std::setw(8) <<
"nhitsSC"
271 <<
"\t" << std::setw(13) <<
"maxSCId_byEnergy"
272 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandSC"
273 <<
"\t" << std::setw(22) <<
"totalSCEnergyOnLayer"
274 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinSC"
275 <<
"\t" << std::setw(25) <<
"energyFractionOfSCinLC"
278 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
279 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
280 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
281 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSCId_byNumberOfHits <<
"\t" << std::setw(8)
282 << maxSCNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSCId_byEnergy <<
"\t" << std::setw(20)
283 << maxEnergySharedLCandSC <<
"\t" << std::setw(22) << totalSCEnergyOnLayer <<
"\t" << std::setw(22)
284 << energyFractionOfLCinSC <<
"\t" << std::setw(25) << energyFractionOfSCinLC <<
"\n";
287 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
288 <<
"Improved lcsInSimCluster INFO (Now containing the linked layer clusters id and energy - score still empty)"
290 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
291 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
292 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
293 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
294 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
295 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
296 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
297 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
298 double tot_energy = 0.;
299 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
300 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
301 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
302 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
303 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
305 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
306 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
307 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
308 << lc.second.first <<
"/" << lc.second.second << std::endl;
313 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"Improved detIdToSimClusterId_Map INFO" << std::endl;
314 for (
auto const& sc : detIdToSimClusterId_Map) {
315 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
316 <<
"For detId: " << (uint32_t)sc.first
317 <<
" we have found the following connections with SimClusters:" << std::endl;
318 for (
auto const& sclu : sc.second) {
319 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
320 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
321 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
328 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
331 std::sort(scsInLayerCluster[lcId].begin(), scsInLayerCluster[lcId].
end());
332 auto last =
std::unique(scsInLayerCluster[lcId].begin(), scsInLayerCluster[lcId].
end());
333 scsInLayerCluster[lcId].erase(
last, scsInLayerCluster[lcId].
end());
334 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
335 unsigned int numberOfHitsInLC = hits_and_fractions.size();
339 for (
auto& scPair : scsInLayerCluster[lcId]) {
341 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t SC id : \t"
342 << scPair.first <<
"\t score \t " << scPair.second <<
"\n";
348 float invLayerClusterEnergyWeight = 0.f;
349 for (
auto const& haf :
clusters[lcId].hitsAndFractions()) {
350 invLayerClusterEnergyWeight +=
351 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
353 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
354 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
355 DetId rh_detid = hits_and_fractions[
i].first;
356 float rhFraction = hits_and_fractions[
i].second;
358 bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end());
360 auto itcheck =
hitMap_->find(rh_detid);
362 float hitEnergyWeight =
hit->energy() *
hit->energy();
364 for (
auto& scPair : scsInLayerCluster[lcId]) {
365 float scFraction = 0.f;
367 auto findHitIt =
std::find(detIdToSimClusterId_Map[rh_detid].begin(),
368 detIdToSimClusterId_Map[rh_detid].
end(),
370 if (findHitIt != detIdToSimClusterId_Map[rh_detid].
end())
371 scFraction = findHitIt->fraction;
374 (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invLayerClusterEnergyWeight;
376 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
377 <<
"rh_detid:\t" << (uint32_t)rh_detid <<
"\tlayerClusterId:\t" << lcId <<
"\t"
378 <<
"rhfraction,scfraction:\t" << rhFraction <<
", " << scFraction <<
"\t"
379 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
380 <<
"current score:\t" << scPair.second <<
"\t"
381 <<
"invLayerClusterEnergyWeight:\t" << invLayerClusterEnergyWeight <<
"\n";
386 if (scsInLayerCluster[lcId].
empty())
387 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tSC id:\t-1 "
394 for (
const auto& scId : sCIndices) {
395 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
396 unsigned int SCNumberOfHits = lcsInSimCluster[scId][layerId].hits_and_fractions.size();
397 if (SCNumberOfHits == 0)
400 int lcWithMaxEnergyInSC = -1;
402 float maxEnergyLCinSC = 0.f;
403 float SCenergy = lcsInSimCluster[scId][layerId].energy;
405 float SCEnergyFractionInLC = 0.f;
406 for (
auto& lc : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
407 if (lc.second.first > maxEnergyLCinSC) {
408 maxEnergyLCinSC = lc.second.first;
409 lcWithMaxEnergyInSC = lc.first;
413 SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy;
415 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
416 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"simcluster\t" << std::setw(15) <<
"sc total energy\t"
417 << std::setw(15) <<
"scEnergyOnLayer\t" << std::setw(14) <<
"SCNhitsOnLayer\t" << std::setw(18)
418 <<
"lcWithMaxEnergyInSC\t" << std::setw(15) <<
"maxEnergyLCinSC\t" << std::setw(20) <<
"SCEnergyFractionInLC"
420 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
421 << std::setw(8) << layerId <<
"\t" << std::setw(12) << scId <<
"\t" << std::setw(15)
422 << simClusters[scId].energy() <<
"\t" << std::setw(15) << SCenergy <<
"\t" << std::setw(14) << SCNumberOfHits
423 <<
"\t" << std::setw(18) << lcWithMaxEnergyInSC <<
"\t" << std::setw(15) << maxEnergyLCinSC <<
"\t"
424 << std::setw(20) << SCEnergyFractionInLC <<
"\n";
427 float invSCEnergyWeight = 0.f;
428 for (
auto const& haf : lcsInSimCluster[scId][layerId].hits_and_fractions) {
429 invSCEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
431 invSCEnergyWeight = 1.f / invSCEnergyWeight;
432 for (
unsigned int i = 0;
i < SCNumberOfHits; ++
i) {
433 auto& sc_hitDetId = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].first;
434 auto& scFraction = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].second;
436 bool hitWithLC =
false;
437 if (scFraction == 0.
f)
439 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId);
440 if (hit_find_in_LC != detIdToLayerClusterId_Map.end())
442 auto itcheck =
hitMap_->find(sc_hitDetId);
444 float hitEnergyWeight =
hit->energy() *
hit->energy();
445 for (
auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
446 unsigned int layerClusterId = lcPair.first;
447 float lcFraction = 0.f;
450 auto findHitIt =
std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(),
451 detIdToLayerClusterId_Map[sc_hitDetId].
end(),
453 if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].
end())
454 lcFraction = findHitIt->fraction;
456 lcPair.second.second +=
457 (lcFraction - scFraction) * (lcFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight;
459 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
460 <<
"scDetId:\t" << (uint32_t)sc_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t"
461 <<
"lcfraction,scfraction:\t" << lcFraction <<
", " << scFraction <<
"\t"
462 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
463 <<
"current score:\t" << lcPair.second.second <<
"\t"
464 <<
"invSCEnergyWeight:\t" << invSCEnergyWeight <<
"\n";
469 if (lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore.empty())
470 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl") <<
"SC Id: \t" << scId <<
"\tLC id:\t-1 "
474 for (
const auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
475 LogDebug(
"SimClusterAssociatorByEnergyScoreImpl")
476 <<
"SC Id: \t" << scId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
477 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t"
478 << (lcPair.second.first / SCenergy) <<
"\n";
483 return {scsInLayerCluster, lcsInSimCluster};