LCFIVertex  0.7.2
candidatevertex.cpp
1 
2 #include "../include/candidatevertex.h"
3 
4 #include "../include/vertexfitter.h"
5 #include "../include/vertexfitterlsm.h"
6 #include "../include/vertexresolver.h"
7 #include "../include/vertexresolverequalsteps.h"
8 #include "../include/vertexfuncmaxfinder.h"
9 #include "../include/vertexfuncmaxfinderclassicstepper.h"
10 #include "../../inc/trackstate.h"
11 #include "../include/vertexfunction.h"
12 #include "../../util/inc/util.h"
13 #include "../../util/inc/memorymanager.h"
14 
15 #include <algorithm>
16 
18 
19 namespace vertex_lcfi
20 {
21 namespace ZVTOP
22 {
23 VertexFitter* CandidateVertex::_FallbackFitter;
24 VertexResolver* CandidateVertex::_FallbackResolver;
25 VertexFuncMaxFinder* CandidateVertex::_FallbackMaxFinder;
26 
27 //Construct from tracks and vertex function
28 CandidateVertex::CandidateVertex(const std::vector<TrackState*>& Tracks, VertexFunction* VertexFunction, VertexFitter* Fitter, VertexResolver* Resolver, VertexFuncMaxFinder* MaxFinder)
29  : _Fitter(Fitter),_Resolver(Resolver),_MaxFinder(MaxFinder),_IP(0),_TrackStates(Tracks),_VertexFunction(VertexFunction),_VertexFuncMaxIsValid(0),_FitIsValid(0),_ErrorOfFitIsValid(0)
30 {/*NO OP*/}
31 
32 //Construct from tracks,ip and vertex function
33 CandidateVertex::CandidateVertex(const std::vector<TrackState*>& Tracks, InteractionPoint* IP,VertexFunction* VertexFunction, VertexFitter* Fitter, VertexResolver* Resolver, VertexFuncMaxFinder* MaxFinder)
34  : _Fitter(Fitter),_Resolver(Resolver),_MaxFinder(MaxFinder),_IP(IP),_TrackStates(Tracks),_VertexFunction(VertexFunction),_VertexFuncMaxIsValid(0),_FitIsValid(0),_ErrorOfFitIsValid(0)
35 {/*NO OP*/}
36 
37 CandidateVertex::CandidateVertex(const Vector3 & Position, const Matrix3x3 & PositionError, double ChiSquaredOfFit, std::map<TrackState*,double> ChiSquaredOfTrack, double ChiSquaredOfIP)
38  :_Position(Position),_PositionError(PositionError),_ChiSquaredOfFit(ChiSquaredOfFit),_ChiSquaredOfTrack(ChiSquaredOfTrack),_ChiSquaredOfIP(ChiSquaredOfIP),_FitIsValid(1),_ErrorOfFitIsValid(1)
39 {/*NO OP*/}
40 
41 CandidateVertex::CandidateVertex(const std::vector<CandidateVertex*> & Vertices, VertexFitter* Fitter, VertexResolver* Resolver, VertexFuncMaxFinder* MaxFinder)
42  : _Fitter(Fitter),_Resolver(Resolver),_MaxFinder(MaxFinder),_VertexFuncMaxIsValid(0),_FitIsValid(0),_ErrorOfFitIsValid(0)
43 {
44  if (Vertices.size()>0)
45  {
46  //Set IP to 0 to start
47  this->setIP(0);
48  //Get the tracks from all vertices
49  for (std::vector<CandidateVertex*>::const_iterator iCV = Vertices.begin();iCV != Vertices.end();++iCV)
50  {
51  for (std::vector<TrackState*>::const_iterator iTrack = (*iCV)->trackStateList().begin();iTrack != (*iCV)->trackStateList().end();++iTrack)
52  {
53  //Check we don't have the track already then add it
54  if (!this->hasTrack((*iTrack)->parentTrack()))
55  {
56  this->addTrackState(*iTrack);
57  }
58  }
59  //If the vertex has an IP copy the pointer over
60  if ((*iCV)->interactionPoint())
61  this->setIP((*iCV)->interactionPoint());
62  }
63  }
64 }
65 
67 {
68  std::vector<TrackState*>::iterator position = find(_TrackStates.begin(), _TrackStates.end(), TrackToRemove);
69  if (position!=_TrackStates.end()) //Found
70  {
71  _TrackStates.erase(position);
72  this->invalidateFit();
73  return 1;
74  }
75  else
76  return 0;
77 }
78 
79 bool CandidateVertex::removeTrack(Track * const TrackToRemove)
80 {
81  std::vector<TrackState*>::iterator iTrack;
82  for (iTrack = _TrackStates.begin();iTrack!=_TrackStates.end() && ((*iTrack)->parentTrack() != TrackToRemove);++iTrack)
83  ;
84  //iTrack now points to end or the track we want to remove
85  if (iTrack!=_TrackStates.end())
86  {
87  _TrackStates.erase(iTrack);
88  this->invalidateFit();
89  return 1;
90  }
91  else
92  return 0;
93 }
94 
96 {
97  _TrackStates.push_back(TrackToAdd);
98  this->invalidateFit();
99 }
100 
101 
103 {
104  if(_IP)
105  {
106  _IP=0;
107  this->invalidateFit();
108  return 1;
109  }
110  else
111  return 0;
112 }
113 
115 {
116  _IP=IP;
117  this->invalidateFit();
118 }
119 
121 {
122  //Check which func max is biggest and keep it.
123  if (this->vertexFuncMaxValue() < SourceVertex->vertexFuncMaxValue())
124  {
125  _VertexFuncMaxPosition = SourceVertex->vertexFuncMaxPosition();
126  _VertexFuncMaxValue = SourceVertex->vertexFuncMaxValue();
127  }
128 
129  if (!this->interactionPoint())
130  if (SourceVertex->interactionPoint())
131  this->setIP(SourceVertex->interactionPoint());
132 
133  std::vector<TrackState*> SourceList = SourceVertex->trackStateList();
134  for (std::vector<TrackState*>::iterator iSourceTrack = SourceList.begin();iSourceTrack != SourceList.end();++iSourceTrack)
135  {
136  this->removeTrack((*iSourceTrack)->parentTrack()); //Removing before we add ensures no duplicates.
137  this->addTrackState(*iSourceTrack);
138  }
139 }
140 
141 void CandidateVertex::claimTracksFrom(const std::list<CandidateVertex*> & LosingVertices)
142 {
143  //Loop over losing verts
144  for (std::list<CandidateVertex*>::const_iterator iLosingVertex = LosingVertices.begin();iLosingVertex != LosingVertices.end();++iLosingVertex)
145  {
146  //Only one IP so if we have it they can't
147  if (_IP)
148  (*iLosingVertex)->removeIP();
149  //Loop over my tracks, messaging to remove from losing vertex
150  for (std::vector<TrackState*>::iterator iTrackState = _TrackStates.begin();iTrackState != _TrackStates.end();++iTrackState)
151  {
152  (*iLosingVertex)->removeTrack((*iTrackState)->parentTrack());
153  }
154  }
155 }
156 
157 int CandidateVertex::trimByProb(const double ProbThreshold)
158 {
159  int NumRemoved = 0;
160  do
161  {
162  //TODO Adjust DOF for vertices containing IP?
163  double Prob = util::prob(this->chiSquaredOfFit() , /*DegreesOfFreedom*/ 2*(this->trackStateList().size())-3);
164 // std::cout << "chi " << this->chiSquaredOfFit() << std::endl;
165 // std::cout << "pos " << this->position() << std::endl;
166  if (Prob < ProbThreshold)
167  {
168  //Find Track with Highest Chi Squared
169  TrackState* HighTrack = 0;
170  double HighChiSquared = -1;
171  for (std::vector<TrackState*>::iterator iTrackState = _TrackStates.begin();iTrackState != _TrackStates.end();++iTrackState)
172  {
173  if (this->chiSquaredOfTrack(*iTrackState) > HighChiSquared) //Refit happens when chiSquaredOfTrackChecks
174  {
175  HighChiSquared = this->chiSquaredOfTrack(*iTrackState);
176  HighTrack = *iTrackState;
177  }
178  }
179  //std::cout << HighChiSquared << std::endl;
180  //std::cout << this->trackStateList().size() << std::endl;
181  this->removeTrackState(HighTrack);
182  ++NumRemoved;
183  }
184  // Otherwise we are below threshold
185  else
186  {
187  //std::cout << "Threshold " << Prob << std::endl;
188  break;
189  }
190  //If we have less than 2 tracks left then also quit
191  if (this->trackStateList().size() < 2)
192  {
193  //std::cout << "< 2 tracks" << std::endl;
194  break;
195  }
196  }
197  while (1);
198  return NumRemoved;
199 }
200 
201 int CandidateVertex::trimByChi2(const double Chi2Threshold)
202 {
203  int NumRemoved = 0;
204  do
205  {
206  //Find Track with Highest Chi Squared
207  TrackState* HighTrack = 0;
208  double HighChiSquared = -1;
209  for (std::vector<TrackState*>::iterator iTrackState = _TrackStates.begin();iTrackState != _TrackStates.end();++iTrackState)
210  {
211  if (this->chiSquaredOfTrack(*iTrackState) > HighChiSquared) //Refit happens when chiSquaredOfTrackChecks
212  {
213  HighChiSquared = this->chiSquaredOfTrack(*iTrackState);
214  HighTrack = *iTrackState;
215  }
216  }
217  //If this track is above threshold then remove it
218  if (HighChiSquared > Chi2Threshold)
219  {
220  this->removeTrackState(HighTrack);
221  ++NumRemoved;
222  }
223  // Otherwise we found nothing above threshold so quit checking
224  else
225  break;
226  //If we have no tracks left then also quit
227  if (HighTrack == 0)
228  break;
229  }
230  while (1);
231  return NumRemoved;
232 }
233 
234 int CandidateVertex::trimByChi2(const double Chi2Threshold, VertexFitter* Fitter)
235 {
236  int NumRemoved = 0;
237  do
238  {
239  //Refit now to make sure we have a fit that used the fitter specified, other wise chiSquaredOfTrack will invoke default!
240  this->refit(Fitter); //TODO CHECK FIT OK
241  //Find Track with Highest Chi Squared
242  //TODO This could be more fast/clever
243  TrackState* HighTrack = 0;
244  double HighChiSquared = -1;
245  for (std::vector<TrackState*>::iterator iTrackState = _TrackStates.begin();iTrackState != _TrackStates.end();++iTrackState)
246  {
247  if (this->chiSquaredOfTrack(*iTrackState) > HighChiSquared)
248  {
249  HighChiSquared = this->chiSquaredOfTrack(*iTrackState);
250  HighTrack = *iTrackState;
251  }
252  }
253  //If this track is above threshold then remove it
254  if (HighChiSquared > Chi2Threshold)
255  {
256  this->removeTrackState(HighTrack);
257  ++NumRemoved;
258  }
259  // Otherwise we found nothing above so quit checking
260  else
261  break;
262  //If we have no tracks left then also quit
263  if (HighTrack == 0)
264  break;
265  }
266  while (1);
267  return NumRemoved;
268 }
269 
270 
272 {
273  _FitIsValid=0;
274  _ErrorOfFitIsValid=0;
275  //this->invalidateFuncMax(); //Taken out as we always keep the first max or one that is larger that it is replaced with when clustering
276  //TODO Could make above optional on a per-vertex basis, but not currently needed.
277 }
278 
279 void CandidateVertex::refit(bool CalculateError) const
280 {
281  if (CalculateError)
282  {
283  _Fitter->fitVertex(this->trackStateList(), this->interactionPoint(),_Position,_ChiSquaredOfFit,_ChiSquaredOfTrack,_ChiSquaredOfIP);
284  }
285  else
286  {
287  _Fitter->fitVertex(this->trackStateList(), this->interactionPoint(),_Position,_PositionError,_ChiSquaredOfFit,_ChiSquaredOfTrack,_ChiSquaredOfIP);
288  }
289  _FitIsValid=1;
290  _ErrorOfFitIsValid=CalculateError;
291 }
292 
293 void CandidateVertex::refit(VertexFitter* Fitter,bool CalculateError) const
294 {
295  if (CalculateError)
296  {
297  Fitter->fitVertex(this->trackStateList(), this->interactionPoint(),_Position,_ChiSquaredOfFit,_ChiSquaredOfTrack,_ChiSquaredOfIP);
298  }
299  else
300  {
301  Fitter->fitVertex(this->trackStateList(), this->interactionPoint(),_Position,_PositionError,_ChiSquaredOfFit,_ChiSquaredOfTrack,_ChiSquaredOfIP);
302  }
303  _FitIsValid=1;
304  _ErrorOfFitIsValid=CalculateError;
305 }
306 
308 {
309  _VertexFuncMaxPosition = _MaxFinder->findNearestMaximum(this->position(), _VertexFunction);
310  _VertexFuncMaxValue = _VertexFunction->valueAt(_VertexFuncMaxPosition);
311  _VertexFuncMaxIsValid = 1;
312  return 1;
313 }
314 
316 {
317  //Todo null vertex pointer check
318  switch (Type)
319  {
320  case FittedPosition:
321  return _Resolver->areResolved(this->position(), Vertex->position(), _VertexFunction, Threshold);
322  break;
323  case NearestMaximum:
324  return _Resolver->areResolved(this->vertexFuncMaxPosition(), Vertex->vertexFuncMaxPosition(), _VertexFunction, Threshold);
325  break;
326  }
327  //TODO Throw as not supported
328  return 0;
329 }
330 
331 bool CandidateVertex::isResolvedFrom(CandidateVertex* const Vertex, const double Threshold, CandidateVertex::eResolveType Type, VertexResolver* Resolver) const
332 {
333  //Todo null vertex pointer check
334  switch (Type)
335  {
336  case FittedPosition:
337  return _Resolver->areResolved(this->position(), Vertex->position(), _VertexFunction, Threshold);
338  break;
339  case NearestMaximum:
340  return _Resolver->areResolved(this->vertexFuncMaxPosition(), Vertex->vertexFuncMaxPosition(), _VertexFunction, Threshold);
341  break;
342  }
343  //TODO Throw as not supported
344  return 0;
345 }
346 
348 {
349  for (std::vector<TrackState*>::const_iterator iTrack = _TrackStates.begin();iTrack != _TrackStates.end();++iTrack)
350  if ((*iTrack)->parentTrack() == Track) return 1;
351 
352  return 0;
353 }
354 
356 {
357  if (!_FitIsValid)
358  this->refit(); //TODO CHECK FIT OK
359  return _Position;
360 }
361 
362 const Matrix3x3 & CandidateVertex::positionError() const //TODO WHATS UP HERE WITH WINDOWS COMPLILING
363 {
364  if (!_ErrorOfFitIsValid)
365  this->refit(1);
366  return _PositionError;
367 }
368 
369 double CandidateVertex::distanceTo(const Vector3 & Point) const
370 {
371  if (!_FitIsValid)
372  this->refit(); //TODO CHECK FIT OK
373  return _Position.distanceTo(Point);
374 }
375 
377 {
378  if (!_FitIsValid)
379  this->refit(); //TODO CHECK FIT OK
380  return (_Position.subtract(Vertex->position())).mag();
381 }
382 
383 
384 
386 {
387  if(!_VertexFuncMaxIsValid) //TODO Handle Fail
388  this->findVertexFuncMax();
389  return _VertexFuncMaxValue;
390 }
391 
393 {
394  //TODO null pointer check
395  return _VertexFunction->valueAt(this->position());
396 }
397 
399 {
400  if(!_VertexFuncMaxIsValid) //TODO Handle Fail
401  this->findVertexFuncMax();
402  return _VertexFuncMaxPosition;
403 }
404 
406 {
407  //TODO I'm pretty sure this can be done faster, as this is the paranoid way
408  if (!_FitIsValid)
409  this->refit(); //TODO CHECK FIT OK
410  std::map<TrackState*,double>::iterator iTrack = _ChiSquaredOfTrack.find(Track);
411  if(iTrack == _ChiSquaredOfTrack.end())
412  return -1;
413  else
414  return _ChiSquaredOfTrack[Track];
415 }
416 
418 {
419  if(_IP)
420  {
421  if (!_FitIsValid)
422  this->refit();
423  return _ChiSquaredOfIP;
424  }
425  else
426  return 0;
427 }
428 
429 const std::map<TrackState*, double> & CandidateVertex::chiSquaredOfAllTracks() const
430 {
431  if (!_FitIsValid)
432  this->refit(); //TODO CHECK FIT OK
433  return _ChiSquaredOfTrack;
434 }
435 
437 {
438  if (!_FitIsValid)
439  this->refit(); //TODO CHECK FIT OK
440  return _ChiSquaredOfFit;
441 }
442 
444 {
445  if (!_FitIsValid)
446  this->refit();
447  //Find Track with Highest Chi Squared
448  //TODO This could be more fast/clever
449  double HighChiSquared = 0;
450  for (std::vector<TrackState*>::const_iterator iTrackState = _TrackStates.begin();iTrackState != _TrackStates.end();++iTrackState)
451  {
452  if (this->chiSquaredOfTrack(*iTrackState) > HighChiSquared)
453  HighChiSquared = this->chiSquaredOfTrack(*iTrackState);
454  }
455  //Check the IP too
456  if (_IP)
457  {
458  if (this->chiSquaredOfIP()>HighChiSquared)
459  HighChiSquared = this->chiSquaredOfIP();
460  }
461  return HighChiSquared;
462 }
463 
464 VertexFitter* CandidateVertex::_getFallbackFitter()
465 {
466  if (!_FallbackFitter)
467  {
468  _FallbackFitter = new FallbackVertexFitter();
469  MemoryManager<VertexFitter>::Run()->registerObject(_FallbackFitter);
470  }
471 
472  return _FallbackFitter;
473 }
474 
475 VertexResolver* CandidateVertex::_getFallbackResolver()
476 {
477  if (!_FallbackResolver)
478  {
479  _FallbackResolver = new FallbackVertexResolver();
480  MemoryManager<VertexResolver>::Run()->registerObject(_FallbackResolver);
481  }
482  return _FallbackResolver;
483 }
484 
485 VertexFuncMaxFinder* CandidateVertex::_getFallbackMaxFinder()
486 {
487  if (!_FallbackMaxFinder)
488  {
489  _FallbackMaxFinder = new FallbackVertexFuncMaxFinder();
490  MemoryManager<VertexFuncMaxFinder>::Run()->registerObject(_FallbackMaxFinder);
491  }
492  return _FallbackMaxFinder;
493 }
494 }
495 }
496 
double vertexFuncMaxValue() const
Return the value of the vertex function at this vertexes local maximum.
void mergeCandidateVertex(const CandidateVertex *SourceVertex)
Merge another vertex into this one.
bool removeTrack(Track *const TrackToRemove)
Remove the first TrackState from this vertices track list which has parent track TrackToRemove.
double chiSquaredOfIP() const
Return the chi squared contribution of the IP if this vertex has one.
double prob(double ChiSquared, double DegreesOfFreedom)
Calculate probability from Chi Squared and Degrees of freedom.
eResolveType
Type of resolution to perform - vertex position or the nearest found maximum.
void refit(bool CalculateError=0) const
Refit the vertex.
int trimByChi2(const double Chi2Threshold)
Trim trackstates with chi squared contributions bigger than the threshold.
double chiSquaredOfTrack(TrackState *Track) const
Return the chi squared contribution of a trackstate in this vertex.
double maxChiSquaredOfTrackIP() const
Return the chi squared contribution of the Track or IP with the highest chi square contribution...
A collection of TrackState objects with a fit and vertex function maximum.
double distanceTo(const Vector3 &Point) const
Return the distance from this Vertex to a point.
double vertexFuncValue() const
Return the value of the vertex function at the vertices position.
void setIP(InteractionPoint *IP)
Add an InteractionPoint.
int trimByProb(const double ProbThreshold)
Trim trackstates in order of decreasing chi squared until the vertex has a probabilty below that of t...
static MemoryManager< T > * Run()
Returns the Run duration singleton instance of the MemoryManager for type T.
void claimTracksFrom(const std::list< CandidateVertex * > &LosingVertices)
Claim tracks and IP from another vertex.
const Vector3 & position() const
Return the fitted position of this Vertex.
InteractionPoint * interactionPoint() const
Return the InteractionPoint in this Vertex.
bool removeTrackState(TrackState *const TrackToRemove)
Remove the first reference to a TrackState from this vertices track list.
Unique Track representation.
const Matrix3x3 & positionError() const
Return the fitted position error matrix of this Vertex.
bool hasTrack(Track *Track) const
Return if this Vertex contains the passed track.
const std::vector< TrackState * > & trackStateList() const
Return the TrackStates in this Vertex.
CandidateVertex(const std::vector< TrackState * > &Tracks, VertexFunction *VertexFunction, VertexFitter *Fitter=_getFallbackFitter(), VertexResolver *Resolver=_getFallbackResolver(), VertexFuncMaxFinder *MaxFinder=_getFallbackMaxFinder())
Constuct with just Track list and VertexFunction.
void invalidateFit() const
Invalidate the fit so that the refit() is called when needed.
double chiSquaredOfFit() const
Return the chi squared of the fit.
const Vector3 & vertexFuncMaxPosition() const
Return the position of this vertexes local maximum.
bool removeIP()
Remove this vertices InteractionPoint.
bool isResolvedFrom(CandidateVertex *const Vertex, const double Threshold, eResolveType Type) const
Resolve two vertices with this vertices resolver.
const std::map< TrackState *, double > & chiSquaredOfAllTracks() const
Return the chi squared contribution of all the trackstates in this vertex.
void addTrackState(TrackState *TrackToAdd)
Add a TrackState to this vertex.
bool findVertexFuncMax() const
Find the local vertex function maximum.