CMS 3D CMS Logo

SeedFinder.h
Go to the documentation of this file.
1 #ifndef FASTSIMULATION_TRACKING_SEEDFINDER_H
2 #define FASTSIMULATION_TRACKING_SEEDFINDER_H
3 
4 // system
5 #include <vector>
6 #include <functional>
7 #include <array>
8 
9 // fastsim tracking
13 
14 class TrackerTopology;
15 class FastTrackerRecHit;
16 
17 
18 
20 {
21 
22 public:
23 private:
24  std::vector<std::vector<SeedFinderSelector*> > _selectorFunctionsByHits;
27 
28 public:
29  SeedFinder(const SeedingTree<TrackingLayer>& seedingTree,const TrackerTopology & trackerTopology):
30  _seedingTree(seedingTree),
31  _trackerTopology(&trackerTopology)
32  {
33  }
34 
36  {
37  if (_selectorFunctionsByHits.size()<nHits)
38  {
39  _selectorFunctionsByHits.resize(nHits);
40  _selectorFunctionsByHits.reserve(nHits);
41  }
42  //shift indices by -1 so that _selectorFunctionsByHits[0] tests 1 hit
43  _selectorFunctionsByHits[nHits-1].push_back(seedFinderSelector);
44  }
45 
46  std::vector<unsigned int> getSeed(const std::vector<const FastTrackerRecHit *>& trackerRecHits) const
47  {
48  std::vector<int> hitIndicesInTree(_seedingTree.numberOfNodes(),-1);
49  //A SeedingNode is associated by its index to this list. The list stores the indices of the hits in 'trackerRecHits'
50  /* example
51  SeedingNode | hit index | hit
52  -------------------------------------------------------------------------------
53  index= 0: [BPix1] | hitIndicesInTree[0] (=1) | trackerRecHits[1]
54  index= 1: -- [BPix2] | hitIndicesInTree[1] (=3) | trackerRecHits[3]
55  index= 2: -- -- [BPix3] | hitIndicesInTree[2] (=4) | trackerRecHits[4]
56  index= 3: -- -- [FPix1_pos] | hitIndicesInTree[3] (=6) | trackerRecHits[6]
57  index= 4: -- -- [FPix1_neg] | hitIndicesInTree[4] (=7) | trackerRecHits[7]
58 
59  The implementation has been chosen such that the tree only needs to be build once upon construction.
60  */
61  std::vector<TrajectorySeedHitCandidate> seedHitCandidates;
62  for(const FastTrackerRecHit * trackerRecHit : trackerRecHits)
63  {
64  TrajectorySeedHitCandidate seedHitCandidate(trackerRecHit,_trackerTopology);
65  seedHitCandidates.push_back(std::move(seedHitCandidate));
66  }
67  return iterateHits(0,seedHitCandidates,hitIndicesInTree,true);
68 
69  //TODO: create pairs of TrackingLayer -> remove TrajectorySeedHitCandidate class
70  }
71 
72  //this method attempts to insert the hit at position 'trackerRecHit' in 'trackerRecHits'
73  //into the seeding tree (stored as 'hitIndicesInTree')
75  const std::vector<TrajectorySeedHitCandidate>& trackerRecHits,
76  std::vector<int>& hitIndicesInTree,
77  const SeedingNode<TrackingLayer>* node, unsigned int trackerHit) const
78  {
79  if (!node->getParent() || hitIndicesInTree[node->getParent()->getIndex()]>=0)
80  {
81  if (hitIndicesInTree[node->getIndex()]<0)
82  {
83  const TrajectorySeedHitCandidate& currentTrackerHit = trackerRecHits[trackerHit];
84  if (currentTrackerHit.getTrackingLayer()!=node->getData())
85  {
86  return nullptr;
87  }
88 
89  const unsigned int NHits = node->getDepth()+1;
90  if (_selectorFunctionsByHits.size()>=NHits)
91  {
92  //are there any selector functions stored for NHits?
93  if (!_selectorFunctionsByHits[NHits-1].empty())
94  {
95  //fill vector of Hits from node to root to be passed to the selector function
96  std::vector<const FastTrackerRecHit*> seedCandidateHitList(node->getDepth()+1);
97  seedCandidateHitList[node->getDepth()]=currentTrackerHit.hit();
98  const SeedingNode<TrackingLayer>* parentNode = node->getParent();
99  while (parentNode!=nullptr)
100  {
101  seedCandidateHitList[parentNode->getDepth()]=trackerRecHits[hitIndicesInTree[parentNode->getIndex()]].hit();
102  parentNode = parentNode->getParent();
103  }
104 
105  //loop over selector functions
106  for (SeedFinderSelector * selectorFunction: _selectorFunctionsByHits[NHits-1])
107  {
108  if (!selectorFunction->pass(seedCandidateHitList))
109  {
110  return nullptr;
111  }
112  }
113  }
114  }
115 
116  //the hit was not rejected by all selector functions -> insert it into the tree
117  hitIndicesInTree[node->getIndex()]=trackerHit;
118  if (node->getChildrenSize()==0)
119  {
120  return node;
121  }
122 
123  return nullptr;
124  }
125  else
126  {
127  for (unsigned int ichild = 0; ichild<node->getChildrenSize(); ++ichild)
128  {
129  const SeedingNode<TrackingLayer>* seed = insertHit(trackerRecHits,hitIndicesInTree,node->getChild(ichild),trackerHit);
130  if (seed)
131  {
132  return seed;
133  }
134  }
135  }
136  }
137  return nullptr;
138  }
139 
140  std::vector<unsigned int> iterateHits(
141  unsigned int start,
142  const std::vector<TrajectorySeedHitCandidate>& trackerRecHits,
143  std::vector<int> hitIndicesInTree,
144  bool processSkippedHits) const
145  {
146  for (unsigned int irecHit = start; irecHit<trackerRecHits.size(); ++irecHit)
147  {
148 
149  // only accept hits that are on one of the requested layers
150  if(_seedingTree.getSingleSet().find(trackerRecHits[irecHit].getTrackingLayer())==_seedingTree.getSingleSet().end())
151  {
152  continue;
153  }
154 
155  unsigned int currentHitIndex=irecHit;
156 
157  for (unsigned int inext=currentHitIndex+1; inext< trackerRecHits.size(); ++inext)
158  {
159  //if multiple hits are on the same layer -> follow all possibilities by recusion
160  if (trackerRecHits[currentHitIndex].getTrackingLayer()==trackerRecHits[inext].getTrackingLayer())
161  {
162  if (processSkippedHits)
163  {
164  //recusively call the method again with hit 'inext' but skip all following on the same layer though 'processSkippedHits=false'
165  std::vector<unsigned int> seedHits = iterateHits(
166  inext,
167  trackerRecHits,
168  hitIndicesInTree,
169  false
170  );
171  if (!seedHits.empty())
172  {
173  return seedHits;
174  }
175  }
176  irecHit+=1;
177  }
178  else
179  {
180  break;
181  }
182  }
183 
184  //processSkippedHits=true
185 
186  const SeedingNode<TrackingLayer>* seedNode = nullptr;
187  for (unsigned int iroot=0; seedNode==nullptr && iroot<_seedingTree.numberOfRoots(); ++iroot)
188  {
189  seedNode=insertHit(trackerRecHits,hitIndicesInTree,_seedingTree.getRoot(iroot), currentHitIndex);
190  }
191  if (seedNode)
192  {
193  std::vector<unsigned int> seedIndices(seedNode->getDepth()+1);
194  while (seedNode)
195  {
196  seedIndices[seedNode->getDepth()]=hitIndicesInTree[seedNode->getIndex()];
197  seedNode=seedNode->getParent();
198  }
199  return seedIndices;
200  }
201 
202  }
203 
204  return std::vector<unsigned int>();
205  }
206 };
207 
208 #endif
209 
Definition: start.py:1
const SingleSet & getSingleSet() const
Definition: SeedingTree.h:195
const DATA & getData() const
Definition: SeedingTree.h:133
unsigned int getChildrenSize() const
Definition: SeedingTree.h:118
const SeedingNode< TrackingLayer > * insertHit(const std::vector< TrajectorySeedHitCandidate > &trackerRecHits, std::vector< int > &hitIndicesInTree, const SeedingNode< TrackingLayer > *node, unsigned int trackerHit) const
Definition: SeedFinder.h:74
unsigned int getDepth() const
Definition: SeedingTree.h:81
const SeedingNode * getParent() const
Definition: SeedingTree.h:109
unsigned int numberOfNodes() const
Definition: SeedingTree.h:218
unsigned int numberOfRoots() const
Definition: SeedingTree.h:213
std::vector< unsigned int > getSeed(const std::vector< const FastTrackerRecHit * > &trackerRecHits) const
Definition: SeedFinder.h:46
SeedFinder(const SeedingTree< TrackingLayer > &seedingTree, const TrackerTopology &trackerTopology)
Definition: SeedFinder.h:29
std::vector< unsigned int > iterateHits(unsigned int start, const std::vector< TrajectorySeedHitCandidate > &trackerRecHits, std::vector< int > hitIndicesInTree, bool processSkippedHits) const
Definition: SeedFinder.h:140
const SeedingTree< TrackingLayer > & _seedingTree
Definition: SeedFinder.h:25
void addHitSelector(SeedFinderSelector *seedFinderSelector, unsigned int nHits)
Definition: SeedFinder.h:35
const SeedingNode< DATA > * getRoot(unsigned int i) const
Definition: SeedingTree.h:223
const FastTrackerRecHit * hit() const
The Hit itself.
unsigned int getIndex() const
Definition: SeedingTree.h:104
const TrackingLayer & getTrackingLayer() const
def move(src, dest)
Definition: eostools.py:511
const SeedingNode< DATA > * getChild(unsigned int ichild) const
Definition: SeedingTree.h:123
const TrackerTopology * _trackerTopology
Definition: SeedFinder.h:26
std::vector< std::vector< SeedFinderSelector * > > _selectorFunctionsByHits
Definition: SeedFinder.h:24