Belle II Software development
BestVXDTrackCandidatesSelectorModule.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/BestVXDTrackCandidatesSelectorModule.h"
10#include <vector>
11#include <numeric>
12
13using namespace Belle2;
14
15
16REG_MODULE(BestVXDTrackCandidatesSelector);
17
19{
20 setDescription("Module that selects a subset with a fixed size x out of all SpacePointTrackCandidates. Based on qualityIndicator.");
22
23
24 addParam("NameSpacePointTrackCands", m_nameSpacePointTrackCands, "Name of expected StoreArray.", std::string(""));
25 addParam("SubsetSize", m_subsetSize, "Target size of selected subset.", (unsigned short)(1000));
26 addParam("SubsetCreation", m_subsetCreation,
27 "If True copy selected SpacePoints to new StoreArray, if False deactivate remaining SpacePoints.", bool(false));
28 addParam("NewNameSpacePointTrackCands", m_newNameSpacePointTrackCands,
29 "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.",
30 std::string("BestSpacePointTrackCands"));
31}
32
34{
36 if (m_subsetCreation) {
39 } else {
41 m_bestCandidates.inheritAllRelations();
42 }
43 }
44}
45
47{
50}
51
53{
54 const int nTracks = m_spacePointTrackCands.getEntries();
55 // define subset
56 if (nTracks > m_subsetSize) {
57 // sort by lowest -> highest quality index
58 std::vector<int> sortedTrackCandIndices = getSortedTrackCandIndices(true);
59
60 // deactivate candidates with lowest quality index until desired subsetSize is reached;
61 for (int iTracks = 0; iTracks < nTracks - m_subsetSize; ++iTracks) {
62 int iCandidate = sortedTrackCandIndices[iTracks];
63 m_spacePointTrackCands[iCandidate]->removeRefereeStatus(SpacePointTrackCand::c_isActive);
64 // Note: assignment state of individual hits does not have to be changed here (assignment not set until SPTC2RTConverterModule)
65 }
66 }
67}
68
70{
71 const unsigned int nTracks = m_spacePointTrackCands.getEntries();
72 // sorting is only required if there are too many candidates
73 if (nTracks > m_subsetSize) {
74 // sort by highest -> lowest quality index
75 std::vector<int> sortedTrackCandIndices = getSortedTrackCandIndices(false);
76
77 // select subset of desired size
78 std::set<int> subset(sortedTrackCandIndices.cbegin(), sortedTrackCandIndices.cbegin() + m_subsetSize);
79 m_bestCandidates.select([subset](const SpacePointTrackCand * sptc) {return subset.count(sptc->getArrayIndex()) != 0;});
80 } else {
81 // only need to do something if target StoreArray is different from source StoreArray
83 m_bestCandidates.select([](const SpacePointTrackCand*) {return true;});
84 }
85 }
86}
87
89{
90 // Create an index for all spacePointTrackCandidates in the StoreArray.
91 // Should be faster than calling 'getArrayIndex()' on all of them.
92 std::vector<int> sortedTrackCandIndices(m_spacePointTrackCands.getEntries());
93 std::iota(sortedTrackCandIndices.begin(), sortedTrackCandIndices.end(), 0);
94
95 std::sort(sortedTrackCandIndices.begin(), sortedTrackCandIndices.end(),
96 [this, increasing](const int lhs, const int rhs) {
97 if (increasing) return m_spacePointTrackCands[lhs]->getQualityIndicator() <
98 m_spacePointTrackCands[rhs]->getQualityIndicator();
99 else return m_spacePointTrackCands[lhs]->getQualityIndicator() > m_spacePointTrackCands[rhs]->getQualityIndicator();
100 });
101
102 return sortedTrackCandIndices;
103}
104
void initialize() override final
Requires SpacePointTrackCands.
std::string m_newNameSpacePointTrackCands
Name of optional output StoreArray containing SpacePointTrackCands.
std::vector< int > getSortedTrackCandIndices(bool increasing)
Return StoreArrayIndices of all candidates sorted by their qualityIndicator.
void event() override final
Application of the cut.
SelectSubset< SpacePointTrackCand > m_bestCandidates
SubsetSelector operating on a custom selection criteria.
void selectSubset()
Copy or delete candidates to achieve a subset creation.
bool m_subsetCreation
If True copy selected SpacePointTrackCands to new StoreArray, If False deactivate remaining SpacePoin...
StoreArray< SpacePointTrackCand > m_spacePointTrackCands
StoreArray for input SpacePointTrackCands.
void deactivateCandidates()
Don't copy/delete candidates but rather deactivate them by setting a SpacePointTrackCandidate flag.
std::string m_nameSpacePointTrackCands
Name of input StoreArray containing SpacePointTrackCands.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Storage for (VXD) SpacePoint-based track candidates.
@ c_isActive
bit 11: SPTC is active (i.e.
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 addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#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.