CMS 3D CMS Logo

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