CMS 3D CMS Logo

TrackTransformerForCosmicMuons.cc
Go to the documentation of this file.
2 
6 
9 
13 
20 
24 
25 
30 
31 
32 using namespace std;
33 using namespace edm;
34 
37 
38  theTrackerRecHitBuilderName = parameterSet.getParameter<string>("TrackerRecHitBuilder");
39  theMuonRecHitBuilderName = parameterSet.getParameter<string>("MuonRecHitBuilder");
40 
41  theRPCInTheFit = parameterSet.getParameter<bool>("RefitRPCHits");
42 
43  theCacheId_TC = theCacheId_GTG = theCacheId_MG = theCacheId_TRH = 0;
44 }
45 
48 
49 
51 
52  const std::string metname = "Reco|TrackingTools|TrackTransformer";
53 
54  setup.get<TrajectoryFitter::Record>().get("KFFitterForRefitInsideOut",theFitterIO);
55  setup.get<TrajectoryFitter::Record>().get("KFSmootherForRefitInsideOut",theSmootherIO);
56  setup.get<TrajectoryFitter::Record>().get("KFFitterForRefitOutsideIn",theFitterOI);
57  setup.get<TrajectoryFitter::Record>().get("KFSmootherForRefitOutsideIn",theSmootherOI);
58 
59  unsigned long long newCacheId_TC = setup.get<TrackingComponentsRecord>().cacheIdentifier();
60 
61  if ( newCacheId_TC != theCacheId_TC ){
62  LogTrace(metname) << "Tracking Component changed!";
63  theCacheId_TC = newCacheId_TC;
64  setup.get<TrackingComponentsRecord>().get("SmartPropagatorRK",thePropagatorIO);
65  setup.get<TrackingComponentsRecord>().get("SmartPropagatorRKOpposite",thePropagatorOI);
66  }
67 
68  // Global Tracking Geometry
69  unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
70  if ( newCacheId_GTG != theCacheId_GTG ) {
71  LogTrace(metname) << "GlobalTrackingGeometry changed!";
72  theCacheId_GTG = newCacheId_GTG;
73  setup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry);
74  }
75 
76  // Magfield Field
77  unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
78  if ( newCacheId_MG != theCacheId_MG ) {
79  LogTrace(metname) << "Magnetic Field changed!";
80  theCacheId_MG = newCacheId_MG;
81  setup.get<IdealMagneticFieldRecord>().get(theMGField);
82  }
83 
84  // Transient Rechit Builders
85  unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
86  if ( newCacheId_TRH != theCacheId_TRH ) {
87  theCacheId_TRH = newCacheId_TRH;
88  LogTrace(metname) << "TransientRecHitRecord changed!";
89  setup.get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
90  setup.get<TransientRecHitRecord>().get(theMuonRecHitBuilderName,theMuonRecHitBuilder);
91  }
92 }
93 
94 
97 
100 
101  bool printout = false;
102 
103  bool quad1 = false;
104  bool quad2 = false;
105  bool quad3 = false;
106  bool quad4 = false;
107 
108  for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit)
109  if((*hit)->isValid())
110  if ( (*hit)->geographicalId().det() == DetId::Muon ){
111  if( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit){
112  LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
113  continue;
114  }
115  staHits.push_back(theMuonRecHitBuilder->build(&**hit));
116  DetId hitId = staHits.back()->geographicalId();
117  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
118  float gpy=glbpoint.y();
119  float gpz=glbpoint.z();
120 // if (gpy != 0 && gpz !=0) slopeSum += gpy / gpz;
121 
122  if (gpy > 0 && gpz > 0) quad1 = true;
123  else if (gpy > 0 && gpz < 0) quad2 = true;
124  else if (gpy < 0 && gpz < 0) quad3 = true;
125  else if (gpy < 0 && gpz > 0) quad4 = true;
126  else return tkHits;
127  }
128 
129  if(staHits.empty()) return staHits;
130 
131  if (quad1 && quad2) return tkHits;
132  if (quad1 && quad3) return tkHits;
133  if (quad1 && quad4) return tkHits;
134  if (quad2 && quad3) return tkHits;
135  if (quad2 && quad4) return tkHits;
136  if (quad3 && quad4) return tkHits;
137 
138 
139  bool doReverse = staHits.front()->globalPosition().y()>0 ? true : false;
140 
141  if (quad1) doReverse = SlopeSum(staHits);
142  if (quad2) doReverse = SlopeSum(staHits);
143  if (quad3) doReverse = SlopeSum(staHits);
144  if (quad4) doReverse = SlopeSum(staHits);
145  if(doReverse){
146  reverse(staHits.begin(),staHits.end());
147  }
148 
149  copy(staHits.begin(),staHits.end(),back_inserter(tkHits));
150 
151 
153 // if ( quad2 && slopeSum > 0) printout = true;
156 // swap = printout;
157 
158  printout = quad1;
159 
160  if (printout) for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator hit = tkHits.begin();
161  hit !=tkHits.end(); ++hit){
162 
163  DetId hitId = (*hit)->geographicalId();
164  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
165 
166 
167  if(hitId.det() == DetId::Muon) {
168  if(hitId.subdetId() == MuonSubdetId::DT)
169  LogTrace("TrackFitters") << glbpoint << " I am DT " << DTWireId(hitId);
170 // std::cout<< glbpoint << " I am DT " << DTWireId(hitId)<<std::endl;
171  else if (hitId.subdetId() == MuonSubdetId::CSC )
172  LogTrace("TrackFitters") << glbpoint << " I am CSC " << CSCDetId(hitId);
173 // std::cout<< glbpoint << " I am CSC " << CSCDetId(hitId)<<std::endl;
174  else if (hitId.subdetId() == MuonSubdetId::RPC )
175  LogTrace("TrackFitters") << glbpoint << " I am RPC " << RPCDetId(hitId);
176  else
177  LogTrace("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
178  }
179  }
180  return tkHits;
181 }
182 
183 
186  if(quad ==1) {if (sumy < 0) return theFitterOI; else return theFitterIO;}
187  if(quad ==2) {if (sumy < 0) return theFitterOI; else return theFitterIO;}
188  if(quad ==3) {if (sumy > 0) return theFitterOI; else return theFitterIO;}
189  if(quad ==4) {if (sumy > 0) return theFitterOI; else return theFitterIO;}
190 
191  if(up) return theFitterOI;
192  else return theFitterIO;
193 }
194 
197  if(quad ==1){ if (sumy < 0) return theSmootherOI; else return theSmootherIO;}
198  if(quad ==2){ if (sumy < 0) return theSmootherOI; else return theSmootherIO;}
199  if(quad ==3){ if (sumy > 0) return theSmootherOI; else return theSmootherIO;}
200  if(quad ==4){ if (sumy > 0) return theSmootherOI; else return theSmootherIO;}
201  if(up) return theSmootherOI;
202  else return theSmootherIO;
203 }
204 
206  if(quad ==1) {if (sumy > 0) return thePropagatorOI; else return thePropagatorIO;}
207  if(quad ==2) {if (sumy > 0) return thePropagatorOI; else return thePropagatorIO;}
208  if(quad ==3) {if (sumy < 0) return thePropagatorOI; else return thePropagatorIO;}
209  if(quad ==4) {if (sumy < 0) return thePropagatorOI; else return thePropagatorIO;}
210  if(up) return thePropagatorIO;
211  else return thePropagatorOI;
212 }
213 
214 
215 
217 vector<Trajectory> TrackTransformerForCosmicMuons::transform(const reco::Track& tr) const {
218 
219  const std::string metname = "Reco|TrackingTools|TrackTransformer";
220 
221  reco::TransientTrack track(tr,magneticField(),trackingGeometry());
222 
223  // Build the transient Rechits
224  TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit;// = getTransientRecHits(track);
226 
227 
228  bool quad1 = false;
229  bool quad2 = false;
230  bool quad3 = false;
231  bool quad4 = false;
232  int quadrant = 0;
233 
234  for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit)
235  if((*hit)->isValid())
236  if ( (*hit)->geographicalId().det() == DetId::Muon ){
237  if( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit){
238  LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
239  continue;
240  }
241  staHits.push_back(theMuonRecHitBuilder->build(&**hit));
242  DetId hitId = staHits.back()->geographicalId();
243  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
244  float gpy=glbpoint.y();
245  float gpz=glbpoint.z();
246 // if (gpy != 0 && gpz !=0) slopeSum += gpy / gpz;
247 
248  if (gpy > 0 && gpz > 0) {quad1 = true; quadrant = 1;}
249  else if (gpy > 0 && gpz < 0){quad2 = true; quadrant = 2;}
250  else if (gpy < 0 && gpz < 0){quad3 = true; quadrant = 3;}
251  else if (gpy < 0 && gpz > 0){quad4 = true; quadrant = 4;}
252  else return vector<Trajectory>();
253  }
254 
255 
256  if(staHits.empty()) return vector<Trajectory>();
257 
258  if (quad1 && quad2) return vector<Trajectory>();
259  if (quad1 && quad3) return vector<Trajectory>();
260  if (quad1 && quad4) return vector<Trajectory>();
261  if (quad2 && quad3) return vector<Trajectory>();
262  if (quad2 && quad4) return vector<Trajectory>();
263  if (quad3 && quad4) return vector<Trajectory>();
264 
265  bool doReverse = false;
266 
267  if (quad1) doReverse = SlopeSum(staHits);
268  if (quad2) doReverse = SlopeSum(staHits);
269  if (quad3) doReverse = SlopeSum(staHits);
270  if (quad4) doReverse = SlopeSum(staHits);
271 
272 
273  if(doReverse){
274  reverse(staHits.begin(),staHits.end());
275  }
276 
277  copy(staHits.begin(),staHits.end(),back_inserter(recHitsForReFit));
278 
284 
285 
286  if(recHitsForReFit.size() < 2) return vector<Trajectory>();
287 
288  bool up = recHitsForReFit.back()->globalPosition().y()>0 ? true : false;
289  LogTrace(metname) << "Up ? " << up;
290 
291  float sumdy = SumDy(recHitsForReFit);
292 
293 
294  PropagationDirection propagationDirection = doReverse ? oppositeToMomentum : alongMomentum;
295  TrajectoryStateOnSurface firstTSOS = doReverse ? track.outermostMeasurementState() : track.innermostMeasurementState();
296  unsigned int innerId = doReverse ? track.track().outerDetId() : track.track().innerDetId();
297 
298  LogTrace(metname) << "Prop Dir: " << propagationDirection << " FirstId " << innerId << " firstTSOS " << firstTSOS;
299 
301 
302 
303  if(recHitsForReFit.front()->geographicalId() != DetId(innerId)){
304  LogTrace(metname)<<"Propagation occurring"<<endl;
305  firstTSOS = propagator(up, quadrant, sumdy)->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
306  LogTrace(metname)<<"Final destination: " << recHitsForReFit.front()->det()->surface().position() << endl;
307  if(!firstTSOS.isValid()){
308  std::cout<<"Propagation error! Dumping RecHits..."<<std::endl;
309 
310  for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator hit = recHitsForReFit.begin();
311  hit !=recHitsForReFit.end(); ++hit){
312 
313  DetId hitId = (*hit)->geographicalId();
314  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
315  if(hitId.subdetId() == MuonSubdetId::DT)
316  std::cout<< glbpoint << " I am DT " << DTWireId(hitId)<<std::endl;
317  else if (hitId.subdetId() == MuonSubdetId::CSC )
318  std::cout<< glbpoint << " I am CSC " << CSCDetId(hitId)<<std::endl;
319  }
320 
321 
322  LogTrace(metname)<<"Propagation error!"<<endl;
323  return vector<Trajectory>();
324  }
325  }
326 
327 
328  vector<Trajectory> && trajectories = fitter(up, quadrant, sumdy)->fit(seed,recHitsForReFit,firstTSOS);
329 
330  if(trajectories.empty()){
331  LogTrace(metname)<<"No Track refitted!"<<endl;
332  return vector<Trajectory>();
333  }
334 
335  Trajectory const & trajectoryBW = trajectories.front();
336 
337  vector<Trajectory> trajectoriesSM = smoother(up, quadrant, sumdy)->trajectories(trajectoryBW);
338 
339  if(trajectoriesSM.empty()){
340  LogTrace(metname)<<"No Track smoothed!"<<endl;
341  }
342 
343  return trajectoriesSM;
344 
345 }
346 
347 
348 
351 
352  bool retval = false;
353  float y1 = 0 ;
354  float z1 = 0 ;
355 
356  bool first = true;
357 
358  std::vector<float> py;
359  std::vector<float> pz;
360 // int quadrant = -1;
361 
362  float sumdy = 0;
363  float sumdz = 0;
364 
365  for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator hit = tkHits.begin();
366  hit !=tkHits.end(); ++hit){
367 
368  DetId hitId = (*hit)->geographicalId();
369  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
370  if ( hitId.det() != DetId::Muon || hitId.subdetId() == 3 )continue;
371 
372  float y2 = glbpoint.y();
373  float z2 = glbpoint.z();
374  float dslope = 0;
375  float dy = y2 - y1;
376  float dz = z2 - z1;
377 
378 // if (y2 > 0 && z2 > 0) quadrant = 1;
379 // else if (y2 > 0 && z2 < 0) quadrant = 2;
380 // else if (y2 < 0 && z2 < 0) quadrant = 3;
381 // else if (y2 < 0 && z2 > 0) quadrant = 4;
382 
383 
384  if (fabs(dz) > 1e-3) dslope = dy / dz;
385  if ( !first) {
386  retval+=dslope;
387  sumdy +=dy;
388  sumdz +=dz;
389  }
390  first = false;
391  py.push_back(y1);
392  pz.push_back(z1);
393  y1 = y2;
394  z1 = z2;
395  }
396 // std::cout<<"quadrant "<<quadrant;
397 // std::cout<<"\tsum dy = "<<sumdy;
398 // std::cout<<"\tsum dz = "<<sumdz;
399 // std::cout<<std::endl;
400 
401 
402  if ( sumdz < 0) retval = true;
403 
404  return retval;
405 
406 }
407 
408 
411 
412  bool retval = false;
413  float y1 = 0 ;
414  float z1 = 0 ;
415 
416  bool first = true;
417 
418  std::vector<float> py;
419  std::vector<float> pz;
420 // int quadrant = -1;
421 
422  float sumdy = 0;
423  float sumdz = 0;
424 
425 
426 
427  for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator hit = tkHits.begin();
428  hit !=tkHits.end(); ++hit){
429 
430  DetId hitId = (*hit)->geographicalId();
431  GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
432  if ( hitId.det() != DetId::Muon || hitId.subdetId() == 3 )continue;
433 
434  float y2 = glbpoint.y();
435  float z2 = glbpoint.z();
436  float dslope = 0;
437  float dy = y2 - y1;
438  float dz = z2 - z1;
439 
440 // if (y2 > 0 && z2 > 0) quadrant = 1;
441 // else if (y2 > 0 && z2 < 0) quadrant = 2;
442 // else if (y2 < 0 && z2 < 0) quadrant = 3;
443 // else if (y2 < 0 && z2 > 0) quadrant = 4;
444 
445 
446  if (fabs(dz) > 1e-3) dslope = dy / dz;
447  if ( !first) {
448  retval+=dslope;
449  sumdy +=dy;
450  sumdz +=dz;
451  }
452  first = false;
453  py.push_back(y1);
454  pz.push_back(z1);
455  y1 = y2;
456  z1 = z2;
457  }
458 // std::cout<<"quadrant "<<quadrant;
459 // std::cout<<"\tsum dy = "<<sumdy;
460 // std::cout<<"\tsum dz = "<<sumdz;
461 // std::cout<<std::endl;
462 
463  return sumdy;
464 }
465 
Definition: BitonicSort.h:8
T getParameter(std::string const &) const
std::vector< Trajectory > transform(const reco::Track &) const override
Convert a reco::Track into Trajectory.
edm::ESHandle< Propagator > propagator(bool, int, float) const
void setServices(const edm::EventSetup &) override
set the services needed by the TrackTransformer
def copy(args, dbName)
const std::string metname
T y() const
Definition: PV3DBase.h:63
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
TrackTransformerForCosmicMuons(const edm::ParameterSet &)
Constructor.
PropagationDirection
edm::ESHandle< TrajectoryFitter > fitter(bool, int, float) const
the refitter used to refit the reco::Track
TrajectoryStateOnSurface innermostMeasurementState() const
float SumDy(const TransientTrackingRecHit::ConstRecHitContainer &) const
decide if the track should be reversed
~TrackTransformerForCosmicMuons() override
Destructor.
T z() const
Definition: PV3DBase.h:64
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:96
bool SlopeSum(const TransientTrackingRecHit::ConstRecHitContainer &) const
calculate the sum of slopes for the track
TrajectoryStateOnSurface outermostMeasurementState() const
#define LogTrace(id)
trackingRecHit_iterator recHitsEnd() const
last iterator to RecHits
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:18
const Track & track() const
static constexpr int RPC
Definition: MuonSubdetId.h:14
edm::ESHandle< TrajectorySmoother > smoother(bool, int, float) const
the smoother used to smooth the trajectory which came from the refitting step
HLT enums.
T get() const
Definition: EventSetup.h:71
static constexpr int DT
Definition: MuonSubdetId.h:12
static constexpr int CSC
Definition: MuonSubdetId.h:13
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:101
trackingRecHit_iterator recHitsBegin() const
first iterator to RecHits
ParameterSet const & parameterSet(Provenance const &provenance)
Definition: Provenance.cc:11
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39
TransientTrackingRecHit::ConstRecHitContainer getTransientRecHits(const reco::TransientTrack &track) const