CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SeedingTree.h
Go to the documentation of this file.
1 #ifndef FastSimulation_Tracking_SeedingTree_h
2 #define FastSimulation_Tracking_SeedingTree_h
3 
4 #include <vector>
5 #include <iostream>
6 #include <algorithm>
7 #include <unordered_set>
8 
9 template <class DATA>
10 class SeedingNode {
11 protected:
12  const DATA _data;
13  const std::vector<SeedingNode<DATA>*>& _allNodes;
14  unsigned int _index; //the index in allNodes
15  int _parentIndex; //the parent's index in allNodes
16  int _childIndex; //the index of this Node in its parent's children vector
17  unsigned int _depth; //the depth within the tree (for root: depth=0 && parentIndex=-1
18  std::vector<unsigned int> _children;
19 
20 public:
21  SeedingNode(const DATA& data, std::vector<SeedingNode*>& allNodes, int parentIndex = -1)
22  : _data(data), _allNodes(allNodes), _index(allNodes.size()), _parentIndex(parentIndex) {
23  allNodes.push_back(this);
24 
25  if (_parentIndex >= 0) {
26  SeedingNode* parent = allNodes[_parentIndex];
27  _depth = parent->_depth + 1;
28  _childIndex = parent->_children.size();
29  parent->_children.push_back(_index);
30 
31  } else {
32  _depth = 0;
33  _childIndex = -1;
34  }
35  }
36 
37  void sort(std::vector<SeedingNode<DATA>*>& allNodes, unsigned int parentIndex) {
38  _parentIndex = parentIndex;
39  _index = allNodes.size();
40  if (_parentIndex >= 0) {
41  allNodes[_parentIndex]->_children[_childIndex] = _index;
42  }
43  allNodes.push_back(this);
44  for (unsigned int ichild = 0; ichild < _children.size(); ++ichild) {
45  _allNodes[_children[ichild]]->sort(allNodes, _index);
46  }
47  }
48 
49  bool insert(const std::vector<DATA>& dataList, std::vector<SeedingNode<DATA>*>& allNodes) {
50  if (_depth + 1 >= dataList.size()) {
51  return false;
52  }
53  for (unsigned int ichild = 0; ichild < _children.size(); ++ichild) {
54  if (allNodes[_children[ichild]]->getData() == dataList[_depth + 1]) {
55  return allNodes[_children[ichild]]->insert(dataList, allNodes);
56  }
57  }
58  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[_depth + 1], allNodes, _index);
59  if (node->getDepth() + 1 >= dataList.size()) {
60  return true;
61  }
62  return node->insert(dataList, allNodes);
63  }
64 
65  inline unsigned int getDepth() const { return _depth; }
66 
67  inline const SeedingNode<DATA>* next() const {
68  if (_index + 1 < _allNodes.size()) {
69  return _allNodes[_index + 1];
70  }
71  return nullptr;
72  }
73 
74  inline const SeedingNode<DATA>* firstChild() const {
75  if (!_children.empty()) {
76  return _allNodes[_children[0]];
77  }
78  return nullptr;
79  }
80 
81  inline unsigned int getIndex() const { return _index; }
82 
83  inline const SeedingNode* getParent() const {
84  if (_parentIndex >= 0) {
85  return _allNodes[_parentIndex];
86  }
87  return nullptr;
88  }
89 
90  inline unsigned int getChildrenSize() const { return _children.size(); }
91 
92  inline const SeedingNode<DATA>* getChild(unsigned int ichild) const { return _allNodes[_children[ichild]]; }
93 
94  inline unsigned int getChildIndex() const { return _childIndex; }
95 
96  inline const DATA& getData() const { return _data; }
97 
98  void print() const {
99  printf("index=%3i, depth=%2i, childIndex=%2i: ", _index, _depth, _childIndex);
100  for (unsigned int i = 0; i < _depth; ++i) {
101  std::cout << " -- ";
102  }
103  printf("[%s, %s] \r\n", _data.toString().c_str(), _data.toIdString().c_str());
104  }
105  void printRecursive() const {
106  print();
107  for (unsigned int ichild = 0; ichild < _children.size(); ++ichild) {
108  _allNodes[_children[ichild]]->printRecursive();
109  }
110  }
111 };
112 
113 template <class DATA>
114 class SeedingTree {
115 public:
116  typedef std::unordered_set<DATA, typename DATA::hashfct, typename DATA::eqfct> SingleSet;
117 
118 protected:
119  std::vector<SeedingNode<DATA>*> _roots;
120  std::vector<SeedingNode<DATA>*> _allNodes;
121 
123 
124 public:
125  //returns true if successfully inserted into tree
126  bool insert(const std::vector<DATA>& dataList) {
127  for (unsigned int i = 0; i < dataList.size(); ++i) {
128  _singleSet.insert(dataList[i]);
129  }
130 
131  if (dataList.empty()) {
132  return false;
133  }
134  for (unsigned int iroot = 0; iroot < _roots.size(); ++iroot) {
135  if (_roots[iroot]->getData() == dataList[0]) {
136  return _roots[iroot]->insert(dataList, _allNodes);
137  }
138  }
139  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[0], _allNodes);
140  _roots.push_back(node);
141  return node->insert(dataList, _allNodes);
142  }
143 
144  inline const SingleSet& getSingleSet() const { return _singleSet; }
145 
146  void sort() {
147  //this setups depth first ordered indexes.
148  std::vector<SeedingNode<DATA>*> allNodes;
149  for (unsigned int iroot = 0; iroot < _roots.size(); ++iroot) {
150  _roots[iroot]->sort(allNodes, -1);
151  }
152  _allNodes = allNodes;
153  }
154 
155  inline unsigned int numberOfRoots() const { return _roots.size(); }
156 
157  inline unsigned int numberOfNodes() const { return _allNodes.size(); }
158 
159  inline const SeedingNode<DATA>* getRoot(unsigned int i) const {
160  if (i < _roots.size()) {
161  return _roots[i];
162  }
163  return nullptr;
164  }
165 
166  void printRecursive() const {
167  std::cout << "SeedingTree: n=" << _allNodes.size() << " [recursive]" << std::endl;
168  for (unsigned int iroot = 0; iroot < _roots.size(); ++iroot) {
169  _roots[iroot]->printRecursive();
170  }
171  }
172  void printOrdered() const {
173  std::cout << "SeedingTree: n=" << _allNodes.size() << " [ordered]" << std::endl;
174  for (unsigned int inode = 0; inode < _allNodes.size(); ++inode) {
175  _allNodes[inode]->print();
176  }
177  }
178 
179  void print() const {
180  std::cout << "SeedingTree: n=" << _allNodes.size() << std::endl;
181  for (unsigned int inode = 0; inode < _allNodes.size(); ++inode) {
182  _allNodes[inode]->print();
183  }
184  }
185 
187  for (unsigned int iroot = 0; iroot < _roots.size(); ++iroot) {
188  delete _roots[iroot];
189  }
190  _roots.clear();
191  }
192 };
193 
194 #endif
int _childIndex
Definition: SeedingTree.h:16
const SeedingNode< DATA > * next() const
Definition: SeedingTree.h:67
const SingleSet & getSingleSet() const
Definition: SeedingTree.h:144
void printRecursive() const
Definition: SeedingTree.h:166
const SeedingNode< DATA > * firstChild() const
Definition: SeedingTree.h:74
SeedingNode(const DATA &data, std::vector< SeedingNode * > &allNodes, int parentIndex=-1)
Definition: SeedingTree.h:21
const DATA & getData() const
Definition: SeedingTree.h:96
unsigned int getChildrenSize() const
Definition: SeedingTree.h:90
unsigned int getDepth() const
Definition: SeedingTree.h:65
const SeedingNode * getParent() const
Definition: SeedingTree.h:83
std::vector< SeedingNode< DATA > * > _roots
Definition: SeedingTree.h:119
bool insert(const std::vector< DATA > &dataList)
Definition: SeedingTree.h:126
unsigned int numberOfNodes() const
Definition: SeedingTree.h:157
void sort()
Definition: SeedingTree.h:146
unsigned int numberOfRoots() const
Definition: SeedingTree.h:155
printf("params %d %f %f %f\n", minT, eps, errmax, chi2max)
void sort(std::vector< SeedingNode< DATA > * > &allNodes, unsigned int parentIndex)
Definition: SeedingTree.h:37
void printRecursive() const
Definition: SeedingTree.h:105
int _parentIndex
Definition: SeedingTree.h:15
std::unordered_set< DATA, typename DATA::hashfct, typename DATA::eqfct > SingleSet
Definition: SeedingTree.h:116
unsigned int _depth
Definition: SeedingTree.h:17
const DATA _data
Definition: SeedingTree.h:12
bool insert(const std::vector< DATA > &dataList, std::vector< SeedingNode< DATA > * > &allNodes)
Definition: SeedingTree.h:49
std::vector< unsigned int > _children
Definition: SeedingTree.h:18
std::vector< SeedingNode< DATA > * > _allNodes
Definition: SeedingTree.h:120
void print() const
Definition: SeedingTree.h:179
void print() const
Definition: SeedingTree.h:98
const SeedingNode< DATA > * getRoot(unsigned int i) const
Definition: SeedingTree.h:159
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
void printOrdered() const
Definition: SeedingTree.h:172
const std::vector< SeedingNode< DATA > * > & _allNodes
Definition: SeedingTree.h:13
tuple cout
Definition: gather_cfg.py:144
unsigned int getIndex() const
Definition: SeedingTree.h:81
SingleSet _singleSet
Definition: SeedingTree.h:122
tuple size
Write out results.
unsigned int getChildIndex() const
Definition: SeedingTree.h:94
const SeedingNode< DATA > * getChild(unsigned int ichild) const
Definition: SeedingTree.h:92
unsigned int _index
Definition: SeedingTree.h:14