LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
GFTrack.cxx
Go to the documentation of this file.
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 <iostream>
20 
21 #include "larreco/Genfit/GFTrack.h"
22 #include "TVirtualGeoTrack.h"
23 
25  : fTrackReps(NULL),fPDG(2112), fCardinal_rep(0), fNextHitToFit(0)
26 {
27  addTrackRep(defaultRep);
28 }
29 
31  : fTrackReps(NULL), fCardinal_rep(0), fNextHitToFit(0)
32 {
33  //trackReps = new TObjArray(defNumTrackReps);
34 }
35 
37  if(fTrackReps!=NULL){
38  for(unsigned int i=0;i<getNumReps();i++) {
39  delete fTrackReps->At(i);
40  }
41  delete fTrackReps;
42  }
43  for(unsigned int i=0;i<fHits.size();i++) {
44  delete fHits[i];
45  }
46  for(unsigned int i=0;i<fBookkeeping.size();++i){
47  if(fBookkeeping.at(i)!=NULL) delete fBookkeeping.at(i);
48  }
49 }
50 
51 genf::GFTrack::GFTrack(const GFTrack& _tr): TObject() {
52  fCand=_tr.fCand;
55  for(unsigned int i=0;i<_tr.getNumHits();i++) {
56  fHits.push_back((_tr.getHit(i))->clone());
57  }
58  fTrackReps = NULL;
59  for(unsigned int i=0; i<_tr.getNumReps();i++) {
60  addTrackRep( (_tr.getTrackRep(i))->clone() );
61  }
62  for(unsigned int i=0; i<fBookkeeping.size(); ++i) delete fBookkeeping[i];
63  fBookkeeping.clear();
64 
65  for(unsigned int i=0;i<_tr.fBookkeeping.size();++i){
66  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
67  }
68  fRepAtHit = _tr.fRepAtHit;
69 }
70 
72  if(fTrackReps!=NULL){
73  for(unsigned int i=0;i<getNumReps();i++) {
74  delete fTrackReps->At(i);
75  }
76  delete fTrackReps;
77  fTrackReps=NULL;
78  }
79  for(unsigned int i=0;i<fHits.size();i++) {
80  delete fHits[i];
81  }
82  for(unsigned int i=0;i<fBookkeeping.size();++i){
83  if(fBookkeeping.at(i)!=NULL) delete fBookkeeping.at(i);
84  }
85 
86  for(unsigned int i=0;i<_tr.getNumReps();++i){
87  addTrackRep(_tr.getTrackRep(i)->clone());
88  }
89  fCand=_tr.fCand;
92  for(unsigned int i=0;i<_tr.getNumHits();i++) {
93  fHits.push_back((_tr.getHit(i))->clone());
94  }
95 
96  //clear the empty bookeeping objs made by addTrackRep and copy the others
97  for(unsigned int i=0; i<fBookkeeping.size(); ++i) delete fBookkeeping[i];
98  fBookkeeping.clear();
99  for(unsigned int i=0;i<_tr.fBookkeeping.size();++i){
100  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
101  }
102  fRepAtHit = _tr.fRepAtHit;
103 
104 
105  return *this;
106 }
107 
108 
109 void
111  if(fTrackReps!=NULL){
112  for(unsigned int i=0;i<getNumReps();i++) {
113  delete fTrackReps->At(i);
114  delete fBookkeeping.at(i);
115  }
116  }
117  for(unsigned int i=0;i<fHits.size();i++) {
118  delete fHits[i];
119  }
120  fHits.clear();
121  fRepAtHit.clear();
122 }
123 
124 void
126  unsigned int nhits=trk->getNumHits();
127  for(unsigned int i=0;i<nhits;++i){
128  unsigned int detId;
129  unsigned int hitId;
130  trk->getCand().getHit(i,detId,hitId);
131  GFAbsRecoHit* hit=trk->getHit(i);
132  addHit(hit,detId,hitId);
133  }
134  trk->fHits.clear();
135 }
136 
137 
138 void
139 genf::GFTrack::setCandidate(const GFTrackCand& cand, bool doreset)
140 {
141  fCand=cand;
142  // reset fits
143  if(doreset) {
144  for(unsigned int i=0;i<getNumReps();i++) {
145  ((GFAbsTrackRep*)fTrackReps->At(i))->reset();
146  }
147  }
148 }
149 
150 void
151 genf::GFTrack::fillGeoTrack(TVirtualGeoTrack* geotrk,unsigned int repid) const
152 {
153  GFAbsTrackRep* rep=getTrackRep(repid);
154  unsigned int n=fCand.getNHits();
155  PrintROOTobject(std::cout, rep->getState());
156  for(unsigned int i=0; i<n; ++i){// loop over hits
157  GFDetPlane pl=fHits[i]->getDetPlane(rep);
158  TVector3 pos=rep->getPos(pl);
159  #ifdef NDEBUG
160  std::cout<<pos.X()<<","<<pos.Y()<<","<<pos.Z()<<std::endl;
161  #endif // NDEBUG
162  geotrk->AddPoint(pos.X(),pos.Y(),pos.Z(),0);
163  }// end loop over hits
164 }
165 
166 
167 void
168 genf::GFTrack::getResiduals(unsigned int detId, // which detector?
169  unsigned int dim, // which projection?
170  unsigned int repid, // which trackrep ?
171  std::vector<double>& result)
172 {
173  unsigned int nhits=getNumHits();
174  if(repid>=getNumReps())return;
175  GFAbsTrackRep* rep=getTrackRep(repid);//->clone();
176  for(unsigned int ih=0; ih<nhits; ++ih){// loop over hits
177  unsigned int anid;
178  unsigned int dummy;
179  fCand.getHit(ih,anid,dummy); // check if this is a hit we want to look at
180  if(anid==detId){
181  GFAbsRecoHit* hit=getHit(ih);
182  // extrapolate trackrep there
183  int repDim=rep->getDim();
184  TMatrixT<Double_t> state(repDim,1);
185  GFDetPlane pl=hit->getDetPlane(rep);
186 
187  rep->extrapolate(pl,state);
188  //rep->setState(state);
189  //rep->setReferencePlane(pl);
190  double res=hit->residualVector(rep,state,pl)[dim][0];
191 
192  //std::cout<<res<<std::endl;
193 
194  result.push_back(res);
195  }
196  }
197 }
198 
199 
200 void genf::GFTrack::printBookkeeping(std::ostream& out /* = std::cout */) const {
201  out << "GFTrack::printBookkeeping()" << std::endl;
202  for(unsigned int i=0;i<getNumReps();++i){
203  out << "trackRep " << i << ":" << std::endl;
204  fBookkeeping.at(i)->Print(out);
205  }
206 
207 }
208 
209 void genf::GFTrack::Print(std::ostream& out /* = std::cout */) const {
210  for(unsigned int i=0;i<getNumReps();++i){
211  getTrackRep(i)->Print(out);
212  fBookkeeping.at(i)->Print(out);
213  }
214  out << "GFTrack has " << getNumHits() << " detector hits." << std::endl;
215 
216 }
217 
218 
219 void genf::GFTrack::getHitsByPlane(std::vector<std::vector<int>*>& retVal){
220  for(int i=0;retVal.size();++i){
221  delete retVal.at(i);
222  }
223  retVal.clear();
224  //this method can only be called when all hits have been loaded
225  if (fHits.size() != fCand.getNHits())
226  throw GFException("genf::GFTrack::getResiduals(): inconsistent hits", __LINE__, __FILE__).setFatal();
227  if (fHits.size() < 2)
228  throw GFException("genf::GFTrack::getResiduals(): less than 2 hits", __LINE__, __FILE__).setFatal();
229  unsigned int detId,hitId,planeId;
230  fCand.getHitWithPlane(0,detId,hitId,planeId);
231  // std::cout << "$$$ " << 0 << " " << detId << " " << hitId << " " << planeId << std::endl;
232  unsigned int lastPlane=planeId;
233  retVal.push_back(new std::vector<int>);
234  retVal.at(0)->push_back(0);
235  for(unsigned int i=1;i<fCand.getNHits();++i){
236  fCand.getHitWithPlane(i,detId,hitId,planeId);
237  //std::cout << "$$$ " << i << " " << detId << " " << hitId << " " << planeId << std::endl;
238  if(lastPlane==planeId){
239  retVal.at(retVal.size()-1)->push_back(i);
240  }
241  else{
242  lastPlane=planeId;
243  retVal.push_back(new std::vector<int>);
244  retVal.at(retVal.size()-1)->push_back(i);
245  }
246  }
247 }
248 
249 //ClassImp(GFTrack)
250 
251 
252 
void printBookkeeping(std::ostream &out=std::cout) const
print bookkeeping
Definition: GFTrack.cxx:200
GFTrack & operator=(const GFTrack &)
assignement operator
Definition: GFTrack.cxx:71
virtual GFAbsTrackRep * clone() const =0
TObjArray * fTrackReps
Collection of track representations.
Definition: GFTrack.h:76
virtual TVector3 getPos(const GFDetPlane &pl)=0
void getHitWithPlane(unsigned int i, unsigned int &detId, unsigned int &hitId, unsigned int &planeId) const
Get detector ID and cluster index (hitId) for hit number i with plane id.
Definition: GFTrackCand.h:106
GFAbsTrackRep * getTrackRep(int id) const
Accessor for track representations.
Definition: GFTrack.h:195
virtual const GFDetPlane & getDetPlane(GFAbsTrackRep *)=0
Get detector plane for a given track representation.
const TMatrixT< Double_t > & getState() const
unsigned int fNextHitToFit
Definition: GFTrack.h:115
std::vector< int > fRepAtHit
repAtHit keeps track of at which hit index which rep is currently defined, to avoid null extrapolatio...
Definition: GFTrack.h:91
Base Class for genfit track representations. Defines interface for track parameterizations.
Definition: GFAbsTrackRep.h:85
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
void fillGeoTrack(TVirtualGeoTrack *tr) const
Fill TVirtualGeoTrack object Cardinal representation is used.
Definition: GFTrack.h:283
GFTrackCand fCand
Helper to store the indices of the hits in the track. See GFTrackCand for details.
Definition: GFTrack.h:96
unsigned int getNumReps() const
Get number of track represenatations.
Definition: GFTrack.h:201
GFTrack()
Default constructor – needed for compatibility with ROOT.
Definition: GFTrack.cxx:30
virtual ~GFTrack()
Definition: GFTrack.cxx:36
GFAbsRecoHit * getHit(int id) const
Definition: GFTrack.h:159
unsigned int fCardinal_rep
Definition: GFTrack.h:113
virtual double extrapolate(const GFDetPlane &plane, TMatrixT< Double_t > &statePred)
returns the tracklength spanned in this extrapolation
void Print(std::ostream &out=std::cout) const
Definition: GFTrack.cxx:209
void getResiduals(unsigned int detId, unsigned int dim, unsigned int rep, std::vector< double > &result)
Get residuals.
Definition: GFTrack.cxx:168
void mergeHits(GFTrack *trk)
Merge two GFTracks. Only hits will be merged.
Definition: GFTrack.cxx:125
Detector simulation of raw signals on wires.
unsigned int getNumHits() const
Definition: GFTrack.h:163
void getHit(unsigned int i, unsigned int &detId, unsigned int &hitId) const
Get detector ID and cluster index (hitId) for hit number i.
Definition: GFTrackCand.h:84
void reset()
Resets the GFTrack – deletes RecoHits!
Definition: GFTrack.cxx:110
std::vector< GFAbsRecoHit * > fHits
Collection of RecoHits.
Definition: GFTrack.h:80
unsigned int getNHits() const
Definition: GFTrackCand.h:116
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:50
void PrintROOTobject(std::ostream &, const ROOTOBJ &)
Small utility functions which print some ROOT objects into an output stream.
Definition: GFException.h:129
GFException & setFatal(bool b=true)
set fatal flag. if this is true, the fit stops for this current track repr.
Definition: GFException.h:80
void getHitsByPlane(std::vector< std::vector< int > * > &retVal)
Definition: GFTrack.cxx:219
const GFTrackCand & getCand() const
Definition: GFTrack.h:157
Char_t n[5]
void addHit(GFAbsRecoHit *theHit)
deprecated!
Definition: GFTrack.h:299
unsigned int getDim() const
returns dimension of state vector
std::vector< GFBookkeeping * > fBookkeeping
Collection of Bookeeping objects for failed hits in every trackrep.
Definition: GFTrack.h:86
virtual TMatrixT< Double_t > residualVector(const GFAbsTrackRep *stateVector, const TMatrixT< Double_t > &state, const GFDetPlane &d)
Calculate residual with respect to a track representation.
Definition: GFAbsRecoHit.h:147
void addTrackRep(GFAbsTrackRep *theTrackRep)
Add track represenation.
Definition: GFTrack.h:326
virtual void Print(std::ostream &out=std::cout) const
void setCandidate(const GFTrackCand &cand, bool doreset=false)
set track candidate
Definition: GFTrack.cxx:139