Belle II Software development
OverlapMatrixCreator.h
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#pragma once
9
10#include <vector>
11#include <algorithm>
12
13namespace Belle2 {
20 public:
28 OverlapMatrixCreator(std::vector <std::vector <unsigned short> > const& hitRelatedTracks,
29 unsigned short nSpacePointTrackCandidates) :
30 m_hitRelatedTracks(hitRelatedTracks), m_overlapMatrix(nSpacePointTrackCandidates)
31 {}
32
38 std::vector <std::vector<unsigned short> > getOverlapMatrix(unsigned allowedOverlaps = 0)
39 {
40 //Loop over all the hits and make corresponding connections for the tracks
41 //This yields unordered indices of tracks, one for each shared hit.
42 for (auto&& tracks : m_hitRelatedTracks) {
43 for (unsigned short ii = 0; ii < tracks.size(); ii++) {
44 for (unsigned short jj = ii + 1; jj < tracks.size(); jj++) {
45 m_overlapMatrix[tracks[ii]].push_back(tracks[jj]);
46 m_overlapMatrix[tracks[jj]].push_back(tracks[ii]);
47 }
48 }
49 }
50
51 //If it makes sense to have this can be explored by Jonas and Felix...
52 //... so let's start with something that works, not necessarily what is fastest.
53 //This is probably slower then the version below for 0 overlaps, but shall work with
54 //any number of allowed overlaps.
55 if (allowedOverlaps) {
56 std::vector <unsigned short> overlapChache;
57
58 for (auto&& overlapTracks : m_overlapMatrix) {
59 std::sort(overlapTracks.begin(), overlapTracks.end());
60
61 auto endIter = overlapTracks.end();
62 auto cacheIter = overlapTracks.begin();
63 for (auto iter = overlapTracks.begin(); iter != endIter; iter++) {
64 if (*iter != *cacheIter) {
65 cacheIter = iter;
66 } else if (iter - cacheIter == allowedOverlaps) {
67 overlapChache.push_back(*iter);
68 }
69 }
70 overlapTracks = overlapChache;
71 overlapChache.clear();
72 }
73 return m_overlapMatrix;
74 }
75
76 //sort and erase duplicate overlaps
77 //TODO: Check in realistic situation alternative approach:
78 //see http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector
79 for (auto&& overlapTracks : m_overlapMatrix) {
80 std::sort(overlapTracks.begin(), overlapTracks.end());
81 overlapTracks.erase(std::unique(overlapTracks.begin(), overlapTracks.end()), overlapTracks.end());
82 }
83
84 return m_overlapMatrix;
85 }
86
87 private:
88 std::vector<std::vector <unsigned short> >const& m_hitRelatedTracks;
89 std::vector<std::vector <unsigned short> > m_overlapMatrix;
90 };
92}
Creates a vector of vectors, that knows which track is conflicting with which other.
std::vector< std::vector< unsigned short > > const & m_hitRelatedTracks
Input information, see constructor.
std::vector< std::vector< unsigned short > > getOverlapMatrix(unsigned allowedOverlaps=0)
Fills and returns the overlap matrix.
OverlapMatrixCreator(std::vector< std::vector< unsigned short > > const &hitRelatedTracks, unsigned short nSpacePointTrackCandidates)
Constructor taking information necessary to perform algorithm.
std::vector< std::vector< unsigned short > > m_overlapMatrix
Output information, see getOverlapMatrix.
Abstract base class for different kinds of events.