CMS 3D CMS Logo

KalmanVertexUpdator.cc
Go to the documentation of this file.
8 
9 #include <algorithm>
10 
11 // Based on the R.Fruhwirth et al Computer Physics Communications 96 (1996) 189-208
12 template <unsigned int N>
15  float weight,
16  int sign) const {
17  if (abs(sign) != 1)
18  throw VertexException("KalmanVertexUpdator::abs(sign) not equal to 1");
19 
20  VertexState newVertexState = positionUpdate(oldVertex.vertexState(), track->linearizedTrack(), weight, sign);
21  if (!newVertexState.isValid())
22  return CachingVertex<N>();
23 
24  float chi1 = oldVertex.totalChiSquared();
25  std::pair<bool, double> chi2P =
26  chi2Increment(oldVertex.vertexState(), newVertexState, track->linearizedTrack(), weight);
27  if (!chi2P.first)
28  return CachingVertex<N>(); // return invalid vertex
29 
30  chi1 += sign * chi2P.second;
31 
32  //adding or removing track from the CachingVertex::VertexTracks
33  std::vector<RefCountedVertexTrack> newVertexTracks = oldVertex.tracks();
34 
35  if (sign > 0) {
36  newVertexTracks.push_back(track);
37  } else {
38  typename std::vector<RefCountedVertexTrack>::iterator pos =
39  find(newVertexTracks.begin(), newVertexTracks.end(), track);
40  if (pos != newVertexTracks.end()) {
41  newVertexTracks.erase(pos);
42  } else {
43  std::cout << "KalmanVertexUpdator::Unable to find requested track in the current vertex" << std::endl;
44  throw VertexException("KalmanVertexUpdator::Unable to find requested track in the current vertex");
45  }
46  }
47 
48  if (oldVertex.hasPrior()) {
49  return CachingVertex<N>(oldVertex.priorVertexState(), newVertexState, newVertexTracks, chi1);
50  } else {
51  return CachingVertex<N>(newVertexState, newVertexTracks, chi1);
52  }
53 }
54 
55 template <unsigned int N>
57  const RefCountedVertexTrack track) const {
58  float weight = track->weight();
59  return update(oldVertex, track, weight, +1);
60 }
61 
62 template <unsigned int N>
64  const RefCountedVertexTrack track) const {
65  float weight = track->weight();
66  return update(oldVertex, track, weight, -1);
67 }
68 
69 template <unsigned int N>
71  const RefCountedLinearizedTrackState linearizedTrack,
72  const float weight,
73  int sign) const {
74  int error;
75 
76  if (!linearizedTrack->isValid())
77  return VertexState();
78 
79  const AlgebraicMatrixN3& a = linearizedTrack->positionJacobian();
80  const AlgebraicMatrixNM& b = linearizedTrack->momentumJacobian();
81 
82  // AlgebraicVectorN trackParameters =
83  // linearizedTrack->predictedStateParameters();
84 
85  AlgebraicSymMatrixNN trackParametersWeight = linearizedTrack->predictedStateWeight(error);
86  if (error != 0) {
87  edm::LogWarning("KalmanVertexUpdator")
88  << "predictedState error matrix inversion failed. An invalid vertex will be returned.";
89  return VertexState();
90  }
91 
92  // Jacobians
93  // edm::LogInfo("RecoVertex/KalmanVertexUpdator")
94  // << "Now updating position" << "\n";
95 
96  //vertex information
97  // AlgebraicSymMatrix33 oldVertexWeight = oldVertex.weight().matrix();
98  AlgebraicSymMatrixMM s = ROOT::Math::SimilarityT(b, trackParametersWeight);
99  if (!invertPosDefMatrix(s)) {
100  edm::LogWarning("KalmanVertexUpdator") << "S matrix inversion failed. An invalid vertex will be returned.";
101  return VertexState();
102  }
103 
105  trackParametersWeight - ROOT::Math::Similarity(trackParametersWeight, ROOT::Math::Similarity(b, s));
106 
107  // Getting the new covariance matrix of the vertex.
108 
109  AlgebraicSymMatrix33 newVertexWeight = oldVertex.weight().matrix() + (weight * sign) * ROOT::Math::SimilarityT(a, gB);
110  // edm::LogInfo("RecoVertex/KalmanVertexUpdator")
111  // << "weight matrix" << newVertexWeight << "\n";
112 
113  AlgebraicVector3 newSwr =
114  oldVertex.weightTimesPosition() +
115  (weight * sign) * ((ROOT::Math::Transpose(a) * gB) *
116  (linearizedTrack->predictedStateParameters() - linearizedTrack->constantTerm()));
117  // edm::LogInfo("RecoVertex/KalmanVertexUpdator")
118  // << "weighttimespos" << newSwr << "\n";
119 
120  VertexState newpos(newSwr, GlobalWeight(newVertexWeight), 1.0);
121 
122  // edm::LogInfo("RecoVertex/KalmanVertexUpdator")
123  // << "pos" << newpos.position() << "\n";
124 
125  return newpos;
126 }
127 
128 template <unsigned int N>
129 std::pair<bool, double> KalmanVertexUpdator<N>::chi2Increment(const VertexState& oldVertex,
130  const VertexState& newVertexState,
131  const RefCountedLinearizedTrackState linearizedTrack,
132  float weight) const {
133  int error;
134  GlobalPoint newVertexPosition = newVertexState.position();
135 
136  if (!linearizedTrack->isValid())
137  return std::pair<bool, double>(false, -1.);
138 
139  AlgebraicVector3 newVertexPositionV;
140  newVertexPositionV(0) = newVertexPosition.x();
141  newVertexPositionV(1) = newVertexPosition.y();
142  newVertexPositionV(2) = newVertexPosition.z();
143 
144  const AlgebraicMatrixN3& a = linearizedTrack->positionJacobian();
145  const AlgebraicMatrixNM& b = linearizedTrack->momentumJacobian();
146 
147  AlgebraicVectorN trackParameters = linearizedTrack->predictedStateParameters();
148 
149  AlgebraicSymMatrixNN trackParametersWeight = linearizedTrack->predictedStateWeight(error);
150  if (error != 0) {
151  edm::LogWarning("KalmanVertexUpdator")
152  << "predictedState error matrix inversion failed. An invalid vertex will be returned.";
153  return std::pair<bool, double>(false, -1.);
154  }
155 
156  AlgebraicSymMatrixMM s = ROOT::Math::SimilarityT(b, trackParametersWeight);
157  if (!invertPosDefMatrix(s)) {
158  edm::LogWarning("KalmanVertexUpdator") << "S matrix inversion failed. An invalid vertex will be returned.";
159  return std::pair<bool, double>(false, -1.);
160  }
161 
162  const AlgebraicVectorN& theResidual = linearizedTrack->constantTerm();
163  AlgebraicVectorN vv = trackParameters - theResidual - a * newVertexPositionV;
164  AlgebraicVectorM newTrackMomentumP = s * ROOT::Math::Transpose(b) * trackParametersWeight * vv;
165 
166  // AlgebraicVectorN rtp = ( theResidual + a * newVertexPositionV + b * newTrackMomentumP);
167 
168  AlgebraicVectorN parameterResiduals = vv - b * newTrackMomentumP;
169  linearizedTrack->checkParameters(parameterResiduals);
170 
171  double chi2 = weight * ROOT::Math::Similarity(parameterResiduals, trackParametersWeight);
172 
173  // chi2 += vertexPositionChi2(oldVertex, newVertexPosition);
174  chi2 += helper.vertexChi2(oldVertex, newVertexState);
175 
176  return std::pair<bool, double>(true, chi2);
177 }
178 
179 template class KalmanVertexUpdator<5>;
180 template class KalmanVertexUpdator<6>;
Definition: helper.py:1
CachingVertex< N > remove(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track) const override
T z() const
Definition: PV3DBase.h:61
Common base class.
VertexState positionUpdate(const VertexState &oldVertex, const RefCountedLinearizedTrackState linearizedTrack, const float weight, int sign) const
bool isValid() const
Make the ReferenceCountingProxy method to check validity public.
Definition: VertexState.h:93
CachingVertex< N > update(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track, float weight, int sign) const
VertexState const & vertexState() const
Definition: weight.py:1
ROOT::Math::SMatrix< double, N, 3, ROOT::Math::MatRepStd< double, N, 3 > > AlgebraicMatrixN3
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
float totalChiSquared() const
ROOT::Math::SMatrix< double, N - 2, N - 2, ROOT::Math::MatRepSym< double, N - 2 > > AlgebraicSymMatrixMM
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
VertexState const & priorVertexState() const
std::vector< RefCountedVertexTrack > tracks() const
ROOT::Math::SVector< double, N - 2 > AlgebraicVectorM
GlobalWeight weight() const
Definition: VertexState.h:69
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ROOT::Math::SVector< double, N > AlgebraicVectorN
const AlgebraicSymMatrix33 matrix() const
GlobalErrorBase< double, WeightMatrixTag > GlobalWeight
Definition: GlobalWeight.h:12
double b
Definition: hdecay.h:118
AlgebraicVector3 weightTimesPosition() const
Definition: VertexState.h:77
#define update(a, b)
bool hasPrior() const
double a
Definition: hdecay.h:119
std::pair< bool, double > chi2Increment(const VertexState &oldVertex, const VertexState &newVertexState, const RefCountedLinearizedTrackState linearizedTrack, float weight) const
ROOT::Math::SVector< double, 3 > AlgebraicVector3
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
CachingVertex< N > add(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track) const override
Log< level::Warning, false > LogWarning
ROOT::Math::SMatrix< double, N, N, ROOT::Math::MatRepSym< double, N > > AlgebraicSymMatrixNN
GlobalPoint position() const
Definition: VertexState.h:62
ROOT::Math::SMatrix< double, N, N - 2, ROOT::Math::MatRepStd< double, N, N - 2 > > AlgebraicMatrixNM