CMS 3D CMS Logo

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