18 const auto&
clusters = *cCCH.product();
19 const auto& simClusters = *sCCH.
product();
20 auto nLayerClusters =
clusters.size();
24 auto nSimClusters = simClusters.size();
25 std::vector<size_t> sCIndices;
26 for (
unsigned int scId = 0; scId < nSimClusters; ++scId) {
28 simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() != 0)) {
29 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
30 <<
"Excluding SimCluster from event: " << simClusters[scId].g4Tracks()[0].eventId().event()
31 <<
" with BX: " << simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() << std::endl;
34 sCIndices.emplace_back(scId);
36 nSimClusters = sCIndices.size();
43 lcsInSimCluster.resize(nSimClusters);
44 for (
unsigned int i = 0;
i < nSimClusters; ++
i) {
45 lcsInSimCluster[
i].resize(
layers_ * 2);
46 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
47 lcsInSimCluster[
i][
j].simClusterId =
i;
48 lcsInSimCluster[
i][
j].energy = 0.f;
49 lcsInSimCluster[
i][
j].hits_and_fractions.clear();
58 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToSimClusterId_Map;
59 for (
const auto& scId : sCIndices) {
60 const auto& hits_and_fractions = simClusters[scId].hits_and_fractions();
61 for (
const auto& it_haf : hits_and_fractions) {
62 const auto hitid = (it_haf.first);
63 const auto scLayerId =
66 const auto itcheck =
hitMap_->find(hitid);
67 if (itcheck !=
hitMap_->end()) {
68 auto hit_find_it = detIdToSimClusterId_Map.find(hitid);
69 if (hit_find_it == detIdToSimClusterId_Map.end()) {
70 detIdToSimClusterId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
72 detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second);
75 lcsInSimCluster[scId][scLayerId].energy += it_haf.second * hit->
energy();
76 lcsInSimCluster[scId][scLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
82 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
83 <<
"lcsInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl;
84 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" # of clusters : " << nLayerClusters << std::endl;
85 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
86 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
87 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
88 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
89 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
90 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
91 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
92 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
93 double tot_energy = 0.;
94 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
95 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
96 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
97 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
98 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
100 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
101 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
102 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
103 << lc.second.first <<
"/" << lc.second.second << std::endl;
108 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"detIdToSimClusterId_Map INFO" << std::endl;
109 for (
auto const& sc : detIdToSimClusterId_Map) {
110 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
111 <<
"For detId: " << (uint32_t)sc.first
112 <<
" we have found the following connections with SimClusters:" << std::endl;
117 for (
auto const& sclu : sc.second) {
118 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
119 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
120 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
128 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToLayerClusterId_Map;
134 scsInLayerCluster.resize(nLayerClusters);
136 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
137 const std::vector<std::pair<DetId, float>>& hits_and_fractions =
clusters[lcId].hitsAndFractions();
138 unsigned int numberOfHitsInLC = hits_and_fractions.size();
139 const auto firstHitDetId = hits_and_fractions[0].first;
143 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
144 const auto rh_detid = hits_and_fractions[hitId].first;
145 const auto rhFraction = hits_and_fractions[hitId].second;
147 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid);
148 if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) {
149 detIdToLayerClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInCluster>();
151 detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction);
153 auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid);
155 if (hit_find_in_SC != detIdToSimClusterId_Map.end()) {
156 const auto itcheck =
hitMap_->find(rh_detid);
161 for (
auto&
h : hit_find_in_SC->second) {
164 lcsInSimCluster[
h.clusterId][lcLayerId].layerClusterIdToEnergyAndScore[lcId].first +=
167 scsInLayerCluster[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> hitsToSimClusterId(numberOfHitsInLC);
191 int maxSCId_byNumberOfHits = -1;
193 unsigned int maxSCNumberOfHitsInLC = 0;
195 int maxSCId_byEnergy = -1;
197 float maxEnergySharedLCandSC = 0.f;
199 float energyFractionOfLCinSC = 0.f;
201 float energyFractionOfSCinLC = 0.f;
202 std::unordered_map<unsigned, unsigned> occurrencesSCinLC;
203 unsigned int numberOfNoiseHitsInLC = 0;
204 std::unordered_map<unsigned, float> SCEnergyInLC;
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_SC = detIdToSimClusterId_Map.find(rh_detid);
218 if (rhFraction == 0.) {
219 hitsToSimClusterId[hitId] = -2;
222 if (hit_find_in_SC == detIdToSimClusterId_Map.end()) {
223 hitsToSimClusterId[hitId] -= 1;
225 const auto itcheck =
hitMap_->find(rh_detid);
227 auto maxSCEnergyInLC = 0.f;
230 for (
auto&
h : hit_find_in_SC->second) {
231 SCEnergyInLC[
h.clusterId] +=
h.fraction * hit->
energy();
234 if (SCEnergyInLC[
h.clusterId] > maxSCEnergyInLC) {
235 maxSCEnergyInLC = SCEnergyInLC[
h.clusterId];
236 maxSCId =
h.clusterId;
239 hitsToSimClusterId[hitId] = maxSCId;
243 for (
const auto&
c : hitsToSimClusterId) {
245 numberOfNoiseHitsInLC++;
247 occurrencesSCinLC[
c]++;
251 for (
const auto&
c : occurrencesSCinLC) {
252 if (
c.second > maxSCNumberOfHitsInLC) {
253 maxSCId_byNumberOfHits =
c.first;
254 maxSCNumberOfHitsInLC =
c.second;
258 for (
const auto&
c : SCEnergyInLC) {
259 if (
c.second > maxEnergySharedLCandSC) {
260 maxSCId_byEnergy =
c.first;
261 maxEnergySharedLCandSC =
c.second;
265 float totalSCEnergyOnLayer = 0.f;
266 if (maxSCId_byEnergy >= 0) {
267 totalSCEnergyOnLayer = lcsInSimCluster[maxSCId_byEnergy][lcLayerId].energy;
268 energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer;
270 energyFractionOfLCinSC = maxEnergySharedLCandSC /
clusters[lcId].energy();
274 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") << std::setw(10) <<
"LayerId:"
275 <<
"\t" << std::setw(12) <<
"layerCluster"
276 <<
"\t" << std::setw(10) <<
"lc energy"
277 <<
"\t" << std::setw(5) <<
"nhits"
278 <<
"\t" << std::setw(12) <<
"noise hits"
279 <<
"\t" << std::setw(22) <<
"maxSCId_byNumberOfHits"
280 <<
"\t" << std::setw(8) <<
"nhitsSC"
281 <<
"\t" << std::setw(13) <<
"maxSCId_byEnergy"
282 <<
"\t" << std::setw(20) <<
"maxEnergySharedLCandSC"
283 <<
"\t" << std::setw(22) <<
"totalSCEnergyOnLayer"
284 <<
"\t" << std::setw(22) <<
"energyFractionOfLCinSC"
285 <<
"\t" << std::setw(25) <<
"energyFractionOfSCinLC"
288 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
289 << std::setw(10) << lcLayerId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
290 <<
clusters[lcId].energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
291 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSCId_byNumberOfHits <<
"\t" << std::setw(8)
292 << maxSCNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSCId_byEnergy <<
"\t" << std::setw(20)
293 << maxEnergySharedLCandSC <<
"\t" << std::setw(22) << totalSCEnergyOnLayer <<
"\t" << std::setw(22)
294 << energyFractionOfLCinSC <<
"\t" << std::setw(25) << energyFractionOfSCinLC <<
"\n";
297 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
298 <<
"Improved lcsInSimCluster INFO (Now containing the linked layer clusters id and energy - score still empty)"
300 for (
size_t sc = 0; sc < lcsInSimCluster.size(); ++sc) {
301 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"For SimCluster Idx: " << sc <<
" we have: " << std::endl;
302 for (
size_t sclay = 0; sclay < lcsInSimCluster[sc].size(); ++sclay) {
303 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" On Layer: " << sclay <<
" we have:" << std::endl;
304 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
305 <<
" SimClusterIdx: " << lcsInSimCluster[sc][sclay].simClusterId << std::endl;
306 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
307 <<
" Energy: " << lcsInSimCluster[sc][sclay].energy << std::endl;
308 double tot_energy = 0.;
309 for (
auto const& haf : lcsInSimCluster[sc][sclay].hits_and_fractions) {
310 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
311 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/"
312 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
313 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
315 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
316 for (
auto const& lc : lcsInSimCluster[sc][sclay].layerClusterIdToEnergyAndScore) {
317 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
" lcIdx/energy/score: " << lc.first <<
"/"
318 << lc.second.first <<
"/" << lc.second.second << std::endl;
323 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"Improved detIdToSimClusterId_Map INFO" << std::endl;
324 for (
auto const& sc : detIdToSimClusterId_Map) {
325 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
326 <<
"For detId: " << (uint32_t)sc.first
327 <<
" we have found the following connections with SimClusters:" << std::endl;
328 for (
auto const& sclu : sc.second) {
329 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
330 <<
" SimCluster Id: " << sclu.clusterId <<
" with fraction: " << sclu.fraction
331 <<
" and energy: " << sclu.fraction *
hitMap_->at(sc.first)->energy() << std::endl;
338 for (
unsigned int lcId = 0; lcId < nLayerClusters; ++lcId) {
341 std::sort(scsInLayerCluster[lcId].
begin(), scsInLayerCluster[lcId].
end());
343 scsInLayerCluster[lcId].erase(
last, scsInLayerCluster[lcId].
end());
344 const auto& hits_and_fractions =
clusters[lcId].hitsAndFractions();
345 unsigned int numberOfHitsInLC = hits_and_fractions.size();
349 for (
auto& scPair : scsInLayerCluster[lcId]) {
351 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerClusterId : \t " << lcId <<
"\t SC id : \t"
352 << scPair.first <<
"\t score \t " << scPair.second <<
"\n";
359 float invLayerClusterEnergyWeight = 0.f;
360 for (
auto const& haf : hits_and_fractions) {
361 invLayerClusterEnergyWeight +=
362 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
364 invLayerClusterEnergyWeight = 1.f / invLayerClusterEnergyWeight;
365 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
366 DetId rh_detid = hits_and_fractions[
i].first;
367 float rhFraction = hits_and_fractions[
i].second;
369 bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end());
371 auto itcheck =
hitMap_->find(rh_detid);
375 for (
auto& scPair : scsInLayerCluster[lcId]) {
376 float scFraction = 0.f;
378 auto findHitIt =
std::find(detIdToSimClusterId_Map[rh_detid].
begin(),
379 detIdToSimClusterId_Map[rh_detid].
end(),
381 if (findHitIt != detIdToSimClusterId_Map[rh_detid].
end())
382 scFraction = findHitIt->fraction;
385 invLayerClusterEnergyWeight;
387 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
388 <<
"rh_detid:\t" << (uint32_t)rh_detid <<
"\tlayerClusterId:\t" << lcId <<
"\t"
389 <<
"rhfraction,scfraction:\t" << rhFraction <<
", " << scFraction <<
"\t"
390 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
391 <<
"current score:\t" << scPair.second <<
"\t"
392 <<
"invLayerClusterEnergyWeight:\t" << invLayerClusterEnergyWeight <<
"\n";
397 if (scsInLayerCluster[lcId].
empty())
398 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"layerCluster Id: \t" << lcId <<
"\tSC id:\t-1 "
405 for (
const auto& scId : sCIndices) {
406 for (
unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) {
407 unsigned int SCNumberOfHits = lcsInSimCluster[scId][layerId].hits_and_fractions.size();
408 if (SCNumberOfHits == 0)
411 int lcWithMaxEnergyInSC = -1;
413 float maxEnergyLCinSC = 0.f;
415 float SCenergy = lcsInSimCluster[scId][layerId].energy;
417 float SCEnergyFractionInLC = 0.f;
418 for (
auto& lc : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
419 if (lc.second.first > maxEnergyLCinSC) {
420 maxEnergyLCinSC = lc.second.first;
421 lcWithMaxEnergyInSC = lc.first;
425 SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy;
427 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
428 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"simcluster\t" << std::setw(15) <<
"sc total energy\t"
429 << std::setw(15) <<
"scEnergyOnLayer\t" << std::setw(14) <<
"SCNhitsOnLayer\t" << std::setw(18)
430 <<
"lcWithMaxEnergyInSC\t" << std::setw(15) <<
"maxEnergyLCinSC\t" << std::setw(20) <<
"SCEnergyFractionInLC"
432 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
433 << std::setw(8) << layerId <<
"\t" << std::setw(12) << scId <<
"\t" << std::setw(15)
434 << simClusters[scId].energy() <<
"\t" << std::setw(15) << SCenergy <<
"\t" << std::setw(14) << SCNumberOfHits
435 <<
"\t" << std::setw(18) << lcWithMaxEnergyInSC <<
"\t" << std::setw(15) << maxEnergyLCinSC <<
"\t"
436 << std::setw(20) << SCEnergyFractionInLC <<
"\n";
439 float invSCEnergyWeight = 0.f;
440 for (
auto const& haf : lcsInSimCluster[scId][layerId].hits_and_fractions) {
441 invSCEnergyWeight +=
std::pow(haf.second *
hitMap_->at(haf.first)->energy(), 2);
443 invSCEnergyWeight = 1.f / invSCEnergyWeight;
444 for (
unsigned int i = 0;
i < SCNumberOfHits; ++
i) {
445 auto& sc_hitDetId = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].first;
446 auto& scFraction = lcsInSimCluster[scId][layerId].hits_and_fractions[
i].second;
448 bool hitWithLC =
false;
449 if (scFraction == 0.
f)
451 auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId);
452 if (hit_find_in_LC != detIdToLayerClusterId_Map.end())
454 auto itcheck =
hitMap_->find(sc_hitDetId);
457 for (
auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
458 unsigned int layerClusterId = lcPair.first;
459 float lcFraction = 0.f;
462 auto findHitIt =
std::find(detIdToLayerClusterId_Map[sc_hitDetId].
begin(),
463 detIdToLayerClusterId_Map[sc_hitDetId].
end(),
465 if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].
end())
466 lcFraction = findHitIt->fraction;
469 hitEnergyWeight * invSCEnergyWeight;
471 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
472 <<
"scDetId:\t" << (uint32_t)sc_hitDetId <<
"\tlayerClusterId:\t" << layerClusterId <<
"\t"
473 <<
"lcfraction,scfraction:\t" << lcFraction <<
", " << scFraction <<
"\t"
474 <<
"hitEnergyWeight:\t" << hitEnergyWeight <<
"\t"
475 <<
"current score:\t" << lcPair.second.second <<
"\t"
476 <<
"invSCEnergyWeight:\t" << invSCEnergyWeight <<
"\n";
481 if (lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore.empty())
482 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl") <<
"SC Id: \t" << scId <<
"\tLC id:\t-1 "
486 for (
const auto& lcPair : lcsInSimCluster[scId][layerId].layerClusterIdToEnergyAndScore) {
487 LogDebug(
"LCToSCAssociatorByEnergyScoreImpl")
488 <<
"SC Id: \t" << scId <<
"\t LC id: \t" << lcPair.first <<
"\t score \t" << lcPair.second.second
489 <<
"\t shared energy:\t" << lcPair.second.first <<
"\t shared energy fraction:\t"
490 << (lcPair.second.first / SCenergy) <<
"\n";
496 return {scsInLayerCluster, lcsInSimCluster};
constexpr float energy() const
const edm::EventSetup & c
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::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
std::vector< std::vector< hgcal::simClusterOnLayer > > simClusterToLayerCluster
const bool hardScatterOnly_
for(Iditer=Id.begin();Iditer!=Id.end();Iditer++)
const std::unordered_map< DetId, const HGCRecHit * > * hitMap_
T const * product() const
std::shared_ptr< hgcal::RecHitTools > recHitTools_
std::vector< std::vector< std::pair< unsigned int, float > > > layerClusterToSimCluster
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)