25 const auto& mClusters = *mCCH.
product();
27 auto nMultiClusters = mClusters.size();
29 std::vector<size_t> cPIndices;
33 auto nCaloParticles = cPIndices.size();
35 std::vector<size_t> cPSelectedIndices;
46 cPOnLayer.resize(nCaloParticles);
47 for (
unsigned int i = 0;
i < nCaloParticles; ++
i) {
48 auto cpIndex = cPIndices[
i];
49 cPOnLayer[cpIndex].resize(
layers_ * 2);
50 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
51 cPOnLayer[cpIndex][
j].caloParticleId = cpIndex;
52 cPOnLayer[cpIndex][
j].energy = 0.f;
53 cPOnLayer[cpIndex][
j].hits_and_fractions.clear();
57 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInCluster>> detIdToCaloParticleId_Map;
59 for (
const auto& cpId : cPIndices) {
63 for (
const auto& it_sc : simClusterRefVector) {
66 for (
const auto& it_haf : hits_and_fractions) {
67 const auto hitid = (it_haf.first);
68 const auto cpLayerId =
70 const auto itcheck =
hitMap_->find(hitid);
71 if (itcheck !=
hitMap_->end()) {
79 auto hit_find_it = detIdToCaloParticleId_Map.find(hitid);
80 if (hit_find_it == detIdToCaloParticleId_Map.end()) {
81 detIdToCaloParticleId_Map[hitid] = std::vector<hgcal::detIdInfoInCluster>();
82 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
84 auto findHitIt =
std::find(detIdToCaloParticleId_Map[hitid].
begin(),
85 detIdToCaloParticleId_Map[hitid].
end(),
87 if (findHitIt != detIdToCaloParticleId_Map[hitid].
end()) {
88 findHitIt->fraction += it_haf.second;
90 detIdToCaloParticleId_Map[hitid].emplace_back(cpId, it_haf.second);
97 cPOnLayer[cpId][cpLayerId].energy += it_haf.second *
hit->energy();
104 auto& haf = cPOnLayer[cpId][cpLayerId].hits_and_fractions;
105 auto found = std::find_if(
106 std::begin(haf), std::end(haf), [&hitid](
const std::pair<DetId, float>&
v) {
return v.first == hitid; });
107 if (
found != haf.end()) {
108 found->second += it_haf.second;
110 cPOnLayer[cpId][cpLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second);
118 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"cPOnLayer INFO" << std::endl;
119 for (
size_t cp = 0;
cp < cPOnLayer.size(); ++
cp) {
120 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"For CaloParticle Idx: " <<
cp <<
" we have: " << std::endl;
121 for (
size_t cpp = 0; cpp < cPOnLayer[
cp].size(); ++cpp) {
122 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" On Layer: " << cpp <<
" we have:" << std::endl;
123 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
124 <<
" CaloParticleIdx: " << cPOnLayer[
cp][cpp].caloParticleId << std::endl;
125 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
126 <<
" Energy: " << cPOnLayer[
cp][cpp].energy << std::endl;
127 double tot_energy = 0.;
128 for (
auto const& haf : cPOnLayer[
cp][cpp].hits_and_fractions) {
129 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
130 <<
" Hits/fraction/energy: " << (uint32_t)haf.first <<
"/" << haf.second <<
"/" 131 << haf.second *
hitMap_->at(haf.first)->energy() << std::endl;
132 tot_energy += haf.second *
hitMap_->at(haf.first)->energy();
134 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" Tot Sum haf: " << tot_energy << std::endl;
135 for (
auto const&
mc : cPOnLayer[
cp][cpp].multiClusterIdToEnergyAndScore) {
136 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
" mcIdx/energy/score: " <<
mc.first <<
"/" 137 <<
mc.second.first <<
"/" <<
mc.second.second << std::endl;
142 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"detIdToCaloParticleId_Map INFO" << std::endl;
143 for (
auto const&
cp : detIdToCaloParticleId_Map) {
144 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
145 <<
"For detId: " << (uint32_t)
cp.first
146 <<
" we have found the following connections with CaloParticles:" << std::endl;
147 for (
auto const& cpp :
cp.second) {
148 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
149 <<
" CaloParticle Id: " << cpp.clusterId <<
" with fraction: " << cpp.fraction
150 <<
" and energy: " << cpp.fraction *
hitMap_->at(
cp.first)->energy() << std::endl;
156 std::unordered_map<DetId, std::vector<hgcal::detIdInfoInMultiCluster>> detIdToMultiClusterId_Map;
162 cpsInMultiCluster.resize(nMultiClusters);
165 for (
unsigned int mcId = 0; mcId < nMultiClusters; ++mcId) {
166 const auto& hits_and_fractions = mClusters[mcId].hitsAndFractions();
167 if (!hits_and_fractions.empty()) {
168 std::unordered_map<unsigned, float> CPEnergyInMCL;
169 int maxCPId_byNumberOfHits = -1;
170 unsigned int maxCPNumberOfHitsInMCL = 0;
171 int maxCPId_byEnergy = -1;
172 float maxEnergySharedMCLandCP = 0.f;
173 float energyFractionOfMCLinCP = 0.f;
174 float energyFractionOfCPinMCL = 0.f;
181 std::unordered_map<unsigned, unsigned> occurrencesCPinMCL;
182 unsigned int numberOfNoiseHitsInMCL = 0;
183 unsigned int numberOfHitsInMCL = 0;
186 unsigned int numberOfHitsInLC = hits_and_fractions.size();
187 numberOfHitsInMCL += numberOfHitsInLC;
188 std::unordered_map<unsigned, float> CPEnergyInLC;
207 std::vector<int> hitsToCaloParticleId(numberOfHitsInLC);
210 const auto firstHitDetId = hits_and_fractions[0].first;
211 int lcLayerId =
recHitTools_->getLayerWithOffset(firstHitDetId) +
215 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
216 const auto rh_detid = hits_and_fractions[hitId].first;
217 const auto rhFraction = hits_and_fractions[hitId].second;
220 const auto itcheck =
hitMap_->find(rh_detid);
221 const auto hit = itcheck->second;
230 auto hit_find_in_LC = detIdToMultiClusterId_Map.find(rh_detid);
231 if (hit_find_in_LC == detIdToMultiClusterId_Map.end()) {
232 detIdToMultiClusterId_Map[rh_detid] = std::vector<hgcal::detIdInfoInMultiCluster>();
237 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;
248 auto maxCPEnergyInLC = 0.f;
250 for (
auto&
h : hit_find_in_CP->second) {
251 auto shared_fraction =
std::min(rhFraction,
h.fraction);
256 CPEnergyInMCL[
h.clusterId] += shared_fraction *
hit->energy();
258 CPEnergyInLC[
h.clusterId] += shared_fraction *
hit->energy();
261 cPOnLayer[
h.clusterId][lcLayerId].multiClusterIdToEnergyAndScore[mcId].first +=
262 shared_fraction *
hit->energy();
263 cPOnLayer[
h.clusterId][lcLayerId].multiClusterIdToEnergyAndScore[mcId].second = FLT_MAX;
266 cpsInMultiCluster[mcId].emplace_back(
h.clusterId, FLT_MAX);
269 if (shared_fraction > maxCPEnergyInLC) {
271 maxCPEnergyInLC = CPEnergyInLC[
h.clusterId];
272 maxCPId =
h.clusterId;
276 hitsToCaloParticleId[hitId] = maxCPId;
283 for (
auto c : hitsToCaloParticleId) {
285 numberOfNoiseHitsInMCL++;
287 occurrencesCPinMCL[
c]++;
293 for (
auto&
c : occurrencesCPinMCL) {
294 if (
c.second > maxCPNumberOfHitsInMCL) {
295 maxCPId_byNumberOfHits =
c.first;
296 maxCPNumberOfHitsInMCL =
c.second;
301 for (
auto&
c : CPEnergyInMCL) {
302 if (
c.second > maxEnergySharedMCLandCP) {
303 maxCPId_byEnergy =
c.first;
304 maxEnergySharedMCLandCP =
c.second;
308 float totalCPEnergyFromLayerCP = 0.f;
309 if (maxCPId_byEnergy >= 0) {
311 for (
unsigned int j = 0;
j <
layers_ * 2; ++
j) {
312 totalCPEnergyFromLayerCP = totalCPEnergyFromLayerCP + cPOnLayer[maxCPId_byEnergy][
j].energy;
314 energyFractionOfCPinMCL = maxEnergySharedMCLandCP / totalCPEnergyFromLayerCP;
315 if (mClusters[mcId].
energy() > 0.
f) {
316 energyFractionOfMCLinCP = maxEnergySharedMCLandCP / mClusters[mcId].energy();
320 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") << std::setw(12) <<
"multiCluster" 321 <<
"\t" << std::setw(10) <<
"mulcl energy" 322 <<
"\t" << std::setw(5) <<
"nhits" 323 <<
"\t" << std::setw(12) <<
"noise hits" 324 <<
"\t" << std::setw(22) <<
"maxCPId_byNumberOfHits" 325 <<
"\t" << std::setw(8) <<
"nhitsCP" 326 <<
"\t" << std::setw(16) <<
"maxCPId_byEnergy" 327 <<
"\t" << std::setw(23) <<
"maxEnergySharedMCLandCP" 328 <<
"\t" << std::setw(22) <<
"totalCPEnergyFromAllLayerCP" 329 <<
"\t" << std::setw(22) <<
"energyFractionOfMCLinCP" 330 <<
"\t" << std::setw(25) <<
"energyFractionOfCPinMCL" 331 <<
"\t" << std::endl;
332 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
333 << std::setw(12) << mcId <<
"\t" << std::setw(10) << mClusters[mcId].energy() <<
"\t" << std::setw(5)
334 << numberOfHitsInMCL <<
"\t" << std::setw(12) << numberOfNoiseHitsInMCL <<
"\t" << std::setw(22)
335 << maxCPId_byNumberOfHits <<
"\t" << std::setw(8) << maxCPNumberOfHitsInMCL <<
"\t" << std::setw(16)
336 << maxCPId_byEnergy <<
"\t" << std::setw(23) << maxEnergySharedMCLandCP <<
"\t" << std::setw(22)
337 << totalCPEnergyFromLayerCP <<
"\t" << std::setw(22) << energyFractionOfMCLinCP <<
"\t" << std::setw(25)
338 << energyFractionOfCPinMCL << std::endl;
344 for (
unsigned int mcId = 0; mcId < nMultiClusters; ++mcId) {
348 cpsInMultiCluster[mcId].erase(
last, cpsInMultiCluster[mcId].
end());
350 const auto& hits_and_fractions = mClusters[mcId].hitsAndFractions();
351 unsigned int numberOfHitsInLC = hits_and_fractions.size();
352 if (numberOfHitsInLC > 0) {
353 if (mClusters[mcId].
energy() == 0. && !cpsInMultiCluster[mcId].
empty()) {
355 for (
auto& cpPair : cpsInMultiCluster[mcId]) {
358 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
359 <<
"multiClusterId : \t " << mcId <<
"\t CP id : \t" << cpPair.first <<
"\t score \t " << cpPair.second
366 float invMultiClusterEnergyWeight = 0.f;
367 for (
auto const& haf : mClusters[mcId].hitsAndFractions()) {
368 invMultiClusterEnergyWeight +=
369 (haf.second *
hitMap_->at(haf.first)->energy()) * (haf.second *
hitMap_->at(haf.first)->energy());
371 invMultiClusterEnergyWeight = 1.f / invMultiClusterEnergyWeight;
373 for (
unsigned int i = 0;
i < numberOfHitsInLC; ++
i) {
374 DetId rh_detid = hits_and_fractions[
i].first;
375 float rhFraction = hits_and_fractions[
i].second;
377 bool hitWithNoCP = (detIdToCaloParticleId_Map.find(rh_detid) == detIdToCaloParticleId_Map.end());
379 auto itcheck =
hitMap_->find(rh_detid);
381 float hitEnergyWeight =
hit->energy() *
hit->energy();
383 for (
auto& cpPair : cpsInMultiCluster[mcId]) {
384 unsigned int multiClusterId = cpPair.first;
385 float cpFraction = 0.f;
387 auto findHitIt =
std::find(detIdToCaloParticleId_Map[rh_detid].
begin(),
388 detIdToCaloParticleId_Map[rh_detid].
end(),
390 if (findHitIt != detIdToCaloParticleId_Map[rh_detid].
end())
391 cpFraction = findHitIt->fraction;
393 if (cpPair.second == FLT_MAX) {
397 (rhFraction - cpFraction) * (rhFraction - cpFraction) * hitEnergyWeight * invMultiClusterEnergyWeight;
402 if (cpsInMultiCluster[mcId].
empty())
403 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl") <<
"multiCluster Id: \t" << mcId <<
"\tCP id:\t-1 " 411 for (
const auto& cpId : cPSelectedIndices) {
412 for (
unsigned int layerId = 0; layerId <
layers_ * 2; ++layerId) {
413 unsigned int CPNumberOfHits = cPOnLayer[cpId][layerId].hits_and_fractions.size();
414 if (CPNumberOfHits == 0)
417 int mcWithMaxEnergyInCP = -1;
418 float maxEnergyMCLperlayerinCP = 0.f;
419 float CPenergy = cPOnLayer[cpId][layerId].energy;
420 float CPEnergyFractionInMCLperlayer = 0.f;
421 for (
auto&
mc : cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore) {
422 if (
mc.second.first > maxEnergyMCLperlayerinCP) {
423 maxEnergyMCLperlayerinCP =
mc.second.first;
424 mcWithMaxEnergyInCP =
mc.first;
428 CPEnergyFractionInMCLperlayer = maxEnergyMCLperlayerinCP / CPenergy;
430 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
431 << std::setw(8) <<
"LayerId:\t" << std::setw(12) <<
"caloparticle\t" << std::setw(15) <<
"cp total energy\t" 432 << std::setw(15) <<
"cpEnergyOnLayer\t" << std::setw(14) <<
"CPNhitsOnLayer\t" << std::setw(18)
433 <<
"mcWithMaxEnergyInCP\t" << std::setw(15) <<
"maxEnergyMCLinCP\t" << std::setw(20)
434 <<
"CPEnergyFractionInMCL" 436 LogDebug(
"MultiClusterAssociatorByEnergyScoreImpl")
437 << std::setw(8) << layerId <<
"\t" << std::setw(12) << cpId <<
"\t" << std::setw(15)
438 <<
caloParticles[cpId].energy() <<
"\t" << std::setw(15) << CPenergy <<
"\t" << std::setw(14)
439 << CPNumberOfHits <<
"\t" << std::setw(18) << mcWithMaxEnergyInCP <<
"\t" << std::setw(15)
440 << maxEnergyMCLperlayerinCP <<
"\t" << std::setw(20) << CPEnergyFractionInMCLperlayer <<
"\n";
443 for (
unsigned int i = 0;
i < CPNumberOfHits; ++
i) {
444 auto& cp_hitDetId = cPOnLayer[cpId][layerId].hits_and_fractions[
i].first;
445 auto& cpFraction = cPOnLayer[cpId][layerId].hits_and_fractions[
i].second;
447 bool hitWithNoMCL =
false;
448 if (cpFraction == 0.
f)
450 auto hit_find_in_MCL = detIdToMultiClusterId_Map.find(cp_hitDetId);
451 if (hit_find_in_MCL == detIdToMultiClusterId_Map.end())
453 auto itcheck =
hitMap_->find(cp_hitDetId);
455 float hitEnergyWeight =
hit->energy() *
hit->energy();
456 for (
auto& mcPair : cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore) {
457 unsigned int multiClusterId = mcPair.first;
458 float mcFraction = 0.f;
461 auto findHitIt =
std::find(detIdToMultiClusterId_Map[cp_hitDetId].
begin(),
462 detIdToMultiClusterId_Map[cp_hitDetId].
end(),
464 if (findHitIt != detIdToMultiClusterId_Map[cp_hitDetId].
end())
465 mcFraction = findHitIt->fraction;
469 if (mcPair.second.second == FLT_MAX) {
470 mcPair.second.second = 0.f;
472 mcPair.second.second += (mcFraction - cpFraction) * (mcFraction - cpFraction) * hitEnergyWeight;
474 LogDebug(
"HGCalValidator") <<
"multiClusterId:\t" << multiClusterId <<
"\tmcfraction,cpfraction:\t" 475 << mcFraction <<
", " << cpFraction <<
"\thitEnergyWeight:\t" << hitEnergyWeight
476 <<
"\tcurrent score numerator:\t" << mcPair.second.second <<
"\n";
481 if (cPOnLayer[cpId][layerId].multiClusterIdToEnergyAndScore.empty())
482 LogDebug(
"HGCalValidator") <<
"CP Id: \t" << cpId <<
"\t MCL id:\t-1 " 483 <<
"\t layer \t " << layerId <<
" Sub score in \t -1" 489 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
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)
const std::unordered_map< DetId, const HGCRecHit * > * hitMap_
std::vector< std::vector< std::pair< unsigned int, float > > > multiClusterToCaloParticle
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)