CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/RecoVertex/VertexPrimitives/src/CachingVertex.cc

Go to the documentation of this file.
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 //to be removed
00009 template <unsigned int N>
00010 CachingVertex<N>::CachingVertex(const GlobalPoint & pos, 
00011                              const GlobalError & posErr, 
00012                              const std::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 //to be removed
00023 template <unsigned int N>
00024 CachingVertex<N>::CachingVertex(const GlobalPoint & pos, 
00025                              const GlobalWeight & posWeight, 
00026                              const std::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 //to be removed
00036 template <unsigned int N>
00037 CachingVertex<N>::CachingVertex(const AlgebraicVector3 & weightTimesPosition, 
00038                              const GlobalWeight & posWeight, 
00039                              const std::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 std::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 std::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 std::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 //to be removed
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 std::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 //to be removed
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 std::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 //to be removed
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 std::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 std::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 std::vector<RefCountedVertexTrack>::const_iterator itk = theTracks.begin(); 
00189        itk != theTracks.end(); ++itk) {
00190     theNDF += (**itk).weight(); // adds up weights
00191   }
00192   theNDF *= 2.; // times 2df for each track
00193   if (!withPrior) theNDF -= 3.; // 3 position coordinates fitted
00194   theNDFAvailable = true;
00195 }
00196 
00197 
00198 template <unsigned int N>
00199 typename CachingVertex<N>::AlgebraicMatrixMM
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     bool transp = false;
00210     if(t1 < t2) {
00211       tr1 = t1;    
00212       tr2 = t2;
00213     }
00214     else {
00215       tr1 = t2;    
00216       tr2 = t1;
00217       transp = true;
00218     }
00219     typename TrackToTrackMap::const_iterator it = theCovMap.find(tr1);
00220     if (it !=  theCovMap.end()) {
00221       const TrackMap & tm = it->second;
00222       typename TrackMap::const_iterator nit = tm.find(tr2);
00223       if (nit != tm.end()) {
00224         if (transp) return( ROOT::Math::Transpose(nit->second) );
00225         else return( nit->second);
00226       }
00227       else {
00228         throw VertexException("CachingVertex::requested TkTkCovariance does not exist");
00229       }       
00230     }
00231     else {
00232       throw VertexException("CachingVertex::requested TkTkCovariance does not exist");
00233     }     
00234   }
00235 }
00236 
00237 template <unsigned int N>
00238 CachingVertex<N>::operator TransientVertex() const
00239 {
00240   //If the vertex is invalid, return an invalid TV !
00241   if (!isValid()) return TransientVertex();
00242 
00243   typedef std::map<reco::TransientTrack, float> TransientTrackToFloatMap;
00244 
00245 // Construct Track vector
00246   std::vector<reco::TransientTrack> ttVect;
00247   ttVect.reserve(theTracks.size());
00248   std::vector<reco::TransientTrack> refTTVect;
00249   TransientTrackToFloatMap theWeightMap;
00250   TTtoTTmap ttCovMap;
00251   // float theMinWeight = 0.5;
00252 
00253   for (typename std::vector<RefCountedVertexTrack>::const_iterator i = theTracks.begin();
00254        i != theTracks.end(); ++i) {
00255     // discard tracks with too low weight
00256     // if ((**i).weight() < theMinWeight) continue;
00257 
00258     reco::TransientTrack t1((**i).linearizedTrack()->track());
00259     ttVect.push_back(t1);
00260     //Fill in the weight map
00261     theWeightMap[t1] = (**i).weight();
00262 
00263     //Fill in the tk-to-tk covariance map
00264     if (theCovMapAvailable) {
00265       for (typename std::vector<RefCountedVertexTrack>::const_iterator j = (i+1);
00266            j != theTracks.end(); ++j) {
00267         reco::TransientTrack t2((**j).linearizedTrack()->track());
00268         ttCovMap[t1][t2] = tkToTkCovariance(*i, *j);
00269       }
00270     }
00271     if ((**i).refittedStateAvailable()) {
00272       refTTVect.push_back( (**i).refittedState()->transientTrack()) ;
00273     }
00274   }
00275   TransientVertex tv;
00276   if (withPrior) {
00277     tv =  TransientVertex(priorVertexState(), vertexState(), ttVect, totalChiSquared(), degreesOfFreedom());
00278   } else {
00279     tv = TransientVertex(vertexState(), ttVect, totalChiSquared(), degreesOfFreedom());
00280   }
00281   tv.weightMap(theWeightMap);
00282   if (theCovMapAvailable) tv.tkToTkCovariance(ttCovMap);
00283   if (!refTTVect.empty()) tv.refittedTracks(refTTVect);
00284   return tv;
00285 }
00286 
00287 
00288 
00289 template class CachingVertex<5>;
00290 template class CachingVertex<6>;