EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GFTrackCand.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GFTrackCand.cxx
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert
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 #include "GFTrackCand.h"
20 #include "GFException.h"
21 #include "TDatabasePDG.h"
22 #include <algorithm>
23 #include <iostream>
24 #include <utility>
25 
27 
28 GFTrackCand::GFTrackCand():fCurv(0),fDip(0), fQ(0),fMcTrackId(-1),fPdg(0), fState6D(TMatrixD(TMatrixD::kZero,TMatrixD(6,1))),fCov6D(-1.0*TMatrixD(TMatrixD::kUnit,TMatrixD(6,6))){}
29 
30 GFTrackCand::~GFTrackCand(){} //no members are pointers
31 
32 void
33 GFTrackCand::addHit(unsigned int detId, unsigned int hitId, double rho, unsigned int planeId)
34 {
35  fDetId.push_back(detId);
36  fHitId.push_back(hitId);
37  fPlaneId.push_back(planeId);
38  fRho.push_back(rho);
39 }
40 
41 std::vector<unsigned int>
42 GFTrackCand::getHitIDs(int detId) const {
43  if(detId<0){ // return hits from all detectors
44  return fHitId;
45  }
46  else {
47  std::vector<unsigned int> result;
48  unsigned int n=fHitId.size();
49  for(unsigned int i=0;i<n;++i){
50  if(fDetId[i]==(unsigned int)detId)result.push_back(fHitId[i]);
51  }
52  return result;
53  }
54 }
55 
56 std::vector<unsigned int>
57 GFTrackCand::GetHitIDs(int detId) const {
58  std::cerr << "the method GFTrackCand::GetHitIDs is deprecated. Use GFTrackCand::getHitIDs instead\n";
59  if(detId<0){ // return hits from all detectors
60  return fHitId;
61  }
62  else {
63  std::vector<unsigned int> result;
64  unsigned int n=fHitId.size();
65  for(unsigned int i=0;i<n;++i){
66  if(fDetId[i]==(unsigned int)detId)result.push_back(fHitId[i]);
67  }
68  return result;
69  }
70 }
71 
72 void
74 {
75  fDetId.clear();fHitId.clear();
76 }
77 
78 bool GFTrackCand::hitInTrack(unsigned int detId, unsigned int hitId) const
79 {
80  for (unsigned int i = 0; i < fDetId.size(); i++){
81  if (detId == fDetId[i])
82  if (hitId == fHitId[i])
83  return true;
84  }
85  return false;
86 }
87 
88 bool operator== (const GFTrackCand& lhs, const GFTrackCand& rhs){
89  if(lhs.getNHits()!=rhs.getNHits()) return false;
90  bool result=std::equal(lhs.fDetId.begin(),lhs.fDetId.end(),rhs.fDetId.begin());
91  result &=std::equal(lhs.fHitId.begin(),lhs.fHitId.end(),rhs.fHitId.begin());
92  return result;
93 }
94 
95 void GFTrackCand::Print(const Option_t* option) const {
96  std::cout << "======== GFTrackCand::print ========\n";
97  std::cout << "mcTrackId=" << fMcTrackId << "\n";
98  std::cout << "seed values for 6D state and cov: " << std::endl;
99  fState6D.Print(option);
100  fCov6D.Print(option);
101  std::cout << "q" << fQ << "\n";
102  std::cout << "PDG code= " << fPdg << "\n";
103  assert(fDetId.size()==fHitId.size());
104  std::cout << "detId|hitId|rho ";
105  for(unsigned int i=0;i<fDetId.size();++i){
106  std::cout << fDetId.at(i) << "|" << fHitId.at(i)
107  << "|" << fRho.at(i) << " ";
108  }
109  std::cout << std::endl;
110 }
111 
113  unsigned int detId,hitId;
114  double rho;
115  for(unsigned int i=0;i<rhs.getNHits();++i){
116  rhs.getHit(i,detId,hitId,rho);
117  addHit(detId,hitId,rho);
118  }
119 }
120 
121 void GFTrackCand::setComplTrackSeed(const TVector3& pos, const TVector3& mom, const int pdgCode, TVector3 posError, TVector3 momError){
122  std::cerr << "the method GFTrackCand::setComplTrackSeed is deprecated. Use GFTrackCand::set6DSeed() or instead\n";
123  setPdgCode(pdgCode); //also sets charge
124  fState6D(0,0) = pos[0];
125  fState6D(1,0) = pos[1];
126  fState6D(2,0) = pos[2];
127  fState6D(3,0) = mom[0];
128  fState6D(4,0) = mom[1];
129  fState6D(5,0) = mom[2];
130  fCov6D(0,0) = posError[0]*posError[0];
131  fCov6D(1,1) = posError[1]*posError[1];
132  fCov6D(2,2) = posError[2]*posError[2];
133  fCov6D(3,3) = momError[0]*momError[0];
134  fCov6D(4,4) = momError[1]*momError[1];
135  fCov6D(5,5) = momError[2]*momError[2];
136 }
137 
138 
140  const unsigned int nHits = getNHits(); // all 4 private vectors must have the same size.
141 
142  //a vector that will be sorted to give after sort indices to sort the other vectors
143  std::vector<std::pair<double, int> > order(nHits);
144  for (unsigned int i = 0; i != nHits; ++i){
145  order[i] = std::make_pair(fRho[i],i);
146  }
147  std::stable_sort(order.begin(), order.end()); // by default sort uses the ".first" value of the pair when sorting a std container of pairs
148 
149  std::vector<unsigned int> indices(nHits);
150  for (unsigned int i = 0; i != nHits; ++i){
151  indices[i] = order[i].second;
152  }
153 
154  sortHits(indices);
155 }
156 
157 
158 void GFTrackCand::sortHits(std::vector<unsigned int> indices){
159 
160  const unsigned int nHits(getNHits());
161  if (indices.size() != nHits){
162  abort();
163  GFException exc("GFTrackCand::sortHits ==> Size of indices != number of hits!",__LINE__,__FILE__);
164  throw exc;
165  }
166 
167  //these containers will hold the sorted results. They are created to avoid probably slower in-place sorting
168  std::vector<unsigned int> sortedDetId(nHits);
169  std::vector<unsigned int> sortedHitId(nHits);
170  std::vector<unsigned int> sortedPlaneId(nHits);
171  std::vector<double> sortedRho(nHits);
172  for (unsigned int i = 0; i != nHits; ++i){
173  unsigned int sortIndex = indices[i];
174  sortedDetId[i] = fDetId[sortIndex];
175  sortedHitId[i] = fHitId[sortIndex];
176  sortedPlaneId[i] = fPlaneId[sortIndex];
177  sortedRho[i] = fRho[sortIndex];
178  }
179  //write the changes back to the private data members:
180  fDetId = sortedDetId;
181  fHitId = sortedHitId;
182  fPlaneId = sortedPlaneId;
183  fRho = sortedRho;
184 }