Back to home page

sPhenix code displayed by LXR

 
 

    


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

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 #include "TrackCand.h"
0021 #include "Exception.h"
0022 #include "TDatabasePDG.h"
0023 #include "IO.h"
0024 
0025 #include <algorithm>
0026 #include <utility>
0027 
0028 namespace genfit {
0029 
0030 
0031 TrackCand::TrackCand() :
0032   mcTrackId_(-1),
0033   pdg_(0),
0034   time_(0),
0035   state6D_(6),
0036   cov6D_(6),
0037   q_(0)
0038 {
0039   ;
0040 }
0041 
0042 TrackCand::~TrackCand() {
0043   for (unsigned int i=0; i<hits_.size(); ++i) {
0044     delete hits_[i];
0045   }
0046   hits_.clear();
0047 }
0048 
0049 
0050 TrackCand::TrackCand( const TrackCand& other ) :
0051   TObject(other),
0052   mcTrackId_(other.mcTrackId_),
0053   pdg_(0),
0054   time_(other.time_),
0055   state6D_(other.state6D_),
0056   cov6D_(other.cov6D_),
0057   q_(other.q_)
0058 {
0059   // deep copy
0060   hits_.reserve(other.hits_.size());
0061   for (unsigned int i=0; i<other.hits_.size(); ++i) {
0062     hits_.push_back( (other.hits_[i])->clone() );
0063   }
0064 
0065   if (other.pdg_ != 0)
0066     setPdgCode(other.pdg_);
0067 }
0068 
0069 TrackCand& TrackCand::operator=(TrackCand other) {
0070   swap(other);
0071   return *this;
0072 }
0073 
0074 
0075 void TrackCand::swap(TrackCand& other) {
0076   // by swapping the members of two classes,
0077   // the two classes are effectively swapped
0078   std::swap(this->hits_, other.hits_);
0079   std::swap(this->mcTrackId_, other.mcTrackId_);
0080   std::swap(this->pdg_, other.pdg_);
0081   std::swap(this->time_, other.time_);
0082   std::swap(this->state6D_, other.state6D_);
0083   std::swap(this->cov6D_, other.cov6D_);
0084   std::swap(this->q_, other.q_);
0085 }
0086 
0087 
0088 TrackCandHit* TrackCand::getHit(int i) const {
0089   if (i < 0)
0090     i += hits_.size();
0091 
0092   return hits_.at(i);
0093 }
0094 
0095 
0096 void TrackCand::getHit(int i, int& detId, int& hitId) const {
0097   if (i < 0)
0098     i += hits_.size();
0099 
0100   detId = hits_.at(i)->getDetId();
0101   hitId = hits_[i]->getHitId();
0102 }
0103 
0104 
0105 void TrackCand::getHit(int i, int& detId, int& hitId, double& sortingParameter) const {
0106   if (i < 0)
0107     i += hits_.size();
0108 
0109   detId = hits_.at(i)->getDetId();
0110   hitId = hits_[i]->getHitId();
0111   sortingParameter = hits_[i]->getSortingParameter();
0112 }
0113 
0114 
0115 void TrackCand::getHitWithPlane(int i, int& detId, int& hitId, int& planeId) const {
0116   if (i < 0)
0117     i += hits_.size();
0118 
0119   detId = hits_.at(i)->getDetId();
0120   hitId = hits_[i]->getHitId();
0121   planeId = hits_[i]->getPlaneId();
0122 }
0123 
0124 
0125 void TrackCand::addHit(int detId, int hitId, int planeId, double sortingParameter)
0126 {
0127   hits_.push_back(new TrackCandHit(detId, hitId, planeId, sortingParameter));
0128 }
0129 
0130 std::vector<int> TrackCand::getHitIDs(int detId) const {
0131   std::vector<int> result;
0132   for(unsigned int i=0; i<hits_.size(); ++i){
0133     if(detId==-2 || hits_[i]->getDetId() == detId) {
0134       result.push_back(hits_[i]->getHitId());
0135     }
0136   }
0137   return result;
0138 }
0139 
0140 std::vector<int> TrackCand::getDetIDs() const {
0141   std::vector<int> result;
0142   for(unsigned int i=0; i<hits_.size(); ++i){
0143     result.push_back(hits_[i]->getDetId());
0144   }
0145   return result;
0146 }
0147 
0148 std::vector<double> TrackCand::getSortingParameters() const {
0149   std::vector<double> result;
0150   for(unsigned int i=0; i<hits_.size(); ++i){
0151     result.push_back(hits_[i]->getSortingParameter());
0152   }
0153   return result;
0154 }
0155 
0156 std::set<int> TrackCand::getUniqueDetIDs() const {
0157   std::set<int> retVal;
0158   for (unsigned int i = 0; i < hits_.size(); ++i) {
0159     retVal.insert(hits_[i]->getDetId());
0160   }
0161   return retVal;
0162 }
0163 
0164 
0165 void TrackCand::setPdgCode(int pdgCode) {
0166   pdg_ = pdgCode;
0167   TParticlePDG* part = TDatabasePDG::Instance()->GetParticle(pdg_);
0168   q_ = part->Charge() / (3.);
0169 }
0170 
0171 
0172 void TrackCand::reset()
0173 {
0174   for (unsigned int i=0; i<hits_.size(); ++i) {
0175     delete hits_[i];
0176   }
0177   hits_.clear();
0178 }
0179 
0180 
0181 bool TrackCand::hitInTrack(int detId, int hitId) const
0182 {
0183   for (unsigned int i = 0; i < hits_.size(); ++i){
0184     if (detId == hits_[i]->getDetId() && hitId == hits_[i]->getHitId())
0185       return true;
0186   }
0187   return false;
0188 }
0189 
0190 
0191 bool operator== (const TrackCand& lhs, const TrackCand& rhs){
0192   if(lhs.getNHits() != rhs.getNHits()) return false;
0193   for (unsigned int i = 0; i < lhs.getNHits(); ++i){
0194     // use == operator of the TrackCandHits
0195     if (*(lhs.getHit(i)) != *(rhs.getHit(i)))
0196       return false;
0197   }
0198   return true;
0199 }
0200 
0201 
0202 void TrackCand::Print(const Option_t* option) const {
0203   printOut << "======== TrackCand::print ========\n";
0204   printOut << "mcTrackId=" << mcTrackId_ << "\n";
0205   printOut << "seed values for 6D state: \n";
0206   state6D_.Print(option);
0207   printOut << "charge = " << q_ << "\n";
0208   printOut << "PDG code = " << pdg_ << "\n";
0209   for(unsigned int i=0; i<hits_.size(); ++i){
0210     hits_[i]->Print();
0211   }
0212 }
0213 
0214 
0215 void TrackCand::append(const TrackCand& rhs){
0216   for(unsigned int i=0; i<rhs.getNHits(); ++i){
0217     addHit(rhs.getHit(i)->clone());
0218   }
0219 }
0220 
0221 
0222 void TrackCand::sortHits(){
0223   std::stable_sort(hits_.begin(), hits_.end(), compareTrackCandHits);
0224 }
0225 
0226 
0227 void TrackCand::sortHits(const std::vector<unsigned int>& indices){
0228 
0229   const unsigned int nHits(getNHits());
0230   if (indices.size() != nHits){
0231     abort();
0232     Exception exc("TrackCand::sortHits ==> Size of indices != number of hits!",__LINE__,__FILE__);
0233     throw exc;
0234   }
0235 
0236   //these containers will hold the sorted results. They are created to avoid probably slower in-place sorting
0237   std::vector<TrackCandHit*> sortedHits(nHits);
0238   for (unsigned int i=0; i<nHits; ++i){
0239     sortedHits[i] = hits_[indices[i]];
0240   }
0241   //write the changes back to the private data members:
0242   hits_ = sortedHits;
0243 }
0244 
0245 
0246 void TrackCand::set6DSeed(const TVectorD& state6D, const double charge) {
0247   if (pdg_ != 0 && q_ != charge)
0248     pdg_ = -pdg_;
0249   q_ = charge;
0250   state6D_ = state6D;
0251 }
0252 
0253 void TrackCand::set6DSeedAndPdgCode(const TVectorD& state6D, const int pdgCode) {
0254   setPdgCode(pdgCode);
0255   state6D_ = state6D;
0256 }
0257 
0258 void TrackCand::setPosMomSeed(const TVector3& pos, const TVector3& mom, const double charge) {
0259   if (pdg_ != 0 && q_ != charge)
0260     pdg_ = -pdg_;
0261   q_ = charge;
0262   state6D_[0] = pos[0];  state6D_[1] = pos[1];  state6D_[2] = pos[2];
0263   state6D_[3] = mom[0];  state6D_[4] = mom[1];  state6D_[5] = mom[2];
0264 }
0265 
0266 void TrackCand::setPosMomSeedAndPdgCode(const TVector3& pos, const TVector3& mom, const int pdgCode) {
0267   setPdgCode(pdgCode);
0268   state6D_[0] = pos[0];  state6D_[1] = pos[1];  state6D_[2] = pos[2];
0269   state6D_[3] = mom[0];  state6D_[4] = mom[1];  state6D_[5] = mom[2];
0270 }
0271 
0272 
0273 void TrackCand::setTime6DSeed(double time, const TVectorD& state6D, const double charge)
0274 {
0275   time_ = time;
0276   set6DSeed(state6D, charge);
0277 }
0278 
0279 void TrackCand::setTime6DSeedAndPdgCode(double time, const TVectorD& state6D, const int pdgCode)
0280 {
0281   time_ = time;
0282   set6DSeedAndPdgCode(state6D, pdgCode);
0283 }
0284 
0285 void TrackCand::setTimePosMomSeed(double time, const TVector3& pos,
0286                   const TVector3& mom, const double charge)
0287 {
0288   time_ = time;
0289   setPosMomSeed(pos, mom, charge);
0290 }
0291 
0292 void TrackCand::setTimePosMomSeedAndPdgCode(double time, const TVector3& pos,
0293                         const TVector3& mom, const int pdgCode)
0294 {
0295   time_ = time;
0296   setPosMomSeedAndPdgCode(pos, mom, pdgCode);
0297 }
0298 
0299 
0300 } /* End of namespace genfit */