CMS 3D CMS Logo

MuonResidualsFromTrack.cc
Go to the documentation of this file.
1 /*
2  * $Id: $
3  */
4 
8 
15 
18 
19 #include "TDecompChol.h"
20 #include <cmath>
21 
23 
27  edm::ESHandle<DetIdAssociator> muonDetIdAssociator_,
29  const Trajectory* traj,
30  const reco::Track* recoTrack,
32  double maxResidual)
33  : m_recoTrack(recoTrack) {
34  bool m_debug = false;
35 
36  if (m_debug) {
37  std::cout << "BEGIN MuonResidualsFromTrack" << std::endl;
38  const std::string metname = " *** MuonResidualsFromTrack *** ";
39  LogTrace(metname) << "Tracking Component changed!";
40  }
41 
42  clear();
43 
46  int iT = 0, iM = 0;
47  for (auto const& hit : m_recoTrack->recHits()) {
48  if (hit->isValid()) {
49  DetId hitId = hit->geographicalId();
50  if (hitId.det() == DetId::Tracker) {
51  iT++;
52  if (m_debug)
53  std::cout << "Tracker Hit " << iT << " is found. Add to refit. Dimension: " << hit->dimension() << std::endl;
54 
55  recHitsForRefit.push_back(trackerRecHitBuilder->build(&*hit));
56  } else if (hitId.det() == DetId::Muon) {
57  // if ( hit->geographicalId().subdetId() == 3 && !theRPCInTheFit ) {
58  // LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
59  // continue;
60  // }
61  iM++;
62  if (m_debug)
63  std::cout << "Muon Hit " << iM
64  << " is found. We do not add muon hits to refit. Dimension: " << hit->dimension() << std::endl;
65  if (hitId.subdetId() == MuonSubdetId::DT) {
66  const DTChamberId chamberId(hitId.rawId());
67  if (m_debug)
68  std::cout << "Muon Hit in DT wheel " << chamberId.wheel() << " station " << chamberId.station()
69  << " sector " << chamberId.sector() << "." << std::endl;
70  } else if (hitId.subdetId() == MuonSubdetId::CSC) {
71  const CSCDetId cscDetId(hitId.rawId());
72  if (m_debug)
73  std::cout << "Muon hit in CSC endcap " << cscDetId.endcap() << " station " << cscDetId.station() << " ring "
74  << cscDetId.ring() << " chamber " << cscDetId.chamber() << "." << std::endl;
75  } else if (hitId.subdetId() == MuonSubdetId::RPC) {
76  if (m_debug)
77  std::cout << "Muon Hit in RPC" << std::endl;
78  } else {
79  if (m_debug)
80  std::cout << "Warning! Muon Hit not in DT or CSC or RPC" << std::endl;
81  }
82  // recHitsForRefit.push_back(theMuonRecHitBuilder->build(&*hit));
83  }
84  }
85  }
86 
87  // TrackTransformer trackTransformer();
88  // std::vector<Trajectory> vTrackerTrajectory = trackTransformer.transform(track, recHitsForReFit);
89  // std::cout << "Tracker trajectories size " << vTrackerTrajectory.size() << std::endl;
90 
91  TrajectoryStateOnSurface lastTrackerTsos;
92  double lastTrackerTsosGlobalPositionR = 0.0;
93 
94  std::vector<TrajectoryMeasurement> vTrajMeasurement = traj->measurements();
95  if (m_debug)
96  std::cout << " Size of vector of TrajectoryMeasurements: " << vTrajMeasurement.size() << std::endl;
97  int nTrajMeasurement = 0;
98  for (std::vector<TrajectoryMeasurement>::const_iterator iTrajMeasurement = vTrajMeasurement.begin();
99  iTrajMeasurement != vTrajMeasurement.end();
100  ++iTrajMeasurement) {
101  nTrajMeasurement++;
102  if (m_debug)
103  std::cout << " TrajectoryMeasurement #" << nTrajMeasurement << std::endl;
104 
105  const TrajectoryMeasurement& trajMeasurement = *iTrajMeasurement;
106 
108  m_tsoscomb(trajMeasurement.forwardPredictedState(), trajMeasurement.backwardPredictedState());
109  const TrajectoryStateOnSurface& tsosF = trajMeasurement.forwardPredictedState();
110  const TrajectoryStateOnSurface& tsosB = trajMeasurement.backwardPredictedState();
111  const TrajectoryStateOnSurface& tsosU = trajMeasurement.updatedState();
112  if (m_debug)
113  std::cout << " TrajectoryMeasurement TSOS validity: " << tsos.isValid() << std::endl;
114  if (tsos.isValid()) {
115  double tsosGlobalPositionR = sqrt(tsos.globalPosition().x() * tsos.globalPosition().x() +
116  tsos.globalPosition().y() * tsos.globalPosition().y());
117  if (m_debug) {
118  std::cout << " TrajectoryMeasurement TSOS localPosition"
119  << " x: " << tsos.localPosition().x() << " y: " << tsos.localPosition().y()
120  << " z: " << tsos.localPosition().z() << std::endl;
121  std::cout << " TrajectoryMeasurement TSOS globalPosition"
122  << " x: " << tsos.globalPosition().x() << " y: " << tsos.globalPosition().y()
123  << " R: " << tsosGlobalPositionR << " z: " << tsos.globalPosition().z() << std::endl;
124  }
125  if (tsosGlobalPositionR > lastTrackerTsosGlobalPositionR) {
126  lastTrackerTsos = tsos;
127  lastTrackerTsosGlobalPositionR = tsosGlobalPositionR;
128  }
129  }
130 
131  const TransientTrackingRecHit* trajMeasurementHit = &(*trajMeasurement.recHit());
132  if (m_debug)
133  std::cout << " TrajectoryMeasurement hit validity: " << trajMeasurementHit->isValid() << std::endl;
134  if (trajMeasurementHit->isValid()) {
135  DetId trajMeasurementHitId = trajMeasurementHit->geographicalId();
136  int trajMeasurementHitDim = trajMeasurementHit->dimension();
137  if (trajMeasurementHitId.det() == DetId::Tracker) {
138  if (m_debug)
139  std::cout << " TrajectoryMeasurement hit Det: Tracker" << std::endl;
140  if (m_debug)
141  std::cout << " TrajectoryMeasurement hit dimension: " << trajMeasurementHitDim << std::endl;
143  double xresid = tsos.localPosition().x() - trajMeasurementHit->localPosition().x();
144  double xresiderr2 = tsos.localError().positionError().xx() + trajMeasurementHit->localPositionError().xx();
145  m_tracker_chi2 += xresid * xresid / xresiderr2;
146 
147  if (trajMeasurementHitId.subdetId() == StripSubdetector::TID ||
148  trajMeasurementHitId.subdetId() == StripSubdetector::TEC) {
149  m_contains_TIDTEC = true;
150  }
151  // YP I add false here. No trajectory measurments in Muon system if we corrected TrackTransformer accordingly
152  } else if (false && trajMeasurementHitId.det() == DetId::Muon) {
153  //AR: I removed the false criteria for cosmic tests
154  // } else if (trajMeasurementHitId.det() == DetId::Muon ) {
155 
156  if (m_debug)
157  std::cout << " TrajectoryMeasurement hit Det: Muon" << std::endl;
158 
159  if (trajMeasurementHitId.subdetId() == MuonSubdetId::DT) {
160  const DTChamberId chamberId(trajMeasurementHitId.rawId());
161  if (m_debug)
162  std::cout << " TrajectoryMeasurement hit subDet: DT wheel " << chamberId.wheel() << " station "
163  << chamberId.station() << " sector " << chamberId.sector() << std::endl;
164 
165  // double gChX = globalGeometry->idToDet(chamberId)->position().x();
166  // double gChY = globalGeometry->idToDet(chamberId)->position().y();
167  // double gChZ = globalGeometry->idToDet(chamberId)->position().z();
168  // std::cout << " The chamber position in global frame x: " << gChX << " y: " << gChY << " z: " << gChZ << std::endl;
169 
170  const GeomDet* geomDet = muonDetIdAssociator_->getGeomDet(chamberId);
171  double chamber_width = geomDet->surface().bounds().width();
172  double chamber_length = geomDet->surface().bounds().length();
173 
174  double hitX = trajMeasurementHit->localPosition().x();
175  double hitY = trajMeasurementHit->localPosition().y();
176  // double hitZ = trajMeasurementHit->localPosition().z();
177 
178  double tsosX = tsos.localPosition().x();
179  double tsosY = tsos.localPosition().y();
180  // double tsosZ = tsos.localPosition().z();
181 
182  int residualDT13IsAdded = false;
183  int residualDT2IsAdded = false;
184 
185  // have we seen this chamber before?
186  if (m_dt13.find(chamberId) == m_dt13.end() && m_dt2.find(chamberId) == m_dt2.end()) {
187  if (m_debug)
188  std::cout << "AR: pushing back chamber: " << chamberId << std::endl;
189  m_chamberIds.push_back(chamberId);
190  //addTrkCovMatrix(chamberId, tsos); // only for the 1st hit
191  }
192  if (m_debug)
193  std::cout << "AR: size of chamberId: " << m_chamberIds.size() << std::endl;
194 
195  if (m_debug)
196  std::cout << " TrajectoryMeasurement hit dimension: " << trajMeasurementHitDim << std::endl;
197  if (trajMeasurementHitDim > 1) {
198  std::vector<const TrackingRecHit*> vDTSeg2D = trajMeasurementHit->recHits();
199  if (m_debug)
200  std::cout << " vDTSeg2D size: " << vDTSeg2D.size() << std::endl;
201  for (std::vector<const TrackingRecHit*>::const_iterator itDTSeg2D = vDTSeg2D.begin();
202  itDTSeg2D != vDTSeg2D.end();
203  ++itDTSeg2D) {
204  std::vector<const TrackingRecHit*> vDTHits1D = (*itDTSeg2D)->recHits();
205  if (m_debug)
206  std::cout << " vDTHits1D size: " << vDTHits1D.size() << std::endl;
207  for (std::vector<const TrackingRecHit*>::const_iterator itDTHits1D = vDTHits1D.begin();
208  itDTHits1D != vDTHits1D.end();
209  ++itDTHits1D) {
210  const TrackingRecHit* hit = *itDTHits1D;
211  if (m_debug)
212  std::cout << " hit dimension: " << hit->dimension() << std::endl;
213 
214  DetId hitId = hit->geographicalId();
215  const DTSuperLayerId superLayerId(hitId.rawId());
216  const DTLayerId layerId(hitId.rawId());
217  if (m_debug)
218  std::cout << " hit superLayerId: " << superLayerId.superLayer() << std::endl;
219  if (m_debug)
220  std::cout << " hit layerId: " << layerId.layer() << std::endl;
221 
222  if (superLayerId.superlayer() == 2 && vDTHits1D.size() >= 3) {
223  if (m_dt2.find(chamberId) == m_dt2.end()) {
224  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
225  m_dt2[chamberId] =
226  new MuonDT2ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
227  if (m_debug)
228  std::cout << " This is first appearance of the DT with hits in superlayer 2"
229  << std::endl;
230  }
231  m_dt2[chamberId]->addResidual(prop, &tsos, hit, chamber_width, chamber_length);
232  residualDT2IsAdded = true;
233 
234  } else if ((superLayerId.superlayer() == 1 || superLayerId.superlayer() == 3) &&
235  vDTHits1D.size() >= 6) {
236  if (m_dt13.find(chamberId) == m_dt13.end()) {
237  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
238  m_dt13[chamberId] =
239  new MuonDT13ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
240  if (m_debug)
241  std::cout << " This is first appearance of the DT with hits in superlayers 1 and 3"
242  << std::endl;
243  }
244  m_dt13[chamberId]->addResidual(prop, &tsos, hit, chamber_width, chamber_length);
245  residualDT13IsAdded = true;
246  }
247  }
248  }
249  }
250 
251  if (residualDT13IsAdded == true && residualDT2IsAdded == true && chamberId.wheel() == 0 &&
252  chamberId.station() == 2 && chamberId.sector() == 7) {
253  if (m_debug) {
254  std::cout << "MYMARK " << tsosX << " " << hitX << " " << tsosX - hitX << " "
255  << m_dt13[chamberId]->trackx() << " " << m_dt13[chamberId]->residual() << " " << tsosY << " "
256  << hitY << " " << tsosY - hitY << " " << m_dt2[chamberId]->tracky() << " "
257  << m_dt2[chamberId]->residual() << " " << tsosF.localPosition().x() << " "
258  << tsosF.localPosition().y() << " " << tsosF.localPosition().z() << " "
259  << tsosB.localPosition().x() << " " << tsosB.localPosition().y() << " "
260  << tsosB.localPosition().z() << " " << tsosU.localPosition().x() << " "
261  << tsosU.localPosition().y() << " " << tsosU.localPosition().z() << std::endl;
262  }
263  }
264 
265  // http://cmslxr.fnal.gov/lxr/source/DataFormats/TrackReco/src/HitPattern.cc#101
266  // YP I add false here. No trajectory measurments in Muon system if we corrected TrackTransformer accordingly
267  } else if (false && trajMeasurementHitId.subdetId() == MuonSubdetId::CSC) {
268  const CSCDetId cscDetId(trajMeasurementHitId.rawId());
269  const CSCDetId chamberId2(cscDetId.endcap(), cscDetId.station(), cscDetId.ring(), cscDetId.chamber());
270  if (m_debug)
271  std::cout << " TrajectoryMeasurement hit subDet: CSC endcap " << cscDetId.endcap() << " station "
272  << cscDetId.station() << " ring " << cscDetId.ring() << " chamber " << cscDetId.chamber()
273  << std::endl;
274  if (m_debug)
275  std::cout << " TrajectoryMeasurement hit dimension: " << trajMeasurementHitDim << std::endl;
276 
277  if (trajMeasurementHitDim == 4) {
278  std::vector<const TrackingRecHit*> vCSCHits2D = trajMeasurementHit->recHits();
279  if (m_debug)
280  std::cout << " vCSCHits2D size: " << vCSCHits2D.size() << std::endl;
281  if (vCSCHits2D.size() >= 5) {
282  for (std::vector<const TrackingRecHit*>::const_iterator itCSCHits2D = vCSCHits2D.begin();
283  itCSCHits2D != vCSCHits2D.end();
284  ++itCSCHits2D) {
285  const TrackingRecHit* cscHit2D = *itCSCHits2D;
286  if (m_debug)
287  std::cout << " cscHit2D dimension: " << cscHit2D->dimension() << std::endl;
288  const TrackingRecHit* hit = cscHit2D;
289  if (m_debug)
290  std::cout << " hit dimension: " << hit->dimension() << std::endl;
291 
292  DetId hitId = hit->geographicalId();
293  const CSCDetId cscDetId(hitId.rawId());
294  if (m_debug)
295  std::cout << " hit layer: " << cscDetId.layer() << std::endl;
296 
297  // not sure why we sometimes get layer == 0
298  if (cscDetId.layer() == 0)
299  continue;
300 
301  // have we seen this chamber before?
302  if (m_csc.find(chamberId2) == m_csc.end()) {
303  m_chamberIds.push_back(chamberId2);
304  //addTrkCovMatrix(chamberId, tsos); // only for the 1st hit
305  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId2);
306  m_csc[chamberId2] =
307  new MuonCSCChamberResidual(globalGeometry, navigator, chamberId2, chamberAlignable);
308  if (m_debug)
309  std::cout << " This is first appearance of the CSC with hits QQQ" << std::endl;
310  }
311 
312  m_csc[chamberId2]->addResidual(prop, &tsos, hit, 250.0, 250.0);
313  }
314  }
315  }
316  } else {
317  if (m_debug)
318  std::cout << " TrajectoryMeasurement hit subDet: UNKNOWN" << std::endl;
319  if (m_debug)
320  std::cout << "AR: trajMeasurementHitId.det(): " << trajMeasurementHitId.subdetId() << std::endl;
321  }
322  } else {
323  if (m_debug)
324  std::cout << " TrajectoryMeasurement hit det: UNKNOWN" << std::endl;
325  if (m_debug)
326  std::cout << "AR: trajMeasurementHitId.det(): " << trajMeasurementHitId.det() << std::endl;
327  if (m_debug)
328  std::cout << "DetId::Tracker: " << DetId::Tracker << std::endl;
329  }
330  }
331  }
332 
333  int iT2 = 0, iM2 = 0;
334  for (auto const& hit2 : m_recoTrack->recHits()) {
335  if (hit2->isValid()) {
336  DetId hitId2 = hit2->geographicalId();
337  if (hitId2.det() == DetId::Tracker) {
338  iT2++;
339  if (m_debug)
340  std::cout << "Tracker Hit " << iT2 << " is found. We don't calcualte Tsos for it" << std::endl;
341  } else if (hitId2.det() == DetId::Muon) {
342  // if ( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit ) {
343  // LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
344  // continue;
345  // }
346  iM2++;
347  if (m_debug)
348  std::cout << "Muon Hit " << iM2 << " is found. Dimension: " << hit2->dimension() << std::endl;
349  if (hitId2.subdetId() == MuonSubdetId::DT) {
350  const DTChamberId chamberId(hitId2.rawId());
351  if (m_debug)
352  std::cout << "Muon Hit in DT wheel " << chamberId.wheel() << " station " << chamberId.station()
353  << " sector " << chamberId.sector() << std::endl;
354 
355  const GeomDet* geomDet = muonDetIdAssociator_->getGeomDet(chamberId);
356  double chamber_width = geomDet->surface().bounds().width();
357  double chamber_length = geomDet->surface().bounds().length();
358 
359  if (hit2->dimension() > 1) {
360  // std::vector<const TrackingRecHit*> vDTSeg2D = hit2->recHits();
361  std::vector<TrackingRecHit*> vDTSeg2D = hit2->recHits();
362 
363  if (m_debug)
364  std::cout << " vDTSeg2D size: " << vDTSeg2D.size() << std::endl;
365 
366  // for ( std::vector<const TrackingRecHit*>::const_iterator itDTSeg2D = vDTSeg2D.begin();
367  // itDTSeg2D != vDTSeg2D.end();
368  // ++itDTSeg2D ) {
369 
370  for (std::vector<TrackingRecHit*>::const_iterator itDTSeg2D = vDTSeg2D.begin(); itDTSeg2D != vDTSeg2D.end();
371  ++itDTSeg2D) {
372  // std::vector<const TrackingRecHit*> vDTHits1D = (*itDTSeg2D)->recHits();
373  std::vector<TrackingRecHit*> vDTHits1D = (*itDTSeg2D)->recHits();
374  if (m_debug)
375  std::cout << " vDTHits1D size: " << vDTHits1D.size() << std::endl;
376  // for ( std::vector<const TrackingRecHit*>::const_iterator itDTHits1D = vDTHits1D.begin();
377  // itDTHits1D != vDTHits1D.end();
378  // ++itDTHits1D ) {
379  for (std::vector<TrackingRecHit*>::const_iterator itDTHits1D = vDTHits1D.begin();
380  itDTHits1D != vDTHits1D.end();
381  ++itDTHits1D) {
382  //const TrackingRecHit* hit = *itDTHits1D;
383  TrackingRecHit* hit = *itDTHits1D;
384  if (m_debug)
385  std::cout << " hit dimension: " << hit->dimension() << std::endl;
386 
387  DetId hitId = hit->geographicalId();
388  const DTSuperLayerId superLayerId(hitId.rawId());
389  const DTLayerId layerId(hitId.rawId());
390  if (m_debug)
391  std::cout << " hit superLayerId: " << superLayerId.superLayer() << std::endl;
392  if (m_debug)
393  std::cout << " hit layerId: " << layerId.layer() << std::endl;
394 
395  if (superLayerId.superlayer() == 2 && vDTHits1D.size() >= 3) {
396  if (m_dt2.find(chamberId) == m_dt2.end()) {
397  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
398  m_dt2[chamberId] =
399  new MuonDT2ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
400  if (m_debug)
401  std::cout << " This is first appearance of the DT with hits in superlayer 2"
402  << std::endl;
403 
404  // have we seen this chamber before? check if it was in dt13
405  if (m_dt13.find(chamberId) == m_dt13.end()) {
406  m_chamberIds.push_back(chamberId);
407  }
408  }
409 
410  TrajectoryStateOnSurface extrapolation;
411  extrapolation = prop->propagate(lastTrackerTsos, globalGeometry->idToDet(hitId)->surface());
412 
413  if (extrapolation.isValid()) {
414  if (m_debug) {
415  std::cout << " extrapolation localPosition()"
416  << " x: " << extrapolation.localPosition().x()
417  << " y: " << extrapolation.localPosition().y()
418  << " z: " << extrapolation.localPosition().z() << std::endl;
419  }
420  m_dt2[chamberId]->addResidual(prop, &extrapolation, hit, chamber_width, chamber_length);
421  }
422  // residualDT2IsAdded = true;
423 
424  } else if ((superLayerId.superlayer() == 1 || superLayerId.superlayer() == 3) &&
425  vDTHits1D.size() >= 6) {
426  if (m_dt13.find(chamberId) == m_dt13.end()) {
427  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
428  m_dt13[chamberId] =
429  new MuonDT13ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
430  if (m_debug)
431  std::cout << " This is first appearance of the DT with hits in superlayers 1 and 3"
432  << std::endl;
433 
434  // have we seen this chamber before? check if it was in dt2
435  if (m_dt2.find(chamberId) == m_dt2.end()) {
436  m_chamberIds.push_back(chamberId);
437  }
438  }
439 
440  TrajectoryStateOnSurface extrapolation;
441  extrapolation = prop->propagate(lastTrackerTsos, globalGeometry->idToDet(hitId)->surface());
442 
443  if (extrapolation.isValid()) {
444  if (m_debug) {
445  std::cout << " extrapolation localPosition()"
446  << " x: " << extrapolation.localPosition().x()
447  << " y: " << extrapolation.localPosition().y()
448  << " z: " << extrapolation.localPosition().z() << std::endl;
449  }
450  m_dt13[chamberId]->addResidual(prop, &extrapolation, hit, chamber_width, chamber_length);
451  }
452  // residualDT13IsAdded = true;
453  }
454  }
455  }
456  }
457 
458  // std::cout << "Extrapolate last Tracker TSOS to muon hit" << std::endl;
459  // TrajectoryStateOnSurface extrapolation;
460  // extrapolation = prop->propagate( lastTrackerTsos, globalGeometry->idToDet(hitId2)->surface() );
461  //
462  // if ( chamberId2.wheel() == 0 && chamberId2.station() == 2 && chamberId2.sector() == 7 ) {
463  //
464  // double hitX2 = hit2->localPosition().x();
465  // double hitY2 = hit2->localPosition().y();
466  // double hitZ2 = hit2->localPosition().z();
467  //
468  // double tsosX2 = extrapolation.localPosition().x();
469  // double tsosY2 = extrapolation.localPosition().y();
470  // double tsosZ2 = extrapolation.localPosition().z();
471  //
472  // std::cout << "MYMARK " << tsosX2 << " " << hitX2 << " " << tsosX2 - hitX2 << " " << "0" << " " << "0"
473  // << " " << tsosY2 << " " << hitY2 << " " << tsosY2 - hitY2 << " " << "0" << " " << "0"
474  // << " 0 0 0 0 0 0 0 0 0 " << std::endl;
478  // }
479 
480  } else if (hitId2.subdetId() == MuonSubdetId::CSC) {
481  const CSCDetId cscDetId2(hitId2.rawId());
482  const CSCDetId chamberId(cscDetId2.endcap(), cscDetId2.station(), cscDetId2.ring(), cscDetId2.chamber());
483  if (m_debug)
484  std::cout << "Muon hit in CSC endcap " << cscDetId2.endcap() << " station " << cscDetId2.station()
485  << " ring " << cscDetId2.ring() << " chamber " << cscDetId2.chamber() << "." << std::endl;
486 
487  if (hit2->dimension() == 4) {
488  // std::vector<const TrackingRecHit*> vCSCHits2D = hit2->recHits();
489  std::vector<TrackingRecHit*> vCSCHits2D = hit2->recHits();
490  if (m_debug)
491  std::cout << " vCSCHits2D size: " << vCSCHits2D.size() << std::endl;
492  if (vCSCHits2D.size() >= 5) {
493  // for ( std::vector<const TrackingRecHit*>::const_iterator itCSCHits2D = vCSCHits2D.begin();
494  // itCSCHits2D != vCSCHits2D.end();
495  // ++itCSCHits2D ) {
496 
497  for (std::vector<TrackingRecHit*>::const_iterator itCSCHits2D = vCSCHits2D.begin();
498  itCSCHits2D != vCSCHits2D.end();
499  ++itCSCHits2D) {
500  // const TrackingRecHit* cscHit2D = *itCSCHits2D;
501  TrackingRecHit* cscHit2D = *itCSCHits2D;
502  if (m_debug)
503  std::cout << " cscHit2D dimension: " << cscHit2D->dimension() << std::endl;
504  // const TrackingRecHit* hit = cscHit2D;
505  TrackingRecHit* hit = cscHit2D;
506  if (m_debug)
507  std::cout << " hit dimension: " << hit->dimension() << std::endl;
508 
509  DetId hitId = hit->geographicalId();
510  const CSCDetId cscDetId(hitId.rawId());
511 
512  if (m_debug) {
513  std::cout << " hit layer: " << cscDetId.layer() << std::endl;
514 
515  std::cout << " hit localPosition"
516  << " x: " << hit->localPosition().x() << " y: " << hit->localPosition().y()
517  << " z: " << hit->localPosition().z() << std::endl;
518  std::cout << " hit globalPosition"
519  << " x: " << globalGeometry->idToDet(hitId)->toGlobal(hit->localPosition()).x()
520  << " y: " << globalGeometry->idToDet(hitId)->toGlobal(hit->localPosition()).y()
521  << " z: " << globalGeometry->idToDet(hitId)->toGlobal(hit->localPosition()).z()
522  << std::endl;
523  }
524 
525  // not sure why we sometimes get layer == 0
526  if (cscDetId.layer() == 0)
527  continue;
528 
529  // have we seen this chamber before?
530  if (m_debug)
531  std::cout << "Have we seen this chamber before?";
532  if (m_csc.find(chamberId) == m_csc.end()) {
533  if (m_debug)
534  std::cout << " NO. m_csc.count() = " << m_csc.count(chamberId) << std::endl;
535  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
536  m_csc[chamberId] = new MuonCSCChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
537  if (m_debug)
538  std::cout << " This is first appearance of the CSC with hits m_csc.count() = "
539  << m_csc.count(chamberId) << std::endl;
540  m_chamberIds.push_back(chamberId);
541  //addTrkCovMatrix(chamberId, tsos); // only for the 1st hit
542  } else {
543  if (m_debug)
544  std::cout << " YES. m_csc.count() = " << m_csc.count(chamberId) << std::endl;
545  }
546 
547  if (m_debug) {
548  std::cout << " lastTrackerTsos localPosition"
549  << " x: " << lastTrackerTsos.localPosition().x()
550  << " y: " << lastTrackerTsos.localPosition().y()
551  << " z: " << lastTrackerTsos.localPosition().z() << std::endl;
552  std::cout << " lastTrackerTsos globalPosition"
553  << " x: " << lastTrackerTsos.globalPosition().x()
554  << " y: " << lastTrackerTsos.globalPosition().y()
555  << " z: " << lastTrackerTsos.globalPosition().z() << std::endl;
556  std::cout << " Do extrapolation from lastTrackerTsos to hit surface" << std::endl;
557  }
558  TrajectoryStateOnSurface extrapolation;
559  extrapolation = prop->propagate(lastTrackerTsos, globalGeometry->idToDet(hitId)->surface());
560  if (m_debug)
561  std::cout << " extrapolation.isValid() = " << extrapolation.isValid() << std::endl;
562 
563  if (extrapolation.isValid()) {
564  if (m_debug) {
565  std::cout << " extrapolation localPosition()"
566  << " x: " << extrapolation.localPosition().x()
567  << " y: " << extrapolation.localPosition().y()
568  << " z: " << extrapolation.localPosition().z() << std::endl;
569  }
570  m_csc[chamberId]->addResidual(prop, &extrapolation, hit, 250.0, 250.0);
571  }
572  }
573  }
574  }
575 
576  } else if (hitId2.subdetId() == MuonSubdetId::RPC) {
577  if (m_debug)
578  std::cout << "Muon Hit in RPC" << std::endl;
579  } else {
580  if (m_debug)
581  std::cout << "Warning! Muon Hit not in DT or CSC or RPC" << std::endl;
582  }
583  // recHitsForRefit.push_back(theMuonRecHitBuilder->build(&**hit));
584  if (hitId2.subdetId() == MuonSubdetId::DT || hitId2.subdetId() == MuonSubdetId::CSC) {
585  }
586  }
587  }
588  }
589 
590  if (m_debug)
591  std::cout << "END MuonResidualsFromTrack" << std::endl << std::endl;
592 }
593 
595  const reco::Muon* recoMuon,
597  double maxResidual)
598  : m_recoMuon(recoMuon) {
599  bool m_debug = false;
600 
601  clear();
604 
605  m_tracker_chi2 = m_recoMuon->innerTrack()->chi2();
606  m_tracker_numHits = m_recoMuon->innerTrack()->ndof() + 5;
608 
609  /*
610  for(auto const& hit : m_recoMuon->innerTrack()->recHits())
611  {
612  DetId id = hit->geographicalId();
613  if (id.det() == DetId::Tracker)
614  {
615  m_tracker_numHits++;
616  if (id.subdetId() == StripSubdetector::TID || id.subdetId() == StripSubdetector::TEC) m_contains_TIDTEC = true;
617  }
618  }
619  */
620 
621  for (std::vector<reco::MuonChamberMatch>::const_iterator chamberMatch = m_recoMuon->matches().begin();
622  chamberMatch != m_recoMuon->matches().end();
623  chamberMatch++) {
624  if (chamberMatch->id.det() != DetId::Muon)
625  continue;
626 
627  for (std::vector<reco::MuonSegmentMatch>::const_iterator segMatch = chamberMatch->segmentMatches.begin();
628  segMatch != chamberMatch->segmentMatches.end();
629  ++segMatch) {
630  // select the only segment that belongs to track and is the best in station by dR
631  if (!(segMatch->isMask(reco::MuonSegmentMatch::BestInStationByDR) &&
633  continue;
634 
635  if (chamberMatch->id.subdetId() == MuonSubdetId::DT) {
636  const DTChamberId chamberId(chamberMatch->id.rawId());
637 
638  DTRecSegment4DRef segmentDT = segMatch->dtSegmentRef;
639  const DTRecSegment4D* segment = segmentDT.get();
640  if (segment == nullptr)
641  continue;
642 
643  if (segment->hasPhi() && fabs(chamberMatch->x - segMatch->x) > maxResidual)
644  continue;
645  if (segment->hasZed() && fabs(chamberMatch->y - segMatch->y) > maxResidual)
646  continue;
647 
648  // have we seen this chamber before?
649  if (m_dt13.find(chamberId) == m_dt13.end() && m_dt2.find(chamberId) == m_dt2.end()) {
650  m_chamberIds.push_back(chamberId);
651  }
652 
653  if (segment->hasZed()) {
654  if (m_dt2.find(chamberId) == m_dt2.end()) {
655  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
656  // YP
657  // m_dt2[chamberId] = new MuonTrackDT2ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
658  } else if (m_debug)
659  std::cout << "multi segment match to tmuon: dt2 -- should not happen!" << std::endl;
660  m_dt2[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
661  }
662  if (segment->hasPhi()) {
663  if (m_dt13.find(chamberId) == m_dt13.end()) {
664  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
665  // YP
666  // m_dt13[chamberId] = new MuonTrackDT13ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
667  } else if (m_debug)
668  std::cout << "multi segment match to tmuon: dt13 -- should not happen!" << std::endl;
669  m_dt13[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
670  }
671  }
672 
673  else if (chamberMatch->id.subdetId() == MuonSubdetId::CSC) {
674  const CSCDetId cscDetId(chamberMatch->id.rawId());
675  const CSCDetId chamberId(cscDetId.chamberId());
676 
677  if (fabs(chamberMatch->x - segMatch->x) > maxResidual)
678  continue;
679 
680  // have we seen this chamber before?
681  if (m_csc.find(chamberId) == m_csc.end()) {
682  m_chamberIds.push_back(chamberId);
683  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
684  // YP
685  // m_csc[chamberId] = new MuonTrackCSCChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
686  } else if (m_debug)
687  std::cout << "multi segment match to tmuon: csc -- should not happen!" << std::endl;
688  m_csc[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
689  }
690  }
691  }
692 }
693 
694 // This is destructor
695 // It deletes all chambers residulas
697  for (std::map<DetId, MuonChamberResidual*>::const_iterator residual = m_dt13.begin(); residual != m_dt13.end();
698  ++residual) {
699  delete residual->second;
700  }
701  for (std::map<DetId, MuonChamberResidual*>::const_iterator residual = m_dt2.begin(); residual != m_dt2.end();
702  ++residual) {
703  delete residual->second;
704  }
705  for (std::map<DetId, MuonChamberResidual*>::const_iterator residual = m_csc.begin(); residual != m_csc.end();
706  ++residual) {
707  delete residual->second;
708  }
709 }
710 
712  m_tracker_numHits = 0;
713  m_tracker_chi2 = 0.;
714  m_contains_TIDTEC = false;
715  m_chamberIds.clear();
716  m_dt13.clear();
717  m_dt2.clear();
718  m_csc.clear();
719  m_trkCovMatrix.clear();
720 }
721 
723  if (m_tracker_numHits > 5)
724  return m_tracker_chi2 / double(m_tracker_numHits - 5);
725  else
726  return -1.;
727 }
728 
730  if (m_recoMuon)
731  return m_recoTrack->normalizedChi2();
732  return trackerRedChi2();
733 }
734 
737  if (m_dt13.find(chamberId) == m_dt13.end())
738  return nullptr;
739  return m_dt13[chamberId];
740  } else if (type == MuonChamberResidual::kDT2) {
741  if (m_dt2.find(chamberId) == m_dt2.end())
742  return nullptr;
743  return m_dt2[chamberId];
744  } else if (type == MuonChamberResidual::kCSC) {
745  if (m_csc.find(chamberId) == m_csc.end())
746  return nullptr;
747  return m_csc[chamberId];
748  } else
749  return nullptr;
750 }
751 
753  const AlgebraicSymMatrix55 cov55 = tsos.localError().matrix();
754  TMatrixDSym cov44(4);
755  // change indices from q/p,dxdz,dydz,x,y to x,y,dxdz,dydz
756  int subs[4] = {3, 4, 1, 2};
757  for (int i = 0; i < 4; i++)
758  for (int j = 0; j < 4; j++)
759  cov44(i, j) = cov55(subs[i], subs[j]);
760  m_trkCovMatrix[chamberId] = cov44;
761 }
762 
763 TMatrixDSym MuonResidualsFromTrack::covMatrix(DetId chamberId) {
764  bool m_debug = false;
765 
766  TMatrixDSym result(4);
767  if (m_debug)
768  std::cout << "MuonResidualsFromTrack:: cov initial:" << std::endl;
769  result.Print();
770  if (m_trkCovMatrix.find(chamberId) == m_trkCovMatrix.end()) {
771  if (m_debug)
772  std::cout << "MuonResidualsFromTrack:: cov does not exist!" << std::endl;
773  return result;
774  }
775  result = m_trkCovMatrix[chamberId];
776 
777  if (m_debug)
778  std::cout << "MuonResidualsFromTrack:: cov before:" << std::endl;
779  result.Print();
780 
781  // add segment's errors in quadratures to track's covariance matrix
782  double r_err;
783  if (m_csc.find(chamberId) == m_csc.end()) {
784  r_err = m_csc[chamberId]->residual_error();
785  result(0, 0) += r_err * r_err;
786  r_err = m_csc[chamberId]->resslope_error();
787  result(2, 2) += r_err * r_err;
788  }
789  if (m_dt13.find(chamberId) == m_dt13.end()) {
790  r_err = m_dt13[chamberId]->residual_error();
791  result(0, 0) += r_err * r_err;
792  r_err = m_dt13[chamberId]->resslope_error();
793  result(2, 2) += r_err * r_err;
794  }
795  if (m_dt2.find(chamberId) == m_dt2.end()) {
796  r_err = m_dt2[chamberId]->residual_error();
797  result(1, 1) += r_err * r_err;
798  r_err = m_dt2[chamberId]->resslope_error();
799  result(3, 3) += r_err * r_err;
800  }
801  if (m_debug)
802  std::cout << "MuonResidualsFromTrack:: cov after:" << std::endl;
803  result.Print();
804 
805  return result;
806 }
807 
809  bool m_debug = false;
810 
811  TMatrixDSym result(4);
812  TMatrixDSym cov44 = covMatrix(chamberId);
813 
814  // invert it using cholesky decomposition
815  TDecompChol decomp(cov44);
816  bool ok = decomp.Invert(result);
817  if (m_debug)
818  std::cout << "MuonResidualsFromTrack:: corr after:" << std::endl;
819  result.Print();
820 
821  if (!ok && m_debug)
822  std::cout << "MuonResidualsFromTrack:: cov inversion failed!" << std::endl;
823  return result;
824 }
825 
827  bool m_debug = false;
828 
829  TMatrixD result(4, 4);
830  TMatrixDSym corr44 = corrMatrix(chamberId);
831 
832  // get an upper triangular matrix U such that corr = U^T * U
833  TDecompChol decomp(corr44);
834  bool ok = decomp.Decompose();
835  result = decomp.GetU();
836 
837  if (m_debug)
838  std::cout << "MuonResidualsFromTrack:: corr cholesky after:" << std::endl;
839  result.Print();
840 
841  if (!ok && m_debug)
842  std::cout << "MuonResidualsFromTrack:: corr decomposition failed!" << std::endl;
843  return result;
844 }
std::map< DetId, MuonChamberResidual * > m_csc
static constexpr auto TEC
virtual int dimension() const =0
std::vector< DetId > m_chamberIds
virtual float length() const =0
const LocalTrajectoryError & localError() const
bool hasPhi() const
Does it have the Phi projection?
std::map< DetId, TMatrixDSym > m_trkCovMatrix
T z() const
Definition: PV3DBase.h:61
const std::string metname
static edm::ESInputTag builderESInputTag()
TrajectoryStateOnSurface const & forwardPredictedState() const
Access to forward predicted state (from fitter or builder)
TMatrixDSym covMatrix(DetId chamberId)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
LocalError positionError() const
assert(be >=bs)
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
const GeomDet * idToDet(DetId) const override
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
static const unsigned int BestInStationByDR
bool isTrackerMuon() const override
Definition: Muon.h:304
#define LogTrace(id)
DataContainer const & measurements() const
Definition: Trajectory.h:178
auto recHits() const
Access to reconstructed hits on the track.
Definition: Track.h:85
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
std::map< DetId, MuonChamberResidual * > m_dt2
GlobalPoint globalPosition() const
bool isValid() const
T sqrt(T t)
Definition: SSEVec.h:19
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
virtual std::vector< const TrackingRecHit * > recHits() const =0
Access to component RecHits (if any)
TrajectoryStateOnSurface const & backwardPredictedState() const
Access to backward predicted state (from smoother)
TMatrixDSym corrMatrix(DetId chamberId)
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
virtual TrackRef innerTrack() const
Definition: Muon.h:45
virtual const GeomDet * getGeomDet(const DetId &) const =0
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:17
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
const reco::Track * m_recoTrack
DetId geographicalId() const
virtual LocalError localPositionError() const =0
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
TMatrixD choleskyCorrMatrix(DetId chamberId)
static constexpr int RPC
Definition: MuonSubdetId.h:13
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:593
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
std::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:145
TrajectoryStateOnSurface const & updatedState() const
void addTrkCovMatrix(DetId, TrajectoryStateOnSurface &)
static const unsigned int BelongsToTrackByDR
const AlgebraicSymMatrix55 & matrix() const
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
static constexpr int DT
Definition: MuonSubdetId.h:11
bool hasZed() const
Does it have the Z projection?
static constexpr int CSC
Definition: MuonSubdetId.h:12
virtual LocalPoint localPosition() const =0
virtual float width() const =0
static constexpr auto TID
std::map< DetId, MuonChamberResidual * > m_dt13
MuonResidualsFromTrack(edm::ESHandle< TransientTrackingRecHitBuilder > builder, edm::ESHandle< MagneticField > magneticField, edm::ESHandle< GlobalTrackingGeometry > globalGeometry, edm::ESHandle< DetIdAssociator > muonDetIdAssociator_, edm::ESHandle< Propagator > prop, const Trajectory *traj, const reco::Track *recoTrack, AlignableNavigator *navigator, double maxResidual)
float xx() const
Definition: LocalError.h:22
ConstRecHitPointer const & recHit() const
TrajectoryStateCombiner m_tsoscomb
const Bounds & bounds() const
Definition: Surface.h:87
MuonChamberResidual * chamberResidual(DetId chamberId, int type)