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(
"LCToCPAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
98 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
99 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
100 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
101 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
102 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
103 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
104 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
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(
"LCToCPAssociatorByEnergyScoreImpl")
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(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
114 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
115 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
116 << lc.second.first <<
"/" << lc.second.second << std::endl;
121 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
122 for (
auto const&
cp : detIdToCaloParticleId_Map) {
123 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
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(
"LCToCPAssociatorByEnergyScoreImpl")
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(
"LCToCPAssociatorByEnergyScoreImpl")
273 << std::setw(10) <<
"LayerId:\t" << std::setw(12) <<
"layerCluster\t" << std::setw(10) <<
"lc energy\t"
274 << std::setw(5) <<
"nhits\t" << std::setw(12) <<
"noise hits\t" << std::setw(22) <<
"maxCPId_byNumberOfHits\t"
275 << std::setw(8) <<
"nhitsCP\t" << std::setw(13) <<
"maxCPId_byEnergy\t" << std::setw(20)
276 <<
"maxEnergySharedLCandCP\t" << std::setw(22) <<
"totalCPEnergyOnLayer\t" << std::setw(22)
277 <<
"energyFractionOfLCinCP\t" << std::setw(25) <<
"energyFractionOfCPinLC\t"
279 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
280 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
281 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
282 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxCPId_byNumberOfHits <<
"\t" << std::setw(8)
283 << maxCPNumberOfHitsInLC <<
"\t" << std::setw(13) << maxCPId_byEnergy <<
"\t" << std::setw(20)
284 << maxEnergySharedLCandCP <<
"\t" << std::setw(22) << totalCPEnergyOnLayer <<
"\t" << std::setw(22)
285 << energyFractionOfLCinCP <<
"\t" << std::setw(25) << energyFractionOfCPinLC <<
"\n";
288 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved cPOnLayer INFO" << std::endl;
289 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
290 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
291 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
292 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
293 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
294 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
295 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
296 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
297 double tot_energy = 0.;
298 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
299 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
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(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
305 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
306 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
307 << lc.second.first <<
"/" << lc.second.second << std::endl;
312 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved detIdToCaloParticleId_Map INFO" << std::endl;
313 for (
auto const&
cp : detIdToCaloParticleId_Map) {
314 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
315 <<
"For detId: " << (uint32_t)
cp.first
316 <<
" we have found the following connections with CaloParticles:" << std::endl;
317 for (
auto const& cpp :
cp.second) {
318 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
319 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
320 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
327 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
329 std::sort(cpsInLayerCluster[lcId].begin(), cpsInLayerCluster[lcId].
end());
330 auto last =
std::unique(cpsInLayerCluster[lcId].begin(), cpsInLayerCluster[lcId].
end());
331 cpsInLayerCluster[lcId].erase(
last, cpsInLayerCluster[lcId].
end());
332 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
333 unsigned int numberOfHitsInLC = hits_and_fractions.size();
337 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
339 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t CP id : \t"
340 << cpPair.first <<
"\t score \t " << cpPair.second <<
"\n";
346 float invLayerClusterEnergyWeight = 0.f;
347 for (
auto const& haf :
clusters[lcId].hitsAndFractions()) {
348 invLayerClusterEnergyWeight +=
349 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
351 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
352 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
353 DetId rh_detid = hits_and_fractions[
i].first;
354 float rhFraction = hits_and_fractions[
i].second;
356 bool hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
358 auto itcheck =
hitMap_->find(rh_detid);
360 float hitEnergyWeight =
hit->energy() *
hit->energy();
362 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
363 float cpFraction = 0.f;
365 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].begin(),
366 detIdToCaloParticleId_Map[rh_detid].
end(),
368 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
369 cpFraction = findHitIt->fraction;
372 (rhFraction - cpFraction) * (rhFraction - cpFraction) * hitEnergyWeight * invLayerClusterEnergyWeight;
376 if (cpsInLayerCluster[lcId].
empty())
377 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tCP id:\t-1 "
378 <<
"\t score \t-1\n";
383 for (
const auto& cpId : cPIndices) {
384 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
385 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
386 if (CPNumberOfHits == 0)
389 int lcWithMaxEnergyInCP = -1;
390 float maxEnergyLCinCP = 0.f;
391 float CPenergy = cPOnLayer[cpId][layerId].energy;
392 float CPEnergyFractionInLC = 0.f;
393 for (
auto& lc : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
394 if (lc.second.first > maxEnergyLCinCP) {
395 maxEnergyLCinCP = lc.second.first;
396 lcWithMaxEnergyInCP = lc.first;
400 CPEnergyFractionInLC = maxEnergyLCinCP / CPenergy;
402 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
403 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t"
404 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
405 <<
"lcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyLCinCP\t" << std::setw(20) <<
"CPEnergyFractionInLC"
407 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
408 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
409 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
410 << CPNumberOfHits <<
"\t" << std::setw(18) << lcWithMaxEnergyInCP <<
"\t" << std::setw(15) << maxEnergyLCinCP
411 <<
"\t" << std::setw(20) << CPEnergyFractionInLC <<
"\n";
414 float invCPEnergyWeight = 0.f;
415 for (
auto const& haf : cPOnLayer[cpId][layerId].hits_and_fractions) {
416 invCPEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
418 invCPEnergyWeight = 1.f / invCPEnergyWeight;
419 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
420 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
421 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
423 bool hitWithNoLC =
false;
424 if (cpFraction == 0.
f)
426 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(cp_hitDetId);
427 if (hit_find_in_LC == detIdToLayerClusterId_Map.end())
429 auto itcheck =
hitMap_->find(cp_hitDetId);
431 float hitEnergyWeight =
hit->energy() *
hit->energy();
432 for (
auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
433 unsigned int layerClusterId = lcPair.first;
434 float lcFraction = 0.f;
437 auto findHitIt =
std::find(detIdToLayerClusterId_Map[cp_hitDetId].begin(),
438 detIdToLayerClusterId_Map[cp_hitDetId].
end(),
440 if (findHitIt != detIdToLayerClusterId_Map[cp_hitDetId].
end())
441 lcFraction = findHitIt->fraction;
443 lcPair.second.second +=
444 (lcFraction - cpFraction) * (lcFraction - cpFraction) * hitEnergyWeight * invCPEnergyWeight;
446 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
447 <<
"cpDetId:\t" << (uint32_t)cp_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t"
448 <<
"lcfraction,cpfraction:\t" << lcFraction <<
", " << cpFraction <<
"\t"
449 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
450 <<
"current score:\t" << lcPair.second.second <<
"\t"
451 <<
"invCPEnergyWeight:\t" << invCPEnergyWeight <<
"\n";
456 if (cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore.empty())
457 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"CP Id: \t" << cpId <<
"\tLC id:\t-1 "
458 <<
"\t score \t-1\n";
460 for (
const auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
461 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
462 <<
"CP Id: \t" << cpId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
463 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t"
464 << (lcPair.second.first / CPenergy) <<
"\n";
469 return {cpsInLayerCluster, cPOnLayer};