CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CosmicMuonTrajectoryBuilder.cc
Go to the documentation of this file.
13 
14 /* Collaborating Class Header */
38 
39 #include <algorithm>
40 
41 using namespace edm;
42 using namespace std;
43 
45 
46  thePropagatorName = par.getParameter<string>("Propagator");
47 
48  bool enableDTMeasurement = par.getParameter<bool>("EnableDTMeasurement");
49  bool enableCSCMeasurement = par.getParameter<bool>("EnableCSCMeasurement");
50  bool enableRPCMeasurement = par.getParameter<bool>("EnableRPCMeasurement");
51 
52 // if(enableDTMeasurement)
53  InputTag DTRecSegmentLabel = par.getParameter<InputTag>("DTRecSegmentLabel");
54 
55 // if(enableCSCMeasurement)
56  InputTag CSCRecSegmentLabel = par.getParameter<InputTag>("CSCRecSegmentLabel");
57 
58 // if(enableRPCMeasurement)
59  InputTag RPCRecSegmentLabel = par.getParameter<InputTag>("RPCRecSegmentLabel");
60  theLayerMeasurements= new MuonDetLayerMeasurements(DTRecSegmentLabel,
61  CSCRecSegmentLabel,
62  RPCRecSegmentLabel,
63  iC,
64  enableDTMeasurement,
65  enableCSCMeasurement,
66  enableRPCMeasurement);
67 
68  ParameterSet muonUpdatorPSet = par.getParameter<ParameterSet>("MuonTrajectoryUpdatorParameters");
69 
70  theNavigation = 0; // new DirectMuonNavigation(theService->detLayerGeometry());
71  theUpdator = new MuonTrajectoryUpdator(muonUpdatorPSet, insideOut);
72 
74 
75  ParameterSet muonBackwardUpdatorPSet = par.getParameter<ParameterSet>("BackwardMuonTrajectoryUpdatorParameters");
76 
77  theBKUpdator = new MuonTrajectoryUpdator(muonBackwardUpdatorPSet, outsideIn);
78 
79  theTraversingMuonFlag = par.getParameter<bool>("BuildTraversingMuon");
80 
81  theStrict1LegFlag = par.getParameter<bool>("Strict1Leg");
82 
83  ParameterSet smootherPSet = par.getParameter<ParameterSet>("MuonSmootherParameters");
84 
85  theNavigationPSet = par.getParameter<ParameterSet>("MuonNavigationParameters");
86 
87  theSmoother = new CosmicMuonSmoother(smootherPSet,theService);
88 
89  theNTraversing = 0;
90  theNSuccess = 0;
91  theCacheId_DG = 0;
92  category_ = "Muon|RecoMuon|CosmicMuon|CosmicMuonTrajectoryBuilder";
93 
94 }
95 
96 
98 
99  LogTrace(category_)<< "CosmicMuonTrajectoryBuilder dtor called";
100  if (theUpdator) delete theUpdator;
101  if (theBKUpdator) delete theBKUpdator;
103  if (theSmoother) delete theSmoother;
104  if (theNavigation) delete theNavigation;
106 
107  LogTrace(category_)<< "CosmicMuonTrajectoryBuilder Traversing: "<<theNSuccess<<"/"<<theNTraversing;
108 
109 }
110 
111 
113 
115 
116  // DetLayer Geometry
117  unsigned long long newCacheId_DG = theService->eventSetup().get<MuonRecoGeometryRecord>().cacheIdentifier();
118  if ( newCacheId_DG != theCacheId_DG ) {
119  LogTrace(category_) << "Muon Reco Geometry changed!";
120  theCacheId_DG = newCacheId_DG;
121  if (theNavigation) delete theNavigation;
123  }
124 
125 
126 }
127 
128 
131 
132  vector<Trajectory*> trajL = vector<Trajectory*>();
133 
135 
136  PTrajectoryStateOnDet ptsd1(seed.startingState());
137  DetId did(ptsd1.detId());
138  const BoundPlane& bp = theService->trackingGeometry()->idToDet(did)->surface();
139  TrajectoryStateOnSurface lastTsos = trajectoryStateTransform::transientState(ptsd1,&bp,&*theService->magneticField());
140  LogTrace(category_) << "Seed: mom "<<lastTsos.globalMomentum()
141  <<"pos: " <<lastTsos.globalPosition();
142  LogTrace(category_) << "Seed: mom eta "<<lastTsos.globalMomentum().eta()
143  <<"pos eta: " <<lastTsos.globalPosition().eta();
144 
145  bool beamhaloFlag = ( (did.subdetId() == MuonSubdetId::CSC) && fabs(lastTsos.globalMomentum().eta()) > 4.0);
146 
147  vector<const DetLayer*> navLayers;
148 
149  if (did.subdetId() == MuonSubdetId::DT) {
150  //DT
151  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), alongMomentum);
152  }
153  else if (beamhaloFlag || (theTraversingMuonFlag && theStrict1LegFlag)) {
154  //CSC
155  navLayers = navigation()->compatibleEndcapLayers(*(lastTsos.freeState()), alongMomentum);
156  } else {
157  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), alongMomentum);
158  }
159 
160  LogTrace(category_) <<"found "<<navLayers.size()<<" compatible DetLayers for the Seed";
161 
162  if (navLayers.empty()) return trajL;
163 
164  vector<DetWithState> detsWithStates;
165  LogTrace(category_) << "Compatible layers: ";
166  for ( vector<const DetLayer*>::const_iterator layer = navLayers.begin();
167  layer != navLayers.end(); layer++) {
168  LogTrace(category_) << debug.dumpMuonId((*layer)->basicComponents().front()->geographicalId())
169  << debug.dumpLayer(*layer);
170  }
171 
172  detsWithStates = navLayers.front()->compatibleDets(lastTsos, *propagator(), *(updator()->estimator()));
173  LogTrace(category_) << "Number of compatible dets: " << detsWithStates.size() << endl;
174 
175  if ( !detsWithStates.empty() ) {
176  // get the updated TSOS
177  if ( detsWithStates.front().second.isValid() ) {
178  LogTrace(category_) << "New starting TSOS is on det: " << endl;
179  LogTrace(category_) << debug.dumpMuonId(detsWithStates.front().first->geographicalId())
180  << debug.dumpLayer(navLayers.front());
181  lastTsos = detsWithStates.front().second;
182  LogTrace(category_) << "Seed after extrapolation: mom " << lastTsos.globalMomentum()
183  << "pos: " << lastTsos.globalPosition();
184  }
185  }
186  detsWithStates.clear();
187  if ( !lastTsos.isValid() ) return trajL;
188 
189  TrajectoryStateOnSurface secondLast = lastTsos;
190 
191  lastTsos.rescaleError(10.0);
192 
193  Trajectory* theTraj = new Trajectory(seed,alongMomentum);
194 
195  navLayers.clear();
196 
197  if (fabs(lastTsos.globalMomentum().eta()) < 1.0) {
198  //DT
199  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), alongMomentum);
200  } else if (beamhaloFlag || (theTraversingMuonFlag && theStrict1LegFlag)) {
201  //CSC
202  navLayers = navigation()->compatibleEndcapLayers(*(lastTsos.freeState()), alongMomentum);
203  } else {
204  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), alongMomentum);
205  }
206 
207 
208  int DTChamberUsedBack = 0;
209  int CSCChamberUsedBack = 0;
210  int RPCChamberUsedBack = 0;
211  int TotalChamberUsedBack = 0;
213  vector<TrajectoryMeasurement> measL;
214 
215  LogTrace(category_) << "Begin forward fit " << navLayers.size();
216 
217  for ( vector<const DetLayer*>::const_iterator rnxtlayer = navLayers.begin(); rnxtlayer!= navLayers.end(); ++rnxtlayer) {
218  LogTrace(category_) << "new layer ";
219  measL.clear();
220  LogTrace(category_) << debug.dumpMuonId((*rnxtlayer)->basicComponents().front()->geographicalId())
221  << debug.dumpLayer(*rnxtlayer);
222  LogTrace(category_) << "from lastTsos " << lastTsos.globalMomentum()<< " at " <<lastTsos.globalPosition();
223 
224  measL = findBestMeasurements(*rnxtlayer, lastTsos, propagator(), (updator()->estimator()));
225 
226  if ( measL.empty() && (fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) && (theService->propagator("StraightLinePropagator").isValid() ) ) {
227  LogTrace(category_) << "try straight line propagator ";
228  measL = findBestMeasurements(*rnxtlayer, lastTsos, &*theService->propagator("StraightLinePropagator"), (updator()->estimator()));
229  }
230  if ( measL.empty() ) continue;
231 
232  for (vector<TrajectoryMeasurement>::const_iterator theMeas = measL.begin(); theMeas != measL.end(); ++theMeas) {
233  pair<bool,TrajectoryStateOnSurface> result = updator()->update((&*theMeas), *theTraj, propagator());
234 
235  if (result.first ) {
236  LogTrace(category_) << "update ok ";
237  incrementChamberCounters((*rnxtlayer), DTChamberUsedBack, CSCChamberUsedBack, RPCChamberUsedBack, TotalChamberUsedBack);
238  secondLast = lastTsos;
239  if ( (!theTraj->empty()) && result.second.isValid() ) {
240  lastTsos = result.second;
241  LogTrace(category_) << "get new lastTsos here " << lastTsos.globalMomentum() << " at " << lastTsos.globalPosition();
242  } else if ((theMeas)->predictedState().isValid()) lastTsos = (theMeas)->predictedState();
243  }
244  }
245  }
246  measL.clear();
247  while (!theTraj->empty()) {
248  theTraj->pop();
249  }
250 
251  if (!theTraj->isValid() || TotalChamberUsedBack < 2 || (DTChamberUsedBack+CSCChamberUsedBack) == 0 || !lastTsos.isValid()) {
252  delete theTraj;
253  return trajL;
254  }
255  delete theTraj;
256 
257 
258  // if got good trajectory, then do backward refitting
259  DTChamberUsedBack = 0;
260  CSCChamberUsedBack = 0;
261  RPCChamberUsedBack = 0;
262  TotalChamberUsedBack = 0;
263 
264  Trajectory myTraj(seed, oppositeToMomentum);
265 
266  // set starting navigation direction for MuonTrajectoryUpdator
267 
268  GlobalPoint lastPos = lastTsos.globalPosition();
269  GlobalPoint secondLastPos = secondLast.globalPosition();
270  GlobalVector momDir = secondLastPos - lastPos;
271 
272  if ( lastPos.basicVector().dot(momDir.basicVector()) > 0 ) {
273 // LogTrace("CosmicMuonTrajectoryBuilder")<<"Fit direction changed to insideOut";
276 
277  if (fabs(lastTsos.globalMomentum().eta()) < 1.0) {
278  //DT
279  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), oppositeToMomentum);
280  } else if (beamhaloFlag || (theTraversingMuonFlag && theStrict1LegFlag)) {
281  //CSC
282  std::reverse(navLayers.begin(), navLayers.end());
283  } else {
284  navLayers = navigation()->compatibleLayers(*(lastTsos.freeState()), oppositeToMomentum);
285  }
286 
287  LogTrace(category_) << "Begin backward refitting, with " << navLayers.size() << " layers" << endl;
288 
289  for (vector<const DetLayer*>::const_iterator rnxtlayer = navLayers.begin();
290  rnxtlayer!= navLayers.end(); ++rnxtlayer) {
291 
292  measL.clear();
293 
294  measL = findBestMeasurements(*rnxtlayer, lastTsos, propagator(), (backwardUpdator()->estimator()));
295 
296  if ( measL.empty() ) {
298  for (MuonRecHitContainer::const_iterator ihit = tmpHits.begin();
299  ihit != tmpHits.end(); ++ihit ) {
300  allUnusedHits.push_back(*ihit);
301  }
302  continue;
303  }
304 
305  for (vector<TrajectoryMeasurement>::const_iterator theMeas = measL.begin(); theMeas != measL.end(); ++theMeas) {
306 
307  // if the part change, we need to reconsider the fit direction
308  if (rnxtlayer != navLayers.begin()) {
309 
310  vector<const DetLayer*>::const_iterator lastlayer = rnxtlayer;
311  lastlayer--;
312 
313  if ( (*rnxtlayer)->location() != (*lastlayer)->location() ) {
314 
315  lastPos = lastTsos.globalPosition();
316  GlobalPoint thisPos = (theMeas)->predictedState().globalPosition();
317  GlobalVector momDir = thisPos - lastPos;
318 // LogTrace("CosmicMuonTrajectoryBuilder")<<"momDir "<<momDir;
319 
320  if ( momDir.mag() > 0.01 ) { //if lastTsos is on the surface, no need
321  if ( thisPos.basicVector().dot(momDir.basicVector()) > 0 ) {
324  }
325  }
326  if ( ((*lastlayer)->location() == GeomDetEnumerators::endcap) &&
327  ((*rnxtlayer)->location() == GeomDetEnumerators::endcap) &&
328  (lastTsos.globalPosition().z() * (theMeas)->predictedState().globalPosition().z() < 0) ) {
330  }
331  }
332 
333 // if (theBKUpdator->fitDirection() == insideOut)
334 // LogTrace("CosmicMuonTrajectoryBuilder")<<"Fit direction insideOut";
335 // else LogTrace("CosmicMuonTrajectoryBuilder")<<"Fit direction outsideIn";
336  pair<bool,TrajectoryStateOnSurface> bkresult
337  = backwardUpdator()->update((&*theMeas), myTraj, propagator());
338 
339  if (bkresult.first ) {
340 
341  incrementChamberCounters((*rnxtlayer), DTChamberUsedBack, CSCChamberUsedBack, RPCChamberUsedBack, TotalChamberUsedBack);
342 
343  if ( theTraversingMuonFlag ) {
344 
345  MuonRecHitContainer tmpUnusedHits = unusedHits(*rnxtlayer,*theMeas);
346  allUnusedHits.insert(allUnusedHits.end(),tmpUnusedHits.begin(),tmpUnusedHits.end());
347  }
348  if ( (!myTraj.empty()) && bkresult.second.isValid() )
349  lastTsos = bkresult.second;
350  else if ((theMeas)->predictedState().isValid())
351  lastTsos = (theMeas)->predictedState();
352  }
353  }
354  }
355 
356  for ( vector<Trajectory*>::iterator t = trajL.begin(); t != trajL.end(); ++t ) delete *t;
357 
358  trajL.clear();
359 
360  if (( !myTraj.isValid() ) || ( myTraj.empty() ) || ( (selfDuplicate(myTraj)) )|| TotalChamberUsedBack < 2 || (DTChamberUsedBack+CSCChamberUsedBack) < 1) {
361  return trajL;
362  }
363 
364  if ( theTraversingMuonFlag && ( allUnusedHits.size() >= 2 )) {
365 // LogTrace(category_)<<utilities()->print(allUnusedHits);
366  LogTrace(category_)<<"Building trajectory in second hemisphere...";
367  buildSecondHalf(myTraj);
368  // check if traversing trajectory has hits in both hemispheres
369 
370  if ( theStrict1LegFlag && !utilities()->isTraversing(myTraj) ) {trajL.clear(); return trajL;}
371  } else if (theStrict1LegFlag && theTraversingMuonFlag) {trajL.clear(); return trajL;}
372 
373  LogTrace(category_) <<" traj ok ";
374 
375 // getDirectionByTime(myTraj);
376  if (beamhaloFlag) estimateDirection(myTraj);
377  if ( myTraj.empty() ) return trajL;
378 
379  // try to smooth it
380  vector<Trajectory> smoothed = theSmoother->trajectories(myTraj);
381 
382  if ( !smoothed.empty() && smoothed.front().foundHits()> 3 ) {
383  LogTrace(category_) <<" Smoothed successfully.";
384  myTraj = smoothed.front();
385  }
386  else {
387  LogTrace(category_) <<" Smooth failed.";
388  }
389 
390  LogTrace(category_) <<"first "<< myTraj.firstMeasurement().updatedState()
391  <<"\n last "<<myTraj.lastMeasurement().updatedState();
392  if ( myTraj.direction() == alongMomentum ) LogTrace(category_)<<"alongMomentum";
393  else if (myTraj.direction() == oppositeToMomentum ) LogTrace(category_)<<"oppositeMomentum";
394  else LogTrace(category_)<<"anyDirection";
395 
396  if (!beamhaloFlag) {
397  if ( myTraj.lastMeasurement().updatedState().globalMomentum().y() > 0 ) {
398  LogTrace(category_)<<"flip trajectory ";
399  flipTrajectory(myTraj);
400  }
401 
402  if ( ( myTraj.direction() == alongMomentum &&
404  < myTraj.lastMeasurement().updatedState().globalPosition().y()))
405  || (myTraj.direction() == oppositeToMomentum &&
407  > myTraj.lastMeasurement().updatedState().globalPosition().y())) ) {
408  LogTrace(category_)<<"reverse propagation direction";
410  }
411  }
412 // getDirectionByTime(myTraj);
413  if ( !myTraj.isValid() ) return trajL;
414 
415  // check direction agree with position!
417  GlobalVector dirFromPos = myTraj.measurements().back().recHit()->globalPosition() - myTraj.measurements().front().recHit()->globalPosition();
418 
419  if ( theStrict1LegFlag && !utilities()->isTraversing(myTraj) ) {trajL.clear(); return trajL;}
420 
421  LogTrace(category_)<< "last hit " <<myTraj.measurements().back().recHit()->globalPosition()<<endl;
422  LogTrace(category_)<< "first hit " <<myTraj.measurements().front().recHit()->globalPosition()<<endl;
423 
424  LogTrace(category_)<< "last tsos " <<myTraj.measurements().back().updatedState().globalPosition()<<" mom "<<myTraj.measurements().back().updatedState().globalMomentum()<<endl;
425  LogTrace(category_)<< "first tsos " <<myTraj.measurements().front().updatedState().globalPosition()<<" mom "<<myTraj.measurements().front().updatedState().globalMomentum()<<endl;
426 
427  PropagationDirection propDir =
429  LogTrace(category_)<<" dir "<<dir <<" propDir "<<propDir<<endl;
430 
431  LogTrace(category_)<<"chi2 "<<myTraj.chiSquared() <<endl;
432 
433  if (dir != propDir ) {
434  LogTrace(category_)<< "reverse propagation direction ";
436  }
437  if ( myTraj.empty() ) return trajL;
438 
439  trajL.push_back(new Trajectory(myTraj));
440  navLayers.clear();
441  return trajL;
442 }
443 
444 
445 //
446 //
447 //
450 
453  for (MuonRecHitContainer::const_iterator ihit = tmpHits.begin();
454  ihit != tmpHits.end(); ++ihit ) {
455  if ((*ihit)->geographicalId() != meas.recHit()->geographicalId() ){
456  result.push_back(*ihit);
457  LogTrace(category_) << "Unused hit: " << (*ihit)->globalPosition() << endl;
458  }
459  }
460 
461  return result;
462 
463 }
464 
465 
466 //
467 // continue to build a trajectory starting from given trajectory state
468 //
470  const NavigationDirection& startingDir,
471  Trajectory& traj) {
472 
473  if ( !ts.isValid() ) return;
474 
475  const FreeTrajectoryState* fts = ts.freeState();
476  if ( !fts ) return;
477 
478  vector<const DetLayer*> navLayers;
479 
480  if (fabs(fts->momentum().basicVector().eta()) < 1.0) {
481  //DT
482  if (fts->position().basicVector().dot(fts->momentum().basicVector())>0){
483  navLayers = navigation()->compatibleLayers((*fts), alongMomentum);
484  } else {
485  navLayers = navigation()->compatibleLayers((*fts), oppositeToMomentum);
486  }
487 
489  //CSC
490  if (fts->position().basicVector().dot(fts->momentum().basicVector())>0){
491  navLayers = navigation()->compatibleEndcapLayers((*fts), alongMomentum);
492  } else {
493  navLayers = navigation()->compatibleEndcapLayers((*fts), oppositeToMomentum);
494  }
495  } else {
496 
497  if (fts->position().basicVector().dot(fts->momentum().basicVector())>0){
498  navLayers = navigation()->compatibleLayers((*fts), alongMomentum);
499  } else {
500  navLayers = navigation()->compatibleLayers((*fts), oppositeToMomentum);
501  }
502 
503 }
504 
505  if (navLayers.empty()) return;
506 
507  theBKUpdator->setFitDirection(startingDir);
508 
509  int DTChamberUsedBack = 0;
510  int CSCChamberUsedBack = 0;
511  int RPCChamberUsedBack = 0;
512  int TotalChamberUsedBack = 0;
513 
514  TrajectoryStateOnSurface lastTsos =
517  propagatorAlong()->propagate((*fts),navLayers.front()->surface()) : propagatorOpposite()->propagate((*fts),navLayers.front()->surface());
518 
519  if ( !lastTsos.isValid() ) {
520  LogTrace(category_)<<"propagation failed from fts to inner cylinder";
521  return;
522  }
523  LogTrace(category_)<<"tsos "<<lastTsos.globalPosition();
524  lastTsos.rescaleError(10.);
525  vector<TrajectoryMeasurement> measL;
526  for (vector<const DetLayer*>::const_iterator rnxtlayer = navLayers.begin();
527  rnxtlayer!= navLayers.end(); ++rnxtlayer) {
528 
529  measL.clear();
530  measL = findBestMeasurements(*rnxtlayer, lastTsos, propagator(), (backwardUpdator()->estimator()));
531 
532  if ( measL.empty() ) continue;
533 
534  for (vector<TrajectoryMeasurement>::const_iterator theMeas = measL.begin(); theMeas != measL.end(); ++theMeas) {
535 
536  pair<bool,TrajectoryStateOnSurface> bkresult
537  = backwardUpdator()->update((&*theMeas), traj, propagator());
538  if (bkresult.first ) {
539  LogTrace(category_)<<"update ok : "<<(theMeas)->recHit()->globalPosition() ;
540 
541  incrementChamberCounters((*rnxtlayer), DTChamberUsedBack, CSCChamberUsedBack, RPCChamberUsedBack, TotalChamberUsedBack);
542 
543  if ( (!traj.empty()) && bkresult.second.isValid() )
544  lastTsos = bkresult.second;
545  else if ((theMeas)->predictedState().isValid())
546  lastTsos = (theMeas)->predictedState();
547  }
548  }
549  }
550  navLayers.clear();
551  updator()->makeFirstTime();
553 
554  measL.clear();
555 
556  return;
557 
558 }
559 
560 
561 //
562 // build trajectory in second hemisphere with pattern
563 // recognition starting from an intermediate state
564 //
566 
567  if ( (traj.firstMeasurement().recHit()->globalPosition().perp()
568  < traj.lastMeasurement().recHit()->globalPosition().perp()) ) {
569  LogTrace(category_)<<"inside-out: reverseTrajectory";
570  reverseTrajectory(traj);
571  }
572  if (traj.empty()) return;
574  if ( !tsos.isValid() ) tsos = traj.lastMeasurement().predictedState();
575  LogTrace(category_)<<"last tsos on traj: pos: "<< tsos.globalPosition()<<" mom: "<< tsos.globalMomentum();
576  if ( !tsos.isValid() ) {
577  LogTrace(category_)<<"last tsos on traj invalid";
578  return;
579  }
580 
581  build(intermediateState(tsos),insideOut,traj);
582 
583  return;
584 
585 }
586 
587 
588 //
589 //
590 //
592 
593  PerpendicularBoundPlaneBuilder planeBuilder;
594  GlobalPoint pos(0.0, 0.0, 0.0);
595  BoundPlane* SteppingPlane = planeBuilder(pos,tsos.globalDirection());
596 
597  TrajectoryStateOnSurface predTsos = propagator()->propagate(tsos, *SteppingPlane);
598  if ( predTsos.isValid() )
599  LogTrace(category_)<<"intermediateState: a intermediate state: pos: "<<predTsos.globalPosition() << "mom: " << predTsos.globalMomentum();
600 
601  return predTsos;
602 
603 }
604 
605 
606 //
607 //
608 //
610 
611  if ( hits.size() < 2 ) return;
612 
614  vector<bool> keep(hits.size(),true);
615  int i(0);
616  int j(0);
617 
618  for (MuonRecHitContainer::const_iterator ihit = hits.begin();
619  ihit != hits.end(); ++ihit ) {
620  if ( !keep[i] ) { i++; continue; };
621  j = i + 1;
622  for (MuonRecHitContainer::const_iterator ihit2 = ihit + 1;
623  ihit2 != hits.end(); ++ihit2 ) {
624  if ( !keep[j] ) { j++; continue; }
625  if ((*ihit)->geographicalId() == (*ihit2)->geographicalId() ) {
626  if ( (*ihit)->dimension() > (*ihit2)->dimension() ) {
627  keep[j] = false;
628  } else if ( (*ihit)->dimension() < (*ihit2)->dimension() ) {
629  keep[i] = false;
630  } else {
631  if ( (*ihit)->transientHits().size()>(*ihit2)->transientHits().size() ) {
632  keep[j] = false;
633  } else if ( (*ihit)->transientHits().size()<(*ihit2)->transientHits().size() ) {
634  keep[i] = false;
635  }
636  else if ( (*ihit)->degreesOfFreedom() != 0 && (*ihit2)->degreesOfFreedom() != 0) {
637  if (((*ihit)->chi2()/(*ihit)->degreesOfFreedom()) > ((*ihit2)->chi2()/(*ihit)->degreesOfFreedom())) keep[i] = false;
638  else keep[j] = false;
639  }
640  }
641  } // if same geomid
642  j++;
643  }
644  i++;
645  }
646 
647  i = 0;
648  for (MuonRecHitContainer::const_iterator ihit = hits.begin();
649  ihit != hits.end(); ++ihit ) {
650  if (keep[i] ) tmp.push_back(*ihit);
651  i++;
652  }
653 
654  hits.clear();
655  hits.swap(tmp);
656 
657  return;
658 
659 }
660 
661 
662 //
663 //
664 //
666 
668 
669  if (traj.empty()) return true;
670 
671  bool result = false;
672  for (ConstRecHitContainer::const_iterator ir = hits.begin(); ir != hits.end(); ir++ ) {
673  if ( !(*ir)->isValid() ) continue;
674  for (ConstRecHitContainer::const_iterator ir2 = ir+1; ir2 != hits.end(); ir2++ ) {
675  if ( !(*ir2)->isValid() ) continue;
676  if ( (*ir) == (*ir2) ) result = true;
677  }
678  }
679 
680  return result;
681 
682 }
683 
684 
685 //
686 // reverse a trajectory without refitting
687 // this can only be used for cosmic muons that come from above
688 //
690 
691  PropagationDirection newDir = (traj.firstMeasurement().recHit()->globalPosition().y()
692  < traj.lastMeasurement().recHit()->globalPosition().y())
694  Trajectory newTraj(traj.seed(), newDir);
695 
696  const std::vector<TrajectoryMeasurement>& meas = traj.measurements();
697 
698  for (std::vector<TrajectoryMeasurement>::const_reverse_iterator itm = meas.rbegin();
699  itm != meas.rend(); ++itm ) {
700  newTraj.push(*itm);
701  }
702  traj = newTraj;
703 
704 }
705 
706 
707 //
708 // reverse a trajectory momentum direction and then refit
709 //
711 
713  if ( !lastTSOS.isValid() ) {
714  LogTrace(category_) << "Error: last TrajectoryState invalid.";
715  }
717  std::reverse(hits.begin(), hits.end());
718 
719  LogTrace(category_) << "last tsos before flipping "<<lastTSOS;
720  utilities()->reverseDirection(lastTSOS,&*theService->magneticField());
721  LogTrace(category_) << "last tsos after flipping "<<lastTSOS;
722 
723  vector<Trajectory> refittedback = theSmoother->fit(traj.seed(),hits,lastTSOS);
724  if ( refittedback.empty() ) {
725  LogTrace(category_) <<"flipTrajectory fail. "<<endl;
726  return;
727  }
728  LogTrace(category_) <<"flipTrajectory: first "<< refittedback.front().firstMeasurement().updatedState()
729  <<"\nflipTrajectory: last "<<refittedback.front().lastMeasurement().updatedState();
730 
731  traj = refittedback.front();
732 
733  return;
734 
735 }
736 
737 
738 //
739 //
740 //
742 
743  if ( traj.direction() == anyDirection ) return;
745  Trajectory newTraj(traj.seed(), newDir);
746  const std::vector<TrajectoryMeasurement>& meas = traj.measurements();
747 
748  for (std::vector<TrajectoryMeasurement>::const_iterator itm = meas.begin(); itm != meas.end(); ++itm) {
749  newTraj.push(*itm);
750  }
751 
752  while (!traj.empty()) {
753  traj.pop();
754  }
755 
756  traj = newTraj;
757 
758 }
759 
760 
761 //
762 // guess the direction by normalized chi2
763 //
765 
767 
769 
771 
772  if ( !firstTSOS.isValid() || !lastTSOS.isValid() ) return;
773 
774  LogTrace(category_) <<"Two ends of the traj "<<firstTSOS.globalPosition()
775  <<", "<<lastTSOS.globalPosition();
776 
777  LogTrace(category_) <<"Their mom: "<<firstTSOS.globalMomentum()
778  <<", "<<lastTSOS.globalMomentum();
779 
780  LogTrace(category_) <<"Their mom eta: "<<firstTSOS.globalMomentum().eta()
781  <<", "<<lastTSOS.globalMomentum().eta();
782 
783  // momentum eta can be used to estimate direction
784  // the beam-halo muon seems enter with a larger |eta|
785 
786  if ( fabs(firstTSOS.globalMomentum().eta()) > fabs(lastTSOS.globalMomentum().eta()) ) {
787 
788  vector<Trajectory> refitted = theSmoother->trajectories(traj.seed(),hits,firstTSOS);
789  if ( !refitted.empty() ) traj = refitted.front();
790 
791  } else {
792  std::reverse(hits.begin(), hits.end());
793  utilities()->reverseDirection(lastTSOS,&*theService->magneticField());
794  vector<Trajectory> refittedback = theSmoother->trajectories(traj.seed(),hits,lastTSOS);
795  if ( !refittedback.empty() ) traj = refittedback.front();
796 
797  }
798 
799  return;
800 
801 }
802 
803 
804 //
805 // get direction from timing information of rechits and segments
806 //
808 
810  LogTrace(category_) << "getDirectionByTime"<<endl;
811  for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator ir = hits.begin(); ir != hits.end(); ir++ ) {
812  if ( !(*ir)->isValid() ) {
813  LogTrace(category_) << "invalid RecHit"<<endl;
814  continue;
815  }
816 
817  const GlobalPoint& pos = (*ir)->globalPosition();
819  << "pos" << pos
820  << "radius " << pos.perp()
821  << " dim " << (*ir)->dimension()
822  << " det " << (*ir)->det()->geographicalId().det()
823  << " sub det " << (*ir)->det()->subDetector()<<endl;
824 
825  if ((*ir)->det()->geographicalId().det() == 2 && (*ir)->det()->subDetector() == 6) {
826 // const CSCRecHit2D* iCSC = dynamic_cast<const CSCRecHit2D*>(&**ir);
827 // if (iCSC) LogTrace(category_)<<"csc from cast tpeak "<<iCSC->tpeak();
828  CSCRecHit2DCollection::range thisrange = cschits_->get(CSCDetId((*ir)->geographicalId()));
829  for (CSCRecHit2DCollection::const_iterator rechit = thisrange.first; rechit!=thisrange.second;++rechit) {
830  if ((*rechit).isValid()) LogTrace(category_)<<"csc from collection tpeak "<<(*rechit).tpeak();
831  }
832  }
833  if ((*ir)->det()->geographicalId().det() == 2 && (*ir)->det()->subDetector() == 7) {
834 // const DTRecHit1D* iDT = dynamic_cast<const DTRecHit1D*>(&**ir);
835 // if (iDT) LogTrace(category_)<<"dt digitime "<<iDT->digiTime();
836  DTRecHitCollection::range thisrange = dthits_->get(DTLayerId((*ir)->geographicalId()));
837  for (DTRecHitCollection::const_iterator rechit = thisrange.first; rechit!=thisrange.second;++rechit) {
838  if ((*rechit).isValid()) LogTrace(category_)<<"dt from collection digitime "<<(*rechit).digiTime();
839  }
840  }
841  }
842 
843  return;
844 
845 }
846 
847 
848 //
849 //
850 //
851 std::vector<TrajectoryMeasurement>
853  const TrajectoryStateOnSurface& tsos,
854  const Propagator* propagator,
855  const MeasurementEstimator* estimator) {
856 
857  std::vector<TrajectoryMeasurement> result;
858  std::vector<TrajectoryMeasurement> measurements;
859 
860  if ( layer->hasGroups() ) {
861  std::vector<TrajectoryMeasurementGroup> measurementGroups =
862  theLayerMeasurements->groupedMeasurements(layer, tsos, *propagator, *estimator);
863 
864  for (std::vector<TrajectoryMeasurementGroup>::const_iterator tmGroupItr = measurementGroups.begin();
865  tmGroupItr != measurementGroups.end(); ++tmGroupItr) {
866 
867  measurements = tmGroupItr->measurements();
868  const TrajectoryMeasurement* bestMeasurement
869  = theBestMeasurementFinder->findBestMeasurement(measurements, propagator);
870 
871  if (bestMeasurement) result.push_back(*bestMeasurement);
872  }
873  }
874  else {
875  measurements = theLayerMeasurements->measurements(layer, tsos, *propagator, *estimator);
876  const TrajectoryMeasurement* bestMeasurement
877  = theBestMeasurementFinder->findBestMeasurement(measurements, propagator);
878 
879  if (bestMeasurement) result.push_back(*bestMeasurement);
880  }
881  measurements.clear();
882 
883  return result;
884 
885 }
886 
887 
888 //
889 //
890 //
892  int& dtChambers,
893  int& cscChambers,
894  int& rpcChambers,
895  int& totalChambers) {
896 
897  if (layer->subDetector()==GeomDetEnumerators::DT) dtChambers++;
898  else if (layer->subDetector()==GeomDetEnumerators::CSC) cscChambers++;
899  else if (layer->subDetector()==GeomDetEnumerators::RPCBarrel || layer->subDetector()==GeomDetEnumerators::RPCEndcap) rpcChambers++;
900  totalChambers++;
901 
902 }
903 
904 
905 //
906 //
907 //
909 
910  if ( (dtseg == 0) || (!dtseg->hasPhi()) ) return 0;
911  // timing information
912  double result = 0;
913  if ( dtseg->phiSegment() == 0 ) return 0;
914  int phiHits = dtseg->phiSegment()->specificRecHits().size();
915  LogTrace(category_) << "phiHits " << phiHits;
916  if ( phiHits > 5 ) {
917  if(dtseg->phiSegment()->ist0Valid()) result = dtseg->phiSegment()->t0();
918  if (dtseg->phiSegment()->ist0Valid()){
919  LogTrace(category_) << " Phi t0: " << dtseg->phiSegment()->t0() << " hits: " << phiHits;
920  } else {
921  LogTrace(category_) << " Phi t0 is invalid: " << dtseg->phiSegment()->t0() << " hits: " << phiHits;
922  }
923  }
924 
925  return result;
926 
927 }
928 
929 
930 //
931 //
932 //
934  const DTRecSegment4D* dtseg2) const {
935 
936  LogTrace(category_) << "comparing dtseg: " << dtseg1 << " " << dtseg2 << endl;
937  if (dtseg1 == dtseg2 || t0(dtseg1) == t0(dtseg2)) return anyDirection;
938 
940  ( t0(dtseg1) < t0(dtseg2) ) ? alongMomentum : oppositeToMomentum;
941 
942  return result;
943 
944 }
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:246
void reverseTrajectoryPropagationDirection(Trajectory &) const
reverse the propagation direction of a trajectory
T getParameter(std::string const &) const
const Propagator * propagator() const
int i
Definition: DBlmapReader.cc:9
double t0(const DTRecSegment4D *deseg) const
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
TrajectoryStateOnSurface const & predictedState() const
const MeasurementEstimator * estimator() const
accasso at the propagator
T perp() const
Definition: PV3DBase.h:72
ConstRecHitPointer const & recHit() const
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:277
std::vector< const DetLayer * > compatibleEndcapLayers(const FreeTrajectoryState &fts, PropagationDirection timeDirection) const
void estimateDirection(Trajectory &) const
check the direction of trajectory by checking eta spread
void makeFirstTime()
reset the theFirstTSOSFlag
TrajectoryStateOnSurface intermediateState(const TrajectoryStateOnSurface &) const
edm::Handle< CSCRecHit2DCollection > cschits_
const DTChamberRecSegment2D * phiSegment() const
The superPhi segment: 0 if no phi projection available.
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
virtual SubDetector subDetector() const =0
The type of detector (PixelBarrel, PixelEndcap, TIB, TOB, TID, TEC, CSC, DT, RPCBarrel, RPCEndcap)
MuonTrajectoryUpdator * backwardUpdator() const
GlobalPoint globalPosition() const
std::vector< Trajectory * > trajectories(const TrajectorySeed &)
build trajectories from seed
MuonCandidate::TrajectoryContainer TrajectoryContainer
edm::Handle< DTRecHitCollection > dthits_
const Propagator * propagatorOpposite() const
PropagationDirection
MuonDetLayerMeasurements * theLayerMeasurements
std::vector< TrajectoryMeasurementGroup > groupedMeasurements(const DetLayer *layer, const TrajectoryStateOnSurface &startingState, const Propagator &prop, const MeasurementEstimator &est, const edm::Event &iEvent)
std::vector< const DetLayer * > compatibleLayers(const FreeTrajectoryState &fts, PropagationDirection timeDirection) const
ConstRecHitContainer recHits(bool splitting=false) const
Definition: Trajectory.cc:67
PropagationDirection const & direction() const
Definition: Trajectory.cc:196
DataContainer const & measurements() const
Definition: Trajectory.h:215
const int keep
DirectMuonNavigation * navigation() const
static const int CSC
Definition: MuonSubdetId.h:13
T mag() const
Definition: PV3DBase.h:67
void flipTrajectory(Trajectory &) const
flip a trajectory with refit (the momentum direction is opposite)
MuonTrajectoryUpdator * updator() const
MuonRecHitContainer recHits(const DetLayer *layer, const edm::Event &iEvent)
returns the rechits which are on the layer
void reverseTrajectory(Trajectory &) const
reverse a trajectory without refit (out the measurements order changed)
MuonBestMeasurementFinder * theBestMeasurementFinder
void setFitDirection(NavigationDirection fitDirection)
set fit direction
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:193
FreeTrajectoryState const * freeState(bool withErrors=true) const
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
void incrementChamberCounters(const DetLayer *layer, int &dtChambers, int &cscChambers, int &rpcChambers, int &totalChambers)
bool hasPhi() const
Does it have the Phi projection?
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
MeasurementContainer measurements(const DetLayer *layer, const GeomDet *det, const TrajectoryStateOnSurface &stateOnDet, const MeasurementEstimator &est, const edm::Event &iEvent)
GlobalVector momentum() const
#define LogTrace(id)
std::vector< DTRecHit1D > specificRecHits() const
Access to specific components.
bool ist0Valid() const
virtual TrajectoryContainer trajectories(const Trajectory &traj) const
virtual TrajectoryStateOnSurface propagate(const FreeTrajectoryState &, const Surface &) const
Definition: Propagator.cc:12
void getDirectionByTime(Trajectory &) const
check the direction of trajectory by checking the timing
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:18
GlobalPoint position() const
PropagationDirection checkDirectionByT0(const DTRecSegment4D *, const DTRecSegment4D *) const
bool isValid() const
Definition: Trajectory.h:271
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:206
PTrajectoryStateOnDet const & startingState() const
#define debug
Definition: HDRShower.cc:19
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
void pop()
Definition: Trajectory.cc:17
std::vector< Trajectory > fit(const Trajectory &) const
virtual bool hasGroups() const =0
CosmicMuonUtilities * utilities() const
virtual ~CosmicMuonTrajectoryBuilder()
Destructor.
T eta() const
Definition: PV3DBase.h:76
void selectHits(MuonTransientTrackingRecHit::MuonRecHitContainer &) const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
void setEvent(const edm::Event &)
set event
GlobalVector globalMomentum() const
if(dp >Float(M_PI)) dp-
CosmicMuonTrajectoryBuilder(const edm::ParameterSet &, const MuonServiceProxy *service, edm::ConsumesCollector &iC)
Constructor.
const Propagator * propagatorAlong() const
TrajectoryMeasurement * findBestMeasurement(std::vector< TrajectoryMeasurement > &measC, const Propagator *propagator)
return the Tm with the best chi2: no cut applied.
MuonTransientTrackingRecHit::MuonRecHitContainer MuonRecHitContainer
std::vector< TrajectoryMeasurement > findBestMeasurements(const DetLayer *, const TrajectoryStateOnSurface &, const Propagator *, const MeasurementEstimator *)
virtual std::pair< bool, TrajectoryStateOnSurface > update(const TrajectoryMeasurement *measurement, Trajectory &trajectory, const Propagator *propagator)
update the Trajectory with the TrajectoryMeasurement
bool selfDuplicate(const Trajectory &) const
check if the trajectory iterates the same hit more than once
static const int DT
Definition: MuonSubdetId.h:12
TrajectoryStateOnSurface const & updatedState() const
dbl *** dir
Definition: mlp_gen.cc:35
MuonTransientTrackingRecHit::MuonRecHitContainer unusedHits(const DetLayer *, const TrajectoryMeasurement &) const
double t0() const
Get the segment t0 (if recomputed, 0 is returned otherwise)
void reverseDirection(TrajectoryStateOnSurface &, const MagneticField *) const
void build(const TrajectoryStateOnSurface &, const NavigationDirection &, Trajectory &)
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
std::vector< MuonRecHitPointer > MuonRecHitContainer
double chiSquared() const
Definition: Trajectory.h:254
GlobalVector globalDirection() const
virtual void setEvent(const edm::Event &)
pass the Event to the algo at each event
T dot(const Basic3DVector &rh) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.