CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/FastSimulation/Tracking/plugins/PixelTracksProducer.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include "FastSimulation/Tracking/plugins/PixelTracksProducer.h"
00003 
00004 #include "FWCore/Framework/interface/Event.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 
00007 #include "DataFormats/Common/interface/Handle.h"
00008 #include "DataFormats/Common/interface/OwnVector.h"
00009 
00010 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00011 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00012 
00013 //Pixel Specific stuff
00014 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegionProducer.h"
00015 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegionProducerFactory.h"
00016 
00017 #include "RecoPixelVertexing/PixelTrackFitting/interface/PixelFitter.h"
00018 #include "RecoPixelVertexing/PixelTrackFitting/interface/PixelFitterFactory.h"
00019 
00020 #include "RecoPixelVertexing/PixelTrackFitting/interface/PixelTrackFilter.h"
00021 #include "RecoPixelVertexing/PixelTrackFitting/interface/PixelTrackFilterFactory.h"
00022 #include "RecoPixelVertexing/PixelTrackFitting/interface/TracksWithHits.h"
00023 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegion.h"
00024 
00025 #include "DataFormats/TrackReco/interface/Track.h"
00026 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00027 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00028 #include "DataFormats/Common/interface/OrphanHandle.h"
00029 #include <vector>
00030 
00031 using namespace pixeltrackfitting;
00032 
00033 PixelTracksProducer::PixelTracksProducer(const edm::ParameterSet& conf) : 
00034   theFitter(0), 
00035   theFilter(0), 
00036   theRegionProducer(0)
00037 {  
00038 
00039   produces<reco::TrackCollection>();
00040   produces<TrackingRecHitCollection>();
00041   produces<reco::TrackExtraCollection>();
00042 
00043   const edm::ParameterSet& regfactoryPSet = conf.getParameter<edm::ParameterSet>("RegionFactoryPSet");
00044   std::string regfactoryName = regfactoryPSet.getParameter<std::string>("ComponentName");
00045   theRegionProducer = TrackingRegionProducerFactory::get()->create(regfactoryName,regfactoryPSet);
00046   
00047   const edm::ParameterSet& fitterPSet = conf.getParameter<edm::ParameterSet>("FitterPSet");
00048   std::string fitterName = fitterPSet.getParameter<std::string>("ComponentName");
00049   theFitter = PixelFitterFactory::get()->create( fitterName, fitterPSet);
00050   
00051   const edm::ParameterSet& filterPSet = conf.getParameter<edm::ParameterSet>("FilterPSet");
00052   std::string filterName = filterPSet.getParameter<std::string>("ComponentName");
00053   theFilter = PixelTrackFilterFactory::get()->create( filterName, filterPSet);
00054   
00055   // The name of the seed producer
00056   seedProducer = conf.getParameter<edm::InputTag>("SeedProducer");
00057 
00058 }
00059 
00060   
00061 // Virtual destructor needed.
00062 PixelTracksProducer::~PixelTracksProducer() {
00063 
00064   delete theFilter;
00065   delete theFitter;
00066   delete theRegionProducer;
00067 
00068 } 
00069  
00070 
00071 // Functions that gets called by framework every event
00072 void 
00073 PixelTracksProducer::produce(edm::Event& e, const edm::EventSetup& es) {        
00074   
00075   std::auto_ptr<reco::TrackCollection> tracks(new reco::TrackCollection);    
00076   std::auto_ptr<TrackingRecHitCollection> recHits(new TrackingRecHitCollection);
00077   std::auto_ptr<reco::TrackExtraCollection> trackExtras(new reco::TrackExtraCollection);
00078   typedef std::vector<const TrackingRecHit *> RecHits;
00079   
00080   TracksWithRecHits pixeltracks;
00081   TracksWithRecHits cleanedTracks;
00082   
00083   edm::Handle<TrajectorySeedCollection> theSeeds;
00084   e.getByLabel(seedProducer,theSeeds);
00085 
00086   // No seed -> output an empty track collection
00087   if(theSeeds->size() == 0) {
00088     e.put(tracks);
00089     e.put(recHits);
00090     e.put(trackExtras);
00091     return;
00092   }
00093   
00094   //only one region Global, but it is called at every event...
00095   //maybe there is a smarter way to set it only once
00096   //NEED TO FIX
00097   typedef std::vector<TrackingRegion* > Regions;
00098   typedef Regions::const_iterator IR;
00099   Regions regions = theRegionProducer->regions(e,es);
00100   for (IR ir=regions.begin(), irEnd=regions.end(); ir < irEnd; ++ir) {
00101     const TrackingRegion & region = **ir;
00102     
00103     // Loop over the seeds
00104     TrajectorySeedCollection::const_iterator aSeed = theSeeds->begin();
00105     TrajectorySeedCollection::const_iterator lastSeed = theSeeds->end();
00106     for ( ; aSeed!=lastSeed; ++aSeed ) { 
00107       
00108       // Find the first hit and last hit of the Seed
00109       TrajectorySeed::range theSeedingRecHitRange = aSeed->recHits();
00110       edm::OwnVector<TrackingRecHit>::const_iterator aSeedingRecHit = theSeedingRecHitRange.first;
00111       edm::OwnVector<TrackingRecHit>::const_iterator theLastSeedingRecHit = theSeedingRecHitRange.second;
00112 
00113       // Loop over the rechits
00114       std::vector<const TrackingRecHit*> TripletHits(3,static_cast<const TrackingRecHit*>(0));
00115       for ( unsigned i=0; aSeedingRecHit!=theLastSeedingRecHit; ++i,++aSeedingRecHit )  
00116         TripletHits[i] = &(*aSeedingRecHit);
00117       
00118       // fitting the triplet
00119       reco::Track* track = theFitter->run(es, TripletHits, region);
00120       
00121       // decide if track should be skipped according to filter 
00122       if ( ! (*theFilter)(track) ) { 
00123         delete track; 
00124         continue; 
00125       }
00126       
00127       // add tracks 
00128       pixeltracks.push_back(TrackWithRecHits(track, TripletHits));
00129       
00130     }
00131   }
00132   
00133   int cc=0;
00134   int nTracks = pixeltracks.size();
00135   for (int i = 0; i < nTracks; ++i) {
00136 
00137     reco::Track* track   =  pixeltracks.at(i).first;
00138     const RecHits & hits = pixeltracks.at(i).second;
00139     
00140     for (unsigned int k = 0; k < hits.size(); k++) {
00141       TrackingRecHit *hit = (hits.at(k))->clone();
00142       track->setHitPattern(*hit, k);
00143       recHits->push_back(hit);
00144     }
00145 
00146     tracks->push_back(*track);
00147     delete track;
00148     
00149   }
00150   
00151   edm::OrphanHandle <TrackingRecHitCollection> ohRH = e.put( recHits );
00152   
00153   for (int k = 0; k < nTracks; ++k) {
00154 
00155     // reco::TrackExtra* theTrackExtra = new reco::TrackExtra();
00156     reco::TrackExtra theTrackExtra;
00157     
00158     //fill the TrackExtra with TrackingRecHitRef
00159     // unsigned int nHits = tracks->at(k).numberOfValidHits();
00160     unsigned nHits = 3; // We are dealing with triplets!
00161     for(unsigned int i = 0; i < nHits; ++i) {
00162       theTrackExtra.add(TrackingRecHitRef(ohRH,cc++));
00163       //theTrackExtra->add(TrackingRecHitRef(ohRH,cc));
00164       //cc++;
00165     }
00166     
00167     trackExtras->push_back(theTrackExtra);
00168     //trackExtras->push_back(*theTrackExtra);
00169     //delete theTrackExtra;
00170   }
00171   
00172   edm::OrphanHandle<reco::TrackExtraCollection> ohTE = e.put(trackExtras);
00173   
00174   for (int k = 0; k < nTracks; k++) {
00175 
00176     const reco::TrackExtraRef theTrackExtraRef(ohTE,k);
00177     (tracks->at(k)).setExtra(theTrackExtraRef);
00178 
00179   }
00180   
00181   e.put(tracks);
00182   
00183   // Avoid a memory leak !
00184   unsigned nRegions = regions.size();
00185   for ( unsigned iRegions=0; iRegions<nRegions; ++iRegions ) {
00186     delete regions[iRegions];
00187   }
00188 
00189 }
00190