Belle II Software  release-05-01-25
WeightedRelationCreator.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/findlets/base/Findlet.h>
13 
14 #include <tracking/trackFindingCDC/eventdata/utils/ClassMnemomics.h>
15 
16 #include <tracking/trackFindingCDC/filters/base/RelationFilterUtil.h>
17 
18 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
19 
20 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
21 #include <tracking/trackFindingCDC/utilities/Functional.h>
22 
23 #include <framework/core/ModuleParamList.h>
24 #include <framework/logging/Logger.h>
25 
26 #include <vector>
27 #include <string>
28 #include <algorithm>
29 
30 namespace Belle2 {
37  namespace TrackFindingCDC {
38 
49  template <class AObject, class ARelationFilter>
50  class WeightedRelationCreator : public Findlet<AObject* const, WeightedRelation<AObject>> {
51 
52  private:
54  using Super = Findlet<AObject, WeightedRelation<AObject> >;
55 
56  public:
59  {
61  }
62 
64  std::string getDescription() final {
65  return "Constructs geometrically constrained relations between " +
67  " filter by some acceptance criterion.";
68  }
69 
71  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) final {
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  B2ASSERT("Expected the objects on which relations are constructed to be sorted",
85  std::is_sorted(inputObjects.begin(), inputObjects.end(), LessOf<Deref>()));
86 
87  RelationFilterUtil::appendUsing(m_relationFilter, inputObjects, weightedRelations);
88 
90  {
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  // cppcheck-suppress unreadVariable
103  nCurrentRepetitions = 1;
104  return false;
105  }
106  };
107 
108  auto itLast = std::unique(weightedRelations.begin(), weightedRelations.end(), sameFrom);
109  weightedRelations.erase(itLast, weightedRelations.end());
110  }
111  }
112 
113  private:
115  ARelationFilter m_relationFilter;
116 
118  int m_param_onlyBest = 0;
119  };
120  }
122 }
Belle2::TrackFindingCDC::BinaryJoin
Functor factory turning a binary functor and two functors into a new functor which executes the binar...
Definition: Functional.h:137
Belle2::TrackFindingCDC::WeightedRelationCreator::Super
Findlet< AObject, WeightedRelation< AObject > > Super
Type of the base class.
Definition: WeightedRelationCreator.h:62
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::addProcessingSignalListener
void addProcessingSignalListener(ProcessingSignalListener *psl)
Register a processing signal listener to be notified.
Definition: CompositeProcessingSignalListener.cc:57
Belle2::TrackFindingCDC::WeightedRelationCreator::getDescription
std::string getDescription() final
Short description of the findlet.
Definition: WeightedRelationCreator.h:72
Belle2::TrackFindingCDC::WeightedRelationCreator::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
Definition: WeightedRelationCreator.h:79
Belle2::TrackFindingCDC::RelationFilterUtil::appendUsing
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.
Definition: RelationFilterUtil.h:71
Belle2::getClassMnemomicParameterDescription
std::string getClassMnemomicParameterDescription(const RecoTrack *dispatchTag __attribute__((unused)))
Returns a short description for class RecoTrack to be used in descriptions of parameters.
Definition: ClassMnemomics.h:33
Belle2::TrackFindingCDC::WeightedRelationCreator::m_param_onlyBest
int m_param_onlyBest
Parameter : Maximal number of the best relations from each item to keep.
Definition: WeightedRelationCreator.h:126
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::WeightedRelationCreator::WeightedRelationCreator
WeightedRelationCreator()
Constructor registering the subordinary findlets to the processing signal distribution machinery.
Definition: WeightedRelationCreator.h:66
Belle2::TrackFindingCDC::WeightedRelationCreator::apply
void apply(const std::vector< AObject * > &inputObjects, std::vector< WeightedRelation< AObject >> &weightedRelations) final
Main function.
Definition: WeightedRelationCreator.h:89
Belle2::TrackFindingCDC::WeightedRelationCreator::m_relationFilter
ARelationFilter m_relationFilter
Relation filter used to select relations.
Definition: WeightedRelationCreator.h:123
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46