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