LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 "TVirtualGeoTrack.h"
23 #include "larreco/Genfit/GFTrack.h"
24 
26  : fTrackReps(NULL), fPDG(2112), fCardinal_rep(0), fNextHitToFit(0)
27 {
28  addTrackRep(defaultRep);
29 }
30 
32 {
33  //trackReps = new TObjArray(defNumTrackReps);
34 }
35 
37 {
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 {
54  fCand = _tr.fCand;
57  for (unsigned int i = 0; i < _tr.getNumHits(); i++) {
58  fHits.push_back((_tr.getHit(i))->clone());
59  }
60  fTrackReps = NULL;
61  for (unsigned int i = 0; i < _tr.getNumReps(); i++) {
62  addTrackRep((_tr.getTrackRep(i))->clone());
63  }
64  for (unsigned int i = 0; i < fBookkeeping.size(); ++i)
65  delete fBookkeeping[i];
66  fBookkeeping.clear();
67 
68  for (unsigned int i = 0; i < _tr.fBookkeeping.size(); ++i) {
69  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
70  }
71  fRepAtHit = _tr.fRepAtHit;
72 }
73 
75 {
76  if (fTrackReps != NULL) {
77  for (unsigned int i = 0; i < getNumReps(); i++) {
78  delete fTrackReps->At(i);
79  }
80  delete fTrackReps;
81  fTrackReps = NULL;
82  }
83  for (unsigned int i = 0; i < fHits.size(); i++) {
84  delete fHits[i];
85  }
86  for (unsigned int i = 0; i < fBookkeeping.size(); ++i) {
87  if (fBookkeeping.at(i) != NULL) delete fBookkeeping.at(i);
88  }
89 
90  for (unsigned int i = 0; i < _tr.getNumReps(); ++i) {
91  addTrackRep(_tr.getTrackRep(i)->clone());
92  }
93  fCand = _tr.fCand;
96  for (unsigned int i = 0; i < _tr.getNumHits(); i++) {
97  fHits.push_back((_tr.getHit(i))->clone());
98  }
99 
100  //clear the empty bookeeping objs made by addTrackRep and copy the others
101  for (unsigned int i = 0; i < fBookkeeping.size(); ++i)
102  delete fBookkeeping[i];
103  fBookkeeping.clear();
104  for (unsigned int i = 0; i < _tr.fBookkeeping.size(); ++i) {
105  fBookkeeping.push_back(new GFBookkeeping(*(_tr.fBookkeeping.at(i))));
106  }
107  fRepAtHit = _tr.fRepAtHit;
108 
109  return *this;
110 }
111 
113 {
114  if (fTrackReps != NULL) {
115  for (unsigned int i = 0; i < getNumReps(); i++) {
116  delete fTrackReps->At(i);
117  delete fBookkeeping.at(i);
118  }
119  }
120  for (unsigned int i = 0; i < fHits.size(); i++) {
121  delete fHits[i];
122  }
123  fHits.clear();
124  fRepAtHit.clear();
125 }
126 
128 {
129  unsigned int nhits = trk->getNumHits();
130  for (unsigned int i = 0; i < nhits; ++i) {
131  unsigned int detId;
132  unsigned int hitId;
133  trk->getCand().getHit(i, detId, hitId);
134  GFAbsRecoHit* hit = trk->getHit(i);
135  addHit(hit, detId, hitId);
136  }
137  trk->fHits.clear();
138 }
139 
140 void 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 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 void genf::GFTrack::getResiduals(unsigned int detId, // which detector?
167  unsigned int dim, // which projection?
168  unsigned int repid, // which trackrep ?
169  std::vector<double>& result)
170 {
171  unsigned int nhits = getNumHits();
172  if (repid >= getNumReps()) return;
173  GFAbsTrackRep* rep = getTrackRep(repid); //->clone();
174  for (unsigned int ih = 0; ih < nhits; ++ih) { // loop over hits
175  unsigned int anid;
176  unsigned int dummy;
177  fCand.getHit(ih, anid, dummy); // check if this is a hit we want to look at
178  if (anid == detId) {
179  GFAbsRecoHit* hit = getHit(ih);
180  // extrapolate trackrep there
181  int repDim = rep->getDim();
182  TMatrixT<Double_t> state(repDim, 1);
183  GFDetPlane pl = hit->getDetPlane(rep);
184 
185  rep->extrapolate(pl, state);
186  //rep->setState(state);
187  //rep->setReferencePlane(pl);
188  double res = hit->residualVector(rep, state, pl)[dim][0];
189 
190  //std::cout<<res<<std::endl;
191 
192  result.push_back(res);
193  }
194  }
195 }
196 
197 void genf::GFTrack::printBookkeeping(std::ostream& out /* = std::cout */) const
198 {
199  out << "GFTrack::printBookkeeping()" << std::endl;
200  for (unsigned int i = 0; i < getNumReps(); ++i) {
201  out << "trackRep " << i << ":" << std::endl;
202  fBookkeeping.at(i)->Print(out);
203  }
204 }
205 
206 void genf::GFTrack::Print(std::ostream& out /* = std::cout */) const
207 {
208  for (unsigned int i = 0; i < getNumReps(); ++i) {
209  getTrackRep(i)->Print(out);
210  fBookkeeping.at(i)->Print(out);
211  }
212  out << "GFTrack has " << getNumHits() << " detector hits." << std::endl;
213 }
214 
215 void genf::GFTrack::getHitsByPlane(std::vector<std::vector<int>*>& retVal)
216 {
217  for (int i = 0; retVal.size(); ++i) {
218  delete retVal.at(i);
219  }
220  retVal.clear();
221  //this method can only be called when all hits have been loaded
222  if (fHits.size() != fCand.getNHits())
223  throw GFException("genf::GFTrack::getResiduals(): inconsistent hits", __LINE__, __FILE__)
224  .setFatal();
225  if (fHits.size() < 2)
226  throw GFException("genf::GFTrack::getResiduals(): less than 2 hits", __LINE__, __FILE__)
227  .setFatal();
228  unsigned int detId, hitId, planeId;
229  fCand.getHitWithPlane(0, detId, hitId, planeId);
230  // std::cout << "$$$ " << 0 << " " << detId << " " << hitId << " " << planeId << std::endl;
231  unsigned int lastPlane = planeId;
232  retVal.push_back(new std::vector<int>);
233  retVal.at(0)->push_back(0);
234  for (unsigned int i = 1; i < fCand.getNHits(); ++i) {
235  fCand.getHitWithPlane(i, detId, hitId, planeId);
236  //std::cout << "$$$ " << i << " " << detId << " " << hitId << " " << planeId << std::endl;
237  if (lastPlane == planeId) { retVal.at(retVal.size() - 1)->push_back(i); }
238  else {
239  lastPlane = planeId;
240  retVal.push_back(new std::vector<int>);
241  retVal.at(retVal.size() - 1)->push_back(i);
242  }
243  }
244 }
245 
246 //ClassImp(GFTrack)
void printBookkeeping(std::ostream &out=std::cout) const
print bookkeeping
Definition: GFTrack.cxx:197
GFTrack & operator=(const GFTrack &)
assignement operator
Definition: GFTrack.cxx:74
virtual GFAbsTrackRep * clone() const =0
TObjArray * fTrackReps
Collection of track representations.
Definition: GFTrack.h:76
std::vector< GFBookkeeping * > fBookkeeping
Collection of Bookeeping objects for failed hits in every trackrep.
Definition: GFTrack.h:86
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:120
GFAbsTrackRep * getTrackRep(int id) const
Accessor for track representations.
Definition: GFTrack.h:193
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:81
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
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:96
unsigned int getNumReps() const
Get number of track represenatations.
Definition: GFTrack.h:200
GFTrack()
Default constructor – needed for compatibility with ROOT.
Definition: GFTrack.cxx:31
virtual ~GFTrack()
Definition: GFTrack.cxx:36
GFAbsRecoHit * getHit(int id) const
Definition: GFTrack.h:161
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:206
void getResiduals(unsigned int detId, unsigned int dim, unsigned int rep, std::vector< double > &result)
Get residuals.
Definition: GFTrack.cxx:166
void mergeHits(GFTrack *trk)
Merge two GFTracks. Only hits will be merged.
Definition: GFTrack.cxx:127
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:93
void reset()
Resets the GFTrack – deletes RecoHits!
Definition: GFTrack.cxx:112
std::vector< GFAbsRecoHit * > fHits
Collection of RecoHits.
Definition: GFTrack.h:80
unsigned int getNHits() const
Definition: GFTrackCand.h:134
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:47
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:75
void getHitsByPlane(std::vector< std::vector< int > * > &retVal)
Definition: GFTrack.cxx:215
const GFTrackCand & getCand() const
Definition: GFTrack.h:159
Char_t n[5]
void addHit(GFAbsRecoHit *theHit)
deprecated!
Definition: GFTrack.h:301
unsigned int getDim() const
returns dimension of state vector
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:149
void addTrackRep(GFAbsTrackRep *theTrackRep)
Add track represenation.
Definition: GFTrack.h:325
virtual void Print(std::ostream &out=std::cout) const
void setCandidate(const GFTrackCand &cand, bool doreset=false)
set track candidate
Definition: GFTrack.cxx:140