CMS 3D CMS Logo

CSCPairResidualsConstraint.cc
Go to the documentation of this file.
1 #include <iomanip>
8 
10  double delta = (m_sum1 * m_sumxx) - (m_sumx * m_sumx);
11  assert(delta > 0.);
13  return ((m_sumxx * m_sumy) - (m_sumx * m_sumxy)) / delta;
14  } else if (m_parent->m_mode == kModePhiz) {
15  return ((m_sum1 * m_sumxy) - (m_sumx * m_sumy)) / delta;
16  } else
17  assert(false);
18 }
19 
21  if (m_parent->m_errorFromRMS) {
22  assert(m_sum1 > 0.);
23  return sqrt((m_sumyy / m_sum1) - pow(m_sumy / m_sum1, 2)) / sqrt(m_sumN);
24  } else {
25  double delta = (m_sum1 * m_sumxx) - (m_sumx * m_sumx);
26  assert(delta > 0.);
28  return sqrt(m_sumxx / delta);
29  } else if (m_parent->m_mode == kModePhiz) {
30  return sqrt(m_sum1 / delta);
31  } else
32  assert(false);
33  }
34 }
35 
37 
39  m_parent = parent;
40 
42  edm::Service<TFileService> tFileService;
43 
44  std::stringstream name, name2, name3, title;
45  title << "i =" << m_id_i << " j =" << m_id_j;
46 
47  name << "slopeResiduals_" << m_identifier;
48  m_slopeResiduals = tFileService->make<TH1F>(name.str().c_str(), title.str().c_str(), 300, -30., 30.);
49 
50  name2 << "offsetResiduals_" << m_identifier;
51  m_offsetResiduals = tFileService->make<TH1F>(name2.str().c_str(), title.str().c_str(), 300, -30., 30.);
52 
53  name3 << "radial_" << m_identifier;
54  m_radial = tFileService->make<TH1F>(name3.str().c_str(), title.str().c_str(), 700, 0., 700.);
55  } else {
56  m_slopeResiduals = nullptr;
57  m_offsetResiduals = nullptr;
58  m_radial = nullptr;
59  }
60 }
61 
63  m_cscGeometry = cscGeometry;
64 
67  2.;
70  2.;
71 
74 
83 
85 }
86 
88 
89 bool CSCPairResidualsConstraint::addTrack(const std::vector<TrajectoryMeasurement> &measurements,
91  const TrackTransformer *trackTransformer) {
92  std::vector<const TransientTrackingRecHit *> hits_i;
93  std::vector<const TransientTrackingRecHit *> hits_j;
94 
95  for (std::vector<TrajectoryMeasurement>::const_iterator measurement = measurements.begin();
96  measurement != measurements.end();
97  ++measurement) {
98  const TransientTrackingRecHit *hit = &*(measurement->recHit());
99 
100  DetId id = hit->geographicalId();
101  if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
102  CSCDetId cscid(id.rawId());
103  CSCDetId chamberId(cscid.endcap(), cscid.station(), cscid.ring(), cscid.chamber(), 0);
104  if (m_parent->m_combineME11 && cscid.station() == 1 && cscid.ring() == 4)
105  chamberId = CSCDetId(cscid.endcap(), 1, 1, cscid.chamber(), 0);
106 
107  if (chamberId == m_id_i)
108  hits_i.push_back(hit);
109  if (chamberId == m_id_j)
110  hits_j.push_back(hit);
111  }
112  }
113 
114  if (m_parent->m_makeHistograms) {
115  m_parent->m_hitsPerChamber->Fill(hits_i.size());
116  m_parent->m_hitsPerChamber->Fill(hits_j.size());
117  }
118 
119  // require minimum number of hits (if the requirement is too low (~2), some NANs might result...)
120  if (int(hits_i.size()) < m_parent->m_minHitsPerChamber || int(hits_j.size()) < m_parent->m_minHitsPerChamber)
121  return false;
122 
123  // maybe require segments to be fiducial
124  if (m_parent->m_fiducial && !(isFiducial(hits_i, true) && isFiducial(hits_j, false)))
125  return false;
126 
127  double intercept_i = 0.;
128  double interceptError2_i = 0.;
129  double slope_i = 0.;
130  double slopeError2_i = 0.;
131  double intercept_j = 0.;
132  double interceptError2_j = 0.;
133  double slope_j = 0.;
134  double slopeError2_j = 0.;
135 
136  // if slopeFromTrackRefit, then you'll need to refit the whole track without this station's hits;
137  // need at least two other stations for that to be reliable
139  double dphidz;
140  if (dphidzFromTrack(measurements, track, trackTransformer, dphidz)) {
141  double sum1_i = 0.;
142  double sumy_i = 0.;
143  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_i.begin(); hit != hits_i.end();
144  ++hit) {
145  double phi, phierr2;
146  calculatePhi(*hit, phi, phierr2, false, true);
147  double z = (*hit)->globalPosition().z() - m_Zplane;
148 
149  double weight = 1.;
151  weight = 1. / phierr2;
152  sum1_i += weight;
153  sumy_i += weight * (phi - z * dphidz);
154  }
155 
156  double sum1_j = 0.;
157  double sumy_j = 0.;
158  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_j.begin(); hit != hits_j.end();
159  ++hit) {
160  double phi, phierr2;
161  calculatePhi(*hit, phi, phierr2, false, true);
162  double z = (*hit)->globalPosition().z() - m_Zplane;
163 
164  double weight = 1.;
166  weight = 1. / phierr2;
167  sum1_j += weight;
168  sumy_j += weight * (phi - z * dphidz);
169  }
170 
171  if (sum1_i != 0. && sum1_j != 0.) {
172  slope_i = slope_j = dphidz;
173 
174  intercept_i = sumy_i / sum1_i;
175  interceptError2_i = 1. / sum1_i;
176 
177  intercept_j = sumy_j / sum1_j;
178  interceptError2_j = 1. / sum1_j;
179  } else
180  return false;
181  }
182  }
183 
184  else { // not slopeFromTrackRefit
185  double sum1_i = 0.;
186  double sumx_i = 0.;
187  double sumy_i = 0.;
188  double sumxx_i = 0.;
189  double sumxy_i = 0.;
190  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_i.begin(); hit != hits_i.end();
191  ++hit) {
192  double phi, phierr2;
193  calculatePhi(*hit, phi, phierr2, false, true);
194  double z = (*hit)->globalPosition().z() - m_Zplane;
195 
196  double weight = 1.;
198  weight = 1. / phierr2;
199  sum1_i += weight;
200  sumx_i += weight * z;
201  sumy_i += weight * phi;
202  sumxx_i += weight * z * z;
203  sumxy_i += weight * z * phi;
204  }
205 
206  double sum1_j = 0.;
207  double sumx_j = 0.;
208  double sumy_j = 0.;
209  double sumxx_j = 0.;
210  double sumxy_j = 0.;
211  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_j.begin(); hit != hits_j.end();
212  ++hit) {
213  double phi, phierr2;
214  calculatePhi(*hit, phi, phierr2, false, true);
215  double z = (*hit)->globalPosition().z() - m_Zplane;
216 
217  double weight = 1.;
219  weight = 1. / phierr2;
220  sum1_j += weight;
221  sumx_j += weight * z;
222  sumy_j += weight * phi;
223  sumxx_j += weight * z * z;
224  sumxy_j += weight * z * phi;
225  }
226 
227  double delta_i = (sum1_i * sumxx_i) - (sumx_i * sumx_i);
228  double delta_j = (sum1_j * sumxx_j) - (sumx_j * sumx_j);
229  if (delta_i != 0. && delta_j != 0.) {
230  intercept_i = ((sumxx_i * sumy_i) - (sumx_i * sumxy_i)) / delta_i;
231  interceptError2_i = sumxx_i / delta_i;
232  slope_i = ((sum1_i * sumxy_i) - (sumx_i * sumy_i)) / delta_i;
233  slopeError2_i = sum1_i / delta_i;
234 
235  intercept_j = ((sumxx_j * sumy_j) - (sumx_j * sumxy_j)) / delta_j;
236  interceptError2_j = sumxx_j / delta_j;
237  slope_j = ((sum1_j * sumxy_j) - (sumx_j * sumy_j)) / delta_j;
238  slopeError2_j = sum1_j / delta_j;
239  } else
240  return false;
241  }
242 
243  // from hits on the two chambers, determine radial_intercepts separately and radial_slope together
244  double sum1_ri = 0.;
245  double sumx_ri = 0.;
246  double sumy_ri = 0.;
247  double sumxx_ri = 0.;
248  double sumxy_ri = 0.;
249  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_i.begin(); hit != hits_i.end(); ++hit) {
250  double r = (*hit)->globalPosition().perp();
251  double z = (*hit)->globalPosition().z() - m_Zplane;
252  sum1_ri += 1.;
253  sumx_ri += z;
254  sumy_ri += r;
255  sumxx_ri += z * z;
256  sumxy_ri += z * r;
257  }
258  double radial_delta_i = (sum1_ri * sumxx_ri) - (sumx_ri * sumx_ri);
259  if (radial_delta_i == 0.)
260  return false;
261  double radial_slope_i = ((sum1_ri * sumxy_ri) - (sumx_ri * sumy_ri)) / radial_delta_i;
262  double radial_intercept_i =
263  ((sumxx_ri * sumy_ri) - (sumx_ri * sumxy_ri)) / radial_delta_i + radial_slope_i * (m_iZ - m_Zplane);
264 
265  double sum1_rj = 0.;
266  double sumx_rj = 0.;
267  double sumy_rj = 0.;
268  double sumxx_rj = 0.;
269  double sumxy_rj = 0.;
270  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits_j.begin(); hit != hits_j.end(); ++hit) {
271  double r = (*hit)->globalPosition().perp();
272  double z = (*hit)->globalPosition().z() - m_Zplane;
273  sum1_rj += 1.;
274  sumx_rj += z;
275  sumy_rj += r;
276  sumxx_rj += z * z;
277  sumxy_rj += z * r;
278  }
279  double radial_delta_j = (sum1_rj * sumxx_rj) - (sumx_rj * sumx_rj);
280  if (radial_delta_j == 0.)
281  return false;
282  double radial_slope_j = ((sum1_rj * sumxy_rj) - (sumx_rj * sumy_rj)) / radial_delta_j;
283  double radial_intercept_j =
284  ((sumxx_rj * sumy_rj) - (sumx_rj * sumxy_rj)) / radial_delta_j + radial_slope_j * (m_jZ - m_Zplane);
285 
286  double radial_delta = ((sum1_ri + sum1_rj) * (sumxx_ri + sumxx_rj)) - ((sumx_ri + sumx_rj) * (sumx_ri + sumx_rj));
287  if (radial_delta == 0.)
288  return false;
289  double radial_intercept =
290  (((sumxx_ri + sumxx_rj) * (sumy_ri + sumy_rj)) - ((sumx_ri + sumx_rj) * (sumxy_ri + sumxy_rj))) / radial_delta;
291  double radial_slope =
292  (((sum1_ri + sum1_rj) * (sumxy_ri + sumxy_rj)) - ((sumx_ri + sumx_rj) * (sumy_ri + sumy_rj))) / radial_delta;
293 
294  if (m_parent->m_makeHistograms) {
295  m_parent->m_drdz->Fill(radial_slope);
296  }
297  if (m_parent->m_maxdrdz > 0. && fabs(radial_slope) > m_parent->m_maxdrdz)
298  return false;
299 
300  double quantity = 0.;
301  double quantityError2 = 0.;
302  if (m_parent->m_mode == kModePhiy) { // phiy comes from track d(rphi)/dz
303  quantity = (slope_i * radial_intercept_i) - (slope_j * radial_intercept_j);
304  quantityError2 = (slopeError2_i)*pow(radial_intercept_i, 2) + (slopeError2_j)*pow(radial_intercept_j, 2);
305  } else if (m_parent->m_mode == kModePhiPos || m_parent->m_mode == kModeRadius) { // phipos comes from phi intercepts
306  quantity = intercept_i - intercept_j;
307  quantityError2 = interceptError2_i + interceptError2_j;
308  } else if (m_parent->m_mode == kModePhiz) { // phiz comes from the slope of rphi intercepts
309  quantity = (intercept_i - intercept_j) * radial_intercept;
310  quantityError2 = (interceptError2_i + interceptError2_j) * pow(radial_intercept, 2);
311  } else
312  assert(false);
313 
314  if (quantityError2 == 0.)
315  return false;
316 
317  double slopeResid = ((slope_i * radial_intercept_i) - (slope_j * radial_intercept_j)) * 1000.;
318  double slopeResidError2 =
319  ((slopeError2_i)*pow(radial_intercept_i, 2) + (slopeError2_j)*pow(radial_intercept_j, 2)) * 1000. * 1000.;
320  double offsetResid = (intercept_i - intercept_j) * radial_intercept * 10.;
321  double offsetResidError2 = (interceptError2_i + interceptError2_j) * pow(radial_intercept, 2) * 10. * 10.;
322 
323  if (m_parent->m_truncateSlopeResid > 0. && fabs(slopeResid) > m_parent->m_truncateSlopeResid)
324  return false;
325  if (m_parent->m_truncateOffsetResid > 0. && fabs(offsetResid) > m_parent->m_truncateOffsetResid)
326  return false;
327 
328  double weight = 1.;
330  weight = 1. / quantityError2;
331 
332  // fill the running sums for this CSCPairResidualsConstraint
333  m_sumN += 1;
334  m_sum1 += weight;
335  m_sumx += weight * (radial_intercept - m_averageRadius);
336  m_sumy += weight * quantity;
337  m_sumxx += weight * pow(radial_intercept - m_averageRadius, 2);
339  m_sumxy += weight * (radial_intercept - m_averageRadius) * quantity;
340 
341  if (m_parent->m_makeHistograms) {
342  double rphi_slope_i = slope_i * radial_intercept_i;
343  double rphi_slope_j = slope_j * radial_intercept_j;
344 
346  m_parent->m_slope->Fill(rphi_slope_i); // == rphi_slope_j
347 
348  if (m_id_i.endcap() == 1 && m_id_i.station() == 4)
349  m_parent->m_slope_MEp4->Fill(rphi_slope_i);
350  if (m_id_i.endcap() == 1 && m_id_i.station() == 3)
351  m_parent->m_slope_MEp3->Fill(rphi_slope_i);
352  if (m_id_i.endcap() == 1 && m_id_i.station() == 2)
353  m_parent->m_slope_MEp2->Fill(rphi_slope_i);
354  if (m_id_i.endcap() == 1 && m_id_i.station() == 1)
355  m_parent->m_slope_MEp1->Fill(rphi_slope_i);
356  if (m_id_i.endcap() == 2 && m_id_i.station() == 1)
357  m_parent->m_slope_MEm1->Fill(rphi_slope_i);
358  if (m_id_i.endcap() == 2 && m_id_i.station() == 2)
359  m_parent->m_slope_MEm2->Fill(rphi_slope_i);
360  if (m_id_i.endcap() == 2 && m_id_i.station() == 3)
361  m_parent->m_slope_MEm3->Fill(rphi_slope_i);
362  if (m_id_i.endcap() == 2 && m_id_i.station() == 4)
363  m_parent->m_slope_MEm4->Fill(rphi_slope_i);
364  } else {
365  m_parent->m_slope->Fill(rphi_slope_i);
366  m_parent->m_slope->Fill(rphi_slope_j);
367 
368  if (m_id_i.endcap() == 1 && m_id_i.station() == 4) {
369  m_parent->m_slope_MEp4->Fill(rphi_slope_i);
370  m_parent->m_slope_MEp4->Fill(rphi_slope_j);
371  }
372  if (m_id_i.endcap() == 1 && m_id_i.station() == 3) {
373  m_parent->m_slope_MEp3->Fill(rphi_slope_i);
374  m_parent->m_slope_MEp3->Fill(rphi_slope_j);
375  }
376  if (m_id_i.endcap() == 1 && m_id_i.station() == 2) {
377  m_parent->m_slope_MEp2->Fill(rphi_slope_i);
378  m_parent->m_slope_MEp2->Fill(rphi_slope_j);
379  }
380  if (m_id_i.endcap() == 1 && m_id_i.station() == 1) {
381  m_parent->m_slope_MEp1->Fill(rphi_slope_i);
382  m_parent->m_slope_MEp1->Fill(rphi_slope_j);
383  }
384  if (m_id_i.endcap() == 2 && m_id_i.station() == 1) {
385  m_parent->m_slope_MEm1->Fill(rphi_slope_i);
386  m_parent->m_slope_MEm1->Fill(rphi_slope_j);
387  }
388  if (m_id_i.endcap() == 2 && m_id_i.station() == 2) {
389  m_parent->m_slope_MEm2->Fill(rphi_slope_i);
390  m_parent->m_slope_MEm2->Fill(rphi_slope_j);
391  }
392  if (m_id_i.endcap() == 2 && m_id_i.station() == 3) {
393  m_parent->m_slope_MEm3->Fill(rphi_slope_i);
394  m_parent->m_slope_MEm3->Fill(rphi_slope_j);
395  }
396  if (m_id_i.endcap() == 2 && m_id_i.station() == 4) {
397  m_parent->m_slope_MEm4->Fill(rphi_slope_i);
398  m_parent->m_slope_MEm4->Fill(rphi_slope_j);
399  }
400  }
401 
402  m_slopeResiduals->Fill(slopeResid);
403  m_offsetResiduals->Fill(offsetResid);
404  m_radial->Fill(radial_intercept);
405 
406  m_parent->m_slopeResiduals->Fill(slopeResid);
407  m_parent->m_slopeResiduals_weighted->Fill(slopeResid, 1. / slopeResidError2);
408  m_parent->m_slopeResiduals_normalized->Fill(slopeResid / sqrt(slopeResidError2));
409 
410  m_parent->m_offsetResiduals->Fill(offsetResid);
411  m_parent->m_offsetResiduals_weighted->Fill(offsetResid, 1. / offsetResidError2);
412  m_parent->m_offsetResiduals_normalized->Fill(offsetResid / sqrt(offsetResidError2));
413 
414  double ringbin = 0;
415  if (m_id_i.endcap() == 2 && m_id_i.station() == 4 && m_id_i.ring() == 2)
416  ringbin = 1.5;
417  else if (m_id_i.endcap() == 2 && m_id_i.station() == 4 && m_id_i.ring() == 1)
418  ringbin = 2.5;
419  else if (m_id_i.endcap() == 2 && m_id_i.station() == 3 && m_id_i.ring() == 2)
420  ringbin = 3.5;
421  else if (m_id_i.endcap() == 2 && m_id_i.station() == 3 && m_id_i.ring() == 1)
422  ringbin = 4.5;
423  else if (m_id_i.endcap() == 2 && m_id_i.station() == 2 && m_id_i.ring() == 2)
424  ringbin = 5.5;
425  else if (m_id_i.endcap() == 2 && m_id_i.station() == 2 && m_id_i.ring() == 1)
426  ringbin = 6.5;
427  else if (m_id_i.endcap() == 2 && m_id_i.station() == 1 && m_id_i.ring() == 3)
428  ringbin = 7.5;
429  else if (m_id_i.endcap() == 2 && m_id_i.station() == 1 && m_id_i.ring() == 2)
430  ringbin = 8.5;
431  else if (m_id_i.endcap() == 2 && m_id_i.station() == 1 && m_id_i.ring() == 1)
432  ringbin = 9.5;
433  else if (m_id_i.endcap() == 2 && m_id_i.station() == 1 && m_id_i.ring() == 4)
434  ringbin = 10.5;
435  else if (m_id_i.endcap() == 1 && m_id_i.station() == 1 && m_id_i.ring() == 4)
436  ringbin = 11.5;
437  else if (m_id_i.endcap() == 1 && m_id_i.station() == 1 && m_id_i.ring() == 1)
438  ringbin = 12.5;
439  else if (m_id_i.endcap() == 1 && m_id_i.station() == 1 && m_id_i.ring() == 2)
440  ringbin = 13.5;
441  else if (m_id_i.endcap() == 1 && m_id_i.station() == 1 && m_id_i.ring() == 3)
442  ringbin = 14.5;
443  else if (m_id_i.endcap() == 1 && m_id_i.station() == 2 && m_id_i.ring() == 1)
444  ringbin = 15.5;
445  else if (m_id_i.endcap() == 1 && m_id_i.station() == 2 && m_id_i.ring() == 2)
446  ringbin = 16.5;
447  else if (m_id_i.endcap() == 1 && m_id_i.station() == 3 && m_id_i.ring() == 1)
448  ringbin = 17.5;
449  else if (m_id_i.endcap() == 1 && m_id_i.station() == 3 && m_id_i.ring() == 2)
450  ringbin = 18.5;
451  else if (m_id_i.endcap() == 1 && m_id_i.station() == 4 && m_id_i.ring() == 1)
452  ringbin = 19.5;
453  else if (m_id_i.endcap() == 1 && m_id_i.station() == 4 && m_id_i.ring() == 2)
454  ringbin = 20.5;
455  m_parent->m_occupancy->Fill(m_id_i.chamber() + 0.5, ringbin);
456  }
457 
458  return true;
459 }
460 
461 bool CSCPairResidualsConstraint::dphidzFromTrack(const std::vector<TrajectoryMeasurement> &measurements,
463  const TrackTransformer *trackTransformer,
464  double &dphidz) {
465  // make a list of hits on all chambers *other* than the ones associated with this constraint
466  std::map<int, int> stations;
468  for (std::vector<TrajectoryMeasurement>::const_iterator measurement = measurements.begin();
469  measurement != measurements.end();
470  ++measurement) {
471  DetId id = measurement->recHit()->geographicalId();
472  if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
473  CSCDetId cscid(id.rawId());
474  CSCDetId chamberId(cscid.endcap(), cscid.station(), cscid.ring(), cscid.chamber(), 0);
475  if (m_parent->m_combineME11 && cscid.station() == 1 && cscid.ring() == 4)
476  chamberId = CSCDetId(cscid.endcap(), 1, 1, cscid.chamber(), 0);
477 
478  if (chamberId != m_id_i && chamberId != m_id_j) {
479  int station = (cscid.endcap() == 1 ? 1 : -1) * cscid.station();
480  if (stations.find(station) == stations.end()) {
481  stations[station] = 0;
482  }
483  stations[station]++;
484 
485  cscHits.push_back(measurement->recHit());
486  }
487  }
488  }
489 
490  // for the fit to be reliable, it needs to cross multiple stations
491  int numStations = 0;
492  for (std::map<int, int>::const_iterator station = stations.begin(); station != stations.end(); ++station) {
493  if (station->second >= m_parent->m_minHitsPerChamber) {
494  numStations++;
495  }
496  }
497 
498  if (numStations >= m_parent->m_minStationsInTrackRefits) {
499  // refit the track with these hits
500  std::vector<Trajectory> trajectories = trackTransformer->transform(track, cscHits);
501 
502  if (!trajectories.empty()) {
503  const std::vector<TrajectoryMeasurement> &measurements2 = trajectories.begin()->measurements();
504 
505  // find the closest TSOS to the Z plane (on both sides)
506  bool found_plus = false;
507  bool found_minus = false;
508  TrajectoryStateOnSurface tsos_plus, tsos_minus;
509  for (std::vector<TrajectoryMeasurement>::const_iterator measurement = measurements2.begin();
510  measurement != measurements2.end();
511  ++measurement) {
512  double z = measurement->recHit()->globalPosition().z();
513  if (z > m_Zplane) {
514  if (!found_plus || fabs(z - m_Zplane) < fabs(tsos_plus.globalPosition().z() - m_Zplane)) {
515  tsos_plus = TrajectoryStateCombiner().combine(measurement->forwardPredictedState(),
516  measurement->backwardPredictedState());
517  }
518  if (tsos_plus.isValid())
519  found_plus = true;
520  } else {
521  if (!found_minus || fabs(z - m_Zplane) < fabs(tsos_minus.globalPosition().z() - m_Zplane)) {
522  tsos_minus = TrajectoryStateCombiner().combine(measurement->forwardPredictedState(),
523  measurement->backwardPredictedState());
524  }
525  if (tsos_minus.isValid())
526  found_minus = true;
527  }
528  }
529 
530  // propagate from the closest TSOS to the Z plane (from both sides, if possible)
531  TrajectoryStateOnSurface from_plus, from_minus;
532  if (found_plus) {
533  from_plus = m_propagator->propagate(tsos_plus, *m_Zsurface);
534  }
535  if (found_minus) {
536  from_minus = m_propagator->propagate(tsos_minus, *m_Zsurface);
537  }
538 
539  // if you have two sides, merge them
541  if (found_plus && from_plus.isValid() && found_minus && from_minus.isValid()) {
542  merged = TrajectoryStateCombiner().combine(from_plus, from_minus);
543  } else if (found_plus && from_plus.isValid()) {
544  merged = from_plus;
545  } else if (found_minus && from_minus.isValid()) {
546  merged = from_minus;
547  } else
548  return false;
549 
550  // if, after all that, we have a good fit-and-propagation, report the direction
551  if (merged.isValid()) {
552  double angle = merged.globalPosition().phi() + M_PI / 2.;
553  GlobalVector direction = merged.globalDirection();
554  double dxdz = direction.x() / direction.z();
555  double dydz = direction.y() / direction.z();
556  dphidz = (dxdz * cos(angle) + dydz * sin(angle)) / merged.globalPosition().perp();
557  return true;
558  }
559 
560  } // end if refit successful
561  } // end if enough hits
562  return false;
563 }
564 
566  output << std::setprecision(14) << std::fixed;
567  output << "CSCPairResidualsConstraint " << m_identifier << " " << i() << " " << j() << " " << m_sumN << " " << m_sum1
568  << " " << m_sumx << " " << m_sumy << " " << m_sumxx << " " << m_sumyy << " " << m_sumxy << " EOLN"
569  << std::endl;
570 }
571 
572 void CSCPairResidualsConstraint::read(std::vector<std::ifstream *> &input, std::vector<std::string> &filenames) {
573  m_sumN = 0;
574  m_sum1 = 0.;
575  m_sumx = 0.;
576  m_sumy = 0.;
577  m_sumxx = 0.;
578  m_sumyy = 0.;
579  m_sumxy = 0.;
580 
581  std::vector<std::ifstream *>::const_iterator inputiter = input.begin();
582  std::vector<std::string>::const_iterator filename = filenames.begin();
583  for (; inputiter != input.end(); ++inputiter, ++filename) {
584  int linenumber = 0;
585  bool touched = false;
586  while (!(*inputiter)->eof()) {
587  linenumber++;
588  std::string name, eoln;
589  unsigned int identifier;
590  int i, j;
591  int sumN;
592  double sum1, sumx, sumy, sumxx, sumyy, sumxy;
593 
594  (**inputiter) >> name >> identifier >> i >> j >> sumN >> sum1 >> sumx >> sumy >> sumxx >> sumyy >> sumxy >> eoln;
595 
596  if (!(*inputiter)->eof() && (name != "CSCPairResidualsConstraint" || eoln != "EOLN"))
597  throw cms::Exception("CorruptTempFile")
598  << "Temporary file " << *filename << " is incorrectly formatted on line " << linenumber << std::endl;
599 
600  if (identifier == m_identifier) {
601  if (i != m_i || j != m_j)
602  throw cms::Exception("CorruptTempFile")
603  << "Wrong (i,j) for CSCPairResidualsConstraint " << m_identifier << " (" << m_i << "," << m_j
604  << ") in file " << *filename << " on line " << linenumber << std::endl;
605  touched = true;
606 
607  m_sumN += sumN;
608  m_sum1 += sum1;
609  m_sumx += sumx;
610  m_sumy += sumy;
611  m_sumxx += sumxx;
612  m_sumyy += sumyy;
613  m_sumxy += sumxy;
614  }
615  }
616 
617  (*inputiter)->clear();
618  (*inputiter)->seekg(0, std::ios::beg);
619 
620  if (!touched)
621  throw cms::Exception("CorruptTempFile")
622  << "CSCPairResidualsConstraint " << m_identifier << " is missing from file " << *filename << std::endl;
623  }
624 }
625 
627  const TransientTrackingRecHit *hit, double &phi, double &phierr2, bool doRphi, bool globalPhi) {
628  align::LocalPoint pos = hit->localPosition();
629  DetId id = hit->geographicalId();
630  CSCDetId cscid = CSCDetId(id.rawId());
631 
632  double r = 0.;
633  if (globalPhi) {
634  phi = hit->globalPosition().phi();
635  r = hit->globalPosition().perp();
636 
637  // double sinAngle = sin(phi);
638  // double cosAngle = cos(phi);
639  // double xx = hit->globalPositionError().cxx();
640  // double xy = hit->globalPositionError().cyx();
641  // double yy = hit->globalPositionError().cyy();
642  // phierr2 = (xx*cosAngle*cosAngle + 2.*xy*sinAngle*cosAngle + yy*sinAngle*sinAngle) / (r*r);
643  } else {
644  // these constants are related to the way CSC chambers are built--- really constant!
645  const double R_ME11 = 181.5;
646  const double R_ME12 = 369.7;
647  const double R_ME21 = 242.7;
648  const double R_ME31 = 252.7;
649  const double R_ME41 = 262.65;
650  const double R_MEx2 = 526.5;
651 
652  double R = 0.;
653  if (cscid.station() == 1 && (cscid.ring() == 1 || cscid.ring() == 4))
654  R = R_ME11;
655  else if (cscid.station() == 1 && cscid.ring() == 2)
656  R = R_ME12;
657  else if (cscid.station() == 2 && cscid.ring() == 1)
658  R = R_ME21;
659  else if (cscid.station() == 3 && cscid.ring() == 1)
660  R = R_ME31;
661  else if (cscid.station() == 4 && cscid.ring() == 1)
662  R = R_ME41;
663  else if (cscid.station() > 1 && cscid.ring() == 2)
664  R = R_MEx2;
665  else
666  assert(false);
667  r = (pos.y() + R);
668 
669  phi = atan2(pos.x(), r);
670 
671  if (cscid.endcap() == 1 && cscid.station() >= 3)
672  phi *= -1;
673  else if (cscid.endcap() == 2 && cscid.station() <= 2)
674  phi *= -1;
675  }
676 
678  double angle = m_cscGeometry->layer(id)->geometry()->stripAngle(strip) - M_PI / 2.;
679  double sinAngle = sin(angle);
680  double cosAngle = cos(angle);
681  double xx = hit->localPositionError().xx();
682  double xy = hit->localPositionError().xy();
683  double yy = hit->localPositionError().yy();
684  phierr2 = (xx * cosAngle * cosAngle + 2. * xy * sinAngle * cosAngle + yy * sinAngle * sinAngle) / (r * r);
685 
686  if (doRphi) {
687  phi *= r;
688  phierr2 *= r * r;
689  }
690 }
691 
692 bool CSCPairResidualsConstraint::isFiducial(std::vector<const TransientTrackingRecHit *> &hits, bool is_i) {
693  // these constants are related to the way CSC chambers are built--- really constant!
694  const double cut_ME11 = 0.086;
695  const double cut_ME12 = 0.090;
696  const double cut_MEx1 = 0.180;
697  const double cut_MEx2 = 0.090;
698 
699  double sum1 = 0.;
700  double sumx = 0.;
701  double sumy = 0.;
702  double sumxx = 0.;
703  double sumxy = 0.;
704  for (std::vector<const TransientTrackingRecHit *>::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
705  double phi, phierr2;
706  calculatePhi(*hit, phi, phierr2);
707  double z = (is_i ? m_cscGeometry->idToDet(m_id_i)->surface() : m_cscGeometry->idToDet(m_id_j)->surface())
708  .toLocal((*hit)->globalPosition())
709  .z();
710 
711  if (m_parent->m_makeHistograms) {
712  if (m_id_i.station() == 1 && (m_id_i.ring() == 1 || m_id_i.ring() == 4)) {
713  m_parent->m_fiducial_ME11->Fill(fabs(phi), sqrt(phierr2));
714  } else if (m_id_i.station() == 1 && m_id_i.ring() == 2) {
715  m_parent->m_fiducial_ME12->Fill(fabs(phi), sqrt(phierr2));
716  } else if (m_id_i.station() > 1 && m_id_i.ring() == 1) {
717  m_parent->m_fiducial_MEx1->Fill(fabs(phi), sqrt(phierr2));
718  } else if (m_id_i.station() > 1 && m_id_i.ring() == 2) {
719  m_parent->m_fiducial_MEx2->Fill(fabs(phi), sqrt(phierr2));
720  }
721  }
722 
723  double weight = 1.;
725  weight = 1. / phierr2;
726  sum1 += weight;
727  sumx += weight * z;
728  sumy += weight * phi;
729  sumxx += weight * z * z;
730  sumxy += weight * z * phi;
731  }
732  double delta = (sum1 * sumxx) - (sumx * sumx);
733  if (delta == 0.)
734  return false;
735  double intercept = ((sumxx * sumy) - (sumx * sumxy)) / delta;
736  double slope = ((sum1 * sumxy) - (sumx * sumy)) / delta;
737 
738  double phi1 = intercept + slope * (is_i ? m_iZ1 : m_jZ1);
739  double phi6 = intercept + slope * (is_i ? m_iZ6 : m_jZ6);
740 
741  if (m_id_i.station() == 1 && (m_id_i.ring() == 1 || m_id_i.ring() == 4)) {
742  return (fabs(phi1) < cut_ME11 && fabs(phi6) < cut_ME11);
743  } else if (m_id_i.station() == 1 && m_id_i.ring() == 2) {
744  return (fabs(phi1) < cut_ME12 && fabs(phi6) < cut_ME12);
745  } else if (m_id_i.station() > 1 && m_id_i.ring() == 1) {
746  return (fabs(phi1) < cut_MEx1 && fabs(phi6) < cut_MEx1);
747  } else if (m_id_i.station() > 1 && m_id_i.ring() == 2) {
748  return (fabs(phi1) < cut_MEx2 && fabs(phi6) < cut_MEx2);
749  } else
750  assert(false);
751 }
float dydz
T perp() const
Definition: PV3DBase.h:69
bool isFiducial(std::vector< const TransientTrackingRecHit *> &hits, bool is_i)
float dxdz
int nearestStrip(const LocalPoint &lp) const
T z() const
Definition: PV3DBase.h:61
static const double slope[3]
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Definition: weight.py:1
void setZplane(const CSCGeometry *cscGeometry)
LocalPoint toLocal(const GlobalPoint &gp) const
assert(be >=bs)
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
float stripAngle(int strip) const
const CSCLayerGeometry * geometry() const
Definition: CSCLayer.h:44
static std::string const input
Definition: EdmProvDump.cc:50
static PlanePointer build(Args &&... args)
Definition: Plane.h:33
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
GlobalPoint globalPosition() const
T sqrt(T t)
Definition: SSEVec.h:23
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
void configure(CSCOverlapsAlignmentAlgorithm *parent)
void write(std::ofstream &output)
void read(std::vector< std::ifstream *> &input, std::vector< std::string > &filenames)
void calculatePhi(const TransientTrackingRecHit *hit, double &phi, double &phierr2, bool doRphi=false, bool globalPhi=false)
int chamber() const
Definition: CSCDetId.h:62
LocalVector toLocal(const reco::Track::Vector &v, const Surface &s)
#define M_PI
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:17
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
const PositionType & position() const
int station() const
Definition: CSCDetId.h:79
int endcap() const
Definition: CSCDetId.h:85
virtual int j() const
bool addTrack(const std::vector< TrajectoryMeasurement > &measurements, const reco::TransientTrack &track, const TrackTransformer *trackTransformer)
CSCOverlapsAlignmentAlgorithm * m_parent
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
Definition: output.py:1
virtual int i() const
int ring() const
Definition: CSCDetId.h:68
bool dphidzFromTrack(const std::vector< TrajectoryMeasurement > &measurements, const reco::TransientTrack &track, const TrackTransformer *trackTransformer, double &drphidz)
const CSCLayer * layer(CSCDetId id) const
Return the layer corresponding to given DetId.
Definition: CSCGeometry.cc:105
static constexpr int CSC
Definition: MuonSubdetId.h:12
std::vector< Trajectory > transform(const reco::Track &) const override
Convert a reco::Track into Trajectory.
TSOS combine(const TSOS &pTsos1, const TSOS &pTsos2) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
void setPropagator(const Propagator *propagator)
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11
const GeomDet * idToDet(DetId) const override
Definition: CSCGeometry.cc:91