CMS 3D CMS Logo

MuonTrackLoader.cc
Go to the documentation of this file.
1 
10 
14 
24 
26 
30 
35 
38 
41 
42 
43 using namespace edm;
44 using namespace std;
45 using namespace reco;
46 
47 std::vector<const TrackingRecHit*> MuonTrackLoader::unpackHit(const TrackingRecHit &hit)
48 {
49  // get rec hit det id and rec hit type
50  DetId id = hit.geographicalId();
51  uint16_t detid = id.det();
52 
53  std::vector<const TrackingRecHit*> hits;
54 
55  if (detid == DetId::Tracker) {
56  hits.push_back(&hit);
57  } else if (detid == DetId::Muon) {
58  uint16_t subdet = id.subdetId();
59  if (subdet == (uint16_t) MuonSubdetId::DT) {
60  if (hit.dimension() == 1) { // DT rechit (granularity 2)
61  hits.push_back(&hit);
62  } else if (hit.dimension() > 1) { // 4D segment (granularity 0).
63  // Both 2 and 4 dim cases. MB4s have 2D, but formatted in 4D segment
64  // 4D --> 2D
65  std::vector<const TrackingRecHit*> seg2D = hit.recHits();
66  // load 1D hits (2D --> 1D)
67  for (std::vector<const TrackingRecHit*>::const_iterator it = seg2D.begin(); it != seg2D.end(); ++it) {
68  std::vector<const TrackingRecHit*> hits1D = (*it)->recHits();
69  copy(hits1D.begin(), hits1D.end(), back_inserter(hits));
70  }
71  }
72  } else if (subdet == (uint16_t) MuonSubdetId::CSC) {
73  if (hit.dimension() == 2) { // CSC rechit (granularity 2)
74  hits.push_back(&hit);
75  } else if (hit.dimension() == 4) { // 4D segment (granularity 0)
76  // load 2D hits (4D --> 1D)
77  hits = hit.recHits();
78  }
79  } else if (subdet == (uint16_t) MuonSubdetId::RPC) {
80  hits.push_back(&hit);
81  }
82  else if (subdet == (uint16_t) MuonSubdetId::GEM) {
83  if (hit.dimension() == 2) { // GEM rechit
84  hits.push_back(&hit);
85  } else if (hit.dimension() == 4) { // GEM segment
86  hits = hit.recHits();
87  }
88  }
89  else if (subdet == (uint16_t) MuonSubdetId::ME0) { //segment
90  hits = hit.recHits();
91  }
92  }
93  return hits;
94 }
95 
96 // constructor (obsolete, should use eventSetUp not service..)
98  theService(service){
99 
100 
101  // option to do or not the smoothing step.
102  // the trajectories which are passed to the track loader are supposed to be non-smoothed
103  theSmoothingStep = parameterSet.getParameter<bool>("DoSmoothing");
104  if(theSmoothingStep)
105  theSmootherName = parameterSet.getParameter<string>("Smoother");
106 
107  theTrackerRecHitBuilderName = parameterSet.getParameter<std::string>("TTRHBuilder");
108 
109  // update at vertex
110  theUpdatingAtVtx = parameterSet.getParameter<bool>("VertexConstraint");
111 
112  // beam spot input tag
113  theBeamSpotInputTag = parameterSet.getParameter<edm::InputTag>("beamSpot");
115 
116 
117  // Flag to put the trajectory into the event
118  theTrajectoryFlag = parameterSet.getUntrackedParameter<bool>("PutTrajectoryIntoEvent",true);
119 
120  theL2SeededTkLabel = parameterSet.getUntrackedParameter<string>("MuonSeededTracksInstance",string());
121 
122  ParameterSet updatorPar = parameterSet.getParameter<ParameterSet>("MuonUpdatorAtVertexParameters");
123  theUpdatorAtVtx = new MuonUpdatorAtVertex(updatorPar,service);
124 
125  thePutTkTrackFlag = parameterSet.getUntrackedParameter<bool>("PutTkTrackIntoEvent",false);
126  theSmoothTkTrackFlag = parameterSet.getUntrackedParameter<bool>("SmoothTkTrack",false);
127  theAllowNoVtxFlag = parameterSet.getUntrackedParameter<bool>("AllowNoVertex",false);
128 }
129 
131  if(theUpdatorAtVtx) delete theUpdatorAtVtx;
132 }
133 
136  Event& event, const TrackerTopology& ttopo, const string& instance, bool reallyDoSmoothing) {
137  std::vector<bool> dummyVecBool;
138  return loadTracks(trajectories, event, dummyVecBool, ttopo, instance, reallyDoSmoothing);
139 }
140 
143  Event& event, std::vector<bool>& tkBoolVec,
144  const TrackerTopology& ttopo,
145  const string& instance, bool reallyDoSmoothing) {
146 
147  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
148 
149  const string metname = "Muon|RecoMuon|MuonTrackLoader";
150 
151  // the track collectios; they will be loaded in the event
152  auto trackCollection = std::make_unique<reco::TrackCollection>();
153  // ... and its reference into the event
154  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
155 
156  // track collection for the tracks updated at vertex
157  auto updatedAtVtxTrackCollection = std::make_unique<reco::TrackCollection>();
158  // ... and its (eventually) reference into the event
159  reco::TrackRefProd trackUpdatedCollectionRefProd;
160  if(theUpdatingAtVtx) trackUpdatedCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance+"UpdatedAtVtx");
161 
162  // Association map between updated and non updated at vtx tracks
163  auto trackToTrackmap = std::make_unique<reco::TrackToTrackMap>(trackCollectionRefProd, trackUpdatedCollectionRefProd);
164 
165  // the track extra collection, it will be loaded in the event
166  auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
167  // ... and its reference into the event
168  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
169 
170  // the rechit collection, it will be loaded in the event
171  auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
172  // ... and its reference into the event
173  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
174 
175  // Collection of Trajectory
176  auto trajectoryCollection = std::make_unique<vector<Trajectory>>();
177 
178  // don't waste any time...
179  if ( trajectories.empty() ) {
180  event.put(std::move(recHitCollection),instance);
181  event.put(std::move(trackExtraCollection),instance);
182  if(theTrajectoryFlag) {
183  event.put(std::move(trajectoryCollection),instance);
184 
185  // Association map between track and trajectory
186  auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
187  event.put(std::move(trajTrackMap), instance );
188  }
189  if(theUpdatingAtVtx){
190  event.put(std::move(trackToTrackmap));
191  event.put(std::move(updatedAtVtxTrackCollection),instance+"UpdatedAtVtx");
192  }
193  return event.put(std::move(trackCollection),instance);
194  }
195 
197  event.getByToken(theBeamSpotToken, beamSpot);
198 
199  LogTrace(metname) << "Create the collection of Tracks";
200 
201  reco::TrackRef::key_type trackIndex = 0;
202  reco::TrackRef::key_type trackUpdatedIndex = 0;
203 
204  reco::TrackExtraRef::key_type trackExtraIndex = 0;
205  TrackingRecHitRef::key_type recHitsIndex = 0;
206 
208  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
209  std::map<unsigned int, unsigned int> tjTkMap;
210 
211  if(doSmoothing) {
213  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
214  theSmoother.reset(aSmoother->clone());
215  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
216  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
217  theTrackerRecHitBuilder.product();
218  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
219  theSmoother->setHitCloner(&hitCloner);
220  }
221 
222 
223  unsigned int tjCnt = 0;
224  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
225  rawTrajectory != trajectories.end(); ++rawTrajectory, ++tjCnt){
226 
227  Trajectory &trajectory = **rawTrajectory;
228 
229  if(doSmoothing){
230  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
231 
232  if(!trajectoriesSM.empty()) {
233  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
234  trajectory = trajectoriesSM.front();
235  trajectory.setSeedRef(tmpSeedRef);
236  LogDebug(metname) << "theSeedRef.isNonnull " << trajectory.seedRef().isNonnull();
237  } else
238  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
239  }
240 
241  if(theTrajectoryFlag) {
242  trajectoryCollection->push_back(trajectory);
243  iTjRef++;
244  }
245 
246 
247  // build the "bare" track from the trajectory.
248  // This track has the parameters defined at PCA (no update)
249  pair<bool,reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
250 
251  // Check if the extrapolation went well
252  if(!resultOfTrackExtrapAtPCA.first) {
253  delete *rawTrajectory;
254  continue;
255  }
256 
257 
258  // take the "bare" track at PCA
259  reco::Track &track = resultOfTrackExtrapAtPCA.second;
260 
261  // build the "bare" track extra from the trajectory
262  reco::TrackExtra trackExtra = buildTrackExtra( trajectory );
263 
264  // get the TrackExtraRef (persitent reference of the track extra)
265  reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++ );
266 
267  // set the persistent track-extra reference to the Track
268  track.setExtra(trackExtraRef);
269 
270  // build the updated-at-vertex track, starting from the previous track
271  pair<bool,reco::Track> updateResult(false,reco::Track());
272 
273  if(theUpdatingAtVtx){
274  // build the "bare" track UPDATED at vtx
275  updateResult = buildTrackUpdatedAtPCA(track, *beamSpot);
276 
277  if(!updateResult.first) ++trackIndex;
278  else{
279 
280  // set the persistent track-extra reference to the Track
281  updateResult.second.setExtra(trackExtraRef);
282 
283  // Fill the map
284  trackToTrackmap->insert(reco::TrackRef(trackCollectionRefProd,trackIndex++),
285  reco::TrackRef(trackUpdatedCollectionRefProd,trackUpdatedIndex++));
286  }
287  }
288 
289 
290  // get the transient rechit and co from the trajectory
291  reco::TrackExtra::TrajParams trajParams;
293  Traj2TrackHits t2t;
294  auto ih = recHitCollection->size();
295  t2t(trajectory,*recHitCollection,trajParams,chi2s);
296  auto ie = recHitCollection->size();
297  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
298  trackExtra.setHits(recHitCollectionRefProd,ih,ie-ih);
299  trackExtra.setTrajParams(std::move(trajParams),std::move(chi2s));
300  assert(trackExtra.trajParams().size()==trackExtra.recHitsSize());
301 
302 
303  // Fill the hit pattern
304  for (;ih<ie; ++ih) {
305  auto const & hit = (*recHitCollection)[ih];
307  for (auto hh : hits) {
308  if unlikely(!track.appendHitPattern(*hh, ttopo)) break;
309  }
310 
311  if(theUpdatingAtVtx && updateResult.first){
312  for (auto hh : hits) {
313  if unlikely(!updateResult.second.appendHitPattern(*hh, ttopo)) break;
314  }
315  }
316  }
317 
318  // fill the TrackExtraCollection
319  trackExtraCollection->push_back(trackExtra);
320 
321  // fill the TrackCollection
322  trackCollection->push_back(track);
323  iTkRef++;
324  LogTrace(metname) << "Debug Track being loaded pt "<< track.pt();
325  // fill the TrackCollection updated at vtx
326  if(theUpdatingAtVtx && updateResult.first)
327  updatedAtVtxTrackCollection->push_back(updateResult.second);
328 
329  // We don't need the original trajectory anymore.
330  // It has been copied by value in the trajectoryCollection, if
331  // it is required to put it into the event.
332  delete *rawTrajectory;
333 
334  if(tkBoolVec.size()>tjCnt) tkBoolVec[tjCnt] = true;
335  if(theTrajectoryFlag) tjTkMap[iTjRef-1] = iTkRef-1;
336  }
337 
338 
339 
340  // Put the Collections in the event
341  LogTrace(metname) << "put the Collections in the event";
342  event.put(std::move(recHitCollection),instance);
343  event.put(std::move(trackExtraCollection),instance);
344 
345  OrphanHandle<reco::TrackCollection> returnTrackHandle;
346  OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
347  if(theUpdatingAtVtx){
348  nonUpdatedHandle = event.put(std::move(trackCollection),instance);
349  event.put(std::move(trackToTrackmap));
350  returnTrackHandle = event.put(std::move(updatedAtVtxTrackCollection),instance+"UpdatedAtVtx");
351  }
352  else {
353  returnTrackHandle = event.put(std::move(trackCollection),instance);
354  nonUpdatedHandle = returnTrackHandle;
355  }
356 
357  if ( theTrajectoryFlag ) {
358  OrphanHandle<std::vector<Trajectory> > rTrajs = event.put(std::move(trajectoryCollection),instance);
359 
360  // Association map between track and trajectory
361  auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, nonUpdatedHandle);
362 
363  // Now Create traj<->tracks association map
364  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
365  i != tjTkMap.end(); i++ ) {
366  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
367  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
368  }
369  event.put(std::move(trajTrackMap), instance );
370  }
371 
372  return returnTrackHandle;
373 }
374 
377  Event& event,
378  const TrackerTopology& ttopo) {
379 
380  const string metname = "Muon|RecoMuon|MuonTrackLoader";
381 
382  // the muon collection, it will be loaded in the event
383  auto trackLinksCollection = std::make_unique<reco::MuonTrackLinksCollection>();
384 
385  // don't waste any time...
386  if ( muonCands.empty() ) {
387  auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
388  auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
389  auto trackCollection = std::make_unique<reco::TrackCollection>();
390 
391  event.put(std::move(recHitCollection));
392  event.put(std::move(trackExtraCollection));
393  event.put(std::move(trackCollection));
394 
395  //need to also put the tracker tracks collection if requested
396  if(thePutTkTrackFlag){
397  //will take care of putting nothing in the event but the empty collection
398  TrajectoryContainer trackerTrajs;
399  loadTracks(trackerTrajs, event, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
400  }
401 
402  return event.put(std::move(trackLinksCollection));
403  }
404 
405  // get combined Trajectories
406  TrajectoryContainer combinedTrajs;
407  TrajectoryContainer trackerTrajs;
408  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it) {
409  LogDebug(metname) << "Loader glbSeedRef " << (*it)->trajectory()->seedRef().isNonnull();
410  if ((*it)->trackerTrajectory() ) LogDebug(metname) << " " << "tkSeedRef " << (*it)->trackerTrajectory()->seedRef().isNonnull();
411 
412  combinedTrajs.push_back((*it)->trajectory());
413  if ( thePutTkTrackFlag ) trackerTrajs.push_back((*it)->trackerTrajectory());
414 
415  else {
416  if ((*it)->trackerTrajectory()) delete ((*it)->trackerTrajectory());
417  }
418 
419  // // Create the links between sta and tracker tracks
420  // reco::MuonTrackLinks links;
421  // links.setStandAloneTrack((*it)->muonTrack());
422  // links.setTrackerTrack((*it)->trackerTrack());
423  // trackLinksCollection->push_back(links);
424  // delete *it;
425  }
426 
427  // create the TrackCollection of combined Trajectories
428  // FIXME: could this be done one track at a time in the previous loop?
429  LogTrace(metname) << "Build combinedTracks";
430  std::vector<bool> combTksVec(combinedTrajs.size(), false);
431  OrphanHandle<reco::TrackCollection> combinedTracks = loadTracks(combinedTrajs, event, combTksVec, ttopo);
432 
434  std::vector<bool> trackerTksVec(trackerTrajs.size(), false);
435  if(thePutTkTrackFlag) {
436  LogTrace(metname) << "Build trackerTracks: "
437  << trackerTrajs.size();
438  trackerTracks = loadTracks(trackerTrajs, event, trackerTksVec, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
439  } else {
440  for (TrajectoryContainer::iterator it = trackerTrajs.begin(); it != trackerTrajs.end(); ++it) {
441  if(*it) delete *it;
442  }
443  }
444 
445  LogTrace(metname) << "Set the final links in the MuonTrackLinks collection";
446 
447  unsigned int candposition(0), position(0), tkposition(0);
448  //reco::TrackCollection::const_iterator glIt = combinedTracks->begin(),
449  // glEnd = combinedTracks->end();
450 
451  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it, ++candposition) {
452 
453  // The presence of the global track determines whether to fill the MuonTrackLinks or not
454  // N.B. We are assuming here that the global tracks in "combinedTracks"
455  // have the same order as the muon candidates in "muonCands"
456  // (except for possible missing tracks), which should always be the case...
457  //if( glIt == glEnd ) break;
458  if(combTksVec[candposition]) {
459  reco::TrackRef combinedTR(combinedTracks, position++);
460  //++glIt;
461 
462  // Create the links between sta and tracker tracks
463  reco::MuonTrackLinks links;
464  links.setStandAloneTrack((*it)->muonTrack());
465  links.setTrackerTrack((*it)->trackerTrack());
466  links.setGlobalTrack(combinedTR);
467 
468  if(thePutTkTrackFlag && trackerTksVec[candposition]) {
469  reco::TrackRef trackerTR(trackerTracks, tkposition++);
470  links.setTrackerTrack(trackerTR);
471  }
472 
473  trackLinksCollection->push_back(links);
474  }
475 
476  else { // if no global track, still increment the tracker-track counter when appropriate
477  if(thePutTkTrackFlag && trackerTksVec[candposition]) tkposition++;
478  }
479 
480  delete *it;
481  }
482 
483  if( thePutTkTrackFlag && trackerTracks.isValid() && !(combinedTracks->size() > 0 && trackerTracks->size() > 0 ) )
484  LogWarning(metname)<<"The MuonTrackLinkCollection is incomplete";
485 
486  // put the MuonCollection in the event
487  LogTrace(metname) << "put the MuonCollection in the event" << "\n";
488 
489  return event.put(std::move(trackLinksCollection));
490 }
491 
494  Event& event, const std::vector<std::pair<Trajectory*,reco::TrackRef> >& miniMap, Handle<reco::TrackCollection> const& trackHandle, const TrackerTopology& ttopo, const string& instance, bool reallyDoSmoothing) {
495 
496  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
497 
498  const string metname = "Muon|RecoMuon|MuonTrackLoader|TevMuonTrackLoader";
499 
500  LogDebug(metname)<<"TeV LoadTracks instance: " << instance;
501 
502  // the track collectios; they will be loaded in the event
503  auto trackCollection = std::make_unique<reco::TrackCollection>();
504  // ... and its reference into the event
505  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
506 
507  // Association map between GlobalMuons and TeVMuons
508  auto trackToTrackmap = std::make_unique<reco::TrackToTrackMap>(trackHandle,trackCollectionRefProd);
509 
510  // the track extra collection, it will be loaded in the event
511  auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
512  // ... and its reference into the event
513  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
514 
515  // the rechit collection, it will be loaded in the event
516  auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
517  // ... and its reference into the event
518  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
519 
520  // Collection of Trajectory
521  auto trajectoryCollection = std::make_unique<std::vector<Trajectory>>();
522 
523  // don't waste any time...
524  if ( trajectories.empty() ) {
525  event.put(std::move(recHitCollection),instance);
526  event.put(std::move(trackExtraCollection),instance);
527  if(theTrajectoryFlag) {
528  event.put(std::move(trajectoryCollection),instance);
529 
530  // Association map between track and trajectory
531  auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
532  event.put(std::move(trajTrackMap), instance );
533  }
534  event.put(std::move(trackToTrackmap), instance);
535  return event.put(std::move(trackCollection),instance);
536  }
537 
538  LogTrace(metname) << "Create the collection of Tracks";
539 
541  event.getByToken(theBeamSpotToken,beamSpot);
542 
543  reco::TrackRef::key_type trackIndex = 0;
544  // reco::TrackRef::key_type trackUpdatedIndex = 0;
545 
546  reco::TrackExtraRef::key_type trackExtraIndex = 0;
547  TrackingRecHitRef::key_type recHitsIndex = 0;
548 
550  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
551  std::map<unsigned int, unsigned int> tjTkMap;
552 
553  if(doSmoothing) {
555  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
556  theSmoother.reset(aSmoother->clone());
557  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
558  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
559  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
560  theSmoother->setHitCloner(&hitCloner);
561  }
562 
563  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
564  rawTrajectory != trajectories.end(); ++rawTrajectory){
565 
566  reco::TrackRef glbRef;
567  std::vector<std::pair<Trajectory*,reco::TrackRef> >::const_iterator mmit;
568  for(mmit = miniMap.begin();mmit!=miniMap.end();++mmit){
569  if(mmit->first == *rawTrajectory) glbRef = mmit->second;
570  }
571 
572  Trajectory &trajectory = **rawTrajectory;
573 
574  if(doSmoothing){
575  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
576 
577  if(!trajectoriesSM.empty()) {
578  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
579  trajectory = trajectoriesSM.front();
580  trajectory.setSeedRef(tmpSeedRef);
581  } else
582  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
583  }
584 
585  if(theTrajectoryFlag) {
586  trajectoryCollection->push_back(trajectory);
587  iTjRef++;
588  }
589 
590  // build the "bare" track from the trajectory.
591  // This track has the parameters defined at PCA (no update)
592  pair<bool,reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
593 
594  // Check if the extrapolation went well
595  if(!resultOfTrackExtrapAtPCA.first) {
596  delete *rawTrajectory;
597  continue;
598  }
599 
600  // take the "bare" track at PCA
601  reco::Track &track = resultOfTrackExtrapAtPCA.second;
602 
603  // build the "bare" track extra from the trajectory
604  reco::TrackExtra trackExtra = buildTrackExtra( trajectory );
605 
606  // get the TrackExtraRef (persitent reference of the track extra)
607  reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++ );
608 
609  // set the persistent track-extra reference to the Track
610  track.setExtra(trackExtraRef);
611 
612  // Fill the map
613  trackToTrackmap->insert(glbRef,
614  reco::TrackRef(trackCollectionRefProd,trackIndex++));
615 
616  // build the updated-at-vertex track, starting from the previous track
617  pair<bool,reco::Track> updateResult(false,reco::Track());
618 
619  // get the transient rechit and co from the trajectory
620  reco::TrackExtra::TrajParams trajParams;
622  Traj2TrackHits t2t;
623  auto ih = recHitCollection->size();
624  t2t(trajectory,*recHitCollection,trajParams,chi2s);
625  auto ie = recHitCollection->size();
626  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
627  trackExtra.setHits(recHitCollectionRefProd,ih,ie-ih);
628  trackExtra.setTrajParams(std::move(trajParams),std::move(chi2s));
629  assert(trackExtra.trajParams().size()==trackExtra.recHitsSize());
630 
631  // Fill the hit pattern
632  for (;ih<ie; ++ih) {
633  auto const & hit = (*recHitCollection)[ih];
635  for (auto hh : hits) {
636  if unlikely(!track.appendHitPattern(*hh, ttopo)) break;
637  }
638 
639  if(theUpdatingAtVtx && updateResult.first){
640  for (auto hh : hits) {
641  if unlikely(!updateResult.second.appendHitPattern(*hh, ttopo)) break;
642  }
643  }
644  }
645 
646  // fill the TrackExtraCollection
647  trackExtraCollection->push_back(trackExtra);
648 
649  // fill the TrackCollection
650  trackCollection->push_back(track);
651  iTkRef++;
652  LogTrace(metname) << "Debug Track being loaded pt "<< track.pt();
653 
654  // We don't need the original trajectory anymore.
655  // It has been copied by value in the trajectoryCollection, if
656  // it is required to put it into the event.
657  delete *rawTrajectory;
658 
659  if(theTrajectoryFlag) tjTkMap[iTjRef-1] = iTkRef-1;
660  }
661 
662 
663 
664  // Put the Collections in the event
665  LogTrace(metname) << "put the Collections in the event";
666  event.put(std::move(recHitCollection),instance);
667  event.put(std::move(trackExtraCollection),instance);
668 
669  OrphanHandle<reco::TrackCollection> returnTrackHandle;
670  OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
671  if(theUpdatingAtVtx){
672  }
673  else {
674  event.put(std::move(trackToTrackmap),instance);
675  returnTrackHandle = event.put(std::move(trackCollection),instance);
676  nonUpdatedHandle = returnTrackHandle;
677  }
678 
679  if ( theTrajectoryFlag ) {
680  OrphanHandle<std::vector<Trajectory> > rTrajs = event.put(std::move(trajectoryCollection),instance);
681 
682  // Association map between track and trajectory
683  auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, nonUpdatedHandle);
684 
685  // Now Create traj<->tracks association map
686  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
687  i != tjTkMap.end(); i++ ) {
688  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
689  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
690  }
691  event.put(std::move(trajTrackMap), instance );
692  }
693 
694  return returnTrackHandle;
695 }
696 
697 
698 pair<bool,reco::Track> MuonTrackLoader::buildTrackAtPCA(const Trajectory& trajectory, const reco::BeamSpot &beamSpot) const {
699 
700  const string metname = "Muon|RecoMuon|MuonTrackLoader";
701 
703 
704  // FIXME: check the prop direction
705  TrajectoryStateOnSurface innerTSOS = trajectory.geometricalInnermostState();
706 
707  // This is needed to extrapolate the tsos at vertex
708  LogTrace(metname) << "Propagate to PCA...";
709  pair<bool,FreeTrajectoryState>
710  extrapolationResult = theUpdatorAtVtx->propagate(innerTSOS, beamSpot);
711  FreeTrajectoryState ftsAtVtx;
712 
713  if(extrapolationResult.first)
714  ftsAtVtx = extrapolationResult.second;
715  else{
716  if(TrackerBounds::isInside(innerTSOS.globalPosition())){
717  LogInfo(metname) << "Track in the Tracker: taking the innermost state instead of the state at PCA";
718  ftsAtVtx = *innerTSOS.freeState();
719  }
720  else{
721  if ( theAllowNoVtxFlag ) {
722  LogInfo(metname) << "Propagation to PCA failed, taking the innermost state instead of the state at PCA";
723  ftsAtVtx = *innerTSOS.freeState();
724  } else {
725  LogInfo(metname) << "Stand Alone track: this track will be rejected";
726  return pair<bool,reco::Track>(false,reco::Track());
727  }
728  }
729  }
730 
731  LogTrace(metname) << "TSOS after the extrapolation at vtx";
732  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
733 
734  GlobalPoint pca = ftsAtVtx.position();
735  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
736  GlobalVector p = ftsAtVtx.momentum();
737  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
738 
739  bool bon = true;
740  if(fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) bon=false;
741  double ndof = trajectory.ndof(bon);
742 
743  reco::Track track(trajectory.chiSquared(),
744  ndof,
745  persistentPCA,
746  persistentMomentum,
747  ftsAtVtx.charge(),
748  ftsAtVtx.curvilinearError());
749 
750  return pair<bool,reco::Track>(true,track);
751 }
752 
753 
755 
756  const string metname = "Muon|RecoMuon|MuonTrackLoader";
758 
759  // build the transient track
760  reco::TransientTrack transientTrack(track,
761  &*theService->magneticField(),
762  theService->trackingGeometry());
763 
764  LogTrace(metname) << "Apply the vertex constraint";
765  pair<bool,FreeTrajectoryState> updateResult = theUpdatorAtVtx->update(transientTrack,beamSpot);
766 
767  if(!updateResult.first){
768  return pair<bool,reco::Track>(false,reco::Track());
769  }
770 
771  LogTrace(metname) << "FTS after the vertex constraint";
772  FreeTrajectoryState &ftsAtVtx = updateResult.second;
773 
774  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
775 
776  GlobalPoint pca = ftsAtVtx.position();
777  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
778  GlobalVector p = ftsAtVtx.momentum();
779  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
780 
781  reco::Track updatedTrack(track.chi2(),
782  track.ndof(),
783  persistentPCA,
784  persistentMomentum,
785  ftsAtVtx.charge(),
786  ftsAtVtx.curvilinearError());
787 
788  return pair<bool,reco::Track>(true,updatedTrack);
789 }
790 
791 
793 
794  const string metname = "Muon|RecoMuon|MuonTrackLoader";
795 
796  const Trajectory::RecHitContainer transRecHits = trajectory.recHits();
797 
798  // put the collection of TrackingRecHit in the event
799 
800  // sets the outermost and innermost TSOSs
801  // FIXME: check it!
802  TrajectoryStateOnSurface outerTSOS;
803  TrajectoryStateOnSurface innerTSOS;
804  unsigned int innerId=0, outerId=0;
806  DetId outerDetId;
807 
808  if (trajectory.direction() == alongMomentum) {
809  LogTrace(metname)<<"alongMomentum";
810  outerTSOS = trajectory.lastMeasurement().updatedState();
811  innerTSOS = trajectory.firstMeasurement().updatedState();
812  outerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
813  innerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
814  outerRecHit = trajectory.lastMeasurement().recHit();
815  outerDetId = trajectory.lastMeasurement().recHit()->geographicalId();
816  }
817  else if (trajectory.direction() == oppositeToMomentum) {
818  LogTrace(metname)<<"oppositeToMomentum";
819  outerTSOS = trajectory.firstMeasurement().updatedState();
820  innerTSOS = trajectory.lastMeasurement().updatedState();
821  outerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
822  innerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
823  outerRecHit = trajectory.firstMeasurement().recHit();
824  outerDetId = trajectory.firstMeasurement().recHit()->geographicalId();
825  }
826  else LogError(metname)<<"Wrong propagation direction!";
827 
828  const GeomDet *outerDet = theService->trackingGeometry()->idToDet(outerDetId);
829  GlobalPoint outerTSOSPos = outerTSOS.globalParameters().position();
830  bool inside = outerDet->surface().bounds().inside(outerDet->toLocal(outerTSOSPos));
831 
832 
833  GlobalPoint hitPos = (outerRecHit->isValid()) ? outerRecHit->globalPosition() : outerTSOS.globalParameters().position() ;
834 
835  if(!inside) {
836  LogTrace(metname)<<"The Global Muon outerMostMeasurementState is not compatible with the recHit detector! Setting outerMost postition to recHit position if recHit isValid: " << outerRecHit->isValid();
837  LogTrace(metname)<<"From " << outerTSOSPos << " to " << hitPos;
838  }
839 
840 
841  //build the TrackExtra
842  GlobalPoint v = (inside) ? outerTSOSPos : hitPos ;
843  GlobalVector p = outerTSOS.globalParameters().momentum();
844  math::XYZPoint outpos( v.x(), v.y(), v.z() );
845  math::XYZVector outmom( p.x(), p.y(), p.z() );
846 
847  v = innerTSOS.globalParameters().position();
848  p = innerTSOS.globalParameters().momentum();
849  math::XYZPoint inpos( v.x(), v.y(), v.z() );
850  math::XYZVector inmom( p.x(), p.y(), p.z() );
851 
852  reco::TrackExtra trackExtra(outpos, outmom, true, inpos, inmom, true,
853  outerTSOS.curvilinearError(), outerId,
854  innerTSOS.curvilinearError(), innerId,
855  trajectory.direction(),trajectory.seedRef());
856 
857  return trackExtra;
858 
859 }
860 
#define LogDebug(id)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
edm::EDGetTokenT< reco::BeamSpot > theBeamSpotToken
std::unique_ptr< TrajectorySmoother > theSmoother
T getUntrackedParameter(std::string const &, T const &) const
MuonTrackLoader(edm::ParameterSet &parameterSet, edm::ConsumesCollector &iC, const MuonServiceProxy *service=0)
Constructor for the STA reco the args must be specify!
static bool isInside(const GlobalPoint &)
ConstRecHitPointer const & recHit() const
std::vector< unsigned char > Chi2sFive
static PFTauRenderPlugin instance
const std::string metname
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
TkClonerImpl hitCloner
const CurvilinearTrajectoryError & curvilinearError() const
static const int GEM
Definition: MuonSubdetId.h:15
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
T y() const
Definition: PV3DBase.h:63
std::string theSmootherName
std::pair< bool, FreeTrajectoryState > propagate(const TrajectoryStateOnSurface &tsos, const reco::BeamSpot &beamSpot) const
Propagate the state to the 2D-PCA.
GlobalPoint globalPosition() const
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:69
ConstRecHitContainer recHits() const
Definition: Trajectory.h:204
static std::vector< const TrackingRecHit * > unpackHit(const TrackingRecHit &hit)
const Bounds & bounds() const
Definition: Surface.h:120
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:337
TrackCharge charge() const
TrajectoryStateOnSurface geometricalInnermostState() const
Definition: Trajectory.cc:220
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:42
const CurvilinearTrajectoryError & curvilinearError() const
MuonCandidate::CandidateContainer CandidateContainer
const MuonServiceProxy * theService
virtual TrajectorySmoother * clone() const =0
std::string dumpFTS(const FreeTrajectoryState &fts) const
PropagationDirection const & direction() const
Definition: Trajectory.cc:140
#define unlikely(x)
virtual std::vector< const TrackingRecHit * > recHits() const =0
Access to component RecHits (if any)
static const int ME0
Definition: MuonSubdetId.h:16
std::vector< LocalTrajectoryParameters > TrajParams
void push_back(D *&d)
Definition: OwnVector.h:290
virtual bool inside(const Local3DPoint &) const =0
Determine if the point is inside the bounds.
static const int CSC
Definition: MuonSubdetId.h:13
edm::InputTag theBeamSpotInputTag
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:544
edm::OrphanHandle< reco::TrackCollection > loadTracks(const TrajectoryContainer &, edm::Event &, const TrackerTopology &ttopo, const std::string &="", bool=true)
Convert the trajectories into tracks and load the tracks in the event.
std::string theTrackerRecHitBuilderName
std::pair< bool, FreeTrajectoryState > update(const reco::TransientTrack &track, const reco::BeamSpot &beamSpot) const
Applies the vertex constraint.
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:550
unsigned int recHitsSize() const
number of RecHits
virtual int dimension() const =0
TrajParams const & trajParams() const
virtual ~MuonTrackLoader()
Destructor.
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:174
double pt() const
track transverse momentum
Definition: TrackBase.h:616
FreeTrajectoryState const * freeState(bool withErrors=true) const
T z() const
Definition: PV3DBase.h:64
edm::RefToBase< TrajectorySeed > seedRef(void) const
Definition: Trajectory.h:321
MuonUpdatorAtVertex * theUpdatorAtVtx
GlobalVector momentum() const
#define LogTrace(id)
MuonCandidate::TrajectoryContainer TrajectoryContainer
Definition: DetId.h:18
GlobalPoint position() const
int ndof(bool bon=true) const
Definition: Trajectory.cc:102
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
void setExtra(const TrackExtraRef &ref)
set reference to "extra" object
Definition: Track.h:184
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:187
ConstRecHitContainer RecHitContainer
Definition: Trajectory.h:46
#define debug
Definition: HDRShower.cc:19
const GlobalTrajectoryParameters & globalParameters() const
std::string theL2SeededTkLabel
Label for L2SeededTracks.
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
TrackingRecHit::ConstRecHitPointer ConstRecHitPointer
reco::TrackExtra buildTrackExtra(const Trajectory &) const
bool appendHitPattern(const TrackingRecHit &hit, const TrackerTopology &ttopo)
append a single hit to the HitPattern
Definition: TrackBase.h:455
void setSeedRef(const edm::RefToBase< TrajectorySeed > &seedRef)
Definition: Trajectory.h:323
float chiSquared() const
Definition: Trajectory.h:262
std::pair< bool, reco::Track > buildTrackAtPCA(const Trajectory &trajectory, const reco::BeamSpot &) const
Build a track at the PCA WITHOUT any vertex constriant.
fixed size matrix
HLT enums.
static const int RPC
Definition: MuonSubdetId.h:14
static int position[264][3]
Definition: ReadPGInfo.cc:509
static const int DT
Definition: MuonSubdetId.h:12
const MuonServiceProxy * theService
TrajectoryStateOnSurface const & updatedState() const
DetId geographicalId() const
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
T x() const
Definition: PV3DBase.h:62
std::pair< bool, reco::Track > buildTrackUpdatedAtPCA(const reco::Track &trackAtPCA, const reco::BeamSpot &) const
Takes a track at the PCA and applies the vertex constriant.
T const * product() const
Definition: ESHandle.h:86
ParameterSet const & parameterSet(Provenance const &provenance)
Definition: Provenance.cc:11
def move(src, dest)
Definition: eostools.py:510
void setTrajParams(TrajParams tmps, Chi2sFive chi2s)
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
Definition: event.py:1