CMS 3D CMS Logo

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