FairRoot/PandaRoot
GFDaf.cxx
Go to the documentation of this file.
1 /* Copyright 2011, Technische Universitaet Muenchen,
2  Authors: Karl Bicker, Christian Hoeppner
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<GFDaf.h>
20 
22 
23  setBetas(81.,8.,4.,1.,1.,1.);
24  setProbCut(0.01);
25 
27 
28 };
29 
31 
32  std::vector<GFDafHit*> eff_hits = initHitsWeights(trk);
33 
34  GFTrack* mini_trk = new GFTrack();
35 
36  for(unsigned int j=0; j<eff_hits.size(); j++) mini_trk->addHit(eff_hits.at(j));
37 
38  mini_trk->setSmoothing();
39 
40  for(unsigned int i=0; i<trk->getNumReps(); i++) {
41 
42  if(trk->getTrackRep(i)->getStatusFlag()!=0) continue;
43 
44  mini_trk->addTrackRep(trk->getTrackRep(i));
45 
46  for(unsigned int iBeta=0; iBeta<fBeta.size(); iBeta++) {
47 
48  for(unsigned int j=0; j<mini_trk->getNumHits(); j++) {
49  GFDafHit* hit = static_cast<GFDafHit*>(mini_trk->getHit(j));
50  hit->setWeights(fWeights.at(i).at(j));
51  hit->setBlowUp(fBeta.at(iBeta));
52  }
53 
54  GFKalman::processTrack(mini_trk);
55 
56  if(mini_trk->getTrackRep(0)->getStatusFlag() != 0) break;
57 
58  if(iBeta != fBeta.size()-1 )
59  try{
60  fWeights.at(i) = calcWeights(mini_trk, fBeta.at(iBeta));
61  } catch(GFException& e) {
62  std::cerr<<e.what();
63  e.info();
64  mini_trk->getTrackRep(0)->setStatusFlag(1);
65  break;
66  }
67 
68  }
69 
70  if(trk->getSmoothing()) copySmoothing(mini_trk, trk, i);
71 
72  mini_trk->releaseTrackReps();
73 
74  }
75 
76  delete mini_trk;
77 
78 };
79 
80 std::vector<std::vector<double> > GFDaf::calcWeights(GFTrack* trk, double beta) {
81 
82  std::vector<std::vector<double> > ret_val;
83 
84  for(unsigned int i=0; i<trk->getNumHits(); i++) {
85 
86  GFDafHit* eff_hit = static_cast<GFDafHit*>(trk->getHit(i));
87  std::vector<double> phi;
88  double phi_sum = 0;
89  double phi_cut = 0;
90 
91  std::vector<double> weights;
92 
93  TMatrixT<double> x_smoo(GFTools::getSmoothedPos(trk, 0, i));
94  if(x_smoo.GetNrows() == 0) {
95  weights.assign(eff_hit->getNumHits(),0.5);
96  std::cout<<"Assumed weight 0.5!!"<<std::endl;
97  ret_val.push_back(weights);
98  continue;
99  }
100 
101  for(unsigned int j=0; j<eff_hit->getNumHits(); j++) {
102  GFAbsRecoHit* real_hit = eff_hit->getHit(j);
103  GFDetPlane pl;
104  try{
105  pl = real_hit->getDetPlane(trk->getTrackRep(0));
106  } catch(GFException& e) {
107  std::cerr<<e.what();
108  e.info();
109  }
110  TMatrixT<double> m(real_hit->getHitCoord(pl));
111  TMatrixT<double> V( beta * real_hit->getHitCov(pl));
112  TMatrixT<double> resid(m - x_smoo);
113  TMatrixT<double> resid_T(resid);
114  resid_T.T();
115  double detV = V.Determinant();
116  TMatrixT<double> Vinv;
117  GFTools::invertMatrix(V,Vinv);
118 
119  phi.push_back((1./(pow(2.*TMath::Pi(),V.GetNrows()/2.)*sqrt(detV)))*exp(-0.5*(resid_T * Vinv * resid)[0][0]));
120  phi_sum += phi.at(j);
121 
122  double cutVal = fchi2Cuts[V.GetNrows()];
123  assert(cutVal>1.E-6);
124  phi_cut += 1./(2.*TMath::Pi()*sqrt(detV))*exp(-0.5*cutVal/beta);
125 
126  }
127 
128  for(unsigned int j=0; j<eff_hit->getNumHits(); j++) {
129 
130 //std::cout<<"Calculated weight (beta = "<<beta<<", hit "<<i<<") "<<phi.at(j)<<"/("<<phi_sum<<"+"<<phi_cut<<") = "<<phi.at(j)/(phi_sum+phi_cut)<<std::endl;
131  weights.push_back(phi.at(j)/(phi_sum+phi_cut));
132 
133  }
134 
135  ret_val.push_back(weights);
136 
137  }
138 
139  return ret_val;
140 
141 };
142 
143 void GFDaf::setProbCut(double val){
144 
145  if(fabs(val-0.01)<1.E-10){
146  fchi2Cuts[1] = 6.63;
147  fchi2Cuts[2] = 9.21;
148  fchi2Cuts[3] = 11.34;
149  fchi2Cuts[4] = 13.23;
150  fchi2Cuts[5] = 15.09;
151  }
152  else if(fabs(val-0.005)<1.E-10){
153  fchi2Cuts[1] = 7.88;
154  fchi2Cuts[2] = 10.60;
155  fchi2Cuts[3] = 12.84;
156  fchi2Cuts[4] = 14.86;
157  fchi2Cuts[5] = 16.75;
158  }
159  else if(fabs(val-0.001)<1.E-10){
160  fchi2Cuts[1] = 10.83;
161  fchi2Cuts[2] = 13.82;
162  fchi2Cuts[3] = 16.27;
163  fchi2Cuts[4] = 18.47;
164  fchi2Cuts[5] = 20.51;
165  }
166  else{
167  GFException exc("GFDafsetProbCut() value is not supported",__LINE__,__FILE__);
168  exc.setFatal();
169  throw exc;
170  }
171 
172 }
173 
174 void GFDaf::setBetas(double b1,double b2,double b3,double b4,double b5,double b6,double b7,double b8,double b9,double b10){
175  assert(b1>0);fBeta.push_back(b1);
176  assert(b2>0 && b2<=b1);fBeta.push_back(b2);
177  if(b3>=0.) {
178  assert(b3<=b2);fBeta.push_back(b3);
179  if(b4>=0.) {
180  assert(b4<=b3);fBeta.push_back(b4);
181  if(b5>=0.) {
182  assert(b5<=b4);fBeta.push_back(b5);
183  if(b6>=0.) {
184  assert(b6<=b5);fBeta.push_back(b6);
185  if(b7>=0.) {
186  assert(b7<=b6);fBeta.push_back(b7);
187  if(b8>=0.) {
188  assert(b8<=b7);fBeta.push_back(b8);
189  if(b9>=0.) {
190  assert(b9<=b8);fBeta.push_back(b9);
191  if(b10>=0.) {
192  assert(b10<=b9);fBeta.push_back(b10);
193  }
194  }
195  }
196  }
197  }
198  }
199  }
200  }
201 }
202 
203 std::vector<GFDafHit*> GFDaf::initHitsWeights(GFTrack* trk) {
204 
205  std::vector< std::vector<int>* > planes;
206  trk->getHitsByPlane(planes);
207  int nPlanes = planes.size();
208 
209  std::vector<GFDafHit*> eff_hits;
210 
211  for(int i=0; i<nPlanes; i++) {
212 
213  std::vector<GFAbsRecoHit*> hits;
214 
215  for(unsigned int j=0; j<planes.at(i)->size(); j++) {
216  hits.push_back(trk->getHit(planes.at(i)->at(j)) );
217  }
218 
219  GFDafHit* eff_hit = new GFDafHit(hits);
220  eff_hits.push_back(eff_hit);
221 
222  }
223 
224  for(unsigned int i=0; i<trk->getNumReps(); i++) {
225  std::vector<std::vector<double> > rep_weights;
226  for(unsigned int j=0; j<eff_hits.size(); j++) {
227  std::vector<double> single_weights;
228  single_weights.assign(eff_hits.at(j)->getNumHits(),1.);
229  rep_weights.push_back(single_weights);
230  }
231  fWeights.push_back(rep_weights);
232  }
233 
234  return eff_hits;
235 
236 }
237 
238 void GFDaf::copySmoothing(GFTrack* source, GFTrack* target, int target_irep) {
239 
240  for(unsigned int i=0; i<target->getNumReps(); i++) {
241 
242  std::vector<std::string> mat_keys = target->getBK(i)->getMatrixKeys();
243  bool already_there = false;
244  for(unsigned int j=0; j<mat_keys.size(); j++) {
245  if(mat_keys.at(j) == "fUpSt") already_there = true;
246  }
247  if(already_there) continue;
248 
249  target->getBK(i)->setNhits(target->getNumHits());
250  target->getBK(i)->bookMatrices("fUpSt");
251  target->getBK(i)->bookMatrices("fUpCov");
252  target->getBK(i)->bookMatrices("bUpSt");
253  target->getBK(i)->bookMatrices("bUpCov");
254  target->getBK(i)->bookGFDetPlanes("fPl");
255  target->getBK(i)->bookGFDetPlanes("bPl");
256  if(target->getTrackRep(i)->hasAuxInfo()) {
257  target->getBK(i)->bookMatrices("fAuxInfo");
258  target->getBK(i)->bookMatrices("bAuxInfo");
259  }
260  }
261 
262  int hit_count = 0;
263 
264  for(unsigned int pl_i=0; pl_i<source->getNumHits(); pl_i++) {
265 
266  GFDafHit* eff_hit = static_cast<GFDafHit*>(source->getHit(pl_i));
267 
268  for(unsigned int hit_i=0; hit_i<eff_hit->getNumHits(); hit_i++) {
269 
270  TMatrixT<double> fUpSt, fUpCov, bUpSt, bUpCov, fAuxInfo, bAuxInfo;
271  GFDetPlane fPl, bPl;
272 
273  source->getBK(0)->getMatrix("fUpSt",hit_i,fUpSt);
274  source->getBK(0)->getMatrix("fUpCov",hit_i,fUpCov);
275  source->getBK(0)->getDetPlane("fPl",hit_i,fPl);
276  source->getBK(0)->getMatrix("bUpSt",hit_i,bUpSt);
277  source->getBK(0)->getMatrix("bUpCov",hit_i,bUpCov);
278  source->getBK(0)->getDetPlane("bPl",hit_i,bPl);
279 
280  if(source->getTrackRep(0)->hasAuxInfo()) {
281  source->getBK(0)->getMatrix("fAuxInfo",hit_i,fAuxInfo);
282  source->getBK(0)->getMatrix("bAuxInfo",hit_i,bAuxInfo);
283  target->getBK(target_irep)->setMatrix("fAuxInfo",hit_count,fAuxInfo);
284  target->getBK(target_irep)->setMatrix("bAuxInfo",hit_count,bAuxInfo);
285  }
286 
287  target->getBK(target_irep)->setMatrix("fUpSt",hit_count,fUpSt);
288  target->getBK(target_irep)->setMatrix("fUpCov",hit_count,fUpCov);
289  target->getBK(target_irep)->setDetPlane("fPl",hit_count,fPl);
290  target->getBK(target_irep)->setMatrix("bUpSt",hit_count,bUpSt);
291  target->getBK(target_irep)->setMatrix("bUpCov",hit_count,bUpCov);
292  target->getBK(target_irep)->setDetPlane("bPl",hit_count,bPl);
293 
294  hit_count++;
295 
296  }
297 
298  }
299 
300 }
301 
std::vector< GFDafHit * > initHitsWeights(GFTrack *trk)
Initialize the GFDafHit and their weights before the fit.
Definition: GFDaf.cxx:203
GFBookkeeping * getBK(int index=-1)
get GFBookKeeping object for particular track rep (default is cardinal rep)
Definition: GFTrack.h:326
unsigned int getNumHits()
Get the number of hits in the GFDafHit.
Definition: GFDafHit.h:121
unsigned int getNumHits() const
Definition: GFTrack.h:148
friend F32vec4 exp(const F32vec4 &a)
Definition: P4_F32vec4.h:109
Int_t i
Definition: run_full.C:25
void addHit(GFAbsRecoHit *theHit)
deprecated!
Definition: GFTrack.h:291
__m128 m
Definition: P4_F32vec4.h:28
Detector plane genfit geometry class.
Definition: GFDetPlane.h:59
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
Track object for genfit. genfit algorithms work on these objects.
Definition: GFTrack.h:60
bool getSmoothing()
Read back if smoothing is/was turned on or off for this track.
Definition: GFTrack.h:405
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
void getHitsByPlane(std::vector< std::vector< int > * > &retVal)
Definition: GFTrack.cxx:227
Generic Kalman Filter implementation.
Definition: GFKalman.h:50
virtual const GFDetPlane & getDetPlane(GFAbsTrackRep *)=0
Get detector plane for a given track representation.
void setSmoothing(bool smooth=true)
Switch smoothing on or off for this track.
Definition: GFTrack.h:401
virtual const char * what() const
standard error message handling for exceptions. use like "std::cerr << e.what();" ...
Definition: GFException.cxx:47
void setProbCut(double prob_cut)
Set the probabilty cut for the weight calculation for the hits.
Definition: GFDaf.cxx:143
void setWeights(std::vector< double > weights)
Set the weights.
Definition: GFDafHit.cxx:46
std::map< int, double > fchi2Cuts
Definition: GFDaf.h:101
GFAbsRecoHit * getHit(unsigned int ihit)
Get at hit from the GFDafHit.
Definition: GFDafHit.cxx:32
TMatrixT< double > getSmoothedPos(GFTrack *trk, unsigned int irep, unsigned int ihit)
Get smoothed track position in plane coordinates.
Definition: GFTools.cxx:4
bool getDetPlane(std::string key, unsigned int index, GFDetPlane &pl)
std::vector< std::vector< double > > calcWeights(GFTrack *trk, double beta)
Calculate the weights for the next fitting pass.
Definition: GFDaf.cxx:80
void bookGFDetPlanes(std::string key)
unsigned int getNumReps() const
Get number of track represenatations.
Definition: GFTrack.h:192
Wrapper class for use with GFDaf.
Definition: GFDafHit.h:40
void setBlowUp(double blow_up)
Set the blow up factor for the covariance matrices.
Definition: GFDafHit.cxx:38
void invertMatrix(const TMatrixT< double > &mat, TMatrixT< double > &inv)
Invert a matrix, throwing GFException when inversion fails.
Definition: GFTools.cxx:334
bool getStatusFlag()
std::vector< std::vector< std::vector< double > > > fWeights
Definition: GFDaf.h:99
virtual bool hasAuxInfo()
See if the track representation has auxillary information stored.
void setDetPlane(std::string key, unsigned int index, const GFDetPlane &pl)
void setNhits(int n)
Definition: GFBookkeeping.h:50
void copySmoothing(GFTrack *source, GFTrack *target, int target_ire)
Copy the smoothing matrices from the source track to the target.
Definition: GFDaf.cxx:238
std::vector< std::string > getMatrixKeys()
virtual TMatrixT< double > getHitCov(const GFDetPlane &)=0
Get hit covariances in a specific detector plane.
void processTrack(GFTrack *trk)
Process a track using the DAF.
Definition: GFDaf.cxx:30
void processTrack(GFTrack *trk)
Performs fit on a GFTrack.
Definition: GFKalman.cxx:40
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
void info()
print information in the exception object
Definition: GFException.cxx:52
Base Class for representing a Hit in GENFIT.
Definition: GFAbsRecoHit.h:73
GFAbsTrackRep * getTrackRep(int id) const
Accessor for track representations.
Definition: GFTrack.h:186
void releaseTrackReps()
Clear TrackRep vector. Note that the Reps will not be deleted!
Definition: GFTrack.h:174
GFTrack * trk
Definition: checkgenfit.C:13
void setBetas(double b1, double b2, double b3=-1., double b4=-1., double b5=-1., double b6=-1., double b7=-1., double b8=-1., double b9=-1., double b10=-1.)
Configure the annealing scheme.
Definition: GFDaf.cxx:174
void setMatrix(std::string key, unsigned int index, const TMatrixT< double > &mat)
int hit(Int_t nEvents=0, TString inFile="sim.root", TString parFile="par.root", TString inDigi="digi.root", TString outFile="hit.root", Int_t timeBased=0)
Definition: hit.C:1
void bookMatrices(std::string key)
bool getMatrix(std::string key, unsigned int index, TMatrixT< double > &mat)
void addTrackRep(GFAbsTrackRep *theTrackRep)
Add track represenation.
Definition: GFTrack.h:318
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:45
GFAbsRecoHit * getHit(int id) const
Definition: GFTrack.h:144
std::vector< double > fBeta
Definition: GFDaf.h:100
void setStatusFlag(int _val)
void setFatal(bool b=true)
set fatal flag. if this is true, the fit stops for this current track repr.
Definition: GFException.h:77
CbmHit * hits[nHits]
Definition: RiemannTest.C:19
GFDaf()
Definition: GFDaf.cxx:21
void setNumIterations(Int_t i)
Set number of iterations for Kalman Filter.
Definition: GFKalman.h:86
Double_t Pi
virtual TMatrixT< double > getHitCoord(const GFDetPlane &)=0
Get hit coordinates in a specific detector plane.