CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/FastSimulation/Muons/plugins/FastTSGFromL2Muon.cc

Go to the documentation of this file.
00001 #include "FWCore/Framework/interface/Event.h"
00002 
00003 #include "DataFormats/MuonSeed/interface/L3MuonTrajectorySeedCollection.h"
00004 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerGSMatchedRecHit2DCollection.h" 
00005 #include "DataFormats/TrackReco/interface/Track.h"
00006 #include "DataFormats/Math/interface/deltaPhi.h"
00007 
00008 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00009 
00010 #include "RecoTracker/TkTrackingRegions/interface/RectangularEtaPhiTrackingRegion.h"
00011 
00012 #include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
00013 #include "RecoMuon/GlobalTrackingTools/interface/MuonTrackingRegionBuilder.h"
00014 
00015 #include "FastSimulation/Muons/plugins/FastTSGFromL2Muon.h"
00016 
00017 //#include <TH1.h>
00018 
00019 #include <set>
00020 
00021 FastTSGFromL2Muon::FastTSGFromL2Muon(const edm::ParameterSet& cfg) : theConfig(cfg) 
00022 {
00023   produces<L3MuonTrajectorySeedCollection>();
00024 
00025   edm::ParameterSet serviceParameters = 
00026     cfg.getParameter<edm::ParameterSet>("ServiceParameters");
00027   theService = new MuonServiceProxy(serviceParameters);
00028 
00029   thePtCut = cfg.getParameter<double>("PtCut");
00030 
00031   theL2CollectionLabel = cfg.getParameter<edm::InputTag>("MuonCollectionLabel");
00032   theSeedCollectionLabels = cfg.getParameter<std::vector<edm::InputTag> >("SeedCollectionLabels");
00033   theSimTrackCollectionLabel  = cfg.getParameter<edm::InputTag>("SimTrackCollectionLabel");
00034   // useTFileService_ = cfg.getUntrackedParameter<bool>("UseTFileService",false);
00035 
00036 }
00037 
00038 FastTSGFromL2Muon::~FastTSGFromL2Muon()
00039 {
00040 }
00041 
00042 void 
00043 FastTSGFromL2Muon::beginRun(edm::Run& run, edm::EventSetup const& es)
00044 {
00045   //update muon proxy service
00046   theService->update(es);
00047   
00048   //region builder
00049   edm::ParameterSet regionBuilderPSet = 
00050     theConfig.getParameter<edm::ParameterSet>("MuonTrackingRegionBuilder");
00051   theRegionBuilder = new MuonTrackingRegionBuilder(regionBuilderPSet,theService);
00052   
00053   /*
00054   if(useTFileService_) {
00055     edm::Service<TFileService> fs;
00056     h_nSeedPerTrack = fs->make<TH1F>("nSeedPerTrack","nSeedPerTrack",76,-0.5,75.5);
00057     h_nGoodSeedPerTrack = fs->make<TH1F>("nGoodSeedPerTrack","nGoodSeedPerTrack",75,-0.5,75.5);
00058     h_nGoodSeedPerEvent = fs->make<TH1F>("nGoodSeedPerEvent","nGoodSeedPerEvent",75,-0.5,75.5);
00059   } else {
00060     h_nSeedPerTrack = 0;
00061     h_nGoodSeedPerEvent = 0;
00062     h_nGoodSeedPerTrack = 0;
00063   }
00064   */
00065 
00066 }
00067 
00068 
00069 void 
00070 FastTSGFromL2Muon::produce(edm::Event& ev, const edm::EventSetup& es)
00071 {
00072 
00073   // Initialize the output product
00074   std::auto_ptr<L3MuonTrajectorySeedCollection> result(new L3MuonTrajectorySeedCollection());
00075 
00076   //intialize service
00077   theService->update(es);
00078   
00079   // Region builder
00080   theRegionBuilder->setEvent(ev);
00081 
00082   // Retrieve the Monte Carlo truth (SimTracks)
00083   edm::Handle<edm::SimTrackContainer> theSimTracks;
00084   ev.getByLabel(theSimTrackCollectionLabel,theSimTracks);
00085 
00086   // Retrieve L2 muon collection
00087   edm::Handle<reco::TrackCollection> l2muonH;
00088   ev.getByLabel(theL2CollectionLabel ,l2muonH); 
00089 
00090   // Retrieve Seed collection
00091   unsigned seedCollections = theSeedCollectionLabels.size();
00092   std::vector<edm::Handle<edm::View<TrajectorySeed> > > theSeeds;
00093   theSeeds.resize(seedCollections);
00094   unsigned seed_size = 0;
00095   for ( unsigned iseed=0; iseed<seedCollections; ++iseed ) { 
00096     ev.getByLabel(theSeedCollectionLabels[iseed], theSeeds[iseed]);
00097     seed_size += theSeeds[iseed]->size();
00098   }
00099 
00100   // Loop on L2 muons
00101   unsigned int imu=0;
00102   unsigned int imuMax=l2muonH->size();
00103   // std::cout << "Found " << imuMax << " L2 muons" << std::endl;
00104   for (;imu!=imuMax;++imu){
00105 
00106     // Make a ref to l2 muon
00107     reco::TrackRef muRef(l2muonH, imu);
00108     
00109     // Cut on muons with low momenta
00110     if ( muRef->pt() < thePtCut 
00111          || muRef->innerMomentum().Rho() < thePtCut 
00112          || muRef->innerMomentum().R() < 2.5 ) continue;
00113     
00114     // Define the region of interest
00115     RectangularEtaPhiTrackingRegion * region = theRegionBuilder->region(muRef);
00116 
00117     // Copy the collection of seeds (ahem, this is time consuming!)
00118     std::vector<TrajectorySeed> tkSeeds;
00119     std::set<unsigned> tkIds;
00120     tkSeeds.reserve(seed_size);
00121     for ( unsigned iseed=0; iseed<seedCollections; ++iseed ) { 
00122       edm::Handle<edm::View<TrajectorySeed> > aSeedCollection = theSeeds[iseed];
00123       unsigned nSeeds = aSeedCollection->size();
00124       for (unsigned seednr = 0; seednr < nSeeds; ++seednr) {
00125             
00126         // The seed
00127         const BasicTrajectorySeed* aSeed = &((*aSeedCollection)[seednr]);
00128         
00129         // Find the first hit of the Seed
00130         TrajectorySeed::range theSeedingRecHitRange = aSeed->recHits();
00131         const SiTrackerGSMatchedRecHit2D * theFirstSeedingRecHit = 
00132           (const SiTrackerGSMatchedRecHit2D*) (&(*(theSeedingRecHitRange.first)));
00133 
00134         // The SimTrack id associated to that recHit
00135         int simTrackId = theFirstSeedingRecHit->simtrackId();
00136 
00137         // Track already associated to a seed
00138         std::set<unsigned>::iterator tkId = tkIds.find(simTrackId);
00139         if( tkId != tkIds.end() ) continue;     
00140 
00141         const SimTrack& theSimTrack = (*theSimTracks)[simTrackId]; 
00142 
00143         if ( clean(muRef,region,aSeed,theSimTrack) ) tkSeeds.push_back(*aSeed);
00144         tkIds.insert(simTrackId);
00145 
00146       } // End loop on seeds
00147  
00148     } // End loop on seed collections
00149   
00150     // Free memory
00151     delete region;
00152 
00153     // A plot
00154     // if(h_nSeedPerTrack) h_nSeedPerTrack->Fill(tkSeeds.size());
00155 
00156     // Another plot
00157     // if(h_nGoodSeedPerTrack) h_nGoodSeedPerTrack->Fill(tkSeeds.size());
00158     
00159 
00160     // Now create the Muon Trajectory Seed
00161     unsigned int is=0;
00162     unsigned int isMax=tkSeeds.size();
00163     for (;is!=isMax;++is){
00164       result->push_back( L3MuonTrajectorySeed(tkSeeds[is], muRef));
00165     } // End of tk seed loop
00166     
00167   } // End of l2 muon loop
00168 
00169   // std::cout << "Found " << result->size() << " seeds for muons" << std::endl;
00170 
00171   // And yet another plot
00172   // if(h_nGoodSeedPerEvent) h_nGoodSeedPerEvent->Fill(result->size());
00173   
00174   //put in the event
00175   ev.put(result);
00176 
00177 }
00178 
00179 bool
00180 FastTSGFromL2Muon::clean(reco::TrackRef muRef,
00181                          RectangularEtaPhiTrackingRegion* region,
00182                          const BasicTrajectorySeed* aSeed, 
00183                          const SimTrack& theSimTrack) { 
00184     
00185   // Eta cleaner   
00186   const PixelRecoRange<float>& etaRange = region->etaRange() ;  
00187   double etaSeed = theSimTrack.momentum().Eta();
00188   double etaLimit  = (fabs(fabs(etaRange.max())-fabs(etaRange.mean())) <0.05) ? 
00189     0.05 : fabs(fabs(etaRange.max()) - fabs(etaRange.mean())) ;
00190   bool inEtaRange = 
00191     etaSeed >= (etaRange.mean() - etaLimit) && 
00192     etaSeed <= (etaRange.mean() + etaLimit) ;
00193   if  ( !inEtaRange ) return false;
00194   
00195   // Phi cleaner
00196   const TkTrackingRegionsMargin<float>& phiMargin = region->phiMargin();
00197   double phiSeed = theSimTrack.momentum().Phi(); 
00198   double phiLimit  = (phiMargin.right() < 0.05 ) ? 0.05 : phiMargin.right(); 
00199   bool inPhiRange = 
00200     (fabs(deltaPhi(phiSeed,double(region->direction().phi()))) < phiLimit );
00201   if  ( !inPhiRange ) return false;
00202   
00203   // pt cleaner
00204   double ptSeed  = std::sqrt(theSimTrack.momentum().Perp2());
00205   double ptMin   = (region->ptMin()>3.5) ? 3.5: region->ptMin();  
00206   bool inPtRange = ptSeed >= ptMin &&  ptSeed<= 2*(muRef->pt());
00207   return inPtRange;
00208   
00209 }