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.
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>
14  const RefCountedVertexTrack track, float weight, int sign) const
15 {
16  if(abs(sign) != 1) throw VertexException
17  ("KalmanVertexUpdator::abs(sign) not equal to 1");
18 
19  VertexState newVertexState = positionUpdate(oldVertex.vertexState(),
20  track->linearizedTrack(), weight, sign);
21  if (!newVertexState.isValid()) return CachingVertex<N>();
22 
23  float chi1 = oldVertex.totalChiSquared();
24  std::pair <bool, double> chi2P = chi2Increment(oldVertex.vertexState(), newVertexState,
25  track->linearizedTrack() , weight );
26  if (!chi2P.first) return CachingVertex<N>(); // return invalid vertex
27 
28  chi1 +=sign * chi2P.second;
29 
30 //adding or removing track from the CachingVertex::VertexTracks
31  std::vector<RefCountedVertexTrack> newVertexTracks = oldVertex.tracks();
32 
33  if (sign > 0) {
34  newVertexTracks.push_back(track);
35  }else{
36 
37  typename std::vector<RefCountedVertexTrack>::iterator pos
38  = find(newVertexTracks.begin(), newVertexTracks.end(), track);
39  if (pos != newVertexTracks.end()) {
40  newVertexTracks.erase(pos);
41  } else {
42  std::cout<<"KalmanVertexUpdator::Unable to find requested track in the current vertex"<<std::endl;
43  throw VertexException("KalmanVertexUpdator::Unable to find requested track in the current vertex");
44  }
45  }
46 
47  if (oldVertex.hasPrior()) {
48  return CachingVertex<N>( oldVertex.priorVertexState(),
49  newVertexState, newVertexTracks, chi1);
50  } else {
51  return CachingVertex<N>(newVertexState, newVertexTracks, chi1);
52  }
53 }
54 
55 
56 template <unsigned int N>
58  const RefCountedVertexTrack track) const
59 {
60  float weight = track->weight();
61  return update(oldVertex,track,weight,+1);
62 }
63 
64 template <unsigned int N>
66  const RefCountedVertexTrack track) const
67 {
68  float weight = track->weight();
69  return update(oldVertex,track,weight,-1);
70 }
71 
72 
73 template <unsigned int N>
76  const RefCountedLinearizedTrackState linearizedTrack,
77  const float weight, int sign) const
78 {
79  int error;
80 
81  if (!linearizedTrack->isValid())
82  return VertexState();
83 
84  const AlgebraicMatrixN3 & a = linearizedTrack->positionJacobian();
85  const AlgebraicMatrixNM & b = linearizedTrack->momentumJacobian();
86 
87 // AlgebraicVectorN trackParameters =
88 // linearizedTrack->predictedStateParameters();
89 
90  AlgebraicSymMatrixNN trackParametersWeight =
91  linearizedTrack->predictedStateWeight(error);
92  if(error != 0) {
93  edm::LogWarning("KalmanVertexUpdator") << "predictedState error matrix inversion failed. An invalid vertex will be returned.";
94  return VertexState();
95  }
96 
97 
98  // Jacobians
99  // edm::LogInfo("RecoVertex/KalmanVertexUpdator")
100  // << "Now updating position" << "\n";
101 
102  //vertex information
103 // AlgebraicSymMatrix33 oldVertexWeight = oldVertex.weight().matrix_new();
104  AlgebraicSymMatrixMM s = ROOT::Math::SimilarityT(b,trackParametersWeight);
105  if (!invertPosDefMatrix(s)) {
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 = oldVertex.weightTimesPosition()
122  + (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  if (!invertPosDefMatrix(s)) {
168  edm::LogWarning("KalmanVertexUpdator") << "S matrix inversion failed. An invalid vertex will be returned.";
169  return std::pair <bool, double> (false, -1.);
170  }
171 
172  const AlgebraicVectorN & theResidual = linearizedTrack->constantTerm();
173  AlgebraicVectorN vv = trackParameters - theResidual - a*newVertexPositionV;
174  AlgebraicVectorM newTrackMomentumP = s * ROOT::Math::Transpose(b) * trackParametersWeight * vv;
175 
176 
177 // AlgebraicVectorN rtp = ( theResidual + a * newVertexPositionV + b * newTrackMomentumP);
178 
179  AlgebraicVectorN parameterResiduals = vv - b * newTrackMomentumP;
180  linearizedTrack->checkParameters(parameterResiduals);
181 
182  double chi2 = weight * ROOT::Math::Similarity(parameterResiduals, trackParametersWeight);
183 
184 // chi2 += vertexPositionChi2(oldVertex, newVertexPosition);
185  chi2 += helper.vertexChi2(oldVertex, newVertexState);
186 
187  return std::pair <bool, double> (true, chi2);
188 }
189 
190 template class KalmanVertexUpdator<5>;
191 template class KalmanVertexUpdator<6>;
std::pair< bool, double > chi2Increment(const VertexState &oldVertex, const VertexState &newVertexState, const RefCountedLinearizedTrackState linearizedTrack, float weight) const
ROOT::Math::SMatrix< double, N, N-2, ROOT::Math::MatRepStd< double, N, N-2 > > AlgebraicMatrixNM
Common base class.
VertexState const & vertexState() const
Definition: CachingVertex.h:85
ROOT::Math::SVector< double, N > AlgebraicVectorN
double sign(double x)
ROOT::Math::SMatrix< double, N, 3, ROOT::Math::MatRepStd< double, N, 3 > > AlgebraicMatrixN3
T y() const
Definition: PV3DBase.h:63
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:50
const AlgebraicSymMatrix33 & matrix_new() const
std::vector< RefCountedVertexTrack > const & tracks() const
Definition: CachingVertex.h:91
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:65
T z() const
Definition: PV3DBase.h:64
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
float totalChiSquared() const
ROOT::Math::SVector< double, 3 > AlgebraicVector3
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool hasPrior() const
Definition: CachingVertex.h:95
GlobalWeight weight() const
Definition: VertexState.h:60
VertexState positionUpdate(const VertexState &oldVertex, const RefCountedLinearizedTrackState linearizedTrack, const float weight, int sign) const
VertexState const & priorVertexState() const
Definition: CachingVertex.h:86
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:88
#define update(a, b)
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:145
CachingVertex< N > add(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track) const
int weight
Definition: histoStyle.py:50
CachingVertex< N > update(const CachingVertex< N > &oldVertex, const RefCountedVertexTrack track, float weight, int sign) const
T x() const
Definition: PV3DBase.h:62
ROOT::Math::SVector< double, N-2 > AlgebraicVectorM