Belle II Software  release-05-02-19
SubGraphID.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler (jakob.lettenbichler@oeaw.ac.at) *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/logging/Logger.h>
14 #include <tracking/dataobjects/FullSecID.h>
15 
16 #include <string>
17 #include <vector>
18 
19 namespace Belle2 {
25  class SubGraphID {
27  protected:
28  std::vector<unsigned> m_idChain;
32  bool areTheSameSector(unsigned a, unsigned b) const
33  {
34  FullSecID idA(a);
35  FullSecID idB(b);
36  return idA.getVxdID() == idB.getVxdID() and idA.getSecID() == idB.getSecID();
37  }
38 
39  public:
40 
42  bool operator==(const SubGraphID& b) const
43  {
44  unsigned chainsize = m_idChain.size();
45  if (b.size() != chainsize) { return false; }
46 
47  for (unsigned i = 0; i < chainsize; i++) {
48  if (b.m_idChain[i] != m_idChain[i]) { return false; }
49  }
50 
51  return true;
52  }
53 
54 
56  bool operator<(const SubGraphID& b) const
57  {
58  // 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.
59  unsigned chainsize = m_idChain.size() < b.size() ? m_idChain.size() : b.size();
60  for (unsigned i = 0 ; i < chainsize; i++) {
61  if (m_idChain[i] < b.m_idChain[i]) return true;
62  if (m_idChain[i] == b.m_idChain[i]) continue;
63  return false;
64  }
65  return m_idChain.size() < b.size();
66  }
67 
68 
70  explicit SubGraphID(const std::vector<unsigned>& idChain) : m_idChain(idChain)
71  {
72  if (m_idChain.empty()) { B2FATAL("SubGraphID-Constructor, given idChain is empty - illegal usage!"); }
73  }
74 
75 
77  unsigned size() const { return m_idChain.size(); }
78 
79 
81  std::vector<unsigned>::const_iterator begin() const { return m_idChain.begin(); }
82 
83 
85  std::vector<unsigned>::const_iterator end() const { return m_idChain.end(); }
86 
87 
89  bool checkSharesTrunk(const SubGraphID& b) const
90  {
91  unsigned chainsize = m_idChain.size();
92  if (b.size() != chainsize) { return false; }
93 
94  for (unsigned i = 0; i < chainsize - 1; i++) {
95  if (b.m_idChain[i] != m_idChain[i]) { return false; }
96  }
97 
98  return true;
99  }
100 
101 
103  std::string print() const
104  {
105  std::string out;
106  for (unsigned iD : m_idChain) {
107  out += FullSecID(iD).getFullSecString() + " ";
108  }
109  return out;
110  }
111 
112 
114  unsigned isElementOf(const SubGraphID& b) const
115  { return isElementOf(b.m_idChain); }
116 
117 
119  unsigned isElementOf(const std::vector<unsigned>& b) const
120  {
121  if (size() < b.size()) return 0;
122 
123  unsigned nIdentical = 0;
124  for (unsigned idA : m_idChain) {
125  for (unsigned idB : b) {
126  if (areTheSameSector(idA, idB)) nIdentical++;
127  }
128  }
129  return nIdentical;
130  }
131 
132 
134  bool hasElement(unsigned b) const
135  {
136  for (unsigned idA : m_idChain) {
137  if (areTheSameSector(idA, b)) return true;
138  }
139  return false;
140  }
141 
142 
144  bool areTheSameSectors(const SubGraphID& b) const
145  {
146  if (size() != b.size()) return false;
147  for (unsigned i = 0; i < size(); i++) {
148  if (areTheSameSector(m_idChain[i], b.m_idChain[i]) == false) return false;
149  }
150  return true;
151  }
152 
153 
155  std::vector<unsigned> hasSharedLayer() const
156  {
157  B2DEBUG(1, "hasSharedLayer-start with iD " << print() << ":");
158  std::vector<unsigned> outerOnes;
159  for (unsigned i = 0; i < size() - 1; i++) { // i: outer iD i+1: inner iD
160  B2DEBUG(1, "found layerIDs for outer/inner: " << FullSecID(m_idChain[i]).getLayerID() << "/" << FullSecID(
161  m_idChain[i + 1]).getLayerID());
162  if (FullSecID(m_idChain[i]).getLayerID() == FullSecID(m_idChain[i + 1]).getLayerID()) {
163  outerOnes.push_back(m_idChain[i]);
164  B2DEBUG(1, "hasSharedLayer good combi found with inner: " << FullSecID(m_idChain[i + 1]).getFullSecString() << ", storing outer: "
165  << FullSecID(m_idChain[i]).getFullSecString());
166  }
167  }
168  return outerOnes;
169  }
170 
171 
173  bool updateID(unsigned newID)
174  {
175  for (unsigned& secID : m_idChain) {
176  if (areTheSameSector(newID, secID) == false) continue;
177  FullSecID tempID(secID);
178  secID = FullSecID(tempID.getVxdID(), true, tempID.getSecID()).getFullSecID();
179  return true;
180  }
181 
182  return false;
183  }
184 
185 
187  std::vector<FullSecID> getFullSecIDs() const
188  {
189  std::vector<FullSecID> ids;
190 
191  for (auto& id : m_idChain) {
192  ids.push_back(FullSecID(id));
193  }
194  return ids;
195  }
196  }; // class SubGraphID
197 
198 
200 } // namespace Belle2
201 
202 namespace std {
204  template <> struct hash<Belle2::SubGraphID> {
209  std::size_t operator()(const Belle2::SubGraphID& graphID) const
210  {
211  std::size_t hashVal = 0;
212  for (unsigned iD : graphID) {
213  hashVal = std::hash<unsigned>()(iD) ^ hashVal;
214  ++hashVal;
215  }
216  return hashVal;
217  }
218  };
219 }
220 
Belle2::SubGraphID::isElementOf
unsigned isElementOf(const SubGraphID &b) const
checks if given sectorPack ( >= 1 sector) is part of this, ignores subLayerID.
Definition: SubGraphID.h:122
Belle2::SubGraphID::end
std::vector< unsigned >::const_iterator end() const
returns last entry of IDchain.
Definition: SubGraphID.h:93
Belle2::SubGraphID::SubGraphID
SubGraphID(const std::vector< unsigned > &idChain)
constructor, mandatory iDChain musst at least contain one iD.
Definition: SubGraphID.h:78
Belle2::SubGraphID::hasElement
bool hasElement(unsigned b) const
checks if given raw secID is part of this, ignores subLayerID.
Definition: SubGraphID.h:142
std::hash< Belle2::SubGraphID >::operator()
std::size_t operator()(const Belle2::SubGraphID &graphID) const
Create Hash value from graphID.
Definition: SubGraphID.h:209
Belle2::SubGraphID::updateID
bool updateID(unsigned newID)
if newID is element of this (ignoring subLayerID), oldID will be replaced by newID....
Definition: SubGraphID.h:181
Belle2::SubGraphID::hasSharedLayer
std::vector< unsigned > hasSharedLayer() const
returns raw IDs of entries being outer friend of sectors on same layer.
Definition: SubGraphID.h:163
Belle2::SubGraphID::checkSharesTrunk
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:97
Belle2::SubGraphID::operator==
bool operator==(const SubGraphID &b) const
overloaded '=='-operator
Definition: SubGraphID.h:50
Belle2::SubGraphID::areTheSameSectors
bool areTheSameSectors(const SubGraphID &b) const
checks if sectors are identical (while ignoring the subLayerID)
Definition: SubGraphID.h:152
Belle2::SubGraphID::m_idChain
std::vector< unsigned > m_idChain
ids of this SubGraphID.
Definition: SubGraphID.h:36
Belle2::FullSecID
Class to identify a sector inside of the VXD.
Definition: FullSecID.h:43
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SubGraphID::getFullSecIDs
std::vector< FullSecID > getFullSecIDs() const
returns SecIDs coded as FullSecIDs:
Definition: SubGraphID.h:195
Belle2::SubGraphID::size
unsigned size() const
returns chainLength of this id.
Definition: SubGraphID.h:85
Belle2::SubGraphID::areTheSameSector
bool areTheSameSector(unsigned a, unsigned b) const
checks if two given ids are the same sector (while ignoring the sublayerID).
Definition: SubGraphID.h:40
Belle2::SubGraphID::operator<
bool operator<(const SubGraphID &b) const
overloaded '<'-operator for sorting algorithms
Definition: SubGraphID.h:64
Belle2::FullSecID::getFullSecString
std::string getFullSecString() const
returns the FullSecID coded as string compatible to secIDs stored in the xml-sectormaps
Definition: FullSecID.cc:119
Belle2::SubGraphID::print
std::string print() const
returns string of entries.
Definition: SubGraphID.h:111
Belle2::SubGraphID
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
Definition: SubGraphID.h:34
Belle2::SubGraphID::begin
std::vector< unsigned >::const_iterator begin() const
returns first entry of IDchain.
Definition: SubGraphID.h:89