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 
667  TransientTrackingRecHit::ConstRecHitContainer const & hits = traj.recHits();
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  /* does not work in gcc4.8?)
697  std::vector<TrajectoryMeasurement> & meas = traj.measurements();
698  for (auto itm = meas.rbegin(); itm != meas.rend(); ++itm ) {
699  newTraj.push(std::move(*itm));
700  }
701  traj = std::move(newTraj);
702  */
703 
704  std::vector<TrajectoryMeasurement> const & meas = traj.measurements();
705  for (auto itm = meas.rbegin(); itm != meas.rend(); ++itm ) {
706  newTraj.push(*itm);
707  }
708  traj = newTraj;
709 
710 
711 }
712 
713 
714 //
715 // reverse a trajectory momentum direction and then refit
716 //
718 
720  if ( !lastTSOS.isValid() ) {
721  LogTrace(category_) << "Error: last TrajectoryState invalid.";
722  }
723  TransientTrackingRecHit::ConstRecHitContainer hits = traj.recHits();
724  std::reverse(hits.begin(), hits.end());
725 
726  LogTrace(category_) << "last tsos before flipping "<<lastTSOS;
727  utilities()->reverseDirection(lastTSOS,&*theService->magneticField());
728  LogTrace(category_) << "last tsos after flipping "<<lastTSOS;
729 
730  vector<Trajectory> refittedback = theSmoother->fit(traj.seed(),hits,lastTSOS);
731  if ( refittedback.empty() ) {
732  LogTrace(category_) <<"flipTrajectory fail. "<<endl;
733  return;
734  }
735  LogTrace(category_) <<"flipTrajectory: first "<< refittedback.front().firstMeasurement().updatedState()
736  <<"\nflipTrajectory: last "<<refittedback.front().lastMeasurement().updatedState();
737 
738  traj = refittedback.front();
739 
740  return;
741 
742 }
743 
744 
745 //
746 //
747 //
749 
750  if ( traj.direction() == anyDirection ) return;
752  Trajectory newTraj(traj.seed(), newDir);
753  const std::vector<TrajectoryMeasurement>& meas = traj.measurements();
754 
755  for (std::vector<TrajectoryMeasurement>::const_iterator itm = meas.begin(); itm != meas.end(); ++itm) {
756  newTraj.push(*itm);
757  }
758 
759  while (!traj.empty()) {
760  traj.pop();
761  }
762 
763  traj = newTraj;
764 
765 }
766 
767 
768 //
769 // guess the direction by normalized chi2
770 //
772 
773  TransientTrackingRecHit::ConstRecHitContainer hits = traj.recHits();
774 
776 
778 
779  if ( !firstTSOS.isValid() || !lastTSOS.isValid() ) return;
780 
781  LogTrace(category_) <<"Two ends of the traj "<<firstTSOS.globalPosition()
782  <<", "<<lastTSOS.globalPosition();
783 
784  LogTrace(category_) <<"Their mom: "<<firstTSOS.globalMomentum()
785  <<", "<<lastTSOS.globalMomentum();
786 
787  LogTrace(category_) <<"Their mom eta: "<<firstTSOS.globalMomentum().eta()
788  <<", "<<lastTSOS.globalMomentum().eta();
789 
790  // momentum eta can be used to estimate direction
791  // the beam-halo muon seems enter with a larger |eta|
792 
793  if ( fabs(firstTSOS.globalMomentum().eta()) > fabs(lastTSOS.globalMomentum().eta()) ) {
794 
795  vector<Trajectory> refitted = theSmoother->trajectories(traj.seed(),hits,firstTSOS);
796  if ( !refitted.empty() ) traj = refitted.front();
797 
798  } else {
799  std::reverse(hits.begin(), hits.end());
800  utilities()->reverseDirection(lastTSOS,&*theService->magneticField());
801  vector<Trajectory> refittedback = theSmoother->trajectories(traj.seed(),hits,lastTSOS);
802  if ( !refittedback.empty() ) traj = refittedback.front();
803 
804  }
805 
806  return;
807 
808 }
809 
810 
811 //
812 // get direction from timing information of rechits and segments
813 //
815 
816  TransientTrackingRecHit::ConstRecHitContainer hits = traj.recHits();
817  LogTrace(category_) << "getDirectionByTime"<<endl;
818  for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator ir = hits.begin(); ir != hits.end(); ir++ ) {
819  if ( !(*ir)->isValid() ) {
820  LogTrace(category_) << "invalid RecHit"<<endl;
821  continue;
822  }
823 
824  const GlobalPoint& pos = (*ir)->globalPosition();
826  << "pos" << pos
827  << "radius " << pos.perp()
828  << " dim " << (*ir)->dimension()
829  << " det " << (*ir)->det()->geographicalId().det()
830  << " sub det " << (*ir)->det()->subDetector()<<endl;
831 
832  if ((*ir)->det()->geographicalId().det() == 2 && (*ir)->det()->subDetector() == 6) {
833 // const CSCRecHit2D* iCSC = dynamic_cast<const CSCRecHit2D*>(&**ir);
834 // if (iCSC) LogTrace(category_)<<"csc from cast tpeak "<<iCSC->tpeak();
835  CSCRecHit2DCollection::range thisrange = cschits_->get(CSCDetId((*ir)->geographicalId()));
836  for (CSCRecHit2DCollection::const_iterator rechit = thisrange.first; rechit!=thisrange.second;++rechit) {
837  if ((*rechit).isValid()) LogTrace(category_)<<"csc from collection tpeak "<<(*rechit).tpeak();
838  }
839  }
840  if ((*ir)->det()->geographicalId().det() == 2 && (*ir)->det()->subDetector() == 7) {
841 // const DTRecHit1D* iDT = dynamic_cast<const DTRecHit1D*>(&**ir);
842 // if (iDT) LogTrace(category_)<<"dt digitime "<<iDT->digiTime();
843  DTRecHitCollection::range thisrange = dthits_->get(DTLayerId((*ir)->geographicalId()));
844  for (DTRecHitCollection::const_iterator rechit = thisrange.first; rechit!=thisrange.second;++rechit) {
845  if ((*rechit).isValid()) LogTrace(category_)<<"dt from collection digitime "<<(*rechit).digiTime();
846  }
847  }
848  }
849 
850  return;
851 
852 }
853 
854 
855 //
856 //
857 //
858 std::vector<TrajectoryMeasurement>
860  const TrajectoryStateOnSurface& tsos,
861  const Propagator* propagator,
863 
864  std::vector<TrajectoryMeasurement> result;
865  std::vector<TrajectoryMeasurement> measurements;
866 
867  if ( layer->hasGroups() ) {
868  std::vector<TrajectoryMeasurementGroup> measurementGroups =
869  theLayerMeasurements->groupedMeasurements(layer, tsos, *propagator, *estimator);
870 
871  for (std::vector<TrajectoryMeasurementGroup>::const_iterator tmGroupItr = measurementGroups.begin();
872  tmGroupItr != measurementGroups.end(); ++tmGroupItr) {
873 
874  measurements = tmGroupItr->measurements();
875  const TrajectoryMeasurement* bestMeasurement
876  = theBestMeasurementFinder->findBestMeasurement(measurements, propagator);
877 
878  if (bestMeasurement) result.push_back(*bestMeasurement);
879  }
880  }
881  else {
882  measurements = theLayerMeasurements->measurements(layer, tsos, *propagator, *estimator);
883  const TrajectoryMeasurement* bestMeasurement
884  = theBestMeasurementFinder->findBestMeasurement(measurements, propagator);
885 
886  if (bestMeasurement) result.push_back(*bestMeasurement);
887  }
888  measurements.clear();
889 
890  return result;
891 
892 }
893 
894 
895 //
896 //
897 //
899  int& dtChambers,
900  int& cscChambers,
901  int& rpcChambers,
902  int& totalChambers) {
903 
904  if (layer->subDetector()==GeomDetEnumerators::DT) dtChambers++;
905  else if (layer->subDetector()==GeomDetEnumerators::CSC) cscChambers++;
906  else if (layer->subDetector()==GeomDetEnumerators::RPCBarrel || layer->subDetector()==GeomDetEnumerators::RPCEndcap) rpcChambers++;
907  totalChambers++;
908 
909 }
910 
911 
912 //
913 //
914 //
916 
917  if ( (dtseg == 0) || (!dtseg->hasPhi()) ) return 0;
918  // timing information
919  double result = 0;
920  if ( dtseg->phiSegment() == 0 ) return 0;
921  int phiHits = dtseg->phiSegment()->specificRecHits().size();
922  LogTrace(category_) << "phiHits " << phiHits;
923  if ( phiHits > 5 ) {
924  if(dtseg->phiSegment()->ist0Valid()) result = dtseg->phiSegment()->t0();
925  if (dtseg->phiSegment()->ist0Valid()){
926  LogTrace(category_) << " Phi t0: " << dtseg->phiSegment()->t0() << " hits: " << phiHits;
927  } else {
928  LogTrace(category_) << " Phi t0 is invalid: " << dtseg->phiSegment()->t0() << " hits: " << phiHits;
929  }
930  }
931 
932  return result;
933 
934 }
935 
936 
937 //
938 //
939 //
941  const DTRecSegment4D* dtseg2) const {
942 
943  LogTrace(category_) << "comparing dtseg: " << dtseg1 << " " << dtseg2 << endl;
944  if (dtseg1 == dtseg2 || t0(dtseg1) == t0(dtseg2)) return anyDirection;
945 
947  ( t0(dtseg1) < t0(dtseg2) ) ? alongMomentum : oppositeToMomentum;
948 
949  return result;
950 
951 }
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
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
if(conf.exists("allCellsPositionCalc"))
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.