Belle II Software  release-08-01-10
TrackCandidateOverlapResolver.cc
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 #include <tracking/vxdHoughTracking/findlets/TrackCandidateOverlapResolver.h>
9 #include <framework/core/ModuleParamList.h>
10 #include <framework/core/ModuleParamList.templateDetails.h>
11 #include <tracking/spacePointCreation/SpacePointTrackCand.h>
12 #include <tracking/spacePointCreation/SpacePoint.h>
13 #include <tracking/trackFindingVXD/trackSetEvaluator/OverlapMatrixCreator.h>
14 #include <tracking/trackFindingVXD/trackSetEvaluator/HopfieldNetwork.h>
15 #include <tracking/trackFindingVXD/trackSetEvaluator/Scrooge.h>
16 #include <tracking/trackFindingVXD/trackSetEvaluator/OverlapResolverNodeInfo.h>
17 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
18 
19 using namespace Belle2;
20 using namespace TrackFindingCDC;
21 using namespace vxdHoughTracking;
22 
23 TrackCandidateOverlapResolver::~TrackCandidateOverlapResolver() = default;
24 
25 
26 TrackCandidateOverlapResolver::TrackCandidateOverlapResolver() : Super()
27 {
28 }
29 
30 void TrackCandidateOverlapResolver::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
31 {
32  Super::exposeParameters(moduleParamList, prefix);
33 
34  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "ResolveMethod"), m_resolveMethod,
35  "Strategy used to resolve overlaps. Currently implemented are \"greedy\" and \"hopfield\".",
37  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "NameSVDClusters"), m_nameSVDClusters,
38  "Name of expected SVDClusters StoreArray.", m_nameSVDClusters);
39 
40  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minActivityState"), m_minActivityState,
41  "Sets the minimal value of activity for acceptance. [0,1]", m_minActivityState);
42 }
43 
45 {
47 
49 
50  B2ASSERT("ResolveMethod has to be either 'greedy' or 'hopfield'. Selected ResolveMethod: " << m_resolveMethod,
51  m_resolveMethod == "greedy" || m_resolveMethod == "hopfield");
52 
53 }
54 
55 void TrackCandidateOverlapResolver::apply(std::vector<SpacePointTrackCand>& spacePointTrackCandsToResolve)
56 {
57  //Create matrix[svdCluster][track]
58  unsigned short nHits = m_svdClusters.getEntries();
59 
60  //Create subset of active Candidates
61  std::vector<SpacePointTrackCand*> activeCandidates;
62  auto requiredSpace = spacePointTrackCandsToResolve.size();
63  activeCandidates.reserve(requiredSpace);
64  for (SpacePointTrackCand& sptc : spacePointTrackCandsToResolve) {
65  if (sptc.hasRefereeStatus(SpacePointTrackCand::c_isActive)) activeCandidates.push_back(&sptc);
66  }
67  unsigned short const nActiveCandidates = activeCandidates.size();
68  if (nActiveCandidates < 2) {
69  B2DEBUG(29, "Less than 2 active SPTC. No reason to do SVDOverlapResolver!");
70  return;
71  }
72 
73  //now fill the cluster/track matrix:
74  std::vector<std::vector<unsigned short> > svdHitRelatedTracks(nHits);
75  //TODO: Check if one saves time by reserving some space for each single of those vectors;
76  for (unsigned short iCand = 0; iCand < nActiveCandidates; ++iCand) {
77 
78  for (const SpacePoint* spacePointPointer : activeCandidates.at(iCand)->getHits()) {
79  //only SVD is handled with this algorithm
80  if (spacePointPointer->getType() != VXD::SensorInfoBase::SensorType::SVD) continue;
81 
82  //at the position of the svdCluster Index, the track index is pushed back;
83  RelationVector<SVDCluster> svdClusterRelations = spacePointPointer->getRelationsTo<SVDCluster>(m_nameSVDClusters);
84  for (SVDCluster const& svdClusterPointer : svdClusterRelations) {
85  svdHitRelatedTracks[svdClusterPointer.getArrayIndex()].push_back(iCand);
86  }
87  }
88  }
89 
90  //Create the overlap matrix and store it into the OverlapNetwork
91  OverlapMatrixCreator overlapMatrixCreator(svdHitRelatedTracks, nActiveCandidates);
92  auto overlapMatrix = overlapMatrixCreator.getOverlapMatrix();
93 
94  //Resolve overlap
95  //Create an empty object of the type,
96  //that needs to be given to Scrooge.
97  std::vector<OverlapResolverNodeInfo> qiTrackOverlap;
98  qiTrackOverlap.reserve(nActiveCandidates);
99 
100  //fill this object with the necessary information:
101  for (unsigned short iCand = 0; iCand < nActiveCandidates; ++iCand) {
102  qiTrackOverlap.emplace_back(
103  activeCandidates[iCand]->getQualityIndicator(),
104  iCand,
105  overlapMatrix.at(iCand),
106  1);
107  }
108 
109  if (m_resolveMethod == "greedy") {
110  //make a Scrooge and udpate the activity
111  Scrooge scrooge;
112  scrooge.performSelection(qiTrackOverlap);
113 
114  } else if (m_resolveMethod == "hopfield") {
115  //Performs the actual HNN.
116  //As the parameter is taken as reference, the values are changed and can be reused below.
117  HopfieldNetwork hopfieldNetwork;
118  unsigned maxIterations = 20;
119  if (hopfieldNetwork.doHopfield(qiTrackOverlap, maxIterations) == maxIterations) {
120  B2WARNING("Hopfield Network failed converge.");
121  }
122  }
123 
124  for (auto&& track : qiTrackOverlap) {
125  if (track.activityState < m_minActivityState) {
126  activeCandidates[track.trackIndex]->removeRefereeStatus(SpacePointTrackCand::c_isActive);
127  }
128  }
129 }
Hopfield Algorithm with number based inputs.
unsigned short doHopfield(std::vector< OverlapResolverNodeInfo > &overlapResolverNodeInfos, unsigned short nIterations=20)
Performance of the actual algorithm.
The Module parameter list class.
Creates a vector of vectors, that knows which track is conflicting with which other.
std::vector< std::vector< unsigned short > > getOverlapMatrix(unsigned allowedOverlaps=0)
Fills and returns the overlap matrix.
Class for type safe access to objects that are referred to in relations.
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition: SVDCluster.h:29
Executes greedy algorithm for vector of QITrackOverlap structs.
Definition: Scrooge.h:26
void performSelection(std::vector< OverlapResolverNodeInfo > &overlapResolverNodeInfo)
Sets the isActive flag in m_qiTrackOverlap to false, for killed tracks.
Definition: Scrooge.h:33
Storage for (VXD) SpacePoint-based track candidates.
@ c_isActive
bit 11: SPTC is active (i.e.
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:42
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
void initialize() override
Receive and dispatch signal before the start of the event processing.
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:69
Interface for an algorithm part that needs to receive the module processing signals.
void apply(std::vector< SpacePointTrackCand > &spacePointTrackCandsToResolve) override
Reject bad SpacePointTrackCands and bad hits inside the remaining.
StoreArray< SVDCluster > m_svdClusters
Stay connected to the DataStore for timing improvement.
std::string m_resolveMethod
Strategy used to resolve overlaps.
double m_minActivityState
Minimum of activityState of candidate required to be accepted by the algorithm.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the parameters of the sub findlets.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.