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