From eee68fcfde5dd2063bf40a17f55c24bd45a0928c Mon Sep 17 00:00:00 2001 From: Bohdan Dudar Date: Wed, 31 Aug 2022 15:35:42 +0200 Subject: [PATCH] Added four utility methods to the LCRelationNavigator to allow a quicker way to extract an object with the highest weight --- src/cpp/include/UTIL/LCRelationNavigator.h | 20 +++++++++ src/cpp/src/UTIL/LCRelationNavigator.cc | 49 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/cpp/include/UTIL/LCRelationNavigator.h b/src/cpp/include/UTIL/LCRelationNavigator.h index 022c2bfa2..1cfa4e7c9 100644 --- a/src/cpp/include/UTIL/LCRelationNavigator.h +++ b/src/cpp/include/UTIL/LCRelationNavigator.h @@ -68,6 +68,26 @@ namespace UTIL { */ virtual const EVENT::FloatVec & getRelatedFromWeights(EVENT::LCObject * to) const ; + /** Object with a highest weight that the given from-object is related to. + * LCObject is of type getToType(). + */ + virtual const EVENT::LCObject* getRelatedToMaxWeightObject(EVENT::LCObject* from, const std::string& weightType) const ; + + /** From-object related to the given object with a highest weight (the inverse relationship). + * LCObject is of type getFromType(). + */ + virtual const EVENT::LCObject* getRelatedFromMaxWeightObject(EVENT::LCObject* to, const std::string& weightType) const ; + + /** The highest weight of the relations returned by a call to getRelatedToObjects(from). + * @see getRelatedToObjects + */ + virtual float getRelatedToMaxWeight(EVENT::LCObject* from, const std::string& weightType) const ; + + /** The highest weight of the relations returned by a call to getRelatedFromObjects(to). + * @see getRelatedFromObjects + */ + virtual float getRelatedFromMaxWeight(EVENT::LCObject* to, const std::string& weightType) const ; + /** Adds a relation. If there is already an existing relation between the two given objects * the weight (or default weight 1.0) is added to that relationship's weight. */ diff --git a/src/cpp/src/UTIL/LCRelationNavigator.cc b/src/cpp/src/UTIL/LCRelationNavigator.cc index 2af07b5a6..d24363dbc 100644 --- a/src/cpp/src/UTIL/LCRelationNavigator.cc +++ b/src/cpp/src/UTIL/LCRelationNavigator.cc @@ -68,6 +68,55 @@ namespace UTIL{ return _rMap[ to ].second ; } + const EVENT::LCObject* LCRelationNavigator::getRelatedToMaxWeightObject(EVENT::LCObject* from, const std::string& weightType) const { + const auto& objects = getRelatedToObjects(from); + const auto& weights = getRelatedToWeights(from); + if ( objects.empty() ) return nullptr; + auto maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return a < b;}); + if (weightType == "track") maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)%10000)/1000. < (int(b)%10000)/1000.;}); + else if (weightType == "cluster") maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)/10000)/1000. < (int(b)/10000)/1000. ;}); + + int i = std::distance(weights.begin(), maxWeightIt); + return objects[i]; + } + + const EVENT::LCObject* LCRelationNavigator::getRelatedFromMaxWeightObject(EVENT::LCObject* to, const std::string& weightType) const { + const auto& objects = getRelatedToObjects(to); + const auto& weights = getRelatedToWeights(to); + if ( objects.empty() ) return nullptr; + + auto maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return a < b;}); + if (weightType == "track") maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)%10000)/1000. < (int(b)%10000)/1000.;}); + else if (weightType == "cluster") maxWeightIt = std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)/10000)/1000. < (int(b)/10000)/1000. ;}); + + int i = std::distance(weights.begin(), maxWeightIt); + return objects[i]; + } + + float LCRelationNavigator::getRelatedToMaxWeight(EVENT::LCObject* from, const std::string& weightType) const { + const auto& objects = getRelatedToObjects(from); + const auto& weights = getRelatedToWeights(from); + if ( objects.empty() ) return 0.; + + float maxWeight = 0.; + if (weightType == "track") maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)%10000)/1000. < (int(b)%10000)/1000.;}); + else if (weightType == "cluster") maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)/10000)/1000. < (int(b)/10000)/1000. ;}); + else maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return a < b ;}); + return maxWeight; + } + + float LCRelationNavigator::getRelatedFromMaxWeight(EVENT::LCObject* to, const std::string& weightType) const { + const auto& objects = getRelatedToObjects(to); + const auto& weights = getRelatedToWeights(to); + if ( objects.empty() ) return 0.; + + float maxWeight = 0.; + if (weightType == "track") maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)%10000)/1000. < (int(b)%10000)/1000.;}); + else if (weightType == "cluster") maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return (int(a)/10000)/1000. < (int(b)/10000)/1000. ;}); + else maxWeight = *std::max_element(weights.begin(), weights.end(), [](float a, float b){return a < b ;}); + return maxWeight; + } + void LCRelationNavigator::addRelation(EVENT::LCObject * from, EVENT::LCObject * to, float weight) {