Belle II Software  release-05-01-25
SPTCSelectorXBestPerFamily.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jonas Wagner, Felix Metzner *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <vector>
13 #include <numeric>
14 
15 #include <tracking/spacePointCreation/SpacePointTrackCand.h>
16 #include <tracking/trackFindingVXD/trackQualityEstimators/QualityEstimatorBase.h>
17 #include <tracking/trackFindingVXD/trackQualityEstimators/QualityEstimatorTripletFit.h>
18 #include <tracking/trackFindingVXD/trackQualityEstimators/QualityEstimatorCircleFit.h>
19 #include <tracking/trackFindingVXD/trackQualityEstimators/QualityEstimatorRiemannHelixFit.h>
20 #include <tracking/trackFindingVXD/trackQualityEstimators/QualityEstimatorRandom.h>
21 
22 
23 namespace Belle2 {
31  class SPTCSelectorXBestPerFamily {
32 
33  public:
34 
40  SPTCSelectorXBestPerFamily(unsigned short xBest = 5, const std::string& estimationMethod = std::string("tripletFit")):
41  m_xBest(xBest)
42  {
43  if (estimationMethod == "tripletFit") {
44  m_estimator = std::make_unique<QualityEstimatorTripletFit>();
45  } else if (estimationMethod == "circleFit") {
46  m_estimator = std::make_unique<QualityEstimatorCircleFit>();
47  } else if (estimationMethod == "helixFit") {
48  m_estimator = std::make_unique<QualityEstimatorRiemannHelixFit>();
49  } else if (estimationMethod == "random") {
50  m_estimator = std::make_unique<QualityEstimatorRandom>();
51  }
52  B2ASSERT("QualityEstimator could not be initialized with method: " << estimationMethod, m_estimator);
53  };
54 
56  ~SPTCSelectorXBestPerFamily() = default;
57 
63  void prepareSelector(unsigned short nFamilies)
64  {
65  m_bestPaths.clear();
66  m_familyToIndex.clear();
67  m_bestPaths.reserve(nFamilies);
68  m_familyToIndex.resize(nFamilies, -1);
69 
70  m_currentIndex = 0;
71  }
72 
79  {
80  sptc.setQualityIndicator(m_estimator->estimateQuality(sptc.getSortedHits()));
81  short family = sptc.getFamily();
82 
83  if (m_familyToIndex.at(family) == -1) {
84  m_bestPaths.emplace_back(std::multiset<SpacePointTrackCand> { sptc });
85  m_familyToIndex.at(family) = m_currentIndex;
87  return;
88  }
89 
90  auto& currentSet = m_bestPaths.at(m_familyToIndex[family]);
91 
92  if (currentSet.size() == m_xBest) {
93  std::multiset<SpacePointTrackCand>::iterator iter = currentSet.begin();
94  if (sptc.getQualityIndicator() < iter->getQualityIndicator()) {
95  return;
96  }
97  currentSet.erase(iter);
98  }
99  currentSet.emplace_hint(currentSet.cbegin(), sptc);
100  }
101 
105  std::vector<SpacePointTrackCand> returnSelection() const
106  {
107  std::vector<SpacePointTrackCand> jointBestPaths;
108  jointBestPaths.reserve(std::accumulate(m_bestPaths.begin(), m_bestPaths.end(), 0,
109  [](int a, auto b) { return a + b.size(); }));
110 
111  for (auto && set : m_bestPaths) {
112  jointBestPaths.insert(jointBestPaths.end(), set.begin(), set.end());
113  }
114 
115  return jointBestPaths;
116  }
117 
121  void setMagneticFieldForQE(double bFieldZ)
122  {
123  m_estimator->setMagneticFieldStrength(bFieldZ);
124  }
125 
126  private:
127 
129  std::unique_ptr<QualityEstimatorBase> m_estimator;
130 
132  std::vector<std::multiset<SpacePointTrackCand> > m_bestPaths;
133 
135  std::vector<short> m_familyToIndex;
136 
139  unsigned short m_currentIndex = 0;
140 
142  unsigned short m_xBest;
143  };
145 }
Belle2::SPTCSelectorXBestPerFamily::testNewSPTC
void testNewSPTC(SpacePointTrackCand &sptc)
Test new SPTC if it is better than the least best of the current x best SPTCs of its respective famil...
Definition: SPTCSelectorXBestPerFamily.h:86
Belle2::SpacePointTrackCand::getSortedHits
const std::vector< const Belle2::SpacePoint * > getSortedHits() const
get hits (space points) sorted by their respective sorting parameter
Definition: SpacePointTrackCand.cc:103
Belle2::SPTCSelectorXBestPerFamily::m_estimator
std::unique_ptr< QualityEstimatorBase > m_estimator
Pointer to the Quality Estimator used to evaluate the SPTCs to find the best.
Definition: SPTCSelectorXBestPerFamily.h:137
Belle2::SpacePointTrackCand::getQualityIndicator
double getQualityIndicator() const
returns the current status of the estimated quality of this track candidate.
Definition: SpacePointTrackCand.h:222
Belle2::SPTCSelectorXBestPerFamily::setMagneticFieldForQE
void setMagneticFieldForQE(double bFieldZ)
Setting magnetic field for the quality estimator.
Definition: SPTCSelectorXBestPerFamily.h:129
Belle2::SPTCSelectorXBestPerFamily::m_xBest
unsigned short m_xBest
Number of allowed best SPTCs per family.
Definition: SPTCSelectorXBestPerFamily.h:150
Belle2::SPTCSelectorXBestPerFamily::prepareSelector
void prepareSelector(unsigned short nFamilies)
Preparation of Best Candidate Selector by resetting the vectors.
Definition: SPTCSelectorXBestPerFamily.h:71
Belle2::SPTCSelectorXBestPerFamily::m_bestPaths
std::vector< std::multiset< SpacePointTrackCand > > m_bestPaths
Vector containing one vector of the best SPTCs per family.
Definition: SPTCSelectorXBestPerFamily.h:140
Belle2::SPTCSelectorXBestPerFamily::m_currentIndex
unsigned short m_currentIndex
Counter for current index used for m_familyToIndex.
Definition: SPTCSelectorXBestPerFamily.h:147
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SPTCSelectorXBestPerFamily::SPTCSelectorXBestPerFamily
SPTCSelectorXBestPerFamily(unsigned short xBest=5, const std::string &estimationMethod=std::string("tripletFit"))
Constructor for the selection of the x best candidates for each family based on the quality index.
Definition: SPTCSelectorXBestPerFamily.h:48
Belle2::SPTCSelectorXBestPerFamily::m_familyToIndex
std::vector< short > m_familyToIndex
Map of family number to respective index for m_bestPaths.
Definition: SPTCSelectorXBestPerFamily.h:143
Belle2::SPTCSelectorXBestPerFamily::~SPTCSelectorXBestPerFamily
~SPTCSelectorXBestPerFamily()=default
Destructor.
Belle2::SPTCSelectorXBestPerFamily::returnSelection
std::vector< SpacePointTrackCand > returnSelection() const
Return vector containing the best SPTCs; maximal m_xBest per family.
Definition: SPTCSelectorXBestPerFamily.h:113
Belle2::SpacePointTrackCand::getFamily
short getFamily() const
return family identifier
Definition: SpacePointTrackCand.h:285
Belle2::SpacePointTrackCand
Storage for (VXD) SpacePoint-based track candidates.
Definition: SpacePointTrackCand.h:51
Belle2::SpacePointTrackCand::setQualityIndicator
void setQualityIndicator(const double newIndicator)
sets the new status of the estimated quality of this track candidate.
Definition: SpacePointTrackCand.h:321