28 const auto&
clusters = *cCCH.product();
29 const auto& simClusters = *sCCH.
product();
30 auto nLayerClusters =
clusters.size();
34 auto nSimClusters = simClusters.size();
35 std::vector<size_t> sCIndices;
36 for (
unsigned int scId = 0; scId < nSimClusters; ++scId) {
38 simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() != 0)) {
39 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
40 <<
"Excluding SimCluster from event: " << simClusters[scId].g4Tracks()[0].eventId().event()
41 <<
" with BX: " << simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() << std::endl;
44 sCIndices.emplace_back(scId);
46 nSimClusters = sCIndices.size();
53 lcsInSimCluster.resize(nSimClusters);
54 for (
unsigned int i = 0;
i < nSimClusters; ++
i) {
55 lcsInSimCluster[
i].resize(
layers_ * 2);
56 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
57 lcsInSimCluster[
i][
j].simClusterId =
i;
58 lcsInSimCluster[
i][
j].energy = 0.f;
59 lcsInSimCluster[
i][
j].hits_and_fractions.clear();
68 std::unordered_map<DetId, std::vector<ticl::detIdInfoInCluster>> detIdToSimClusterId_Map;
69 for (
const auto& scId : sCIndices) {
70 std::vector<std::pair<uint32_t, float>> hits_and_fractions = simClusters[scId].hits_and_fractions();
71 if constexpr (std::is_same_v<HIT, HGCRecHit>)
72 hits_and_fractions = simClusters[scId].endcap_hits_and_fractions();
74 hits_and_fractions = simClusters[scId].barrel_hits_and_fractions();
75 for (
const auto& it_haf : hits_and_fractions) {
76 const auto hitid = (it_haf.first);
78 if constexpr (std::is_same_v<HIT, HGCRecHit>)
80 const auto itcheck =
hitMap_->find(hitid);
81 if (itcheck !=
hitMap_->end()) {
82 auto hit_find_it = detIdToSimClusterId_Map.find(hitid);
83 if (hit_find_it == detIdToSimClusterId_Map.end()) {
84 detIdToSimClusterId_Map[hitid] = std::vector<ticl::detIdInfoInCluster>();
86 detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second);
87 const HIT*
hit =
hits_[itcheck->second];
88 lcsInSimCluster[scId][scLayerId].energy += it_haf.second *
hit->energy();
89 lcsInSimCluster[scId][scLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
95 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
96 <<
"lcsInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl;
97 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" # of clusters : " << nLayerClusters << std::endl;
98 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
99 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
100 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
101 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
102 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
103 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
104 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
105 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
106 double tot_energy = 0.;
107 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
109 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" 110 << haf.second <<
"/" << haf.second *
hit->energy() << std::endl;
111 tot_energy += haf.second *
hit->energy();
113 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
114 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
115 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 116 << lc.second.first <<
"/" << lc.second.second << std::endl;
121 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"detIdToSimClusterId_Map INFO" << std::endl;
122 for (
auto const& sc : detIdToSimClusterId_Map) {
123 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
124 <<
"For detId: " << (uint32_t)sc.first
125 <<
" we have found the following connections with SimClusters:" << std::endl;
131 for (
auto const& sclu : sc.second) {
132 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
133 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
134 <<
" and energy: " << sclu.fraction *
hit->energy() << std::endl;
142 std::unordered_map<DetId, std::vector<ticl::detIdInfoInCluster>> detIdToLayerClusterId_Map;
148 scsInLayerCluster.resize(nLayerClusters);
150 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
151 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
152 unsigned int numberOfHitsInLC = hits_and_fractions.size();
153 const auto firstHitDetId = hits_and_fractions[0].first;
155 if constexpr (std::is_same_v<HIT, HGCRecHit>)
157 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
158 const auto rh_detid = hits_and_fractions[hitId].first;
159 const auto rhFraction = hits_and_fractions[hitId].second;
161 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
162 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
163 detIdToLayerClusterId_Map[rh_detid] = std::vector<ticl::detIdInfoInCluster>();
165 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
167 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
169 if (hit_find_in_SC != detIdToSimClusterId_Map.end()) {
170 const auto itcheck =
hitMap_->find(rh_detid);
171 const HIT*
hit =
hits_[itcheck->second];
175 for (
auto&
h : hit_find_in_SC->second) {
178 lcsInSimCluster[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
179 h.fraction *
hit->energy();
181 scsInLayerCluster[lcId].emplace_back(
h.clusterId, 0.f);
188 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
189 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
190 unsigned int numberOfHitsInLC = hits_and_fractions.size();
191 const auto firstHitDetId = hits_and_fractions[0].first;
193 if constexpr (std::is_same_v<HIT, HGCRecHit>)
203 std::vector<int> hitsToSimClusterId(numberOfHitsInLC);
205 int maxSCId_byNumberOfHits = -1;
207 unsigned int maxSCNumberOfHitsInLC = 0;
209 int maxSCId_byEnergy = -1;
211 float maxEnergySharedLCandSC = 0.f;
213 float energyFractionOfLCinSC = 0.f;
215 float energyFractionOfSCinLC = 0.f;
216 std::unordered_map<unsigned, unsigned> occurrencesSCinLC;
217 unsigned int numberOfNoiseHitsInLC = 0;
218 std::unordered_map<unsigned, float> SCEnergyInLC;
220 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
221 const auto rh_detid = hits_and_fractions[hitId].first;
222 const auto rhFraction = hits_and_fractions[hitId].second;
224 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
232 if (rhFraction == 0.) {
233 hitsToSimClusterId[hitId] = -2;
236 if (hit_find_in_SC == detIdToSimClusterId_Map.end()) {
237 hitsToSimClusterId[hitId] -= 1;
239 const auto itcheck =
hitMap_->find(rh_detid);
240 const HIT*
hit =
hits_[itcheck->second];
241 auto maxSCEnergyInLC = 0.f;
244 for (
auto&
h : hit_find_in_SC->second) {
245 SCEnergyInLC[
h.clusterId] +=
h.fraction *
hit->energy();
248 if (SCEnergyInLC[
h.clusterId] > maxSCEnergyInLC) {
249 maxSCEnergyInLC = SCEnergyInLC[
h.clusterId];
250 maxSCId =
h.clusterId;
253 hitsToSimClusterId[hitId] = maxSCId;
257 for (
const auto&
c : hitsToSimClusterId) {
259 numberOfNoiseHitsInLC++;
261 occurrencesSCinLC[
c]++;
265 for (
const auto&
c : occurrencesSCinLC) {
266 if (
c.second > maxSCNumberOfHitsInLC) {
267 maxSCId_byNumberOfHits =
c.first;
268 maxSCNumberOfHitsInLC =
c.second;
272 for (
const auto&
c : SCEnergyInLC) {
273 if (
c.second > maxEnergySharedLCandSC) {
274 maxSCId_byEnergy =
c.first;
275 maxEnergySharedLCandSC =
c.second;
279 float totalSCEnergyOnLayer = 0.f;
280 if (maxSCId_byEnergy >= 0) {
281 totalSCEnergyOnLayer = lcsInSimCluster[maxSCId_byEnergy][lcLayerId].energy;
282 energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer;
284 energyFractionOfLCinSC = maxEnergySharedLCandSC /
clusters[lcId].energy();
288 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") << std::setw(10) <<
"LayerId:" 289 <<
"\t" << std::setw(12) <<
"layerCluster" 290 <<
"\t" << std::setw(10) <<
"lc energy" 291 <<
"\t" << std::setw(5) <<
"nhits" 292 <<
"\t" << std::setw(12) <<
"noise hits" 293 <<
"\t" << std::setw(22) <<
"maxSCId_byNumberOfHits" 294 <<
"\t" << std::setw(8) <<
"nhitsSC" 295 <<
"\t" << std::setw(13) <<
"maxSCId_byEnergy" 296 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandSC" 297 <<
"\t" << std::setw(22) <<
"totalSCEnergyOnLayer" 298 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinSC" 299 <<
"\t" << std::setw(25) <<
"energyFractionOfSCinLC" 302 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
303 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
304 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
305 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSCId_byNumberOfHits <<
"\t" << std::setw(8)
306 << maxSCNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSCId_byEnergy <<
"\t" << std::setw(20)
307 << maxEnergySharedLCandSC <<
"\t" << std::setw(22) << totalSCEnergyOnLayer <<
"\t" << std::setw(22)
308 << energyFractionOfLCinSC <<
"\t" << std::setw(25) << energyFractionOfSCinLC <<
"\n";
311 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
312 <<
"Improved lcsInSimCluster INFO (Now containing the linked layer clusters id and energy - score still empty)" 314 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
315 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
316 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
317 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
318 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
319 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
320 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
321 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
322 double tot_energy = 0.;
323 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
325 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" 326 << haf.second <<
"/" << haf.second *
hit->energy() << std::endl;
327 tot_energy += haf.second *
hit->energy();
329 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
330 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
331 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/" 332 << lc.second.first <<
"/" << lc.second.second << std::endl;
337 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"Improved detIdToSimClusterId_Map INFO" << std::endl;
338 for (
auto const& sc : detIdToSimClusterId_Map) {
340 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
341 <<
"For detId: " << (uint32_t)sc.first
342 <<
" we have found the following connections with SimClusters:" << std::endl;
343 for (
auto const& sclu : sc.second) {
344 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
345 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
346 <<
" and energy: " << sclu.fraction *
hit->energy() << std::endl;
353 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
358 scsInLayerCluster[lcId].erase(
last, scsInLayerCluster[lcId].
end());
359 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
360 unsigned int numberOfHitsInLC = hits_and_fractions.size();
364 for (
auto& scPair : scsInLayerCluster[lcId]) {
366 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t SC id : \t" 367 << scPair.first <<
"\t score \t " << scPair.second <<
"\n";
374 float invLayerClusterEnergyWeight = 0.f;
375 for (
auto const& haf : hits_and_fractions) {
377 invLayerClusterEnergyWeight += (haf.second *
hit->energy()) * (haf.second *
hit->energy());
379 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
380 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
381 DetId rh_detid = hits_and_fractions[
i].first;
382 float rhFraction = hits_and_fractions[
i].second;
384 bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end());
386 auto itcheck =
hitMap_->find(rh_detid);
387 const HIT*
hit =
hits_[itcheck->second];
388 float hitEnergyWeight =
hit->energy() *
hit->energy();
390 for (
auto& scPair : scsInLayerCluster[lcId]) {
391 float scFraction = 0.f;
393 auto findHitIt =
std::find(detIdToSimClusterId_Map[rh_detid].
begin(),
394 detIdToSimClusterId_Map[rh_detid].
end(),
396 if (findHitIt != detIdToSimClusterId_Map[rh_detid].
end())
397 scFraction = findHitIt->fraction;
400 invLayerClusterEnergyWeight;
402 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
403 <<
"rh_detid:\t" << (uint32_t)rh_detid <<
"\tlayerClusterId:\t" << lcId <<
"\t" 404 <<
"rhfraction,scfraction:\t" << rhFraction <<
", " << scFraction <<
"\t" 405 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t" 406 <<
"current score:\t" << scPair.second <<
"\t" 407 <<
"invLayerClusterEnergyWeight:\t" << invLayerClusterEnergyWeight <<
"\n";
412 if (scsInLayerCluster[lcId].
empty())
413 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tSC id:\t-1 " 420 for (
const auto& scId : sCIndices) {
421 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
422 unsigned int SCNumberOfHits = lcsInSimCluster[scId][layerId].hits_and_fractions.size();
423 if (SCNumberOfHits == 0)
426 int lcWithMaxEnergyInSC = -1;
428 float maxEnergyLCinSC = 0.f;
430 float SCenergy = lcsInSimCluster[scId][layerId].energy;
432 float SCEnergyFractionInLC = 0.f;
433 for (
auto& lc : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
434 if (lc.second.first > maxEnergyLCinSC) {
435 maxEnergyLCinSC = lc.second.first;
436 lcWithMaxEnergyInSC = lc.first;
440 SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy;
442 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
443 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"simcluster\t" << std::setw(15) <<
"sc total energy\t" 444 << std::setw(15) <<
"scEnergyOnLayer\t" << std::setw(14) <<
"SCNhitsOnLayer\t" << std::setw(18)
445 <<
"lcWithMaxEnergyInSC\t" << std::setw(15) <<
"maxEnergyLCinSC\t" << std::setw(20) <<
"SCEnergyFractionInLC" 447 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
448 << std::setw(8) << layerId <<
"\t" << std::setw(12) << scId <<
"\t" << std::setw(15)
449 << simClusters[scId].energy() <<
"\t" << std::setw(15) << SCenergy <<
"\t" << std::setw(14) << SCNumberOfHits
450 <<
"\t" << std::setw(18) << lcWithMaxEnergyInSC <<
"\t" << std::setw(15) << maxEnergyLCinSC <<
"\t" 451 << std::setw(20) << SCEnergyFractionInLC <<
"\n";
454 float invSCEnergyWeight = 0.f;
455 for (
auto const& haf : lcsInSimCluster[scId][layerId].hits_and_fractions) {
457 invSCEnergyWeight +=
std::pow(haf.second *
hit->energy(), 2);
459 invSCEnergyWeight = 1.f / invSCEnergyWeight;
460 for (
unsigned int i = 0;
i < SCNumberOfHits; ++
i) {
461 auto& sc_hitDetId = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].first;
462 auto& scFraction = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].second;
464 bool hitWithLC =
false;
465 if (scFraction == 0.
f)
467 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId);
468 if (hit_find_in_LC != detIdToLayerClusterId_Map.end())
470 auto itcheck =
hitMap_->find(sc_hitDetId);
471 const HIT*
hit =
hits_[itcheck->second];
472 float hitEnergyWeight =
hit->energy() *
hit->energy();
473 for (
auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
474 unsigned int layerClusterId = lcPair.first;
475 float lcFraction = 0.f;
478 auto findHitIt =
std::find(detIdToLayerClusterId_Map[sc_hitDetId].
begin(),
479 detIdToLayerClusterId_Map[sc_hitDetId].
end(),
481 if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].
end())
482 lcFraction = findHitIt->fraction;
485 hitEnergyWeight * invSCEnergyWeight;
487 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
488 <<
"scDetId:\t" << (uint32_t)sc_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t" 489 <<
"lcfraction,scfraction:\t" << lcFraction <<
", " << scFraction <<
"\t" 490 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t" 491 <<
"current score:\t" << lcPair.second.second <<
"\t" 492 <<
"invSCEnergyWeight:\t" << invSCEnergyWeight <<
"\n";
497 if (lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore.empty())
498 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"SC Id: \t" << scId <<
"\tLC id:\t-1 " 502 for (
const auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
503 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
504 <<
"SC Id: \t" << scId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
505 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t" 506 << (lcPair.second.first / SCenergy) <<
"\n";
512 return {scsInLayerCluster, lcsInSimCluster};
std::vector< std::vector< ticl::simClusterOnCLayer > > simClusterToLayerCluster
std::vector< std::vector< std::pair< unsigned int, float > > > layerClusterToSimCluster
std::shared_ptr< hgcal::RecHitTools > recHitTools_
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
const bool hardScatterOnly_
T const * product() const
const std::unordered_map< DetId, const unsigned int > * hitMap_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
def unique(seq, keepstr=True)
std::vector< const HIT * > hits_
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)