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  // update at vertex
98  theUpdatingAtVtx = parameterSet.getParameter<bool>("VertexConstraint");
99 
100  // beam spot input tag
101  theBeamSpotInputTag = parameterSet.getParameter<edm::InputTag>("beamSpot");
103 
104 
105  // Flag to put the trajectory into the event
106  theTrajectoryFlag = parameterSet.getUntrackedParameter<bool>("PutTrajectoryIntoEvent",true);
107 
108  theL2SeededTkLabel = parameterSet.getUntrackedParameter<string>("MuonSeededTracksInstance",string());
109 
110  ParameterSet updatorPar = parameterSet.getParameter<ParameterSet>("MuonUpdatorAtVertexParameters");
111  theUpdatorAtVtx = new MuonUpdatorAtVertex(updatorPar,service);
112 
113  thePutTkTrackFlag = parameterSet.getUntrackedParameter<bool>("PutTkTrackIntoEvent",false);
114  theSmoothTkTrackFlag = parameterSet.getUntrackedParameter<bool>("SmoothTkTrack",false);
115  theAllowNoVtxFlag = parameterSet.getUntrackedParameter<bool>("AllowNoVertex",false);
116 }
117 
119  if(theUpdatorAtVtx) delete theUpdatorAtVtx;
120 }
121 
123 MuonTrackLoader::loadTracks(const TrajectoryContainer& trajectories,
124  Event& event, const string& instance, bool reallyDoSmoothing) {
125  std::vector<bool> dummyVecBool;
126  return loadTracks(trajectories, event, dummyVecBool, instance, reallyDoSmoothing);
127 }
128 
130 MuonTrackLoader::loadTracks(const TrajectoryContainer& trajectories,
131  Event& event, std::vector<bool>& tkBoolVec,
132  const string& instance, bool reallyDoSmoothing) {
133 
134  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
135 
136  const string metname = "Muon|RecoMuon|MuonTrackLoader";
137 
138  // the track collectios; they will be loaded in the event
139  auto_ptr<reco::TrackCollection> trackCollection(new reco::TrackCollection());
140  // ... and its reference into the event
141  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
142 
143  // track collection for the tracks updated at vertex
144  auto_ptr<reco::TrackCollection> updatedAtVtxTrackCollection(new reco::TrackCollection());
145  // ... and its (eventually) reference into the event
146  reco::TrackRefProd trackUpdatedCollectionRefProd;
147  if(theUpdatingAtVtx) trackUpdatedCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance+"UpdatedAtVtx");
148 
149  // Association map between updated and non updated at vtx tracks
150  auto_ptr<reco:: TrackToTrackMap> trackToTrackmap(new reco::TrackToTrackMap);
151 
152  // the track extra collection, it will be loaded in the event
153  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
154  // ... and its reference into the event
155  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
156 
157  // the rechit collection, it will be loaded in the event
158  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
159  // ... and its reference into the event
160  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
161 
162  // Collection of Trajectory
163  auto_ptr<vector<Trajectory> > trajectoryCollection(new vector<Trajectory>);
164 
165  // Association map between track and trajectory
166  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
167 
168  // don't waste any time...
169  if ( trajectories.empty() ) {
170  event.put(recHitCollection,instance);
171  event.put(trackExtraCollection,instance);
172  if(theTrajectoryFlag) {
173  event.put(trajectoryCollection,instance);
174  event.put( trajTrackMap, instance );
175  }
176  if(theUpdatingAtVtx){
177  event.put(trackToTrackmap);
178  event.put(updatedAtVtxTrackCollection,instance+"UpdatedAtVtx");
179  }
180  return event.put(trackCollection,instance);
181  }
182 
184  event.getByToken(theBeamSpotToken, beamSpot);
185 
186  LogTrace(metname) << "Create the collection of Tracks";
187 
188  reco::TrackRef::key_type trackIndex = 0;
189  reco::TrackRef::key_type trackUpdatedIndex = 0;
190 
191  reco::TrackExtraRef::key_type trackExtraIndex = 0;
192  TrackingRecHitRef::key_type recHitsIndex = 0;
193 
195  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
196  std::map<unsigned int, unsigned int> tjTkMap;
197 
198  if(doSmoothing) {
200  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
201  theSmoother.reset(aSmoother->clone());
202  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
203  try {
204  std::string theTrackerRecHitBuilderName("WithAngleAndTemplate"); // to be moved to cfg in another PR
205  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
206  theTrackerRecHitBuilder.product();
207  } catch(...) {
208  std::string theTrackerRecHitBuilderName("hltESPTTRHBWithTrackAngle"); // FIXME FIXME
209  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
210  }
211  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
212  theSmoother->setHitCloner(&hitCloner);
213  }
214 
215 
216  unsigned int tjCnt = 0;
217  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
218  rawTrajectory != trajectories.end(); ++rawTrajectory, ++tjCnt){
219 
220  Trajectory &trajectory = **rawTrajectory;
221 
222  if(doSmoothing){
223  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
224 
225  if(!trajectoriesSM.empty()) {
226  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
227  trajectory = trajectoriesSM.front();
228  trajectory.setSeedRef(tmpSeedRef);
229  LogDebug(metname) << "theSeedRef.isNonnull " << trajectory.seedRef().isNonnull();
230  } else
231  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
232  }
233 
234  if(theTrajectoryFlag) {
235  trajectoryCollection->push_back(trajectory);
236  iTjRef++;
237  }
238 
239  // get the transient rechit from the trajectory
240  Trajectory::RecHitContainer transHits = trajectory.recHits();
241 
242  if ( trajectory.direction() == oppositeToMomentum)
243  reverse(transHits.begin(),transHits.end());
244 
245  // build the "bare" track from the trajectory.
246  // This track has the parameters defined at PCA (no update)
247  pair<bool,reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
248 
249  // Check if the extrapolation went well
250  if(!resultOfTrackExtrapAtPCA.first) {
251  delete *rawTrajectory;
252  continue;
253  }
254 
255  // take the "bare" track at PCA
256  reco::Track &track = resultOfTrackExtrapAtPCA.second;
257 
258  // build the "bare" track extra from the trajectory
259  reco::TrackExtra trackExtra = buildTrackExtra( trajectory );
260 
261  // get the TrackExtraRef (persitent reference of the track extra)
262  reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++ );
263 
264  // set the persistent track-extra reference to the Track
265  track.setExtra(trackExtraRef);
266 
267  // build the updated-at-vertex track, starting from the previous track
268  pair<bool,reco::Track> updateResult(false,reco::Track());
269 
270  if(theUpdatingAtVtx){
271  // build the "bare" track UPDATED at vtx
272  updateResult = buildTrackUpdatedAtPCA(track, *beamSpot);
273 
274  if(!updateResult.first) ++trackIndex;
275  else{
276 
277  // set the persistent track-extra reference to the Track
278  updateResult.second.setExtra(trackExtraRef);
279 
280  // Fill the map
281  trackToTrackmap->insert(reco::TrackRef(trackCollectionRefProd,trackIndex++),
282  reco::TrackRef(trackUpdatedCollectionRefProd,trackUpdatedIndex++));
283  }
284  }
285 
286  // Fill the track extra with the rec hit (persistent-)reference
287  for (Trajectory::RecHitContainer::const_iterator recHit = transHits.begin(); recHit != transHits.end(); ++recHit) {
288  TrackingRecHit *singleHit = (**recHit).hit()->clone();
289  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
290  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
291  if unlikely(!track.appendHitPattern(**it)){
292  break;
293  }
294  }
295 
296  if(theUpdatingAtVtx && updateResult.first){
297  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
298  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
299  if unlikely(!updateResult.second.appendHitPattern(**it)){
300  break;
301  }
302  }
303  }
304  recHitCollection->push_back( singleHit );
305  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
306  trackExtra.add(TrackingRecHitRef(recHitCollectionRefProd, recHitsIndex++ ));
307  }
308 
309  // fill the TrackExtraCollection
310  trackExtraCollection->push_back(trackExtra);
311 
312  // fill the TrackCollection
313  trackCollection->push_back(track);
314  iTkRef++;
315  LogTrace(metname) << "Debug Track being loaded pt "<< track.pt();
316  // fill the TrackCollection updated at vtx
317  if(theUpdatingAtVtx && updateResult.first)
318  updatedAtVtxTrackCollection->push_back(updateResult.second);
319 
320  // We don't need the original trajectory anymore.
321  // It has been copied by value in the trajectoryCollection, if
322  // it is required to put it into the event.
323  delete *rawTrajectory;
324 
325  if(tkBoolVec.size()>tjCnt) tkBoolVec[tjCnt] = true;
326  if(theTrajectoryFlag) tjTkMap[iTjRef-1] = iTkRef-1;
327  }
328 
329 
330 
331  // Put the Collections in the event
332  LogTrace(metname) << "put the Collections in the event";
333  event.put(recHitCollection,instance);
334  event.put(trackExtraCollection,instance);
335 
336  OrphanHandle<reco::TrackCollection> returnTrackHandle;
337  OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
338  if(theUpdatingAtVtx){
339  nonUpdatedHandle = event.put(trackCollection,instance);
340  event.put(trackToTrackmap);
341  returnTrackHandle = event.put(updatedAtVtxTrackCollection,instance+"UpdatedAtVtx");
342  }
343  else {
344  returnTrackHandle = event.put(trackCollection,instance);
345  nonUpdatedHandle = returnTrackHandle;
346  }
347 
348  if ( theTrajectoryFlag ) {
349  OrphanHandle<std::vector<Trajectory> > rTrajs = event.put(trajectoryCollection,instance);
350  // Now Create traj<->tracks association map
351  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
352  i != tjTkMap.end(); i++ ) {
353  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
354  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
355  }
356  event.put( trajTrackMap, instance );
357  }
358 
359  return returnTrackHandle;
360 }
361 
364  Event& event) {
365 
366  const string metname = "Muon|RecoMuon|MuonTrackLoader";
367 
368  // the muon collection, it will be loaded in the event
369  auto_ptr<reco::MuonTrackLinksCollection> trackLinksCollection(new reco::MuonTrackLinksCollection());
370 
371  // don't waste any time...
372  if ( muonCands.empty() ) {
373  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
374  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
375  auto_ptr<reco::TrackCollection> trackCollection( new reco::TrackCollection() );
376 
377  event.put(recHitCollection);
378  event.put(trackExtraCollection);
379  event.put(trackCollection);
380 
381  //need to also put the tracker tracks collection if requested
382  if(thePutTkTrackFlag){
383  //will take care of putting nothing in the event but the empty collection
384  TrajectoryContainer trackerTrajs;
385  loadTracks(trackerTrajs, event, theL2SeededTkLabel, theSmoothTkTrackFlag);
386  }
387 
388  return event.put(trackLinksCollection);
389  }
390 
391  // get combined Trajectories
392  TrajectoryContainer combinedTrajs;
393  TrajectoryContainer trackerTrajs;
394  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it) {
395  LogDebug(metname) << "Loader glbSeedRef " << (*it)->trajectory()->seedRef().isNonnull();
396  if ((*it)->trackerTrajectory() ) LogDebug(metname) << " " << "tkSeedRef " << (*it)->trackerTrajectory()->seedRef().isNonnull();
397 
398  combinedTrajs.push_back((*it)->trajectory());
399  if ( thePutTkTrackFlag ) trackerTrajs.push_back((*it)->trackerTrajectory());
400 
401  else {
402  if ((*it)->trackerTrajectory()) delete ((*it)->trackerTrajectory());
403  }
404 
405  // // Create the links between sta and tracker tracks
406  // reco::MuonTrackLinks links;
407  // links.setStandAloneTrack((*it)->muonTrack());
408  // links.setTrackerTrack((*it)->trackerTrack());
409  // trackLinksCollection->push_back(links);
410  // delete *it;
411  }
412 
413  // create the TrackCollection of combined Trajectories
414  // FIXME: could this be done one track at a time in the previous loop?
415  LogTrace(metname) << "Build combinedTracks";
416  std::vector<bool> combTksVec(combinedTrajs.size(), false);
417  OrphanHandle<reco::TrackCollection> combinedTracks = loadTracks(combinedTrajs, event, combTksVec);
418 
420  std::vector<bool> trackerTksVec(trackerTrajs.size(), false);
421  if(thePutTkTrackFlag) {
422  LogTrace(metname) << "Build trackerTracks: "
423  << trackerTrajs.size();
424  trackerTracks = loadTracks(trackerTrajs, event, trackerTksVec, theL2SeededTkLabel, theSmoothTkTrackFlag);
425  } else {
426  for (TrajectoryContainer::iterator it = trackerTrajs.begin(); it != trackerTrajs.end(); ++it) {
427  if(*it) delete *it;
428  }
429  }
430 
431  LogTrace(metname) << "Set the final links in the MuonTrackLinks collection";
432 
433  unsigned int candposition(0), position(0), tkposition(0);
434  //reco::TrackCollection::const_iterator glIt = combinedTracks->begin(),
435  // glEnd = combinedTracks->end();
436 
437  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it, ++candposition) {
438 
439  // The presence of the global track determines whether to fill the MuonTrackLinks or not
440  // N.B. We are assuming here that the global tracks in "combinedTracks"
441  // have the same order as the muon candidates in "muonCands"
442  // (except for possible missing tracks), which should always be the case...
443  //if( glIt == glEnd ) break;
444  if(combTksVec[candposition]) {
445  reco::TrackRef combinedTR(combinedTracks, position++);
446  //++glIt;
447 
448  // Create the links between sta and tracker tracks
450  links.setStandAloneTrack((*it)->muonTrack());
451  links.setTrackerTrack((*it)->trackerTrack());
452  links.setGlobalTrack(combinedTR);
453 
454  if(thePutTkTrackFlag && trackerTksVec[candposition]) {
455  reco::TrackRef trackerTR(trackerTracks, tkposition++);
456  links.setTrackerTrack(trackerTR);
457  }
458 
459  trackLinksCollection->push_back(links);
460  }
461 
462  else { // if no global track, still increment the tracker-track counter when appropriate
463  if(thePutTkTrackFlag && trackerTksVec[candposition]) tkposition++;
464  }
465 
466  delete *it;
467  }
468 
469  if( thePutTkTrackFlag && trackerTracks.isValid() && !(combinedTracks->size() > 0 && trackerTracks->size() > 0 ) )
470  LogWarning(metname)<<"The MuonTrackLinkCollection is incomplete";
471 
472  // put the MuonCollection in the event
473  LogTrace(metname) << "put the MuonCollection in the event" << "\n";
474 
475  return event.put(trackLinksCollection);
476 }
477 
479 MuonTrackLoader::loadTracks(const TrajectoryContainer& trajectories,
480  Event& event, const std::vector<std::pair<Trajectory*,reco::TrackRef> >& miniMap, const string& instance, bool reallyDoSmoothing) {
481 
482  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
483 
484  const string metname = "Muon|RecoMuon|MuonTrackLoader|TevMuonTrackLoader";
485 
486  LogDebug(metname)<<"TeV LoadTracks instance: " << instance;
487 
488  // the track collectios; they will be loaded in the event
489  auto_ptr<reco::TrackCollection> trackCollection(new reco::TrackCollection());
490  // ... and its reference into the event
491  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
492 
493  // Association map between GlobalMuons and TeVMuons
494  auto_ptr<reco:: TrackToTrackMap> trackToTrackmap(new reco::TrackToTrackMap);
495 
496  // the track extra collection, it will be loaded in the event
497  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
498  // ... and its reference into the event
499  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
500 
501  // the rechit collection, it will be loaded in the event
502  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
503  // ... and its reference into the event
504  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
505 
506  // Collection of Trajectory
507  auto_ptr<vector<Trajectory> > trajectoryCollection(new vector<Trajectory>);
508 
509  // Association map between track and trajectory
510  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
511 
512  // don't waste any time...
513  if ( trajectories.empty() ) {
514  event.put(recHitCollection,instance);
515  event.put(trackExtraCollection,instance);
516  if(theTrajectoryFlag) {
517  event.put(trajectoryCollection,instance);
518  event.put( trajTrackMap, instance );
519  }
520  event.put(trackToTrackmap, instance);
521  return event.put(trackCollection,instance);
522  }
523 
524  LogTrace(metname) << "Create the collection of Tracks";
525 
527  event.getByToken(theBeamSpotToken,beamSpot);
528 
529  reco::TrackRef::key_type trackIndex = 0;
530  // reco::TrackRef::key_type trackUpdatedIndex = 0;
531 
532  reco::TrackExtraRef::key_type trackExtraIndex = 0;
533  TrackingRecHitRef::key_type recHitsIndex = 0;
534 
536  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
537  std::map<unsigned int, unsigned int> tjTkMap;
538 
539  if(doSmoothing) {
541  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
542  theSmoother.reset(aSmoother->clone());
543  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
544  try {
545  std::string theTrackerRecHitBuilderName("WithAngleAndTemplate"); // to be moved to cfg in another PR
546  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
547  theTrackerRecHitBuilder.product();
548  } catch(...) {
549  std::string theTrackerRecHitBuilderName("hltESPTTRHBWithTrackAngle"); // FIXME FIXME
550  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
551  }
552  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
553  theSmoother->setHitCloner(&hitCloner);
554  }
555 
556  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
557  rawTrajectory != trajectories.end(); ++rawTrajectory){
558 
559  reco::TrackRef glbRef;
560  std::vector<std::pair<Trajectory*,reco::TrackRef> >::const_iterator mmit;
561  for(mmit = miniMap.begin();mmit!=miniMap.end();++mmit){
562  if(mmit->first == *rawTrajectory) glbRef = mmit->second;
563  }
564 
565  Trajectory &trajectory = **rawTrajectory;
566 
567  if(doSmoothing){
568  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
569 
570  if(!trajectoriesSM.empty()) {
571  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
572  trajectory = trajectoriesSM.front();
573  trajectory.setSeedRef(tmpSeedRef);
574  } else
575  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
576  }
577 
578  if(theTrajectoryFlag) {
579  trajectoryCollection->push_back(trajectory);
580  iTjRef++;
581  }
582 
583  // get the transient rechit from the trajectory
584  Trajectory::RecHitContainer transHits = trajectory.recHits();
585 
586  if ( trajectory.direction() == oppositeToMomentum)
587  reverse(transHits.begin(),transHits.end());
588 
589  // build the "bare" track from the trajectory.
590  // This track has the parameters defined at PCA (no update)
591  pair<bool,reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
592 
593  // Check if the extrapolation went well
594  if(!resultOfTrackExtrapAtPCA.first) {
595  // ++trackIndex;//ADAM
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  // Fill the track extra with the rec hit (persistent-)reference
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  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
641  trackExtra.add(TrackingRecHitRef(recHitCollectionRefProd, recHitsIndex++ ));
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) tjTkMap[iTjRef-1] = iTkRef-1;
658  }
659 
660 
661 
662  // Put the Collections in the event
663  LogTrace(metname) << "put the Collections in the event";
664  event.put(recHitCollection,instance);
665  event.put(trackExtraCollection,instance);
666 
667  OrphanHandle<reco::TrackCollection> returnTrackHandle;
668  OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
669  if(theUpdatingAtVtx){
670  }
671  else {
672  event.put(trackToTrackmap,instance);
673  returnTrackHandle = event.put(trackCollection,instance);
674  nonUpdatedHandle = returnTrackHandle;
675  }
676 
677  if ( theTrajectoryFlag ) {
678  OrphanHandle<std::vector<Trajectory> > rTrajs = event.put(trajectoryCollection,instance);
679  // Now Create traj<->tracks association map
680  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
681  i != tjTkMap.end(); i++ ) {
682  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
683  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
684  }
685  event.put( trajTrackMap, instance );
686  }
687 
688  return returnTrackHandle;
689 }
690 
691 
692 pair<bool,reco::Track> MuonTrackLoader::buildTrackAtPCA(const Trajectory& trajectory, const reco::BeamSpot &beamSpot) const {
693 
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>
704  extrapolationResult = theUpdatorAtVtx->propagate(innerTSOS, beamSpot);
705  FreeTrajectoryState ftsAtVtx;
706 
707  if(extrapolationResult.first)
708  ftsAtVtx = extrapolationResult.second;
709  else{
710  if(TrackerBounds::isInside(innerTSOS.globalPosition())){
711  LogInfo(metname) << "Track in the Tracker: taking the innermost state instead of the state at PCA";
712  ftsAtVtx = *innerTSOS.freeState();
713  }
714  else{
715  if ( theAllowNoVtxFlag ) {
716  LogInfo(metname) << "Propagation to PCA failed, taking the innermost state instead of the state at PCA";
717  ftsAtVtx = *innerTSOS.freeState();
718  } else {
719  LogInfo(metname) << "Stand Alone track: this track will be rejected";
720  return pair<bool,reco::Track>(false,reco::Track());
721  }
722  }
723  }
724 
725  LogTrace(metname) << "TSOS after the extrapolation at vtx";
726  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
727 
728  GlobalPoint pca = ftsAtVtx.position();
729  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
730  GlobalVector p = ftsAtVtx.momentum();
731  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
732 
733  bool bon = true;
734  if(fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) bon=false;
735  double ndof = trajectory.ndof(bon);
736 
737  reco::Track track(trajectory.chiSquared(),
738  ndof,
739  persistentPCA,
740  persistentMomentum,
741  ftsAtVtx.charge(),
742  ftsAtVtx.curvilinearError());
743 
744  return pair<bool,reco::Track>(true,track);
745 }
746 
747 
748 pair<bool,reco::Track> MuonTrackLoader::buildTrackUpdatedAtPCA(const reco::Track &track, const reco::BeamSpot &beamSpot) const {
749 
750  const string metname = "Muon|RecoMuon|MuonTrackLoader";
752 
753  // build the transient track
754  reco::TransientTrack transientTrack(track,
755  &*theService->magneticField(),
756  theService->trackingGeometry());
757 
758  LogTrace(metname) << "Apply the vertex constraint";
759  pair<bool,FreeTrajectoryState> updateResult = theUpdatorAtVtx->update(transientTrack,beamSpot);
760 
761  if(!updateResult.first){
762  return pair<bool,reco::Track>(false,reco::Track());
763  }
764 
765  LogTrace(metname) << "FTS after the vertex constraint";
766  FreeTrajectoryState &ftsAtVtx = updateResult.second;
767 
768  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
769 
770  GlobalPoint pca = ftsAtVtx.position();
771  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
772  GlobalVector p = ftsAtVtx.momentum();
773  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
774 
775  reco::Track updatedTrack(track.chi2(),
776  track.ndof(),
777  persistentPCA,
778  persistentMomentum,
779  ftsAtVtx.charge(),
780  ftsAtVtx.curvilinearError());
781 
782  return pair<bool,reco::Track>(true,updatedTrack);
783 }
784 
785 
787 
788  const string metname = "Muon|RecoMuon|MuonTrackLoader";
789 
790  const Trajectory::RecHitContainer transRecHits = trajectory.recHits();
791 
792  // put the collection of TrackingRecHit in the event
793 
794  // sets the outermost and innermost TSOSs
795  // FIXME: check it!
796  TrajectoryStateOnSurface outerTSOS;
797  TrajectoryStateOnSurface innerTSOS;
798  unsigned int innerId=0, outerId=0;
800  DetId outerDetId;
801 
802  if (trajectory.direction() == alongMomentum) {
803  LogTrace(metname)<<"alongMomentum";
804  outerTSOS = trajectory.lastMeasurement().updatedState();
805  innerTSOS = trajectory.firstMeasurement().updatedState();
806  outerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
807  innerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
808  outerRecHit = trajectory.lastMeasurement().recHit();
809  outerDetId = trajectory.lastMeasurement().recHit()->geographicalId();
810  }
811  else if (trajectory.direction() == oppositeToMomentum) {
812  LogTrace(metname)<<"oppositeToMomentum";
813  outerTSOS = trajectory.firstMeasurement().updatedState();
814  innerTSOS = trajectory.lastMeasurement().updatedState();
815  outerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
816  innerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
817  outerRecHit = trajectory.firstMeasurement().recHit();
818  outerDetId = trajectory.firstMeasurement().recHit()->geographicalId();
819  }
820  else LogError(metname)<<"Wrong propagation direction!";
821 
822  const GeomDet *outerDet = theService->trackingGeometry()->idToDet(outerDetId);
823  GlobalPoint outerTSOSPos = outerTSOS.globalParameters().position();
824  bool inside = outerDet->surface().bounds().inside(outerDet->toLocal(outerTSOSPos));
825 
826 
827  GlobalPoint hitPos = (outerRecHit->isValid()) ? outerRecHit->globalPosition() : outerTSOS.globalParameters().position() ;
828 
829  if(!inside) {
830  LogTrace(metname)<<"The Global Muon outerMostMeasurementState is not compatible with the recHit detector! Setting outerMost postition to recHit position if recHit isValid: " << outerRecHit->isValid();
831  LogTrace(metname)<<"From " << outerTSOSPos << " to " << hitPos;
832  }
833 
834 
835  //build the TrackExtra
836  GlobalPoint v = (inside) ? outerTSOSPos : hitPos ;
837  GlobalVector p = outerTSOS.globalParameters().momentum();
838  math::XYZPoint outpos( v.x(), v.y(), v.z() );
839  math::XYZVector outmom( p.x(), p.y(), p.z() );
840 
841  v = innerTSOS.globalParameters().position();
842  p = innerTSOS.globalParameters().momentum();
843  math::XYZPoint inpos( v.x(), v.y(), v.z() );
844  math::XYZVector inmom( p.x(), p.y(), p.z() );
845 
846  reco::TrackExtra trackExtra(outpos, outmom, true, inpos, inmom, true,
847  outerTSOS.curvilinearError(), outerId,
848  innerTSOS.curvilinearError(), innerId,
849  trajectory.direction(),trajectory.seedRef());
850 
851  return trackExtra;
852 
853 }
854 
#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
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:280
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
void add(const TrackingRecHitRef &ref)
add a reference to a RecHit
edm::InputTag theBeamSpotInputTag
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:597
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:603
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
virtual ~MuonTrackLoader()
Destructor.
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
double pt() const
track transverse momentum
Definition: TrackBase.h:669
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:394
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