24 const auto&
clusters = *cCCH.product();
26 auto nLayerClusters =
clusters.size();
29 std::vector<size_t> cPIndices;
31 auto nCaloParticles = cPIndices.size();
38 cPOnLayer.resize(nCaloParticles);
39 for (
unsigned int i = 0;
i < nCaloParticles; ++
i) {
41 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
42 cPOnLayer[
i][
j].caloParticleId =
i;
43 cPOnLayer[
i][
j].energy = 0.f;
44 cPOnLayer[
i][
j].hits_and_fractions.clear();
54 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToCaloParticleId_Map;
55 for (
const auto& cpId : cPIndices) {
57 for (
const auto& it_sc : simClusterRefVector) {
60 for (
const auto& it_haf : hits_and_fractions) {
61 const auto hitid = (it_haf.first);
62 const auto cpLayerId =
64 const auto itcheck =
hitMap_->find(hitid);
65 if (itcheck !=
hitMap_->end()) {
66 auto hit_find_it = detIdToCaloParticleId_Map.find(hitid);
67 if (hit_find_it == detIdToCaloParticleId_Map.end()) {
68 detIdToCaloParticleId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
69 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
71 auto findHitIt =
std::find(detIdToCaloParticleId_Map[hitid].
begin(),
72 detIdToCaloParticleId_Map[hitid].
end(),
74 if (findHitIt != detIdToCaloParticleId_Map[hitid].
end()) {
75 findHitIt->fraction += it_haf.second;
77 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
81 cPOnLayer[cpId][cpLayerId].energy += it_haf.second *
hit->energy();
88 auto& haf = cPOnLayer[cpId][cpLayerId].hits_and_fractions;
89 auto found = std::find_if(
90 std::begin(haf), std::end(haf), [&hitid](
const std::pair<DetId, float>&
v) {
return v.first == hitid; });
91 if (
found != haf.end()) {
92 found->second += it_haf.second;
94 cPOnLayer[cpId][cpLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
102 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
103 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
104 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
105 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
106 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
107 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
108 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
109 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
110 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
111 double tot_energy = 0.;
112 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
113 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
114 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/" 115 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
116 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
118 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
119 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
120 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 121 << lc.second.first <<
"/" << lc.second.second << std::endl;
126 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
127 for (
auto const&
cp : detIdToCaloParticleId_Map) {
128 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
129 <<
"For detId: " << (uint32_t)
cp.first
130 <<
" we have found the following connections with CaloParticles:" << std::endl;
131 for (
auto const& cpp :
cp.second) {
132 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
133 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
134 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
140 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
146 cpsInLayerCluster.resize(nLayerClusters);
148 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
149 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
150 unsigned int numberOfHitsInLC = hits_and_fractions.size();
151 const auto firstHitDetId = hits_and_fractions[0].first;
155 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
156 const auto rh_detid = hits_and_fractions[hitId].first;
157 const auto rhFraction = hits_and_fractions[hitId].second;
159 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
160 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
161 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
163 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
165 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
167 if (hit_find_in_CP != detIdToCaloParticleId_Map.end()) {
168 const auto itcheck =
hitMap_->find(rh_detid);
170 for (
auto&
h : hit_find_in_CP->second) {
171 cPOnLayer[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
h.fraction *
hit->energy();
172 cpsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
179 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
180 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
181 unsigned int numberOfHitsInLC = hits_and_fractions.size();
182 const auto firstHitDetId = hits_and_fractions[0].first;
194 std::vector<int> hitsToCaloParticleId(numberOfHitsInLC);
196 int maxCPId_byNumberOfHits = -1;
198 unsigned int maxCPNumberOfHitsInLC = 0;
200 int maxCPId_byEnergy = -1;
202 float maxEnergySharedLCandCP = 0.f;
204 float energyFractionOfLCinCP = 0.f;
206 float energyFractionOfCPinLC = 0.f;
207 std::unordered_map<unsigned, unsigned> occurrencesCPinLC;
208 unsigned int numberOfNoiseHitsInLC = 0;
209 std::unordered_map<unsigned, float> CPEnergyInLC;
211 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
212 const auto rh_detid = hits_and_fractions[hitId].first;
213 const auto rhFraction = hits_and_fractions[hitId].second;
215 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
223 if (rhFraction == 0.) {
224 hitsToCaloParticleId[hitId] = -2;
226 if (hit_find_in_CP == detIdToCaloParticleId_Map.end()) {
227 hitsToCaloParticleId[hitId] -= 1;
229 const auto itcheck =
hitMap_->find(rh_detid);
231 auto maxCPEnergyInLC = 0.f;
233 for (
auto&
h : hit_find_in_CP->second) {
234 CPEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
237 if (CPEnergyInLC[
h.clusterId] > maxCPEnergyInLC) {
238 maxCPEnergyInLC = CPEnergyInLC[
h.clusterId];
239 maxCPId =
h.clusterId;
242 hitsToCaloParticleId[hitId] = maxCPId;
246 for (
const auto&
c : hitsToCaloParticleId) {
248 numberOfNoiseHitsInLC++;
250 occurrencesCPinLC[
c]++;
254 for (
const auto&
c : occurrencesCPinLC) {
255 if (
c.second > maxCPNumberOfHitsInLC) {
256 maxCPId_byNumberOfHits =
c.first;
257 maxCPNumberOfHitsInLC =
c.second;
261 for (
const auto&
c : CPEnergyInLC) {
262 if (
c.second > maxEnergySharedLCandCP) {
263 maxCPId_byEnergy =
c.first;
264 maxEnergySharedLCandCP =
c.second;
268 float totalCPEnergyOnLayer = 0.f;
269 if (maxCPId_byEnergy >= 0) {
270 totalCPEnergyOnLayer = cPOnLayer[maxCPId_byEnergy][lcLayerId].energy;
271 energyFractionOfCPinLC = maxEnergySharedLCandCP / totalCPEnergyOnLayer;
273 energyFractionOfLCinCP = maxEnergySharedLCandCP /
clusters[lcId].energy();
277 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
278 << std::setw(10) <<
"LayerId:\t" << std::setw(12) <<
"layerCluster\t" << std::setw(10) <<
"lc energy\t" 279 << std::setw(5) <<
"nhits\t" << std::setw(12) <<
"noise hits\t" << std::setw(22) <<
"maxCPId_byNumberOfHits\t" 280 << std::setw(8) <<
"nhitsCP\t" << std::setw(13) <<
"maxCPId_byEnergy\t" << std::setw(20)
281 <<
"maxEnergySharedLCandCP\t" << std::setw(22) <<
"totalCPEnergyOnLayer\t" << std::setw(22)
282 <<
"energyFractionOfLCinCP\t" << std::setw(25) <<
"energyFractionOfCPinLC\t" 284 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
285 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
286 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
287 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxCPId_byNumberOfHits <<
"\t" << std::setw(8)
288 << maxCPNumberOfHitsInLC <<
"\t" << std::setw(13) << maxCPId_byEnergy <<
"\t" << std::setw(20)
289 << maxEnergySharedLCandCP <<
"\t" << std::setw(22) << totalCPEnergyOnLayer <<
"\t" << std::setw(22)
290 << energyFractionOfLCinCP <<
"\t" << std::setw(25) << energyFractionOfCPinLC <<
"\n";
293 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved cPOnLayer INFO" << std::endl;
294 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
295 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
296 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
297 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
298 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
299 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
300 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
301 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
302 double tot_energy = 0.;
303 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
304 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
305 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/" 306 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
307 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
309 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
310 for (
auto const& lc : cPOnLayer[
cp][cpp].layerClusterIdToEnergyAndScore) {
311 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 312 << lc.second.first <<
"/" << lc.second.second << std::endl;
317 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"Improved detIdToCaloParticleId_Map INFO" << std::endl;
318 for (
auto const&
cp : detIdToCaloParticleId_Map) {
319 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
320 <<
"For detId: " << (uint32_t)
cp.first
321 <<
" we have found the following connections with CaloParticles:" << std::endl;
322 for (
auto const& cpp :
cp.second) {
323 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
324 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
325 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
332 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
336 cpsInLayerCluster[lcId].erase(
last, cpsInLayerCluster[lcId].
end());
337 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
338 unsigned int numberOfHitsInLC = hits_and_fractions.size();
342 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
344 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t CP id : \t" 345 << cpPair.first <<
"\t score \t " << cpPair.second <<
"\n";
352 float invLayerClusterEnergyWeight = 0.f;
353 for (
auto const& haf : hits_and_fractions) {
354 invLayerClusterEnergyWeight +=
355 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
357 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
358 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
359 DetId rh_detid = hits_and_fractions[
i].first;
360 float rhFraction = hits_and_fractions[
i].second;
362 bool hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
364 auto itcheck =
hitMap_->find(rh_detid);
366 float hitEnergyWeight =
hit->energy() *
hit->energy();
368 for (
auto& cpPair : cpsInLayerCluster[lcId]) {
369 float cpFraction = 0.f;
371 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].
begin(),
372 detIdToCaloParticleId_Map[rh_detid].
end(),
374 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
375 cpFraction = findHitIt->fraction;
378 invLayerClusterEnergyWeight;
382 if (cpsInLayerCluster[lcId].
empty())
383 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tCP id:\t-1 " 384 <<
"\t score \t-1\n";
389 for (
const auto& cpId : cPIndices) {
390 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
391 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
392 if (CPNumberOfHits == 0)
395 int lcWithMaxEnergyInCP = -1;
396 float maxEnergyLCinCP = 0.f;
397 float CPenergy = cPOnLayer[cpId][layerId].energy;
398 float CPEnergyFractionInLC = 0.f;
399 for (
auto& lc : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
400 if (lc.second.first > maxEnergyLCinCP) {
401 maxEnergyLCinCP = lc.second.first;
402 lcWithMaxEnergyInCP = lc.first;
406 CPEnergyFractionInLC = maxEnergyLCinCP / CPenergy;
408 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
409 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t" 410 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
411 <<
"lcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyLCinCP\t" << std::setw(20) <<
"CPEnergyFractionInLC" 413 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
414 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
415 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
416 << CPNumberOfHits <<
"\t" << std::setw(18) << lcWithMaxEnergyInCP <<
"\t" << std::setw(15) << maxEnergyLCinCP
417 <<
"\t" << std::setw(20) << CPEnergyFractionInLC <<
"\n";
420 float invCPEnergyWeight = 0.f;
421 for (
auto const& haf : cPOnLayer[cpId][layerId].hits_and_fractions) {
422 invCPEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
424 invCPEnergyWeight = 1.f / invCPEnergyWeight;
425 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
426 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
427 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
429 bool hitWithNoLC =
false;
430 if (cpFraction == 0.
f)
432 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(cp_hitDetId);
433 if (hit_find_in_LC == detIdToLayerClusterId_Map.end())
435 auto itcheck =
hitMap_->find(cp_hitDetId);
437 float hitEnergyWeight =
hit->energy() *
hit->energy();
438 for (
auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
439 unsigned int layerClusterId = lcPair.first;
440 float lcFraction = 0.f;
443 auto findHitIt =
std::find(detIdToLayerClusterId_Map[cp_hitDetId].
begin(),
444 detIdToLayerClusterId_Map[cp_hitDetId].
end(),
446 if (findHitIt != detIdToLayerClusterId_Map[cp_hitDetId].
end())
447 lcFraction = findHitIt->fraction;
450 hitEnergyWeight * invCPEnergyWeight;
452 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
453 <<
"cpDetId:\t" << (uint32_t)cp_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t" 454 <<
"lcfraction,cpfraction:\t" << lcFraction <<
", " << cpFraction <<
"\t" 455 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t" 456 <<
"current score:\t" << lcPair.second.second <<
"\t" 457 <<
"invCPEnergyWeight:\t" << invCPEnergyWeight <<
"\n";
462 if (cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore.empty())
463 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl") <<
"CP Id: \t" << cpId <<
"\tLC id:\t-1 " 464 <<
"\t score \t-1\n";
466 for (
const auto& lcPair : cPOnLayer[cpId][layerId].layerClusterIdToEnergyAndScore) {
467 LogDebug(
"LCToCPAssociatorByEnergyScoreImpl")
468 <<
"CP Id: \t" << cpId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
469 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t" 470 << (lcPair.second.first / CPenergy) <<
"\n";
476 return {cpsInLayerCluster, cPOnLayer};
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
T const * product() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
std::vector< std::pair< uint32_t, float > > hits_and_fractions() const
Returns list of rechit IDs and fractions for this SimCluster.
const bool hardScatterOnly_
Monte Carlo truth information used for tracking validation.
def unique(seq, keepstr=True)
const std::unordered_map< DetId, const HGCRecHit * > * hitMap_
std::vector< std::vector< std::pair< unsigned int, float > > > layerClusterToCaloParticle
std::shared_ptr< hgcal::RecHitTools > recHitTools_
std::vector< std::vector< hgcal::caloParticleOnLayer > > caloParticleToLayerCluster
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)