CMS 3D CMS Logo

GsfVertexSmoother.cc
Go to the documentation of this file.
5 
6 GsfVertexSmoother::GsfVertexSmoother(bool limit, const GsfVertexMerger* merger) : limitComponents(limit) {
7  if (limitComponents)
8  theMerger = merger->clone();
9 }
10 
12  std::vector<RefCountedVertexTrack> tracks = vertex.tracks();
13  int numberOfTracks = tracks.size();
14  if (numberOfTracks < 1)
15  return vertex;
16 
17  // Initial vertex for ascending fit
18  GlobalPoint priorVertexPosition = tracks[0]->linearizedTrack()->linearizationPoint();
19  AlgebraicSymMatrix33 we = ROOT::Math::SMatrixIdentity();
20  GlobalError priorVertexError(we * 10000);
21 
22  std::vector<RefCountedVertexTrack> initialTracks;
23  CachingVertex<5> fitVertex(priorVertexPosition, priorVertexError, initialTracks, 0);
24  //In case prior vertex was used.
25  if (vertex.hasPrior()) {
26  const VertexState& priorVertexState = vertex.priorVertexState();
27  fitVertex = CachingVertex<5>(priorVertexState, priorVertexState, initialTracks, 0);
28  }
29 
30  // vertices from ascending fit
31  std::vector<CachingVertex<5> > ascendingFittedVertices;
32  ascendingFittedVertices.reserve(numberOfTracks);
33  ascendingFittedVertices.push_back(fitVertex);
34 
35  // ascending fit
36  for (std::vector<RefCountedVertexTrack>::const_iterator i = tracks.begin(); i != (tracks.end() - 1); ++i) {
37  fitVertex = theUpdator.add(fitVertex, *i);
38  if (limitComponents)
39  fitVertex = theMerger->merge(fitVertex);
40  ascendingFittedVertices.push_back(fitVertex);
41  }
42 
43  // Initial vertex for descending fit
44  priorVertexPosition = tracks[0]->linearizedTrack()->linearizationPoint();
45  priorVertexError = GlobalError(we * 10000);
46  fitVertex = CachingVertex<5>(priorVertexPosition, priorVertexError, initialTracks, 0);
47 
48  // vertices from descending fit
49  std::vector<CachingVertex<5> > descendingFittedVertices;
50  descendingFittedVertices.reserve(numberOfTracks);
51  descendingFittedVertices.push_back(fitVertex);
52 
53  // descending fit
54  for (std::vector<RefCountedVertexTrack>::const_iterator i = (tracks.end() - 1); i != (tracks.begin()); --i) {
55  fitVertex = theUpdator.add(fitVertex, *i);
56  if (limitComponents)
57  fitVertex = theMerger->merge(fitVertex);
58  descendingFittedVertices.insert(descendingFittedVertices.begin(), fitVertex);
59  }
60 
61  std::vector<RefCountedVertexTrack> newTracks;
62  double smoothedChi2 = 0.; // Smoothed chi2
63 
64  // Track refit
65  for (std::vector<RefCountedVertexTrack>::const_iterator i = tracks.begin(); i != tracks.end(); i++) {
66  int indexNumber = i - tracks.begin();
67  //First, combine the vertices:
68  VertexState meanedVertex = meanVertex(ascendingFittedVertices[indexNumber].vertexState(),
69  descendingFittedVertices[indexNumber].vertexState());
70  if (limitComponents)
71  meanedVertex = theMerger->merge(meanedVertex);
72  // Add the vertex and smooth the track:
73  TrackChi2Pair thePair = vertexAndTrackUpdate(meanedVertex, *i, vertex.position());
74  smoothedChi2 += thePair.second.second;
75  newTracks.push_back(theVTFactory.vertexTrack((**i).linearizedTrack(),
76  vertex.vertexState(),
77  thePair.first,
78  thePair.second.second,
80  (**i).weight()));
81  }
82 
83  if (vertex.hasPrior()) {
84  smoothedChi2 += priorVertexChi2(vertex.priorVertexState(), vertex.vertexState());
85  return CachingVertex<5>(vertex.priorVertexState(), vertex.vertexState(), newTracks, smoothedChi2);
86  } else {
87  return CachingVertex<5>(vertex.vertexState(), newTracks, smoothedChi2);
88  }
89 }
90 
93  const GlobalPoint& referencePosition) const {
94  VSC prevVtxComponents = oldVertex.components();
95 
96  if (prevVtxComponents.empty()) {
97  throw VertexException("GsfVertexSmoother::(Previous) Vertex to update has no components");
98  }
99 
100  LTC ltComponents = track->linearizedTrack()->components();
101  if (ltComponents.empty()) {
102  throw VertexException("GsfVertexSmoother::Track to add to vertex has no components");
103  }
104  float trackWeight = track->weight();
105 
106  std::vector<RefittedTrackComponent> newTrackComponents;
107  newTrackComponents.reserve(prevVtxComponents.size() * ltComponents.size());
108 
109  for (VSC::iterator vertexCompIter = prevVtxComponents.begin(); vertexCompIter != prevVtxComponents.end();
110  vertexCompIter++) {
111  for (LTC::iterator trackCompIter = ltComponents.begin(); trackCompIter != ltComponents.end(); trackCompIter++) {
112  newTrackComponents.push_back(createNewComponent(*vertexCompIter, *trackCompIter, trackWeight));
113  }
114  }
115 
116  return assembleTrackComponents(newTrackComponents, referencePosition);
117 }
118 
125  const std::vector<GsfVertexSmoother::RefittedTrackComponent>& trackComponents,
126  const GlobalPoint& referencePosition) const {
127  //renormalize weights
128 
129  double totalWeight = 0.;
130  double totalVtxChi2 = 0., totalTrkChi2 = 0.;
131 
132  for (std::vector<RefittedTrackComponent>::const_iterator iter = trackComponents.begin();
133  iter != trackComponents.end();
134  ++iter) {
135  totalWeight += iter->first.second;
136  totalVtxChi2 += iter->second.first * iter->first.second;
137  totalTrkChi2 += iter->second.second * iter->first.second;
138  }
139 
140  totalVtxChi2 /= totalWeight;
141  totalTrkChi2 /= totalWeight;
142 
143  std::vector<RefCountedRefittedTrackState> reWeightedRTSC;
144  reWeightedRTSC.reserve(trackComponents.size());
145 
146  for (std::vector<RefittedTrackComponent>::const_iterator iter = trackComponents.begin();
147  iter != trackComponents.end();
148  ++iter) {
149  if (iter->second.first != 0) {
150  reWeightedRTSC.push_back(iter->first.first->stateWithNewWeight(iter->second.first / totalWeight));
151  }
152  }
153 
155  RefCountedRefittedTrackState(new MultiRefittedTS(reWeightedRTSC, referencePosition));
156  return TrackChi2Pair(finalRTS, VtxTrkChi2Pair(totalVtxChi2, totalTrkChi2));
157 }
158 
165  const VertexState& oldVertex, const RefCountedLinearizedTrackState linTrack, float trackWeight) const {
166  int sign = +1;
167 
168  // Weight of the component in the mixture (non-normalized)
169  double weightInMixture = theWeightCalculator.calculate(oldVertex, linTrack, 1000000000.);
170 
171  // position estimate of the component
172  VertexState newVertex = kalmanVertexUpdator.positionUpdate(oldVertex, linTrack, trackWeight, sign);
173 
175  theVertexTrackUpdator.trackRefit(newVertex, linTrack, trackWeight);
176 
177  //Chi**2 contribution of the track component
178  double vtxChi2 = helper.vertexChi2(oldVertex, newVertex);
179  std::pair<bool, double> trkCi2 = helper.trackParameterChi2(linTrack, thePair.first);
180 
181  return RefittedTrackComponent(TrackWeightPair(thePair.first, weightInMixture),
182  VtxTrkChi2Pair(vtxChi2, trkCi2.second));
183 }
184 
185 VertexState GsfVertexSmoother::meanVertex(const VertexState& vertexA, const VertexState& vertexB) const {
186  std::vector<VertexState> vsCompA = vertexA.components();
187  std::vector<VertexState> vsCompB = vertexB.components();
188  std::vector<VertexState> finalVS;
189  finalVS.reserve(vsCompA.size() * vsCompB.size());
190  for (std::vector<VertexState>::iterator iA = vsCompA.begin(); iA != vsCompA.end(); ++iA) {
191  for (std::vector<VertexState>::iterator iB = vsCompB.begin(); iB != vsCompB.end(); ++iB) {
192  AlgebraicSymMatrix33 newWeight = iA->weight().matrix() + iB->weight().matrix();
193  AlgebraicVector3 newWtP = iA->weightTimesPosition() + iB->weightTimesPosition();
194  double newWeightInMixture = iA->weightInMixture() * iB->weightInMixture();
195  finalVS.push_back(VertexState(newWtP, newWeight, newWeightInMixture));
196  }
197  }
198 #ifndef CMS_NO_COMPLEX_RETURNS
199  return VertexState(new BasicMultiVertexState(finalVS));
200 #else
201  VertexState theFinalVM(new BasicMultiVertexState(finalVS));
202  return theFinalVM;
203 #endif
204 }
205 
206 double GsfVertexSmoother::priorVertexChi2(const VertexState priorVertex, const VertexState fittedVertex) const {
207  std::vector<VertexState> priorVertexComp = priorVertex.components();
208  std::vector<VertexState> fittedVertexComp = fittedVertex.components();
209  double vetexChi2 = 0.;
210  for (std::vector<VertexState>::iterator pvI = priorVertexComp.begin(); pvI != priorVertexComp.end(); ++pvI) {
211  for (std::vector<VertexState>::iterator fvI = fittedVertexComp.begin(); fvI != fittedVertexComp.end(); ++fvI) {
212  vetexChi2 += (pvI->weightInMixture()) * (fvI->weightInMixture()) * helper.vertexChi2(*pvI, *fvI);
213  }
214  }
215  return vetexChi2;
216 }
VertexTrackFactory< 5 > theVTFactory
double calculate(const VertexState &oldVertex, const RefCountedLinearizedTrackState track, double cov) const
VertexTrack< 5 >::RefCountedRefittedTrackState RefCountedRefittedTrackState
std::vector< RefCountedLinearizedTrackState > LTC
CachingVertex< 5 > merge(const CachingVertex< 5 > &vertex) const
CachingVertex< 5 > add(const CachingVertex< 5 > &oldVertex, const RefCountedVertexTrack track) const override
Definition: helper.py:1
CachingVertex< 5 >::RefCountedVertexTrack RefCountedVertexTrack
Common base class.
VertexState positionUpdate(const VertexState &oldVertex, const RefCountedLinearizedTrackState linearizedTrack, const float weight, int sign) const
GsfVertexSmoother(bool limit, const GsfVertexMerger *merger)
RefCountedVertexTrack vertexTrack(const RefCountedLinearizedTrackState lt, const VertexState vs, float weight=1.0) const
std::pair< double, double > VtxTrkChi2Pair
GlobalErrorBase< double, ErrorMatrixTag > GlobalError
Definition: GlobalError.h:13
std::pair< RefCountedRefittedTrackState, double > TrackWeightPair
GsfVertexUpdator theUpdator
TrackChi2Pair vertexAndTrackUpdate(const VertexState &oldVertex, const RefCountedVertexTrack track, const GlobalPoint &referencePosition) const
GsfVertexWeightCalculator theWeightCalculator
KalmanVertexUpdator< 5 > kalmanVertexUpdator
std::pair< RefCountedRefittedTrackState, VtxTrkChi2Pair > TrackChi2Pair
std::vector< VertexState > components() const
Definition: VertexState.h:90
KalmanVertexTrackUpdator< 5 > theVertexTrackUpdator
double priorVertexChi2(const VertexState priorVertex, const VertexState fittedVertex) const
std::vector< VertexState > VSC
VertexState meanVertex(const VertexState &vertexA, const VertexState &vertexB) const
TrackChi2Pair assembleTrackComponents(const std::vector< RefittedTrackComponent > &trackComponents, const GlobalPoint &referencePosition) const
std::pair< TrackWeightPair, VtxTrkChi2Pair > RefittedTrackComponent
RefittedTrackComponent createNewComponent(const VertexState &oldVertex, const RefCountedLinearizedTrackState linTrack, float weight) const
auto const & tracks
cannot be loose
trackMatrixPair trackRefit(const VertexState &vertex, RefCountedLinearizedTrackState linTrackState, float weight) const
DeepCopyPointerByClone< GsfVertexMerger > theMerger
CachingVertex< 5 > smooth(const CachingVertex< 5 > &vertex) const override
GsfVertexMerger * clone() const
ROOT::Math::SVector< double, 3 > AlgebraicVector3
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
VertexTrack< 5 >::AlgebraicSymMatrixOO AlgebraicSymMatrixOO
float trackWeight(const reco::Vertex &sv, const reco::TransientTrack &track)