CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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>;
std::pair< bool, double > chi2Increment(const VertexState &oldVertex, const VertexState &newVertexState, const RefCountedLinearizedTrackState linearizedTrack, float weight) const
std::vector< RefCountedVertexTrack > tracks() const
CachingVertex< N > remove(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track) const override
Common base class.
VertexState const & vertexState() const
const AlgebraicSymMatrix33 matrix() const
double sign(double x)
T y() const
Definition: PV3DBase.h:60
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
GlobalPoint position() const
Definition: VertexState.h:62
ROOT::Math::SMatrix< double, N-2, N-2, ROOT::Math::MatRepSym< double, N-2 > > AlgebraicSymMatrixMM
ROOT::Math::SVector< double, N-2 > AlgebraicVectorM
AlgebraicVector3 weightTimesPosition() const
Definition: VertexState.h:77
T z() const
Definition: PV3DBase.h:61
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
float totalChiSquared() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ROOT::Math::SVector< double, N > AlgebraicVectorN
bool hasPrior() const
GlobalWeight weight() const
Definition: VertexState.h:69
VertexState positionUpdate(const VertexState &oldVertex, const RefCountedLinearizedTrackState linearizedTrack, const float weight, int sign) const
VertexState const & priorVertexState() const
GlobalErrorBase< double, WeightMatrixTag > GlobalWeight
Definition: GlobalWeight.h:12
double b
Definition: hdecay.h:118
bool isValid() const
Make the ReferenceCountingProxy method to check validity public.
Definition: VertexState.h:93
#define update(a, b)
double a
Definition: hdecay.h:119
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
tuple cout
Definition: gather_cfg.py:144
Log< level::Warning, false > LogWarning
int weight
Definition: histoStyle.py:51
CachingVertex< N > update(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track, float weight, int sign) const
T x() const
Definition: PV3DBase.h:59
ROOT::Math::SMatrix< double, N, N, ROOT::Math::MatRepSym< double, N > > AlgebraicSymMatrixNN
ROOT::Math::SMatrix< double, N, N-2, ROOT::Math::MatRepStd< double, N, N-2 > > AlgebraicMatrixNM