Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport 14_2_X] Make HGCAL Validation reproducible #47066

Open
wants to merge 5 commits into
base: CMSSW_14_2_X
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ AllTracksterToSimTracksterAssociatorsByHitsProducer::AllTracksterToSimTracksterA
const auto& tracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("tracksterCollections");
for (const auto& tag : tracksterCollections) {
std::string label = tag.label();
if (tag.instance() != "") {
if (!tag.instance().empty()) {
label += tag.instance();
}
tracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
Expand All @@ -69,7 +69,7 @@ AllTracksterToSimTracksterAssociatorsByHitsProducer::AllTracksterToSimTracksterA
const auto& simTracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("simTracksterCollections");
for (const auto& tag : simTracksterCollections) {
std::string label = tag.label();
if (tag.instance() != "") {
if (!tag.instance().empty()) {
label += tag.instance();
}
simTracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
Expand Down Expand Up @@ -259,14 +259,15 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
const auto& recHit = rechitManager[hitIndex];
float recoFraction = recoTracksterHitsAndFractions[i].fraction();
float squaredRecoFraction = recoFraction * recoFraction;
float squaredRecHitEnergy = recHit.energy() * recHit.energy();
float recoSharedEnergy = recHit.energy() * recoFraction;
float rechitEnergy = recHit.energy();
float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
float recoSharedEnergy = rechitEnergy * recoFraction;
const auto& simTracksterVec = hitToAssociatedSimTracksterMap[i];
for (const auto& simTracksterElement : simTracksterVec) {
auto simTracksterIndex = simTracksterElement.index();
auto simFraction = simTracksterElement.fraction();
edm::Ref<std::vector<ticl::Trackster>> simTracksterRef(simTrackstersHandle, simTracksterIndex);
float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy);
float sharedEnergy = std::min(simFraction * rechitEnergy, recoSharedEnergy);
float squaredFraction =
std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
float score = invDenominator * squaredFraction * squaredRecHitEnergy;
Expand Down Expand Up @@ -297,15 +298,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
: std::find_if(hitToSimClusterMap[hitIndex].begin(),
hitToSimClusterMap[hitIndex].end(),
[simObjectIndex](const auto& pair) { return pair.index() == simObjectIndex; });
if (it != hitToCaloParticleMap[hitIndex].end() and it != hitToSimClusterMap[hitIndex].end()) {
if ((isSimTracksterFromCP and it != hitToCaloParticleMap[hitIndex].end()) or
(!isSimTracksterFromCP and it != hitToSimClusterMap[hitIndex].end())) {
simFractions[i] = it->fraction();
}
float simFraction = simFractions[i];
const auto& recHit = rechitManager[hitIndex];
float rechitEnergy = recHit.energy();
float squaredSimFraction = simFraction * simFraction;
float squaredRecHitEnergy = recHit.energy() * recHit.energy();
float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;

const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex];
for (const auto& recoTracksterElement : hitToRecoTracksterVec) {
unsigned int recoTracksterIndex = recoTracksterElement.index();
Expand Down Expand Up @@ -333,22 +335,22 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
}
}

assert(simToRecoScoresDenominator > 0.f);
const float invDenominator = 1.f / simToRecoScoresDenominator;

for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
const auto& hitIndex = simTracksterHitsAndFractions[i].index();
float simFraction = simFractions[i];
const auto& recHit = rechitManager[hitIndex];
float rechitEnergy = recHit.energy();
float squaredSimFraction = simFraction * simFraction;
float squaredRecHitEnergy = recHit.energy() * recHit.energy();
float simSharedEnergy = recHit.energy() * simFraction;

float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
float simSharedEnergy = rechitEnergy * simFraction;
const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i];
for (const auto& recoTracksterElement : hitToRecoTracksterVec) {
auto recoTracksterIndex = recoTracksterElement.index();
float recoFraction = recoTracksterElement.fraction();
edm::Ref<std::vector<ticl::Trackster>> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex);
float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy);
float sharedEnergy = std::min(recoFraction * rechitEnergy, simSharedEnergy);
float squaredFraction =
std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
float score = invDenominator * squaredFraction * squaredRecHitEnergy;
Expand All @@ -357,9 +359,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
}
}

auto sortingFunc = [](const auto& a, const auto& b) {
if (a.score() != b.score())
return a.score() < b.score();
else
return a.index() < b.index();
};

// Sort the maps by score in ascending order
tracksterToSimTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });
simTracksterToTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });
tracksterToSimTracksterMap->sort(sortingFunc);
simTracksterToTracksterMap->sort(sortingFunc);

// After populating the maps, store them in the event
iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AllTracksterToSimTracksterAssociatorsByLCsProducer::AllTracksterToSimTracksterAs
const auto& tracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("tracksterCollections");
for (const auto& tag : tracksterCollections) {
std::string label = tag.label();
if (tag.instance() != "") {
if (!tag.instance().empty()) {
label += tag.instance();
}
tracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
Expand All @@ -57,7 +57,7 @@ AllTracksterToSimTracksterAssociatorsByLCsProducer::AllTracksterToSimTracksterAs
const auto& simTracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("simTracksterCollections");
for (const auto& tag : simTracksterCollections) {
std::string label = tag.label();
if (tag.instance() != "") {
if (!tag.instance().empty()) {
label += tag.instance();
}
simTracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
Expand Down Expand Up @@ -244,7 +244,7 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID,
}
}
}

assert(simToRecoScoresDenominator > 0.f);
const float invDenominator = 1.f / simToRecoScoresDenominator;

for (unsigned int i = 0; i < layerClustersIds.size(); ++i) {
Expand All @@ -269,9 +269,16 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID,
}
}
}
auto sortingFunc = [](const auto& a, const auto& b) {
if (a.score() != b.score())
return a.score() < b.score();
else
return a.index() < b.index();
};

// Sort the maps by score in ascending order
tracksterToSimTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });
simTracksterToTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });
tracksterToSimTracksterMap->sort(sortingFunc);
simTracksterToTracksterMap->sort(sortingFunc);

// After populating the maps, store them in the event
iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first);
Expand Down
6 changes: 4 additions & 2 deletions Validation/HGCalValidation/interface/HGCalValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "SimDataFormats/Associations/interface/LayerClusterToSimClusterAssociator.h"
#include "SimDataFormats/Associations/interface/TICLAssociationMap.h"

#include "CommonTools/RecoAlgos/interface/MultiVectorManager.h"

class PileupSummaryInfo;

struct HGCalValidatorHistograms {
Expand Down Expand Up @@ -65,7 +67,7 @@ class HGCalValidator : public DQMGlobalEDAnalyzer<HGCalValidatorHistograms> {
std::vector<size_t>& selected_cPeff,
unsigned int layers,
std::unordered_map<DetId, const unsigned int> const&,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

Expand Down Expand Up @@ -111,7 +113,7 @@ class HGCalValidator : public DQMGlobalEDAnalyzer<HGCalValidatorHistograms> {
edm::EDGetTokenT<ticl::RecoToSimCollectionWithSimClusters> associatorMapRtSim;
std::unique_ptr<HGVHistoProducerAlgo> histoProducerAlgo_;
std::vector<edm::InputTag> hits_label_;
std::vector<edm::EDGetTokenT<HGCRecHitCollection>> hits_token_;
std::vector<edm::EDGetTokenT<HGCRecHitCollection>> hits_tokens_;
std::unique_ptr<TICLCandidateValidator> candidateVal_;
std::vector<edm::EDGetTokenT<TracksterToTracksterMap>> tracksterToTracksterAssociatorsTokens_;
std::vector<edm::EDGetTokenT<TracksterToTracksterMap>> tracksterToTracksterByHitsAssociatorsTokens_;
Expand Down
16 changes: 9 additions & 7 deletions Validation/HGCalValidation/interface/HGVHistoProducerAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "DataFormats/HGCalReco/interface/Trackster.h"

#include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
#include "CommonTools/RecoAlgos/interface/MultiVectorManager.h"

#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
#include "SimDataFormats/Vertex/interface/SimVertex.h"
#include "RecoLocalCalo/HGCalRecProducers/interface/HGCalClusteringAlgoBase.h"
Expand Down Expand Up @@ -280,7 +282,7 @@ class HGVHistoProducerAlgo {
unsigned int layers,
const ticl::RecoToSimCollection& recSimColl,
const ticl::SimToRecoCollection& simRecColl,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;
void layerClusters_to_SimClusters(const Histograms& histograms,
const int count,
edm::Handle<reco::CaloClusterCollection> clusterHandle,
Expand All @@ -293,7 +295,7 @@ class HGVHistoProducerAlgo {
unsigned int layers,
const ticl::RecoToSimCollectionWithSimClusters& recSimColl,
const ticl::SimToRecoCollectionWithSimClusters& simRecColl,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;

void tracksters_to_SimTracksters_fp(const Histograms& histograms,
const int count,
Expand All @@ -312,7 +314,7 @@ class HGVHistoProducerAlgo {
std::vector<SimVertex> const& simVertices,
unsigned int layers,
std::unordered_map<DetId, const unsigned int> const&,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;
void fill_generic_cluster_histos(const Histograms& histograms,
const int count,
edm::Handle<reco::CaloClusterCollection> clusterHandle,
Expand All @@ -327,7 +329,7 @@ class HGVHistoProducerAlgo {
std::vector<int> thicknesses,
const ticl::RecoToSimCollection& recSimColl,
const ticl::SimToRecoCollection& simRecColl,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;
void fill_simCluster_histos(const Histograms& histograms,
std::vector<SimCluster> const& simClusters,
unsigned int layers,
Expand All @@ -344,7 +346,7 @@ class HGVHistoProducerAlgo {
unsigned int layers,
const ticl::RecoToSimCollectionWithSimClusters& recSimColl,
const ticl::SimToRecoCollectionWithSimClusters& simRecColl,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;
void fill_cluster_histos(const Histograms& histograms, const int count, const reco::CaloCluster& cluster) const;
void fill_trackster_histos(const Histograms& histograms,
const int count,
Expand All @@ -360,7 +362,7 @@ class HGVHistoProducerAlgo {
std::vector<size_t> const& cPSelectedIndices,
std::unordered_map<DetId, const unsigned int> const& hitMap,
unsigned int layers,
std::vector<HGCRecHit> const& hits,
MultiVectorManager<HGCRecHit> const& hits,
bool mapsFound,
const edm::Handle<TracksterToTracksterMap>& trackstersToSimTrackstersByLCsMapH,
const edm::Handle<TracksterToTracksterMap>& simTrackstersToTrackstersByLCsMapH,
Expand All @@ -378,7 +380,7 @@ class HGVHistoProducerAlgo {

DetId findmaxhit(const reco::CaloCluster& cluster,
std::unordered_map<DetId, const unsigned int> const&,
std::vector<HGCRecHit> const& hits) const;
MultiVectorManager<HGCRecHit> const& hits) const;

struct detIdInfoInCluster {
bool operator==(const detIdInfoInCluster& o) const { return clusterId == o.clusterId; };
Expand Down
Loading