24 const auto&
clusters = *cCCH.product();
26 auto nLayerClusters =
clusters.size();
28 std::vector<size_t> cPIndices;
32 auto nCaloParticles = cPIndices.size();
37 cPOnLayer.resize(nCaloParticles);
38 for (
unsigned int i = 0;
i < nCaloParticles; ++
i) {
40 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
41 cPOnLayer[
i][
j].caloParticleId =
i;
42 cPOnLayer[
i][
j].energy = 0.f;
43 cPOnLayer[
i][
j].hits_and_fractions.clear();
49 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToCaloParticleId_Map;
50 for (
const auto& cpId : cPIndices) {
52 for (
const auto& it_sc : simClusterRefVector) {
55 for (
const auto& it_haf : hits_and_fractions) {
56 const auto hitid = (it_haf.first);
57 const auto cpLayerId =
59 const auto itcheck =
hitMap_->find(hitid);
60 if (itcheck !=
hitMap_->end()) {
61 auto hit_find_it = detIdToCaloParticleId_Map.find(hitid);
62 if (hit_find_it == detIdToCaloParticleId_Map.end()) {
63 detIdToCaloParticleId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
64 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
66 auto findHitIt =
std::find(detIdToCaloParticleId_Map[hitid].begin(),
67 detIdToCaloParticleId_Map[hitid].
end(),
69 if (findHitIt != detIdToCaloParticleId_Map[hitid].
end()) {
70 findHitIt->fraction += it_haf.second;
72 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
76 cPOnLayer[cpId][cpLayerId].energy += it_haf.second *
hit->energy();
83 auto& haf = cPOnLayer[cpId][cpLayerId].hits_and_fractions;
84 auto found = std::find_if(
85 std::begin(haf),
std::end(haf), [&hitid](
const std::pair<DetId, float>&
v) {
return v.first == hitid; });
86 if (
found != haf.end()) {
87 found->second += it_haf.second;
89 cPOnLayer[cpId][cpLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
97 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
98 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
99 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
100 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
101 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
102 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
103 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
104 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
105 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
106 double tot_energy = 0.;
107 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
108 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
109 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
110 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
111 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
113 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
114 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
115 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
116 << lc.second.first <<
"/" << lc.second.second << std::endl;
121 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
122 for (
auto const&
cp : detIdToCaloParticleId_Map) {
123 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
124 <<
"For detId: " << (uint32_t)
cp.first
125 <<
" we have found the following connections with CaloParticles:" << std::endl;
126 for (
auto const& cpp :
cp.second) {
127 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
128 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
129 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
135 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
141 cpsInLayerCluster.resize(nLayerClusters);
143 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
144 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
145 unsigned int numberOfHitsInLC = hits_and_fractions.size();
146 const auto firstHitDetId = hits_and_fractions[0].first;
150 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
151 const auto rh_detid = hits_and_fractions[hitId].first;
152 const auto rhFraction = hits_and_fractions[hitId].second;
154 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
155 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
156 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
158 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
160 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
162 if (hit_find_in_CP != detIdToCaloParticleId_Map.end()) {
163 const auto itcheck =
hitMap_->find(rh_detid);
165 for (
auto&
h : hit_find_in_CP->second) {
166 cPOnLayer[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
h.fraction *
hit->energy();
167 cpsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
174 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
175 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
176 unsigned int numberOfHitsInLC = hits_and_fractions.size();
177 const auto firstHitDetId = hits_and_fractions[0].first;
189 std::vector<int> hitsToCaloParticleId(numberOfHitsInLC);
191 int maxCPId_byNumberOfHits = -1;
193 unsigned int maxCPNumberOfHitsInLC = 0;
195 int maxCPId_byEnergy = -1;
197 float maxEnergySharedLCandCP = 0.f;
199 float energyFractionOfLCinCP = 0.f;
201 float energyFractionOfCPinLC = 0.f;
202 std::unordered_map<unsigned, unsigned> occurrencesCPinLC;
203 unsigned int numberOfNoiseHitsInLC = 0;
204 std::unordered_map<unsigned, float> CPEnergyInLC;
206 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
207 const auto rh_detid = hits_and_fractions[hitId].first;
208 const auto rhFraction = hits_and_fractions[hitId].second;
210 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
218 if (rhFraction == 0.) {
219 hitsToCaloParticleId[hitId] = -2;
221 if (hit_find_in_CP == detIdToCaloParticleId_Map.end()) {
222 hitsToCaloParticleId[hitId] -= 1;
224 const auto itcheck =
hitMap_->find(rh_detid);
226 auto maxCPEnergyInLC = 0.f;
228 for (
auto&
h : hit_find_in_CP->second) {
229 CPEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
232 if (CPEnergyInLC[
h.clusterId] > maxCPEnergyInLC) {
233 maxCPEnergyInLC = CPEnergyInLC[
h.clusterId];
234 maxCPId =
h.clusterId;
237 hitsToCaloParticleId[hitId] = maxCPId;
241 for (
const auto&
c : hitsToCaloParticleId) {
243 numberOfNoiseHitsInLC++;
245 occurrencesCPinLC[
c]++;
249 for (
const auto&
c : occurrencesCPinLC) {
250 if (
c.second > maxCPNumberOfHitsInLC) {
251 maxCPId_byNumberOfHits =
c.first;
252 maxCPNumberOfHitsInLC =
c.second;
256 for (
const auto&
c : CPEnergyInLC) {
257 if (
c.second > maxEnergySharedLCandCP) {
258 maxCPId_byEnergy =
c.first;
259 maxEnergySharedLCandCP =
c.second;
263 float totalCPEnergyOnLayer = 0.f;
264 if (maxCPId_byEnergy >= 0) {
265 totalCPEnergyOnLayer = cPOnLayer[maxCPId_byEnergy][lcLayerId].energy;
266 energyFractionOfCPinLC = maxEnergySharedLCandCP / totalCPEnergyOnLayer;
268 energyFractionOfLCinCP = maxEnergySharedLCandCP /
clusters[lcId].energy();
272 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") << std::setw(10) <<
"LayerId:"
273 <<
"\t" << std::setw(12) <<
"layerCluster"
274 <<
"\t" << std::setw(10) <<
"lc energy"
275 <<
"\t" << std::setw(5) <<
"nhits"
276 <<
"\t" << std::setw(12) <<
"noise hits"
277 <<
"\t" << std::setw(22) <<
"maxCPId_byNumberOfHits"
278 <<
"\t" << std::setw(8) <<
"nhitsCP"
279 <<
"\t" << std::setw(13) <<
"maxCPId_byEnergy"
280 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandCP"
281 <<
"\t" << std::setw(22) <<
"totalCPEnergyOnLayer"
282 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinCP"
283 <<
"\t" << std::setw(25) <<
"energyFractionOfCPinLC"
286 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
287 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
288 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
289 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxCPId_byNumberOfHits <<
"\t" << std::setw(8)
290 << maxCPNumberOfHitsInLC <<
"\t" << std::setw(13) << maxCPId_byEnergy <<
"\t" << std::setw(20)
291 << maxEnergySharedLCandCP <<
"\t" << std::setw(22) << totalCPEnergyOnLayer <<
"\t" << std::setw(22)
292 << energyFractionOfLCinCP <<
"\t" << std::setw(25) << energyFractionOfCPinLC <<
"\n";
295 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"Improved cPOnLayer INFO" << std::endl;
296 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
297 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
298 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
299 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
300 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
301 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
302 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
303 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
304 double tot_energy = 0.;
305 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
306 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
307 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
308 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
309 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
311 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
312 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
313 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
314 << lc.second.first <<
"/" << lc.second.second << std::endl;
319 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"Improved detIdToCaloParticleId_Map INFO" << std::endl;
320 for (
auto const&
cp : detIdToCaloParticleId_Map) {
321 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
322 <<
"For detId: " << (uint32_t)
cp.first
323 <<
" we have found the following connections with CaloParticles:" << std::endl;
324 for (
auto const& cpp :
cp.second) {
325 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
326 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
327 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
334 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
336 std::sort(cpsInLayerCluster[lcId].begin(), cpsInLayerCluster[lcId].
end());
337 auto last =
std::unique(cpsInLayerCluster[lcId].begin(), cpsInLayerCluster[lcId].
end());
338 cpsInLayerCluster[lcId].erase(
last, cpsInLayerCluster[lcId].
end());
339 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
340 unsigned int numberOfHitsInLC = hits_and_fractions.size();
344 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
346 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t CP id : \t"
347 << cpPair.first <<
"\t score \t " << cpPair.second <<
"\n";
353 float invLayerClusterEnergyWeight = 0.f;
354 for (
auto const& haf :
clusters[lcId].hitsAndFractions()) {
355 invLayerClusterEnergyWeight +=
356 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
358 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
359 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
360 DetId rh_detid = hits_and_fractions[
i].first;
361 float rhFraction = hits_and_fractions[
i].second;
363 bool hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
365 auto itcheck =
hitMap_->find(rh_detid);
367 float hitEnergyWeight =
hit->energy() *
hit->energy();
369 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
370 float cpFraction = 0.f;
372 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].begin(),
373 detIdToCaloParticleId_Map[rh_detid].
end(),
375 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
376 cpFraction = findHitIt->fraction;
379 (rhFraction - cpFraction) * (rhFraction - cpFraction) * hitEnergyWeight * invLayerClusterEnergyWeight;
383 if (cpsInLayerCluster[lcId].
empty())
384 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tCP id:\t-1 "
391 for (
const auto& cpId : cPIndices) {
392 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
393 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
394 if (CPNumberOfHits == 0)
397 int lcWithMaxEnergyInCP = -1;
398 float maxEnergyLCinCP = 0.f;
399 float CPenergy = cPOnLayer[cpId][layerId].energy;
400 float CPEnergyFractionInLC = 0.f;
401 for (
auto& lc : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
402 if (lc.second.first > maxEnergyLCinCP) {
403 maxEnergyLCinCP = lc.second.first;
404 lcWithMaxEnergyInCP = lc.first;
408 CPEnergyFractionInLC = maxEnergyLCinCP / CPenergy;
410 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
411 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t"
412 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
413 <<
"lcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyLCinCP\t" << std::setw(20) <<
"CPEnergyFractionInLC"
415 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
416 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
417 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
418 << CPNumberOfHits <<
"\t" << std::setw(18) << lcWithMaxEnergyInCP <<
"\t" << std::setw(15) << maxEnergyLCinCP
419 <<
"\t" << std::setw(20) << CPEnergyFractionInLC <<
"\n";
422 float invCPEnergyWeight = 0.f;
423 for (
auto const& haf : cPOnLayer[cpId][layerId].hits_and_fractions) {
424 invCPEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
426 invCPEnergyWeight = 1.f / invCPEnergyWeight;
427 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
428 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
429 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
431 bool hitWithNoLC =
false;
432 if (cpFraction == 0.
f)
434 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(cp_hitDetId);
435 if (hit_find_in_LC == detIdToLayerClusterId_Map.end())
437 auto itcheck =
hitMap_->find(cp_hitDetId);
439 float hitEnergyWeight =
hit->energy() *
hit->energy();
440 for (
auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
441 unsigned int layerClusterId = lcPair.first;
442 float lcFraction = 0.f;
445 auto findHitIt =
std::find(detIdToLayerClusterId_Map[cp_hitDetId].begin(),
446 detIdToLayerClusterId_Map[cp_hitDetId].
end(),
448 if (findHitIt != detIdToLayerClusterId_Map[cp_hitDetId].
end())
449 lcFraction = findHitIt->fraction;
451 lcPair.second.second +=
452 (lcFraction - cpFraction) * (lcFraction - cpFraction) * hitEnergyWeight * invCPEnergyWeight;
454 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
455 <<
"cpDetId:\t" << (uint32_t)cp_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t"
456 <<
"lcfraction,cpfraction:\t" << lcFraction <<
", " << cpFraction <<
"\t"
457 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
458 <<
"current score:\t" << lcPair.second.second <<
"\t"
459 <<
"invCPEnergyWeight:\t" << invCPEnergyWeight <<
"\n";
464 if (cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore.empty())
465 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl") <<
"CP Id: \t" << cpId <<
"\tLC id:\t-1 "
469 for (
const auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
470 LogDebug(
"LayerClusterAssociatorByEnergyScoreImpl")
471 <<
"CP Id: \t" << cpId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
472 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t"
473 << (lcPair.second.first / CPenergy) <<
"\n";
478 return {cpsInLayerCluster, cPOnLayer};