Skip to content

Commit

Permalink
Added four utility methods to the LCRelationNavigator to allow a quic…
Browse files Browse the repository at this point in the history
…ker way to extract an object with the highest weight
  • Loading branch information
dudarboh committed Sep 12, 2022
1 parent 397f594 commit eee68fc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/cpp/include/UTIL/LCRelationNavigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
49 changes: 49 additions & 0 deletions src/cpp/src/UTIL/LCRelationNavigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit eee68fc

Please sign in to comment.