Belle II Software  release-06-02-00
SVDOverlapResolverModule.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 
9 #include <tracking/modules/trackSetEvaluatorVXD/SVDOverlapResolverModule.h>
10 
11 #include <framework/logging/Logger.h>
12 
13 
14 #include <tracking/spacePointCreation/SpacePoint.h>
15 
16 #include <tracking/trackFindingVXD/trackSetEvaluator/OverlapMatrixCreator.h>
17 #include <tracking/trackFindingVXD/trackSetEvaluator/HopfieldNetwork.h>
18 #include <tracking/trackFindingVXD/trackSetEvaluator/Scrooge.h>
19 #include <tracking/trackFindingVXD/trackSetEvaluator/OverlapResolverNodeInfo.h>
20 
21 #include <vxd/geometry/SensorInfoBase.h>
22 
23 #include <vector>
24 
25 using namespace std;
26 using namespace Belle2;
27 
28 REG_MODULE(SVDOverlapResolver)
29 
31 {
32  setDescription("Module checks for overlaps of SpacePointTrackCands");
33  setPropertyFlags(c_ParallelProcessingCertified);
34 
35  addParam("NameSpacePointTrackCands", m_nameSpacePointTrackCands, "Name of expected StoreArray.", string(""));
36 
37  addParam("NameSVDClusters", m_nameSVDClusters, "Name of expected StoreArray.", string(""));
38 
39  addParam("ResolveMethod", m_resolveMethod, "Strategy used to resolve overlaps. Currently implemented are \"greedy\" and "
40  " \"hopfield\" ", string("greedy"));
41 
42  addParam("minActivityState", m_minActivityState, "Sets the minimal value of activity for acceptance. (0,1)",
43  float(0.7));
44 }
45 
46 
47 void SVDOverlapResolverModule::initialize()
48 {
49  m_spacePointTrackCands.isRequired(m_nameSpacePointTrackCands);
50  m_svdClusters.isRequired(m_nameSVDClusters);
51 
52  B2ASSERT("ResolveMethod has to be either 'greedy' or 'hopfield'. Selected ResolveMethod: " << m_resolveMethod,
53  m_resolveMethod == "greedy" || m_resolveMethod == "hopfield");
54 }
55 
56 void SVDOverlapResolverModule::event()
57 {
58 
59  //Create matrix[svdCluster][track]
60  unsigned short nHits = m_svdClusters.getEntries();
61 
62  //Create subset of active Candidates
63  vector<SpacePointTrackCand*> activeCandidates;
64  auto requiredSpace = m_spacePointTrackCands.getEntries();
65  if (m_estimatedActiveCandidates < m_spacePointTrackCands.getEntries()) {
66  requiredSpace = m_estimatedActiveCandidates;
67  }
68  activeCandidates.reserve(requiredSpace);
69  for (SpacePointTrackCand& sptc : m_spacePointTrackCands) {
70  if (sptc.hasRefereeStatus(SpacePointTrackCand::c_isActive)) activeCandidates.push_back(&sptc);
71  }
72  unsigned short const nActiveCandidates = activeCandidates.size();
73  if (nActiveCandidates < 2) {
74  B2DEBUG(29, "Less than 2 active SPTC. No reason to do SVDOverlapResolver!");
75  return;
76  }
77 
78  //now fill the cluster/track matrix:
79  vector<vector<unsigned short> > svdHitRelatedTracks(nHits);
80  //TODO: Check if one saves time by reserving some space for each single of those vectors;
81  for (unsigned short iCand = 0; iCand < nActiveCandidates; ++iCand) {
82 
83  for (const SpacePoint* spacePointPointer : activeCandidates.at(iCand)->getHits()) {
84  //only SVD is handled with this algorithm
85  if (spacePointPointer->getType() != VXD::SensorInfoBase::SensorType::SVD) continue;
86 
87  //at the position of the svdCluster Index, the track index is pushed back;
88  RelationVector<SVDCluster> svdClusterRelations = spacePointPointer->getRelationsTo<SVDCluster>(m_nameSVDClusters);
89  for (SVDCluster const& svdClusterPointer : svdClusterRelations) {
90  svdHitRelatedTracks[svdClusterPointer.getArrayIndex()].push_back(iCand);
91  }
92  }
93  }
94 
95  //Create the overlap matrix and store it into the OverlapNetwork
96  OverlapMatrixCreator overlapMatrixCreator(svdHitRelatedTracks, nActiveCandidates);
97  auto overlapMatrix = overlapMatrixCreator.getOverlapMatrix();
98 
99  //Resolve overlap
100  //Create an empty object of the type,
101  //that needs to be given to Scrooge.
102  std::vector<OverlapResolverNodeInfo> qiTrackOverlap;
103  qiTrackOverlap.reserve(nActiveCandidates);
104 
105  //fill this object with the necessary information:
106  for (unsigned short iCand = 0; iCand < nActiveCandidates; ++iCand) {
107  qiTrackOverlap.emplace_back(
108  activeCandidates[iCand]->getQualityIndicator(),
109  iCand,
110  overlapMatrix.at(iCand),
111  1);
112  }
113 
114  if (m_resolveMethod == "greedy") {
115  //make a Scrooge and udpate the activity
116  Scrooge scrooge;
117  scrooge.performSelection(qiTrackOverlap);
118 
119  } else if (m_resolveMethod == "hopfield") {
120  //Performs the actual HNN.
121  //As the parameter is taken as reference, the values are changed and can be reused below.
122  HopfieldNetwork hopfieldNetwork;
123  unsigned maxIterations = 20;
124  if (hopfieldNetwork.doHopfield(qiTrackOverlap, maxIterations) == maxIterations) {
125  B2WARNING("Hopfield Network failed converge.");
126  }
127  }
128 
129  for (auto && track : qiTrackOverlap) {
130  if (track.activityState < m_minActivityState) {
131  activeCandidates[track.trackIndex]->removeRefereeStatus(SpacePointTrackCand::c_isActive);
132  }
133  }
134 }
Hopfield Algorithm with number based inputs.
unsigned short doHopfield(std::vector< OverlapResolverNodeInfo > &overlapResolverNodeInfos, unsigned short nIterations=20)
Performance of the actual algorithm.
Base class for Modules.
Definition: Module.h:72
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:28
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.
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:42
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.