00001 #include "RecoVertex/VertexPrimitives/interface/CachingVertex.h"
00002 #include "RecoVertex/VertexPrimitives/interface/LinearizedTrackState.h"
00003 #include "RecoVertex/VertexPrimitives/interface/VertexException.h"
00004 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00005 #include "RecoVertex/VertexPrimitives/interface/TTtoTTmap.h"
00006 #include <map>
00007
00008
00009 template <unsigned int N>
00010 CachingVertex<N>::CachingVertex(const GlobalPoint & pos,
00011 const GlobalError & posErr,
00012 const vector<RefCountedVertexTrack> & tks,
00013 float totalChiSq)
00014 : theVertexState(pos, posErr),
00015 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00016 theTracks(tks), theCovMapAvailable(false), withPrior(false),
00017 theValid(true)
00018
00019 {}
00020
00021
00022
00023 template <unsigned int N>
00024 CachingVertex<N>::CachingVertex(const GlobalPoint & pos,
00025 const GlobalWeight & posWeight,
00026 const vector<RefCountedVertexTrack> & tks,
00027 float totalChiSq)
00028 : theVertexState(pos, posWeight),
00029 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00030 theTracks(tks), theCovMapAvailable(false), withPrior(false),
00031 theValid(true)
00032 {}
00033
00034
00035
00036 template <unsigned int N>
00037 CachingVertex<N>::CachingVertex(const AlgebraicVector3 & weightTimesPosition,
00038 const GlobalWeight & posWeight,
00039 const vector<RefCountedVertexTrack> & tks,
00040 float totalChiSq)
00041 : theVertexState(weightTimesPosition, posWeight),
00042 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00043 theTracks(tks), theCovMapAvailable(false), withPrior(false),
00044 theValid(true)
00045 {}
00046
00047 template <unsigned int N>
00048 CachingVertex<N>::CachingVertex(const VertexState & aVertexState,
00049 const vector<RefCountedVertexTrack> & tks,
00050 float totalChiSq)
00051 : theVertexState(aVertexState),
00052 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00053 theTracks(tks), theCovMapAvailable(false), withPrior(false),
00054 theValid(true)
00055 {}
00056
00057
00058 template <unsigned int N>
00059 CachingVertex<N>::CachingVertex(const VertexState & aVertexState,
00060 const vector<RefCountedVertexTrack> & tks,
00061 float totalChiSq,
00062 const TrackToTrackMap & covMap)
00063 : theVertexState(aVertexState),
00064 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00065 theTracks(tks), theCovMap(covMap), theCovMapAvailable(true),
00066 withPrior(false), theValid(true)
00067 {
00068 if (theCovMap.empty()) theCovMapAvailable = false;
00069 }
00070
00071 template <unsigned int N>
00072 CachingVertex<N>::CachingVertex(const VertexState & priorVertexState,
00073 const VertexState & aVertexState,
00074 const vector<RefCountedVertexTrack> & tks,
00075 float totalChiSq)
00076 : theVertexState(aVertexState), theChiSquared(totalChiSq),
00077 theNDF(0), theNDFAvailable(false), theTracks(tks),
00078 theCovMapAvailable(false), thePriorVertexState(priorVertexState),
00079 withPrior(true), theValid(true)
00080 {}
00081
00082
00083 template <unsigned int N>
00084 CachingVertex<N>::CachingVertex(const GlobalPoint & priorPos,
00085 const GlobalError & priorErr,
00086 const GlobalPoint & pos,
00087 const GlobalError & posErr,
00088 const vector<RefCountedVertexTrack> & tks,
00089 float totalChiSq)
00090 : theVertexState(pos, posErr),
00091 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00092 theTracks(tks), theCovMapAvailable(false),
00093 thePriorVertexState(priorPos, priorErr), withPrior(true), theValid(true)
00094 {}
00095
00096
00097
00098 template <unsigned int N>
00099 CachingVertex<N>::CachingVertex(const GlobalPoint & priorPos,
00100 const GlobalError & priorErr,
00101 const GlobalPoint & pos,
00102 const GlobalWeight & posWeight,
00103 const vector<RefCountedVertexTrack> & tks,
00104 float totalChiSq)
00105 : theVertexState(pos, posWeight),
00106 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00107 theTracks(tks), theCovMapAvailable(false),
00108 thePriorVertexState(priorPos, priorErr), withPrior(true), theValid(true)
00109 {}
00110
00111
00112
00113 template <unsigned int N>
00114 CachingVertex<N>::CachingVertex(const GlobalPoint & priorPos,
00115 const GlobalError & priorErr,
00116 const AlgebraicVector3 & weightTimesPosition,
00117 const GlobalWeight & posWeight,
00118 const vector<RefCountedVertexTrack> & tks,
00119 float totalChiSq)
00120 : theVertexState(weightTimesPosition, posWeight),
00121 theChiSquared(totalChiSq), theNDF(0), theNDFAvailable(false),
00122 theTracks(tks), theCovMapAvailable(false),
00123 thePriorVertexState(priorPos, priorErr), withPrior(true), theValid(true)
00124 {}
00125
00126
00127 template <unsigned int N>
00128 CachingVertex<N>::CachingVertex(const VertexState & priorVertexState,
00129 const VertexState & aVertexState,
00130 const vector<RefCountedVertexTrack> & tks,
00131 float totalChiSq,
00132 const TrackToTrackMap & covMap)
00133 : theVertexState(aVertexState), theChiSquared(totalChiSq),
00134 theNDF(0), theNDFAvailable(false), theTracks(tks),
00135 theCovMap(covMap), theCovMapAvailable(true),
00136 thePriorVertexState(priorVertexState), withPrior(true), theValid(true)
00137 {
00138 if (theCovMap.empty()) theCovMapAvailable = false;
00139 }
00140
00141 template <unsigned int N>
00142 CachingVertex<N>::CachingVertex()
00143 : theChiSquared(-1), theNDF(0), theNDFAvailable(false), theTracks(),
00144 theCovMapAvailable(false), withPrior(false),
00145 theValid(false)
00146 {}
00147
00148 template <unsigned int N>
00149 GlobalPoint CachingVertex<N>::position() const
00150 {
00151 return theVertexState.position();
00152 }
00153
00154
00155 template <unsigned int N>
00156 GlobalError CachingVertex<N>::error() const
00157 {
00158 return theVertexState.error();
00159 }
00160
00161
00162 template <unsigned int N>
00163 GlobalWeight CachingVertex<N>::weight() const
00164 {
00165 return theVertexState.weight();
00166 }
00167
00168
00169 template <unsigned int N>
00170 AlgebraicVector3 CachingVertex<N>::weightTimesPosition() const
00171 {
00172 return theVertexState.weightTimesPosition();
00173 }
00174
00175
00176 template <unsigned int N>
00177 float CachingVertex<N>::degreesOfFreedom() const
00178 {
00179 if (!theNDFAvailable) computeNDF();
00180 return theNDF;
00181 }
00182
00183
00184 template <unsigned int N>
00185 void CachingVertex<N>::computeNDF() const
00186 {
00187 theNDF = 0;
00188 for (typename vector<RefCountedVertexTrack>::const_iterator itk = theTracks.begin();
00189 itk != theTracks.end(); ++itk) {
00190 theNDF += (**itk).weight();
00191 }
00192 theNDF *= 2.;
00193 if (!withPrior) theNDF -= 3.;
00194 theNDFAvailable = true;
00195 }
00196
00197
00198 template <unsigned int N>
00199 typename CachingVertex<N>::AlgebraicSymMatrixMM
00200 CachingVertex<N>::tkToTkCovariance(const RefCountedVertexTrack t1,
00201 const RefCountedVertexTrack t2) const
00202 {
00203 if (!tkToTkCovarianceIsAvailable()) {
00204 throw VertexException("CachingVertex::TkTkCovariance requested before been calculated");
00205 }
00206 else {
00207 RefCountedVertexTrack tr1;
00208 RefCountedVertexTrack tr2;
00209 if(t1 < t2) {
00210 tr1 = t1;
00211 tr2 = t2;
00212 }
00213 else {
00214 tr1 = t2;
00215 tr2 = t1;
00216 }
00217 typename TrackToTrackMap::const_iterator it = theCovMap.find(tr1);
00218 if (it != theCovMap.end()) {
00219 const TrackMap & tm = it->second;
00220 typename TrackMap::const_iterator nit = tm.find(tr2);
00221 if (nit != tm.end()) {
00222 return( nit->second);
00223 }
00224 else {
00225 throw VertexException("CachingVertex::requested TkTkCovariance does not exist");
00226 }
00227 }
00228 else {
00229 throw VertexException("CachingVertex::requested TkTkCovariance does not exist");
00230 }
00231 }
00232 }
00233
00234 template <unsigned int N>
00235 CachingVertex<N>::operator TransientVertex() const
00236 {
00237
00238 if (!isValid()) return TransientVertex();
00239
00240 typedef map<reco::TransientTrack, float> TransientTrackToFloatMap;
00241
00242
00243 vector<reco::TransientTrack> ttVect;
00244 ttVect.reserve(theTracks.size());
00245 vector<reco::TransientTrack> refTTVect;
00246 TransientTrackToFloatMap theWeightMap;
00247 TTtoTTmap ttCovMap;
00248
00249
00250 for (typename vector<RefCountedVertexTrack>::const_iterator i = theTracks.begin();
00251 i != theTracks.end(); ++i) {
00252
00253
00254
00255 reco::TransientTrack t1((**i).linearizedTrack()->track());
00256 ttVect.push_back(t1);
00257
00258 theWeightMap[t1] = (**i).weight();
00259
00260
00261 if (theCovMapAvailable) {
00262 for (typename vector<RefCountedVertexTrack>::const_iterator j = (i+1);
00263 j != theTracks.end(); ++j) {
00264 reco::TransientTrack t2((**j).linearizedTrack()->track());
00265 if (t1 < t2) {
00266 ttCovMap[t1][t2] = tkToTkCovariance(*i, *j);
00267 } else {
00268 ttCovMap[t2][t1] = tkToTkCovariance(*i, *j);
00269 }
00270 }
00271 }
00272 if ((**i).refittedStateAvailable()) {
00273 refTTVect.push_back( (**i).refittedState()->transientTrack()) ;
00274 }
00275 }
00276 TransientVertex tv;
00277 if (withPrior) {
00278 tv = TransientVertex(priorVertexState(), vertexState(), ttVect, totalChiSquared(), degreesOfFreedom());
00279 } else {
00280 tv = TransientVertex(vertexState(), ttVect, totalChiSquared(), degreesOfFreedom());
00281 }
00282 tv.weightMap(theWeightMap);
00283 if (theCovMapAvailable) tv.tkToTkCovariance(ttCovMap);
00284 if (!refTTVect.empty()) tv.refittedTracks(refTTVect);
00285 return tv;
00286 }
00287
00288
00289
00290 template class CachingVertex<5>;
00291 template class CachingVertex<6>;