EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackCand.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackCand.cc
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert & Johannes Rauch
3 
4  This file is part of GENFIT.
5 
6  GENFIT is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  GENFIT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "TrackCand.h"
21 #include "Exception.h"
22 #include "TDatabasePDG.h"
23 #include "IO.h"
24 
25 #include <algorithm>
26 #include <utility>
27 
28 namespace genfit {
29 
30 
32  mcTrackId_(-1),
33  pdg_(0),
34  time_(0),
35  state6D_(6),
36  cov6D_(6),
37  q_(0)
38 {
39  ;
40 }
41 
43  for (unsigned int i=0; i<hits_.size(); ++i) {
44  delete hits_[i];
45  }
46  hits_.clear();
47 }
48 
49 
51  TObject(other),
52  mcTrackId_(other.mcTrackId_),
53  pdg_(0),
54  time_(other.time_),
55  state6D_(other.state6D_),
56  cov6D_(other.cov6D_),
57  q_(other.q_)
58 {
59  // deep copy
60  hits_.reserve(other.hits_.size());
61  for (unsigned int i=0; i<other.hits_.size(); ++i) {
62  hits_.push_back( (other.hits_[i])->clone() );
63  }
64 
65  if (other.pdg_ != 0)
66  setPdgCode(other.pdg_);
67 }
68 
70  swap(other);
71  return *this;
72 }
73 
74 
75 void TrackCand::swap(TrackCand& other) {
76  // by swapping the members of two classes,
77  // the two classes are effectively swapped
78  std::swap(this->hits_, other.hits_);
79  std::swap(this->mcTrackId_, other.mcTrackId_);
80  std::swap(this->pdg_, other.pdg_);
81  std::swap(this->time_, other.time_);
82  std::swap(this->state6D_, other.state6D_);
83  std::swap(this->cov6D_, other.cov6D_);
84  std::swap(this->q_, other.q_);
85 }
86 
87 
89  if (i < 0)
90  i += hits_.size();
91 
92  return hits_.at(i);
93 }
94 
95 
96 void TrackCand::getHit(int i, int& detId, int& hitId) const {
97  if (i < 0)
98  i += hits_.size();
99 
100  detId = hits_.at(i)->getDetId();
101  hitId = hits_[i]->getHitId();
102 }
103 
104 
105 void TrackCand::getHit(int i, int& detId, int& hitId, double& sortingParameter) const {
106  if (i < 0)
107  i += hits_.size();
108 
109  detId = hits_.at(i)->getDetId();
110  hitId = hits_[i]->getHitId();
111  sortingParameter = hits_[i]->getSortingParameter();
112 }
113 
114 
115 void TrackCand::getHitWithPlane(int i, int& detId, int& hitId, int& planeId) const {
116  if (i < 0)
117  i += hits_.size();
118 
119  detId = hits_.at(i)->getDetId();
120  hitId = hits_[i]->getHitId();
121  planeId = hits_[i]->getPlaneId();
122 }
123 
124 
125 void TrackCand::addHit(int detId, int hitId, int planeId, double sortingParameter)
126 {
127  hits_.push_back(new TrackCandHit(detId, hitId, planeId, sortingParameter));
128 }
129 
130 std::vector<int> TrackCand::getHitIDs(int detId) const {
131  std::vector<int> result;
132  for(unsigned int i=0; i<hits_.size(); ++i){
133  if(detId==-2 || hits_[i]->getDetId() == detId) {
134  result.push_back(hits_[i]->getHitId());
135  }
136  }
137  return result;
138 }
139 
140 std::vector<int> TrackCand::getDetIDs() const {
141  std::vector<int> result;
142  for(unsigned int i=0; i<hits_.size(); ++i){
143  result.push_back(hits_[i]->getDetId());
144  }
145  return result;
146 }
147 
148 std::vector<double> TrackCand::getSortingParameters() const {
149  std::vector<double> result;
150  for(unsigned int i=0; i<hits_.size(); ++i){
151  result.push_back(hits_[i]->getSortingParameter());
152  }
153  return result;
154 }
155 
156 std::set<int> TrackCand::getUniqueDetIDs() const {
157  std::set<int> retVal;
158  for (unsigned int i = 0; i < hits_.size(); ++i) {
159  retVal.insert(hits_[i]->getDetId());
160  }
161  return retVal;
162 }
163 
164 
165 void TrackCand::setPdgCode(int pdgCode) {
166  pdg_ = pdgCode;
167  TParticlePDG* part = TDatabasePDG::Instance()->GetParticle(pdg_);
168  q_ = part->Charge() / (3.);
169 }
170 
171 
173 {
174  for (unsigned int i=0; i<hits_.size(); ++i) {
175  delete hits_[i];
176  }
177  hits_.clear();
178 }
179 
180 
181 bool TrackCand::hitInTrack(int detId, int hitId) const
182 {
183  for (unsigned int i = 0; i < hits_.size(); ++i){
184  if (detId == hits_[i]->getDetId() && hitId == hits_[i]->getHitId())
185  return true;
186  }
187  return false;
188 }
189 
190 
191 bool operator== (const TrackCand& lhs, const TrackCand& rhs){
192  if(lhs.getNHits() != rhs.getNHits()) return false;
193  for (unsigned int i = 0; i < lhs.getNHits(); ++i){
194  // use == operator of the TrackCandHits
195  if (*(lhs.getHit(i)) != *(rhs.getHit(i)))
196  return false;
197  }
198  return true;
199 }
200 
201 
202 void TrackCand::Print(const Option_t* option) const {
203  printOut << "======== TrackCand::print ========\n";
204  printOut << "mcTrackId=" << mcTrackId_ << "\n";
205  printOut << "seed values for 6D state: \n";
206  state6D_.Print(option);
207  printOut << "charge = " << q_ << "\n";
208  printOut << "PDG code = " << pdg_ << "\n";
209  for(unsigned int i=0; i<hits_.size(); ++i){
210  hits_[i]->Print();
211  }
212 }
213 
214 
215 void TrackCand::append(const TrackCand& rhs){
216  for(unsigned int i=0; i<rhs.getNHits(); ++i){
217  addHit(rhs.getHit(i)->clone());
218  }
219 }
220 
221 
223  std::stable_sort(hits_.begin(), hits_.end(), compareTrackCandHits);
224 }
225 
226 
227 void TrackCand::sortHits(const std::vector<unsigned int>& indices){
228 
229  const unsigned int nHits(getNHits());
230  if (indices.size() != nHits){
231  abort();
232  Exception exc("TrackCand::sortHits ==> Size of indices != number of hits!",__LINE__,__FILE__);
233  throw exc;
234  }
235 
236  //these containers will hold the sorted results. They are created to avoid probably slower in-place sorting
237  std::vector<TrackCandHit*> sortedHits(nHits);
238  for (unsigned int i=0; i<nHits; ++i){
239  sortedHits[i] = hits_[indices[i]];
240  }
241  //write the changes back to the private data members:
242  hits_ = sortedHits;
243 }
244 
245 
246 void TrackCand::set6DSeed(const TVectorD& state6D, const double charge) {
247  if (pdg_ != 0 && q_ != charge)
248  pdg_ = -pdg_;
249  q_ = charge;
250  state6D_ = state6D;
251 }
252 
253 void TrackCand::set6DSeedAndPdgCode(const TVectorD& state6D, const int pdgCode) {
254  setPdgCode(pdgCode);
255  state6D_ = state6D;
256 }
257 
258 void TrackCand::setPosMomSeed(const TVector3& pos, const TVector3& mom, const double charge) {
259  if (pdg_ != 0 && q_ != charge)
260  pdg_ = -pdg_;
261  q_ = charge;
262  state6D_[0] = pos[0]; state6D_[1] = pos[1]; state6D_[2] = pos[2];
263  state6D_[3] = mom[0]; state6D_[4] = mom[1]; state6D_[5] = mom[2];
264 }
265 
266 void TrackCand::setPosMomSeedAndPdgCode(const TVector3& pos, const TVector3& mom, const int pdgCode) {
267  setPdgCode(pdgCode);
268  state6D_[0] = pos[0]; state6D_[1] = pos[1]; state6D_[2] = pos[2];
269  state6D_[3] = mom[0]; state6D_[4] = mom[1]; state6D_[5] = mom[2];
270 }
271 
272 
273 void TrackCand::setTime6DSeed(double time, const TVectorD& state6D, const double charge)
274 {
275  time_ = time;
276  set6DSeed(state6D, charge);
277 }
278 
279 void TrackCand::setTime6DSeedAndPdgCode(double time, const TVectorD& state6D, const int pdgCode)
280 {
281  time_ = time;
282  set6DSeedAndPdgCode(state6D, pdgCode);
283 }
284 
285 void TrackCand::setTimePosMomSeed(double time, const TVector3& pos,
286  const TVector3& mom, const double charge)
287 {
288  time_ = time;
289  setPosMomSeed(pos, mom, charge);
290 }
291 
292 void TrackCand::setTimePosMomSeedAndPdgCode(double time, const TVector3& pos,
293  const TVector3& mom, const int pdgCode)
294 {
295  time_ = time;
296  setPosMomSeedAndPdgCode(pos, mom, pdgCode);
297 }
298 
299 
300 } /* End of namespace genfit */