CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Validation/RecoMuon/src/MuonTrackResidualAnalyzer.cc

Go to the documentation of this file.
00001 #include "Validation/RecoMuon/src/MuonTrackResidualAnalyzer.h"
00002 
00003 // Collaborating Class Header
00004 #include "FWCore/Framework/interface/Frameworkfwd.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00010 
00011 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00012 #include "TrackingTools/KalmanUpdators/interface/KFUpdator.h"
00013 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00014 #include "TrackingTools/TrackFitters/interface/KFTrajectoryFitter.h"
00015 #include "TrackingTools/TrackFitters/interface/KFTrajectorySmoother.h"
00016 
00017 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00018 #include "DataFormats/TrackReco/interface/Track.h"
00019 #include "DataFormats/MuonDetId/interface/DTLayerId.h"
00020 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
00021 
00022 #include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
00023 #include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
00024 #include "Validation/RecoMuon/src/Histograms.h"
00025 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHitBuilder.h"
00026 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHit.h"
00027 
00028 
00029 #include "TFile.h"
00030 #include "TH1F.h"
00031 #include "TH2F.h"
00032 #include "TDirectory.h"
00033 
00034 using namespace std;
00035 using namespace edm;
00036 
00038 MuonTrackResidualAnalyzer::MuonTrackResidualAnalyzer(const edm::ParameterSet& pset){
00039   
00040     // service parameters
00041   ParameterSet serviceParameters = pset.getParameter<ParameterSet>("ServiceParameters");
00042   // the services
00043   theService = new MuonServiceProxy(serviceParameters);
00044   
00045   theMuonTrackLabel = pset.getParameter<InputTag>("MuonTrack");
00046   theSeedCollectionLabel = pset.getParameter<InputTag>("MuonSeed");
00047 
00048   cscSimHitLabel = pset.getParameter<InputTag>("CSCSimHit");
00049   dtSimHitLabel = pset.getParameter<InputTag>("DTSimHit");
00050   rpcSimHitLabel = pset.getParameter<InputTag>("RPCSimHit");
00051 
00052   dbe_ = edm::Service<DQMStore>().operator->();
00053   out = pset.getUntrackedParameter<string>("rootFileName");
00054   dirName_ = pset.getUntrackedParameter<std::string>("dirName");
00055   
00056   // Sim or Real
00057   theDataType = pset.getParameter<InputTag>("DataType"); 
00058   if(theDataType.label() != "RealData" && theDataType.label() != "SimData")
00059     LogDebug("MuonTrackResidualAnalyzer")<<"Error in Data Type!!";
00060 
00061   theEtaRange = (EtaRange) pset.getParameter<int>("EtaRange");
00062 
00063   theUpdator = new KFUpdator();
00064   theEstimator = new Chi2MeasurementEstimator(100000.);
00065 
00066   theMuonSimHitNumberPerEvent = 0;
00067 }
00068 
00070 MuonTrackResidualAnalyzer::~MuonTrackResidualAnalyzer(){
00071   delete theUpdator;
00072   delete theEstimator;
00073   delete theService;
00074 }
00075 
00076 // Operations
00077 void MuonTrackResidualAnalyzer::beginJob(){
00078   LogDebug("MuonTrackResidualAnalyzer")<<"Begin Job";
00079   
00080   dbe_->showDirStructure();
00081   
00082   dbe_->cd();
00083   InputTag algo = theMuonTrackLabel;
00084   string dirName=dirName_;
00085   if (algo.process()!="")
00086     dirName+=algo.process()+"_";
00087   if(algo.label()!="")
00088     dirName+=algo.label()+"_";
00089   if(algo.instance()!="")
00090     dirName+=algo.instance()+"";
00091   if (dirName.find("Tracks")<dirName.length()){
00092     dirName.replace(dirName.find("Tracks"),6,"");
00093   }
00094   std::replace(dirName.begin(), dirName.end(), ':', '_');
00095   dbe_->setCurrentFolder(dirName.c_str());
00096   
00097   
00098   hDPtRef = dbe_->book1D("DeltaPtRef","P^{in}_{t}-P^{in ref}",10000,-20,20);
00099   
00100   // Resolution wrt the 1D Rec Hits
00101   //  h1DRecHitRes = new HResolution1DRecHit("TotalRec");
00102   
00103   // Resolution wrt the 1d Sim Hits
00104   //  h1DSimHitRes = new HResolution1DRecHit("TotalSim");
00105   
00106   hSimHitsPerTrack  = dbe_->book1D("SimHitsPerTrack","Number of sim hits per track",55,0,55);
00107   hSimHitsPerTrackVsEta  = dbe_->book2D("SimHitsPerTrackVsEta","Number of sim hits per track VS #eta",120,-3.,3.,55,0,55);
00108   hDeltaPtVsEtaSim = dbe_->book2D("DeltaPtVsEtaSim","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
00109   hDeltaPtVsEtaSim2 = dbe_->book2D("DeltaPtVsEtaSim2","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
00110 }
00111 
00112 void MuonTrackResidualAnalyzer::endJob(){
00113   if ( out.size() != 0 && dbe_ ) dbe_->save(out);
00114 }
00115  
00116 
00117 void MuonTrackResidualAnalyzer::analyze(const edm::Event & event, const edm::EventSetup& eventSetup){
00118   LogDebug("MuonTrackResidualAnalyzer")<<"Analyze";
00119 
00120   // Update the services
00121   theService->update(eventSetup);
00122   MuonPatternRecoDumper debug;
00123   theMuonSimHitNumberPerEvent = 0;
00124 
00125   // Get the SimHit collection from the event
00126   Handle<PSimHitContainer> dtSimHits;
00127   event.getByLabel(dtSimHitLabel.instance(),dtSimHitLabel.label(), dtSimHits);
00128 
00129   Handle<PSimHitContainer> cscSimHits;
00130   event.getByLabel(cscSimHitLabel.instance(),cscSimHitLabel.label(), cscSimHits);
00131 
00132   Handle<PSimHitContainer> rpcSimHits;
00133   event.getByLabel(rpcSimHitLabel.instance(),rpcSimHitLabel.label(), rpcSimHits);
00134 
00135   Handle<SimTrackContainer> simTracks;
00136 
00137   // FIXME Add the tracker one??
00138 
00139   // Map simhits per DetId
00140   map<DetId, const PSimHit* > muonSimHitsPerId =
00141     mapMuSimHitsPerId(dtSimHits,cscSimHits,rpcSimHits);
00142   
00143   hSimHitsPerTrack->Fill(theMuonSimHitNumberPerEvent);
00144   
00145   double etaSim=0;
00146 
00147   if(theDataType.label() == "SimData"){
00148     
00149     // Get the SimTrack collection from the event
00150     event.getByLabel(theDataType.instance(),simTracks);
00151     
00152     // Loop over the Sim tracks
00153     SimTrackContainer::const_iterator simTrack;
00154     
00155     for (simTrack = simTracks->begin(); simTrack != simTracks->end(); ++simTrack)
00156       if (abs((*simTrack).type()) == 13){
00157         hSimHitsPerTrackVsEta->Fill((*simTrack).momentum().eta(),theMuonSimHitNumberPerEvent);
00158         etaSim = (*simTrack).momentum().eta();
00159         theSimTkId = (*simTrack).trackId();
00160 
00161       }
00162   }
00163 
00164   
00165   // Get the RecTrack collection from the event
00166   Handle<reco::TrackCollection> muonTracks;
00167   event.getByLabel(theMuonTrackLabel, muonTracks);
00168 
00169   reco::TrackCollection::const_iterator muonTrack;
00170   
00171   // Loop over the Rec tracks
00172   for (muonTrack = muonTracks->begin(); muonTrack != muonTracks->end(); ++muonTrack) {
00173     
00174     reco::TransientTrack track(*muonTrack,&*theService->magneticField(),theService->trackingGeometry());   
00175 
00176     TrajectoryStateOnSurface outerTSOS = track.outermostMeasurementState();
00177     TrajectoryStateOnSurface innerTSOS = track.innermostMeasurementState();
00178 
00179     TransientTrackingRecHit::ConstRecHitContainer result;
00180     
00181     trackingRecHit_iterator rhend = track.recHitsBegin()-1;
00182     trackingRecHit_iterator rhbegin = track.recHitsEnd()-2;
00183     
00184     // SimHit Energy loss analysis  
00185     double momAtEntry = -150., momAtExit = -150.;
00186 
00187     if(theSimHitContainer.size()>1){
00188 
00189       const GeomDet *geomDetA = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.front()->detUnitId()));
00190       double distA = geomDetA->toGlobal(theSimHitContainer.front()->localPosition()).mag();
00191 
00192       const GeomDet *geomDetB = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.back()->detUnitId()));
00193       double distB = geomDetB->toGlobal(theSimHitContainer.back()->localPosition()).mag();
00194 
00195       LogTrace("MuonTrackResidualAnalyzer")<<"Inner SimHit: "<<theSimHitContainer.front()->particleType()
00196                                            <<" Pt: "<<theSimHitContainer.front()->momentumAtEntry().perp()
00197                                            <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
00198                                            <<" R: "<<distA<<endl;
00199       LogTrace("MuonTrackResidualAnalyzer")<<"Outer SimHit: "<<theSimHitContainer.back()->particleType()
00200                                            <<" Pt: "<<theSimHitContainer.back()->momentumAtEntry().perp()
00201                                            <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
00202                                            <<" R: "<<distB<<endl;
00203       
00204       momAtEntry = theSimHitContainer.front()->momentumAtEntry().perp();
00205       momAtExit =  theSimHitContainer.back()->momentumAtEntry().perp();
00206     }
00207     
00208     trackingRecHit_iterator rhFirst = track.recHitsBegin();
00209     trackingRecHit_iterator rhLast = track.recHitsEnd()-1;
00210     map<DetId,const PSimHit*>::const_iterator itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
00211     map<DetId,const PSimHit*>::const_iterator itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
00212     
00213     double momAtEntry2 = -150, momAtExit2 = -150.;
00214     if (itFirst != muonSimHitsPerId.end() )
00215       momAtEntry2 = itFirst->second->momentumAtEntry().perp();
00216     else {
00217       LogDebug("MuonTrackResidualAnalyzer")<<"No first sim hit found";
00218       ++rhFirst;
00219       itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
00220       if (itFirst != muonSimHitsPerId.end() )
00221         momAtEntry2 = itFirst->second->momentumAtEntry().perp();
00222       else{
00223         LogDebug("MuonTrackResidualAnalyzer")<<"No second sim hit found";
00224         // continue;
00225       }
00226     }
00227     
00228     if (itLast != muonSimHitsPerId.end() )
00229       momAtExit2 = itLast->second->momentumAtEntry().perp();
00230     else {
00231       LogDebug("MuonTrackResidualAnalyzer")<<"No last sim hit found";
00232       --rhLast;
00233       itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
00234       if (itLast != muonSimHitsPerId.end() )
00235         momAtExit2 = itLast->second->momentumAtEntry().perp();
00236       else{
00237         LogDebug("MuonTrackResidualAnalyzer")<<"No last but one sim hit found";
00238         // continue;
00239       }
00240     }
00241     
00242     if(etaSim){
00243       if(momAtEntry >=0 && momAtExit >= 0)
00244         hDeltaPtVsEtaSim->Fill(etaSim,momAtEntry-momAtExit);
00245       if(momAtEntry2 >=0 && momAtExit2 >= 0)
00246         hDeltaPtVsEtaSim2->Fill(etaSim,momAtEntry2-momAtExit2);
00247     }
00248     else
00249       LogDebug("MuonTrackResidualAnalyzer")<<"NO SimTrack'eta";
00250     //
00251 
00252     // computeResolution(trajectoryBW,muonSimHitsPerId,h1DSimHitRes);
00253     // computeResolution(smoothed,muonSimHitsPerId,h1DSimHitRes);
00254     
00255   }
00256 }
00257 
00258 bool MuonTrackResidualAnalyzer::isInTheAcceptance(double eta){
00259   switch(theEtaRange){
00260   case all:
00261     return ( abs(eta) <= 2.4 ) ? true : false;
00262   case barrel:
00263     return ( abs(eta) < 1.1 ) ? true : false;
00264   case endcap:
00265     return ( abs(eta) >= 1.1 && abs(eta) <= 2.4 ) ? true : false;  
00266   default:
00267     {LogDebug("MuonTrackResidualAnalyzer")<<"No correct Eta range selected!! "; return false;}
00268   }
00269 }
00270 
00271 // map the muon simhits by id
00272 map<DetId,const PSimHit*>
00273 MuonTrackResidualAnalyzer::mapMuSimHitsPerId(Handle<PSimHitContainer> dtSimhits,
00274                                              Handle<PSimHitContainer> cscSimhits,
00275                                              Handle<PSimHitContainer> rpcSimhits) {
00276   
00277   MuonPatternRecoDumper debug;
00278   
00279   map<DetId,const PSimHit*> hitIdMap;
00280   theSimHitContainer.clear();
00281 
00282   mapMuSimHitsPerId(dtSimhits,hitIdMap);
00283   mapMuSimHitsPerId(cscSimhits,hitIdMap);
00284   mapMuSimHitsPerId(rpcSimhits,hitIdMap);
00285 
00286   if(theSimHitContainer.size() >1)
00287     stable_sort(theSimHitContainer.begin(),theSimHitContainer.end(),RadiusComparatorInOut(theService->trackingGeometry()));
00288 
00289   LogDebug("MuonTrackResidualAnalyzer")<<"Sim Hit list";
00290   int count=1;
00291   for(vector<const PSimHit*>::const_iterator it = theSimHitContainer.begin(); 
00292       it != theSimHitContainer.end(); ++it){
00293     LogTrace("MuonTrackResidualAnalyzer")<<count 
00294         << " " 
00295         << " Process Type: " << (*it)->processType()
00296         << " " 
00297         << debug.dumpMuonId(DetId( (*it)->detUnitId() ))<<endl;
00298   }  
00299 
00300   return hitIdMap;
00301 }
00302 
00303 
00304 void MuonTrackResidualAnalyzer::mapMuSimHitsPerId(Handle<PSimHitContainer> simhits, 
00305                                                   map<DetId,const PSimHit*> &hitIdMap){
00306   
00307   for(PSimHitContainer::const_iterator simhit = simhits->begin();
00308       simhit != simhits->end(); ++simhit) {
00309     
00310     if ( abs(simhit->particleType()) != 13 && theSimTkId != simhit->trackId()) continue; 
00311     
00312     theSimHitContainer.push_back(&*simhit);
00313     DetId id = DetId(simhit->detUnitId());
00314     
00315     if(id.subdetId() == MuonSubdetId::DT){
00316       DTLayerId lId(id.rawId());
00317       id = DetId(lId.rawId());
00318     }
00319 
00320     map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(id);
00321     
00322     if (it == hitIdMap.end() )
00323       hitIdMap[id] = &*simhit;
00324     else
00325       LogDebug("MuonTrackResidualAnalyzer")<<"TWO muons in the same sensible volume!!";
00326     
00327     ++theMuonSimHitNumberPerEvent;
00328   }
00329 }
00330 
00331 
00332 void MuonTrackResidualAnalyzer::computeResolution(Trajectory &trajectory, 
00333                                                   map<DetId,const PSimHit*> &hitIdMap,
00334                                                   HResolution1DRecHit *histos){
00335   
00336   Trajectory::DataContainer data = trajectory.measurements();
00337 
00338   for(Trajectory::DataContainer::const_iterator datum = data.begin();
00339       datum != data.end(); ++datum){
00340     
00341     GlobalPoint fitPoint = datum->updatedState().globalPosition();
00342 
00343     // FIXME!
00344     //     double errX = datum->updatedState().cartesianError().matrix()[0][0];
00345     //     double errY = datum->updatedState().cartesianError().matrix()[1][1];
00346     //     double errZ = datum->updatedState().cartesianError().matrix()[2][2];
00347     //
00348     double errX = datum->updatedState().localError().matrix()(3,3);
00349     double errY = datum->updatedState().localError().matrix()(4,4);
00350     double errZ = 1.;
00351 
00352     map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(datum->recHit()->geographicalId());
00353  
00354     
00355     if (it == hitIdMap.end() ) continue; // FIXME! Put a counter
00356     
00357     const PSimHit* simhit = it->second;
00358     
00359     LocalPoint simHitPoint = simhit->localPosition();
00360 
00361     const GeomDet* geomDet = theService->trackingGeometry()->idToDet(DetId(simhit->detUnitId()));
00362 
00363     LocalPoint fitLocalPoint = geomDet->toLocal(fitPoint);
00364     
00365     LocalVector diff = fitLocalPoint-simHitPoint;
00366 
00367     
00368     cout << "SimHit position "<< simHitPoint << endl;
00369     cout << "Fit position "<<  fitLocalPoint << endl;
00370     cout << "Fit position2 "<< datum->updatedState().localPosition() << endl;
00371     cout << "Errors on the fit position: (" << errX << "," << errY << "," << errZ << ")"<<endl;
00372     cout << "Resolution on x: " << diff.x()/abs(simHitPoint.x()) << endl;
00373     cout << "Resolution on y: " << diff.y()/abs(simHitPoint.y()) << endl;
00374     cout << "Resolution on z: " << diff.z()/abs(simHitPoint.z()) << endl;
00375 
00376     cout << "Eta direction: "<< simhit->momentumAtEntry().eta() <<" eta position: " << simHitPoint.eta() << endl;
00377     cout << "Phi direction: "<< simhit->momentumAtEntry().phi() <<" phi position: " << simHitPoint.phi() << endl;
00378 
00379     
00380     histos->Fill( simHitPoint.x(), simHitPoint.y(), simHitPoint.z(), 
00381                   diff.x(), diff.y(), diff.z(),
00382                   errX,  errY, errZ,
00383                   simhit->momentumAtEntry().eta(), simhit->momentumAtEntry().phi());
00384                   // simHitPoint.eta(), simHitPoint.phi() ); // FIXME!
00385   }
00386 
00387 
00388 }