Belle II Software development
WeightedRelationCreator.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackingUtilities/findlets/base/Findlet.h>
11
12#include <tracking/trackingUtilities/eventdata/utils/ClassMnemomics.h>
13
14#include <tracking/trackingUtilities/filters/base/RelationFilterUtil.h>
15
16#include <tracking/trackingUtilities/utilities/WeightedRelation.h>
17
18#include <tracking/trackingUtilities/utilities/StringManipulation.h>
19#include <tracking/trackingUtilities/utilities/Functional.h>
20
21#include <framework/core/ModuleParamList.h>
22#include <framework/logging/Logger.h>
23
24#include <vector>
25#include <string>
26#include <algorithm>
27
28namespace Belle2 {
33
34
35 namespace TrackingUtilities {
36
47 template <class AObject, class ARelationFilter>
48 class WeightedRelationCreator : public Findlet<AObject* const, WeightedRelation<AObject>> {
49
50 private:
53
54 public:
60
62 std::string getDescription() final
63 {
64 return "Constructs geometrically constrained relations between " +
65 getClassMnemomicParameterDescription((AObject*)nullptr) +
66 " filter by some acceptance criterion.";
67 }
68
70 void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) final
71 {
72 m_relationFilter.exposeParameters(moduleParamList, prefix);
73 moduleParamList->addParameter(prefixed(prefix, "onlyBest"),
75 "Maximal number of the best relation to keep from each " +
76 getClassMnemomicParameterDescription((AObject*)nullptr),
78 }
79
81 void apply(const std::vector<AObject*>& inputObjects,
82 std::vector<WeightedRelation<AObject>>& weightedRelations) final
83 {
84
85 B2ASSERT("Expected the objects on which relations are constructed to be sorted",
86 std::is_sorted(inputObjects.begin(), inputObjects.end(), LessOf<Deref>()));
87
88 RelationFilterUtil::appendUsing(m_relationFilter, inputObjects, weightedRelations);
89
90 if (m_param_onlyBest > 0) {
91 const int nMaxRepetitions = m_param_onlyBest;
92 int nCurrentRepetitions = 1;
93 auto sameFrom =
94 [&nMaxRepetitions,
95 &nCurrentRepetitions](const WeightedRelation<AObject>& relation,
96 const WeightedRelation<AObject>& otherRelation) -> bool {
97 if (relation.getFrom() == otherRelation.getFrom())
98 {
99 ++nCurrentRepetitions;
100 return nCurrentRepetitions > nMaxRepetitions;
101 } else
102 {
103 // cppcheck-suppress unreadVariable
104 nCurrentRepetitions = 1;
105 return false;
106 }
107 };
108
109 auto itLast = std::unique(weightedRelations.begin(), weightedRelations.end(), sameFrom);
110 weightedRelations.erase(itLast, weightedRelations.end());
111 }
112 }
113
114 private:
116 ARelationFilter m_relationFilter;
117
120 };
121 }
123}
The Module parameter list class.
Interface for a minimal algorithm part that wants to expose some parameters to a module.
Definition Findlet.h:26
std::string getDescription() final
Short description of the findlet.
WeightedRelationCreator()
Constructor registering the subordinary findlets to the processing signal distribution machinery.
void apply(const std::vector< AObject * > &inputObjects, std::vector< WeightedRelation< AObject > > &weightedRelations) final
Main function.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
ARelationFilter m_relationFilter
Relation filter used to select relations.
Findlet< AObject, WeightedRelation< AObject > > Super
Type of the base class.
int m_param_onlyBest
Parameter : Maximal number of the best relations from each item to keep.
Type for two related objects with a weight.
Abstract base class for different kinds of events.
static void appendUsing(ARelationFilter &relationFilter, const std::vector< AObject * > &froms, const std::vector< AObject * > &tos, std::vector< WeightedRelation< AObject > > &weightedRelations, unsigned int maximumNumberOfRelations=std::numeric_limits< unsigned int >::max())
Appends relations between elements in the given AItems using the ARelationFilter.
std::string getClassMnemomicParameterDescription(const CDCWireHit *dispatchTag)
Returns a short description for class CDCWireHit to be used in descriptions of parameters.