LCFIVertex  0.7.2
decaychain.cpp
1 #include "../inc/decaychain.h"
2 #include "../inc/vertex.h"
3 #include "../inc/track.h"
4 #include <vector>
5 #include <algorithm>
6 
7 namespace vertex_lcfi
8 {
9  // Ascending sorting function by track d0 - TODO Upgrade to d0 to IP
10  bool Trackd0Ascending(const Track* rpStart, const Track* rpEnd)
11  {
12  return rpStart->helixRep().d0() < rpEnd->helixRep().d0();
13  }
14 
15  // Ascending sorting function by vertex distance to 0,0,0 - TODO Upgrade to distance from IP
16  bool Vertexd0Ascending(const Vertex* rpStart, const Vertex* rpEnd)
17  {
18  return rpStart->position().mag() < rpEnd->position().mag();
19  }
20 
21  DecayChain::DecayChain(const DecayChain & OldDecayChain)
22  :_OwnerJet(OldDecayChain.jet()),_AttachedTracks(OldDecayChain.attachedTracks()),_Vertices(OldDecayChain.vertices())
23  {
24  //To make a copy of the decay chain we need to copy the constituant vertices
25  //However we keep the same tracks as the decay chains methods cannot effect them.
26  //The current vertices are in our list aleady from construction
27  std::vector<Vertex*> OldVerts = _Vertices;
28  _Vertices.clear();
29  for (std::vector<Vertex*>::const_iterator iVertex = OldVerts.begin();iVertex!=OldVerts.end();++iVertex)
30  {
31  //Make a copy
32  Vertex* NewVert = new Vertex(**iVertex);
33  //Add it to our list
34  _Vertices.push_back(NewVert);
35  }
36 
37  std::sort(_AttachedTracks.begin(),_AttachedTracks.end(),Trackd0Ascending);
38  std::sort(_Vertices.begin(),_Vertices.end(),Vertexd0Ascending);
39  //Ensure cache is not used
40  this->_invalidateCache();
41  }
42 
43  DecayChain::DecayChain(Jet* MyJet, const std::vector<Track*> & Tracks, const std::vector<Vertex*> & Vertices)
44  :_OwnerJet(MyJet),_AttachedTracks(Tracks),_Vertices(Vertices)
45  {
46  std::sort(_AttachedTracks.begin(),_AttachedTracks.end(),Trackd0Ascending);
47  std::sort(_Vertices.begin(),_Vertices.end(),Vertexd0Ascending);
48  //Ensure cache is not used
49  this->_invalidateCache();
50  }
51 
52  const std::vector<Track*> & DecayChain::allTracks() const
53  {
54  //Chached variable check the status of the cache
55  if (_AllTracksValid)
56  {/*no op*/}
57  else
58  {
59  _AllTracks.clear();
60  for (std::vector<Track*>::const_iterator iTrack = _AttachedTracks.begin();iTrack!=_AttachedTracks.end();++iTrack)
61  {
62  _AllTracks.push_back(*iTrack);
63  }
64 
65  for (std::vector<Vertex*>::const_iterator iVertex = _Vertices.begin();iVertex!=_Vertices.end();++iVertex)
66  {
67  for (std::vector<Track*>::const_iterator iTrack = (*iVertex)->tracks().begin();iTrack!=(*iVertex)->tracks().end();++iTrack)
68  _AllTracks.push_back(*iTrack);
69  }
70  std::sort(_AllTracks.begin(),_AllTracks.end(),Trackd0Ascending);
71  }
72  return _AllTracks;
73  }
74 
75  const std::vector<Track*> & DecayChain::attachedTracks() const
76  {
77  return _AttachedTracks;
78  }
79 
80  const std::vector<Vertex*> & DecayChain::vertices() const
81  {
82  return _Vertices;
83  }
84 
85  double DecayChain::charge() const
86  {
87  //Chached variable check the status of the cache
88  if (_ChargeValid)
89  {/*no op*/}
90  else
91  {
92  _Charge=0.0;
93  std::vector<Track*> allTracks = this->allTracks();
94  for (std::vector<Track*>::const_iterator iTrack = allTracks.begin();iTrack!=allTracks.end();++iTrack)
95  {
96  //So that we only count the external legs of the decay chain check that the track is only in one vertex
97  int numVertsWithTrack = 0;
98  for (std::vector<Vertex*>::const_iterator iVertex = _Vertices.begin();iVertex!=_Vertices.end();++iVertex)
99  {
100  if ((*iVertex)->hasTrack(*iTrack))
101  ++numVertsWithTrack;
102  }
103  if (numVertsWithTrack < 2) _Charge += (*iTrack)->charge();
104  }
105  }
106  return _Charge;
107  }
108 
110  {
111  //Chached variable check the status of the cache
112  if (_MomValid)
113  {/*no op*/}
114  else
115  {
116  _Momentum.clear();
117  std::vector<Track*> allTracks = this->allTracks();
118  for (std::vector<Track*>::const_iterator iTrack = allTracks.begin();iTrack!=allTracks.end();++iTrack)
119  {
120  //So that we only count the external legs of the decay chain check that the track is only in one vertex
121  int numVertsWithTrack = 0;
122  for (std::vector<Vertex*>::const_iterator iVertex = _Vertices.begin();iVertex!=_Vertices.end();++iVertex)
123  {
124  if ((*iVertex)->hasTrack(*iTrack))
125  ++numVertsWithTrack;
126  }
127  if (numVertsWithTrack < 2) _Momentum += (*iTrack)->momentum();
128  }
129  }
130  return _Momentum;
131  }
132 
134  {
135  _AttachedTracks.push_back(ATrack);
136  this->_invalidateCache();
137  std::sort(_AttachedTracks.begin(),_AttachedTracks.end(),Trackd0Ascending);
138  }
139 
141  {
142  bool found = 1;
143  std::vector<Track*>::iterator position = std::find(_AttachedTracks.begin(), _AttachedTracks.end(), TrackR);
144  if (position!=_AttachedTracks.end()) //Found
145  {
146  _AttachedTracks.erase(position);
147  found = 1;
148  }
149 
150  for (std::vector<Vertex*>::iterator iVertex = _Vertices.begin();iVertex!=_Vertices.end();++iVertex)
151  {
152  if ((*iVertex)->removeTrack(TrackR))
153  found = 1;
154  }
155  if (found)
156  {
157  this->_invalidateCache();
158  std::sort(_AttachedTracks.begin(),_AttachedTracks.end(),Trackd0Ascending);
159  std::sort(_Vertices.begin(),_Vertices.end(),Vertexd0Ascending);
160  }
161  return found;
162  }
163 
164  bool DecayChain::hasTrack(Track* HTrack) const
165  {
166  if (find(_AttachedTracks.begin(), _AttachedTracks.end(), HTrack) != _AttachedTracks.end()) //Found
167  {
168  return 1;
169  }
170  else
171  for (std::vector<Vertex*>::const_iterator iVertex = _Vertices.begin();iVertex!=_Vertices.end();++iVertex)
172  {
173  if ((*iVertex)->hasTrack(HTrack))
174  return 1;
175  }
176  //Not found in either
177  return 0;
178  }
179 
181  {
182  _Vertices.push_back(AVertex);
183  this->_invalidateCache();
184  std::sort(_Vertices.begin(),_Vertices.end(),Vertexd0Ascending);
185  }
186 
188  {
189  std::vector<Vertex*>::iterator position = std::find(_Vertices.begin(), _Vertices.end(), VertexR);
190  if (position!=_Vertices.end()) //Found
191  {
192  _Vertices.erase(position);
193  this->_invalidateCache();
194  std::sort(_Vertices.begin(),_Vertices.end(),Vertexd0Ascending);
195  return 1;
196  }
197  else
198  return 0;
199  }
200 
201  bool DecayChain::hasVertex(Vertex* HVertex) const
202  {
203  if (find(_Vertices.begin(), _Vertices.end(), HVertex) != _Vertices.end()) //Found
204  {
205  return 1;
206  }
207  else
208  return 0;
209  }
210 
211  void DecayChain::_invalidateCache() const
212  {
213  _MomValid=0;
214  _ChargeValid=0;
215  _AllTracksValid=0;
216  }
217 
218 }
bool removeVertex(Vertex *Vertex)
Remove Vertex.
Definition: decaychain.cpp:187
bool removeTrack(Track *Track)
Remove Track.
Definition: decaychain.cpp:140
const std::vector< Track * > & allTracks() const
All tracks contained in DecayChain.
Definition: decaychain.cpp:52
void addVertex(Vertex *Vertex)
Add Vertex.
Definition: decaychain.cpp:180
bool hasVertex(Vertex *Vertex) const
Does the DecayChain have this Vertex?
Definition: decaychain.cpp:201
void addTrack(Track *Track)
Add Track.
Definition: decaychain.cpp:133
bool hasTrack(Track *Track) const
Does the DecayChain have this Track?
Definition: decaychain.cpp:164
const std::vector< Vertex * > & vertices() const
Vertices contained in DecayChain.
Definition: decaychain.cpp:80
double charge() const
Charge sum of all tracks in the DecayChain.
Definition: decaychain.cpp:85
Unique Track representation.
const util::Vector3 & momentum() const
Perigee momentum sum of all tracks in the DecayChain.
Definition: decaychain.cpp:109
const std::vector< Track * > & attachedTracks() const
Attached tracks contained in DecayChain.
Definition: decaychain.cpp:75