CMS 3D CMS Logo

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