CMS 3D CMS Logo

MuonDTSeedFromRecHits.cc
Go to the documentation of this file.
1 
12 
14 
18 
20 
21 #include "gsl/gsl_statistics.h"
22 
23 using namespace std;
24 
26 
28  double pt[16] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.};
29  // these weights are supposed to be 1/sigma^2(pt), but they're so small.
30  // Instead of the 0.2-0.5 GeV here, we'll add something extra in quadrature later
31  double spt[8] = {1 / 0.048, 1 / 0.075, 1 / 0.226, 1 / 0.132, 1 / 0.106, 1 / 0.175, 1 / 0.125, 1 / 0.185};
32 
33  const std::string metname = "Muon|RecoMuon|MuonDTSeedFromRecHits";
34 
36  computePtWithVtx(pt, spt);
37 
39  computePtWithoutVtx(pt, spt);
40 
41  // some dump...
42  LogTrace(metname) << " Pt MB1 @vtx: " << pt[0] << " w: " << spt[0] << "\n"
43  << " Pt MB2 @vtx: " << pt[1] << " w: " << spt[1] << endl;
44 
45  LogTrace(metname) << " Pt MB2-MB1 " << pt[2] << " w: " << spt[2] << "\n"
46  << " Pt MB3-MB1 " << pt[3] << " w: " << spt[3] << "\n"
47  << " Pt MB3-MB2 " << pt[4] << " w: " << spt[4] << "\n"
48  << " Pt MB4-MB1 " << pt[5] << " w: " << spt[5] << "\n"
49  << " Pt MB4-MB2 " << pt[6] << " w: " << spt[6] << "\n"
50  << " Pt MB4-MB3 " << pt[7] << " w: " << spt[7] << endl;
51 
53  float ptmean = 0.;
54  float sptmean = 0.;
55  //@@ Use Shih-Chuan's
56  computeBestPt(pt, spt, ptmean, sptmean);
57 
58  // add an extra term to the error in quadrature, 30% of pT per point
59  int npoints = 0;
60  for (int i = 0; i < 8; ++i)
61  if (pt[i] != 0)
62  ++npoints;
63  if (npoints != 0) {
64  sptmean = sqrt(sptmean * sptmean + 0.09 * ptmean * ptmean / npoints);
65  }
66 
67  LogTrace(metname) << " Seed Pt: " << ptmean << " +/- " << sptmean << endl;
68  // take the best candidate
70  return createSeed(ptmean, sptmean, last);
71 }
72 
74  const MuonRecHitContainer& barrelHits) const {
75  int alt_npt = 0;
76  int best_npt = 0;
77  int cur_npt = 0;
78  MuonRecHitPointer best = nullptr;
79  MuonRecHitPointer alter = nullptr;
80 
81  for (MuonRecHitContainer::const_iterator iter = barrelHits.begin(); iter != barrelHits.end(); iter++) {
82  bool hasZed = ((*iter)->projectionMatrix()[1][1] == 1);
83 
84  cur_npt = 0;
85  vector<TrackingRecHit*> slrhs = (*iter)->recHits();
86  for (vector<TrackingRecHit*>::const_iterator slrh = slrhs.begin(); slrh != slrhs.end(); ++slrh) {
87  cur_npt += (*slrh)->recHits().size();
88  }
89  float radius1 = (*iter)->det()->position().perp();
90 
91  if (hasZed) {
92  if (cur_npt > best_npt) {
93  best = (*iter);
94  best_npt = cur_npt;
95  } else if (best && cur_npt == best_npt) {
96  float radius2 = best->det()->position().perp();
97  if (radius1 < radius2) {
98  best = (*iter);
99  best_npt = cur_npt;
100  }
101  }
102  }
103 
104  if (cur_npt > alt_npt) {
105  alter = (*iter);
106  alt_npt = cur_npt;
107  } else if (alter && cur_npt == alt_npt) {
108  float radius2 = alter->det()->position().perp();
109  if (radius1 < radius2) {
110  alter = (*iter);
111  alt_npt = cur_npt;
112  }
113  }
114  }
115 
116  if (!best)
117  best = alter;
118 
119  return best;
120 }
121 
123  int Maxseg = 0;
124  float Msdeta = 100.;
125  float result = (*theRhits.begin())->globalPosition().eta();
126 
127  for (MuonRecHitContainer::const_iterator iter = theRhits.begin(); iter != theRhits.end(); iter++) {
128  float eta1 = (*iter)->globalPosition().eta();
129 
130  int Nseg = 0;
131  float sdeta = .0;
132 
133  for (MuonRecHitContainer::const_iterator iter2 = theRhits.begin(); iter2 != theRhits.end(); iter2++) {
134  if (iter2 == iter)
135  continue;
136 
137  float eta2 = (*iter2)->globalPosition().eta();
138 
139  if (fabs(eta1 - eta2) > .2)
140  continue;
141 
142  Nseg++;
143  sdeta += fabs(eta1 - eta2);
144 
145  if (Nseg > Maxseg || (Nseg == Maxseg && sdeta < Msdeta)) {
146  Maxseg = Nseg;
147  Msdeta = sdeta;
148  result = eta1;
149  }
150  }
151  } // +v.
152  return result;
153 }
154 
155 void MuonDTSeedFromRecHits::computePtWithVtx(double* pt, double* spt) const {
156  float eta0 = bestEta();
157 
158  for (MuonRecHitContainer::const_iterator iter = theRhits.begin(); iter != theRhits.end(); iter++) {
159  //+vvp !:
160 
161  float eta1 = (*iter)->globalPosition().eta();
162  if (fabs(eta1 - eta0) > .2)
163  continue; // !!! +vvp
164 
165  // assign Pt from MB1 & vtx
166  unsigned int stat = DTChamberId((**iter).geographicalId()).station();
167  if (stat > 2)
168  continue;
169 
170  double thispt = thePtExtractor->pT_extract(*iter, *iter)[0];
171  float ptmax = 2000.;
172  if (thispt > ptmax)
173  thispt = ptmax;
174  if (thispt < -ptmax)
175  thispt = -ptmax;
176 
177  if (stat == 1) {
178  pt[0] = thispt;
179  }
180  // assign Pt from MB2 & vtx
181  if (stat == 2) {
182  pt[1] = thispt;
183  }
184  }
185 }
186 
187 void MuonDTSeedFromRecHits::computePtWithoutVtx(double* pt, double* spt) const {
188  float eta0 = bestEta();
189 
190  for (MuonRecHitContainer::const_iterator iter = theRhits.begin(); iter != theRhits.end(); iter++) {
191  //+vvp !:
192  float eta1 = (*iter)->globalPosition().eta();
193  if (fabs(eta1 - eta0) > .2)
194  continue; // !!! +vvp
195 
196  for (MuonRecHitContainer::const_iterator iter2 = theRhits.begin(); iter2 != iter; iter2++) {
197  //+vvp !:
198  float eta2 = (*iter2)->globalPosition().eta();
199  if (fabs(eta2 - eta0) > .2)
200  continue; // !!! +vvp
201 
202  unsigned int stat1 = DTChamberId((*iter)->geographicalId()).station();
203  unsigned int stat2 = DTChamberId((*iter2)->geographicalId()).station();
204 
205  if (stat1 > stat2) {
206  int tmp = stat1;
207  stat1 = stat2;
208  stat2 = tmp;
209  }
210  unsigned int st = stat1 * 10 + stat2;
211  double thispt = thePtExtractor->pT_extract(*iter, *iter2)[0];
212  switch (st) {
213  case 12: { //MB1-MB2
214  pt[2] = thispt;
215  break;
216  }
217  case 13: { //MB1-MB3
218  pt[3] = thispt;
219  break;
220  }
221  case 14: { //MB1-MB4
222  pt[5] = thispt;
223  break;
224  }
225  case 23: { //MB2-MB3
226  pt[4] = thispt;
227  break;
228  }
229  case 24: { //MB2-MB4
230  pt[6] = thispt;
231  break;
232  }
233  case 34: { //MB3-MB4
234  pt[7] = thispt;
235  break;
236  }
237  default: { break; }
238  }
239  }
240  }
241 }
242 
243 void MuonDTSeedFromRecHits::computeBestPt(double* pt, double* spt, float& ptmean, float& sptmean) const {
244  const std::string metname = "Muon|RecoMuon|MuonDTSeedFromRecHits";
245 
246  int nTotal = 8;
247  int igood = -1;
248  for (int i = 0; i <= 7; i++) {
249  if (pt[i] == 0) {
250  spt[i] = 0.;
251  nTotal--;
252  } else {
253  igood = i;
254  }
255  }
256 
257  if (nTotal == 1) {
259  ptmean = pt[igood];
260  sptmean = 1 / sqrt(spt[igood]);
261  } else if (nTotal == 0) {
262  // No estimate (e.g. only one rechit!)
263  ptmean = 50;
264  sptmean = 30;
265  } else {
267  // calculate pt with vertex
268  float ptvtx = 0.0;
269  float sptvtx = 0.0;
270  computeMean(pt, spt, 2, false, ptvtx, sptvtx);
271  LogTrace(metname) << " GSL: Pt w vtx :" << ptvtx << "+/-" << sptvtx << endl;
272 
273  // FIXME: temp hack
274  if (ptvtx != 0.) {
275  ptmean = ptvtx;
276  sptmean = sptvtx;
277  return;
278  //
279  }
280  // calculate pt w/o vertex
281  float ptMB = 0.0;
282  float sptMB = 0.0;
283  computeMean(pt + 2, spt + 2, 6, false, ptMB, sptMB);
284  LogTrace(metname) << " GSL: Pt w/o vtx :" << ptMB << "+/-" << sptMB << endl;
285 
286  // decide wheter the muon comes or not from vertex
287  float ptpool = 0.0;
288  if ((ptvtx + ptMB) != 0.0)
289  ptpool = (ptvtx - ptMB) / (ptvtx + ptMB);
290  bool fromvtx = true;
291  if (fabs(ptpool) > 0.2)
292  fromvtx = false;
293  LogTrace(metname) << "From vtx? " << fromvtx << " ptpool " << ptpool << endl;
294 
295  // now choose the "right" pt => weighted mean
296  computeMean(pt, spt, 8, true, ptmean, sptmean);
297  LogTrace(metname) << " GSL Pt :" << ptmean << "+/-" << sptmean << endl;
298  }
299 }
300 
302  const double* pt, const double* weights, int sz, bool tossOutlyers, float& ptmean, float& sptmean) const {
303  int n = 0;
304  double ptTmp[8];
305  double wtTmp[8];
306  assert(sz <= 8);
307 
308  for (int i = 0; i < 8; ++i) {
309  ptTmp[i] = 0.;
310  wtTmp[i] = 0;
311  if (i < sz && pt[i] != 0) {
312  ptTmp[n] = pt[i];
313  wtTmp[n] = weights[i];
314  ++n;
315  }
316  }
317  if (n != 0) {
318  if (n == 1) {
319  ptmean = ptTmp[0];
320  sptmean = 1 / sqrt(wtTmp[0]);
321  } else {
322  ptmean = gsl_stats_wmean(wtTmp, 1, ptTmp, 1, n);
323  sptmean = sqrt(gsl_stats_wvariance_m(wtTmp, 1, ptTmp, 1, n, ptmean));
324  }
325 
326  if (tossOutlyers) {
327  // Recompute mean with a cut at 3 sigma
328  for (int nm = 0; nm < 8; nm++) {
329  if (ptTmp[nm] != 0 && fabs(ptTmp[nm] - ptmean) > 3 * (sptmean)) {
330  wtTmp[nm] = 0.;
331  }
332  }
333  ptmean = gsl_stats_wmean(wtTmp, 1, ptTmp, 1, n);
334  sptmean = sqrt(gsl_stats_wvariance_m(wtTmp, 1, ptTmp, 1, n, ptmean));
335  }
336  }
337 }
MuonTransientTrackingRecHit::MuonRecHitPointer MuonRecHitPointer
ConstMuonRecHitPointer bestBarrelHit(const MuonRecHitContainer &barrelHits) const
void computePtWithVtx(double *pt, double *spt) const
void computeBestPt(double *pt, double *spt, float &ptmean, float &sptmean) const
const std::string metname
MuonTransientTrackingRecHit::MuonRecHitContainer theRhits
MuonTransientTrackingRecHit::MuonRecHitContainer MuonRecHitContainer
MuonTransientTrackingRecHit::ConstMuonRecHitPointer ConstMuonRecHitPointer
const MuonSeedPtExtractor * thePtExtractor
T sqrt(T t)
Definition: SSEVec.h:19
static const int npoints
void computeMean(const double *pt, const double *weights, int sz, bool tossOutlyers, float &ptmean, float &sptmean) const
#define LogTrace(id)
void computePtWithoutVtx(double *pt, double *spt) const
virtual TrajectorySeed seed() const
TrajectorySeed createSeed(float ptmean, float sptmean, MuonTransientTrackingRecHit::ConstMuonRecHitPointer last) const
virtual std::vector< double > pT_extract(MuonTransientTrackingRecHit::ConstMuonRecHitPointer firstHit, MuonTransientTrackingRecHit::ConstMuonRecHitPointer secondHit) const
tmp
align.sh
Definition: createJobs.py:716