EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GFTrack.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GFTrack.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 <assert.h>
20 #include <iostream>
21 
22 #include "GFTrack.h"
23 #include "GFException.h"
24 #include "GFAbsRecoHitComparator.h"
25 #include "TVirtualGeoTrack.h"
26 
27 GFTrack::GFTrack(GFAbsTrackRep* defaultRep, bool smooth)
28  : fTrackReps(NULL),fCardinal_rep(0), fNextHitToFit(0), fSmooth(false)
29 {
30  addTrackRep(defaultRep);
31  fSmooth = smooth;
32  fSmoothFast = false;
33 }
34 
36  : fTrackReps(NULL), fCardinal_rep(0), fNextHitToFit(0), fSmooth(false)
37 {
38  //trackReps = new TObjArray(defNumTrackReps);
39 }
40 
42  if(fTrackReps!=NULL){
43  for(unsigned int i=0;i<getNumReps();i++) {
44  delete fTrackReps->At(i);
45  }
46  delete fTrackReps;
47  }
48  for(unsigned int i=0;i<fHits.size();i++) {
49  if(fHits.at(i)!=NULL) delete fHits.at(i);
50  }
51  for(unsigned int i=0;i<fBookkeeping.size();++i){
52  if(fBookkeeping.at(i)!=NULL) delete fBookkeeping.at(i);
53  }
54 }
55 
57  fCand=_tr.fCand;
60  fSmooth=_tr.fSmooth;
62  for(unsigned int i=0;i<_tr.getNumHits();i++) {
63  fHits.push_back((_tr.getHit(i))->clone());
64  }
65  fTrackReps = NULL;
66  for(unsigned int i=0; i<_tr.getNumReps();i++) {
67  addTrackRep( (_tr.getTrackRep(i))->clone() );
68  }
69  for(unsigned int i=0; i<fBookkeeping.size(); ++i) delete fBookkeeping[i];
70  fBookkeeping.clear();
71 
72  for(unsigned int i=0;i<_tr.fBookkeeping.size();++i){
73  assert(_tr.fBookkeeping.at(i)!= NULL) ;
74  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
75  }
76  fRepAtHit = _tr.fRepAtHit;
77 }
78 
80  if(fTrackReps!=NULL){
81  for(unsigned int i=0;i<getNumReps();i++) {
82  delete fTrackReps->At(i);
83  }
84  delete fTrackReps;
85  fTrackReps=NULL;
86  }
87  for(unsigned int i=0;i<fHits.size();i++) {
88  delete fHits[i];
89  }
90  for(unsigned int i=0;i<fBookkeeping.size();++i){
91  if(fBookkeeping.at(i)!=NULL) delete fBookkeeping.at(i);
92  }
93 
94  for(unsigned int i=0;i<_tr.getNumReps();++i){
95  addTrackRep(_tr.getTrackRep(i)->clone());
96  }
97  fCand=_tr.fCand;
100  fSmooth=_tr.fSmooth;
102  for(unsigned int i=0;i<_tr.getNumHits();i++) {
103  fHits.push_back((_tr.getHit(i))->clone());
104  }
105 
106  //clear the empty bookeeping objs made by addTrackRep and copy the others
107  for(unsigned int i=0; i<fBookkeeping.size(); ++i) delete fBookkeeping[i];
108  fBookkeeping.clear();
109  for(unsigned int i=0;i<_tr.fBookkeeping.size();++i){
110  assert(_tr.fBookkeeping.at(i)!= NULL) ;
111  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
112  }
113  fRepAtHit = _tr.fRepAtHit;
114 
115 
116  return *this;
117 }
118 
119 
120 void
122  if(fTrackReps!=NULL){
123  for(unsigned int i=0;i<getNumReps();i++) {
124  if(fTrackReps->At(i)!=NULL) delete fTrackReps->At(i);
125  }
126  }
127  for(unsigned int i=0;i<fBookkeeping.size();++i){
128  if(fBookkeeping.at(i)!=NULL) delete fBookkeeping.at(i);
129  }
130  for(unsigned int i=0;i<fHits.size();i++) {
131  if(fHits.at(i)!=NULL) delete fHits.at(i);
132  }
133  fHits.clear();
134  fRepAtHit.clear();
135  fBookkeeping.clear();
136 }
137 
138 void
140  unsigned int nhits=trk->getNumHits();
141  for(unsigned int i=0;i<nhits;++i){
142  unsigned int detId;
143  unsigned int hitId;
144  trk->getCand().getHit(i,detId,hitId);
145  GFAbsRecoHit* hit=trk->getHit(i);
146  addHit(hit,detId,hitId);
147  }
148  trk->fHits.clear();
149 }
150 
151 
153 
154  unsigned int nHits(getNumHits());
155 
156  std::vector< std::pair<unsigned int, GFAbsRecoHit*> > pv;
157  pv.reserve(nHits);
158 
159  for (unsigned int i=0; i<nHits; ++i) {
160  pv.push_back( std::pair<unsigned int, GFAbsRecoHit*>(i, fHits[i]) ) ;
161  }
162 
163  std::stable_sort(pv.begin(), pv.end(), GFAbsRecoHitComparator());
164 
165  // get the indices -> now we know at which position which hit is
166  std::vector<unsigned int> indices;
167  indices.reserve(nHits);
168  for (unsigned int i=0; i<nHits; ++i){
169  indices.push_back(pv[i].first);
170  }
171 
172  // now we have to do the actual sorting of everything
173 
174  // sort fHits
175  std::vector<GFAbsRecoHit*> sortedHits;
176  sortedHits.reserve(nHits);
177  for (unsigned int i=0; i<nHits; ++i){
178  sortedHits.push_back(fHits[indices[i]]);
179  }
180  fHits = sortedHits;
181 
182  // sort trackCand
183  if (nHits == fCand.getNHits()){
184  fCand.sortHits(indices);
185  }
186  else {
187  GFException exc("GFTrack::sortHits ==> Cannot sort GFTrackCand accordingly since it has not the same number of hits as the GFTrack.",__LINE__,__FILE__);
188  throw exc;
189  }
190 
191  // sorting the bookkeeping is not yet supported (and probably wouldn't make sense either)
193 
194  // update other member variables
195  for (unsigned int i=0; i<fRepAtHit.size(); ++i){
196  fRepAtHit[i] = std::find(indices.begin(), indices.end(), fRepAtHit[i]) - indices.begin();
197  }
198 
199  // reset fNextHitToFit
200  fNextHitToFit = 0;
201 
202 }
203 
204 
205 void
206 GFTrack::setCandidate(const GFTrackCand& cand, bool doreset)
207 {
208  fCand=cand;
209  // reset fits
210  if(doreset) {
211  for(unsigned int i=0;i<getNumReps();i++) {
212  ((GFAbsTrackRep*)fTrackReps->At(i))->reset();
213  }
214  }
215 }
216 
217 void
218 GFTrack::fillGeoTrack(TVirtualGeoTrack* geotrk,unsigned int repid) const
219 {
220  GFAbsTrackRep* rep=getTrackRep(repid);
221  unsigned int n=fCand.getNHits();
222  rep->getState().Print();
223  for(unsigned int i=0; i<n; ++i){// loop over hits
224  GFDetPlane pl=fHits[i]->getDetPlane(rep);
225  TVector3 pos=rep->getPos(pl);
226  std::cout<<pos.X()<<","<<pos.Y()<<","<<pos.Z()<<std::endl;
227  geotrk->AddPoint(pos.X(),pos.Y(),pos.Z(),0);
228  }// end loop over hits
229 }
230 
231 
232 void
233 GFTrack::getResiduals(unsigned int detId, // which detector?
234  unsigned int dim, // which projection?
235  unsigned int repid, // which trackrep ?
236  std::vector<double>& result)
237 {
238 
239  unsigned int nhits=getNumHits();
240  if(repid>=getNumReps())return;
241  GFAbsTrackRep* rep=getTrackRep(repid);//->clone();
242  assert(rep->getState()==getTrackRep(repid)->getState());
243  for(unsigned int ih=0; ih<nhits; ++ih){// loop over hits
244  unsigned int anid;
245  unsigned int dummy;
246  fCand.getHit(ih,anid,dummy); // check if this is a hit we want to look at
247  if(anid==detId){
248  GFAbsRecoHit* hit=getHit(ih);
249  // extrapolate trackrep there
250  int repDim=rep->getDim();
251  TMatrixT<double> state(repDim,1);
252  TMatrixT<double> cov(repDim,repDim);
253  GFDetPlane pl=hit->getDetPlane(rep);
254 
255  rep->extrapolate(pl,state,cov);
256  //rep->setState(state);
257  //rep->setReferencePlane(pl);
258 
259  TMatrixT<double> H = hit->getHMatrix(rep);
260  TMatrixT<double> m,V;
261  hit->getMeasurement(rep,pl,state,cov,m,V);
262  double res=(m-(H*state))[dim][0];;
263 
264  //std::cout<<res<<std::endl;
265 
266  result.push_back(res);
267  }
268  }
269 }
270 
271 
273  std::cout << "GFTrack::printBookkeeping()" << std::endl;
274  for(unsigned int i=0;i<getNumReps();++i){
275  std::cout << "trackRep " << i << ":" << std::endl;
276  fBookkeeping.at(i)->Print();
277  }
278 
279 }
280 
281 void GFTrack::Print(const Option_t* option) const{
282  for(unsigned int i=0;i<getNumReps();++i){
283  std::cout << "TrackRep " << i << " (defined at hit " << getRepAtHit(i) << "):\n";
284  getTrackRep(i)->Print(option);
285  fBookkeeping.at(i)->Print(option);
286  }
287  std::cout << "GFTrack has " << getNumHits() << " detector hits. ";
288  if (fSmooth && fSmoothFast ) std::cout << "Fast smoothing is enabled.";
289  else if (fSmooth) std::cout << "Smoothing is enabled.";
290  else std::cout << "Smoothing is disabled.";
291  std::cout << std::endl;
292 }
293 
294 
295 std::map<GFAbsRecoHit*, unsigned int> GFTrack::getHitMap(){
296  std::map<GFAbsRecoHit*, unsigned int> hitMap;
297  unsigned int nHits = getNumHits();
298 
299  for (unsigned int i=0; i<nHits; ++i){
300  hitMap.insert(std::pair<GFAbsRecoHit*, unsigned int>(fHits[i], i));
301  }
302 
303  return hitMap;
304 }
305 
306 
307 bool GFTrack::getHitsByPlane(std::vector<std::vector<int>*>& retVal){
308  for(int i=0;retVal.size();++i){
309  delete retVal.at(i);
310  }
311  retVal.clear();
312  //this method can only be called when all hits have been loaded
313  assert(fHits.size()==fCand.getNHits());
314  if(fHits.size() <= 1) return false;
315  unsigned int detId,hitId,planeId;
316  fCand.getHitWithPlane(0,detId,hitId,planeId);
317  // std::cout << "$$$ " << 0 << " " << detId << " " << hitId << " " << planeId << std::endl;
318  unsigned int lastPlane=planeId;
319  retVal.push_back(new std::vector<int>);
320  retVal.at(0)->push_back(0);
321  for(unsigned int i=1;i<fCand.getNHits();++i){
322  fCand.getHitWithPlane(i,detId,hitId,planeId);
323  //std::cout << "$$$ " << i << " " << detId << " " << hitId << " " << planeId << std::endl;
324  if(lastPlane==planeId){
325  retVal.at(retVal.size()-1)->push_back(i);
326  }
327  else{
328  lastPlane=planeId;
329  retVal.push_back(new std::vector<int>);
330  retVal.at(retVal.size()-1)->push_back(i);
331  }
332  }
333  return true;
334 }
335 
336 
337 void
338 GFTrack::blowUpCovs(double blowUpFactor){
339  int nreps=getNumReps();
340  for(int irep=0; irep<nreps; ++irep){
341  GFAbsTrackRep* arep=getTrackRep(irep);
342 
343  //dont do it for already compromsied reps, since they wont be fitted anyway
344  if(arep->getStatusFlag()==0) {
345  TMatrixT<double> cov = arep->getCov();
346  for(int i=0;i<cov.GetNrows();++i){
347  for(int j=0;j<cov.GetNcols();++j){
348  //if(i!=j){//off diagonal
349  // cov[i][j]=0.;
350  //}
351  //else{//diagonal
352  cov[i][j] = cov[i][j] * blowUpFactor;
353  //}
354  }
355  }
356  arep->setCov(cov);
357  }
358  }
359 }
360 
361 
363 
364 
365