CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTSegmentUpdator.cc
Go to the documentation of this file.
1 
12 /* This Class Header */
14 
15 /* Collaborating Class Header */
16 
17 //mene
19 
26 
30 
34 
39 
40 /* C++ Headers */
41 #include <string>
42 
43 using namespace std;
44 using namespace edm;
45 
46 /* ====================================================================== */
47 
50  theFitter(new DTLinearFit()) ,
51  vdrift_4parfit(config.getParameter<bool>("performT0_vdriftSegCorrection")),
52  T0_hit_resolution(config.getParameter<double>("hit_afterT0_resolution")),
53  perform_delta_rejecting(config.getParameter<bool>("perform_delta_rejecting")),
54  debug(config.getUntrackedParameter<bool>("debug",false))
55 {
56  string theAlgoName = config.getParameter<string>("recAlgo");
57  theAlgo = DTRecHitAlgoFactory::get()->create(theAlgoName,
58  config.getParameter<ParameterSet>("recAlgoConfig"));
59 
60  if(debug)
61  cout << "[DTSegmentUpdator] Constructor called" << endl;
62 }
63 
66  delete theFitter;
67 }
68 
69 /* Operations */
70 
72  setup.get<MuonGeometryRecord>().get(theGeom);
73  theAlgo->setES(setup);
74 }
75 
76 void DTSegmentUpdator::update(DTRecSegment4D* seg, const bool calcT0) const {
77 
78  if(debug) cout << "[DTSegmentUpdator] Starting to update the segment" << endl;
79 
80  const bool hasPhi = seg->hasPhi();
81  const bool hasZed = seg->hasZed();
82 
83  //reject the bad hits (due to delta rays)
84  if(perform_delta_rejecting && hasPhi) rejectBadHits(seg->phiSegment());
85 
86  int step = (hasPhi && hasZed) ? 3 : 2;
87  if(calcT0) step = 4;
88 
89  GlobalPoint pos = theGeom->idToDet(seg->geographicalId())->toGlobal(seg->localPosition());
90  GlobalVector dir = theGeom->idToDet(seg->geographicalId())->toGlobal(seg->localDirection());
91 
92  if(calcT0) calculateT0corr(seg);
93 
94  if(hasPhi) updateHits(seg->phiSegment(),pos,dir,step);
95  if(hasZed) updateHits(seg->zSegment() ,pos,dir,step);
96 
97  fit(seg);
98 }
99 
101  GlobalPoint pos = (theGeom->idToDet(seg->geographicalId()))->toGlobal(seg->localPosition());
102  GlobalVector dir = (theGeom->idToDet(seg->geographicalId()))->toGlobal(seg->localDirection());
103 
104  updateHits(seg,pos,dir);
105  fit(seg);
106 }
107 
109  // after the update must refit the segments
110  if(seg->hasPhi()) fit(seg->phiSegment());
111  if(seg->hasZed()) fit(seg->zSegment());
112 
113  const DTChamber* theChamber = theGeom->chamber(seg->chamberId());
114 
115  if(seg->hasPhi() && seg->hasZed() ) {
116 
117  DTChamberRecSegment2D *segPhi=seg->phiSegment();
118  DTSLRecSegment2D *segZed=seg->zSegment();
119 
120  // NB Phi seg is already in chamber ref
121  LocalPoint posPhiInCh = segPhi->localPosition();
122  LocalVector dirPhiInCh= segPhi->localDirection();
123 
124  // Zed seg is in SL one
125  const DTSuperLayer* zSL = theChamber->superLayer(segZed->superLayerId());
126  LocalPoint zPos(segZed->localPosition().x(),
127  (zSL->toLocal(theChamber->toGlobal(segPhi->localPosition()))).y(),
128  0.);
129 
130  LocalPoint posZInCh = theChamber->toLocal(zSL->toGlobal(zPos));
131 
132  LocalVector dirZInCh = theChamber->toLocal(zSL->toGlobal(segZed->localDirection()));
133 
134  LocalPoint posZAt0 = posZInCh + dirZInCh*(-posZInCh.z())/cos(dirZInCh.theta());
135 
136  // given the actual definition of chamber refFrame, (with z poiniting to IP),
137  // the zed component of direction is negative.
138  LocalVector dir=LocalVector(dirPhiInCh.x()/fabs(dirPhiInCh.z()),dirZInCh.y()/fabs(dirZInCh.z()),-1.);
139 
140  seg->setPosition(LocalPoint(posPhiInCh.x(),posZAt0.y(),0.));
141  seg->setDirection(dir.unit());
142 
143  AlgebraicSymMatrix mat(4);
144 
145  // set cov matrix
146  mat[0][0] = segPhi->parametersError()[0][0]; //sigma (dx/dz)
147  mat[0][2] = segPhi->parametersError()[0][1]; //cov(dx/dz,x)
148  mat[2][2] = segPhi->parametersError()[1][1]; //sigma (x)
149 
150  seg->setCovMatrix(mat);
151  seg->setCovMatrixForZed(posZInCh);
152 
153  }
154 
155  else if (seg->hasPhi()) {
156  DTChamberRecSegment2D *segPhi=seg->phiSegment();
157 
158  seg->setPosition(segPhi->localPosition());
159  seg->setDirection(segPhi->localDirection());
160 
161  AlgebraicSymMatrix mat(4);
162  // set cov matrix
163  mat[0][0] = segPhi->parametersError()[0][0]; //sigma (dx/dz)
164  mat[0][2] = segPhi->parametersError()[0][1]; //cov(dx/dz,x)
165  mat[2][2] = segPhi->parametersError()[1][1]; //sigma (x)
166 
167  seg->setCovMatrix(mat);
168  }
169 
170  else if (seg->hasZed()) {
171  DTSLRecSegment2D *segZed = seg->zSegment();
172 
173  // Zed seg is in SL one
174  GlobalPoint glbPosZ = ( theGeom->superLayer(segZed->superLayerId()) )->toGlobal(segZed->localPosition());
175  LocalPoint posZInCh = ( theGeom->chamber(segZed->superLayerId().chamberId()) )->toLocal(glbPosZ);
176 
177  GlobalVector glbDirZ = (theGeom->superLayer(segZed->superLayerId()) )->toGlobal(segZed->localDirection());
178  LocalVector dirZInCh = (theGeom->chamber(segZed->superLayerId().chamberId()) )->toLocal(glbDirZ);
179 
180  LocalPoint posZAt0 = posZInCh+
181  dirZInCh*(-posZInCh.z())/cos(dirZInCh.theta());
182 
183  seg->setPosition(posZAt0);
184  seg->setDirection(dirZInCh);
185 
186  AlgebraicSymMatrix mat(4);
187  // set cov matrix
188  seg->setCovMatrix(mat);
189  seg->setCovMatrixForZed(posZInCh);
190  }
191 }
192 
194  if (!seg->good()) return false;
195 
196  vector<float> x;
197  vector<float> y;
198  vector<float> sigy;
199 
200  DTSegmentCand::AssPointCont hits=seg->hits();
201  for (DTSegmentCand::AssPointCont::const_iterator iter=hits.begin();
202  iter!=hits.end(); ++iter) {
203  LocalPoint pos = (*iter).first->localPosition((*iter).second);
204  x.push_back(pos.z());
205  y.push_back(pos.x());
206  sigy.push_back(sqrt((*iter).first->localPositionError().xx()));
207  }
208 
209  LocalPoint pos;
211  AlgebraicSymMatrix covMat(2);
212 
213  double chi2 = 0.;
214  fit(x,y,sigy,pos,dir,covMat,chi2);
215 
216  seg->setPosition(pos);
217  seg->setDirection(dir);
218 
219  //cout << "pos " << segPosition<< endl;
220  //cout << "dir " << segDirection<< endl;
221 
222  seg->setCovMatrix(covMat);
223  // cout << "Mat " << covMat << endl;
224 
225  seg->setChi2(chi2);
226  return true;
227 }
228 
230  // WARNING: since this method is called both with a 2D and a 2DPhi as argument
231  // seg->geographicalId() can be a superLayerId or a chamberId
232 
233  vector<float> x;
234  vector<float> y;
235  vector<float> sigy;
236 
237  vector<DTRecHit1D> hits=seg->specificRecHits();
238  for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
239  hit!=hits.end(); ++hit) {
240 
241  // I have to get the hits position (the hit is in the layer rf) in SL frame...
242  GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
243  LocalPoint pos = ( theGeom->idToDet(seg->geographicalId()) )->toLocal(glbPos);
244 
245  x.push_back(pos.z());
246  y.push_back(pos.x());
247 
248  // Get local error in SL frame
249  //RB: is it right in this way?
251  GlobalError glbErr =
252  tran.transform( hit->localPositionError(),(theGeom->layer( hit->wireId().layerId() ))->surface());
253  LocalError slErr =
254  tran.transform( glbErr, (theGeom->idToDet(seg->geographicalId()))->surface());
255 
256  sigy.push_back(sqrt(slErr.xx()));
257  }
258 
259  LocalPoint pos;
261  AlgebraicSymMatrix covMat(2);
262  double chi2 = 0.;
263 
264  fit(x,y,sigy,pos,dir,covMat,chi2);
265 
266  seg->setPosition(pos);
267  seg->setDirection(dir);
268 
269  //cout << "pos " << segPosition << endl;
270  //cout << "dir " << segDirection << endl;
271 
272  seg->setCovMatrix(covMat);
273  // cout << "Mat " << mat << endl;
274 
275  seg->setChi2(chi2);
276 }
277 
278 void DTSegmentUpdator::fit(const vector<float>& x,
279  const vector<float>& y,
280  const vector<float>& sigy,
281  LocalPoint& pos,
282  LocalVector& dir,
283  AlgebraicSymMatrix& covMatrix,
284  double& chi2) const {
285 
286  float slope = 0.;
287  float intercept = 0.;
288  float covss = 0.;
289  float covii = 0.;
290  float covsi = 0.;
291 
292  // do the fit
293  theFitter->fit(x,y,x.size(),sigy,slope,intercept,covss,covii,covsi);
294  // cout << "slope " << slope << endl;
295  // cout << "intercept " << intercept << endl;
296 
297  // intercept is the x() in chamber frame when the segment cross the chamber
298  // plane (at z()=0), the y() is not measured, so let's put the center of the
299  // chamber.
300  pos = LocalPoint(intercept,0.,0.);
301 
302  // slope is dx()/dz(), while dy()/dz() is by definition 0, finally I want the
303  // segment to point outward, so opposite to local z
304  dir = LocalVector(-slope,0.,-1.).unit();
305 
306  covMatrix = AlgebraicSymMatrix(2);
307  covMatrix[0][0] = covss; // this is var(dy/dz)
308  covMatrix[1][1] = covii; // this is var(y)
309  covMatrix[1][0] = covsi; // this is cov(dy/dz,y)
310 
311  /* Calculate chi2. */
312  chi2 = 0.;
313  for(unsigned int i=0; i<x.size() ; ++i) {
314  double resid= y[i] - (intercept + slope*x[i]);
315  chi2 += (resid/sigy[i])*(resid/sigy[i]);
316  }
317 }
318 
319 // The GlobalPoint and the GlobalVector can be either the glb position and the direction
320 // of the 2D-segment itself or the glb position and direction of the 4D segment
322  GlobalVector &gdir, const int step) const{
323 
324  // it is not necessary to have DTRecHit1D* to modify the obj in the container
325  // but I have to be carefully, since I cannot make a copy before the iteration!
326 
327  vector<DTRecHit1D> toBeUpdatedRecHits = seg->specificRecHits();
328  vector<DTRecHit1D> updatedRecHits;
329 
330  for (vector<DTRecHit1D>::iterator hit= toBeUpdatedRecHits.begin();
331  hit!=toBeUpdatedRecHits.end(); ++hit) {
332 
333  const DTLayer* layer = theGeom->layer( hit->wireId().layerId() );
334 
335  LocalPoint segPos=layer->toLocal(gpos);
336  LocalVector segDir=layer->toLocal(gdir);
337 
338  // define impact angle needed by the step 2
339  const float angle = atan(segDir.x()/-segDir.z());
340 
341  // define the local position (extr.) of the segment. Needed by the third step
342  LocalPoint segPosAtLayer=segPos+segDir*(-segPos.z())/cos(segDir.theta());
343 
344  DTRecHit1D newHit1D = (*hit);
345 
346  bool ok = true;
347 
348  if (step == 2) {
349  ok = theAlgo->compute(layer,*hit,angle,newHit1D);
350 
351  } else if (step == 3) {
352 
353  LocalPoint hitPos(hit->localPosition().x(),+segPosAtLayer.y(),0.);
354 
355  GlobalPoint glbpos= theGeom->layer( hit->wireId().layerId() )->toGlobal(hitPos);
356 
357  newHit1D.setPosition(hitPos);
358 
359  ok = theAlgo->compute(layer,*hit,angle,glbpos,newHit1D);
360 
361  } else if (step == 4) {
362 
363  //const double vminf = seg->vDrift(); // vdrift correction are recorded in the segment
364  double vminf =0.;
365  if(vdrift_4parfit) vminf = seg->vDrift(); // use vdrift recorded in the segment only if vdrift_4parfit=True
366 
367  double cminf = 0.;
368  if(seg->ist0Valid()) cminf = - seg->t0()*0.00543;
369 
370  //cout << "In updateHits: t0 = " << seg->t0() << endl;
371  //cout << "In updateHits: vminf = " << vminf << endl;
372  //cout << "In updateHits: cminf = " << cminf << endl;
373 
374  const float xwire = layer->specificTopology().wirePosition(hit->wireId().wire());
375  const float distance = fabs(hit->localPosition().x() - xwire);
376 
377  const int ilc = ( hit->lrSide() == DTEnums::Left ) ? 1 : -1;
378 
379  const double dy_corr = (vminf*ilc*distance-cminf*ilc );
380 
381  LocalPoint point(hit->localPosition().x() + dy_corr, +segPosAtLayer.y(), 0.);
382 
384 
385  newHit1D.setPositionAndError(point, error);
386 
387  //FIXME: check that the hit is still inside the cell
388  ok = true;
389 
390  } else throw cms::Exception("DTSegmentUpdator")<<" updateHits called with wrong step " << endl;
391 
392  if (ok) updatedRecHits.push_back(newHit1D);
393  else {
394  LogError("DTSegmentUpdator")<<"DTSegmentUpdator::updateHits failed update" << endl;
395  throw cms::Exception("DTSegmentUpdator")<<"updateHits failed update"<<endl;
396  }
397 
398  }
399  seg->update(updatedRecHits);
400 }
401 
403 
404  vector<float> x;
405  vector<float> y;
406 
407  if(debug) cout << " Inside the segment updator, now loop on hits: ( x == z_loc , y == x_loc) " << endl;
408 
409  vector<DTRecHit1D> hits = phiSeg->specificRecHits();
410  for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
411  hit!=hits.end(); ++hit) {
412 
413  // I have to get the hits position (the hit is in the layer rf) in SL frame...
414  GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
415  LocalPoint pos = ( theGeom->idToDet(phiSeg->geographicalId()) )->toLocal(glbPos);
416 
417  x.push_back(pos.z());
418  y.push_back(pos.x());
419  }
420 
421  if(debug){
422  cout << " end of segment! " << endl;
423  cout << " size = Number of Hits: " << x.size() << " " << y.size() << endl;
424  }
425 
426  // Perform the 2 par fit:
427  float par[2]={0.,0.}; // q , m
428 
429  //variables to perform the fit:
430  float Sx = 0.;
431  float Sy = 0.;
432  float Sx2 = 0.;
433  float Sy2 = 0.;
434  float Sxy = 0.;
435 
436  const int N = x.size();
437 
438  for(int i = 0; i < N;++i){
439  Sx += x.at(i);
440  Sy += y.at(i);
441  Sx2 += x.at(i)*x.at(i);
442  Sy2 += y.at(i)*y.at(i);
443  Sxy += x.at(i)*y.at(i);
444 
445  }
446 
447  const float delta = N*Sx2 - Sx*Sx;
448  par[0] = ( Sx2*Sy - Sx*Sxy )/delta;
449  par[1] = ( N*Sxy - Sx*Sy )/delta;
450 
451  if(debug) cout << "fit 2 parameters done ----> par0: "<< par[0] << " par1: "<< par[1] << endl;
452 
453  // Calc residuals:
454  float residuals[N];
455 
456  for(int i = 0; i < N;++i)
457  residuals[i] = 0;
458 
459  for(int i = 0; i < N;++i)
460  residuals[i] = y.at(i) - par[1]*x.at(i) - par[0];
461 
462  if(debug) cout << " Residuals computed! "<< endl;
463 
464 
465  // Perform bad hit rejecting -- update hits
466  vector<DTRecHit1D> updatedRecHits;
467 
468  float mean_residual = 0.; //mean of the absolute values of residuals
469 
470  for (int i = 0; i < N; ++i)
471  mean_residual += fabs(residuals[i]);
472 
473  mean_residual = mean_residual/(N - 2);
474 
475  if(debug) cout << " mean_residual: "<< mean_residual << endl;
476 
477  int i = 0;
478 
479  for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
480  hit!=hits.end(); ++hit) {
481 
482  DTRecHit1D newHit1D = (*hit);
483 
484  if(fabs(residuals[i])/mean_residual < 1.5){
485 
486  updatedRecHits.push_back(newHit1D);
487  if(debug) cout << " accepted "<< i+1 << "th hit" <<" Irej: " << fabs(residuals[i])/mean_residual << endl;
488  ++i;
489  }
490  else {
491  if(debug) cout << " rejected "<< i+1 << "th hit" <<" Irej: " << fabs(residuals[i])/mean_residual << endl;
492  ++i;
493  continue;
494  }
495  }
496 
497  phiSeg->update(updatedRecHits);
498 
499  //final check!
500  if(debug){
501 
502  vector<float> x_upd;
503  vector<float> y_upd;
504 
505  cout << " Check the update action: " << endl;
506 
507  vector<DTRecHit1D> hits_upd = phiSeg->specificRecHits();
508  for (vector<DTRecHit1D>::const_iterator hit=hits_upd.begin();
509  hit!=hits_upd.end(); ++hit) {
510 
511  // I have to get the hits position (the hit is in the layer rf) in SL frame...
512  GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
513  LocalPoint pos = ( theGeom->idToDet(phiSeg->geographicalId()) )->toLocal(glbPos);
514 
515  x_upd.push_back(pos.z());
516  y_upd.push_back(pos.x());
517 
518  cout << " x_upd: "<< pos.z() << " y_upd: "<< pos.x() << endl;
519 
520 
521  }
522 
523  cout << " end of segment! " << endl;
524  cout << " size = Number of Hits: " << x_upd.size() << " " << y_upd.size() << endl;
525 
526  }// end debug
527 
528  return;
529 } //end DTSegmentUpdator::rejectBadHits
530 
532  if(seg->hasPhi()) calculateT0corr(seg->phiSegment());
533  if(seg->hasZed()) calculateT0corr(seg->zSegment());
534 }
535 
537  // WARNING: since this method is called both with a 2D and a 2DPhi as argument
538  // seg->geographicalId() can be a superLayerId or a chamberId
539 
540  vector<double> d_drift;
541  vector<float> x;
542  vector<float> y;
543  vector<int> lc;
544 
545  vector<DTRecHit1D> hits=seg->specificRecHits();
546 
547  DTWireId wireId;
548  int nptfit = 0;
549 
550  for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
551  hit!=hits.end(); ++hit) {
552 
553  // I have to get the hits position (the hit is in the layer rf) in SL frame...
554  GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
555  LocalPoint pos = ( theGeom->idToDet(seg->geographicalId()) )->toLocal(glbPos);
556 
557  const DTLayer* layer = theGeom->layer( hit->wireId().layerId() );
558  float xwire = layer->specificTopology().wirePosition(hit->wireId().wire());
559  float distance = fabs(hit->localPosition().x() - xwire);
560 
561  int ilc = ( hit->lrSide() == DTEnums::Left ) ? 1 : -1;
562 
563  nptfit++;
564  x.push_back(pos.z());
565  y.push_back(pos.x());
566  lc.push_back(ilc);
567  d_drift.push_back(distance);
568 
569  // cout << " d_drift "<<distance <<" npt= " <<npt<<endl;
570  }
571 
572  double chi2fit = 0.;
573  float cminf = 0.;
574  double vminf = 0.;
575 
576  if ( nptfit > 2 ) {
577  //NB chi2fit is normalized
578  Fit4Var(x,y,lc,d_drift,nptfit,cminf,vminf,chi2fit);
579 
580  double t0cor = -999.;
581  if(cminf > -998.) t0cor = - cminf/0.00543 ; // in ns
582 
583  //cout << "In calculateT0corr: t0 = " << t0cor << endl;
584  //cout << "In calculateT0corr: vminf = " << vminf << endl;
585  //cout << "In calculateT0corr: cminf = " << cminf << endl;
586  //cout << "In calculateT0corr: chi2 = " << chi2fit << endl;
587 
588  seg->setT0(t0cor); // time and
589  seg->setVdrift(vminf); // vdrift correction are recorded in the segment
590  }
591 }
592 
593 
594 void DTSegmentUpdator::Fit4Var(const vector<float>& xfit,
595  const vector<float>& yfit,
596  const vector<int>& lfit,
597  const vector<double>& tfit,
598  const int nptfit,
599  float& cminf,
600  double& vminf,
601  double& chi2fit) const {
602 
603  const double sigma = 0.0295;// errors can be inserted .just load them/that is the usual TB resolution value for DT chambers
604  double aminf = 0.;
605  double bminf = 0.;
606  int nppar = 0;
607  double sx = 0.;
608  double sx2 = 0.;
609  double sy = 0.;
610  double sxy = 0.;
611  double sl = 0.;
612  double sl2 = 0.;
613  double sly = 0.;
614  double slx = 0.;
615  double st = 0.;
616  double st2 = 0.;
617  double slt = 0.;
618  double sltx = 0.;
619  double slty = 0.;
620  double chi2fitN2 = -1. ;
621  double chi2fit3 = -1.;
622  double chi2fitN3 = -1. ;
623  double chi2fitN4 = -1.;
624  float bminf3 = bminf;
625  float aminf3 = aminf;
626  float cminf3 = cminf;
627  int nppar2 = 0;
628  int nppar3 = 0;
629  int nppar4 = 0;
630 
631  cminf = -999.;
632  vminf = 0.;
633 
634  for (int j=0; j<nptfit; j++){
635  sx = sx + xfit[j];
636  sy = sy + yfit[j];
637  sx2 = sx2 + xfit[j]*xfit[j];
638  sxy = sxy + xfit[j]*yfit[j];
639  sl = sl + lfit[j];
640  sl2 = sl2 + lfit[j]*lfit[j];
641  sly = sly + lfit[j]*yfit[j];
642  slx = slx + lfit[j]*xfit[j];
643  st = st + tfit[j];
644  st2 = st2 + tfit[j] * tfit[j];
645  slt = slt + lfit[j] * tfit[j];
646  sltx = sltx + lfit[j] * tfit[j]*xfit[j];
647  slty = slty + lfit[j] * tfit[j]*yfit[j];
648 
649  } //end loop
650 
651  const double delta = nptfit*sx2 - sx*sx;
652 
653  double a = 0.;
654  double b = 0.;
655 
656  if (delta!=0){ //
657  a = (sx2*sy - sx*sxy)/delta;
658  b = (nptfit*sxy - sx*sy)/delta;
659 
660  // cout << " NPAR=2 : slope = "<<b<< " intercept = "<<a <<endl;
661  for (int j=0; j<nptfit; j++){
662  const double ypred = a + b*xfit[j];
663  const double dy = (yfit[j] - ypred)/sigma;
664  chi2fit = chi2fit + dy*dy;
665  } //end loop chi2
666  }
667 
668  bminf = b;
669  aminf = a;
670 
671  nppar = 2;
672  nppar2 = nppar;
673 
674  chi2fitN2 = chi2fit/(nptfit-2);
675 
676  // cout << "dt0 = 0chi2fit = " << chi2fit << " slope = "<<b<<endl;
677 
678  if (nptfit >= 3) {
679 
680  const double d1 = sy;
681  const double d2 = sxy;
682  const double d3 = sly;
683  const double c1 = sl;
684  const double c2 = slx;
685  const double c3 = sl2;
686  const double b1 = sx;
687  const double b2 = sx2;
688  const double b3 = slx;
689  const double a1 = nptfit;
690  const double a2 = sx;
691  const double a3 = sl;
692 
693  //these parameters are not used in the 4-variables fit
694  const double b4 = b2*a1-b1*a2;
695  const double c4 = c2*a1-c1*a2;
696  const double d4 = d2*a1-d1*a2;
697  const double b5 = a1*b3-a3*b1;
698  const double c5 = a1*c3-a3*c1;
699  const double d5 = a1*d3-d1*a3;
700  const double a6 = slt;
701  const double b6 = sltx;
702  const double c6 = st;
703  const double v6 = st2;
704  const double d6 = slty;
705 
706  if (((c5*b4-c4*b5)*b4*a1)!=0) {
707  nppar = 3;
708  chi2fit = 0.;
709  cminf = (d5*b4-d4*b5)/(c5*b4-c4*b5);
710  bminf = d4/b4 -cminf *c4/b4;
711  aminf = (d1/a1 -cminf*c1/a1 -bminf*b1/a1);
712 
713  for (int j=0; j<nptfit; j++){
714  const double ypred = aminf + bminf*xfit[j];
715  const double dy = (yfit[j]-cminf*lfit[j] - ypred)/sigma;
716  chi2fit = chi2fit + dy*dy;
717 
718  } //end loop chi2
719  chi2fit3 = chi2fit;
720  if (nptfit>3)
721  chi2fitN3 = chi2fit /(nptfit-3);
722 
723  }
724  else {
725  cminf = -999.;
726  bminf = b;
727  aminf = a;
728  chi2fit3 = chi2fit;
729  chi2fitN3 = chi2fit /(nptfit-2);
730  }
731 
732  bminf3 = bminf;
733  aminf3 = aminf;
734  cminf3 = cminf;
735  nppar3 = nppar;
736 
737  if (debug) {
738  cout << "dt0= 0 : slope 2 = " << b << " pos in = " << a << " chi2fitN2 = " << chi2fitN2
739  << " nppar = " << nppar2 << " nptfit = " << nptfit << endl;
740  cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = "
741  << chi2fitN3 << " nppar = " << nppar3 << " T0_ev ns = " << cminf/0.00543 << endl;
742  }
743 
744  //***********************************
745  // cout << " vdrift_4parfit "<< vdrift_4parfit<<endl;
746  if( nptfit>=5) {
747  const double det = (a1*a1*(b2*v6 - b6*b6) - a1*(a2*a2*v6 - 2*a2*a6*b6 + a6*a6*b2 + b2*c6*c6 + b3*(b3*v6 - 2*b6*c6))
748  + a2*a2*c6*c6 + 2*a2*(a3*(b3*v6 - b6*c6) - a6*b3*c6) + a3*a3*(b6*b6 - b2*v6)
749  + a6*(2*a3*(b2*c6 - b3*b6) + a6*b3*b3));
750 
751  // the dv/vdrift correction may be computed under vdrift_4parfit request;
752  if (det != 0) {
753  nppar = 4;
754  chi2fit = 0.;
755  // computation of a, b, c e v
756  aminf = (a1*(a2*(b6*d6 - v6*d2) + a6*(b6*d2 - b2*d6) + d1*(b2*v6 - b6*b6)) - a2*(b3*(c6*d6 - v6*d3)
757  + c6*(b6*d3 - c6*d2)) + a3*(b2*(c6*d6 - v6*d3) + b3*(v6*d2 - b6*d6) + b6*(b6*d3 - c6*d2))
758  + a6*(b2*c6*d3 + b3*(b3*d6 - b6*d3 - c6*d2)) - d1*(b2*c6*c6 + b3*(b3*v6 - 2*b6*c6)))/det;
759  bminf = - (a1*a1*(b6*d6 - v6*d2) - a1*(a2*(a6*d6 - v6*d1) - a6*a6*d2 + a6*b6*d1 + b3*(c6*d6 - v6*d3)
760  + c6*(b6*d3 - c6*d2)) + a2*(a3*(c6*d6 - v6*d3) + c6*(a6*d3 - c6*d1)) + a3*a3*(v6*d2 - b6*d6)
761  + a3*(a6*(b3*d6 + b6*d3 - 2*c6*d2) - d1*(b3*v6 - b6*c6)) - a6*b3*(a6*d3 - c6*d1))/det;
762  cminf = -(a1*(b2*(c6*d6 - v6*d3) + b3*(v6*d2 - b6*d6) + b6*(b6*d3 - c6*d2)) + a2*a2*(v6*d3 - c6*d6)
763  + a2*(a3*(b6*d6 - v6*d2) + a6*(b3*d6 - 2*b6*d3 + c6*d2) - d1*(b3*v6 - b6*c6))
764  + a3*(d1*(b2*v6 - b6*b6) - a6*(b2*d6 - b6*d2)) + a6*(a6*(b2*d3 - b3*d2) - d1*(b2*c6 - b3*b6)))/det;
765  vminf = - (a1*a1*(b2*d6 - b6*d2) - a1*(a2*a2*d6 - a2*(a6*d2 + b6*d1) + a6*b2*d1 + b2*c6*d3
766  + b3*(b3*d6 - b6*d3 - c6*d2)) + a2*a2*c6*d3 + a2*(a3*(2*b3*d6 - b6*d3 - c6*d2) - b3*(a6*d3 + c6*d1))
767  + a3*a3*(b6*d2 - b2*d6) + a3*(a6*(b2*d3 - b3*d2) + d1*(b2*c6 - b3*b6)) + a6*b3*b3*d1)/det;
768 
769  // chi 2
770  for (int j=0; j<nptfit; j++) {
771  const double ypred = aminf + bminf*xfit[j];
772  const double dy = (yfit[j]+vminf*lfit[j]*tfit[j]-cminf*lfit[j] -ypred)/sigma;
773  chi2fit = chi2fit + dy*dy;
774 
775  } //end loop chi2
776  if (nptfit<=nppar){
777  chi2fitN4=-1;
778  // cout << "nptfit " << nptfit << " nppar " << nppar << endl;
779  }
780  else{
781  chi2fitN4= chi2fit / (nptfit-nppar);
782  }
783  }
784  else {
785  vminf = 0.;
786 
787  if (nptfit <= nppar) chi2fitN4=-1;
788  else chi2fitN4 = chi2fit / (nptfit-nppar);
789  }
790 
791  if (fabs(vminf) >= 0.29) {
792  // for safety and for code construction..dont accept correction on dv/vdrift greater then 0.09
793  vminf = 0.;
794  cminf = cminf3;
795  aminf = aminf3;
796  bminf = bminf3;
797  nppar = 3;
798  chi2fit = chi2fit3;
799  }
800 
801  } //end if vdrift
802 
803  if(!vdrift_4parfit){ //if not required explicitly leave the t0 and track step as at step 3
804  // just update vdrift value vmin for storing in the segments for monitoring
805  cminf = cminf3;
806  aminf = aminf3;
807  bminf = bminf3;
808  nppar = 3;
809  chi2fit = chi2fit3;
810  }
811 
812  nppar4 = nppar;
813 
814  } //end nptfit >=3
815 
816  if (debug) {
817  cout << " dt0= 0 : slope 4 = " << bminf << " pos out = " << aminf <<" chi2fitN4 = " << chi2fitN4
818  << " nppar= " << nppar4 << " T0_ev ns= " << cminf/0.00543 <<" delta v = " << vminf <<endl;
819  cout << nptfit << " nptfit " << " end chi2fit = " << chi2fit/ (nptfit-nppar ) << " T0_ev ns= " << cminf/0.00543 << " delta v = " << vminf <<endl;
820  }
821 
822  if ( fabs(vminf) >= 0.09 && debug ) { //checks only vdrift less then 10 % accepted
823  cout << "vminf gt 0.09 det= " << endl;
824  cout << "dt0= 0 : slope 4 = "<< bminf << " pos out = " << aminf << " chi2fitN4 = " << chi2fitN4
825  << " T0_ev ns = " << cminf/0.00543 << " delta v = "<< vminf << endl;
826  cout << "dt0 = 0 : slope 2 = "<< b << " pos in = " << a <<" chi2fitN2 = " << chi2fitN2
827  << " nppar = " << nppar-1 << " nptfit = " << nptfit <<endl;
828  cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = "
829  << chi2fitN3 << " T0_ev ns = " << cminf/0.00543 << endl;
830  cout << nptfit <<" nptfit "<< " end chi2fit = " << chi2fit << "T0_ev ns= " << cminf/0.00543 << "delta v = "<< vminf <<endl;
831  }
832 
833  if (nptfit != nppar) chi2fit = chi2fit / (nptfit-nppar);
834 }
dbl * delta
Definition: mlp_gen.cc:36
T getParameter(std::string const &) const
float wirePosition(int wireNumber) const
Returns the x position in the layer of a given wire number.
Definition: DTTopology.cc:88
static GlobalError transform(const LocalError &le, const Surface &surf)
int i
Definition: DBlmapReader.cc:9
void rejectBadHits(DTChamberRecSegment2D *) const
float xx() const
Definition: LocalError.h:24
void setChi2(const double &chi2)
Local3DVector LocalVector
Definition: LocalVector.h:12
void Fit4Var(const std::vector< float > &xfit, const std::vector< float > &yfit, const std::vector< int > &lfit, const std::vector< double > &tfit, const int nptfit, float &cminf, double &vminf, double &chi2fit) const
void update(DTRecSegment4D *seg, const bool calcT0=false) const
recompute hits position and refit the segment4D
void updateHits(DTRecSegment2D *seg, GlobalPoint &gpos, GlobalVector &gdir, const int step=2) const
list step
Definition: launcher.py:15
void calculateT0corr(DTRecSegment2D *seg) const
double zPos
DTChamberId chamberId() const
Return the corresponding ChamberId.
static const double slope[3]
const DTChamberRecSegment2D * phiSegment() const
The superPhi segment: 0 if no phi projection available.
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:47
T y() const
Definition: PV3DBase.h:62
~DTSegmentUpdator()
Destructor.
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:62
virtual void setChi2(double &chi2)
set chi2
virtual DTChamberId chamberId() const
The (specific) DetId of the chamber on which the segment resides.
void setCovMatrix(const AlgebraicSymMatrix &cov)
void setCovMatrixForZed(const LocalPoint &posZInCh)
void update(std::vector< DTRecHit1D > &updatedRecHits)
virtual bool good() const
edm::ESHandle< DTGeometry > theGeom
Geom::Theta< T > theta() const
Definition: PV3DBase.h:74
virtual LocalVector localDirection() const
Local direction in Chamber frame.
const DTTopology & specificTopology() const
Definition: DTLayer.cc:44
DTLinearFit * theFitter
void setES(const edm::EventSetup &setup)
set the setup
double vDrift() const
T sqrt(T t)
Definition: SSEVec.h:46
T z() const
Definition: PV3DBase.h:63
virtual void setES(const edm::EventSetup &setup)=0
Pass the Event Setup to the algo at each event.
virtual void setCovMatrix(AlgebraicSymMatrix &cov)
set the cov matrix
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
bool fit(DTSegmentCand *seg) const
void setDirection(LocalVector dir)
Set direction.
bool hasPhi() const
Does it have the Phi projection?
virtual bool compute(const DTLayer *layer, const DTDigi &digi, LocalPoint &leftPoint, LocalPoint &rightPoint, LocalError &error) const =0
virtual LocalPoint localPosition() const
Local position in Chamber frame.
std::vector< DTRecHit1D > specificRecHits() const
Access to specific components.
bool ist0Valid() const
virtual AssPointCont hits() const
the used hits
DTSuperLayerId superLayerId() const
The id of the superlayer on which reside the segment.
virtual LocalPoint localPosition() const
local position in SL frame
Vector3DBase unit() const
Definition: Vector3DBase.h:57
bool hasZed() const
Does it have the Z projection?
std::set< AssPoint, AssPointLessZ > AssPointCont
Definition: DTSegmentCand.h:41
const DTSLRecSegment2D * zSegment() const
The Z segment: 0 if not zed projection available.
#define N
Definition: blowfish.cc:9
virtual AlgebraicSymMatrix parametersError() const
const T & get() const
Definition: EventSetup.h:55
double b
Definition: hdecay.h:120
void setT0(const double &t0)
double a
Definition: hdecay.h:121
virtual void setDirection(LocalVector &dir)
set direction
Definition: DTSegmentCand.h:94
void fit(const std::vector< float > &x, const std::vector< float > &y, int ndat, const std::vector< float > &sigy, float &slope, float &intercept, float &covss, float &covii, float &covsi) const
Definition: DTLinearFit.cc:28
void setPosition(const LocalPoint &pos)
virtual LocalVector localDirection() const
the local direction in SL frame
CLHEP::HepSymMatrix AlgebraicSymMatrix
void setDirection(const LocalVector &dir)
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
void setPosition(LocalPoint pos)
Set position.
tuple cout
Definition: gather_cfg.py:121
void setVdrift(const double &vdrift)
DetId geographicalId() const
dbl *** dir
Definition: mlp_gen.cc:35
virtual void setPosition(LocalPoint &pos)
set position
Definition: DTSegmentCand.h:91
Definition: DDAxes.h:10
double t0() const
Get the segment t0 (if recomputed, 0 is returned otherwise)
#define debug
Definition: MEtoEDMFormat.h:34
T x() const
Definition: PV3DBase.h:61
DTRecHitBaseAlgo * theAlgo
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
T get(const Candidate &c)
Definition: component.h:56
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
DTSegmentUpdator(const edm::ParameterSet &config)
Constructor.
const DTSuperLayer * superLayer(DTSuperLayerId id) const
Return the superlayer corresponding to the given id.
Definition: DTChamber.cc:67
void setCovMatrix(AlgebraicSymMatrix mat)
Set covariance matrix.
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11