00001 #include <memory>
00002
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "DataFormats/Common/interface/OwnVector.h"
00010 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
00011 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00012 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerGSRecHit2DCollection.h"
00013 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerGSMatchedRecHit2DCollection.h"
00014 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00015 #include "DataFormats/TrackReco/interface/Track.h"
00016 #include "DataFormats/TrackReco/interface/TrackExtraFwd.h"
00017
00018 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00019 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00020
00021 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00022 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00023
00024 #include "FastSimulation/Tracking/interface/TrackerRecHit.h"
00025
00026
00027 #include "FastSimulation/Tracking/plugins/TrackCandidateProducer.h"
00028
00029 #include <vector>
00030 #include <map>
00031
00032
00033
00034
00035
00036 TrackCandidateProducer::TrackCandidateProducer(const edm::ParameterSet& conf)
00037 {
00038 #ifdef FAMOS_DEBUG
00039 std::cout << "TrackCandidateProducer created" << std::endl;
00040 #endif
00041
00042
00043 produces<TrackCandidateCollection>();
00044
00045
00046
00047 produces<reco::TrackCollection>();
00048 produces<TrackingRecHitCollection>();
00049 produces<reco::TrackExtraCollection>();
00050 produces<std::vector<Trajectory> >();
00051 produces<TrajTrackAssociationCollection>();
00052
00053
00054 seedProducer = conf.getParameter<edm::InputTag>("SeedProducer");
00055
00056
00057 hitProducer = conf.getParameter<edm::InputTag>("HitProducer");
00058
00059
00060
00061 trackProducers = conf.getParameter<std::vector<edm::InputTag> >("TrackProducers");
00062
00063
00064 keepFittedTracks = conf.getParameter<bool>("KeepFittedTracks");
00065
00066
00067 minNumberOfCrossedLayers = conf.getParameter<unsigned int>("MinNumberOfCrossedLayers");
00068
00069
00070 maxNumberOfCrossedLayers = conf.getParameter<unsigned int>("MaxNumberOfCrossedLayers");
00071
00072
00073 rejectOverlaps = conf.getParameter<bool>("OverlapCleaning");
00074
00075
00076 splitHits = conf.getParameter<bool>("SplitHits");
00077
00078
00079
00080 seedCleaning = conf.getParameter<bool>("SeedCleaning");
00081
00082 }
00083
00084
00085
00086 TrackCandidateProducer::~TrackCandidateProducer() {
00087
00088
00089 #ifdef FAMOS_DEBUG
00090 std::cout << "TrackCandidateProducer destructed" << std::endl;
00091 #endif
00092
00093 }
00094
00095 void
00096 TrackCandidateProducer::beginRun(edm::Run & run, const edm::EventSetup & es) {
00097
00098
00099
00100
00101 edm::ESHandle<TrackerGeometry> geometry;
00102
00103
00104 es.get<TrackerDigiGeometryRecord>().get(geometry);
00105
00106 theGeometry = &(*geometry);
00107
00108 }
00109
00110
00111 void
00112 TrackCandidateProducer::produce(edm::Event& e, const edm::EventSetup& es) {
00113
00114 #ifdef FAMOS_DEBUG
00115 std::cout << "################################################################" << std::endl;
00116 std::cout << " TrackCandidateProducer produce init " << std::endl;
00117 #endif
00118
00119
00120 typedef std::pair<reco::TrackRef,edm::Ref<std::vector<Trajectory> > > TrackPair;
00121 typedef std::map<unsigned,TrackPair> TrackMap;
00122
00123
00124 std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
00125 std::auto_ptr<reco::TrackCollection> recoTracks(new reco::TrackCollection);
00126 std::auto_ptr<TrackingRecHitCollection> recoHits(new TrackingRecHitCollection);
00127 std::auto_ptr<reco::TrackExtraCollection> recoTrackExtras(new reco::TrackExtraCollection);
00128 std::auto_ptr<std::vector<Trajectory> > recoTrajectories(new std::vector<Trajectory> );
00129 std::auto_ptr<TrajTrackAssociationCollection> recoTrajTrackMap( new TrajTrackAssociationCollection() );
00130
00131
00132
00133 edm::Handle<edm::View<TrajectorySeed> > theSeeds;
00134 e.getByLabel(seedProducer,theSeeds);
00135
00136
00137 if(theSeeds->size() == 0) {
00138 e.put(output);
00139 e.put(recoTracks);
00140 e.put(recoHits);
00141 e.put(recoTrackExtras);
00142 e.put(recoTrajectories);
00143 e.put(recoTrajTrackMap);
00144 return;
00145 }
00146
00147
00148
00149 edm::Handle<SiTrackerGSMatchedRecHit2DCollection> theGSRecHits;
00150 e.getByLabel(hitProducer, theGSRecHits);
00151
00152
00153
00154
00155
00156
00157
00158
00159 std::vector<edm::Handle<reco::TrackCollection> > theTrackCollections;
00160 std::vector<edm:: Handle<std::vector<Trajectory> > > theTrajectoryCollections;
00161 std::vector<edm::Handle<TrajTrackAssociationCollection> > theAssoMaps;
00162 std::vector<bool> isTrackCollections;
00163 TrajTrackAssociationCollection::const_iterator anAssociation;
00164 TrajTrackAssociationCollection::const_iterator lastAssociation;
00165 TrackMap theTrackMap;
00166 unsigned nCollections = trackProducers.size();
00167 unsigned nRecoHits = 0;
00168
00169 if ( nCollections ) {
00170 theTrackCollections.resize(nCollections);
00171 theTrajectoryCollections.resize(nCollections);
00172 theAssoMaps.resize(nCollections);
00173 isTrackCollections.resize(nCollections);
00174 for ( unsigned tprod=0; tprod < nCollections; ++tprod ) {
00175 isTrackCollections[tprod] = e.getByLabel(trackProducers[tprod],theTrackCollections[tprod]);
00176
00177 if ( isTrackCollections[tprod] ) {
00178
00179 reco::TrackCollection::const_iterator aTrack = theTrackCollections[tprod]->begin();
00180 reco::TrackCollection::const_iterator lastTrack = theTrackCollections[tprod]->end();
00181
00182 for ( ; aTrack!=lastTrack; ++aTrack ) nRecoHits+= aTrack->recHitsSize();
00183 e.getByLabel(trackProducers[tprod],theTrajectoryCollections[tprod]);
00184 e.getByLabel(trackProducers[tprod],theAssoMaps[tprod]);
00185
00186 anAssociation = theAssoMaps[tprod]->begin();
00187 lastAssociation = theAssoMaps[tprod]->end();
00188 #ifdef FAMOS_DEBUG
00189 std::cout << "Input Track Producer " << tprod << " : " << trackProducers[tprod] << std::endl;
00190 std::cout << "List of tracks already reconstructed " << std::endl;
00191 #endif
00192
00193 for ( ; anAssociation != lastAssociation; ++anAssociation ) {
00194 edm::Ref<std::vector<Trajectory> > aTrajectoryRef = anAssociation->key;
00195 reco::TrackRef aTrackRef = anAssociation->val;
00196
00197 int recoTrackId = findId(*aTrackRef);
00198 if ( recoTrackId < 0 ) continue;
00199 #ifdef FAMOS_DEBUG
00200 std::cout << recoTrackId << " ";
00201 #endif
00202
00203 theTrackMap[recoTrackId] = TrackPair(aTrackRef,aTrajectoryRef);
00204 }
00205 #ifdef FAMOS_DEBUG
00206 std::cout << std::endl;
00207 #endif
00208 }
00209 }
00210
00211 recoHits->reserve(nRecoHits);
00212 }
00213
00214
00215 int currentTrackId = -1;
00216
00217
00218
00219
00220
00221
00222
00223 #ifdef FAMOS_DEBUG
00224 std::cout << "Input seed Producer : " << seedProducer << std::endl;
00225 std::cout << "Number of seeds : " << theSeeds->size() << std::endl;
00226 #endif
00227 unsigned seed_size = theSeeds->size();
00228 for (unsigned seednr = 0; seednr < seed_size; ++seednr){
00229
00230
00231 const BasicTrajectorySeed* aSeed = &((*theSeeds)[seednr]);
00232
00233
00234 TrajectorySeed::range theSeedingRecHitRange = aSeed->recHits();
00235
00236
00237 const SiTrackerGSMatchedRecHit2D * theFirstSeedingRecHit =
00238 (const SiTrackerGSMatchedRecHit2D*) (&(*(theSeedingRecHitRange.first)));
00239
00240 TrackerRecHit theFirstSeedingTrackerRecHit(theFirstSeedingRecHit,theGeometry);
00241
00242
00243
00244 int simTrackId = theFirstSeedingRecHit->simtrackId();
00245
00246
00247
00248
00249
00250
00251 if ( seedCleaning && currentTrackId == simTrackId ) continue;
00252 currentTrackId = simTrackId;
00253
00254
00255 std::vector<TrackerRecHit> theTrackerRecHits;
00256
00257 unsigned theNumberOfCrossedLayers = 0;
00258
00259
00260 TrackMap::const_iterator theTrackIt = theTrackMap.find(simTrackId);
00261
00262 if ( nCollections && theTrackIt != theTrackMap.end() ) {
00263
00264 if ( keepFittedTracks ) {
00265
00266 #ifdef FAMOS_DEBUG
00267 std::cout << "Track " << simTrackId << " already reconstructed -> copy it" << std::endl;
00268 #endif
00269
00270 reco::TrackRef aTrackRef = theTrackIt->second.first;
00271 edm::Ref<std::vector<Trajectory> > aTrajectoryRef = theTrackIt->second.second;
00272
00273
00274 reco::Track aRecoTrack(*aTrackRef);
00275 recoTracks->push_back(aRecoTrack);
00276
00277
00278 unsigned nh = aRecoTrack.recHitsSize();
00279 for ( unsigned ih=0; ih<nh; ++ih ) {
00280 TrackingRecHit *hit = aRecoTrack.recHit(ih)->clone();
00281 recoHits->push_back(hit);
00282 }
00283
00284
00285 recoTrajectories->push_back(*aTrajectoryRef);
00286
00287 } else {
00288
00289 #ifdef FAMOS_DEBUG
00290 std::cout << "Track " << simTrackId << " already reconstructed -> ignore it" << std::endl;
00291 #endif
00292
00293 }
00294
00295
00296
00297 } else {
00298
00299 #ifdef FAMOS_DEBUG
00300 std::cout << "Track " << simTrackId << " will return a track candidate" << std::endl;
00301 #endif
00302
00303 SiTrackerGSMatchedRecHit2DCollection::range theRecHitRange = theGSRecHits->get(simTrackId);
00304 SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorBegin = theRecHitRange.first;
00305 SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorEnd = theRecHitRange.second;
00306 SiTrackerGSMatchedRecHit2DCollection::const_iterator iterRecHit;
00307
00308 bool firstRecHit = true;
00309
00310 TrackerRecHit theCurrentRecHit, thePreviousRecHit;
00311
00312 TrackerRecHit theFirstHitComp, theSecondHitComp;
00313
00314 for ( iterRecHit = theRecHitRangeIteratorBegin;
00315 iterRecHit != theRecHitRangeIteratorEnd;
00316 ++iterRecHit) {
00317
00318
00319 if ( theNumberOfCrossedLayers >= maxNumberOfCrossedLayers ) continue;
00320
00321
00322 thePreviousRecHit = theCurrentRecHit;
00323 theCurrentRecHit = TrackerRecHit(&(*iterRecHit),theGeometry);
00324
00325
00326 if ( firstRecHit && theCurrentRecHit != theFirstSeedingTrackerRecHit ) continue;
00327
00328
00329 if ( !theCurrentRecHit.isOnTheSameLayer(thePreviousRecHit) )
00330 ++theNumberOfCrossedLayers;
00331
00332
00333
00334 if ( !rejectOverlaps || firstRecHit ) {
00335
00336
00337 if ( splitHits && theCurrentRecHit.matchedHit()->isMatched() ) {
00338
00339 addSplitHits(theCurrentRecHit,theTrackerRecHits);
00340
00341
00342 } else {
00343
00344 theTrackerRecHits.push_back(theCurrentRecHit);
00345
00346 }
00347
00348 firstRecHit = false;
00349
00350
00351
00352 } else {
00353
00354
00355 if ( theCurrentRecHit.subDetId() != thePreviousRecHit.subDetId() ||
00356 theCurrentRecHit.layerNumber() != thePreviousRecHit.layerNumber() ) {
00357
00358
00359 if ( splitHits && theCurrentRecHit.matchedHit()->isMatched() ) {
00360
00361 addSplitHits(theCurrentRecHit,theTrackerRecHits);
00362
00363
00364 } else {
00365
00366 theTrackerRecHits.push_back(theCurrentRecHit);
00367
00368 }
00369
00370
00371 } else if ( theCurrentRecHit.localError() < thePreviousRecHit.localError() ) {
00372
00373
00374 if( splitHits && theCurrentRecHit.matchedHit()->isMatched() ){
00375
00376
00377 theTrackerRecHits.pop_back();
00378 if ( thePreviousRecHit.matchedHit()->isMatched() ) theTrackerRecHits.pop_back();
00379
00380
00381 addSplitHits(theCurrentRecHit,theTrackerRecHits);
00382
00383
00384 } else {
00385
00386
00387 theTrackerRecHits.back() = theCurrentRecHit;
00388
00389 }
00390
00391 } else {
00392
00393 }
00394
00395 }
00396
00397 }
00398
00399 }
00400
00401 #ifdef FAMOS_DEBUG
00402 std::cout << "Hit number " << theTrackerRecHits.size() << std::endl;
00403 #endif
00404
00405
00406
00407 edm::OwnVector<TrackingRecHit> recHits;
00408 unsigned nTrackerHits = theTrackerRecHits.size();
00409 recHits.reserve(nTrackerHits);
00410
00411 for ( unsigned ih=0; ih<nTrackerHits; ++ih ) {
00412 TrackingRecHit* aTrackingRecHit = theTrackerRecHits[ih].hit()->clone();
00413 recHits.push_back(aTrackingRecHit);
00414
00415 #ifdef FAMOS_DEBUG
00416 const DetId& detId = theTrackerRecHits[ih].hit()->geographicalId();
00417 std::cout << "Added RecHit from detid " << detId.rawId()
00418 << " subdet = " << theTrackerRecHits[ih].subDetId()
00419 << " layer = " << theTrackerRecHits[ih].layerNumber()
00420 << " ring = " << theTrackerRecHits[ih].ringNumber()
00421 << " error = " << theTrackerRecHits[ih].localError()
00422 << std::endl;
00423
00424 std::cout << "Track/z/r : "
00425 << simTrackId << " "
00426 << theTrackerRecHits[ih].globalPosition().z() << " "
00427 << theTrackerRecHits[ih].globalPosition().perp() << std::endl;
00428 if ( theTrackerRecHits[ih].matchedHit() && theTrackerRecHits[ih].matchedHit()->isMatched() )
00429 std::cout << "Matched : " << theTrackerRecHits[ih].matchedHit()->isMatched()
00430 << "Rphi Hit = " << theTrackerRecHits[ih].matchedHit()->monoHit()->simhitId()
00431 << "Stereo Hit = " << theTrackerRecHits[ih].matchedHit()->stereoHit()->simhitId()
00432 <<std::endl;
00433
00434 #endif
00435 }
00436
00437
00438 if ( theNumberOfCrossedLayers < minNumberOfCrossedLayers ) continue;
00439
00440
00441
00442 TrackCandidate
00443 newTrackCandidate(recHits,
00444 *aSeed,
00445 aSeed->startingState(),
00446 edm::RefToBase<TrajectorySeed>(theSeeds,seednr));
00447
00448
00449
00450 #ifdef FAMOS_DEBUG
00451
00452 std::cout << "\tSeed Information " << std::endl;
00453 std::cout << "\tSeed Direction = " << aSeed->direction() << std::endl;
00454 std::cout << "\tSeed StartingDet = " << aSeed->startingState().detId() << std::endl;
00455
00456 std::cout << "\tTrajectory Parameters "
00457 << std::endl;
00458 std::cout << "\t\t detId = "
00459 << newTrackCandidate.trajectoryStateOnDet().detId()
00460 << std::endl;
00461 std::cout << "\t\t loc.px = "
00462 << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().x()
00463 << std::endl;
00464 std::cout << "\t\t loc.py = "
00465 << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().y()
00466 << std::endl;
00467 std::cout << "\t\t loc.pz = "
00468 << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().z()
00469 << std::endl;
00470 std::cout << "\t\t error = ";
00471 for(std::vector< float >::const_iterator iElement = newTrackCandidate.trajectoryStateOnDet().errorMatrix().begin();
00472 iElement < newTrackCandidate.trajectoryStateOnDet().errorMatrix().end();
00473 ++iElement) {
00474 std::cout << "\t" << *iElement;
00475 }
00476 std::cout << std::endl;
00477 #endif
00478
00479 output->push_back(newTrackCandidate);
00480
00481 }
00482
00483
00484 #ifdef FAMOS_DEBUG
00485 std::cout << "Saving "
00486 << output->size() << " track candidates and "
00487 << recoTracks->size() << " reco::Tracks " << std::endl;
00488 #endif
00489
00490 e.put(output);
00491
00492
00493
00494
00495
00496 edm::OrphanHandle <TrackingRecHitCollection> theRecoHits = e.put(recoHits );
00497
00498
00499 unsigned hits=0;
00500 unsigned nTracks = recoTracks->size();
00501 recoTrackExtras->reserve(nTracks);
00502 for ( unsigned index = 0; index < nTracks; ++index ) {
00503
00504 reco::Track& aTrack = recoTracks->at(index);
00505 reco::TrackExtra aTrackExtra(aTrack.outerPosition(),
00506 aTrack.outerMomentum(),
00507 aTrack.outerOk(),
00508 aTrack.innerPosition(),
00509 aTrack.innerMomentum(),
00510 aTrack.innerOk(),
00511 aTrack.outerStateCovariance(),
00512 aTrack.outerDetId(),
00513 aTrack.innerStateCovariance(),
00514 aTrack.innerDetId(),
00515 aTrack.seedDirection(),
00516 aTrack.seedRef());
00517
00518 unsigned nHits = aTrack.recHitsSize();
00519 for ( unsigned int ih=0; ih<nHits; ++ih) {
00520 aTrackExtra.add(TrackingRecHitRef(theRecoHits,hits++));
00521 }
00522 recoTrackExtras->push_back(aTrackExtra);
00523 }
00524
00525
00526
00527 edm::OrphanHandle<reco::TrackExtraCollection> theRecoTrackExtras = e.put(recoTrackExtras);
00528
00529
00530 for ( unsigned index = 0; index<nTracks; ++index ) {
00531 const reco::TrackExtraRef theTrackExtraRef(theRecoTrackExtras,index);
00532 (recoTracks->at(index)).setExtra(theTrackExtraRef);
00533 }
00534
00535
00536 edm::OrphanHandle<reco::TrackCollection> theRecoTracks = e.put(recoTracks);
00537
00538
00539 edm::OrphanHandle<std::vector<Trajectory> > theRecoTrajectories = e.put(recoTrajectories);
00540
00541
00542 for ( unsigned index = 0; index<nTracks; ++index ) {
00543 edm::Ref<std::vector<Trajectory> > trajRef( theRecoTrajectories, index );
00544 edm::Ref<reco::TrackCollection> tkRef( theRecoTracks, index );
00545 recoTrajTrackMap->insert(trajRef,tkRef);
00546 }
00547
00548
00549 e.put(recoTrajTrackMap);
00550
00551 }
00552
00553 int
00554 TrackCandidateProducer::findId(const reco::Track& aTrack) const {
00555 int trackId = -1;
00556 trackingRecHit_iterator aHit = aTrack.recHitsBegin();
00557 trackingRecHit_iterator lastHit = aTrack.recHitsEnd();
00558 for ( ; aHit!=lastHit; ++aHit ) {
00559 if ( !aHit->get()->isValid() ) continue;
00560
00561 const SiTrackerGSMatchedRecHit2D * rechit = (const SiTrackerGSMatchedRecHit2D*) (aHit->get());
00562 trackId = rechit->simtrackId();
00563 break;
00564 }
00565 return trackId;
00566 }
00567
00568 void
00569 TrackCandidateProducer::addSplitHits(const TrackerRecHit& theCurrentRecHit,
00570 std::vector<TrackerRecHit>& theTrackerRecHits) {
00571
00572 const SiTrackerGSRecHit2D* mHit = theCurrentRecHit.matchedHit()->monoHit();
00573 const SiTrackerGSRecHit2D* sHit = theCurrentRecHit.matchedHit()->stereoHit();
00574
00575
00576 if( mHit->simhitId() < sHit->simhitId() ) {
00577
00578 theTrackerRecHits.push_back(TrackerRecHit(mHit,theCurrentRecHit));
00579 theTrackerRecHits.push_back(TrackerRecHit(sHit,theCurrentRecHit));
00580
00581 } else {
00582
00583 theTrackerRecHits.push_back(TrackerRecHit(sHit,theCurrentRecHit));
00584 theTrackerRecHits.push_back(TrackerRecHit(mHit,theCurrentRecHit));
00585
00586 }
00587
00588 }