Belle II Software  release-05-01-25
BestVXDTrackCandidatesSelectorModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jonas Wagner *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include "tracking/modules/trackSetEvaluatorVXD/BestVXDTrackCandidatesSelectorModule.h"
12 #include <vector>
13 #include <numeric>
14 
15 using namespace Belle2;
16 
17 
18 REG_MODULE(BestVXDTrackCandidatesSelector)
19 
21 {
22  setDescription("Module that selects a subset with a fixed size x out of all SpacePointTrackCandidates. Based on qualityIndicator.");
23  setPropertyFlags(c_ParallelProcessingCertified);
24 
25 
26  addParam("NameSpacePointTrackCands", m_nameSpacePointTrackCands, "Name of expected StoreArray.", std::string(""));
27  addParam("SubsetSize", m_subsetSize, "Target size of selected subset.", (unsigned short)(1000));
28  addParam("SubsetCreation", m_subsetCreation,
29  "If True copy selected SpacePoints to new StoreArray, if False deactivate remaining SpacePoints.", bool(false));
30  addParam("NewNameSpacePointTrackCands", m_newNameSpacePointTrackCands,
31  "Only required if 'CreateNewStoreArray' is true. Name of StoreArray to store the subset. If the target name is equal to the source candidates not matching the selection criteria are deleted.",
32  std::string("BestSpacePointTrackCands"));
33 }
34 
36 {
38  if (m_subsetCreation) {
41  } else {
43  m_bestCandidates.inheritAllRelations();
44  }
45  }
46 }
47 
49 {
51  else deactivateCandidates();
52 }
53 
55 {
56  const int nTracks = m_spacePointTrackCands.getEntries();
57  // define subset
58  if (nTracks > m_subsetSize) {
59  // sort by lowest -> highest quality index
60  std::vector<int> sortedTrackCandIndices = getSortedTrackCandIndices(true);
61 
62  // deactivate candidates with lowest quality index until desired subsetSize is reached;
63  for (int iTracks = 0; iTracks < nTracks - m_subsetSize; ++iTracks) {
64  int iCandidate = sortedTrackCandIndices[iTracks];
65  m_spacePointTrackCands[iCandidate]->removeRefereeStatus(SpacePointTrackCand::c_isActive);
66  // Note: assignment state of individual hits does not have to be changed here (assignment not set until SPTC2RTConverterModule)
67  }
68  }
69 }
70 
72 {
73  const unsigned int nTracks = m_spacePointTrackCands.getEntries();
74  // sorting is only required if there are too many candidates
75  if (nTracks > m_subsetSize) {
76  // sort by highest -> lowest quality index
77  std::vector<int> sortedTrackCandIndices = getSortedTrackCandIndices(false);
78 
79  // select subset of desired size
80  std::set<int> subset(sortedTrackCandIndices.cbegin(), sortedTrackCandIndices.cbegin() + m_subsetSize);
81  m_bestCandidates.select([subset](const SpacePointTrackCand * sptc) {return subset.count(sptc->getArrayIndex()) != 0;});
82  } else {
83  // only need to do something if target StoreArray is different from source StoreArray
85  m_bestCandidates.select([](const SpacePointTrackCand*) {return true;});
86  }
87  }
88 }
89 
91 {
92  // Create an index for all spacePointTrackCandidates in the StoreArray.
93  // Should be faster than calling 'getArrayIndex()' on all of them.
94  std::vector<int> sortedTrackCandIndices(m_spacePointTrackCands.getEntries());
95  std::iota(sortedTrackCandIndices.begin(), sortedTrackCandIndices.end(), 0);
96 
97  std::sort(sortedTrackCandIndices.begin(), sortedTrackCandIndices.end(),
98  [this, increasing](const int lhs, const int rhs) {
99  if (increasing) return m_spacePointTrackCands[lhs]->getQualityIndicator() <
100  m_spacePointTrackCands[rhs]->getQualityIndicator();
101  else return m_spacePointTrackCands[lhs]->getQualityIndicator() > m_spacePointTrackCands[rhs]->getQualityIndicator();
102  });
103 
104  return sortedTrackCandIndices;
105 }
106 
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::BestVXDTrackCandidatesSelectorModule::deactivateCandidates
void deactivateCandidates()
Don't copy/delete candidates but rather deactivate them by setting a SpacePointTrackCandidate flag.
Definition: BestVXDTrackCandidatesSelectorModule.cc:54
Belle2::SpacePointTrackCand::c_isActive
@ c_isActive
bit 11: SPTC is active (i.e.
Definition: SpacePointTrackCand.h:94
Belle2::BestVXDTrackCandidatesSelectorModule::initialize
void initialize() override final
Requires SpacePointTrackCands.
Definition: BestVXDTrackCandidatesSelectorModule.cc:35
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::BestVXDTrackCandidatesSelectorModule::m_newNameSpacePointTrackCands
std::string m_newNameSpacePointTrackCands
Name of optional output StoreArray containing SpacePointTrackCands.
Definition: BestVXDTrackCandidatesSelectorModule.h:80
Belle2::BestVXDTrackCandidatesSelectorModule::getSortedTrackCandIndices
std::vector< int > getSortedTrackCandIndices(bool increasing)
Return StoreArrayIndices of all candidates sorted by their qualityIndicator.
Definition: BestVXDTrackCandidatesSelectorModule.cc:90
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
Belle2::BestVXDTrackCandidatesSelectorModule::m_subsetSize
unsigned short m_subsetSize
target size of subset
Definition: BestVXDTrackCandidatesSelectorModule.h:69
Belle2::BestVXDTrackCandidatesSelectorModule::m_subsetCreation
bool m_subsetCreation
If True copy selected SpacePointTrackCands to new StoreArray, If False deactivate remaining SpacePoin...
Definition: BestVXDTrackCandidatesSelectorModule.h:77
Belle2::BestVXDTrackCandidatesSelectorModule::m_nameSpacePointTrackCands
std::string m_nameSpacePointTrackCands
Name of input StoreArray containing SpacePointTrackCands.
Definition: BestVXDTrackCandidatesSelectorModule.h:72
Belle2::BestVXDTrackCandidatesSelectorModule::event
void event() override final
Application of the cut.
Definition: BestVXDTrackCandidatesSelectorModule.cc:48
Belle2::BestVXDTrackCandidatesSelectorModule::m_spacePointTrackCands
StoreArray< SpacePointTrackCand > m_spacePointTrackCands
StoreArray for input SpacePointTrackCands.
Definition: BestVXDTrackCandidatesSelectorModule.h:86
Belle2::BestVXDTrackCandidatesSelectorModule::selectSubset
void selectSubset()
Copy or delete candidates to achieve a subset creation.
Definition: BestVXDTrackCandidatesSelectorModule.cc:71
Belle2::BestVXDTrackCandidatesSelectorModule
Module that selects a subset with a fixed size x out of all SpacePointTrackCandidates.
Definition: BestVXDTrackCandidatesSelectorModule.h:44
Belle2::BestVXDTrackCandidatesSelectorModule::m_bestCandidates
SelectSubset< SpacePointTrackCand > m_bestCandidates
SubsetSelector operating on a custom selection criteria.
Definition: BestVXDTrackCandidatesSelectorModule.h:92
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::SpacePointTrackCand
Storage for (VXD) SpacePoint-based track candidates.
Definition: SpacePointTrackCand.h:51