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(
"LCToSCAssociatorByEnergyScoreImpl")
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(
"LCToSCAssociatorByEnergyScoreImpl")
78 <<
"lcsInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl;
79 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
80 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
81 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
82 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
83 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
84 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
85 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
86 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
87 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" # of clusters : " << nLayerClusters << std::endl;
88 double tot_energy = 0.;
89 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
90 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
91 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
92 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
93 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
95 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
96 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
97 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
98 << lc.second.first <<
"/" << lc.second.second << std::endl;
103 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"detIdToSimClusterId_Map INFO" << std::endl;
104 for (
auto const& sc : detIdToSimClusterId_Map) {
105 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
106 <<
"For detId: " << (uint32_t)sc.first
107 <<
" we have found the following connections with SimClusters:" << std::endl;
108 for (
auto const& sclu : sc.second) {
109 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
110 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
111 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
117 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
123 scsInLayerCluster.resize(nLayerClusters);
125 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
126 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
127 unsigned int numberOfHitsInLC = hits_and_fractions.size();
128 const auto firstHitDetId = hits_and_fractions[0].first;
132 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
133 const auto rh_detid = hits_and_fractions[hitId].first;
134 const auto rhFraction = hits_and_fractions[hitId].second;
136 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
137 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
138 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
140 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
142 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
144 if (hit_find_in_SC != detIdToSimClusterId_Map.end()) {
145 const auto itcheck =
hitMap_->find(rh_detid);
150 for (
auto&
h : hit_find_in_SC->second) {
153 lcsInSimCluster[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
154 h.fraction *
hit->energy();
156 scsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
163 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
164 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
165 unsigned int numberOfHitsInLC = hits_and_fractions.size();
166 const auto firstHitDetId = hits_and_fractions[0].first;
178 std::vector<int> hitsToSimClusterId(numberOfHitsInLC);
180 int maxSCId_byNumberOfHits = -1;
182 unsigned int maxSCNumberOfHitsInLC = 0;
184 int maxSCId_byEnergy = -1;
186 float maxEnergySharedLCandSC = 0.f;
188 float energyFractionOfLCinSC = 0.f;
190 float energyFractionOfSCinLC = 0.f;
191 std::unordered_map<unsigned, unsigned> occurrencesSCinLC;
192 unsigned int numberOfNoiseHitsInLC = 0;
193 std::unordered_map<unsigned, float> SCEnergyInLC;
195 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
196 const auto rh_detid = hits_and_fractions[hitId].first;
197 const auto rhFraction = hits_and_fractions[hitId].second;
199 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
207 if (rhFraction == 0.) {
208 hitsToSimClusterId[hitId] = -2;
211 if (hit_find_in_SC == detIdToSimClusterId_Map.end()) {
212 hitsToSimClusterId[hitId] -= 1;
214 const auto itcheck =
hitMap_->find(rh_detid);
216 auto maxSCEnergyInLC = 0.f;
219 for (
auto&
h : hit_find_in_SC->second) {
220 SCEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
223 if (SCEnergyInLC[
h.clusterId] > maxSCEnergyInLC) {
224 maxSCEnergyInLC = SCEnergyInLC[
h.clusterId];
225 maxSCId =
h.clusterId;
228 hitsToSimClusterId[hitId] = maxSCId;
232 for (
const auto&
c : hitsToSimClusterId) {
234 numberOfNoiseHitsInLC++;
236 occurrencesSCinLC[
c]++;
240 for (
const auto&
c : occurrencesSCinLC) {
241 if (
c.second > maxSCNumberOfHitsInLC) {
242 maxSCId_byNumberOfHits =
c.first;
243 maxSCNumberOfHitsInLC =
c.second;
247 for (
const auto&
c : SCEnergyInLC) {
248 if (
c.second > maxEnergySharedLCandSC) {
249 maxSCId_byEnergy =
c.first;
250 maxEnergySharedLCandSC =
c.second;
254 float totalSCEnergyOnLayer = 0.f;
255 if (maxSCId_byEnergy >= 0) {
256 totalSCEnergyOnLayer = lcsInSimCluster[maxSCId_byEnergy][lcLayerId].energy;
257 energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer;
259 energyFractionOfLCinSC = maxEnergySharedLCandSC /
clusters[lcId].energy();
263 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") << std::setw(10) <<
"LayerId:"
264 <<
"\t" << std::setw(12) <<
"layerCluster"
265 <<
"\t" << std::setw(10) <<
"lc energy"
266 <<
"\t" << std::setw(5) <<
"nhits"
267 <<
"\t" << std::setw(12) <<
"noise hits"
268 <<
"\t" << std::setw(22) <<
"maxSCId_byNumberOfHits"
269 <<
"\t" << std::setw(8) <<
"nhitsSC"
270 <<
"\t" << std::setw(13) <<
"maxSCId_byEnergy"
271 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandSC"
272 <<
"\t" << std::setw(22) <<
"totalSCEnergyOnLayer"
273 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinSC"
274 <<
"\t" << std::setw(25) <<
"energyFractionOfSCinLC"
277 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
278 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
279 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
280 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSCId_byNumberOfHits <<
"\t" << std::setw(8)
281 << maxSCNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSCId_byEnergy <<
"\t" << std::setw(20)
282 << maxEnergySharedLCandSC <<
"\t" << std::setw(22) << totalSCEnergyOnLayer <<
"\t" << std::setw(22)
283 << energyFractionOfLCinSC <<
"\t" << std::setw(25) << energyFractionOfSCinLC <<
"\n";
286 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
287 <<
"Improved lcsInSimCluster INFO (Now containing the linked layer clusters id and energy - score still empty)"
289 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
290 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
291 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
292 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
293 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
294 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
295 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
296 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
297 double tot_energy = 0.;
298 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
299 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
300 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
301 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
302 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
304 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
305 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
306 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
307 << lc.second.first <<
"/" << lc.second.second << std::endl;
312 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"Improved detIdToSimClusterId_Map INFO" << std::endl;
313 for (
auto const& sc : detIdToSimClusterId_Map) {
314 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
315 <<
"For detId: " << (uint32_t)sc.first
316 <<
" we have found the following connections with SimClusters:" << std::endl;
317 for (
auto const& sclu : sc.second) {
318 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
319 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
320 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
327 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
330 std::sort(scsInLayerCluster[lcId].begin(), scsInLayerCluster[lcId].
end());
331 auto last =
std::unique(scsInLayerCluster[lcId].begin(), scsInLayerCluster[lcId].
end());
332 scsInLayerCluster[lcId].erase(
last, scsInLayerCluster[lcId].
end());
333 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
334 unsigned int numberOfHitsInLC = hits_and_fractions.size();
338 for (
auto& scPair : scsInLayerCluster[lcId]) {
340 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t SC id : \t"
341 << scPair.first <<
"\t score \t " << scPair.second <<
"\n";
347 float invLayerClusterEnergyWeight = 0.f;
348 for (
auto const& haf :
clusters[lcId].hitsAndFractions()) {
349 invLayerClusterEnergyWeight +=
350 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
352 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
353 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
354 DetId rh_detid = hits_and_fractions[
i].first;
355 float rhFraction = hits_and_fractions[
i].second;
357 bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end());
359 auto itcheck =
hitMap_->find(rh_detid);
361 float hitEnergyWeight =
hit->energy() *
hit->energy();
363 for (
auto& scPair : scsInLayerCluster[lcId]) {
364 float scFraction = 0.f;
366 auto findHitIt =
std::find(detIdToSimClusterId_Map[rh_detid].begin(),
367 detIdToSimClusterId_Map[rh_detid].
end(),
369 if (findHitIt != detIdToSimClusterId_Map[rh_detid].
end())
370 scFraction = findHitIt->fraction;
373 (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invLayerClusterEnergyWeight;
375 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
376 <<
"rh_detid:\t" << (uint32_t)rh_detid <<
"\tlayerClusterId:\t" << lcId <<
"\t"
377 <<
"rhfraction,scfraction:\t" << rhFraction <<
", " << scFraction <<
"\t"
378 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
379 <<
"current score:\t" << scPair.second <<
"\t"
380 <<
"invLayerClusterEnergyWeight:\t" << invLayerClusterEnergyWeight <<
"\n";
385 if (scsInLayerCluster[lcId].
empty())
386 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tSC id:\t-1 "
393 for (
const auto& scId : sCIndices) {
394 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
395 unsigned int SCNumberOfHits = lcsInSimCluster[scId][layerId].hits_and_fractions.size();
396 if (SCNumberOfHits == 0)
399 int lcWithMaxEnergyInSC = -1;
401 float maxEnergyLCinSC = 0.f;
402 float SCenergy = lcsInSimCluster[scId][layerId].energy;
404 float SCEnergyFractionInLC = 0.f;
405 for (
auto& lc : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
406 if (lc.second.first > maxEnergyLCinSC) {
407 maxEnergyLCinSC = lc.second.first;
408 lcWithMaxEnergyInSC = lc.first;
412 SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy;
414 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
415 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"simcluster\t" << std::setw(15) <<
"sc total energy\t"
416 << std::setw(15) <<
"scEnergyOnLayer\t" << std::setw(14) <<
"SCNhitsOnLayer\t" << std::setw(18)
417 <<
"lcWithMaxEnergyInSC\t" << std::setw(15) <<
"maxEnergyLCinSC\t" << std::setw(20) <<
"SCEnergyFractionInLC"
419 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
420 << std::setw(8) << layerId <<
"\t" << std::setw(12) << scId <<
"\t" << std::setw(15)
421 << simClusters[scId].energy() <<
"\t" << std::setw(15) << SCenergy <<
"\t" << std::setw(14) << SCNumberOfHits
422 <<
"\t" << std::setw(18) << lcWithMaxEnergyInSC <<
"\t" << std::setw(15) << maxEnergyLCinSC <<
"\t"
423 << std::setw(20) << SCEnergyFractionInLC <<
"\n";
426 float invSCEnergyWeight = 0.f;
427 for (
auto const& haf : lcsInSimCluster[scId][layerId].hits_and_fractions) {
428 invSCEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
430 invSCEnergyWeight = 1.f / invSCEnergyWeight;
431 for (
unsigned int i = 0;
i < SCNumberOfHits; ++
i) {
432 auto& sc_hitDetId = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].first;
433 auto& scFraction = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].second;
435 bool hitWithLC =
false;
436 if (scFraction == 0.
f)
438 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId);
439 if (hit_find_in_LC != detIdToLayerClusterId_Map.end())
441 auto itcheck =
hitMap_->find(sc_hitDetId);
443 float hitEnergyWeight =
hit->energy() *
hit->energy();
444 for (
auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
445 unsigned int layerClusterId = lcPair.first;
446 float lcFraction = 0.f;
449 auto findHitIt =
std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(),
450 detIdToLayerClusterId_Map[sc_hitDetId].
end(),
452 if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].
end())
453 lcFraction = findHitIt->fraction;
455 lcPair.second.second +=
456 (lcFraction - scFraction) * (lcFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight;
458 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
459 <<
"scDetId:\t" << (uint32_t)sc_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t"
460 <<
"lcfraction,scfraction:\t" << lcFraction <<
", " << scFraction <<
"\t"
461 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
462 <<
"current score:\t" << lcPair.second.second <<
"\t"
463 <<
"invSCEnergyWeight:\t" << invSCEnergyWeight <<
"\n";
468 if (lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore.empty())
469 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"SC Id: \t" << scId <<
"\tLC id:\t-1 "
473 for (
const auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
474 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
475 <<
"SC Id: \t" << scId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
476 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t"
477 << (lcPair.second.first / SCenergy) <<
"\n";
482 return {scsInLayerCluster, lcsInSimCluster};