CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonResidualsFromTrack.cc
Go to the documentation of this file.
1 /*
2  * $Id: $
3  */
4 
8 
15 
16 #include "TDecompChol.h"
17 
18 
20 {
21  const AlgebraicSymMatrix55 cov55 = tsos.localError().matrix();
22  TMatrixDSym cov44(4);
23  // change indices from q/p,dxdz,dydz,x,y to x,y,dxdz,dydz
24  int subs[4] = { 3, 4, 1, 2 };
25  for (int i=0;i<4;i++) for (int j=0;j<4;j++) cov44(i,j) = cov55( subs[i], subs[j] );
26  m_trkCovMatrix[chamberId] = cov44;
27 }
28 
29 
31  const Trajectory *traj,
32  const reco::Track* trk,
33  AlignableNavigator *navigator, double maxResidual)
34  : track(trk)
35 {
36  clear();
37 
38  std::vector<TrajectoryMeasurement> measurements = traj->measurements();
39  for (std::vector<TrajectoryMeasurement>::const_iterator im = measurements.begin(); im != measurements.end(); ++im) {
40  TrajectoryMeasurement meas = *im;
41  const TransientTrackingRecHit *hit = &(*meas.recHit());
42  DetId id = hit->geographicalId();
43 
44  if (hit->isValid()) {
46  if (tsos.isValid() && fabs(tsos.localPosition().x() - hit->localPosition().x()) < maxResidual) {
47 
48  if (id.det() == DetId::Tracker) {
49  double xresid = tsos.localPosition().x() - hit->localPosition().x();
50  double xresiderr2 = tsos.localError().positionError().xx() + hit->localPositionError().xx();
51 
53  m_tracker_chi2 += xresid * xresid / xresiderr2;
54 
55  if (id.subdetId() == StripSubdetector::TID || id.subdetId() == StripSubdetector::TEC) m_contains_TIDTEC = true;
56  }
57 
58  else if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::DT) {
59  const DTChamberId chamberId(id.rawId());
60  const DTSuperLayerId superLayerId(id.rawId());
61 
62  // have we seen this chamber before?
63  if (m_dt13.find(chamberId) == m_dt13.end() && m_dt2.find(chamberId) == m_dt2.end()) {
64  m_chamberIds.push_back(chamberId);
65  //addTrkCovMatrix(chamberId, tsos); // only for the 1st hit
66  }
67 
68  if (superLayerId.superlayer() == 2) {
69  if (m_dt2.find(chamberId) == m_dt2.end()) {
70  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
71  m_dt2[chamberId] = new MuonDT2ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
72  }
73 
74  m_dt2[chamberId]->addResidual(&tsos, hit);
75  }
76 
77  else {
78  if (m_dt13.find(chamberId) == m_dt13.end()) {
79  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
80  m_dt13[chamberId] = new MuonDT13ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
81  }
82 
83  m_dt13[chamberId]->addResidual(&tsos, hit);
84  }
85  }
86 
87  else if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
88  const CSCDetId cscDetId(id.rawId());
89  const CSCDetId chamberId(cscDetId.endcap(), cscDetId.station(), cscDetId.ring(), cscDetId.chamber());
90 
91  // not sure why we sometimes get layer == 0
92  if (cscDetId.layer() == 0) continue;
93 
94  // have we seen this chamber before?
95  if (m_csc.find(chamberId) == m_csc.end())
96  {
97  m_chamberIds.push_back(chamberId);
98  //addTrkCovMatrix(chamberId, tsos); // only for the 1st hit
99  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
100  m_csc[chamberId] = new MuonCSCChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
101  }
102 
103  m_csc[chamberId]->addResidual(&tsos, hit);
104  }
105 
106  } // end if track propagation is valid
107  } // end if hit is valid
108  } // end loop over measurments
109 }
110 
111 
113  : muon(mu)
114 {
115  clear();
117  track = muon->innerTrack().get();
118 
119  m_tracker_chi2 = muon->innerTrack()->chi2();
120  m_tracker_numHits = muon->innerTrack()->ndof() + 5;
122 
123  /*
124  for (trackingRecHit_iterator hit = muon->innerTrack()->recHitsBegin(); hit != muon->innerTrack()->recHitsEnd(); ++hit)
125  {
126  DetId id = (*hit)->geographicalId();
127  if (id.det() == DetId::Tracker)
128  {
129  m_tracker_numHits++;
130  if (id.subdetId() == StripSubdetector::TID || id.subdetId() == StripSubdetector::TEC) m_contains_TIDTEC = true;
131  }
132  }
133  */
134 
135  for (std::vector<reco::MuonChamberMatch>::const_iterator chamberMatch = muon->matches().begin();
136  chamberMatch != muon->matches().end(); chamberMatch++)
137  {
138  if (chamberMatch->id.det() != DetId::Muon ) continue;
139 
140  for (std::vector<reco::MuonSegmentMatch>::const_iterator segMatch = chamberMatch->segmentMatches.begin();
141  segMatch != chamberMatch->segmentMatches.end(); ++segMatch)
142  {
143  // select the only segment that belongs to track and is the best in station by dR
144  if (! (segMatch->isMask(reco::MuonSegmentMatch::BestInStationByDR) &&
145  segMatch->isMask(reco::MuonSegmentMatch::BelongsToTrackByDR)) ) continue;
146 
147  if (chamberMatch->id.subdetId() == MuonSubdetId::DT)
148  {
149  const DTChamberId chamberId(chamberMatch->id.rawId());
150 
151  DTRecSegment4DRef segmentDT = segMatch->dtSegmentRef;
152  const DTRecSegment4D* segment = segmentDT.get();
153  if (segment == 0) continue;
154 
155  if ( segment->hasPhi() && fabs(chamberMatch->x - segMatch->x) > maxResidual ) continue;
156  if ( segment->hasZed() && fabs(chamberMatch->y - segMatch->y) > maxResidual ) continue;
157 
158  // have we seen this chamber before?
159  if (m_dt13.find(chamberId) == m_dt13.end() && m_dt2.find(chamberId) == m_dt2.end()) {
160  m_chamberIds.push_back(chamberId);
161  }
162 
163  if (segment->hasZed())
164  {
165  if (m_dt2.find(chamberId) == m_dt2.end())
166  {
167  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
168  m_dt2[chamberId] = new MuonTrackDT2ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
169  }
170  else std::cout<<"multi segment match to tmuon: dt2 -- should not happen!"<<std::endl;
171  m_dt2[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
172  }
173  if (segment->hasPhi())
174  {
175  if (m_dt13.find(chamberId) == m_dt13.end())
176  {
177  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
178  m_dt13[chamberId] = new MuonTrackDT13ChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
179  }
180  else std::cout<<"multi segment match to tmuon: dt13 -- should not happen!"<<std::endl;
181  m_dt13[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
182  }
183  }
184 
185  else if (chamberMatch->id.subdetId() == MuonSubdetId::CSC)
186  {
187  const CSCDetId cscDetId(chamberMatch->id.rawId());
188  const CSCDetId chamberId(cscDetId.chamberId());
189 
190  if ( fabs(chamberMatch->x - segMatch->x) > maxResidual ) continue;
191 
192  // have we seen this chamber before?
193  if (m_csc.find(chamberId) == m_csc.end())
194  {
195  m_chamberIds.push_back(chamberId);
196  AlignableDetOrUnitPtr chamberAlignable = navigator->alignableFromDetId(chamberId);
197  m_csc[chamberId] = new MuonTrackCSCChamberResidual(globalGeometry, navigator, chamberId, chamberAlignable);
198  }
199  else std::cout<<"multi segment match to tmuon: csc -- should not happen!"<<std::endl;
200  m_csc[chamberId]->setSegmentResidual(&(*chamberMatch), &(*segMatch));
201  }
202 
203  }
204  }
205 }
206 
207 
209 {
210  for (std::map<DetId,MuonChamberResidual*>::const_iterator residual = m_dt13.begin(); residual != m_dt13.end(); ++residual) {
211  delete residual->second;
212  }
213  for (std::map<DetId,MuonChamberResidual*>::const_iterator residual = m_dt2.begin(); residual != m_dt2.end(); ++residual) {
214  delete residual->second;
215  }
216  for (std::map<DetId,MuonChamberResidual*>::const_iterator residual = m_csc.begin(); residual != m_csc.end(); ++residual) {
217  delete residual->second;
218  }
219 }
220 
221 
223 {
224  m_tracker_numHits = 0;
225  m_tracker_chi2 = 0.;
226  m_contains_TIDTEC = false;
227  m_chamberIds.clear();
228  m_dt13.clear();
229  m_dt2.clear();
230  m_csc.clear();
231  m_trkCovMatrix.clear();
232 }
233 
234 
236 {
237  if (m_tracker_numHits > 5) return m_tracker_chi2 / double(m_tracker_numHits - 5);
238  else return -1.;
239 }
240 
241 
243 {
244  if (muon) return track->normalizedChi2();
245  return trackerRedChi2();
246 }
247 
248 
250 {
251  if (type == MuonChamberResidual::kDT13) {
252  if (m_dt13.find(chamberId) == m_dt13.end()) return NULL;
253  return m_dt13[chamberId];
254  }
255  else if (type == MuonChamberResidual::kDT2) {
256  if (m_dt2.find(chamberId) == m_dt2.end()) return NULL;
257  return m_dt2[chamberId];
258  }
259  else if (type == MuonChamberResidual::kCSC) {
260  if (m_csc.find(chamberId) == m_csc.end()) return NULL;
261  return m_csc[chamberId];
262  }
263  else return NULL;
264 }
265 
266 
268 {
269  TMatrixDSym result(4);
270  std::cout<<"MuonResidualsFromTrack:: cov initial:"<<std::endl;
271  result.Print();
272  if (m_trkCovMatrix.find(chamberId) == m_trkCovMatrix.end())
273  {
274  std::cout<<"MuonResidualsFromTrack:: cov does not exist!"<<std::endl;
275  return result;
276  }
277  result = m_trkCovMatrix[chamberId];
278 
279  std::cout<<"MuonResidualsFromTrack:: cov before:"<<std::endl;
280  result.Print();
281 
282  // add segment's errors in quadratures to track's covariance matrix
283  double r_err;
284  if (m_csc.find(chamberId) == m_csc.end())
285  {
286  r_err = m_csc[chamberId]->residual_error();
287  result(0,0) += r_err*r_err;
288  r_err = m_csc[chamberId]->resslope_error();
289  result(2,2) += r_err*r_err;
290  }
291  if (m_dt13.find(chamberId) == m_dt13.end())
292  {
293  r_err = m_dt13[chamberId]->residual_error();
294  result(0,0) += r_err*r_err;
295  r_err = m_dt13[chamberId]->resslope_error();
296  result(2,2) += r_err*r_err;
297  }
298  if (m_dt2.find(chamberId) == m_dt2.end())
299  {
300  r_err = m_dt2[chamberId]->residual_error();
301  result(1,1) += r_err*r_err;
302  r_err = m_dt2[chamberId]->resslope_error();
303  result(3,3) += r_err*r_err;
304  }
305  std::cout<<"MuonResidualsFromTrack:: cov after:"<<std::endl;
306  result.Print();
307 
308  return result;
309 }
310 
312 {
313  TMatrixDSym result(4);
314  TMatrixDSym cov44 = covMatrix(chamberId);
315 
316  // invert it using cholesky decomposition
317  TDecompChol decomp(cov44);
318  bool ok = decomp.Invert(result);
319  std::cout<<"MuonResidualsFromTrack:: corr after:"<<std::endl;
320  result.Print();
321 
322  if (!ok){std::cout<<"MuonResidualsFromTrack:: cov inversion failed!"<<std::endl;}
323  return result;
324 }
325 
327 {
328  TMatrixD result(4,4);
329  TMatrixDSym corr44 = corrMatrix(chamberId);
330 
331  // get an upper triangular matrix U such that corr = U^T * U
332  TDecompChol decomp(corr44);
333  bool ok = decomp.Decompose();
334  result = decomp.GetU();
335 
336  std::cout<<"MuonResidualsFromTrack:: corr cholesky after:"<<std::endl;
337  result.Print();
338 
339  if (!ok){std::cout<<"MuonResidualsFromTrack:: corr decomposition failed!"<<std::endl;}
340  return result;
341 }
int chamber() const
Definition: CSCDetId.h:81
type
Definition: HCALResponse.h:21
int i
Definition: DBlmapReader.cc:9
float xx() const
Definition: LocalError.h:24
std::vector< DetId > m_chamberIds
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
ConstRecHitPointer const & recHit() const
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:609
virtual TrackRef innerTrack() const
Definition: Muon.h:48
bool isTrackerMuon() const
Definition: Muon.h:219
std::map< DetId, MuonChamberResidual * > m_dt2
assert(m_qm.get())
TMatrixDSym covMatrix(DetId chamberId)
#define NULL
Definition: scimark2.h:8
std::map< DetId, MuonChamberResidual * > m_csc
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
int layer() const
Definition: CSCDetId.h:74
LocalError positionError() const
static const unsigned int BestInStationByDR
int endcap() const
Definition: CSCDetId.h:106
DataContainer const & measurements() const
Definition: Trajectory.h:203
static const int CSC
Definition: MuonSubdetId.h:13
MuonResidualsFromTrack(edm::ESHandle< GlobalTrackingGeometry > globalGeometry, const Trajectory *traj, const reco::Track *trk, AlignableNavigator *navigator, double maxResidual)
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
TMatrixDSym corrMatrix(DetId chamberId)
const AlgebraicSymMatrix55 & matrix() const
const int mu
Definition: Constants.h:22
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:242
bool hasPhi() const
Does it have the Phi projection?
const LocalTrajectoryError & localError() const
TrajectoryStateOnSurface const & forwardPredictedState() const
Access to forward predicted state (from fitter or builder)
int superlayer() const
Return the superlayer number (deprecated method name)
int ring() const
Definition: CSCDetId.h:88
bool hasZed() const
Does it have the Z projection?
Definition: DetId.h:18
std::map< DetId, TMatrixDSym > m_trkCovMatrix
virtual LocalError localPositionError() const =0
TMatrixD choleskyCorrMatrix(DetId chamberId)
bool isValid() const
std::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:140
void addTrkCovMatrix(DetId, TrajectoryStateOnSurface &)
static const unsigned int BelongsToTrackByDR
int station() const
Definition: CSCDetId.h:99
tuple cout
Definition: gather_cfg.py:121
static const int DT
Definition: MuonSubdetId.h:12
DetId geographicalId() const
T x() const
Definition: PV3DBase.h:62
virtual LocalPoint localPosition() const =0
std::map< DetId, MuonChamberResidual * > m_dt13
AlignableDetOrUnitPtr alignableFromDetId(const DetId &detid)
Returns AlignableDetOrUnitPtr corresponding to given DetId.
TrajectoryStateOnSurface const & backwardPredictedState() const
Access to backward predicted state (from smoother)
TrajectoryStateCombiner m_tsoscomb
MuonChamberResidual * chamberResidual(DetId chamberId, int type)