CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/RecoMuon/MuonSeedGenerator/plugins/SETMuonSeedProducer.cc

Go to the documentation of this file.
00001 
00005 #include "RecoMuon/MuonSeedGenerator/plugins/SETMuonSeedProducer.h"
00006 
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/Framework/interface/Event.h"
00009 #include "DataFormats/TrajectorySeed/interface/TrajectorySeed.h"
00010 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00011 #include "DataFormats/TrajectoryState/interface/PTrajectoryStateOnDet.h"
00012 #include "DataFormats/TrajectorySeed/interface/PropagationDirection.h"
00013 
00014 #include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
00015 #include "RecoMuon/Navigation/interface/DirectMuonNavigation.h"
00016 
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018 
00019 #include "DataFormats/TrajectoryState/interface/PTrajectoryStateOnDet.h"
00020 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00021 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00022 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00023 #include "TrackingTools/DetLayers/interface/DetLayer.h"
00024 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00025 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00026 #include "TMath.h"
00027 
00028 using namespace edm;
00029 using namespace std;
00030 
00031 SETMuonSeedProducer::SETMuonSeedProducer(const ParameterSet& parameterSet)
00032 : thePatternRecognition(parameterSet),
00033   theSeedFinder(parameterSet),
00034   theBeamSpotTag(parameterSet.getParameter<edm::InputTag>("beamSpotTag"))
00035 {
00036   const string metname = "Muon|RecoMuon|SETMuonSeedSeed";  
00037   //std::cout<<" The SET SEED producer started."<<std::endl;
00038 
00039   ParameterSet serviceParameters = parameterSet.getParameter<ParameterSet>("ServiceParameters");
00040   theService        = new MuonServiceProxy(serviceParameters);
00041   thePatternRecognition.setServiceProxy(theService);
00042   theSeedFinder.setServiceProxy(theService);
00043   // Parameter set for the Builder
00044   ParameterSet trajectoryBuilderParameters = parameterSet.getParameter<ParameterSet>("SETTrajBuilderParameters");
00045 
00046   LogTrace(metname) << "constructor called" << endl;
00047   
00048   apply_prePruning = trajectoryBuilderParameters.getParameter<bool>("Apply_prePruning");
00049 
00050   useSegmentsInTrajectory = trajectoryBuilderParameters.getParameter<bool>("UseSegmentsInTrajectory");
00051 
00052   // The inward-outward fitter (starts from seed state)
00053   ParameterSet filterPSet = trajectoryBuilderParameters.getParameter<ParameterSet>("FilterParameters");
00054   filterPSet.addUntrackedParameter("UseSegmentsInTrajectory", useSegmentsInTrajectory);
00055   theFilter = new SETFilter(filterPSet,theService);
00056 
00057   //----
00058 
00059   produces<TrajectorySeedCollection>();
00060 
00061 } 
00062 
00063 SETMuonSeedProducer::~SETMuonSeedProducer(){
00064 
00065   LogTrace("Muon|RecoMuon|SETMuonSeedProducer") 
00066     << "SETMuonSeedProducer destructor called" << endl;
00067   
00068   if(theFilter) delete theFilter;
00069    if (theService) delete theService;
00070 }
00071 
00072 void SETMuonSeedProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup){
00073   //std::cout<<" start producing..."<<std::endl;  
00074   const string metname = "Muon|RecoMuon|SETMuonSeedSeed";  
00075 
00076   MuonPatternRecoDumper debug;
00077 
00078   //Get the CSC Geometry :
00079   theService->update(eventSetup);
00080 
00081   std::auto_ptr<TrajectorySeedCollection> output(new TrajectorySeedCollection());
00082   
00083   Handle<View<TrajectorySeed> > seeds; 
00084 
00085   setEvent(event);
00086 
00087   reco::BeamSpot beamSpot;
00088   edm::Handle<reco::BeamSpot> beamSpotHandle;
00089   event.getByLabel(theBeamSpotTag, beamSpotHandle);
00090   if ( beamSpotHandle.isValid() )
00091   {
00092     beamSpot = *beamSpotHandle;
00093 
00094   } else
00095   {
00096     edm::LogInfo("MuonSeedGenerator")
00097       << "No beam spot available from EventSetup \n";
00098   }
00099 
00100   // make it a vector so we can subtract it from position vectors
00101   GlobalVector gv(beamSpot.x0(), beamSpot.y0(), beamSpot.z0());
00102   theSeedFinder.setBeamSpot(gv);
00103 
00104   bool fwFitFailed = true;
00105 
00106   std::vector <SeedCandidate> seedCandidates_AllChosen;
00107   std::vector< MuonRecHitContainer > MuonRecHitContainer_clusters;
00108   //---- this is "clustering"; later a trajectory can not use hits from different clusters
00109   thePatternRecognition.produce(event, eventSetup, MuonRecHitContainer_clusters);
00110 
00111   //std::cout<<"We have formed "<<MuonRecHitContainer_clusters.size()<<" clusters"<<std::endl;
00112   //---- for each cluster,
00113   for(unsigned int cluster = 0; cluster < MuonRecHitContainer_clusters.size(); ++cluster) {
00114     //std::cout<<" This is cluster number : "<<cluster<<std::endl;
00115     std::vector <SeedCandidate> seedCandidates_inCluster; 
00116     //---- group hits in detector layers (if in same layer); the idea is that
00117     //---- some hits could not belong to a track simultaneously - these will be in a 
00118     //---- group; two hits from one and the same group will not go to the same track 
00119     std::vector< MuonRecHitContainer > MuonRecHitContainer_perLayer = theSeedFinder.sortByLayer(MuonRecHitContainer_clusters[cluster]);
00120     //---- Add protection against huge memory consumption
00121     //---- Delete busy layers if needed (due to combinatorics)
00122     theSeedFinder.limitCombinatorics(MuonRecHitContainer_perLayer);
00123     //---- build all possible combinations (valid sets)
00124     std::vector <MuonRecHitContainer> allValidSets = theSeedFinder.findAllValidSets(MuonRecHitContainer_perLayer);
00125     if(apply_prePruning){
00126       //---- remove "wild" segments from the combination
00127       theSeedFinder.validSetsPrePruning(allValidSets);
00128     }
00129     
00130     //---- build the appropriate output: seedCandidates_inCluster
00131     //---- if too many (?) valid sets in a cluster - skip it 
00132     if(allValidSets.size()<500){// hardcoded - remove it
00133       seedCandidates_inCluster = theSeedFinder.fillSeedCandidates(allValidSets);
00134     }
00135     //---- find the best valid combinations using simple (no propagation errors) chi2-fit 
00136     //std::cout<<"  Found "<<seedCandidates_inCluster.size()<<" valid sets in the current cluster."<<std::endl;
00137     if(!seedCandidates_inCluster.empty()){
00138       //---- this is the forward fitter (segments); choose which of the SETs in a cluster to be considered further
00139       std::vector < SeedCandidate> bestSets_inCluster;
00140       fwFitFailed = !(filter()->fwfit_SET(seedCandidates_inCluster, bestSets_inCluster));
00141       
00142       //---- has the fit failed? continue to the next cluster instead of returning the empty trajectoryContainer and stop the loop IBL 080903
00143       if(fwFitFailed){
00144         //std::cout<<"  fwfit_SET failed!"<<std::endl;
00145         continue;
00146       }  
00147       for(unsigned int iSet = 0; iSet<bestSets_inCluster.size();++iSet){
00148         seedCandidates_AllChosen.push_back(bestSets_inCluster[iSet]);
00149       }
00150     }
00151   }
00152   //---- loop over all the SETs candidates
00153   for(unsigned int iMuon = 0;iMuon<seedCandidates_AllChosen.size();++iMuon){
00154     //std::cout<<" chosen iMuon = "<<iMuon<<std::endl;
00155     Trajectory::DataContainer finalCandidate;
00156     SeedCandidate * aFinalSet = &(seedCandidates_AllChosen[iMuon]);
00157     fwFitFailed = !(filter()->buildTrajectoryMeasurements(aFinalSet, finalCandidate));
00158     if(fwFitFailed ){
00159       //std::cout<<"  buildTrajectoryMeasurements failed!"<<std::endl;
00160       continue;
00161     }
00162     //---- are there measurements (or detLayers) used at all?
00163     if( filter()->layers().size() )
00164       LogTrace(metname) << debug.dumpLayer( filter()->lastDetLayer());
00165     else {
00166       continue;
00167     }
00168     //std::cout<<"  chambers used - all : "<<filter()->getTotalChamberUsed()<<", DT : "<<filter()->getDTChamberUsed()<<
00169     //", CSC : "<<filter()->getCSCChamberUsed()<<", RPC : "<<filter()->getRPCChamberUsed()<<std::endl;
00170     //---- ask for some "reasonable" conditions to build a STA muon; 
00171     //---- (totalChambers >= 2, dtChambers + cscChambers >0)
00172     if (filter()->goodState()) {
00173       TransientTrackingRecHit::ConstRecHitContainer hitContainer;
00174       TrajectoryStateOnSurface firstTSOS;
00175       bool conversionPassed = false;
00176       if(!useSegmentsInTrajectory){
00177         //---- transforms set of segment measurements to a set of rechit measurements
00178         conversionPassed = filter()->transform(finalCandidate, hitContainer, firstTSOS);
00179       }
00180       else{
00181         //---- transforms set of segment measurements to a set of segment measurements
00182         conversionPassed = filter()->transformLight(finalCandidate, hitContainer, firstTSOS);
00183       }
00184       if ( conversionPassed && !finalCandidate.empty() && !hitContainer.empty()) {
00185         //---- doesn't work... 
00186         //output->push_back( theSeedFinder.makeSeed(firstTSOS, hitContainer) );
00187         
00188         edm::OwnVector<TrackingRecHit> recHitsContainer;
00189         for(unsigned int iHit = 0;iHit < hitContainer.size();++iHit){
00190           recHitsContainer.push_back(hitContainer.at(iHit)->hit()->clone());
00191         }
00192         PropagationDirection dir = oppositeToMomentum;
00193         if(useSegmentsInTrajectory){
00194           dir = alongMomentum;// why forward (for rechits) later?
00195         }
00196         
00197         PTrajectoryStateOnDet seedTSOS =
00198          trajectoryStateTransform::persistentState( firstTSOS, hitContainer.at(0)->geographicalId().rawId());
00199         TrajectorySeed seed(seedTSOS,recHitsContainer,dir);
00200         
00201         //MuonPatternRecoDumper debug;
00202         //std::cout<<"  firstTSOS (not IP) = "<<debug.dumpTSOS(firstTSOS)<<std::endl;
00203         //std::cout<<"  hits(from range) = "<<range.second-range.first<<" hits (from size) = "<<hitContainer.size()<<std::endl;
00204         //for(unsigned int iRH=0;iRH<hitContainer.size();++iRH){
00205         //std::cout<<"  RH = "<<iRH+1<<" globPos = "<<hitContainer.at(iRH)->globalPosition()<<std::endl;
00206         //}
00207         output->push_back(seed);
00208       }
00209       else{
00210         //std::cout<<" Transformation from TrajectoryMeasurements to RecHitContainer faild - skip "<<std::endl;
00211         continue;
00212       }
00213     }else{
00214       //std::cout<<" Not enough (as defined) measurements to build trajectory - skip"<<std::endl;
00215       continue;
00216     }
00217   }
00218   event.put(output);
00219   theFilter->reset();
00220 }
00221 
00222 //
00223 void SETMuonSeedProducer::setEvent(const edm::Event& event){
00224   theFilter->setEvent(event);
00225 }