30 const auto& mClusters = *mCCH.
product();
32 auto nMultiClusters = mClusters.size();
34 std::vector<size_t> cPIndices;
38 auto nCaloParticles = cPIndices.size();
40 std::vector<size_t> cPSelectedIndices;
51 cPOnLayer.resize(nCaloParticles);
52 for (
unsigned int i = 0;
i < nCaloParticles; ++
i) {
53 auto cpIndex = cPIndices[
i];
54 cPOnLayer[cpIndex].resize(
layers_ * 2);
55 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
56 cPOnLayer[cpIndex][
j].caloParticleId = cpIndex;
57 cPOnLayer[cpIndex][
j].energy = 0.f;
58 cPOnLayer[cpIndex][
j].hits_and_fractions.clear();
62 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToCaloParticleId_Map;
64 for (
const auto& cpId : cPIndices) {
68 for (
const auto& it_sc : simClusterRefVector) {
71 for (
const auto& it_haf : hits_and_fractions) {
72 const auto hitid = (it_haf.first);
73 const auto cpLayerId =
75 const auto itcheck =
hitMap_->find(hitid);
76 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<hgcal::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);
102 cPOnLayer[cpId][cpLayerId].energy += it_haf.second *
hit->energy();
109 auto& haf = cPOnLayer[cpId][cpLayerId].hits_and_fractions;
110 auto found = std::find_if(
111 std::begin(haf), std::end(haf), [&hitid](
const std::pair<DetId, float>&
v) {
return v.first == hitid; });
112 if (
found != haf.end()) {
113 found->second += it_haf.second;
115 cPOnLayer[cpId][cpLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
123 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
124 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
125 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
126 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
127 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
128 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
129 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
130 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
131 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
132 double tot_energy = 0.;
133 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
135 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
136 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/" 137 << haf.second *
hit->energy() << std::endl;
138 tot_energy += haf.second *
hit->energy();
140 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
141 for (
auto const&
mc : cPOnLayer[
cp][cpp].multiClusterIdToEnergyAndScore) {
142 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" mcIdx/energy/score: " <<
mc.first <<
"/" 143 <<
mc.second.first <<
"/" <<
mc.second.second << std::endl;
148 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
149 for (
auto const&
cp : detIdToCaloParticleId_Map) {
151 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
152 <<
"For detId: " << (uint32_t)
cp.first
153 <<
" we have found the following connections with CaloParticles:" << std::endl;
154 for (
auto const& cpp :
cp.second) {
155 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
156 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
157 <<
" and energy: " << cpp.fraction *
hit->energy() << std::endl;
163 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInMultiCluster>> detIdToMultiClusterId_Map;
169 cpsInMultiCluster.resize(nMultiClusters);
172 for (
unsigned int mcId = 0; mcId < nMultiClusters; ++mcId) {
173 const auto& hits_and_fractions = mClusters[mcId].hitsAndFractions();
174 if (!hits_and_fractions.empty()) {
175 std::unordered_map<unsigned, float> CPEnergyInMCL;
176 int maxCPId_byNumberOfHits = -1;
177 unsigned int maxCPNumberOfHitsInMCL = 0;
178 int maxCPId_byEnergy = -1;
179 float maxEnergySharedMCLandCP = 0.f;
180 float energyFractionOfMCLinCP = 0.f;
181 float energyFractionOfCPinMCL = 0.f;
188 std::unordered_map<unsigned, unsigned> occurrencesCPinMCL;
189 unsigned int numberOfNoiseHitsInMCL = 0;
190 unsigned int numberOfHitsInMCL = 0;
193 unsigned int numberOfHitsInLC = hits_and_fractions.size();
194 numberOfHitsInMCL += numberOfHitsInLC;
195 std::unordered_map<unsigned, float> CPEnergyInLC;
214 std::vector<int> hitsToCaloParticleId(numberOfHitsInLC);
217 const auto firstHitDetId = hits_and_fractions[0].first;
218 int lcLayerId =
recHitTools_->getLayerWithOffset(firstHitDetId) +
222 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
223 const auto rh_detid = hits_and_fractions[hitId].first;
224 const auto rhFraction = hits_and_fractions[hitId].second;
227 const auto itcheck =
hitMap_->find(rh_detid);
237 auto hit_find_in_LC = detIdToMultiClusterId_Map.find(rh_detid);
238 if (hit_find_in_LC == detIdToMultiClusterId_Map.end()) {
239 detIdToMultiClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInMultiCluster>();
244 auto hit_find_in_CP = detIdToCaloParticleId_Map.find(rh_detid);
249 if (rhFraction == 0.) {
250 hitsToCaloParticleId[hitId] = -2;
252 if (hit_find_in_CP == detIdToCaloParticleId_Map.end()) {
253 hitsToCaloParticleId[hitId] -= 1;
255 auto maxCPEnergyInLC = 0.f;
257 for (
auto&
h : hit_find_in_CP->second) {
258 auto shared_fraction =
std::min(rhFraction,
h.fraction);
263 CPEnergyInMCL[
h.clusterId] += shared_fraction *
hit->energy();
265 CPEnergyInLC[
h.clusterId] += shared_fraction *
hit->energy();
268 cPOnLayer[
h.clusterId][lcLayerId].multiClusterIdToEnergyAndScore[mcId].first +=
269 shared_fraction *
hit->energy();
270 cPOnLayer[
h.clusterId][lcLayerId].multiClusterIdToEnergyAndScore[mcId].second = FLT_MAX;
273 cpsInMultiCluster[mcId].emplace_back(
h.clusterId, FLT_MAX);
276 if (shared_fraction > maxCPEnergyInLC) {
278 maxCPEnergyInLC = CPEnergyInLC[
h.clusterId];
279 maxCPId =
h.clusterId;
283 hitsToCaloParticleId[hitId] = maxCPId;
290 for (
auto c : hitsToCaloParticleId) {
292 numberOfNoiseHitsInMCL++;
294 occurrencesCPinMCL[
c]++;
300 for (
auto&
c : occurrencesCPinMCL) {
301 if (
c.second > maxCPNumberOfHitsInMCL) {
302 maxCPId_byNumberOfHits =
c.first;
303 maxCPNumberOfHitsInMCL =
c.second;
308 for (
auto&
c : CPEnergyInMCL) {
309 if (
c.second > maxEnergySharedMCLandCP) {
310 maxCPId_byEnergy =
c.first;
311 maxEnergySharedMCLandCP =
c.second;
315 float totalCPEnergyFromLayerCP = 0.f;
316 if (maxCPId_byEnergy >= 0) {
318 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
319 totalCPEnergyFromLayerCP = totalCPEnergyFromLayerCP + cPOnLayer[maxCPId_byEnergy][
j].energy;
321 energyFractionOfCPinMCL = maxEnergySharedMCLandCP / totalCPEnergyFromLayerCP;
322 if (mClusters[mcId].
energy() > 0.
f) {
323 energyFractionOfMCLinCP = maxEnergySharedMCLandCP / mClusters[mcId].energy();
327 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") << std::setw(12) <<
"multiCluster" 328 <<
"\t" << std::setw(10) <<
"mulcl energy" 329 <<
"\t" << std::setw(5) <<
"nhits" 330 <<
"\t" << std::setw(12) <<
"noise hits" 331 <<
"\t" << std::setw(22) <<
"maxCPId_byNumberOfHits" 332 <<
"\t" << std::setw(8) <<
"nhitsCP" 333 <<
"\t" << std::setw(16) <<
"maxCPId_byEnergy" 334 <<
"\t" << std::setw(23) <<
"maxEnergySharedMCLandCP" 335 <<
"\t" << std::setw(22) <<
"totalCPEnergyFromAllLayerCP" 336 <<
"\t" << std::setw(22) <<
"energyFractionOfMCLinCP" 337 <<
"\t" << std::setw(25) <<
"energyFractionOfCPinMCL" 338 <<
"\t" << std::endl;
339 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
340 << std::setw(12) << mcId <<
"\t" << std::setw(10) << mClusters[mcId].energy() <<
"\t" << std::setw(5)
341 << numberOfHitsInMCL <<
"\t" << std::setw(12) << numberOfNoiseHitsInMCL <<
"\t" << std::setw(22)
342 << maxCPId_byNumberOfHits <<
"\t" << std::setw(8) << maxCPNumberOfHitsInMCL <<
"\t" << std::setw(16)
343 << maxCPId_byEnergy <<
"\t" << std::setw(23) << maxEnergySharedMCLandCP <<
"\t" << std::setw(22)
344 << totalCPEnergyFromLayerCP <<
"\t" << std::setw(22) << energyFractionOfMCLinCP <<
"\t" << std::setw(25)
345 << energyFractionOfCPinMCL << std::endl;
351 for (
unsigned int mcId = 0; mcId < nMultiClusters; ++mcId) {
355 cpsInMultiCluster[mcId].erase(
last, cpsInMultiCluster[mcId].
end());
357 const auto& hits_and_fractions = mClusters[mcId].hitsAndFractions();
358 unsigned int numberOfHitsInLC = hits_and_fractions.size();
359 if (numberOfHitsInLC > 0) {
360 if (mClusters[mcId].
energy() == 0. && !cpsInMultiCluster[mcId].
empty()) {
362 for (
auto& cpPair : cpsInMultiCluster[mcId]) {
365 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
366 <<
"multiClusterId : \t " << mcId <<
"\t CP id : \t" << cpPair.first <<
"\t score \t " << cpPair.second
373 float invMultiClusterEnergyWeight = 0.f;
374 for (
auto const& haf : mClusters[mcId].hitsAndFractions()) {
376 invMultiClusterEnergyWeight += (haf.second *
hit->energy()) * (haf.second *
hit->energy());
378 invMultiClusterEnergyWeight = 1.f / invMultiClusterEnergyWeight;
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 hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
386 auto itcheck =
hitMap_->find(rh_detid);
388 float hitEnergyWeight =
hit->energy() *
hit->energy();
390 for (
auto& cpPair : cpsInMultiCluster[mcId]) {
391 unsigned int multiClusterId = cpPair.first;
392 float cpFraction = 0.f;
394 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].
begin(),
395 detIdToCaloParticleId_Map[rh_detid].
end(),
397 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
398 cpFraction = findHitIt->fraction;
400 if (cpPair.second == FLT_MAX) {
404 (rhFraction - cpFraction) * (rhFraction - cpFraction) * hitEnergyWeight * invMultiClusterEnergyWeight;
409 if (cpsInMultiCluster[mcId].
empty())
410 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"multiCluster Id: \t" << mcId <<
"\tCP id:\t-1 " 418 for (
const auto& cpId : cPSelectedIndices) {
419 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
420 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
421 if (CPNumberOfHits == 0)
424 int mcWithMaxEnergyInCP = -1;
425 float maxEnergyMCLperlayerinCP = 0.f;
426 float CPenergy = cPOnLayer[cpId][layerId].energy;
427 float CPEnergyFractionInMCLperlayer = 0.f;
428 for (
auto&
mc : cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore) {
429 if (
mc.second.first > maxEnergyMCLperlayerinCP) {
430 maxEnergyMCLperlayerinCP =
mc.second.first;
431 mcWithMaxEnergyInCP =
mc.first;
435 CPEnergyFractionInMCLperlayer = maxEnergyMCLperlayerinCP / CPenergy;
437 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
438 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t" 439 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
440 <<
"mcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyMCLinCP\t" << std::setw(20)
441 <<
"CPEnergyFractionInMCL" 443 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
444 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
445 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
446 << CPNumberOfHits <<
"\t" << std::setw(18) << mcWithMaxEnergyInCP <<
"\t" << std::setw(15)
447 << maxEnergyMCLperlayerinCP <<
"\t" << std::setw(20) << CPEnergyFractionInMCLperlayer <<
"\n";
450 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
451 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
452 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
454 bool hitWithNoMCL =
false;
455 if (cpFraction == 0.
f)
457 auto hit_find_in_MCL = detIdToMultiClusterId_Map.find(cp_hitDetId);
458 if (hit_find_in_MCL == detIdToMultiClusterId_Map.end())
460 auto itcheck =
hitMap_->find(cp_hitDetId);
462 float hitEnergyWeight =
hit->energy() *
hit->energy();
463 for (
auto& mcPair : cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore) {
464 unsigned int multiClusterId = mcPair.first;
465 float mcFraction = 0.f;
468 auto findHitIt =
std::find(detIdToMultiClusterId_Map[cp_hitDetId].
begin(),
469 detIdToMultiClusterId_Map[cp_hitDetId].
end(),
471 if (findHitIt != detIdToMultiClusterId_Map[cp_hitDetId].
end())
472 mcFraction = findHitIt->fraction;
476 if (mcPair.second.second == FLT_MAX) {
477 mcPair.second.second = 0.f;
479 mcPair.second.second += (mcFraction - cpFraction) * (mcFraction - cpFraction) * hitEnergyWeight;
481 LogDebug(
"HGCalValidator") <<
"multiClusterId:\t" << multiClusterId <<
"\tmcfraction,cpfraction:\t" 482 << mcFraction <<
", " << cpFraction <<
"\thitEnergyWeight:\t" << hitEnergyWeight
483 <<
"\tcurrent score numerator:\t" << mcPair.second.second <<
"\n";
488 if (cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore.empty())
489 LogDebug(
"HGCalValidator") <<
"CP Id: \t" << cpId <<
"\t MCL id:\t-1 " 490 <<
"\t layer \t " << layerId <<
" Sub score in \t -1" 496 return {cpsInMultiCluster, cPOnLayer};
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
std::shared_ptr< hgcal::RecHitTools > recHitTools_
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)
std::vector< std::pair< uint32_t, float > > hits_and_fractions() const
Returns list of rechit IDs and fractions for this SimCluster.
std::vector< std::vector< hgcal::caloParticleOnALayer > > caloParticleToMultiCluster
Monte Carlo truth information used for tracking validation.
def unique(seq, keepstr=True)
std::vector< std::vector< std::pair< unsigned int, float > > > multiClusterToCaloParticle
std::vector< const HGCRecHit * > hits_
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
def cp(fromDir, toDir, listOfFiles, overwrite=False, smallList=False)