From 984b5805e01177f209e72b22cc679daf2ac9c967 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Wed, 13 Nov 2024 14:10:01 +0100 Subject: [PATCH 01/10] Enhance TICLAssociationMap --- .../interface/TICLAssociationMap.h | 280 +++++++++++++----- SimDataFormats/Associations/src/classes.h | 2 + .../Associations/src/classes_def.xml | 70 ++++- 3 files changed, 262 insertions(+), 90 deletions(-) diff --git a/SimDataFormats/Associations/interface/TICLAssociationMap.h b/SimDataFormats/Associations/interface/TICLAssociationMap.h index 492ce7e4e803b..c1553767a3cbd 100644 --- a/SimDataFormats/Associations/interface/TICLAssociationMap.h +++ b/SimDataFormats/Associations/interface/TICLAssociationMap.h @@ -7,6 +7,7 @@ #include #include #include +#include // CMSSW specific includes #include "DataFormats/Common/interface/Ref.h" @@ -15,23 +16,142 @@ namespace ticl { - // Define the possible map types - using mapWithFraction = std::vector>>; - using mapWithFractionAndScore = std::vector>>>; - using oneToOneMapWithFraction = std::vector>; - using oneToOneMapWithFractionAndScore = std::vector>>; + // Define wrapper types to differentiate between fraction and shared energy + struct FractionType { + float value; + FractionType(float v = 0.0f) : value(v) {} + FractionType& operator+=(float v) { + value += v; + return *this; + } + }; + + struct SharedEnergyType { + float value; + SharedEnergyType(float v = 0.0f) : value(v) {} + SharedEnergyType& operator+=(float v) { + value += v; + return *this; + } + }; + + // AssociationElement class to store index and value, and provide methods directly + template + class AssociationElement { + public: + using value_type = V; + + AssociationElement(unsigned int idx = 0, V val = V()) : index_(idx), value_(val) {} + + unsigned int index() const { return index_; } + + // Enable fraction() if ValueType is FractionType + template , int> = 0> + float fraction() const { + return value_.value; + } + + // Enable sharedEnergy() if ValueType is SharedEnergyType + template , int> = 0> + float sharedEnergy() const { + return value_.value; + } + + // Enable fraction() and score() if ValueType is std::pair + template >, int> = 0> + float fraction() const { + return value_.first.value; + } + template >, int> = 0> + float score() const { + return value_.second; + } + + // Enable sharedEnergy() and score() if ValueType is std::pair + template >, int> = 0> + float sharedEnergy() const { + return value_.first.value; + } + template >, int> = 0> + float score() const { + return value_.second; + } + + // Method to accumulate values + void accumulate(const V& other_value) { + if constexpr (std::is_same_v || std::is_same_v) { + value_.value += other_value.value; + } else if constexpr (std::is_same_v> || + std::is_same_v>) { + value_.first.value += other_value.first.value; + value_.second += other_value.second; + } + } + + private: + unsigned int index_; + V value_; + }; + + // Type traits to differentiate between one-to-one and one-to-many maps + template + struct MapTraits; + + template + struct MapTraits>> { + static constexpr bool is_one_to_one = true; + using AssociationElementType = AssociationElement; + using ValueType = V; + }; + + template + struct MapTraits>>> { + static constexpr bool is_one_to_one = false; + using AssociationElementType = AssociationElement; + using ValueType = V; + }; + + // Trait to check if V is a std::pair (i.e., has a score) + template + struct IsValueTypeWithScore : std::false_type {}; + + template + struct IsValueTypeWithScore> : std::true_type {}; + + // Define map types using AssociationElement and container types + using mapWithFraction = std::vector>>; + using mapWithFractionAndScore = std::vector>>>; + using oneToOneMapWithFraction = std::vector>; + using oneToOneMapWithFractionAndScore = std::vector>>; + + using mapWithSharedEnergy = std::vector>>; + using mapWithSharedEnergyAndScore = std::vector>>>; + using oneToOneMapWithSharedEnergy = std::vector>; + using oneToOneMapWithSharedEnergyAndScore = std::vector>>; + // AssociationMap class templated on MapType template class AssociationMap { private: + MapType map_; + // Type alias for conditionally including collectionRefProds using CollectionRefProdType = typename std::conditional_t || std::is_void_v, std::monostate, std::pair, edm::RefProd>>; + CollectionRefProdType collectionRefProds; + + // Traits to deduce AssociationElementType and ValueType + using Traits = MapTraits; + using AssociationElementType = typename Traits::AssociationElementType; + using V = typename Traits::ValueType; + static constexpr bool is_one_to_one = Traits::is_one_to_one; + public: AssociationMap() : collectionRefProds() {} + // Constructor for generic use template ::value) { - if (index1 >= map_.size()) { - map_.resize(index1 + 1); - } - auto& vec = map_[index1]; - auto it = std::find_if(vec.begin(), vec.end(), [&](const auto& pair) { return pair.first == index2; }); - if (it != vec.end()) { - it->second += fraction; - } else { - vec.emplace_back(index2, fraction); - } - } else if constexpr (std::is_same::value) { - if (index1 >= map_.size()) { - map_.resize(index1 + 1); - } + void insert(unsigned int index1, unsigned int index2, float fraction_or_energy, float score = 0.0f) { + assert(index1 < map_.size()); + V value; + if constexpr (IsValueTypeWithScore::value) { + using FirstType = typename V::first_type; + value = V(FirstType(fraction_or_energy), score); + } else { + value = V(fraction_or_energy); + } + AssociationElementType element(index2, value); + + if constexpr (is_one_to_one) { + map_[index1] = element; + } else { auto& vec = map_[index1]; - auto it = std::find_if(vec.begin(), vec.end(), [&](const auto& pair) { return pair.first == index2; }); + auto it = + std::find_if(vec.begin(), vec.end(), [&](const AssociationElementType& e) { return e.index() == index2; }); if (it != vec.end()) { - it->second.first += fraction; - it->second.second += score; - } else { - vec.emplace_back(index2, std::make_pair(fraction, score)); - } - } else if constexpr (std::is_same::value) { - auto it = std::find_if(map_.begin(), map_.end(), [&](const auto& pair) { return pair.first == index1; }); - if (it != map_.end()) { - it->second += fraction; - } else { - map_.emplace_back(index1, fraction); - } - } else if constexpr (std::is_same::value) { - auto it = std::find_if(map_.begin(), map_.end(), [&](const auto& pair) { return pair.first == index1; }); - if (it != map_.end()) { - it->second.first += fraction; - it->second.second += score; + // Accumulate value + it->accumulate(value); } else { - map_.emplace_back(index1, std::make_pair(fraction, score)); + vec.push_back(element); } } } @@ -131,49 +235,43 @@ namespace ticl { template && !std::is_void_v, int> = 0> - void insert(const edm::Ref& ref1, const edm::Ref& ref2, float fraction, float score = 0.0f) { - insert(ref1.key(), ref2.key(), fraction, score); + void insert(const edm::Ref& ref1, const edm::Ref& ref2, float fraction_or_energy, float score = 0.0f) { + insert(ref1.key(), ref2.key(), fraction_or_energy, score); } void sort(bool byScore = false) { - static_assert(!std::is_same_v && - !std::is_same_v, - "Sort is not applicable for one-to-one maps"); - - if constexpr (std::is_same_v) { - for (auto& vec : map_) { - std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second > b.second; }); - } - } else if constexpr (std::is_same_v) { + if constexpr (is_one_to_one) { + // Sorting not applicable for one-to-one maps + } else { for (auto& vec : map_) { if (byScore) { - std::sort( - vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second.second > b.second.second; }); + std::sort(vec.begin(), vec.end(), [](const AssociationElementType& a, const AssociationElementType& b) { + return a.score() > b.score(); + }); } else { - std::sort( - vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second.first > b.second.first; }); + if constexpr (std::is_same_v || std::is_same_v>) { + std::sort(vec.begin(), vec.end(), [](const AssociationElementType& a, const AssociationElementType& b) { + return a.fraction() > b.fraction(); + }); + } else { + std::sort(vec.begin(), vec.end(), [](const AssociationElementType& a, const AssociationElementType& b) { + return a.sharedEnergy() > b.sharedEnergy(); + }); + } } } } } + // Access methods + const auto& operator[](unsigned int index1) const { return map_[index1]; } + auto& operator[](unsigned int index1) { return map_[index1]; } - const auto& operator[](unsigned int index1) const { return map_[index1]; } + const auto& at(unsigned int index1) const { return map_.at(index1); } - const auto& at(unsigned int index1) const { - if (index1 >= map_.size()) { - throw std::out_of_range("Index out of range"); - } - return map_[index1]; - } + auto& at(unsigned int index1) { return map_.at(index1); } - auto& at(unsigned int index1) { - if (index1 >= map_.size()) { - throw std::out_of_range("Index out of range"); - } - return map_[index1]; - } // CMSSW-specific resize method template size()); } - // Constructor for generic use + + // Generic resize method template && std::is_void_v, int> = 0> @@ -193,23 +292,44 @@ namespace ticl { void print(std::ostream& os) const { for (size_t i = 0; i < map_.size(); ++i) { os << "Index " << i << ":\n"; - for (const auto& pair : map_[i]) { - os << " (" << pair.first << ", "; - if constexpr (std::is_same::value || - std::is_same::value) { - os << pair.second.first << ", " << pair.second.second; + if constexpr (is_one_to_one) { + const auto& elem = map_[i]; + os << " (" << elem.index() << ", "; + if constexpr (IsValueTypeWithScore::value) { + if constexpr (std::is_same_v) { + os << "Fraction: " << elem.fraction() << ", Score: " << elem.score(); + } else { + os << "SharedEnergy: " << elem.sharedEnergy() << ", Score: " << elem.score(); + } } else { - os << pair.second; + if constexpr (std::is_same_v) { + os << "Fraction: " << elem.fraction(); + } else if constexpr (std::is_same_v) { + os << "SharedEnergy: " << elem.sharedEnergy(); + } } os << ")\n"; + } else { + for (const auto& elem : map_[i]) { + os << " (" << elem.index() << ", "; + if constexpr (IsValueTypeWithScore::value) { + if constexpr (std::is_same_v) { + os << "Fraction: " << elem.fraction() << ", Score: " << elem.score(); + } else { + os << "SharedEnergy: " << elem.sharedEnergy() << ", Score: " << elem.score(); + } + } else { + if constexpr (std::is_same_v) { + os << "Fraction: " << elem.fraction(); + } else if constexpr (std::is_same_v) { + os << "SharedEnergy: " << elem.sharedEnergy(); + } + } + os << ")\n"; + } } } } - - private: - // For CMSSW-specific use - CollectionRefProdType collectionRefProds; - MapType map_; // Store the map directly }; } // namespace ticl diff --git a/SimDataFormats/Associations/src/classes.h b/SimDataFormats/Associations/src/classes.h index 792266ae1bdda..a240d5382b5a5 100644 --- a/SimDataFormats/Associations/src/classes.h +++ b/SimDataFormats/Associations/src/classes.h @@ -24,3 +24,5 @@ #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h" #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h" #include "SimDataFormats/Associations/interface/TICLAssociationMap.h" +#include "DataFormats/Common/interface/RefProd.h" +#include "DataFormats/Common/interface/Ref.h" diff --git a/SimDataFormats/Associations/src/classes_def.xml b/SimDataFormats/Associations/src/classes_def.xml index ec07f75e23b37..9b48d619fae60 100644 --- a/SimDataFormats/Associations/src/classes_def.xml +++ b/SimDataFormats/Associations/src/classes_def.xml @@ -30,16 +30,66 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From ee2eba244768597b798cf36bd4c6908d7a1914fb Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Fri, 22 Nov 2024 09:19:04 +0100 Subject: [PATCH 02/10] Fix insertion, rewrite sort to become reproducible and use custom comparator --- .../interface/TICLAssociationMap.h | 96 ++++++++++++++++--- 1 file changed, 82 insertions(+), 14 deletions(-) diff --git a/SimDataFormats/Associations/interface/TICLAssociationMap.h b/SimDataFormats/Associations/interface/TICLAssociationMap.h index c1553767a3cbd..62c776744d522 100644 --- a/SimDataFormats/Associations/interface/TICLAssociationMap.h +++ b/SimDataFormats/Associations/interface/TICLAssociationMap.h @@ -9,6 +9,8 @@ #include #include +#include + // CMSSW specific includes #include "DataFormats/Common/interface/Ref.h" #include "DataFormats/Common/interface/RefProd.h" @@ -40,11 +42,27 @@ namespace ticl { class AssociationElement { public: using value_type = V; - - AssociationElement(unsigned int idx = 0, V val = V()) : index_(idx), value_(val) {} + AssociationElement() : index_(std::numeric_limits::max()) { + if constexpr (std::is_same_v || std::is_same_v) { + value_.value = -1.f; + } else if constexpr (std::is_same_v> || + std::is_same_v>) { + value_.first.value = -1.f; + } + } + AssociationElement(unsigned int index, const V& value) : index_(index), value_(value) {} unsigned int index() const { return index_; } + bool isValid() const { + if constexpr (std::is_same_v || std::is_same_v) { + return value_.value >= 0.f; + } else if constexpr (std::is_same_v> || + std::is_same_v>) { + return value_.first.value >= 0.f; + } + } + // Enable fraction() if ValueType is FractionType template , int> = 0> float fraction() const { @@ -87,6 +105,11 @@ namespace ticl { value_.second += other_value.second; } } + bool operator==(const AssociationElement& other) const { + return index_ == other.index_ && value_.value == other.value_.value; + } + + bool operator!=(const AssociationElement& other) const { return !(*this == other); } private: unsigned int index_; @@ -115,8 +138,8 @@ namespace ticl { template struct IsValueTypeWithScore : std::false_type {}; - template - struct IsValueTypeWithScore> : std::true_type {}; + template + struct IsValueTypeWithScore> : std::true_type {}; // Define map types using AssociationElement and container types using mapWithFraction = std::vector>>; @@ -182,6 +205,8 @@ namespace ticl { const MapType& getMap() const { return map_; } + const auto size() const { return map_.size(); } + // CMSSW-specific method to get references template b.score(); + if (byScore && IsValueTypeWithScore::value) { + std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { + if (a.score() != b.score()) { + return a.score() > b.score(); + } else { + return a.index() < b.index(); + } }); } else { if constexpr (std::is_same_v || std::is_same_v>) { - std::sort(vec.begin(), vec.end(), [](const AssociationElementType& a, const AssociationElementType& b) { - return a.fraction() > b.fraction(); + std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { + if (a.fraction() != b.fraction()) { + return a.fraction() > b.fraction(); + } else { + return a.index() < b.index(); + } }); } else { - std::sort(vec.begin(), vec.end(), [](const AssociationElementType& a, const AssociationElementType& b) { - return a.sharedEnergy() > b.sharedEnergy(); + std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { + if (a.sharedEnergy() != b.sharedEnergy()) { + return a.sharedEnergy() > b.sharedEnergy(); + } else { + return a.index() < b.index(); + } }); } } @@ -263,14 +300,40 @@ namespace ticl { } } + // Overload of sort() that accepts a custom comparator + template + void sort(Compare comp) { + if constexpr (is_one_to_one) { + // Sorting not applicable for one-to-one maps + } else { + for (auto& vec : map_) { + std::sort(vec.begin(), vec.end(), comp); + } + } + } + // Access methods const auto& operator[](unsigned int index1) const { return map_[index1]; } auto& operator[](unsigned int index1) { return map_[index1]; } - const auto& at(unsigned int index1) const { return map_.at(index1); } + const auto& at(unsigned int index1) const { + const auto& elem = map_.at(index1); + if (!elem.isValid()) { + throw std::out_of_range("Attempted to access an unset element in AssociationMap. Element index: " + + std::to_string(index1)); + } + return elem; + } - auto& at(unsigned int index1) { return map_.at(index1); } + auto& at(unsigned int index1) { + auto& elem = map_.at(index1); + if (!elem.isValid()) { + throw std::out_of_range("Attempted to access an unset element in AssociationMap. Element index: " + + std::to_string(index1)); + } + return elem; + } // CMSSW-specific resize method template ::value) { if constexpr (std::is_same_v) { @@ -310,6 +377,7 @@ namespace ticl { } os << ")\n"; } else { + os << "Index " << i << ":\n"; for (const auto& elem : map_[i]) { os << " (" << elem.index() << ", "; if constexpr (IsValueTypeWithScore::value) { From 12980bf470951cf9f704712f954ea6cbfd3e9489 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Wed, 13 Nov 2024 15:18:18 +0100 Subject: [PATCH 03/10] update Associators with new ticl::AssociationMap --- ...erClusterToTracksterAssociatorsProducer.cc | 5 +- ...ToSimTracksterAssociatorsByHitsProducer.cc | 69 +++++++++++-------- ...rToSimTracksterAssociatorsByLCsProducer.cc | 58 +++++++++------- .../plugins/LCToTSAssociatorProducer.cc | 5 +- 4 files changed, 81 insertions(+), 56 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllLayerClusterToTracksterAssociatorsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllLayerClusterToTracksterAssociatorsProducer.cc index a0d5cae0a429e..4abf2bd1b46f4 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllLayerClusterToTracksterAssociatorsProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllLayerClusterToTracksterAssociatorsProducer.cc @@ -40,7 +40,8 @@ AllLayerClusterToTracksterAssociatorsProducer::AllLayerClusterToTracksterAssocia // Produce separate association maps for each trackster collection using the trackster label for (const auto& tracksterToken : tracksterCollectionTokens_) { - produces, std::vector>>( + produces< + ticl::AssociationMap, std::vector>>( tracksterToken.first); } } @@ -59,7 +60,7 @@ void AllLayerClusterToTracksterAssociatorsProducer::produce(edm::StreamID, // Create association map auto lcToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + ticl::AssociationMap, std::vector>>( layer_clusters, tracksters, iEvent); // Loop over tracksters diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc index 62cdbea6dedc6..64b50469941e6 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc @@ -91,11 +91,11 @@ AllTracksterToSimTracksterAssociatorsByHitsProducer::AllTracksterToSimTracksterA for (const auto& tracksterToken : tracksterCollectionTokens_) { for (const auto& simTracksterToken : simTracksterCollectionTokens_) { std::string instanceLabel = tracksterToken.first + "To" + simTracksterToken.first; - produces, std::vector>>(instanceLabel); std::string reverseInstanceLabel = simTracksterToken.first + "To" + tracksterToken.first; - produces, std::vector>>(reverseInstanceLabel); } @@ -180,11 +180,13 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, const auto& simTracksterToHitMap = *simTracksterToHitMapHandle; // Create the association maps - auto tracksterToSimTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + auto tracksterToSimTracksterMap = std::make_unique, + std::vector>>( recoTrackstersHandle, simTrackstersHandle, iEvent); - auto simTracksterToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + auto simTracksterToTracksterMap = std::make_unique, + std::vector>>( simTrackstersHandle, recoTrackstersHandle, iEvent); for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) { @@ -195,7 +197,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, recoTracksterHitsAndFractions.size()); std::vector associatedSimTracksterIndices; for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[i]; + const auto& hitElement = recoTracksterHitsAndFractions[i]; + unsigned int hitIndex = hitElement.index(); + float recoFraction = hitElement.fraction(); const auto& recHit = rechitManager[hitIndex]; float squaredRecoFraction = recoFraction * recoFraction; float rechitEnergy = recHit.energy(); @@ -203,25 +207,27 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy; const auto& hitToSimTracksterVec = hitToSimTracksterMap[hitIndex]; - for (const auto& [simTracksterIndex, fraction] : hitToSimTracksterVec) { + for (const auto& simTracksterElement : hitToSimTracksterVec) { + auto simTracksterIndex = simTracksterElement.index(); const auto& simTrackster = simTracksters[simTracksterIndex]; auto& seed = simTrackster.seedID(); float simFraction = 0; if (seed == caloParticlesHandle.id()) { unsigned int caloParticleIndex = simTrackster.seedIndex(); - auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(), - hitToCaloParticleMap[hitIndex].end(), - [caloParticleIndex](const auto& pair) { return pair.first == caloParticleIndex; }); + auto it = + std::find_if(hitToCaloParticleMap[hitIndex].begin(), + hitToCaloParticleMap[hitIndex].end(), + [caloParticleIndex](const auto& pair) { return pair.index() == caloParticleIndex; }); if (it != hitToCaloParticleMap[hitIndex].end()) { - simFraction = it->second; + simFraction = it->fraction(); } } else { unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex(); auto it = std::find_if(hitToSimClusterMap[hitIndex].begin(), hitToSimClusterMap[hitIndex].end(), - [simClusterIndex](const auto& pair) { return pair.first == simClusterIndex; }); + [simClusterIndex](const auto& pair) { return pair.index() == simClusterIndex; }); if (it != hitToSimClusterMap[hitIndex].end()) { - simFraction = it->second; + simFraction = it->fraction(); } } hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, simFraction); @@ -235,11 +241,11 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, // Add missing sim tracksters with 0 shared energy to hitToAssociatedSimTracksterMap for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - unsigned int hitId = recoTracksterHitsAndFractions[i].first; + unsigned int hitId = recoTracksterHitsAndFractions[i].index(); const auto& simTracksterVec = hitToSimTracksterMap[hitId]; for (unsigned int simTracksterIndex : associatedSimTracksterIndices) { if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) { - return pair.first == simTracksterIndex; + return pair.index() == simTracksterIndex; }) == simTracksterVec.end()) { hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, 0); } @@ -249,14 +255,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, const float invDenominator = 1.f / recoToSimScoresDenominator; for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - unsigned int hitIndex = recoTracksterHitsAndFractions[i].first; + unsigned int hitIndex = recoTracksterHitsAndFractions[i].index(); const auto& recHit = rechitManager[hitIndex]; - float recoFraction = recoTracksterHitsAndFractions[i].second; + float recoFraction = recoTracksterHitsAndFractions[i].fraction(); float squaredRecoFraction = recoFraction * recoFraction; float squaredRecHitEnergy = recHit.energy() * recHit.energy(); float recoSharedEnergy = recHit.energy() * recoFraction; const auto& simTracksterVec = hitToAssociatedSimTracksterMap[i]; - for (const auto& [simTracksterIndex, simFraction] : simTracksterVec) { + for (const auto& simTracksterElement : simTracksterVec) { + auto simTracksterIndex = simTracksterElement.index(); + auto simFraction = simTracksterElement.fraction(); edm::Ref> simTracksterRef(simTrackstersHandle, simTracksterIndex); float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy); float squaredFraction = @@ -281,17 +289,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, bool isSimTracksterFromCP = (seed == caloParticlesHandle.id()); std::vector simFractions(simTracksterHitsAndFractions.size(), 0.f); for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; - + auto hitIndex = simTracksterHitsAndFractions[i].index(); auto it = isSimTracksterFromCP ? (std::find_if(hitToCaloParticleMap[hitIndex].begin(), hitToCaloParticleMap[hitIndex].end(), - [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; })) + [simObjectIndex](const auto& pair) { return pair.index() == simObjectIndex; })) : std::find_if(hitToSimClusterMap[hitIndex].begin(), hitToSimClusterMap[hitIndex].end(), - [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; }); + [simObjectIndex](const auto& pair) { return pair.index() == simObjectIndex; }); if (it != hitToCaloParticleMap[hitIndex].end() and it != hitToSimClusterMap[hitIndex].end()) { - simFractions[i] = it->second; + simFractions[i] = it->fraction(); } float simFraction = simFractions[i]; const auto& recHit = rechitManager[hitIndex]; @@ -300,7 +307,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy; const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { + for (const auto& recoTracksterElement : hitToRecoTracksterVec) { + unsigned int recoTracksterIndex = recoTracksterElement.index(); + float recoFraction = recoTracksterElement.fraction(); hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, recoFraction); associatedRecoTracksterIndices.push_back(recoTracksterIndex); } @@ -312,12 +321,12 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, associatedRecoTracksterIndices.end()); for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - unsigned int hitIndex = simTracksterHitsAndFractions[i].first; + unsigned int hitIndex = simTracksterHitsAndFractions[i].index(); const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) { if (std::find_if( hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](const auto& pair) { - return pair.first == recoTracksterIndex; + return pair.index() == recoTracksterIndex; }) == hitToRecoTracksterVec.end()) { hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, 0); } @@ -327,7 +336,7 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, const float invDenominator = 1.f / simToRecoScoresDenominator; for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; + const auto& hitIndex = simTracksterHitsAndFractions[i].index(); float simFraction = simFractions[i]; const auto& recHit = rechitManager[hitIndex]; float squaredSimFraction = simFraction * simFraction; @@ -335,7 +344,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, float simSharedEnergy = recHit.energy() * simFraction; const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { + for (const auto& recoTracksterElement : hitToRecoTracksterVec) { + auto recoTracksterIndex = recoTracksterElement.index(); + float recoFraction = recoTracksterElement.fraction(); edm::Ref> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex); float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy); float squaredFraction = diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc index 237b4635e4194..553b70481b034 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc @@ -28,12 +28,12 @@ class AllTracksterToSimTracksterAssociatorsByLCsProducer : public edm::global::E std::vector, std::vector>>>> + ticl::AssociationMap, std::vector>>>> layerClusterToTracksterMapTokens_; std::vector, std::vector>>>> + ticl::AssociationMap, std::vector>>>> layerClusterToSimTracksterMapTokens_; }; @@ -50,7 +50,7 @@ AllTracksterToSimTracksterAssociatorsByLCsProducer::AllTracksterToSimTracksterAs layerClusterToTracksterMapTokens_.emplace_back( label, consumes< - ticl::AssociationMap, std::vector>>( + ticl::AssociationMap, std::vector>>( edm::InputTag("allLayerClusterToTracksterAssociations", label))); } @@ -64,7 +64,7 @@ AllTracksterToSimTracksterAssociatorsByLCsProducer::AllTracksterToSimTracksterAs layerClusterToSimTracksterMapTokens_.emplace_back( label, consumes< - ticl::AssociationMap, std::vector>>( + ticl::AssociationMap, std::vector>>( edm::InputTag("allLayerClusterToTracksterAssociations", label))); } @@ -72,11 +72,11 @@ AllTracksterToSimTracksterAssociatorsByLCsProducer::AllTracksterToSimTracksterAs for (const auto& tracksterToken : tracksterCollectionTokens_) { for (const auto& simTracksterToken : simTracksterCollectionTokens_) { std::string instanceLabel = tracksterToken.first + "To" + simTracksterToken.first; - produces, std::vector>>(instanceLabel); std::string reverseInstanceLabel = simTracksterToken.first + "To" + tracksterToken.first; - produces, std::vector>>(reverseInstanceLabel); } @@ -98,7 +98,7 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, const auto& recoTracksters = *recoTrackstersHandle; // Retrieve the correct LayerClusterToTracksterMap for the current trackster collection - Handle, std::vector>> + Handle, std::vector>> layerClusterToTracksterMapHandle; auto tracksterMapTokenIter = std::find_if(layerClusterToTracksterMapTokens_.begin(), @@ -115,7 +115,8 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, const auto& simTracksters = *simTrackstersHandle; // Retrieve the correct LayerClusterToSimTracksterMap for the current simTrackster collection - Handle, std::vector>> + Handle< + ticl::AssociationMap, std::vector>> layerClusterToSimTracksterMapHandle; auto simTracksterMapTokenIter = std::find_if(layerClusterToSimTracksterMapTokens_.begin(), @@ -127,11 +128,13 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, const auto& layerClusterToSimTracksterMap = *layerClusterToSimTracksterMapHandle; // Create the association maps - auto tracksterToSimTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + auto tracksterToSimTracksterMap = std::make_unique, + std::vector>>( recoTrackstersHandle, simTrackstersHandle, iEvent); - auto simTracksterToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + auto simTracksterToTracksterMap = std::make_unique, + std::vector>>( simTrackstersHandle, recoTrackstersHandle, iEvent); for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) { @@ -139,7 +142,8 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, edm::Ref> recoTracksterRef(recoTrackstersHandle, tracksterIndex); const auto& layerClustersIds = recoTrackster.vertices(); float recoToSimScoresDenominator = 0.f; - ticl::mapWithFraction layerClusterToAssociatedSimTracksterMap(layerClustersIds.size()); + ticl::AssociationMap layerClusterToAssociatedSimTracksterMap( + layerClustersIds.size()); std::vector associatedSimTracksterIndices; for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { unsigned int layerClusterId = layerClustersIds[i]; @@ -149,8 +153,10 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); recoToSimScoresDenominator += squaredLayerClusterEnergy * squaredRecoFraction; const auto& simTracksterVec = layerClusterToSimTracksterMap[layerClusterId]; - for (const auto& [simTracksterIndex, simSharedEnergy] : simTracksterVec) { - layerClusterToAssociatedSimTracksterMap[i].push_back({simTracksterIndex, simSharedEnergy}); + for (const auto& simTracksterElement : simTracksterVec) { + auto simTracksterIndex = simTracksterElement.index(); + auto simSharedEnergy = simTracksterElement.sharedEnergy(); + layerClusterToAssociatedSimTracksterMap[i].emplace_back(simTracksterIndex, simSharedEnergy); associatedSimTracksterIndices.push_back(simTracksterIndex); } } @@ -167,7 +173,7 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, const auto& simTracksterVec = layerClusterToSimTracksterMap[layerClusterId]; for (unsigned int simTracksterIndex : associatedSimTracksterIndices) { if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) { - return pair.first == simTracksterIndex; + return pair.index() == simTracksterIndex; }) == simTracksterVec.end()) { layerClusterToAssociatedSimTracksterMap[i].push_back({simTracksterIndex, 0.f}); } @@ -185,7 +191,9 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, float recoSharedEnergy = layerCluster.energy() * recoFraction; float invLayerClusterEnergy = 1.f / layerCluster.energy(); const auto& simTracksterVec = layerClusterToAssociatedSimTracksterMap[i]; - for (const auto& [simTracksterIndex, simSharedEnergy] : simTracksterVec) { + for (const auto& simTracksterElement : simTracksterVec) { + auto simTracksterIndex = simTracksterElement.index(); + float simSharedEnergy = simTracksterElement.sharedEnergy(); edm::Ref> simTracksterRef(simTrackstersHandle, simTracksterIndex); float sharedEnergy = std::min(simSharedEnergy, recoSharedEnergy); float simFraction = simSharedEnergy * invLayerClusterEnergy; @@ -202,7 +210,7 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, edm::Ref> simTracksterRef(simTrackstersHandle, tracksterIndex); const auto& layerClustersIds = simTrackster.vertices(); float simToRecoScoresDenominator = 0.f; - ticl::mapWithFraction layerClusterToAssociatedTracksterMap(layerClustersIds.size()); + ticl::AssociationMap layerClusterToAssociatedTracksterMap(layerClustersIds.size()); std::vector associatedRecoTracksterIndices; for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { unsigned int layerClusterId = layerClustersIds[i]; @@ -212,8 +220,10 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); simToRecoScoresDenominator += squaredLayerClusterEnergy * squaredSimFraction; const auto& recoTracksterVec = layerClusterToTracksterMap[layerClusterId]; - for (const auto& [recoTracksterIndex, recoSharedEnergy] : recoTracksterVec) { - layerClusterToAssociatedTracksterMap[i].push_back({recoTracksterIndex, recoSharedEnergy}); + for (const auto& recoTracksterElement : recoTracksterVec) { + auto recoTracksterIndex = recoTracksterElement.index(); + auto recoSharedEnergy = recoTracksterElement.sharedEnergy(); + layerClusterToAssociatedTracksterMap[i].emplace_back(recoTracksterIndex, recoSharedEnergy); associatedRecoTracksterIndices.push_back(recoTracksterIndex); } } @@ -228,9 +238,9 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, const auto& recoTracksterVec = layerClusterToTracksterMap[layerClusterId]; for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) { if (std::find_if(recoTracksterVec.begin(), recoTracksterVec.end(), [recoTracksterIndex](const auto& pair) { - return pair.first == recoTracksterIndex; + return pair.index() == recoTracksterIndex; }) == recoTracksterVec.end()) { - layerClusterToAssociatedTracksterMap[i].push_back({recoTracksterIndex, 0.f}); + layerClusterToAssociatedTracksterMap[i].emplace_back(recoTracksterIndex, 0.f); } } } @@ -246,7 +256,9 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, float simSharedEnergy = layerCluster.energy() * simFraction; float invLayerClusterEnergy = 1.f / layerCluster.energy(); const auto& recoTracksterVec = layerClusterToAssociatedTracksterMap[i]; - for (const auto& [recoTracksterIndex, recoSharedEnergy] : recoTracksterVec) { + for (const auto& recoTracksterElement : recoTracksterVec) { + auto recoTracksterIndex = recoTracksterElement.index(); + float recoSharedEnergy = recoTracksterElement.sharedEnergy(); edm::Ref> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex); float sharedEnergy = std::min(recoSharedEnergy, simSharedEnergy); float recoFraction = recoSharedEnergy * invLayerClusterEnergy; diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/LCToTSAssociatorProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/LCToTSAssociatorProducer.cc index 6d44f84463e37..f4968e960d64c 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/LCToTSAssociatorProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/LCToTSAssociatorProducer.cc @@ -18,7 +18,8 @@ LCToTSAssociatorProducer::LCToTSAssociatorProducer(const edm::ParameterSet &pset : LCCollectionToken_(consumes>(pset.getParameter("layer_clusters"))), tracksterCollectionToken_( consumes>(pset.getParameter("tracksters"))) { - produces, std::vector>>(); + produces< + ticl::AssociationMap, std::vector>>(); } LCToTSAssociatorProducer::~LCToTSAssociatorProducer() {} @@ -39,7 +40,7 @@ void LCToTSAssociatorProducer::produce(edm::StreamID, edm::Event &iEvent, const // Create association map auto lcToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( + ticl::AssociationMap, std::vector>>( layer_clusters, tracksters, iEvent); // Loop over tracksters From 523be7f91dfa4745b085343234f362cbe37734e6 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Fri, 22 Nov 2024 09:19:52 +0100 Subject: [PATCH 04/10] Sort in ascending order by score --- .../AllTracksterToSimTracksterAssociatorsByHitsProducer.cc | 5 +++-- .../AllTracksterToSimTracksterAssociatorsByLCsProducer.cc | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc index 64b50469941e6..20d81fa1e19aa 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByHitsProducer.cc @@ -357,8 +357,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID, } } - tracksterToSimTracksterMap->sort(true); - simTracksterToTracksterMap->sort(true); + // 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(); }); // After populating the maps, store them in the event iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first); diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc index 553b70481b034..02992b166ed43 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/AllTracksterToSimTracksterAssociatorsByLCsProducer.cc @@ -269,8 +269,9 @@ void AllTracksterToSimTracksterAssociatorsByLCsProducer::produce(edm::StreamID, } } } - tracksterToSimTracksterMap->sort(true); - simTracksterToTracksterMap->sort(true); + // 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(); }); // After populating the maps, store them in the event iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first); From bac3a30a545b23a0514d13f206ffbee40f5b6ebb Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 14 Nov 2024 10:48:41 +0100 Subject: [PATCH 05/10] update TICLDumper with new ticl::AssociationMap --- RecoHGCal/TICL/plugins/TICLDumper.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/RecoHGCal/TICL/plugins/TICLDumper.cc b/RecoHGCal/TICL/plugins/TICLDumper.cc index 5a76532f36afe..208e3515ae4bc 100644 --- a/RecoHGCal/TICL/plugins/TICLDumper.cc +++ b/RecoHGCal/TICL/plugins/TICLDumper.cc @@ -62,7 +62,7 @@ #include "CommonTools/UtilAlgos/interface/TFileService.h" using TracksterToTracksterMap = - ticl::AssociationMap, std::vector>; + ticl::AssociationMap, std::vector>; // Helper class for geometry, magnetic field, etc class DetectorTools { public: @@ -514,10 +514,10 @@ class TracksterToSimTracksterAssociationHelper { recoToSim_sharedE.resize(numberOfTracksters); for (size_t i = 0; i < numberOfTracksters; ++i) { - for (const auto& [simTracksterIndex, sharedEnergyAndScorePair] : recoToSimMap[i]) { - recoToSim[i].push_back(simTracksterIndex); - recoToSim_sharedE[i].push_back(sharedEnergyAndScorePair.first); - recoToSim_score[i].push_back(sharedEnergyAndScorePair.second); + for (const auto& simTracksterElement : recoToSimMap[i]) { + recoToSim[i].push_back(simTracksterElement.index()); + recoToSim_sharedE[i].push_back(simTracksterElement.sharedEnergy()); + recoToSim_score[i].push_back(simTracksterElement.score()); } } @@ -528,10 +528,10 @@ class TracksterToSimTracksterAssociationHelper { simToReco_sharedE.resize(numberOfSimTracksters); for (size_t i = 0; i < numberOfSimTracksters; ++i) { - for (const auto& [recoTracksterIndex, sharedEnergyAndScorePair] : simToRecoMap[i]) { - simToReco[i].push_back(recoTracksterIndex); - simToReco_sharedE[i].push_back(sharedEnergyAndScorePair.first); - simToReco_score[i].push_back(sharedEnergyAndScorePair.second); + for (const auto& recoTracksterElement : simToRecoMap[i]) { + simToReco[i].push_back(recoTracksterElement.index()); + simToReco_sharedE[i].push_back(recoTracksterElement.sharedEnergy()); + simToReco_score[i].push_back(recoTracksterElement.score()); } } } From 5ce964cc58e34c9c30b41ffd093211762abcabea Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 14 Nov 2024 10:49:24 +0100 Subject: [PATCH 06/10] update Validation with new ticl::AssociationMap --- .../interface/HGCalValidator.h | 2 +- .../interface/HGVHistoProducerAlgo.h | 2 +- .../interface/TICLCandidateValidator.h | 2 +- .../src/HGVHistoProducerAlgo.cc | 18 ++++++-------- .../src/TICLCandidateValidator.cc | 24 +++++++++---------- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Validation/HGCalValidation/interface/HGCalValidator.h b/Validation/HGCalValidation/interface/HGCalValidator.h index 175ac5ef096fe..56d83bd0916c0 100644 --- a/Validation/HGCalValidation/interface/HGCalValidator.h +++ b/Validation/HGCalValidation/interface/HGCalValidator.h @@ -44,7 +44,7 @@ class HGCalValidator : public DQMGlobalEDAnalyzer { public: using Histograms = HGCalValidatorHistograms; using TracksterToTracksterMap = - ticl::AssociationMap, std::vector>; + ticl::AssociationMap, std::vector>; using SimClusterToCaloParticleMap = ticl::AssociationMap, std::vector>; diff --git a/Validation/HGCalValidation/interface/HGVHistoProducerAlgo.h b/Validation/HGCalValidation/interface/HGVHistoProducerAlgo.h index 216c1f89b9780..fbcc6934ef686 100644 --- a/Validation/HGCalValidation/interface/HGVHistoProducerAlgo.h +++ b/Validation/HGCalValidation/interface/HGVHistoProducerAlgo.h @@ -229,7 +229,7 @@ class HGVHistoProducerAlgo { typedef dqm::legacy::DQMStore DQMStore; typedef dqm::legacy::MonitorElement MonitorElement; using TracksterToTracksterMap = - ticl::AssociationMap, std::vector>; + ticl::AssociationMap, std::vector>; using SimClusterToCaloParticleMap = ticl::AssociationMap, std::vector>; enum validationType { byHits_CP = 0, byLCs, byLCs_CP, byHits }; diff --git a/Validation/HGCalValidation/interface/TICLCandidateValidator.h b/Validation/HGCalValidation/interface/TICLCandidateValidator.h index df96d3e1090c3..ae78a9c250e53 100644 --- a/Validation/HGCalValidation/interface/TICLCandidateValidator.h +++ b/Validation/HGCalValidation/interface/TICLCandidateValidator.h @@ -22,7 +22,7 @@ namespace ticl { using TracksterToTracksterMap = - ticl::AssociationMap, std::vector>; + ticl::AssociationMap, std::vector>; } struct TICLCandidateValidatorHistograms { diff --git a/Validation/HGCalValidation/src/HGVHistoProducerAlgo.cc b/Validation/HGCalValidation/src/HGVHistoProducerAlgo.cc index f828332400e47..2917ae9b759f0 100644 --- a/Validation/HGCalValidation/src/HGVHistoProducerAlgo.cc +++ b/Validation/HGCalValidation/src/HGVHistoProducerAlgo.cc @@ -2524,8 +2524,8 @@ void HGVHistoProducerAlgo::tracksters_to_SimTracksters_fp(const Histograms& hist const auto productID = simTS.seedID(); if (productID == cPHandle_id) { return simTS.seedIndex(); - } else { // SimTrackster from SimCluster - return int(scToCpMap[simTS.seedIndex()].first); + } else { + return int(scToCpMap.at(simTS.seedIndex()).index()); } }; @@ -2539,7 +2539,6 @@ void HGVHistoProducerAlgo::tracksters_to_SimTracksters_fp(const Histograms& hist float iTS_phi = trackster.barycenter().phi(); float iTS_en = trackster.raw_energy(); float iTS_pt = trackster.raw_pt(); - histograms.h_denom_trackster_eta[valType][count]->Fill(iTS_eta); histograms.h_denom_trackster_phi[valType][count]->Fill(iTS_phi); histograms.h_denom_trackster_en[valType][count]->Fill(iTS_en); @@ -2547,8 +2546,8 @@ void HGVHistoProducerAlgo::tracksters_to_SimTracksters_fp(const Histograms& hist // loop over trackstersToSimTrackstersMap[tracksterIndex] by index for (unsigned int i = 0; i < trackstersToSimTrackstersMap[tracksterIndex].size(); ++i) { - const auto& [sts_id, sharedEnergyScorePair] = trackstersToSimTrackstersMap[tracksterIndex][i]; - const auto& [sharedEnergy, score] = sharedEnergyScorePair; + auto sharedEnergy = trackstersToSimTrackstersMap[tracksterIndex][i].sharedEnergy(); + auto score = trackstersToSimTrackstersMap[tracksterIndex][i].score(); float sharedEnergyFraction = sharedEnergy / trackster.raw_energy(); if (i == 0) { histograms.h_score_trackster2bestCaloparticle[valType][count]->Fill(score); @@ -2590,10 +2589,9 @@ void HGVHistoProducerAlgo::tracksters_to_SimTracksters_fp(const Histograms& hist // only to the selected caloParaticles. for (unsigned int simTracksterIndex = 0; simTracksterIndex < nSimTracksters; ++simTracksterIndex) { const auto& simTrackster = *(simTrackstersToTrackstersMap.getRefFirst(simTracksterIndex)); - const auto& cpId = getCPId(simTrackster, cPHandle_id, scToCpMap); + const auto cpId = getCPId(simTrackster, cPHandle_id, scToCpMap); if (std::find(cPSelectedIndices.begin(), cPSelectedIndices.end(), cpId) == cPSelectedIndices.end()) continue; - const auto sts_eta = simTrackster.barycenter().eta(); const auto sts_phi = simTrackster.barycenter().phi(); const auto sts_en = simTrackster.raw_energy(); @@ -2613,11 +2611,9 @@ void HGVHistoProducerAlgo::tracksters_to_SimTracksters_fp(const Histograms& hist bool sts_considered_pure = false; for (unsigned int i = 0; i < simTrackstersToTrackstersMap[simTracksterIndex].size(); ++i) { - const auto& [tracksterId, sharedEnergyScorePair] = simTrackstersToTrackstersMap[simTracksterIndex][i]; - const auto& [sharedEnergy, score] = sharedEnergyScorePair; - + const auto sharedEnergy = simTrackstersToTrackstersMap[simTracksterIndex][i].sharedEnergy(); + const auto score = simTrackstersToTrackstersMap[simTracksterIndex][i].score(); float sharedEnergyFraction = sharedEnergy * inv_simtrackster_energy; - if (i == 0) { histograms.h_scorePur_caloparticle2trackster[valType][count]->Fill(score); histograms.h_sharedenergy_caloparticle2trackster_assoc[valType][count]->Fill(sharedEnergyFraction); diff --git a/Validation/HGCalValidation/src/TICLCandidateValidator.cc b/Validation/HGCalValidation/src/TICLCandidateValidator.cc index 6120780b68381..e10ff0cc6bc2d 100644 --- a/Validation/HGCalValidation/src/TICLCandidateValidator.cc +++ b/Validation/HGCalValidation/src/TICLCandidateValidator.cc @@ -365,10 +365,10 @@ void TICLCandidateValidator::fillCandidateHistos(const edm::Event& event, if (!ts_vec.empty()) { auto min_elem = std::min_element(ts_vec.begin(), ts_vec.end(), [](auto const& ts1_id_pair, auto const& ts2_id_pair) { - return ts1_id_pair.second.second < ts2_id_pair.second.second; + return ts1_id_pair.score() < ts2_id_pair.score(); }); - shared_energy = min_elem->second.first; - cand_idx = min_elem->first; + shared_energy = min_elem->sharedEnergy(); + cand_idx = min_elem->index(); } // no reco associated to sim if (cand_idx == -1) @@ -442,10 +442,10 @@ void TICLCandidateValidator::fillCandidateHistos(const edm::Event& event, if (!ts_vec.empty()) { auto min_elem = std::min_element(ts_vec.begin(), ts_vec.end(), [](auto const& ts1_id_pair, auto const& ts2_id_pair) { - return ts1_id_pair.second.second < ts2_id_pair.second.second; + return ts1_id_pair.score() < ts2_id_pair.score(); }); - shared_energy = min_elem->second.first; - cand_idx = min_elem->first; + shared_energy = min_elem->sharedEnergy(); + cand_idx = min_elem->index(); } // no reco associated to sim @@ -555,10 +555,10 @@ void TICLCandidateValidator::fillCandidateHistos(const edm::Event& event, if (!sts_vec.empty()) { auto min_elem = std::min_element(sts_vec.begin(), sts_vec.end(), [](auto const& sts1_id_pair, auto const& sts2_id_pair) { - return sts1_id_pair.second.second < sts2_id_pair.second.second; + return sts1_id_pair.score() < sts2_id_pair.score(); }); - shared_energy = min_elem->second.first; - simCand_idx = min_elem->first; + shared_energy = min_elem->sharedEnergy(); + simCand_idx = min_elem->index(); } if (simCand_idx == -1) @@ -641,10 +641,10 @@ void TICLCandidateValidator::fillCandidateHistos(const edm::Event& event, if (!sts_vec.empty()) { auto min_elem = std::min_element(sts_vec.begin(), sts_vec.end(), [](auto const& sts1_id_pair, auto const& sts2_id_pair) { - return sts1_id_pair.second.second < sts2_id_pair.second.second; + return sts1_id_pair.score() < sts2_id_pair.score(); }); - shared_energy = min_elem->second.first; - simCand_idx = min_elem->first; + shared_energy = min_elem->sharedEnergy(); + simCand_idx = min_elem->index(); } if (simCand_idx == -1) From b572cd714e667b8df976f64df18c5bb48c02026a Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 14 Nov 2024 10:47:10 +0100 Subject: [PATCH 07/10] remove old associators --- ...rToSimTracksterAssociatorByHitsProducer.cc | 446 ------------------ ...erToSimTracksterAssociatorByHitsProducer.h | 48 -- ...acksterToSimTracksterAssociatorProducer.cc | 202 -------- ...racksterToSimTracksterAssociatorProducer.h | 44 -- .../python/TSToSimTSAssociationByHits_cfi.py | 37 -- .../python/TSToSimTSAssociation_cfi.py | 1 - 6 files changed, 778 deletions(-) delete mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.cc delete mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.h delete mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.cc delete mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.h diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.cc deleted file mode 100644 index 69d0a0a07b1d4..0000000000000 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.cc +++ /dev/null @@ -1,446 +0,0 @@ -// Author: Felice Pantaleo, felice.pantaleo@cern.ch 06/2024 -#include "TracksterToSimTracksterAssociatorByHitsProducer.h" -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/MakerMacros.h" -#include "FWCore/Framework/interface/ESHandle.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "FWCore/Utilities/interface/EDGetToken.h" -#include "DataFormats/HGCalReco/interface/Trackster.h" -#include "SimDataFormats/Associations/interface/TICLAssociationMap.h" -#include "DataFormats/Provenance/interface/ProductID.h" -#include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h" -#include "CommonTools/RecoAlgos/interface/MultiVectorManager.h" -#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h" -#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h" - -TracksterToSimTracksterAssociatorByHitsProducer::TracksterToSimTracksterAssociatorByHitsProducer( - const edm::ParameterSet& pset) - : recoTracksterCollectionToken_( - consumes>(pset.getParameter("tracksters"))), - simTracksterCollectionToken_( - consumes>(pset.getParameter("simTracksters"))), - simTracksterFromCPCollectionToken_( - consumes>(pset.getParameter("simTrackstersFromCP"))), - hitToTracksterMapToken_( - consumes>(pset.getParameter("hitToTracksterMap"))), - hitToSimTracksterMapToken_(consumes>( - pset.getParameter("hitToSimTracksterMap"))), - hitToSimTracksterFromCPMapToken_(consumes>( - pset.getParameter("hitToSimTracksterFromCPMap"))), - hitToSimClusterMapToken_(consumes>( - pset.getParameter("hitToSimClusterMap"))), - hitToCaloParticleMapToken_(consumes>( - pset.getParameter("hitToCaloParticleMap"))), - tracksterToHitMapToken_( - consumes>(pset.getParameter("tracksterToHitMap"))), - simTracksterToHitMapToken_(consumes>( - pset.getParameter("simTracksterToHitMap"))), - simTracksterFromCPToHitMapToken_(consumes>( - pset.getParameter("simTracksterFromCPToHitMap"))), - caloParticleToken_(consumes>(pset.getParameter("caloParticles"))) { - auto hitsTags = pset.getParameter>("hits"); - for (const auto& tag : hitsTags) { - hitsTokens_.push_back(consumes(tag)); - } - produces< - ticl::AssociationMap, std::vector>>( - "tracksterToSimTracksterMap"); - produces< - ticl::AssociationMap, std::vector>>( - "simTracksterToTracksterMap"); - produces< - ticl::AssociationMap, std::vector>>( - "tracksterToSimTracksterFromCPMap"); - produces< - ticl::AssociationMap, std::vector>>( - "simTracksterFromCPToTracksterMap"); -} - -TracksterToSimTracksterAssociatorByHitsProducer::~TracksterToSimTracksterAssociatorByHitsProducer() {} - -void TracksterToSimTracksterAssociatorByHitsProducer::produce(edm::StreamID, - edm::Event& iEvent, - const edm::EventSetup& iSetup) const { - using namespace edm; - - Handle> recoTrackstersHandle; - iEvent.getByToken(recoTracksterCollectionToken_, recoTrackstersHandle); - const auto& recoTracksters = *recoTrackstersHandle; - - Handle> simTrackstersHandle; - iEvent.getByToken(simTracksterCollectionToken_, simTrackstersHandle); - const auto& simTracksters = *simTrackstersHandle; - - Handle> simTrackstersFromCPHandle; - iEvent.getByToken(simTracksterFromCPCollectionToken_, simTrackstersFromCPHandle); - const auto& simTrackstersFromCP = *simTrackstersFromCPHandle; - - Handle> hitToTracksterMapHandle; - iEvent.getByToken(hitToTracksterMapToken_, hitToTracksterMapHandle); - const auto& hitToTracksterMap = *hitToTracksterMapHandle; - - Handle> hitToSimTracksterMapHandle; - iEvent.getByToken(hitToSimTracksterMapToken_, hitToSimTracksterMapHandle); - const auto& hitToSimTracksterMap = *hitToSimTracksterMapHandle; - - Handle> hitToSimTracksterFromCPMapHandle; - iEvent.getByToken(hitToSimTracksterFromCPMapToken_, hitToSimTracksterFromCPMapHandle); - const auto& hitToSimTracksterFromCPMap = *hitToSimTracksterFromCPMapHandle; - - Handle> hitToSimClusterMapHandle; - iEvent.getByToken(hitToSimClusterMapToken_, hitToSimClusterMapHandle); - const auto& hitToSimClusterMap = *hitToSimClusterMapHandle; - - Handle> hitToCaloParticleMapHandle; - iEvent.getByToken(hitToCaloParticleMapToken_, hitToCaloParticleMapHandle); - const auto& hitToCaloParticleMap = *hitToCaloParticleMapHandle; - - Handle> tracksterToHitMapHandle; - iEvent.getByToken(tracksterToHitMapToken_, tracksterToHitMapHandle); - const auto& tracksterToHitMap = *tracksterToHitMapHandle; - - Handle> simTracksterToHitMapHandle; - iEvent.getByToken(simTracksterToHitMapToken_, simTracksterToHitMapHandle); - const auto& simTracksterToHitMap = *simTracksterToHitMapHandle; - - Handle> simTracksterFromCPToHitMapHandle; - iEvent.getByToken(simTracksterFromCPToHitMapToken_, simTracksterFromCPToHitMapHandle); - const auto& simTracksterFromCPToHitMap = *simTracksterFromCPToHitMapHandle; - - Handle> caloParticlesHandle; - iEvent.getByToken(caloParticleToken_, caloParticlesHandle); - - MultiVectorManager rechitManager; - for (const auto& token : hitsTokens_) { - Handle hitsHandle; - iEvent.getByToken(token, hitsHandle); - rechitManager.addVector(*hitsHandle); - } - - auto tracksterToSimTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - recoTrackstersHandle, simTrackstersHandle, iEvent); - auto tracksterToSimTracksterFromCPMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - recoTrackstersHandle, simTrackstersFromCPHandle, iEvent); - - auto simTracksterToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - simTrackstersHandle, recoTrackstersHandle, iEvent); - auto simTracksterFromCPToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - simTrackstersFromCPHandle, recoTrackstersHandle, iEvent); - for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) { - edm::Ref> recoTracksterRef(recoTrackstersHandle, tracksterIndex); - float recoToSimScoresDenominator = 0.f; - const auto& recoTracksterHitsAndFractions = tracksterToHitMap[tracksterIndex]; - ticl::AssociationMap hitToAssociatedSimTracksterMap(recoTracksterHitsAndFractions.size()); - std::vector associatedSimTracksterIndices; - ticl::AssociationMap hitToAssociatedSimTracksterFromCPMap( - recoTracksterHitsAndFractions.size()); - std::vector associatedSimTracksterFromCPIndices; - for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[i]; - const auto& recHit = rechitManager[hitIndex]; - float squaredRecoFraction = recoFraction * recoFraction; - float rechitEnergy = recHit.energy(); - float squaredRecHitEnergy = rechitEnergy * rechitEnergy; - recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy; - - const auto& hitToSimTracksterVec = hitToSimTracksterMap[hitIndex]; - for (const auto& [simTracksterIndex, fraction] : hitToSimTracksterVec) { - const auto& simTrackster = simTracksters[simTracksterIndex]; - auto& seed = simTrackster.seedID(); - float simFraction = 0; - if (seed == caloParticlesHandle.id()) { - unsigned int caloParticleIndex = simTrackster.seedIndex(); - auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(), - hitToCaloParticleMap[hitIndex].end(), - [caloParticleIndex](const auto& pair) { return pair.first == caloParticleIndex; }); - if (it != hitToCaloParticleMap[hitIndex].end()) { - simFraction = it->second; - } - } else { - unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex(); - auto it = std::find_if(hitToSimClusterMap[hitIndex].begin(), - hitToSimClusterMap[hitIndex].end(), - [simClusterIndex](const auto& pair) { return pair.first == simClusterIndex; }); - if (it != hitToSimClusterMap[hitIndex].end()) { - simFraction = it->second; - } - } - hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, simFraction); - associatedSimTracksterIndices.push_back(simTracksterIndex); - } - - // do the same for caloparticles and simTracksterFromCP - const auto& hitToSimTracksterFromCPVec = hitToSimTracksterFromCPMap[hitIndex]; - for (const auto& [simTracksterIndex, simFraction] : hitToSimTracksterFromCPVec) { - unsigned int caloParticleIndex = simTracksters[simTracksterIndex].seedIndex(); - float caloParticleFraction = 0; - auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(), - hitToCaloParticleMap[hitIndex].end(), - [caloParticleIndex](const auto& pair) { return pair.first == caloParticleIndex; }); - if (it != hitToCaloParticleMap[hitIndex].end()) { - caloParticleFraction = it->second; - } - hitToAssociatedSimTracksterFromCPMap.insert(i, simTracksterIndex, caloParticleFraction); - associatedSimTracksterFromCPIndices.push_back(simTracksterIndex); - } - } - std::sort(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()); - associatedSimTracksterIndices.erase( - std::unique(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()), - associatedSimTracksterIndices.end()); - - std::sort(associatedSimTracksterFromCPIndices.begin(), associatedSimTracksterFromCPIndices.end()); - associatedSimTracksterFromCPIndices.erase( - std::unique(associatedSimTracksterFromCPIndices.begin(), associatedSimTracksterFromCPIndices.end()), - associatedSimTracksterFromCPIndices.end()); - - // Add missing sim tracksters with 0 shared energy to hitToAssociatedSimTracksterMap and hitToAssociatedSimTracksterFromCPMap - for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - unsigned int hitId = recoTracksterHitsAndFractions[i].first; - const auto& simTracksterVec = hitToSimTracksterMap[hitId]; - for (unsigned int simTracksterIndex : associatedSimTracksterIndices) { - if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) { - return pair.first == simTracksterIndex; - }) == simTracksterVec.end()) { - hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, 0); - } - } - - const auto& simTracksterFromCPVec = hitToSimTracksterFromCPMap[hitId]; - for (unsigned int simTracksterIndex : associatedSimTracksterFromCPIndices) { - if (std::find_if( - simTracksterFromCPVec.begin(), simTracksterFromCPVec.end(), [simTracksterIndex](const auto& pair) { - return pair.first == simTracksterIndex; - }) == simTracksterFromCPVec.end()) { - hitToAssociatedSimTracksterFromCPMap.insert(i, simTracksterIndex, 0); - } - } - } - - const float invDenominator = 1.f / recoToSimScoresDenominator; - - for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) { - unsigned int hitIndex = recoTracksterHitsAndFractions[i].first; - const auto& recHit = rechitManager[hitIndex]; - float recoFraction = recoTracksterHitsAndFractions[i].second; - float squaredRecoFraction = recoFraction * recoFraction; - float squaredRecHitEnergy = recHit.energy() * recHit.energy(); - float recoSharedEnergy = recHit.energy() * recoFraction; - const auto& simTracksterVec = hitToAssociatedSimTracksterMap[i]; - for (const auto& [simTracksterIndex, simFraction] : simTracksterVec) { - edm::Ref> simTracksterRef(simTrackstersHandle, simTracksterIndex); - float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy); - float squaredFraction = - std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction)); - float score = invDenominator * squaredFraction * squaredRecHitEnergy; - tracksterToSimTracksterMap->insert(recoTracksterRef, simTracksterRef, sharedEnergy, score); - } - - const auto& simTracksterFromCPVec = hitToAssociatedSimTracksterFromCPMap[i]; - for (const auto& [simTracksterIndex, simFraction] : simTracksterFromCPVec) { - edm::Ref> simTracksterRef(simTrackstersFromCPHandle, simTracksterIndex); - float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy); - float squaredFraction = - std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction)); - float score = invDenominator * squaredFraction * squaredRecHitEnergy; - tracksterToSimTracksterFromCPMap->insert(recoTracksterRef, simTracksterRef, sharedEnergy, score); - } - } - } - - // Reverse mapping: SimTrackster -> RecoTrackster - for (unsigned int tracksterIndex = 0; tracksterIndex < simTracksters.size(); ++tracksterIndex) { - edm::Ref> simTracksterRef(simTrackstersHandle, tracksterIndex); - float simToRecoScoresDenominator = 0.f; - const auto& simTracksterHitsAndFractions = simTracksterToHitMap[tracksterIndex]; - ticl::AssociationMap hitToAssociatedRecoTracksterMap(simTracksterHitsAndFractions.size()); - std::vector associatedRecoTracksterIndices; - const auto& simTrackster = simTracksters[tracksterIndex]; - auto& seed = simTrackster.seedID(); - unsigned int simObjectIndex = simTrackster.seedIndex(); - bool isSimTracksterFromCP = (seed == caloParticlesHandle.id()); - std::vector simFractions(simTracksterHitsAndFractions.size(), 0.f); - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; - - auto it = isSimTracksterFromCP - ? (std::find_if(hitToCaloParticleMap[hitIndex].begin(), - hitToCaloParticleMap[hitIndex].end(), - [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; })) - : std::find_if(hitToSimClusterMap[hitIndex].begin(), - hitToSimClusterMap[hitIndex].end(), - [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; }); - if (it != hitToCaloParticleMap[hitIndex].end() and it != hitToSimClusterMap[hitIndex].end()) { - simFractions[i] = it->second; - } - float simFraction = simFractions[i]; - const auto& recHit = rechitManager[hitIndex]; - float squaredSimFraction = simFraction * simFraction; - float squaredRecHitEnergy = recHit.energy() * recHit.energy(); - simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy; - - const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { - hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, recoFraction); - associatedRecoTracksterIndices.push_back(recoTracksterIndex); - } - } - - std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()); - associatedRecoTracksterIndices.erase( - std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()), - associatedRecoTracksterIndices.end()); - - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - unsigned int hitIndex = simTracksterHitsAndFractions[i].first; - const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; - for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) { - if (std::find_if( - hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](const auto& pair) { - return pair.first == recoTracksterIndex; - }) == hitToRecoTracksterVec.end()) { - hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, 0); - } - } - } - - const float invDenominator = 1.f / simToRecoScoresDenominator; - - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; - float simFraction = simFractions[i]; - const auto& recHit = rechitManager[hitIndex]; - float squaredSimFraction = simFraction * simFraction; - float squaredRecHitEnergy = recHit.energy() * recHit.energy(); - float simSharedEnergy = recHit.energy() * simFraction; - - const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { - edm::Ref> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex); - float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy); - float squaredFraction = - std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction)); - float score = invDenominator * squaredFraction * squaredRecHitEnergy; - simTracksterToTracksterMap->insert(simTracksterRef, recoTracksterRef, sharedEnergy, score); - } - } - } - - // Repeat the reverse mapping process for SimTracksterFromCP - for (unsigned int tracksterIndex = 0; tracksterIndex < simTrackstersFromCP.size(); ++tracksterIndex) { - edm::Ref> simTracksterRef(simTrackstersFromCPHandle, tracksterIndex); - float simToRecoScoresDenominator = 0.f; - const auto& simTracksterHitsAndFractions = simTracksterFromCPToHitMap[tracksterIndex]; - ticl::AssociationMap hitToAssociatedRecoTracksterMap(simTracksterHitsAndFractions.size()); - std::vector associatedRecoTracksterIndices; - std::vector simFractions(simTracksterHitsAndFractions.size(), 0.f); - const auto& simTrackster = simTrackstersFromCP[tracksterIndex]; - unsigned int simObjectIndex = simTrackster.seedIndex(); - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; - auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(), - hitToCaloParticleMap[hitIndex].end(), - [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; }); - if (it != hitToCaloParticleMap[hitIndex].end()) { - simFractions[i] = it->second; - } - float simFraction = simFractions[i]; - - const auto& recHit = rechitManager[hitIndex]; - float squaredSimFraction = simFraction * simFraction; - float squaredRecHitEnergy = recHit.energy() * recHit.energy(); - simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy; - - const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { - hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, recoFraction); - associatedRecoTracksterIndices.push_back(recoTracksterIndex); - } - } - - std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()); - associatedRecoTracksterIndices.erase( - std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()), - associatedRecoTracksterIndices.end()); - - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - unsigned int hitIndex = simTracksterHitsAndFractions[i].first; - const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex]; - for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) { - if (std::find_if( - hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](const auto& pair) { - return pair.first == recoTracksterIndex; - }) == hitToRecoTracksterVec.end()) { - hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, 0.f); - } - } - } - - const float invDenominator = 1.f / simToRecoScoresDenominator; - - for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) { - const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i]; - const auto& recHit = rechitManager[hitIndex]; - float simFraction = simFractions[i]; - float squaredSimFraction = simFraction * simFraction; - float squaredRecHitEnergy = recHit.energy() * recHit.energy(); - float simSharedEnergy = recHit.energy() * simFraction; - - const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i]; - for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) { - edm::Ref> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex); - float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy); - float squaredFraction = - std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction)); - float score = invDenominator * squaredFraction * squaredRecHitEnergy; - simTracksterFromCPToTracksterMap->insert(simTracksterRef, recoTracksterRef, sharedEnergy, score); - } - } - } - tracksterToSimTracksterMap->sort(true); - tracksterToSimTracksterFromCPMap->sort(true); - simTracksterToTracksterMap->sort(true); - simTracksterFromCPToTracksterMap->sort(true); - - iEvent.put(std::move(tracksterToSimTracksterMap), "tracksterToSimTracksterMap"); - iEvent.put(std::move(tracksterToSimTracksterFromCPMap), "tracksterToSimTracksterFromCPMap"); - iEvent.put(std::move(simTracksterToTracksterMap), "simTracksterToTracksterMap"); - iEvent.put(std::move(simTracksterFromCPToTracksterMap), "simTracksterFromCPToTracksterMap"); -} - -void TracksterToSimTracksterAssociatorByHitsProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - edm::ParameterSetDescription desc; - desc.add("tracksters", edm::InputTag("ticlTrackstersMerge")); - desc.add("simTracksters", edm::InputTag("ticlSimTracksters")); - desc.add("simTrackstersFromCP", edm::InputTag("ticlSimTracksters", "fromCPs")); - - desc.add("hitToTracksterMap", edm::InputTag("hitToTracksterAssociator", "hitToTracksterMap")); - desc.add("hitToSimTracksterMap", - edm::InputTag("allHitToTracksterAssociations", "hitToticlSimTracksters")); - desc.add("hitToSimTracksterFromCPMap", - edm::InputTag("allHitToTracksterAssociations", "hitToticlSimTrackstersfromCPs")); - desc.add("hitToSimClusterMap", - edm::InputTag("hitToSimClusterCaloParticleAssociator", "hitToSimClusterMap")); - desc.add("hitToCaloParticleMap", - edm::InputTag("hitToSimClusterCaloParticleAssociator", "hitToCaloParticleMap")); - desc.add("tracksterToHitMap", edm::InputTag("hitToTrackstersAssociationPR", "tracksterToHitMap")); - desc.add("simTracksterToHitMap", - edm::InputTag("allHitToTracksterAssociations", "ticlSimTrackstersToHit")); - desc.add("simTracksterFromCPToHitMap", - edm::InputTag("allHitToTracksterAssociations", "ticlSimTrackstersfromCPsToHit")); - desc.add("caloParticles", edm::InputTag("mix", "MergedCaloTruth")); - - desc.add>("hits", - {edm::InputTag("HGCalRecHit", "HGCEERecHits"), - edm::InputTag("HGCalRecHit", "HGCHEFRecHits"), - edm::InputTag("HGCalRecHit", "HGCHEBRecHits")}); - descriptions.add("tracksterToSimTracksterAssociatorByHitsProducer", desc); -} - -// Define this as a plug-in -DEFINE_FWK_MODULE(TracksterToSimTracksterAssociatorByHitsProducer); \ No newline at end of file diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.h deleted file mode 100644 index ae38d6ceb797a..0000000000000 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorByHitsProducer.h +++ /dev/null @@ -1,48 +0,0 @@ -// Author: Felice Pantaleo, felice.pantaleo@cern.ch 06/2024 - -#ifndef TracksterToSimTracksterAssociatorByHitsProducer_h -#define TracksterToSimTracksterAssociatorByHitsProducer_h - -#include - -#include "FWCore/Framework/interface/global/EDProducer.h" -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/MakerMacros.h" -#include "FWCore/Framework/interface/ESHandle.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "FWCore/Utilities/interface/EDGetToken.h" -#include "DataFormats/HGCalReco/interface/Trackster.h" -#include "SimDataFormats/Associations/interface/TICLAssociationMap.h" -#include "DataFormats/Provenance/interface/ProductID.h" -#include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h" -#include "CommonTools/RecoAlgos/interface/MultiVectorManager.h" -#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h" -#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h" - -class TracksterToSimTracksterAssociatorByHitsProducer : public edm::global::EDProducer<> { -public: - explicit TracksterToSimTracksterAssociatorByHitsProducer(const edm::ParameterSet&); - ~TracksterToSimTracksterAssociatorByHitsProducer() override; - - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - -private: - void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override; - - edm::EDGetTokenT> recoTracksterCollectionToken_; - edm::EDGetTokenT> simTracksterCollectionToken_; - edm::EDGetTokenT> simTracksterFromCPCollectionToken_; - edm::EDGetTokenT> hitToTracksterMapToken_; - edm::EDGetTokenT> hitToSimTracksterMapToken_; - edm::EDGetTokenT> hitToSimTracksterFromCPMapToken_; - edm::EDGetTokenT> hitToSimClusterMapToken_; - edm::EDGetTokenT> hitToCaloParticleMapToken_; - edm::EDGetTokenT> tracksterToHitMapToken_; - edm::EDGetTokenT> simTracksterToHitMapToken_; - edm::EDGetTokenT> simTracksterFromCPToHitMapToken_; - edm::EDGetTokenT> caloParticleToken_; - std::vector> hitsTokens_; -}; - -#endif diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.cc deleted file mode 100644 index addadd8e4b1e8..0000000000000 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.cc +++ /dev/null @@ -1,202 +0,0 @@ -// Author: Felice Pantaleo, felice.pantaleo@cern.ch 06/2024 - -#include "TracksterToSimTracksterAssociatorProducer.h" -#include "SimDataFormats/Associations/interface/TICLAssociationMap.h" - -TracksterToSimTracksterAssociatorProducer::TracksterToSimTracksterAssociatorProducer(const edm::ParameterSet& pset) - : recoTracksterCollectionToken_( - consumes>(pset.getParameter("tracksters"))), - simTracksterCollectionToken_( - consumes>(pset.getParameter("simTracksters"))), - layerClustersCollectionToken_( - consumes>(pset.getParameter("layerClusters"))), - LayerClusterToTracksterMapToken_( - consumes< - ticl::AssociationMap, std::vector>>( - pset.getParameter("tracksterMap"))), - LayerClusterToSimTracksterMapToken_( - consumes< - ticl::AssociationMap, std::vector>>( - pset.getParameter("simTracksterMap"))) { - produces< - ticl::AssociationMap, std::vector>>( - "tracksterToSimTracksterMap"); - produces< - ticl::AssociationMap, std::vector>>( - "simTracksterToTracksterMap"); -} - -TracksterToSimTracksterAssociatorProducer::~TracksterToSimTracksterAssociatorProducer() {} - -void TracksterToSimTracksterAssociatorProducer::produce(edm::StreamID, - edm::Event& iEvent, - const edm::EventSetup& iSetup) const { - edm::Handle> recoTrackstersHandle; - iEvent.getByToken(recoTracksterCollectionToken_, recoTrackstersHandle); - const auto& recoTracksters = *recoTrackstersHandle; - - edm::Handle> simTrackstersHandle; - iEvent.getByToken(simTracksterCollectionToken_, simTrackstersHandle); - const auto& simTracksters = *simTrackstersHandle; - - edm::Handle> layerClustersHandle; - iEvent.getByToken(layerClustersCollectionToken_, layerClustersHandle); - const auto& layerClusters = *layerClustersHandle; - - edm::Handle, std::vector>> - layerClusterToTracksterMapHandle; - iEvent.getByToken(LayerClusterToTracksterMapToken_, layerClusterToTracksterMapHandle); - const auto& layerClusterToTracksterMap = *layerClusterToTracksterMapHandle; - - edm::Handle, std::vector>> - layerClusterToSimTracksterMapHandle; - iEvent.getByToken(LayerClusterToSimTracksterMapToken_, layerClusterToSimTracksterMapHandle); - const auto& layerClusterToSimTracksterMap = *layerClusterToSimTracksterMapHandle; - - auto tracksterToSimTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - recoTrackstersHandle, simTrackstersHandle, iEvent); - auto simTracksterToTracksterMap = std::make_unique< - ticl::AssociationMap, std::vector>>( - simTrackstersHandle, recoTrackstersHandle, iEvent); - - for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) { - const auto& recoTrackster = recoTracksters[tracksterIndex]; - edm::Ref> recoTracksterRef(recoTrackstersHandle, tracksterIndex); - const auto& layerClustersIds = recoTrackster.vertices(); - float recoToSimScoresDenominator = 0.f; - ticl::mapWithFraction layerClusterToAssociatedSimTracksterMap(layerClustersIds.size()); - std::vector associatedSimTracksterIndices; - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& layerCluster = layerClusters[layerClusterId]; - float recoFraction = 1.f / recoTrackster.vertex_multiplicity(i); - float squaredRecoFraction = recoFraction * recoFraction; - float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); - recoToSimScoresDenominator += squaredLayerClusterEnergy * squaredRecoFraction; - const auto& simTracksterVec = layerClusterToSimTracksterMap[layerClusterId]; - for (const auto& [simTracksterIndex, simSharedEnergy] : simTracksterVec) { - layerClusterToAssociatedSimTracksterMap[i].push_back({simTracksterIndex, simSharedEnergy}); - associatedSimTracksterIndices.push_back(simTracksterIndex); - } - } - - // Keep only unique associatedSimTracksterIndices - std::sort(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()); - associatedSimTracksterIndices.erase( - std::unique(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()), - associatedSimTracksterIndices.end()); - - // Add missing sim tracksters with 0 shared energy to layerClusterToAssociatedSimTracksterMap - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& simTracksterVec = layerClusterToSimTracksterMap[layerClusterId]; - for (unsigned int simTracksterIndex : associatedSimTracksterIndices) { - if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) { - return pair.first == simTracksterIndex; - }) == simTracksterVec.end()) { - layerClusterToAssociatedSimTracksterMap[i].push_back({simTracksterIndex, 0.f}); - } - } - } - - const float invDenominator = 1.f / recoToSimScoresDenominator; - - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& layerCluster = layerClusters[layerClusterId]; - float recoFraction = 1.f / recoTrackster.vertex_multiplicity(i); - float squaredRecoFraction = recoFraction * recoFraction; - float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); - float recoSharedEnergy = layerCluster.energy() * recoFraction; - float invLayerClusterEnergy = 1.f / layerCluster.energy(); - const auto& simTracksterVec = layerClusterToAssociatedSimTracksterMap[i]; - for (const auto& [simTracksterIndex, simSharedEnergy] : simTracksterVec) { - edm::Ref> simTracksterRef(simTrackstersHandle, simTracksterIndex); - float sharedEnergy = std::min(simSharedEnergy, recoSharedEnergy); - float simFraction = simSharedEnergy * invLayerClusterEnergy; - float score = invDenominator * - std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction)) * - squaredLayerClusterEnergy; - tracksterToSimTracksterMap->insert(recoTracksterRef, simTracksterRef, sharedEnergy, score); - } - } - } - - for (unsigned int tracksterIndex = 0; tracksterIndex < simTracksters.size(); ++tracksterIndex) { - const auto& simTrackster = simTracksters[tracksterIndex]; - edm::Ref> simTracksterRef(simTrackstersHandle, tracksterIndex); - const auto& layerClustersIds = simTrackster.vertices(); - float simToRecoScoresDenominator = 0.f; - ticl::mapWithFraction layerClusterToAssociatedTracksterMap(layerClustersIds.size()); - std::vector associatedRecoTracksterIndices; - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& layerCluster = layerClusters[layerClusterId]; - float simFraction = 1.f / simTrackster.vertex_multiplicity(i); - float squaredSimFraction = simFraction * simFraction; - float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); - simToRecoScoresDenominator += squaredLayerClusterEnergy * squaredSimFraction; - const auto& recoTracksterVec = layerClusterToTracksterMap[layerClusterId]; - for (const auto& [recoTracksterIndex, recoSharedEnergy] : recoTracksterVec) { - layerClusterToAssociatedTracksterMap[i].push_back({recoTracksterIndex, recoSharedEnergy}); - associatedRecoTracksterIndices.push_back(recoTracksterIndex); - } - } - // keep only unique associatedRecoTracksterIndices - std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()); - associatedRecoTracksterIndices.erase( - std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()), - associatedRecoTracksterIndices.end()); - // for each layer cluster, loop over associatedRecoTracksterIndices and add the missing reco tracksters with 0 shared energy to layerClusterToAssociatedTracksterMap - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& recoTracksterVec = layerClusterToTracksterMap[layerClusterId]; - for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) { - if (std::find_if(recoTracksterVec.begin(), recoTracksterVec.end(), [recoTracksterIndex](const auto& pair) { - return pair.first == recoTracksterIndex; - }) == recoTracksterVec.end()) { - layerClusterToAssociatedTracksterMap[i].push_back({recoTracksterIndex, 0.f}); - } - } - } - - const float invDenominator = 1.f / simToRecoScoresDenominator; - - for (unsigned int i = 0; i < layerClustersIds.size(); ++i) { - unsigned int layerClusterId = layerClustersIds[i]; - const auto& layerCluster = layerClusters[layerClusterId]; - float simFraction = 1.f / simTrackster.vertex_multiplicity(i); - float squaredSimFraction = simFraction * simFraction; - float squaredLayerClusterEnergy = layerCluster.energy() * layerCluster.energy(); - float simSharedEnergy = layerCluster.energy() * simFraction; - float invLayerClusterEnergy = 1.f / layerCluster.energy(); - const auto& recoTracksterVec = layerClusterToAssociatedTracksterMap[i]; - for (const auto& [recoTracksterIndex, recoSharedEnergy] : recoTracksterVec) { - edm::Ref> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex); - float sharedEnergy = std::min(recoSharedEnergy, simSharedEnergy); - float recoFraction = recoSharedEnergy * invLayerClusterEnergy; - float score = invDenominator * - std::min(squaredSimFraction, (simFraction - recoFraction) * (simFraction - recoFraction)) * - squaredLayerClusterEnergy; - simTracksterToTracksterMap->insert(tracksterIndex, recoTracksterIndex, sharedEnergy, score); - } - } - } - tracksterToSimTracksterMap->sort(true); - simTracksterToTracksterMap->sort(true); - iEvent.put(std::move(tracksterToSimTracksterMap), "tracksterToSimTracksterMap"); - iEvent.put(std::move(simTracksterToTracksterMap), "simTracksterToTracksterMap"); -} - -void TracksterToSimTracksterAssociatorProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - edm::ParameterSetDescription desc; - desc.add("tracksters", edm::InputTag("trackstersProducer")); - desc.add("simTracksters", edm::InputTag("simTrackstersProducer")); - desc.add("layerClusters", edm::InputTag("hgcalMergeLayerClusters")); - desc.add("tracksterMap", edm::InputTag("tracksterAssociatorProducer")); - desc.add("simTracksterMap", edm::InputTag("simTracksterAssociatorProducer")); - descriptions.add("tracksterToSimTracksterAssociatorProducer", desc); -} - -DEFINE_FWK_MODULE(TracksterToSimTracksterAssociatorProducer); diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.h deleted file mode 100644 index d7854d9549e10..0000000000000 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterToSimTracksterAssociatorProducer.h +++ /dev/null @@ -1,44 +0,0 @@ -// Author: Felice Pantaleo, felice.pantaleo@cern.ch 06/2024 - -#ifndef TracksterToSimTracksterAssociatorProducer_h -#define TracksterToSimTracksterAssociatorProducer_h - -#include -#include -#include -#include -#include - -#include "FWCore/Framework/interface/global/EDProducer.h" -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/MakerMacros.h" -#include "FWCore/Framework/interface/ESHandle.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "FWCore/Utilities/interface/EDGetToken.h" -#include "DataFormats/HGCalReco/interface/Trackster.h" -#include "DataFormats/CaloRecHit/interface/CaloCluster.h" -#include "SimDataFormats/Associations/interface/TICLAssociationMap.h" - -class TracksterToSimTracksterAssociatorProducer : public edm::global::EDProducer<> { -public: - explicit TracksterToSimTracksterAssociatorProducer(const edm::ParameterSet&); - ~TracksterToSimTracksterAssociatorProducer() override; - - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - -private: - void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override; - - edm::EDGetTokenT> recoTracksterCollectionToken_; - edm::EDGetTokenT> simTracksterCollectionToken_; - edm::EDGetTokenT> layerClustersCollectionToken_; - edm::EDGetTokenT< - ticl::AssociationMap, std::vector>> - LayerClusterToTracksterMapToken_; - edm::EDGetTokenT< - ticl::AssociationMap, std::vector>> - LayerClusterToSimTracksterMapToken_; -}; - -#endif diff --git a/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociationByHits_cfi.py b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociationByHits_cfi.py index eefe81b702bff..43a05ba1b9743 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociationByHits_cfi.py +++ b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociationByHits_cfi.py @@ -1,34 +1,5 @@ import FWCore.ParameterSet.Config as cms from SimCalorimetry.HGCalAssociatorProducers.HitToTracksterAssociation_cfi import * -from SimCalorimetry.HGCalAssociatorProducers.tracksterToSimTracksterAssociatorByHitsProducer_cfi import tracksterToSimTracksterAssociatorByHitsProducer - - - -tracksterSimTracksterAssociationByHitsLinking = tracksterToSimTracksterAssociatorByHitsProducer.clone( - tracksters = cms.InputTag("ticlTrackstersMerge"), - hitToTracksterMap = cms.InputTag("allHitToTracksterAssociations","hitTo"+"ticlTrackstersMerge"), - tracksterToHitMap = cms.InputTag("allHitToTracksterAssociations","ticlTrackstersMerge"+"ToHit"), - hitToSimTracksterMap = cms.InputTag("allHitToTracksterAssociations", "hitTo"+"ticlSimTracksters"), - hitToSimTracksterFromCPMap = cms.InputTag("allHitToTracksterAssociations", 'hitTo'+'ticlSimTracksters'+'fromCPs'), - simTracksterToHitMap = cms.InputTag('allHitToTracksterAssociations', 'ticlSimTracksters'+'ToHit'), - simTracksterFromCPToHitMap = cms.InputTag('allHitToTracksterAssociations', 'ticlSimTracksters'+'fromCPs'+'ToHit'), -) - - -tracksterSimTracksterAssociationByHitsPR = tracksterToSimTracksterAssociatorByHitsProducer.clone( - tracksters = cms.InputTag("ticlTrackstersCLUE3DHigh"), - hitToTracksterMap = cms.InputTag("allHitToTracksterAssociations","hitTo"+"ticlTrackstersCLUE3DHigh"), - tracksterToHitMap = cms.InputTag("allHitToTracksterAssociations","ticlTrackstersCLUE3DHigh"+"ToHit"), -) - - - -from Configuration.ProcessModifiers.ticl_v5_cff import ticl_v5 - -ticl_v5.toModify(tracksterSimTracksterAssociationByHitsLinking, tracksters = cms.InputTag("ticlCandidate"), hitToTracksterMap = cms.InputTag("allHitToTracksterAssociations","hitTo"+"ticlCandidate"), tracksterToHitMap = cms.InputTag("allHitToTracksterAssociations","ticlCandidate"+"ToHit")) - - - from SimCalorimetry.HGCalAssociatorProducers.AllTracksterToSimTracksterAssociatorsByHitsProducer_cfi import AllTracksterToSimTracksterAssociatorsByHitsProducer from RecoHGCal.TICL.iterativeTICL_cff import ticlIterLabels @@ -45,14 +16,6 @@ from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2 -premix_stage2.toModify(tracksterSimTracksterAssociationByHitsLinking, - caloParticles = "mixData:MergedCaloTruth", -) - -premix_stage2.toModify(tracksterSimTracksterAssociationByHitsPR, - caloParticles = "mixData:MergedCaloTruth", -) - premix_stage2.toModify(allTrackstersToSimTrackstersAssociationsByHits, caloParticles = "mixData:MergedCaloTruth", ) diff --git a/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociation_cfi.py b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociation_cfi.py index 92a527475a641..374a01fb24efe 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociation_cfi.py +++ b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSimTSAssociation_cfi.py @@ -1,6 +1,5 @@ import FWCore.ParameterSet.Config as cms from SimCalorimetry.HGCalAssociatorProducers.LCToTSAssociator_cfi import layerClusterToCLUE3DTracksterAssociation, layerClusterToTracksterMergeAssociation, layerClusterToSimTracksterAssociation, layerClusterToSimTracksterFromCPsAssociation -from SimCalorimetry.HGCalAssociatorProducers.tracksterToSimTracksterAssociatorProducer_cfi import tracksterToSimTracksterAssociatorProducer from Configuration.ProcessModifiers.ticl_v5_cff import ticl_v5 from Configuration.ProcessModifiers.ticl_superclustering_mustache_ticl_cff import ticl_superclustering_mustache_ticl From 5c6be10ec8f1a16665648d1a675d81c0bea786aa Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 14 Nov 2024 11:19:28 +0100 Subject: [PATCH 08/10] cleanup event content. Add SimTauProducer. --- .../python/RecoHGCal_EventContent_cff.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/RecoHGCal/Configuration/python/RecoHGCal_EventContent_cff.py b/RecoHGCal/Configuration/python/RecoHGCal_EventContent_cff.py index d23503818ea8d..679ee3c68fd4a 100644 --- a/RecoHGCal/Configuration/python/RecoHGCal_EventContent_cff.py +++ b/RecoHGCal/Configuration/python/RecoHGCal_EventContent_cff.py @@ -23,7 +23,7 @@ ['keep *_pfTICL_*_*'] + ['keep CaloParticles_mix_*_*', 'keep SimClusters_mix_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*'] + ['keep *_SimClusterToCaloParticleAssociation*_*_*', 'keep *_layerClusterSimClusterAssociationProducer_*_*','keep *_layerClusterCaloParticleAssociationProducer_*_*', 'keep *_layerClusterSimTracksterAssociationProducer_*_*'] + - ['keep *_tracksterSimTracksterAssociation*_*_*' , 'keep *_tracksterSimTracksterFromCPsAssociation*_*_*' ] + ['keep *_allTrackstersToSimTrackstersAssociations*_*_*'] ) ) @@ -50,7 +50,8 @@ 'keep *_ticlSimTracksters_*_*', 'keep *_ticlSimTICLCandidates_*_*', 'keep *_ticlSimTrackstersFromCP_*_*', - 'keep *_SimTau*_*_*' + 'keep *_SimTau*_*_*', + 'keep *_allTrackstersToSimTrackstersAssociations*_*_*' ) ) TICL_FEVT.outputCommands.extend(TICL_RECO.outputCommands) @@ -61,6 +62,7 @@ 'keep *_ticlSimTrackstersFromCP_*_*', 'keep CaloParticles_mix_*_*', 'keep SimClusters_mix_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*', 'keep *_layerClusterSimClusterAssociationProducer_*_*','keep *_layerClusterCaloParticleAssociationProducer_*_*', 'keep *_layerClusterSimTracksterAssociationProducer_*_*', + 'keep *_SimTau*_*_*', 'keep *_allTrackstersToSimTrackstersAssociations*_*_*' ) @@ -110,10 +112,10 @@ def cleanOutputAndSet(outputModule, ticl_outputCommads): 'keep *_layerClusterCaloParticleAssociationProducer_*_*', 'keep *_randomEngineStateProducer_*_*', 'keep *_layerClusterSimTracksterAssociationProducer_*_*', - 'keep *_tracksterSimTracksterAssociation*_*_*', - 'keep *_tracksterSimTracksterFromCPsAssociation*_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*', - 'keep *_simClusterToCaloParticleAssociator*_*_*' + 'keep *_simClusterToCaloParticleAssociator*_*_*', + 'keep *_SimTau*_*_*', + 'keep *_allTrackstersToSimTrackstersAssociations*_*_*' ]) if hasattr(process, 'FEVTDEBUGEventContent'): From 56acb3360284bcb7f06f88e098bb6e34f21e2aa7 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 14 Nov 2024 15:09:15 +0100 Subject: [PATCH 09/10] fix validation plots for ticl versions other than 5 --- Validation/HGCalValidation/scripts/makeHGCalValidationPlots.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Validation/HGCalValidation/scripts/makeHGCalValidationPlots.py b/Validation/HGCalValidation/scripts/makeHGCalValidationPlots.py index 80259923b0abe..0e4d1d8750f61 100755 --- a/Validation/HGCalValidation/scripts/makeHGCalValidationPlots.py +++ b/Validation/HGCalValidation/scripts/makeHGCalValidationPlots.py @@ -29,9 +29,10 @@ tracksters = [] def main(opts): - + drawArgs={} extendedFlag = False + ticlVersion = 4 if opts.no_ratio: drawArgs["ratio"] = False if opts.separate: From 595c9294ee1267b1232dde66bc5914949b73d313 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Mon, 2 Dec 2024 15:41:58 +0100 Subject: [PATCH 10/10] Modify input for CaloParticles and SimClusters when premix process modifier is active --- Validation/HGCalValidation/python/HGCalValidator_cff.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Validation/HGCalValidation/python/HGCalValidator_cff.py b/Validation/HGCalValidation/python/HGCalValidator_cff.py index 455f7c0ac8abb..2c3494421b0a4 100644 --- a/Validation/HGCalValidation/python/HGCalValidator_cff.py +++ b/Validation/HGCalValidation/python/HGCalValidator_cff.py @@ -13,7 +13,9 @@ from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2 premix_stage2.toModify(hgcalValidator, - label_cp_fake = "mixData:MergedCaloTruth" + label_cp_fake = "mixData:MergedCaloTruth", + label_cp_effic = "mixData:MergedCaloTruth", + label_scl = "mixData:MergedCaloTruth", ) from Configuration.Eras.Modifier_phase2_hgcalV10_cff import phase2_hgcalV10