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