Belle II Software development
SubGraphID.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
9#pragma once
10
11#include <framework/logging/Logger.h>
12#include <tracking/dataobjects/FullSecID.h>
13
14#include <string>
15#include <vector>
16
17namespace Belle2 {
24 class SubGraphID {
25 protected:
26 std::vector<unsigned> m_idChain;
30 bool areTheSameSector(unsigned a, unsigned b) const
31 {
32 FullSecID idA(a);
33 FullSecID idB(b);
34 return idA.getVxdID() == idB.getVxdID() and idA.getSecID() == idB.getSecID();
35 }
36
37 public:
38
40 bool operator==(const SubGraphID& b) const
41 {
42 unsigned chainsize = m_idChain.size();
43 if (b.size() != chainsize) { return false; }
44
45 for (unsigned i = 0; i < chainsize; i++) {
46 if (b.m_idChain[i] != m_idChain[i]) { return false; }
47 }
48
49 return true;
50 }
51
52
54 bool operator<(const SubGraphID& b) const
55 {
56 // sorting: if outermost (== first) sector > of b.outermost this > b. If outermost == b.outermost, step is repeated until end of chain length is reached. last straw: longer chain is bigger.
57 unsigned chainsize = m_idChain.size() < b.size() ? m_idChain.size() : b.size();
58 for (unsigned i = 0 ; i < chainsize; i++) {
59 if (m_idChain[i] < b.m_idChain[i]) return true;
60 if (m_idChain[i] == b.m_idChain[i]) continue;
61 return false;
62 }
63 return m_idChain.size() < b.size();
64 }
65
66
68 explicit SubGraphID(const std::vector<unsigned>& idChain) : m_idChain(idChain)
69 {
70 if (m_idChain.empty()) { B2FATAL("SubGraphID-Constructor, given idChain is empty - illegal usage!"); }
71 }
72
73
75 unsigned size() const { return m_idChain.size(); }
76
77
79 std::vector<unsigned>::const_iterator begin() const { return m_idChain.begin(); }
80
81
83 std::vector<unsigned>::const_iterator end() const { return m_idChain.end(); }
84
85
87 bool checkSharesTrunk(const SubGraphID& b) const
88 {
89 unsigned chainsize = m_idChain.size();
90 if (b.size() != chainsize) { return false; }
91
92 for (unsigned i = 0; i < chainsize - 1; i++) {
93 if (b.m_idChain[i] != m_idChain[i]) { return false; }
94 }
95
96 return true;
97 }
98
99
101 std::string print() const
102 {
103 std::string out;
104 for (unsigned iD : m_idChain) {
105 out += FullSecID(iD).getFullSecString() + " ";
106 }
107 return out;
108 }
109
110
112 unsigned isElementOf(const SubGraphID& b) const
113 { return isElementOf(b.m_idChain); }
114
115
117 unsigned isElementOf(const std::vector<unsigned>& b) const
118 {
119 if (size() < b.size()) return 0;
120
121 unsigned nIdentical = 0;
122 for (unsigned idA : m_idChain) {
123 for (unsigned idB : b) {
124 if (areTheSameSector(idA, idB)) nIdentical++;
125 }
126 }
127 return nIdentical;
128 }
129
130
132 bool hasElement(unsigned b) const
133 {
134 for (unsigned idA : m_idChain) {
135 if (areTheSameSector(idA, b)) return true;
136 }
137 return false;
138 }
139
140
142 bool areTheSameSectors(const SubGraphID& b) const
143 {
144 if (size() != b.size()) return false;
145 for (unsigned i = 0; i < size(); i++) {
146 if (areTheSameSector(m_idChain[i], b.m_idChain[i]) == false) return false;
147 }
148 return true;
149 }
150
151
153 std::vector<unsigned> hasSharedLayer() const
154 {
155 B2DEBUG(20, "hasSharedLayer-start with iD " << print() << ":");
156 std::vector<unsigned> outerOnes;
157 for (unsigned i = 0; i < size() - 1; i++) { // i: outer iD i+1: inner iD
158 B2DEBUG(20, "found layerIDs for outer/inner: " << FullSecID(m_idChain[i]).getLayerID() << "/" << FullSecID(
159 m_idChain[i + 1]).getLayerID());
160 if (FullSecID(m_idChain[i]).getLayerID() == FullSecID(m_idChain[i + 1]).getLayerID()) {
161 outerOnes.push_back(m_idChain[i]);
162 B2DEBUG(20, "hasSharedLayer good combi found with inner: " << FullSecID(m_idChain[i + 1]).getFullSecString() << ", storing outer: "
163 << FullSecID(m_idChain[i]).getFullSecString());
164 }
165 }
166 return outerOnes;
167 }
168
169
171 bool updateID(unsigned newID)
172 {
173 for (unsigned& secID : m_idChain) {
174 if (areTheSameSector(newID, secID) == false) continue;
175 FullSecID tempID(secID);
176 secID = FullSecID(tempID.getVxdID(), true, tempID.getSecID()).getFullSecID();
177 return true;
178 }
179
180 return false;
181 }
182
183
185 std::vector<FullSecID> getFullSecIDs() const
186 {
187 std::vector<FullSecID> ids;
188
189 for (auto& id : m_idChain) {
190 ids.push_back(FullSecID(id));
191 }
192 return ids;
193 }
194 }; // class SubGraphID
195
196
198} // namespace Belle2
199
200namespace std {
202 template <> struct hash<Belle2::SubGraphID> {
207 std::size_t operator()(const Belle2::SubGraphID& graphID) const
208 {
209 std::size_t hashVal = 0;
210 for (unsigned iD : graphID) {
211 hashVal = std::hash<unsigned>()(iD) ^ hashVal;
212 ++hashVal;
213 }
214 return hashVal;
215 }
216 };
217}
218
Class to identify a sector inside of the VXD.
Definition: FullSecID.h:33
std::string getFullSecString() const
returns the FullSecID coded as string compatible to secIDs stored in the xml-sectormaps
Definition: FullSecID.cc:116
VxdID getVxdID() const
returns VxdID of sensor.
Definition: FullSecID.h:138
unsigned int getFullSecID() const
returns the FullSecID coded as integer for further use (can be reconverted to FullSecID by using Full...
Definition: FullSecID.h:150
short int getSecID() const
returns SecID of current FullSecID (only unique for each sensor).
Definition: FullSecID.h:146
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
Definition: SubGraphID.h:24
unsigned size() const
returns chainLength of this id.
Definition: SubGraphID.h:75
bool hasElement(unsigned b) const
checks if given raw secID is part of this, ignores subLayerID.
Definition: SubGraphID.h:132
bool updateID(unsigned newID)
if newID is element of this (ignoring subLayerID), oldID will be replaced by newID....
Definition: SubGraphID.h:171
bool areTheSameSector(unsigned a, unsigned b) const
checks if two given ids are the same sector (while ignoring the sublayerID).
Definition: SubGraphID.h:30
unsigned isElementOf(const SubGraphID &b) const
checks if given sectorPack ( >= 1 sector) is part of this, ignores subLayerID.
Definition: SubGraphID.h:112
SubGraphID(const std::vector< unsigned > &idChain)
constructor, mandatory iDChain musst at least contain one iD.
Definition: SubGraphID.h:68
std::vector< unsigned > hasSharedLayer() const
returns raw IDs of entries being outer friend of sectors on same layer.
Definition: SubGraphID.h:153
bool operator<(const SubGraphID &b) const
overloaded '<'-operator for sorting algorithms
Definition: SubGraphID.h:54
std::vector< unsigned >::const_iterator end() const
returns last entry of IDchain.
Definition: SubGraphID.h:83
std::vector< FullSecID > getFullSecIDs() const
returns SecIDs coded as FullSecIDs:
Definition: SubGraphID.h:185
bool checkSharesTrunk(const SubGraphID &b) const
if both graphs have got the same IDs except the last one, they share a trunk.
Definition: SubGraphID.h:87
bool areTheSameSectors(const SubGraphID &b) const
checks if sectors are identical (while ignoring the subLayerID)
Definition: SubGraphID.h:142
std::string print() const
returns string of entries.
Definition: SubGraphID.h:101
std::vector< unsigned >::const_iterator begin() const
returns first entry of IDchain.
Definition: SubGraphID.h:79
unsigned isElementOf(const std::vector< unsigned > &b) const
checks if given sectorPack ( >= 1 sector) is part of this, ignores subLayerID.
Definition: SubGraphID.h:117
bool operator==(const SubGraphID &b) const
overloaded '=='-operator
Definition: SubGraphID.h:40
std::vector< unsigned > m_idChain
ids of this SubGraphID.
Definition: SubGraphID.h:26
Abstract base class for different kinds of events.
STL namespace.
std::size_t operator()(const Belle2::SubGraphID &graphID) const
Create Hash value from graphID.
Definition: SubGraphID.h:207