EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GFDaf.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GFDaf.cxx
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);
26 
27 };
28 
30 
31  fWeights.clear();
32 
33  std::vector<GFDafHit*> eff_hits = initHitsWeights(trk);
34  if(eff_hits.size() == 0) {
35  for(unsigned int i=0; i<trk->getNumReps(); i++) {
36  trk->getTrackRep(i)->setStatusFlag(1);
37  }
38  return;
39  }
40 
41  GFTrack* mini_trk = new GFTrack();
42 
43  for(unsigned int j=0; j<eff_hits.size(); j++) mini_trk->addHit(eff_hits.at(j));
44 
45  mini_trk->setSmoothing();
46 
47  for(unsigned int i=0; i<trk->getNumReps(); i++) {
48 
49  trk->getBK(i)->setNhits(trk->getNumHits());
50  if(trk->getTrackRep(i)->getStatusFlag()!=0) continue;
51 
52  mini_trk->addTrackRep(trk->getTrackRep(i));
53 
54  for(unsigned int iBeta=0; iBeta<fBeta.size(); iBeta++) {
55 
56  for(unsigned int j=0; j<mini_trk->getNumHits(); j++) {
57  GFDafHit* hit = static_cast<GFDafHit*>(mini_trk->getHit(j));
58  hit->setWeights(fWeights.at(i).at(j));
59  }
60  if ( iBeta != 0){
61  GFKalman::blowUpCovs(mini_trk);
62  }
63  GFKalman::processTrack(mini_trk);
64 
65  if(mini_trk->getTrackRep(0)->getStatusFlag() != 0) break;
66 
67  if(iBeta != fBeta.size()-1 )
68  try{
69  fWeights.at(i) = calcWeights(mini_trk, fBeta.at(iBeta));
70  } catch(GFException& e) {
71  std::cerr<<e.what();
72  e.info();
73  mini_trk->getTrackRep(0)->setStatusFlag(1);
74  break;
75  }
76 
77  }
78 
79  if(trk->getSmoothing()) copySmoothing(mini_trk, trk, i);
80 
81  mini_trk->releaseTrackReps();
82 
83  }
84 
85  saveWeights(trk, fWeights);
86 
87  delete mini_trk;
88 
89 };
90 
91 std::vector<std::vector<double> > GFDaf::calcWeights(GFTrack* trk, double beta) {
92 
93  std::vector<std::vector<double> > ret_val;
94 
95  for(unsigned int i=0; i<trk->getNumHits(); i++) {
96 
97  GFDafHit* eff_hit = static_cast<GFDafHit*>(trk->getHit(i));
98  std::vector<double> phi;
99  double phi_sum = 0;
100  double phi_cut = 0;
101 
102  std::vector<double> weights;
103 
104  TMatrixT<double> x_smoo(GFTools::getBiasedSmoothedPos(trk, 0, i));
105  TMatrixT<double> smoothedState,smoothedCov;
106  GFTools::getBiasedSmoothedData(trk, 0, i, smoothedState,smoothedCov);
107  if(x_smoo.GetNrows() == 0) {
108  weights.assign(eff_hit->getNumHits(),0.5);
109  std::cout<<"Assumed weight 0.5!!"<<std::endl;
110  ret_val.push_back(weights);
111  continue;
112  }
113 
114  for(unsigned int j=0; j<eff_hit->getNumHits(); j++) {
115  GFAbsRecoHit* real_hit = eff_hit->getHit(j);
116  GFDetPlane pl;
117  TMatrixT<double> m,Vorig;
118  try{
119  pl = real_hit->getDetPlane(trk->getTrackRep(0));
120  real_hit->getMeasurement(trk->getTrackRep(0),pl,smoothedState,smoothedCov,m,Vorig);
121  } catch(GFException& e) {
122  std::cerr<<e.what();
123  e.info();
124  continue; //m and Vorig do not contain sensible values, skip hit
125  }
126 
127  TMatrixT<double> V( beta * Vorig);
128  TMatrixT<double> resid(m - x_smoo);
129  TMatrixT<double> resid_T(resid);
130  resid_T.T();
131  double detV = V.Determinant();
132  TMatrixT<double> Vinv;
133  GFTools::invertMatrix(V,Vinv);
134 
135  phi.push_back((1./(pow(2.*TMath::Pi(),V.GetNrows()/2.)*sqrt(detV)))*exp(-0.5*(resid_T * Vinv * resid)[0][0]));
136  phi_sum += phi.at(j);
137 
138  double cutVal = fchi2Cuts[V.GetNrows()];
139  assert(cutVal>1.E-6);
140  phi_cut += (1./(pow(2.*TMath::Pi(),V.GetNrows()/2.)*sqrt(detV)))*exp(-0.5*cutVal/beta);
141 
142  }
143 
144  for(unsigned int j=0; j<eff_hit->getNumHits(); j++) {
145 
146  weights.push_back(phi.at(j)/(phi_sum+phi_cut));
147 
148  }
149  ret_val.push_back(weights);
150 
151  }
152 
153  return ret_val;
154 
155 };
156 
157 void GFDaf::setProbCut(double val){
158 
159  if(fabs(val-0.01)<1.E-10){
160  fchi2Cuts[1] = 6.63;
161  fchi2Cuts[2] = 9.21;
162  fchi2Cuts[3] = 11.34;
163  fchi2Cuts[4] = 13.23;
164  fchi2Cuts[5] = 15.09;
165  }
166  else if(fabs(val-0.005)<1.E-10){
167  fchi2Cuts[1] = 7.88;
168  fchi2Cuts[2] = 10.60;
169  fchi2Cuts[3] = 12.84;
170  fchi2Cuts[4] = 14.86;
171  fchi2Cuts[5] = 16.75;
172  }
173  else if(fabs(val-0.001)<1.E-10){
174  fchi2Cuts[1] = 10.83;
175  fchi2Cuts[2] = 13.82;
176  fchi2Cuts[3] = 16.27;
177  fchi2Cuts[4] = 18.47;
178  fchi2Cuts[5] = 20.51;
179  }
180  else{
181  GFException exc("GFDafsetProbCut() value is not supported",__LINE__,__FILE__);
182  exc.setFatal();
183  throw exc;
184  }
185 
186 }
187 
188 void GFDaf::setBetas(double b1,double b2,double b3,double b4,double b5,double b6,double b7,double b8,double b9,double b10){
189  fBeta.clear();
190  assert(b1>0);fBeta.push_back(b1);
191  if(b2>0){
192  assert(b2<=b1);fBeta.push_back(b2);
193  if(b3>=0.) {
194  assert(b3<=b2);fBeta.push_back(b3);
195  if(b4>=0.) {
196  assert(b4<=b3);fBeta.push_back(b4);
197  if(b5>=0.) {
198  assert(b5<=b4);fBeta.push_back(b5);
199  if(b6>=0.) {
200  assert(b6<=b5);fBeta.push_back(b6);
201  if(b7>=0.) {
202  assert(b7<=b6);fBeta.push_back(b7);
203  if(b8>=0.) {
204  assert(b8<=b7);fBeta.push_back(b8);
205  if(b9>=0.) {
206  assert(b9<=b8);fBeta.push_back(b9);
207  if(b10>=0.) {
208  assert(b10<=b9);fBeta.push_back(b10);
209  }
210  }
211  }
212  }
213  }
214  }
215  }
216  }
217  }
218 }
219 
220 std::vector<GFDafHit*> GFDaf::initHitsWeights(GFTrack* trk) {
221 
222  std::vector<GFDafHit*> eff_hits;
223 
224  std::vector< std::vector<int>* > planes;
225  if(not trk->getHitsByPlane(planes)) return eff_hits;
226  int nPlanes = planes.size();
227 
228  for(int i=0; i<nPlanes; i++) {
229 
230  std::vector<GFAbsRecoHit*> hits;
231 
232  for(unsigned int j=0; j<planes.at(i)->size(); j++) {
233  hits.push_back(trk->getHit(planes.at(i)->at(j)) );
234  }
235 
236  GFDafHit* eff_hit = new GFDafHit(hits);
237  eff_hits.push_back(eff_hit);
238 
239  }
240 
241  for(unsigned int i=0; i<trk->getNumReps(); i++) {
242  std::vector<std::vector<double> > rep_weights;
243  for(unsigned int j=0; j<eff_hits.size(); j++) {
244  std::vector<double> single_weights;
245  single_weights.assign(eff_hits.at(j)->getNumHits(),1.);
246  rep_weights.push_back(single_weights);
247  }
248  fWeights.push_back(rep_weights);
249  }
250 
251  return eff_hits;
252 
253 }
254 
255 void GFDaf::copySmoothing(GFTrack* source, GFTrack* target, int target_irep) {
256 
257  for(unsigned int i=0; i<target->getNumReps(); i++) {
258 
259  std::vector<std::string> mat_keys = target->getBK(i)->getMatrixKeys();
260  bool already_there = false;
261  for(unsigned int j=0; j<mat_keys.size(); j++) {
262  if(mat_keys.at(j) == "fUpSt") already_there = true;
263  }
264 
265  if(!already_there) {
266  target->getBK(i)->bookMatrices("fUpSt");
267  target->getBK(i)->bookMatrices("fUpCov");
268  target->getBK(i)->bookMatrices("bUpSt");
269  target->getBK(i)->bookMatrices("bUpCov");
270  target->getBK(i)->bookGFDetPlanes("fPl");
271  target->getBK(i)->bookGFDetPlanes("bPl");
272  if(target->getTrackRep(i)->hasAuxInfo()) {
273  target->getBK(i)->bookMatrices("fAuxInfo");
274  target->getBK(i)->bookMatrices("bAuxInfo");
275  }
276  }
277  }
278 
279  int hit_count = 0;
280 
281  for(unsigned int pl_i=0; pl_i<source->getNumHits(); pl_i++) {
282 
283  GFDafHit* eff_hit = static_cast<GFDafHit*>(source->getHit(pl_i));
284 
285  for(unsigned int hit_i=0; hit_i<eff_hit->getNumHits(); hit_i++) {
286 
287  TMatrixT<double> fUpSt, fUpCov, bUpSt, bUpCov, fAuxInfo, bAuxInfo;
288  GFDetPlane fPl, bPl;
289 
290  source->getBK(0)->getMatrix("fUpSt",pl_i,fUpSt);
291  source->getBK(0)->getMatrix("fUpCov",pl_i,fUpCov);
292  source->getBK(0)->getDetPlane("fPl",pl_i,fPl);
293  source->getBK(0)->getMatrix("bUpSt",pl_i,bUpSt);
294  source->getBK(0)->getMatrix("bUpCov",pl_i,bUpCov);
295  source->getBK(0)->getDetPlane("bPl",pl_i,bPl);
296 
297  if(source->getTrackRep(0)->hasAuxInfo()) {
298  source->getBK(0)->getMatrix("fAuxInfo",pl_i,fAuxInfo);
299  source->getBK(0)->getMatrix("bAuxInfo",pl_i,bAuxInfo);
300  target->getBK(target_irep)->setMatrix("fAuxInfo",hit_count,fAuxInfo);
301  target->getBK(target_irep)->setMatrix("bAuxInfo",hit_count,bAuxInfo);
302  }
303 
304  target->getBK(target_irep)->setMatrix("fUpSt",hit_count,fUpSt);
305  target->getBK(target_irep)->setMatrix("fUpCov",hit_count,fUpCov);
306  target->getBK(target_irep)->setDetPlane("fPl",hit_count,fPl);
307  target->getBK(target_irep)->setMatrix("bUpSt",hit_count,bUpSt);
308  target->getBK(target_irep)->setMatrix("bUpCov",hit_count,bUpCov);
309  target->getBK(target_irep)->setDetPlane("bPl",hit_count,bPl);
310 
311  hit_count++;
312 
313  }
314 
315  }
316 
317 }
318 
319 void GFDaf::saveWeights(GFTrack* trk, const std::vector<std::vector<std::vector<double> > >& weights) const {
320 
321  assert(weights.size() == trk->getNumReps());
322 
323  for(unsigned int rep_i = 0; rep_i < weights.size(); rep_i++) {
324  GFBookkeeping* bk = trk->getBK(rep_i);
325  bk->bookNumbers("dafWeight");
326  unsigned int hit_i = 0;
327  for(unsigned int dafhit_i = 0; dafhit_i < weights.at(rep_i).size(); dafhit_i++) {
328  for(unsigned int hit_on_pl_i = 0; hit_on_pl_i < weights.at(rep_i).at(dafhit_i).size(); hit_on_pl_i++) {
329  bk->setNumber("dafWeight", hit_i, weights.at(rep_i).at(dafhit_i).at(hit_on_pl_i));
330  hit_i++;
331  }
332  }
333  assert(hit_i == trk->getNumHits());
334  }
335 
336 }
337