CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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>
11 {
12  protected:
13  const DATA _data;
14  const std::vector<SeedingNode<DATA>*>& _allNodes;
15  unsigned int _index; //the index in allNodes
16  int _parentIndex; //the parent's index in allNodes
17  int _childIndex; //the index of this Node in its parent's children vector
18  unsigned int _depth; //the depth within the tree (for root: depth=0 && parentIndex=-1
19  std::vector<unsigned int> _children;
20 
21  public:
22  SeedingNode(const DATA& data, std::vector<SeedingNode*>& allNodes, int parentIndex=-1):
23  _data(data),
24  _allNodes(allNodes),
25  _index(allNodes.size()),
26  _parentIndex(parentIndex)
27  {
28  allNodes.push_back(this);
29 
30  if (_parentIndex>=0)
31  {
32  SeedingNode* parent = allNodes[_parentIndex];
33  _depth=parent->_depth+1;
34  _childIndex=parent->_children.size();
35  parent->_children.push_back(_index);
36 
37  }
38  else
39  {
40  _depth=0;
41  _childIndex=-1;
42  }
43  }
44 
45  void sort(std::vector<SeedingNode<DATA>*>& allNodes,unsigned int parentIndex)
46  {
47  _parentIndex=parentIndex;
48  _index=allNodes.size();
49  if (_parentIndex>=0)
50  {
51  allNodes[_parentIndex]->_children[_childIndex]=_index;
52  }
53  allNodes.push_back(this);
54  for (unsigned int ichild=0; ichild<_children.size();++ichild)
55  {
56  _allNodes[_children[ichild]]->sort(allNodes,_index);
57  }
58  }
59 
60  bool insert(const std::vector<DATA>& dataList, std::vector<SeedingNode<DATA>*>& allNodes)
61  {
62  if (_depth+1>=dataList.size())
63  {
64  //std::cout<<"\tSeedingNode::insert: return"<<std::endl;
65  return false;
66  }
67  for (unsigned int ichild=0; ichild<_children.size();++ichild)
68  {
69  if (allNodes[_children[ichild]]->getData()==dataList[_depth+1])
70  {
71  //std::cout<<"\tSeedingNode::insert: has child "<<dataList[_depth+1].print()<<std::endl;
72  return allNodes[_children[ichild]]->insert(dataList,allNodes);
73  }
74  }
75  //std::cout<<"\tSeedingNode::insert: create child "<<dataList[_depth+1].print()<<std::endl;
76  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[_depth+1],allNodes,_index);
77  if (node->getDepth()+1>=dataList.size())
78  {
79  return true;
80  }
81  return node->insert(dataList,allNodes);
82  }
83 
84  inline unsigned int getDepth() const
85  {
86  return _depth;
87  }
88 
89  inline const SeedingNode<DATA>* next() const
90  {
91  if (_index+1<_allNodes.size())
92  {
93  return _allNodes[_index+1];
94  }
95  return nullptr;
96  }
97 
98  inline const SeedingNode<DATA>* firstChild() const
99  {
100  if (_children.size()>0)
101  {
102  return _allNodes[_children[0]];
103  }
104  return nullptr;
105  }
106 
107  inline unsigned int getIndex() const
108  {
109  return _index;
110  }
111 
112  inline const SeedingNode* getParent() const
113  {
114  if (_parentIndex>=0)
115  {
116  return _allNodes[_parentIndex];
117  }
118  return nullptr;
119  }
120 
121  inline unsigned int getChildrenSize() const
122  {
123  return _children.size();
124  }
125 
126  inline const SeedingNode<DATA>* getChild(unsigned int ichild) const
127  {
128  return _allNodes[_children[ichild]];
129  }
130 
131  inline unsigned int getChildIndex() const
132  {
133  return _childIndex;
134  }
135 
136  inline const DATA& getData() const
137  {
138  return _data;
139  }
140 
141  void print() const
142  {
143 
144  printf("index=%3i, depth=%2i, childIndex=%2i: ",_index,_depth,_childIndex);
145  for (unsigned int i=0;i<_depth;++i)
146  {
147  std::cout<<" -- ";
148  }
149  printf("[%s, %s] \r\n",_data.toString().c_str(),_data.toIdString().c_str());
150 
151  }
152  void printRecursive() const
153  {
154  print();
155  for (unsigned int ichild=0; ichild<_children.size(); ++ichild)
156  {
157  _allNodes[_children[ichild]]->printRecursive();
158  }
159  }
160 };
161 
162 template<class DATA>
164 {
165  public:
166  typedef std::unordered_set<DATA,typename DATA:: hashfct, typename DATA:: eqfct> SingleSet;
167  protected:
168  std::vector<SeedingNode<DATA>*> _roots;
169  std::vector<SeedingNode<DATA>*> _allNodes;
170 
172  public:
174  {
175  }
176 
177  //returns true if successfully inserted into tree
178  bool insert(const std::vector<DATA>& dataList)
179  {
180  for (unsigned int i = 0; i< dataList.size(); ++i)
181  {
182  _singleSet.insert(dataList[i]);
183  }
184 
185  if (dataList.size()==0)
186  {
187  return false;
188  }
189  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
190  {
191  if (_roots[iroot]->getData()==dataList[0])
192  {
193  //std::cout<<"\tfound root: "<<dataList[0].print()<<std::endl;
194  return _roots[iroot]->insert(dataList,_allNodes);
195  }
196  }
197  //std::cout<<"\tnew root: "<<dataList[0].print()<<std::endl;
198  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[0],_allNodes);
199  _roots.push_back(node);
200  return node->insert(dataList,_allNodes);
201  }
202 
203  inline const SingleSet& getSingleSet() const
204  {
205  return _singleSet;
206  }
207 
208  void sort()
209  {
210  //this setups depth first ordered indexes.
211  std::vector<SeedingNode<DATA>*> allNodes;
212  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
213  {
214  _roots[iroot]->sort(allNodes,-1);
215  }
216  _allNodes=allNodes;
217  }
218 
219 
220 
221  inline unsigned int numberOfRoots() const
222  {
223  return _roots.size();
224  }
225 
226  inline unsigned int numberOfNodes() const
227  {
228  return _allNodes.size();
229  }
230 
231  inline const SeedingNode<DATA>* getRoot(unsigned int i) const
232  {
233  if (i<_roots.size())
234  {
235  return _roots[i];
236  }
237  return nullptr;
238  }
239 
240  void printRecursive() const
241  {
242  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [recursive]"<<std::endl;
243  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
244  {
245  _roots[iroot]->printRecursive();
246  }
247  }
248  void printOrdered() const
249  {
250  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [ordered]"<<std::endl;
251  for (unsigned int inode=0; inode<_allNodes.size();++inode)
252  {
253  _allNodes[inode]->print();
254  }
255  }
256 
257  void print() const
258  {
259  std::cout<<"SeedingTree: n="<<_allNodes.size()<<std::endl;
260  for (unsigned int inode=0; inode<_allNodes.size();++inode)
261  {
262  _allNodes[inode]->print();
263  }
264  }
265 
267  {
268  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
269  {
270  delete _roots[iroot];
271  }
272  _roots.clear();
273  }
274 };
275 
276 #endif
277 
int i
Definition: DBlmapReader.cc:9
int _childIndex
Definition: SeedingTree.h:17
const SeedingNode< DATA > * next() const
Definition: SeedingTree.h:89
const SingleSet & getSingleSet() const
Definition: SeedingTree.h:203
void printRecursive() const
Definition: SeedingTree.h:240
const SeedingNode< DATA > * firstChild() const
Definition: SeedingTree.h:98
SeedingNode(const DATA &data, std::vector< SeedingNode * > &allNodes, int parentIndex=-1)
Definition: SeedingTree.h:22
const DATA & getData() const
Definition: SeedingTree.h:136
unsigned int getChildrenSize() const
Definition: SeedingTree.h:121
unsigned int getDepth() const
Definition: SeedingTree.h:84
const SeedingNode * getParent() const
Definition: SeedingTree.h:112
std::vector< SeedingNode< DATA > * > _roots
Definition: SeedingTree.h:168
bool insert(const std::vector< DATA > &dataList)
Definition: SeedingTree.h:178
unsigned int numberOfNodes() const
Definition: SeedingTree.h:226
void sort()
Definition: SeedingTree.h:208
unsigned int numberOfRoots() const
Definition: SeedingTree.h:221
std::unordered_set< DATA, typename DATA::hashfct, typename DATA::eqfct > SingleSet
Definition: SeedingTree.h:166
void sort(std::vector< SeedingNode< DATA > * > &allNodes, unsigned int parentIndex)
Definition: SeedingTree.h:45
void printRecursive() const
Definition: SeedingTree.h:152
int _parentIndex
Definition: SeedingTree.h:16
unsigned int _depth
Definition: SeedingTree.h:18
const DATA _data
Definition: SeedingTree.h:13
bool insert(const std::vector< DATA > &dataList, std::vector< SeedingNode< DATA > * > &allNodes)
Definition: SeedingTree.h:60
std::vector< unsigned int > _children
Definition: SeedingTree.h:19
std::vector< SeedingNode< DATA > * > _allNodes
Definition: SeedingTree.h:169
void print() const
Definition: SeedingTree.h:257
void print() const
Definition: SeedingTree.h:141
const SeedingNode< DATA > * getRoot(unsigned int i) const
Definition: SeedingTree.h:231
void printOrdered() const
Definition: SeedingTree.h:248
const std::vector< SeedingNode< DATA > * > & _allNodes
Definition: SeedingTree.h:14
tuple cout
Definition: gather_cfg.py:145
unsigned int getIndex() const
Definition: SeedingTree.h:107
SingleSet _singleSet
Definition: SeedingTree.h:171
tuple size
Write out results.
unsigned int getChildIndex() const
Definition: SeedingTree.h:131
const SeedingNode< DATA > * getChild(unsigned int ichild) const
Definition: SeedingTree.h:126
unsigned int _index
Definition: SeedingTree.h:15