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