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