35 const auto&
clusters = *cCCH.product();
37 auto nLayerClusters =
clusters.size();
40 std::vector<size_t> cPIndices;
42 auto nCaloParticles = cPIndices.size();
49 cPOnLayer.resize(nCaloParticles);
50 for (
unsigned int i = 0;
i < nCaloParticles; ++
i) {
52 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
53 cPOnLayer[
i][
j].caloParticleId =
i;
54 cPOnLayer[
i][
j].energy = 0.f;
55 cPOnLayer[
i][
j].hits_and_fractions.clear();
65 std::unordered_map<DetId, std::vector<ticl::detIdInfoInCluster>> detIdToCaloParticleId_Map;
66 for (
const auto& cpId : cPIndices) {
68 for (
const auto& it_sc : simClusterRefVector) {
70 std::vector<std::pair<uint32_t, float>> hits_and_fractions;
71 if constexpr (std::is_same_v<HIT, HGCRecHit>)
75 for (
const auto& it_haf : hits_and_fractions) {
76 const auto hitid = (it_haf.first);
77 unsigned int cpLayerId =
recHitTools_->getLayerWithOffset(hitid);
78 if constexpr (std::is_same_v<HIT, HGCRecHit>)
81 const auto itcheck =
hitMap_->find(hitid);
83 if (itcheck !=
hitMap_->end()) {
84 auto hit_find_it = detIdToCaloParticleId_Map.find(hitid);
85 if (hit_find_it == detIdToCaloParticleId_Map.end()) {
86 detIdToCaloParticleId_Map[hitid] = std::vector<ticl::detIdInfoInCluster>();
87 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
89 auto findHitIt =
std::find(detIdToCaloParticleId_Map[hitid].
begin(),
90 detIdToCaloParticleId_Map[hitid].
end(),
92 if (findHitIt != detIdToCaloParticleId_Map[hitid].
end()) {
93 findHitIt->fraction += it_haf.second;
95 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
98 const HIT*
hit =
hits_[itcheck->second];
99 cPOnLayer[cpId][cpLayerId].energy += it_haf.second *
hit->energy();
106 auto& haf = cPOnLayer[cpId][cpLayerId].hits_and_fractions;
107 auto found = std::find_if(
108 std::begin(haf), std::end(haf), [&hitid](
const std::pair<DetId, float>&
v) {
return v.first == hitid; });
109 if (
found != haf.end()) {
110 found->second += it_haf.second;
112 cPOnLayer[cpId][cpLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
120 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
121 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
122 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
123 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
124 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
125 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
126 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
127 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
128 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
129 double tot_energy = 0.;
130 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
133 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" 134 << haf.second <<
"/" << haf.second *
hit->energy() << std::endl;
135 tot_energy += haf.second *
hit->energy();
137 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
138 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
139 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 140 << lc.second.first <<
"/" << lc.second.second << std::endl;
145 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
146 for (
auto const&
cp : detIdToCaloParticleId_Map) {
147 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
148 <<
"For detId: " << (uint32_t)
cp.first
149 <<
" we have found the following connections with CaloParticles:" << std::endl;
151 for (
auto const& cpp :
cp.second) {
152 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
153 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
154 <<
" and energy: " << cpp.fraction *
hit->energy() << std::endl;
160 std::unordered_map<DetId, std::vector<ticl::detIdInfoInCluster>> detIdToLayerClusterId_Map;
166 cpsInLayerCluster.resize(nLayerClusters);
168 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
169 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
170 unsigned int numberOfHitsInLC = hits_and_fractions.size();
171 const auto firstHitDetId = hits_and_fractions[0].first;
172 unsigned int lcLayerId =
recHitTools_->getLayerWithOffset(firstHitDetId);
173 if constexpr (std::is_same_v<HIT, HGCRecHit>)
175 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
176 const auto rh_detid = hits_and_fractions[hitId].first;
177 const auto rhFraction = hits_and_fractions[hitId].second;
179 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
180 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
181 detIdToLayerClusterId_Map[rh_detid] = std::vector<ticl::detIdInfoInCluster>();
183 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
185 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
187 if (hit_find_in_CP != detIdToCaloParticleId_Map.end()) {
188 const auto itcheck =
hitMap_->find(rh_detid);
189 const HIT*
hit =
hits_[itcheck->second];
190 for (
auto&
h : hit_find_in_CP->second) {
191 cPOnLayer[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
h.fraction *
hit->energy();
192 cpsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
199 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
200 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
201 unsigned int numberOfHitsInLC = hits_and_fractions.size();
202 const auto firstHitDetId = hits_and_fractions[0].first;
203 int lcLayerId =
recHitTools_->getLayerWithOffset(firstHitDetId);
204 if constexpr (std::is_same_v<HIT, HGCRecHit>)
214 std::vector<int> hitsToCaloParticleId(numberOfHitsInLC);
216 int maxCPId_byNumberOfHits = -1;
218 unsigned int maxCPNumberOfHitsInLC = 0;
220 int maxCPId_byEnergy = -1;
222 float maxEnergySharedLCandCP = 0.f;
224 float energyFractionOfLCinCP = 0.f;
226 float energyFractionOfCPinLC = 0.f;
227 std::unordered_map<unsigned, unsigned> occurrencesCPinLC;
228 unsigned int numberOfNoiseHitsInLC = 0;
229 std::unordered_map<unsigned, float> CPEnergyInLC;
230 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
231 const auto rh_detid = hits_and_fractions[hitId].first;
232 const auto rhFraction = hits_and_fractions[hitId].second;
234 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
242 if (rhFraction == 0.) {
243 hitsToCaloParticleId[hitId] = -2;
245 if (hit_find_in_CP == detIdToCaloParticleId_Map.end()) {
246 hitsToCaloParticleId[hitId] -= 1;
249 auto maxCPEnergyInLC = 0.f;
251 for (
auto&
h : hit_find_in_CP->second) {
252 CPEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
255 if (CPEnergyInLC[
h.clusterId] > maxCPEnergyInLC) {
256 maxCPEnergyInLC = CPEnergyInLC[
h.clusterId];
257 maxCPId =
h.clusterId;
260 hitsToCaloParticleId[hitId] = maxCPId;
263 for (
const auto&
c : hitsToCaloParticleId) {
265 numberOfNoiseHitsInLC++;
267 occurrencesCPinLC[
c]++;
271 for (
const auto&
c : occurrencesCPinLC) {
272 if (
c.second > maxCPNumberOfHitsInLC) {
273 maxCPId_byNumberOfHits =
c.first;
274 maxCPNumberOfHitsInLC =
c.second;
278 for (
const auto&
c : CPEnergyInLC) {
279 if (
c.second > maxEnergySharedLCandCP) {
280 maxCPId_byEnergy =
c.first;
281 maxEnergySharedLCandCP =
c.second;
285 float totalCPEnergyOnLayer = 0.f;
286 if (maxCPId_byEnergy >= 0) {
287 totalCPEnergyOnLayer = cPOnLayer[maxCPId_byEnergy][lcLayerId].energy;
288 energyFractionOfCPinLC = maxEnergySharedLCandCP / totalCPEnergyOnLayer;
290 energyFractionOfLCinCP = maxEnergySharedLCandCP /
clusters[lcId].energy();
294 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
295 << std::setw(10) <<
"LayerId:\t" << std::setw(12) <<
"layerCluster\t" << std::setw(10) <<
"lc energy\t" 296 << std::setw(5) <<
"nhits\t" << std::setw(12) <<
"noise hits\t" << std::setw(22) <<
"maxCPId_byNumberOfHits\t" 297 << std::setw(8) <<
"nhitsCP\t" << std::setw(13) <<
"maxCPId_byEnergy\t" << std::setw(20)
298 <<
"maxEnergySharedLCandCP\t" << std::setw(22) <<
"totalCPEnergyOnLayer\t" << std::setw(22)
299 <<
"energyFractionOfLCinCP\t" << std::setw(25) <<
"energyFractionOfCPinLC\t" 301 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
302 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
303 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
304 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxCPId_byNumberOfHits <<
"\t" << std::setw(8)
305 << maxCPNumberOfHitsInLC <<
"\t" << std::setw(13) << maxCPId_byEnergy <<
"\t" << std::setw(20)
306 << maxEnergySharedLCandCP <<
"\t" << std::setw(22) << totalCPEnergyOnLayer <<
"\t" << std::setw(22)
307 << energyFractionOfLCinCP <<
"\t" << std::setw(25) << energyFractionOfCPinLC <<
"\n";
310 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved cPOnLayer INFO" << std::endl;
311 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
312 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
313 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
314 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
315 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
316 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
317 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
318 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
319 double tot_energy = 0.;
320 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
322 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" 323 << haf.second <<
"/" << haf.second *
hit->energy() << std::endl;
324 tot_energy += haf.second *
hit->energy();
326 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
327 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
328 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 329 << lc.second.first <<
"/" << lc.second.second << std::endl;
334 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved detIdToCaloParticleId_Map INFO" << std::endl;
335 for (
auto const&
cp : detIdToCaloParticleId_Map) {
336 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
337 <<
"For detId: " << (uint32_t)
cp.first
338 <<
" we have found the following connections with CaloParticles:" << std::endl;
340 for (
auto const& cpp :
cp.second) {
341 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
342 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
343 <<
" and energy: " << cpp.fraction *
hit->energy() << std::endl;
350 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
354 cpsInLayerCluster[lcId].erase(
last, cpsInLayerCluster[lcId].
end());
355 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
356 unsigned int numberOfHitsInLC = hits_and_fractions.size();
360 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
362 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t CP id : \t" 363 << cpPair.first <<
"\t score \t " << cpPair.second <<
"\n";
370 float invLayerClusterEnergyWeight = 0.f;
371 for (
auto const& haf : hits_and_fractions) {
373 invLayerClusterEnergyWeight += (haf.second *
hit->energy()) * (haf.second *
hit->energy());
375 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
376 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
377 DetId rh_detid = hits_and_fractions[
i].first;
378 float rhFraction = hits_and_fractions[
i].second;
380 bool hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
382 auto itcheck =
hitMap_->find(rh_detid);
383 const HIT*
hit =
hits_[itcheck->second];
384 float hitEnergyWeight =
hit->energy() *
hit->energy();
386 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
387 float cpFraction = 0.f;
389 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].
begin(),
390 detIdToCaloParticleId_Map[rh_detid].
end(),
392 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
393 cpFraction = findHitIt->fraction;
396 invLayerClusterEnergyWeight;
400 if (cpsInLayerCluster[lcId].
empty())
401 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tCP id:\t-1 " 402 <<
"\t score \t-1\n";
407 for (
const auto& cpId : cPIndices) {
408 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
409 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
410 if (CPNumberOfHits == 0)
413 int lcWithMaxEnergyInCP = -1;
414 float maxEnergyLCinCP = 0.f;
415 float CPenergy = cPOnLayer[cpId][layerId].energy;
416 float CPEnergyFractionInLC = 0.f;
417 for (
auto& lc : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
418 if (lc.second.first > maxEnergyLCinCP) {
419 maxEnergyLCinCP = lc.second.first;
420 lcWithMaxEnergyInCP = lc.first;
424 CPEnergyFractionInLC = maxEnergyLCinCP / CPenergy;
426 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
427 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t" 428 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
429 <<
"lcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyLCinCP\t" << std::setw(20) <<
"CPEnergyFractionInLC" 431 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
432 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
433 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
434 << CPNumberOfHits <<
"\t" << std::setw(18) << lcWithMaxEnergyInCP <<
"\t" << std::setw(15) << maxEnergyLCinCP
435 <<
"\t" << std::setw(20) << CPEnergyFractionInLC <<
"\n";
438 float invCPEnergyWeight = 0.f;
439 for (
auto const& haf : cPOnLayer[cpId][layerId].hits_and_fractions) {
441 invCPEnergyWeight +=
std::pow(haf.second *
hit->energy(), 2);
443 invCPEnergyWeight = 1.f / invCPEnergyWeight;
444 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
445 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
446 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
448 bool hitWithNoLC =
false;
449 if (cpFraction == 0.
f)
451 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(cp_hitDetId);
452 if (hit_find_in_LC == detIdToLayerClusterId_Map.end())
454 auto itcheck =
hitMap_->find(cp_hitDetId);
455 const HIT*
hit =
hits_[itcheck->second];
456 float hitEnergyWeight =
hit->energy() *
hit->energy();
457 for (
auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
458 unsigned int layerClusterId = lcPair.first;
459 float lcFraction = 0.f;
462 auto findHitIt =
std::find(detIdToLayerClusterId_Map[cp_hitDetId].
begin(),
463 detIdToLayerClusterId_Map[cp_hitDetId].
end(),
465 if (findHitIt != detIdToLayerClusterId_Map[cp_hitDetId].
end())
466 lcFraction = findHitIt->fraction;
469 hitEnergyWeight * invCPEnergyWeight;
471 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
472 <<
"cpDetId:\t" << (uint32_t)cp_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t" 473 <<
"lcfraction,cpfraction:\t" << lcFraction <<
", " << cpFraction <<
"\t" 474 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t" 475 <<
"current score:\t" << lcPair.second.second <<
"\t" 476 <<
"invCPEnergyWeight:\t" << invCPEnergyWeight <<
"\n";
481 if (cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore.empty())
482 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"CP Id: \t" << cpId <<
"\tLC id:\t-1 " 483 <<
"\t score \t-1\n";
485 for (
const auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
486 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
487 <<
"CP Id: \t" << cpId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
488 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t" 489 << (lcPair.second.first / CPenergy) <<
"\n";
495 return {cpsInLayerCluster, cPOnLayer};
const std::unordered_map< DetId, const unsigned int > * hitMap_
std::vector< std::pair< uint32_t, float > > endcap_hits_and_fractions() const
Returns list of rechit IDs and fractions in the endcap for this SimCluster.
T const * product() const
std::vector< const HIT * > hits_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
std::vector< std::vector< std::pair< unsigned int, float > > > layerClusterToCaloParticle
std::vector< std::vector< ticl::caloParticleOnLayer > > caloParticleToLayerCluster
Monte Carlo truth information used for tracking validation.
def unique(seq, keepstr=True)
std::vector< std::pair< uint32_t, float > > barrel_hits_and_fractions() const
Returns list of rechit IDs and fractions in the barrel for this SimCluster.
const bool hardScatterOnly_
std::shared_ptr< hgcal::RecHitTools > recHitTools_
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Power< A, B >::type pow(const A &a, const B &b)
def cp(fromDir, toDir, listOfFiles, overwrite=False, smallList=False)