Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:18:26

0001 /* Copyright 2008-2010, Technische Universitaet Muenchen,
0002    Authors: Christian Hoeppner & Sebastian Neubert & Johannes Rauch
0003 
0004    This file is part of GENFIT.
0005 
0006    GENFIT is free software: you can redistribute it and/or modify
0007    it under the terms of the GNU Lesser General Public License as published
0008    by the Free Software Foundation, either version 3 of the License, or
0009    (at your option) any later version.
0010 
0011    GENFIT is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU Lesser General Public License for more details.
0015 
0016    You should have received a copy of the GNU Lesser General Public License
0017    along with GENFIT.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 
0021 #include "GFRaveConverters.h"
0022 
0023 #include "Exception.h"
0024 
0025 #include "rave/Plane.h"
0026 
0027 #include "GFRaveTrackParameters.h"
0028 
0029 #include <iostream>
0030 
0031 
0032 namespace genfit {
0033 
0034 
0035 std::vector < rave::Track >
0036 GFTracksToTracks(const std::vector < genfit::Track* >  & GFTracks,
0037     std::vector < genfit::MeasuredStateOnPlane* > * GFStates,
0038     std::map<int, genfit::trackAndState>& IdGFTrackStateMap,
0039     int startID){
0040 
0041   unsigned int ntracks(GFTracks.size());
0042 
0043   if (GFStates != nullptr)
0044     if (GFTracks.size() != GFStates->size()) {
0045       Exception exc("GFTracksToTracks ==> GFStates has not the same size as GFTracks!",__LINE__,__FILE__);
0046       throw exc;
0047     }
0048 
0049   std::vector < rave::Track > ravetracks;
0050   ravetracks.reserve(ntracks);
0051 
0052   for (unsigned int i=0; i<ntracks; ++i){
0053 
0054     if (GFTracks[i] == nullptr) {
0055       Exception exc("GFTracksToTracks ==> genfit::Track is nullptr",__LINE__,__FILE__);
0056       throw exc;
0057     }
0058 
0059     // only convert successfully fitted tracks!
0060     if (!GFTracks[i]->getFitStatus(GFTracks[i]->getCardinalRep())->isFitConverged()) continue;
0061 
0062     if (IdGFTrackStateMap.count(startID) > 0){
0063       Exception exc("GFTracksToTracks ==> IdGFTrackStateMap has already an entry for this id",__LINE__,__FILE__);
0064       throw exc;
0065     }
0066     IdGFTrackStateMap[startID].track_ = GFTracks[i];
0067     if (GFStates == nullptr)
0068       IdGFTrackStateMap[startID].state_ = new MeasuredStateOnPlane(GFTracks[i]->getFittedState()); // here clones are made so that the state of the original GFTracks and their TrackReps will not be altered by the vertexing process
0069     else
0070       IdGFTrackStateMap[startID].state_ = (*GFStates)[i];
0071 
0072     ravetracks.push_back(GFTrackToTrack(IdGFTrackStateMap[startID], startID) );
0073 
0074     ++startID;
0075   }
0076 
0077   //std::cout << "IdGFTrackStateMap size " << IdGFTrackStateMap.size() << std::endl;
0078   return ravetracks;
0079 }
0080 
0081 
0082 rave::Track
0083 GFTrackToTrack(trackAndState trackAndState, int id, std::string tag){
0084 
0085   if (trackAndState.track_ == nullptr) {
0086     Exception exc("GFTrackToTrack ==> originaltrack is nullptr",__LINE__,__FILE__);
0087     throw exc;
0088   }
0089 
0090   if (! trackAndState.track_->getFitStatus()->isFitConverged()) {
0091     Exception exc("GFTrackToTrack ==> Trackfit is not converged",__LINE__,__FILE__);
0092     throw exc;
0093   }
0094 
0095   TVector3 pos, mom;
0096   TMatrixDSym cov;
0097 
0098   trackAndState.track_->getFittedState().getPosMomCov(pos, mom, cov);
0099 
0100   // state
0101   rave::Vector6D ravestate(pos.X(), pos.Y(), pos.Z(),
0102                            mom.X(), mom.Y(), mom.Z());
0103 
0104   // covariance
0105   rave::Covariance6D ravecov(cov(0,0), cov(1,0), cov(2,0),
0106                              cov(1,1), cov(2,1), cov(2,2),
0107                              cov(3,0), cov(4,0), cov(5,0),
0108                              cov(3,1), cov(4,1), cov(5,1),
0109                              cov(3,2), cov(4,2), cov(5,2),
0110                              cov(3,3), cov(4,3), cov(5,3),
0111                              cov(4,4), cov(5,4), cov(5,5));
0112 
0113   //std::cerr<<"create rave track with id " << id << std::endl;
0114   //std::cerr<<"  pos: "; Point3DToTVector3(ravestate.position()).Print();
0115   //std::cerr<<"  mom: "; Vector3DToTVector3(ravestate.momentum()).Print();
0116 
0117   rave::Track ret(id, ravestate, ravecov,
0118       trackAndState.track_->getFitStatus()->getCharge(),
0119       trackAndState.track_->getFitStatus()->getChi2(),
0120       trackAndState.track_->getFitStatus()->getNdf(),
0121       static_cast<void*>(const_cast<Track*>(trackAndState.track_)), tag);
0122 
0123   //std::cout << "ret.originalObject() " << ret.originalObject() << "\n";
0124   //std::cout << "ret.id() " << ret.id() << "\n";
0125 
0126   return ret;
0127 }
0128 
0129 
0130 void
0131 setData(const rave::Track& orig, MeasuredStateOnPlane* state){
0132 
0133   state->setPosMomCov(TVector3(orig.state().x(), orig.state().y(), orig.state().z()),
0134                       TVector3(orig.state().px(), orig.state().py(), orig.state().pz()),
0135                       Covariance6DToTMatrixDSym(orig.error()));
0136 
0137 }
0138 
0139 
0140 GFRaveVertex*
0141 RaveToGFVertex(const rave::Vertex & raveVertex,
0142     const std::map<int, genfit::trackAndState>& IdGFTrackStateMap){
0143 
0144   if (!(raveVertex.isValid())) {
0145     Exception exc("RaveToGFVertex ==> rave Vertex is not valid!",__LINE__,__FILE__);
0146     throw exc;
0147   }
0148 
0149   std::vector < std::pair < float, rave::Track > > raveWeightedTracks(raveVertex.weightedTracks());
0150   std::vector < std::pair < float, rave::Track > > raveSmoothedTracks(raveVertex.weightedRefittedTracks());
0151 
0152   int id;
0153   unsigned int nTrks(raveWeightedTracks.size());
0154 
0155   // check if rave vertex has  refitted tracks
0156   bool smoothing(true);
0157   if (! (raveVertex.hasRefittedTracks()) ) {
0158     smoothing = false;
0159   }
0160 
0161   // check numbers of tracks and smoothed tracks
0162   if (smoothing && nTrks != raveSmoothedTracks.size()){
0163     Exception exc("RaveToGFVertex ==> number of smoothed tracks != number of tracks",__LINE__,__FILE__);
0164     throw exc;
0165   }
0166 
0167   // (smoothed) track parameters
0168   std::vector < GFRaveTrackParameters* > trackParameters;
0169   trackParameters.reserve(nTrks);
0170 
0171   // convert tracks
0172   for (unsigned int i=0; i<nTrks; ++i){
0173     id = raveWeightedTracks[i].second.id();
0174 
0175     if (IdGFTrackStateMap.count(id) == 0){
0176       Exception exc("RaveToGFVertex ==> rave track id is not present in IdGFTrackStateMap",__LINE__,__FILE__);
0177       throw exc;
0178     }
0179 
0180     GFRaveTrackParameters* trackparams;
0181 
0182     if(smoothing) {
0183       // convert smoothed track parameters
0184       trackparams = new GFRaveTrackParameters(IdGFTrackStateMap.at(id).track_, //track
0185           IdGFTrackStateMap.at(id).state_, //state
0186           raveWeightedTracks[i].first, //weight
0187           Vector6DToTVectorD(raveSmoothedTracks[i].second.state()), //smoothed state
0188           Covariance6DToTMatrixDSym(raveSmoothedTracks[i].second.error()), //smoothed cov
0189           true);
0190     }
0191     else {
0192       // convert track parameters, no smoothed tracks available
0193       trackparams = new GFRaveTrackParameters(IdGFTrackStateMap.at(id).track_, //track
0194           IdGFTrackStateMap.at(id).state_, //state
0195           raveWeightedTracks[i].first, //weight
0196           Vector6DToTVectorD(raveWeightedTracks[i].second.state()), //state
0197           Covariance6DToTMatrixDSym(raveWeightedTracks[i].second.error()), //cov
0198           false);
0199     }
0200     trackParameters.push_back(trackparams);
0201   }
0202 
0203   return new GFRaveVertex(Point3DToTVector3(raveVertex.position()),
0204                           Covariance3DToTMatrixDSym(raveVertex.error()),
0205                           trackParameters,
0206                           raveVertex.ndf(), raveVertex.chiSquared(), raveVertex.id());
0207 }
0208 
0209 void
0210 RaveToGFVertices(std::vector<GFRaveVertex*> * GFVertices,
0211     const std::vector<rave::Vertex> & raveVertices,
0212     const std::map<int, genfit::trackAndState>& IdGFTrackStateMap){
0213 
0214   unsigned int nVert(raveVertices.size());
0215 
0216   GFVertices->reserve(nVert);
0217 
0218   for (unsigned int i=0; i<nVert; ++i){
0219     GFVertices->push_back(RaveToGFVertex(raveVertices[i], IdGFTrackStateMap));
0220   }
0221 }
0222 
0223 
0224 SharedPlanePtr
0225 PlaneToGFDetPlane(const ravesurf::Plane& rplane) {
0226   return SharedPlanePtr(new DetPlane(Point3DToTVector3(rplane.position()),
0227                     Vector3DToTVector3(rplane.normalVector()) ));
0228 }
0229 
0230 
0231 TVector3
0232 Point3DToTVector3(const rave::Point3D& v) {
0233   return TVector3(v.x(), v.y(), v.z());
0234 }
0235 
0236 TVector3
0237 Vector3DToTVector3(const rave::Vector3D& v) {
0238   return TVector3(v.x(), v.y(), v.z());
0239 }
0240 
0241 
0242 TMatrixDSym
0243 Covariance3DToTMatrixDSym(const rave::Covariance3D& ravecov){
0244   TMatrixDSym cov(3);
0245 
0246   cov(0,0) = ravecov.dxx();
0247   cov(0,1) = ravecov.dxy();
0248   cov(0,2) = ravecov.dxz();
0249 
0250   cov(1,0) = ravecov.dxy();
0251   cov(1,1) = ravecov.dyy();
0252   cov(1,2) = ravecov.dyz();
0253 
0254   cov(2,0) = ravecov.dxz();
0255   cov(2,1) = ravecov.dyz();
0256   cov(2,2) = ravecov.dzz();
0257 
0258   return cov;
0259 }
0260 
0261 
0262 TVectorD
0263 Vector6DToTVectorD(const rave::Vector6D& ravevec){
0264   TVectorD vec(6);
0265 
0266   vec[0] = ravevec.x();
0267   vec[1] = ravevec.y();
0268   vec[2] = ravevec.z();
0269 
0270   vec[3] = ravevec.px();
0271   vec[4] = ravevec.py();
0272   vec[5] = ravevec.pz();
0273 
0274   return vec;
0275 }
0276 
0277 
0278 TMatrixDSym
0279 Covariance6DToTMatrixDSym(const rave::Covariance6D& ravecov){
0280   TMatrixDSym cov(6);
0281 
0282   cov(0,0) = ravecov.dxx();
0283   cov(0,1) = ravecov.dxy();
0284   cov(0,2) = ravecov.dxz();
0285   cov(0,3) = ravecov.dxpx();
0286   cov(0,4) = ravecov.dxpy();
0287   cov(0,5) = ravecov.dxpz();
0288 
0289   cov(1,0) = ravecov.dxy();
0290   cov(1,1) = ravecov.dyy();
0291   cov(1,2) = ravecov.dyz();
0292   cov(1,3) = ravecov.dypx();
0293   cov(1,4) = ravecov.dypy();
0294   cov(1,5) = ravecov.dypz();
0295 
0296   cov(2,0) = ravecov.dxz();
0297   cov(2,1) = ravecov.dyz();
0298   cov(2,2) = ravecov.dzz();
0299   cov(2,3) = ravecov.dzpx();
0300   cov(2,4) = ravecov.dzpy();
0301   cov(2,5) = ravecov.dzpz();
0302 
0303   cov(3,0) = ravecov.dxpx();
0304   cov(3,1) = ravecov.dypx();
0305   cov(3,2) = ravecov.dzpx();
0306   cov(3,3) = ravecov.dpxpx();
0307   cov(3,4) = ravecov.dpxpy();
0308   cov(3,5) = ravecov.dpxpz();
0309 
0310   cov(4,0) = ravecov.dxpy();
0311   cov(4,1) = ravecov.dypy();
0312   cov(4,2) = ravecov.dzpy();
0313   cov(4,3) = ravecov.dpxpy();
0314   cov(4,4) = ravecov.dpypy();
0315   cov(4,5) = ravecov.dpypz();
0316 
0317   cov(5,0) = ravecov.dxpz();
0318   cov(5,1) = ravecov.dypz();
0319   cov(5,2) = ravecov.dzpz();
0320   cov(5,3) = ravecov.dpxpz();
0321   cov(5,4) = ravecov.dpypz();
0322   cov(5,5) = ravecov.dpzpz();
0323 
0324   return cov;
0325 }
0326 
0327 
0328 rave::Point3D
0329 TVector3ToPoint3D(const TVector3 & vec){
0330   return rave::Point3D(vec.X(), vec.Y(), vec.Z());
0331 }
0332 
0333 
0334 rave::Covariance3D
0335 TMatrixDSymToCovariance3D(const TMatrixDSym & matrix){
0336   if (matrix.GetNrows()!=3) {
0337     Exception exc("TMatrixDSymToCovariance3D ==> TMatrixDSym is not 3x3!",__LINE__,__FILE__);
0338     throw exc;
0339   }
0340 
0341   return rave::Covariance3D(matrix(0,0), matrix(0,1), matrix(0,2),
0342                             matrix(1,1), matrix(1,2), matrix(2,2));
0343 
0344 }
0345 
0346 
0347 } // end of namespace genfit
0348