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 TrackerTopology& ttopo, const string& instance, bool reallyDoSmoothing) {
127  std::vector<bool> dummyVecBool;
128  return loadTracks(trajectories, event, dummyVecBool, ttopo, instance, reallyDoSmoothing);
129 }
130 
132 MuonTrackLoader::loadTracks(const TrajectoryContainer& trajectories,
133  Event& event, std::vector<bool>& tkBoolVec,
134  const TrackerTopology& ttopo,
135  const string& instance, bool reallyDoSmoothing) {
136 
137  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
138 
139  const string metname = "Muon|RecoMuon|MuonTrackLoader";
140 
141  // the track collectios; they will be loaded in the event
142  auto_ptr<reco::TrackCollection> trackCollection(new reco::TrackCollection());
143  // ... and its reference into the event
144  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
145 
146  // track collection for the tracks updated at vertex
147  auto_ptr<reco::TrackCollection> updatedAtVtxTrackCollection(new reco::TrackCollection());
148  // ... and its (eventually) reference into the event
149  reco::TrackRefProd trackUpdatedCollectionRefProd;
150  if(theUpdatingAtVtx) trackUpdatedCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance+"UpdatedAtVtx");
151 
152  // Association map between updated and non updated at vtx tracks
153  auto_ptr<reco:: TrackToTrackMap> trackToTrackmap(new reco::TrackToTrackMap(trackCollectionRefProd, trackUpdatedCollectionRefProd));
154 
155  // the track extra collection, it will be loaded in the event
156  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
157  // ... and its reference into the event
158  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
159 
160  // the rechit collection, it will be loaded in the event
161  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
162  // ... and its reference into the event
163  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
164 
165  // Collection of Trajectory
166  auto_ptr<vector<Trajectory> > trajectoryCollection(new vector<Trajectory>);
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 
175  // Association map between track and trajectory
176  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
177  event.put( trajTrackMap, instance );
178  }
179  if(theUpdatingAtVtx){
180  event.put(trackToTrackmap);
181  event.put(updatedAtVtxTrackCollection,instance+"UpdatedAtVtx");
182  }
183  return event.put(trackCollection,instance);
184  }
185 
187  event.getByToken(theBeamSpotToken, beamSpot);
188 
189  LogTrace(metname) << "Create the collection of Tracks";
190 
191  reco::TrackRef::key_type trackIndex = 0;
192  reco::TrackRef::key_type trackUpdatedIndex = 0;
193 
194  reco::TrackExtraRef::key_type trackExtraIndex = 0;
195  TrackingRecHitRef::key_type recHitsIndex = 0;
196 
198  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
199  std::map<unsigned int, unsigned int> tjTkMap;
200 
201  if(doSmoothing) {
203  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
204  theSmoother.reset(aSmoother->clone());
205  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
206  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
207  theTrackerRecHitBuilder.product();
208  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
209  theSmoother->setHitCloner(&hitCloner);
210  }
211 
212 
213  unsigned int tjCnt = 0;
214  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
215  rawTrajectory != trajectories.end(); ++rawTrajectory, ++tjCnt){
216 
217  Trajectory &trajectory = **rawTrajectory;
218 
219  if(doSmoothing){
220  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
221 
222  if(!trajectoriesSM.empty()) {
223  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
224  trajectory = trajectoriesSM.front();
225  trajectory.setSeedRef(tmpSeedRef);
226  LogDebug(metname) << "theSeedRef.isNonnull " << trajectory.seedRef().isNonnull();
227  } else
228  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
229  }
230 
231  if(theTrajectoryFlag) {
232  trajectoryCollection->push_back(trajectory);
233  iTjRef++;
234  }
235 
236  // get the transient rechit from the trajectory
237  Trajectory::RecHitContainer transHits = trajectory.recHits();
238 
239  if ( trajectory.direction() == oppositeToMomentum)
240  reverse(transHits.begin(),transHits.end());
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) ++trackIndex;
272  else{
273 
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  // Fill the track extra with the rec hit (persistent-)reference
284  unsigned int nHitsAdded = 0;
285  for (Trajectory::RecHitContainer::const_iterator recHit = transHits.begin(); recHit != transHits.end(); ++recHit) {
286  TrackingRecHit *singleHit = (**recHit).hit()->clone();
287  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
288  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
289  if unlikely(!track.appendHitPattern(**it, ttopo)){
290  break;
291  }
292  }
293 
294  if(theUpdatingAtVtx && updateResult.first){
295  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
296  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
297  if unlikely(!updateResult.second.appendHitPattern(**it, ttopo)){
298  break;
299  }
300  }
301  }
302  recHitCollection->push_back( singleHit );
303  ++nHitsAdded;
304  }
305  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
306  trackExtra.setHits(recHitCollectionRefProd, recHitsIndex, nHitsAdded);
307  recHitsIndex +=nHitsAdded;
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 
351  // Association map between track and trajectory
352  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection(rTrajs, nonUpdatedHandle) );
353 
354  // Now Create traj<->tracks association map
355  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
356  i != tjTkMap.end(); i++ ) {
357  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
358  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
359  }
360  event.put( trajTrackMap, instance );
361  }
362 
363  return returnTrackHandle;
364 }
365 
368  Event& event,
369  const TrackerTopology& ttopo) {
370 
371  const string metname = "Muon|RecoMuon|MuonTrackLoader";
372 
373  // the muon collection, it will be loaded in the event
374  auto_ptr<reco::MuonTrackLinksCollection> trackLinksCollection(new reco::MuonTrackLinksCollection());
375 
376  // don't waste any time...
377  if ( muonCands.empty() ) {
378  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
379  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
380  auto_ptr<reco::TrackCollection> trackCollection( new reco::TrackCollection() );
381 
382  event.put(recHitCollection);
383  event.put(trackExtraCollection);
384  event.put(trackCollection);
385 
386  //need to also put the tracker tracks collection if requested
387  if(thePutTkTrackFlag){
388  //will take care of putting nothing in the event but the empty collection
389  TrajectoryContainer trackerTrajs;
390  loadTracks(trackerTrajs, event, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
391  }
392 
393  return event.put(trackLinksCollection);
394  }
395 
396  // get combined Trajectories
397  TrajectoryContainer combinedTrajs;
398  TrajectoryContainer trackerTrajs;
399  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it) {
400  LogDebug(metname) << "Loader glbSeedRef " << (*it)->trajectory()->seedRef().isNonnull();
401  if ((*it)->trackerTrajectory() ) LogDebug(metname) << " " << "tkSeedRef " << (*it)->trackerTrajectory()->seedRef().isNonnull();
402 
403  combinedTrajs.push_back((*it)->trajectory());
404  if ( thePutTkTrackFlag ) trackerTrajs.push_back((*it)->trackerTrajectory());
405 
406  else {
407  if ((*it)->trackerTrajectory()) delete ((*it)->trackerTrajectory());
408  }
409 
410  // // Create the links between sta and tracker tracks
411  // reco::MuonTrackLinks links;
412  // links.setStandAloneTrack((*it)->muonTrack());
413  // links.setTrackerTrack((*it)->trackerTrack());
414  // trackLinksCollection->push_back(links);
415  // delete *it;
416  }
417 
418  // create the TrackCollection of combined Trajectories
419  // FIXME: could this be done one track at a time in the previous loop?
420  LogTrace(metname) << "Build combinedTracks";
421  std::vector<bool> combTksVec(combinedTrajs.size(), false);
422  OrphanHandle<reco::TrackCollection> combinedTracks = loadTracks(combinedTrajs, event, combTksVec, ttopo);
423 
425  std::vector<bool> trackerTksVec(trackerTrajs.size(), false);
426  if(thePutTkTrackFlag) {
427  LogTrace(metname) << "Build trackerTracks: "
428  << trackerTrajs.size();
429  trackerTracks = loadTracks(trackerTrajs, event, trackerTksVec, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
430  } else {
431  for (TrajectoryContainer::iterator it = trackerTrajs.begin(); it != trackerTrajs.end(); ++it) {
432  if(*it) delete *it;
433  }
434  }
435 
436  LogTrace(metname) << "Set the final links in the MuonTrackLinks collection";
437 
438  unsigned int candposition(0), position(0), tkposition(0);
439  //reco::TrackCollection::const_iterator glIt = combinedTracks->begin(),
440  // glEnd = combinedTracks->end();
441 
442  for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it, ++candposition) {
443 
444  // The presence of the global track determines whether to fill the MuonTrackLinks or not
445  // N.B. We are assuming here that the global tracks in "combinedTracks"
446  // have the same order as the muon candidates in "muonCands"
447  // (except for possible missing tracks), which should always be the case...
448  //if( glIt == glEnd ) break;
449  if(combTksVec[candposition]) {
450  reco::TrackRef combinedTR(combinedTracks, position++);
451  //++glIt;
452 
453  // Create the links between sta and tracker tracks
455  links.setStandAloneTrack((*it)->muonTrack());
456  links.setTrackerTrack((*it)->trackerTrack());
457  links.setGlobalTrack(combinedTR);
458 
459  if(thePutTkTrackFlag && trackerTksVec[candposition]) {
460  reco::TrackRef trackerTR(trackerTracks, tkposition++);
461  links.setTrackerTrack(trackerTR);
462  }
463 
464  trackLinksCollection->push_back(links);
465  }
466 
467  else { // if no global track, still increment the tracker-track counter when appropriate
468  if(thePutTkTrackFlag && trackerTksVec[candposition]) tkposition++;
469  }
470 
471  delete *it;
472  }
473 
474  if( thePutTkTrackFlag && trackerTracks.isValid() && !(combinedTracks->size() > 0 && trackerTracks->size() > 0 ) )
475  LogWarning(metname)<<"The MuonTrackLinkCollection is incomplete";
476 
477  // put the MuonCollection in the event
478  LogTrace(metname) << "put the MuonCollection in the event" << "\n";
479 
480  return event.put(trackLinksCollection);
481 }
482 
484 MuonTrackLoader::loadTracks(const TrajectoryContainer& trajectories,
485  Event& event, const std::vector<std::pair<Trajectory*,reco::TrackRef> >& miniMap, Handle<reco::TrackCollection> const& trackHandle, const TrackerTopology& ttopo, const string& instance, bool reallyDoSmoothing) {
486 
487  const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
488 
489  const string metname = "Muon|RecoMuon|MuonTrackLoader|TevMuonTrackLoader";
490 
491  LogDebug(metname)<<"TeV LoadTracks instance: " << instance;
492 
493  // the track collectios; they will be loaded in the event
494  auto_ptr<reco::TrackCollection> trackCollection(new reco::TrackCollection());
495  // ... and its reference into the event
496  reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
497 
498  // Association map between GlobalMuons and TeVMuons
499  auto_ptr<reco:: TrackToTrackMap> trackToTrackmap(new reco::TrackToTrackMap(trackHandle,trackCollectionRefProd ));
500 
501  // the track extra collection, it will be loaded in the event
502  auto_ptr<reco::TrackExtraCollection> trackExtraCollection(new reco::TrackExtraCollection() );
503  // ... and its reference into the event
504  reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
505 
506  // the rechit collection, it will be loaded in the event
507  auto_ptr<TrackingRecHitCollection> recHitCollection(new TrackingRecHitCollection() );
508  // ... and its reference into the event
509  TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
510 
511  // Collection of Trajectory
512  auto_ptr<vector<Trajectory> > trajectoryCollection(new vector<Trajectory>);
513 
514  // don't waste any time...
515  if ( trajectories.empty() ) {
516  event.put(recHitCollection,instance);
517  event.put(trackExtraCollection,instance);
518  if(theTrajectoryFlag) {
519  event.put(trajectoryCollection,instance);
520 
521  // Association map between track and trajectory
522  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
523  event.put( trajTrackMap, instance );
524  }
525  event.put(trackToTrackmap, instance);
526  return event.put(trackCollection,instance);
527  }
528 
529  LogTrace(metname) << "Create the collection of Tracks";
530 
532  event.getByToken(theBeamSpotToken,beamSpot);
533 
534  reco::TrackRef::key_type trackIndex = 0;
535  // reco::TrackRef::key_type trackUpdatedIndex = 0;
536 
537  reco::TrackExtraRef::key_type trackExtraIndex = 0;
538  TrackingRecHitRef::key_type recHitsIndex = 0;
539 
541  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
542  std::map<unsigned int, unsigned int> tjTkMap;
543 
544  if(doSmoothing) {
546  theService->eventSetup().get<TrajectoryFitter::Record>().get(theSmootherName,aSmoother);
547  theSmoother.reset(aSmoother->clone());
548  edm::ESHandle<TransientTrackingRecHitBuilder> theTrackerRecHitBuilder;
549  try {
550  std::string theTrackerRecHitBuilderName("WithAngleAndTemplate"); // to be moved to cfg in another PR
551  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
552  theTrackerRecHitBuilder.product();
553  } catch(...) {
554  std::string theTrackerRecHitBuilderName("hltESPTTRHBWithTrackAngle"); // FIXME FIXME
555  theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
556  }
557  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product())->cloner();
558  theSmoother->setHitCloner(&hitCloner);
559  }
560 
561  for(TrajectoryContainer::const_iterator rawTrajectory = trajectories.begin();
562  rawTrajectory != trajectories.end(); ++rawTrajectory){
563 
564  reco::TrackRef glbRef;
565  std::vector<std::pair<Trajectory*,reco::TrackRef> >::const_iterator mmit;
566  for(mmit = miniMap.begin();mmit!=miniMap.end();++mmit){
567  if(mmit->first == *rawTrajectory) glbRef = mmit->second;
568  }
569 
570  Trajectory &trajectory = **rawTrajectory;
571 
572  if(doSmoothing){
573  vector<Trajectory> trajectoriesSM = theSmoother->trajectories(**rawTrajectory);
574 
575  if(!trajectoriesSM.empty()) {
576  const edm::RefToBase<TrajectorySeed> tmpSeedRef = (**rawTrajectory).seedRef();
577  trajectory = trajectoriesSM.front();
578  trajectory.setSeedRef(tmpSeedRef);
579  } else
580  LogInfo(metname)<<"The trajectory has not been smoothed!"<<endl;
581  }
582 
583  if(theTrajectoryFlag) {
584  trajectoryCollection->push_back(trajectory);
585  iTjRef++;
586  }
587 
588  // get the transient rechit from the trajectory
589  Trajectory::RecHitContainer transHits = trajectory.recHits();
590 
591  if ( trajectory.direction() == oppositeToMomentum)
592  reverse(transHits.begin(),transHits.end());
593 
594  // build the "bare" track from the trajectory.
595  // This track has the parameters defined at PCA (no update)
596  pair<bool,reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
597 
598  // Check if the extrapolation went well
599  if(!resultOfTrackExtrapAtPCA.first) {
600  // ++trackIndex;//ADAM
601  delete *rawTrajectory;
602  continue;
603  }
604 
605  // take the "bare" track at PCA
606  reco::Track &track = resultOfTrackExtrapAtPCA.second;
607 
608  // build the "bare" track extra from the trajectory
609  reco::TrackExtra trackExtra = buildTrackExtra( trajectory );
610 
611  // get the TrackExtraRef (persitent reference of the track extra)
612  reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++ );
613 
614  // set the persistent track-extra reference to the Track
615  track.setExtra(trackExtraRef);
616 
617  // Fill the map
618  trackToTrackmap->insert(glbRef,
619  reco::TrackRef(trackCollectionRefProd,trackIndex++));
620 
621  // build the updated-at-vertex track, starting from the previous track
622  pair<bool,reco::Track> updateResult(false,reco::Track());
623 
624  // Fill the track extra with the rec hit (persistent-)reference
625  unsigned int nHitsAdded = 0;
626  for (Trajectory::RecHitContainer::const_iterator recHit = transHits.begin();
627  recHit != transHits.end(); ++recHit) {
628  TrackingRecHit *singleHit = (**recHit).hit()->clone();
629 
630  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
631  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
632  if unlikely(!track.appendHitPattern(**it, ttopo)){
633  break;
634  }
635  }
636 
637  if(theUpdatingAtVtx && updateResult.first){
638  std::vector<const TrackingRecHit*> hits = MuonTrackLoader::unpackHit(*singleHit);
639  for (std::vector<const TrackingRecHit*>::const_iterator it = hits.begin(); it != hits.end(); ++it) {
640  if unlikely(!updateResult.second.appendHitPattern(**it, ttopo)){
641  break;
642  }
643  }
644  }
645  recHitCollection->push_back( singleHit );
646  ++nHitsAdded;
647  }
648  // set the TrackingRecHitRef (persitent reference of the tracking rec hits)
649  trackExtra.setHits(recHitCollectionRefProd, recHitsIndex, nHitsAdded);
650  recHitsIndex += nHitsAdded;
651 
652  // fill the TrackExtraCollection
653  trackExtraCollection->push_back(trackExtra);
654 
655  // fill the TrackCollection
656  trackCollection->push_back(track);
657  iTkRef++;
658  LogTrace(metname) << "Debug Track being loaded pt "<< track.pt();
659 
660  // We don't need the original trajectory anymore.
661  // It has been copied by value in the trajectoryCollection, if
662  // it is required to put it into the event.
663  delete *rawTrajectory;
664 
665  if(theTrajectoryFlag) tjTkMap[iTjRef-1] = iTkRef-1;
666  }
667 
668 
669 
670  // Put the Collections in the event
671  LogTrace(metname) << "put the Collections in the event";
672  event.put(recHitCollection,instance);
673  event.put(trackExtraCollection,instance);
674 
675  OrphanHandle<reco::TrackCollection> returnTrackHandle;
676  OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
677  if(theUpdatingAtVtx){
678  }
679  else {
680  event.put(trackToTrackmap,instance);
681  returnTrackHandle = event.put(trackCollection,instance);
682  nonUpdatedHandle = returnTrackHandle;
683  }
684 
685  if ( theTrajectoryFlag ) {
686  OrphanHandle<std::vector<Trajectory> > rTrajs = event.put(trajectoryCollection,instance);
687 
688  // Association map between track and trajectory
689  std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection(rTrajs, nonUpdatedHandle) );
690 
691  // Now Create traj<->tracks association map
692  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
693  i != tjTkMap.end(); i++ ) {
694  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
695  edm::Ref<reco::TrackCollection>( nonUpdatedHandle, (*i).second ) );
696  }
697  event.put( trajTrackMap, instance );
698  }
699 
700  return returnTrackHandle;
701 }
702 
703 
704 pair<bool,reco::Track> MuonTrackLoader::buildTrackAtPCA(const Trajectory& trajectory, const reco::BeamSpot &beamSpot) const {
705 
706  const string metname = "Muon|RecoMuon|MuonTrackLoader";
707 
709 
710  // FIXME: check the prop direction
711  TrajectoryStateOnSurface innerTSOS = trajectory.geometricalInnermostState();
712 
713  // This is needed to extrapolate the tsos at vertex
714  LogTrace(metname) << "Propagate to PCA...";
715  pair<bool,FreeTrajectoryState>
716  extrapolationResult = theUpdatorAtVtx->propagate(innerTSOS, beamSpot);
717  FreeTrajectoryState ftsAtVtx;
718 
719  if(extrapolationResult.first)
720  ftsAtVtx = extrapolationResult.second;
721  else{
722  if(TrackerBounds::isInside(innerTSOS.globalPosition())){
723  LogInfo(metname) << "Track in the Tracker: taking the innermost state instead of the state at PCA";
724  ftsAtVtx = *innerTSOS.freeState();
725  }
726  else{
727  if ( theAllowNoVtxFlag ) {
728  LogInfo(metname) << "Propagation to PCA failed, taking the innermost state instead of the state at PCA";
729  ftsAtVtx = *innerTSOS.freeState();
730  } else {
731  LogInfo(metname) << "Stand Alone track: this track will be rejected";
732  return pair<bool,reco::Track>(false,reco::Track());
733  }
734  }
735  }
736 
737  LogTrace(metname) << "TSOS after the extrapolation at vtx";
738  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
739 
740  GlobalPoint pca = ftsAtVtx.position();
741  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
742  GlobalVector p = ftsAtVtx.momentum();
743  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
744 
745  bool bon = true;
746  if(fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) bon=false;
747  double ndof = trajectory.ndof(bon);
748 
749  reco::Track track(trajectory.chiSquared(),
750  ndof,
751  persistentPCA,
752  persistentMomentum,
753  ftsAtVtx.charge(),
754  ftsAtVtx.curvilinearError());
755 
756  return pair<bool,reco::Track>(true,track);
757 }
758 
759 
760 pair<bool,reco::Track> MuonTrackLoader::buildTrackUpdatedAtPCA(const reco::Track &track, const reco::BeamSpot &beamSpot) const {
761 
762  const string metname = "Muon|RecoMuon|MuonTrackLoader";
764 
765  // build the transient track
766  reco::TransientTrack transientTrack(track,
767  &*theService->magneticField(),
768  theService->trackingGeometry());
769 
770  LogTrace(metname) << "Apply the vertex constraint";
771  pair<bool,FreeTrajectoryState> updateResult = theUpdatorAtVtx->update(transientTrack,beamSpot);
772 
773  if(!updateResult.first){
774  return pair<bool,reco::Track>(false,reco::Track());
775  }
776 
777  LogTrace(metname) << "FTS after the vertex constraint";
778  FreeTrajectoryState &ftsAtVtx = updateResult.second;
779 
780  LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
781 
782  GlobalPoint pca = ftsAtVtx.position();
783  math::XYZPoint persistentPCA(pca.x(),pca.y(),pca.z());
784  GlobalVector p = ftsAtVtx.momentum();
785  math::XYZVector persistentMomentum(p.x(),p.y(),p.z());
786 
787  reco::Track updatedTrack(track.chi2(),
788  track.ndof(),
789  persistentPCA,
790  persistentMomentum,
791  ftsAtVtx.charge(),
792  ftsAtVtx.curvilinearError());
793 
794  return pair<bool,reco::Track>(true,updatedTrack);
795 }
796 
797 
799 
800  const string metname = "Muon|RecoMuon|MuonTrackLoader";
801 
802  const Trajectory::RecHitContainer transRecHits = trajectory.recHits();
803 
804  // put the collection of TrackingRecHit in the event
805 
806  // sets the outermost and innermost TSOSs
807  // FIXME: check it!
808  TrajectoryStateOnSurface outerTSOS;
809  TrajectoryStateOnSurface innerTSOS;
810  unsigned int innerId=0, outerId=0;
812  DetId outerDetId;
813 
814  if (trajectory.direction() == alongMomentum) {
815  LogTrace(metname)<<"alongMomentum";
816  outerTSOS = trajectory.lastMeasurement().updatedState();
817  innerTSOS = trajectory.firstMeasurement().updatedState();
818  outerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
819  innerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
820  outerRecHit = trajectory.lastMeasurement().recHit();
821  outerDetId = trajectory.lastMeasurement().recHit()->geographicalId();
822  }
823  else if (trajectory.direction() == oppositeToMomentum) {
824  LogTrace(metname)<<"oppositeToMomentum";
825  outerTSOS = trajectory.firstMeasurement().updatedState();
826  innerTSOS = trajectory.lastMeasurement().updatedState();
827  outerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
828  innerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
829  outerRecHit = trajectory.firstMeasurement().recHit();
830  outerDetId = trajectory.firstMeasurement().recHit()->geographicalId();
831  }
832  else LogError(metname)<<"Wrong propagation direction!";
833 
834  const GeomDet *outerDet = theService->trackingGeometry()->idToDet(outerDetId);
835  GlobalPoint outerTSOSPos = outerTSOS.globalParameters().position();
836  bool inside = outerDet->surface().bounds().inside(outerDet->toLocal(outerTSOSPos));
837 
838 
839  GlobalPoint hitPos = (outerRecHit->isValid()) ? outerRecHit->globalPosition() : outerTSOS.globalParameters().position() ;
840 
841  if(!inside) {
842  LogTrace(metname)<<"The Global Muon outerMostMeasurementState is not compatible with the recHit detector! Setting outerMost postition to recHit position if recHit isValid: " << outerRecHit->isValid();
843  LogTrace(metname)<<"From " << outerTSOSPos << " to " << hitPos;
844  }
845 
846 
847  //build the TrackExtra
848  GlobalPoint v = (inside) ? outerTSOSPos : hitPos ;
849  GlobalVector p = outerTSOS.globalParameters().momentum();
850  math::XYZPoint outpos( v.x(), v.y(), v.z() );
851  math::XYZVector outmom( p.x(), p.y(), p.z() );
852 
853  v = innerTSOS.globalParameters().position();
854  p = innerTSOS.globalParameters().momentum();
855  math::XYZPoint inpos( v.x(), v.y(), v.z() );
856  math::XYZVector inmom( p.x(), p.y(), p.z() );
857 
858  reco::TrackExtra trackExtra(outpos, outmom, true, inpos, inmom, true,
859  outerTSOS.curvilinearError(), outerId,
860  innerTSOS.curvilinearError(), innerId,
861  trajectory.direction(),trajectory.seedRef());
862 
863  return trackExtra;
864 
865 }
866 
#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:14
T y() const
Definition: PV3DBase.h:63
std::string theSmootherName
std::pair< bool, FreeTrajectoryState > propagate(const TrajectoryStateOnSurface &tsos, const reco::BeamSpot &beamSpot) const
Propagate the state to the 2D-PCA.
GlobalPoint globalPosition() const
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h: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:330
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:536
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:542
virtual ~MuonTrackLoader()
Destructor.
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
double pt() const
track transverse momentum
Definition: TrackBase.h:608
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)
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
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
bool appendHitPattern(const TrackingRecHit &hit, const TrackerTopology &ttopo)
append a single hit to the HitPattern
Definition: TrackBase.h:447
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:168