CMS 3D CMS Logo

MuonAssociatorByHits.cc

Go to the documentation of this file.
00001 #include "SimMuon/MCTruth/interface/MuonAssociatorByHits.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00004 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00005 #include "DataFormats/DetId/interface/DetId.h"
00006 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
00007 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00008 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00009 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00010 #include "DataFormats/SiStripDetId/interface/TECDetId.h" 
00011 #include "DataFormats/SiStripDetId/interface/TIBDetId.h" 
00012 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00013 #include "DataFormats/SiStripDetId/interface/TOBDetId.h" 
00014 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00015 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00016 #include <sstream>
00017 
00018 using namespace reco;
00019 using namespace std;
00020 
00021 MuonAssociatorByHits::MuonAssociatorByHits (const edm::ParameterSet& conf) :  
00022   AbsoluteNumberOfHits_track(conf.getParameter<bool>("AbsoluteNumberOfHits_track")),
00023   MinHitCut_track(conf.getParameter<unsigned int>("MinHitCut_track")),
00024   AbsoluteNumberOfHits_muon(conf.getParameter<bool>("AbsoluteNumberOfHits_muon")),
00025   MinHitCut_muon(conf.getParameter<unsigned int>("MinHitCut_muon")),
00026   PurityCut_track(conf.getParameter<double>("PurityCut_track")),
00027   PurityCut_muon(conf.getParameter<double>("PurityCut_muon")),
00028   SimToReco_useTracker(conf.getParameter<bool>("SimToReco_useTracker")),
00029   EfficiencyCut_track(conf.getParameter<double>("EfficiencyCut_track")),
00030   SimToReco_useMuon(conf.getParameter<bool>("SimToReco_useMuon")),
00031   EfficiencyCut_muon(conf.getParameter<double>("EfficiencyCut_muon")),
00032   UsePixels(conf.getParameter<bool>("UsePixels")),
00033   UseGrouped(conf.getParameter<bool>("UseGrouped")),
00034   UseSplitting(conf.getParameter<bool>("UseSplitting")),
00035   ThreeHitTracksAreSpecial(conf.getParameter<bool>("ThreeHitTracksAreSpecial")),
00036   dumpDT(conf.getParameter<bool>("dumpDT")),
00037   dumpInputCollections(conf.getParameter<bool>("dumpInputCollections")),
00038   crossingframe(conf.getParameter<bool>("crossingframe")),
00039   simtracksTag(conf.getParameter<edm::InputTag>("simtracksTag")),
00040   simtracksXFTag(conf.getParameter<edm::InputTag>("simtracksXFTag")),
00041   conf_(conf)
00042 {
00043   LogTrace("MuonAssociatorByHits") << "constructing  MuonAssociatorByHits" << conf_.dump();
00044 }
00045 
00046 MuonAssociatorByHits::~MuonAssociatorByHits()
00047 {
00048 }
00049 
00050 RecoToSimCollection  
00051 MuonAssociatorByHits::associateRecoToSim(edm::RefToBaseVector<reco::Track>& tC,
00052                                           edm::RefVector<TrackingParticleCollection>& TPCollectionH,
00053                                           const edm::Event * e, const edm::EventSetup * setup) const{
00054 
00055   int tracker_nshared = 0;
00056   int muon_nshared = 0;
00057   int global_nshared = 0;
00058 
00059   double tracker_quality = 0;
00060   double tracker_quality_cut;
00061   if (AbsoluteNumberOfHits_track) tracker_quality_cut = static_cast<double>(MinHitCut_track); 
00062   else tracker_quality_cut = PurityCut_track;
00063 
00064   double muon_quality = 0;
00065   double muon_quality_cut;
00066   if (AbsoluteNumberOfHits_muon) muon_quality_cut = static_cast<double>(MinHitCut_muon); 
00067   else muon_quality_cut = PurityCut_muon;
00068 
00069   double global_quality = 0;
00070   
00071   std::vector< SimHitIdpr> tracker_matchedIds, muon_matchedIds;
00072 
00073   RecoToSimCollection  outputCollection;
00074   bool printRtS(true);
00075 
00076   // Tracker hit association  
00077   TrackerHitAssociator * trackertruth = new TrackerHitAssociator::TrackerHitAssociator(*e, conf_);
00078   // CSC hit association
00079   MuonTruth csctruth(conf_);
00080   csctruth.eventSetup(*e,*setup);
00081   // DT hit association
00082   printRtS = true;
00083   DTHitAssociator dttruth(*e,*setup,conf_,printRtS);
00084   // RPC hit association
00085   RPCHitAssociator rpctruth(*e,*setup,conf_);
00086   
00087   TrackingParticleCollection tPC;
00088   if (TPCollectionH.size()!=0) tPC = *(TPCollectionH.product());
00089 
00090   if (dumpInputCollections) {
00091     // reco::Track collection
00092     edm::LogVerbatim("MuonAssociatorByHits")<<"\n"<<"reco::Track collection --- size = "<<tC.size();
00093     int i = 0;
00094     for (edm::RefToBaseVector<reco::Track>::const_iterator ITER=tC.begin(); ITER!=tC.end(); ITER++, i++) {
00095       edm::LogVerbatim("MuonAssociatorByHits")
00096         <<"Track "<<i<<" : p = "<<(*ITER)->p()<<", charge = "<<(*ITER)->charge()
00097         <<", pT = "<<(*ITER)->pt()<<", eta = "<<(*ITER)->eta()<<", phi = "<<(*ITER)->phi();    
00098     }
00099 
00100     // TrackingParticle collection
00101     edm::LogVerbatim("MuonAssociatorByHits")<<"\n"<<"TrackingParticle collection --- size = "<<tPC.size();
00102     int j = 0;
00103     for(TrackingParticleCollection::const_iterator ITER=tPC.begin(); ITER!=tPC.end(); ITER++, j++) {
00104       edm::LogVerbatim("MuonAssociatorByHits")
00105         <<"TrackingParticle "<<j<<", q = "<<ITER->charge()<<", p = "<<ITER->p()
00106         <<", pT = "<<ITER->pt()<<", eta = "<<ITER->eta()<<", phi = "<<ITER->phi();
00107       
00108       edm::LogVerbatim("MuonAssociatorByHits")
00109         <<"\t pdg code = "<<ITER->pdgId()<<", made of "<<ITER->trackPSimHit().size()<<" PSimHit"
00110         <<" (in "<<ITER->matchedHit()<<" layers)"
00111         <<" from "<<ITER->g4Tracks().size()<<" SimTrack:";
00112       for (TrackingParticle::g4t_iterator g4T=ITER->g4Track_begin(); g4T!=ITER->g4Track_end(); g4T++) {
00113         edm::LogVerbatim("MuonAssociatorByHits")
00114           <<"\t\t Id:"<<g4T->trackId()<<"/Evt:("<<g4T->eventId().event()<<","<<g4T->eventId().bunchCrossing()<<")";
00115       }    
00116     }
00117 
00118     // SimTrack collection
00119     edm::Handle<CrossingFrame<SimTrack> > cf_simtracks;
00120     edm::Handle<edm::SimTrackContainer> simTrackCollection;
00121     
00122     if (crossingframe) {
00123       e->getByLabel(simtracksXFTag,cf_simtracks);
00124       auto_ptr<MixCollection<SimTrack> > SimTk( new MixCollection<SimTrack>(cf_simtracks.product()) );
00125       edm::LogVerbatim("MuonAssociatorByHits")<<"\n"<<"CrossingFrame<SimTrack> collection with InputTag = "<<simtracksXFTag
00126                                               <<" has size = "<<SimTk->size();
00127       int k = 0;
00128       for (MixCollection<SimTrack>::MixItr ITER=SimTk->begin(); ITER!=SimTk->end(); ITER++, k++) {
00129         edm::LogVerbatim("MuonAssociatorByHits")
00130           <<"SimTrack "<<k
00131           <<" - Id:"<<ITER->trackId()<<"/Evt:("<<ITER->eventId().event()<<","<<ITER->eventId().bunchCrossing()<<")"
00132           <<" pdgId = "<<ITER->type()<<", q = "<<ITER->charge()<<", p = "<<ITER->momentum().P()
00133           <<", pT = "<<ITER->momentum().Pt()<<", eta = "<<ITER->momentum().Eta()<<", phi = "<<ITER->momentum().Phi();
00134       }
00135     }
00136     else {
00137       e->getByLabel(simtracksTag,simTrackCollection);
00138       const edm::SimTrackContainer simTC = *(simTrackCollection.product());
00139       edm::LogVerbatim("MuonAssociatorByHits")<<"\n"<<"SimTrack collection with InputTag = "<<simtracksTag
00140                                               <<" has size = "<<simTC.size()<<endl;
00141       int k = 0;
00142       for(edm::SimTrackContainer::const_iterator ITER=simTC.begin(); ITER!=simTC.end(); ITER++, k++){
00143         edm::LogVerbatim("MuonAssociatorByHits")
00144           <<"SimTrack "<<k
00145           <<" - Id:"<<ITER->trackId()<<"/Evt:("<<ITER->eventId().event()<<","<<ITER->eventId().bunchCrossing()<<")"
00146           <<" pdgId = "<<ITER->type()<<", q = "<<ITER->charge()<<", p = "<<ITER->momentum().P()
00147           <<", pT = "<<ITER->momentum().Pt()<<", eta = "<<ITER->momentum().Eta()<<", phi = "<<ITER->momentum().Phi();
00148       }
00149     }
00150   }
00151   
00152   int tindex=0;
00153   for (edm::RefToBaseVector<reco::Track>::const_iterator track=tC.begin(); track!=tC.end(); track++, tindex++) {
00154     edm::LogVerbatim("MuonAssociatorByHits")
00155       <<"\n"<<"reco::Track "<<tindex
00156       <<", pT = " << (*track)->pt()<<", eta = "<<(*track)->momentum().eta()<<", phi = "<<(*track)->momentum().phi()
00157       <<", number of RecHits = "<<(*track)->recHitsSize()<<" ("<<(*track)->found()<<" valid hits, "<<(*track)->lost()<<" lost hits) \n";
00158     
00159     tracker_matchedIds.clear();
00160     muon_matchedIds.clear();
00161 
00162     bool this_track_matched = false;
00163     int n_matching_simhits = 0;
00164 
00165     int n_valid_hits = 0;          // number of valid hits      (Total)
00166     int n_tracker_valid_hits = 0;  //                           (Tracker)
00167     int n_muon_valid_hits = 0;     //                           (DT+CSC+RPC)
00168     int n_dt_valid_hits = 0;       //                           (DT)
00169     int n_csc_valid_hits = 0;      //                           (CSC)
00170     int n_rpc_valid_hits = 0;      //                           (RPC)
00171 
00172     int n_matched_hits = 0;          // number of matched hits    (Total)
00173     int n_tracker_matched_hits = 0;  //                           (Tracker)
00174     int n_muon_matched_hits = 0;     //                           (DT+CSC+RPC)
00175     int n_dt_matched_hits = 0;       //                           (DT)
00176     int n_csc_matched_hits = 0;      //                           (CSC)
00177     int n_rpc_matched_hits = 0;      //                           (RPC)
00178 
00179     printRtS = true;
00180     getMatchedIds<trackingRecHit_iterator>(tracker_matchedIds, muon_matchedIds,
00181                                            n_valid_hits, n_tracker_valid_hits, n_dt_valid_hits, n_csc_valid_hits, n_rpc_valid_hits,
00182                                            n_matched_hits, n_tracker_matched_hits, n_dt_matched_hits, n_csc_matched_hits, n_rpc_matched_hits,
00183                                            (*track)->recHitsBegin(), (*track)->recHitsEnd(), 
00184                                            trackertruth, dttruth, csctruth, rpctruth, printRtS);
00185 
00186     n_matching_simhits = tracker_matchedIds.size() + muon_matchedIds.size(); 
00187     n_muon_valid_hits = n_dt_valid_hits + n_csc_valid_hits + n_rpc_valid_hits;
00188     n_muon_matched_hits = n_dt_matched_hits + n_csc_matched_hits + n_rpc_matched_hits;
00189 
00190     edm::LogVerbatim("MuonAssociatorByHits")
00191       <<"\n"<<"##### all RecHits = "<<(*track)->recHitsSize()
00192       <<", # matching SimHits = " << n_matching_simhits <<" (may be more than one per rechit)"
00193       <<"\n"<< "# valid RecHits   = " <<n_valid_hits  <<" (" <<n_tracker_valid_hits<<"/"
00194       <<n_dt_valid_hits<<"/"<<n_csc_valid_hits<<"/"<<n_rpc_valid_hits<<" in Tracker/DT/CSC/RPC)"
00195       <<"\n"<< "# matched RecHits = " <<n_matched_hits<<" ("<<n_tracker_matched_hits<<"/"
00196       <<n_dt_matched_hits<<"/"<<n_csc_matched_hits<<"/"<<n_rpc_matched_hits<<" in Tracker/DT/CSC/RPC)";
00197 
00198     if (!n_matching_simhits) {
00199       edm::LogWarning("MuonAssociatorByHits")<<"*** WARNING: no matching PSimHit found for this reco::Track !";
00200     }
00201 
00202     if (n_valid_hits != (*track)->found()) {
00203       edm::LogWarning("MuonAssociatorByHits")
00204         <<"*** WARNING: Number of valid RecHits in this track should be:  track->found() = "<<(*track)->found()
00205         <<", but getMatchedIds finds:  n_valid_hits = "<<n_valid_hits;
00206     }
00207 
00208     std::vector<SimHitIdpr> tracker_idcachev, muon_idcachev;
00209 
00210     if(n_matching_simhits) {
00211       edm::LogVerbatim("MuonAssociatorByHits")
00212         <<"\n"<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00213         <<"\n"<< "reco::Track "<<tindex<<", q = "<<(*track)->charge()<<", p = " << (*track)->p()<<", pT = " << (*track)->pt()
00214         <<", eta = "<<(*track)->momentum().eta()<<", phi = "<<(*track)->momentum().phi()
00215         <<"\n\t"<< "made of "<<n_valid_hits<<" valid RecHits (tracker:"<<n_tracker_valid_hits<<"/muons:"<<n_muon_valid_hits<<")";
00216 
00217       int tpindex = 0;
00218       for (TrackingParticleCollection::const_iterator trpart = tPC.begin(); trpart != tPC.end(); ++trpart, ++tpindex) {
00219         tracker_idcachev.clear();
00220         tracker_nshared = getShared(tracker_matchedIds, tracker_idcachev, trpart);
00221         muon_idcachev.clear();
00222         muon_nshared = getShared(muon_matchedIds, muon_idcachev, trpart);
00223         global_nshared = tracker_nshared + muon_nshared;
00224 
00225         if (AbsoluteNumberOfHits_track) tracker_quality = static_cast<double>(tracker_nshared);
00226         else if(n_tracker_valid_hits!=0) tracker_quality = (static_cast<double>(tracker_nshared)/static_cast<double>(n_tracker_valid_hits));
00227         else tracker_quality = 0;
00228 
00229         if (AbsoluteNumberOfHits_muon) muon_quality = static_cast<double>(muon_nshared);
00230         else if(n_muon_valid_hits!=0) muon_quality = (static_cast<double>(muon_nshared)/static_cast<double>(n_muon_valid_hits));
00231         else muon_quality = 0;
00232 
00233         if(n_valid_hits!=0) global_quality = (static_cast<double>(global_nshared)/static_cast<double>(n_valid_hits));
00234         else global_quality = 0;
00235 
00236         bool matchOk = true;
00237         if (n_valid_hits==0) matchOk = false;
00238         else {
00239           if (n_tracker_valid_hits != 0) {
00240             if (tracker_quality < tracker_quality_cut) matchOk = false;
00241             //if a track has just 3 hits in the Tracker we require that all 3 hits are shared
00242             if (ThreeHitTracksAreSpecial && n_tracker_valid_hits==3 && tracker_nshared<3) matchOk = false;
00243           }
00244           
00245           if (n_muon_valid_hits != 0) {
00246             if (muon_quality < muon_quality_cut) matchOk = false;
00247           }
00248         }
00249         
00250         if (matchOk) {
00251 
00252           outputCollection.insert(tC[tindex],
00253                                   std::make_pair(edm::Ref<TrackingParticleCollection>(TPCollectionH, tpindex),
00254                                                  global_quality));
00255           this_track_matched = true;
00256 
00257           edm::LogVerbatim("MuonAssociatorByHits")
00258             << "\t\t"<< " **MATCHED** with quality = "<<global_quality<<" (tracker: "<<tracker_quality<<" / muon: "<<muon_quality<<") to:"
00259             <<"\n"<< "TrackingParticle " <<tpindex<<", q = "<<(*trpart).charge()<<", p = "<<(*trpart).p()
00260             <<", pT = "<<(*trpart).pt()<<", eta = "<<(*trpart).eta()<<", phi = "<<(*trpart).phi()
00261             <<"\n\t"<< " pdg code = "<<(*trpart).pdgId()<<", made of "<<(*trpart).trackPSimHit().size()<<" PSimHits"
00262             //      <<" (in "<<(*trpart).matchedHit()<<" layers)"
00263             <<" from "<<(*trpart).g4Tracks().size()<<" SimTrack:";
00264           for(TrackingParticle::g4t_iterator g4T=(*trpart).g4Track_begin(); 
00265               g4T!=(*trpart).g4Track_end(); 
00266               ++g4T) {
00267             edm::LogVerbatim("MuonAssociatorByHits")
00268               <<"\t\t"<< " Id:"<<(*g4T).trackId()<<"/Evt:("<<(*g4T).eventId().event()<<","<<(*g4T).eventId().bunchCrossing()<<")";
00269           }
00270         }
00271         else {
00272           // print something only if this TrackingParticle shares some hits with the current reco::Track
00273           if (global_nshared >0) 
00274             edm::LogVerbatim("MuonAssociatorByHits")
00275               <<"\t\t"<< " NOT matched to TrackingParticle "<<tpindex
00276               << " with quality = "<<global_quality<<" (tracker: "<<tracker_quality<<" / muon: "<<muon_quality<<")";
00277         }
00278         
00279       }    //  loop over TrackingParticle
00280 
00281       if (!this_track_matched) {
00282         edm::LogVerbatim("MuonAssociatorByHits")
00283           <<"\n"<<" NOT matched to any TrackingParticle";
00284       }
00285       
00286       edm::LogVerbatim("MuonAssociatorByHits")
00287         <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<"\n";
00288       
00289     }    //  if(!n_matching_simhits)
00290     
00291   }    // loop over reco::Track
00292 
00293   if (!tC.size()) 
00294     edm::LogVerbatim("MuonAssociatorByHits")<<"0 reconstructed tracks (-->> 0 associated !)";
00295 
00296   delete trackertruth;
00297   outputCollection.post_insert();
00298   return outputCollection;
00299 }
00300 
00301 
00302 SimToRecoCollection  
00303 MuonAssociatorByHits::associateSimToReco(edm::RefToBaseVector<reco::Track>& tC, 
00304                                           edm::RefVector<TrackingParticleCollection>& TPCollectionH,
00305                                           const edm::Event * e, const edm::EventSetup * setup) const{
00306 
00307   int tracker_nshared = 0;
00308   int muon_nshared = 0;
00309   int global_nshared = 0;
00310 
00311   double tracker_quality = 0;
00312   double tracker_quality_cut;
00313   if (AbsoluteNumberOfHits_track) tracker_quality_cut = static_cast<double>(MinHitCut_track); 
00314   else tracker_quality_cut = EfficiencyCut_track;
00315   
00316   double muon_quality = 0;
00317   double muon_quality_cut;
00318   if (AbsoluteNumberOfHits_muon) muon_quality_cut = static_cast<double>(MinHitCut_muon); 
00319   else muon_quality_cut = EfficiencyCut_muon;
00320 
00321   double global_quality = 0;
00322 
00323   double tracker_purity = 0;
00324   double muon_purity = 0;
00325   double global_purity = 0;
00326   
00327   std::vector< SimHitIdpr> tracker_matchedIds, muon_matchedIds;
00328 
00329   SimToRecoCollection  outputCollection;
00330 
00331   bool printRtS(true);
00332 
00333   // Tracker hit association  
00334   TrackerHitAssociator * trackertruth = new TrackerHitAssociator::TrackerHitAssociator(*e, conf_);
00335   // CSC hit association
00336   MuonTruth csctruth(conf_);
00337   csctruth.eventSetup(*e,*setup);
00338   // DT hit association
00339   printRtS = false;
00340   DTHitAssociator dttruth(*e,*setup,conf_,printRtS);  
00341   // RPC hit association
00342   RPCHitAssociator rpctruth(*e,*setup,conf_);
00343  
00344   TrackingParticleCollection tPC;
00345   if (TPCollectionH.size()!=0) tPC = *(TPCollectionH.product());
00346 
00347   bool any_trackingParticle_matched = false;
00348   
00349   int tindex=0;
00350   for (edm::RefToBaseVector<reco::Track>::const_iterator track=tC.begin(); track!=tC.end(); track++, tindex++) {
00351     if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00352       <<"\n"<<"reco::Track "<<tindex
00353       <<", pT = " << (*track)->pt()<<", eta = "<<(*track)->momentum().eta()<<", phi = "<<(*track)->momentum().phi()
00354       <<", number of RecHits = "<<(*track)->recHitsSize()<<" ("<<(*track)->found()<<" valid hits, "<<(*track)->lost()<<" lost hits)";
00355     
00356     tracker_matchedIds.clear();
00357     muon_matchedIds.clear();
00358     int n_matching_simhits = 0;
00359 
00360     int n_valid_hits = 0;          // number of valid hits      (Total)
00361     int n_tracker_valid_hits = 0;  //                           (Tracker)
00362     int n_muon_valid_hits = 0;     //                           (DT+CSC+RPC)
00363     int n_dt_valid_hits = 0;       //                           (DT)
00364     int n_csc_valid_hits = 0;      //                           (CSC)
00365     int n_rpc_valid_hits = 0;      //                           (RPC)
00366 
00367     int n_matched_hits = 0;          // number of matched hits    (Total)
00368     int n_tracker_matched_hits = 0;  //                           (Tracker)
00369     int n_muon_matched_hits = 0;     //                           (DT+CSC+RPC)
00370     int n_dt_matched_hits = 0;       //                           (DT)
00371     int n_csc_matched_hits = 0;      //                           (CSC)
00372     int n_rpc_matched_hits = 0;      //                           (RPC)
00373 
00374     printRtS = false;
00375     getMatchedIds<trackingRecHit_iterator>(tracker_matchedIds, muon_matchedIds,
00376                                            n_valid_hits, n_tracker_valid_hits, n_dt_valid_hits, n_csc_valid_hits, n_rpc_valid_hits,
00377                                            n_matched_hits, n_tracker_matched_hits, n_dt_matched_hits, n_csc_matched_hits, n_rpc_matched_hits,
00378                                            (*track)->recHitsBegin(), (*track)->recHitsEnd(), 
00379                                            trackertruth, dttruth, csctruth, rpctruth, printRtS);
00380     
00381     n_matching_simhits = tracker_matchedIds.size() + muon_matchedIds.size(); 
00382     n_muon_valid_hits = n_dt_valid_hits + n_csc_valid_hits + n_rpc_valid_hits;
00383     n_muon_matched_hits = n_dt_matched_hits + n_csc_matched_hits + n_rpc_matched_hits;
00384 
00385     if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00386       <<"\n"<<"*** # all RecHits = "<<(*track)->recHitsSize()
00387       <<", # matching PSimHit = " << n_matching_simhits <<" (may be more than one per rechit)"
00388       <<"\n"<< "# valid RecHits   = " <<n_valid_hits  <<" (" <<n_tracker_valid_hits<<"/"
00389       <<n_dt_valid_hits<<"/"<<n_csc_valid_hits<<"/"<<n_rpc_valid_hits<<" in Tracker/DT/CSC/RPC)"
00390       <<"\n"<< "# matched RecHits = " <<n_matched_hits<<" ("<<n_tracker_matched_hits<<"/"
00391       <<n_dt_matched_hits<<"/"<<n_csc_matched_hits<<"/"<<n_rpc_matched_hits<<" in Tracker/DT/CSC/RPC)";
00392     
00393     if (!n_matching_simhits) {
00394       edm::LogWarning("MuonAssociatorByHits")
00395         <<"*** WARNING: no matching PSimHit found for this reco::Track !";
00396     }
00397     
00398     if (n_valid_hits != (*track)->found()) {
00399       edm::LogWarning("MuonAssociatorByHits")
00400         <<"*** WARNING: Number of valid RecHits in this track should be:  track->found() = "<<(*track)->found()
00401         <<", but getMatchedIds finds:  n_valid_hits = "<<n_valid_hits;
00402     }
00403 
00404     std::vector<SimHitIdpr> tracker_idcachev, muon_idcachev;
00405     
00406     if(n_matching_simhits) {
00407       int tpindex =0;
00408       for (TrackingParticleCollection::const_iterator trpart = tPC.begin(); trpart != tPC.end(); ++trpart, ++tpindex) {
00409 
00410         tracker_idcachev.clear();
00411         muon_idcachev.clear();
00412 
00413         int n_tracker_simhits = 0;
00414         int n_tracker_recounted_simhits = 0; 
00415         int n_muon_simhits = 0; 
00416         int n_global_simhits = 0; 
00417         std::vector<PSimHit> tphits;
00418 
00419         tracker_nshared = getShared(tracker_matchedIds, tracker_idcachev, trpart);
00420         muon_nshared = getShared(muon_matchedIds, muon_idcachev, trpart);
00421         global_nshared = tracker_nshared + muon_nshared;
00422 
00423         if (global_nshared == 0) continue; // if this TP shares no hits with the current reco::Track loop over 
00424 
00425         for(std::vector<PSimHit>::const_iterator TPhit = trpart->pSimHit_begin(); TPhit != trpart->pSimHit_end(); TPhit++) {
00426           DetId dId = DetId(TPhit->detUnitId());
00427           DetId::Detector detector = dId.det();
00428           
00429           if (detector == DetId::Tracker) {
00430             n_tracker_simhits++;
00431 
00432             unsigned int subdetId = static_cast<unsigned int>(dId.subdetId());
00433             if (!UsePixels && (subdetId==PixelSubdetector::PixelBarrel || subdetId==PixelSubdetector::PixelEndcap) )
00434               continue;
00435 
00436             SiStripDetId* stripDetId = 0;
00437             if (subdetId==SiStripDetId::TIB||subdetId==SiStripDetId::TOB||
00438                 subdetId==SiStripDetId::TID||subdetId==SiStripDetId::TEC)
00439               stripDetId= new SiStripDetId(dId);
00440             
00441             bool newhit = true;
00442             for(std::vector<PSimHit>::const_iterator TPhitOK = tphits.begin(); TPhitOK != tphits.end(); TPhitOK++) {
00443               DetId dIdOK = DetId(TPhitOK->detUnitId());
00444               //no grouped, no splitting
00445               if (!UseGrouped && !UseSplitting)
00446                 if (LayerFromDetid(dId)==LayerFromDetid(dIdOK) &&
00447                     dId.subdetId()==dIdOK.subdetId()) newhit = false;
00448               //no grouped, splitting
00449               if (!UseGrouped && UseSplitting)
00450                 if (LayerFromDetid(dId)==LayerFromDetid(dIdOK) &&
00451                     dId.subdetId()==dIdOK.subdetId() &&
00452                     (stripDetId==0 || stripDetId->partnerDetId()!=dIdOK.rawId()))
00453                   newhit = false;
00454               //grouped, no splitting
00455               if (UseGrouped && !UseSplitting)
00456                 if (LayerFromDetid(dId)==LayerFromDetid(dIdOK) &&
00457                     dId.subdetId()==dIdOK.subdetId() &&
00458                     stripDetId!=0 && stripDetId->partnerDetId()==dIdOK.rawId())
00459                   newhit = false;
00460               //grouped, splitting
00461               if (UseGrouped && UseSplitting)
00462                 newhit = true;
00463             }
00464             if (newhit) {
00465               tphits.push_back(*TPhit);
00466             }
00467             delete stripDetId;
00468           }
00469           else if (detector == DetId::Muon) {
00470             n_muon_simhits++;
00471           }
00472         }
00473         
00474         n_tracker_recounted_simhits = tphits.size();
00475 
00476         // global number of simhits in TrackingParticle's is taken according to the type of reco::Track's to associate 
00477         if (SimToReco_useMuon) n_global_simhits = n_muon_simhits;
00478         if (SimToReco_useTracker) n_global_simhits += n_tracker_recounted_simhits;
00479      
00480         if (AbsoluteNumberOfHits_track) tracker_quality = static_cast<double>(tracker_nshared);
00481         else if (n_tracker_recounted_simhits!=0) 
00482           tracker_quality = static_cast<double>(tracker_nshared)/static_cast<double>(n_tracker_recounted_simhits);
00483         else tracker_quality = 0;
00484         
00485         if (AbsoluteNumberOfHits_muon) muon_quality = static_cast<double>(muon_nshared);
00486         else if (n_muon_simhits!=0) 
00487           muon_quality = static_cast<double>(muon_nshared)/static_cast<double>(n_muon_simhits);
00488         else muon_quality = 0;
00489 
00490         if (n_global_simhits!=0) 
00491           global_quality = static_cast<double>(global_nshared)/static_cast<double>(n_global_simhits);
00492         else global_quality = 0;
00493         
00494         bool matchOk = true;
00495         if (n_valid_hits==0) matchOk = false;
00496         else {
00497           if (n_tracker_valid_hits != 0) {
00498             if (tracker_quality < tracker_quality_cut) matchOk = false;
00499             
00500             tracker_purity = static_cast<double>(tracker_nshared)/static_cast<double>(n_tracker_valid_hits);
00501             if ((!AbsoluteNumberOfHits_track) && tracker_purity < PurityCut_track) matchOk = false;
00502             
00503             //if a track has just 3 hits in the Tracker we require that all 3 hits are shared
00504             if (ThreeHitTracksAreSpecial && n_tracker_valid_hits==3 && tracker_nshared<3) matchOk = false;
00505           }
00506           
00507           if (n_muon_valid_hits != 0) {
00508             if (muon_quality < muon_quality_cut) matchOk = false;
00509             
00510             muon_purity = static_cast<double>(muon_nshared)/static_cast<double>(n_muon_valid_hits);
00511             if ((!AbsoluteNumberOfHits_muon) && muon_purity < PurityCut_muon) matchOk = false;
00512           }
00513           
00514           global_purity = static_cast<double>(global_nshared)/static_cast<double>(n_valid_hits);
00515         }
00516         
00517         if (matchOk) {
00518           
00519           outputCollection.insert(edm::Ref<TrackingParticleCollection>(TPCollectionH, tpindex), 
00520                                   std::make_pair(tC[tindex],global_quality));
00521           any_trackingParticle_matched = true;
00522           
00523           edm::LogVerbatim("MuonAssociatorByHits")
00524             <<"************************************************************************************************************************"  
00525             <<"\n"<< "TrackingParticle " << tpindex <<", q = "<<(*trpart).charge()<<", p = "<<(*trpart).p()
00526             <<", pT = "<<(*trpart).pt()<<", eta = "<<(*trpart).eta()<<", phi = "<<(*trpart).phi()
00527             <<"\n"<<" pdg code = "<<(*trpart).pdgId()
00528             <<", made of "<<(*trpart).trackPSimHit().size()<<" PSimHits, recounted "<<n_global_simhits<<" PSimHits"
00529             <<" (tracker:"<<n_tracker_recounted_simhits<<"/muons:"<<n_muon_simhits<<")"
00530             <<", from "<<(*trpart).g4Tracks().size()<<" SimTrack:";
00531           for(TrackingParticle::g4t_iterator g4T=(*trpart).g4Track_begin(); 
00532               g4T!=(*trpart).g4Track_end(); 
00533               ++g4T) {
00534             edm::LogVerbatim("MuonAssociatorByHits")
00535               <<" Id:"<<(*g4T).trackId()<<"/Evt:("<<(*g4T).eventId().event()<<","<<(*g4T).eventId().bunchCrossing()<<")";
00536           }
00537           edm::LogVerbatim("MuonAssociatorByHits")
00538             << "\t **MATCHED** with quality = "<<global_quality<<" (tracker: "<<tracker_quality<<" / muon: "<<muon_quality<<")"
00539             << "\n\t               and purity = "<<global_purity<<" (tracker: "<<tracker_purity<<" / muon: "<<muon_purity<<") to:"
00540             <<"\n" <<"reco::Track "<<tindex<<", q = "<<(*track)->charge()<<", p = " << (*track)->p()<<", pT = " << (*track)->pt()
00541             <<", eta = "<<(*track)->momentum().eta()<<", phi = "<<(*track)->momentum().phi()
00542             <<"\n"<< " made of "<<n_valid_hits<<" valid RecHits (tracker:"<<n_tracker_valid_hits<<"/muons:"<<n_muon_valid_hits<<")";
00543         }
00544         else {
00545           // print something only if this TrackingParticle shares some hits with the current reco::Track
00546           if (global_nshared >0) {
00547             if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00548               <<"************************************************************************************************************************"  
00549               <<"\n"<<"TrackingParticle " << tpindex <<", q = "<<(*trpart).charge()<<", p = "<<(*trpart).p()
00550               <<", pT = "<<(*trpart).pt()<<", eta = "<<(*trpart).eta()<<", phi = "<<(*trpart).phi()
00551               <<"\n"<<" pdg code = "<<(*trpart).pdgId()
00552               <<", made of "<<(*trpart).trackPSimHit().size()<<" PSimHits, recounted "<<n_global_simhits<<" PSimHits"
00553               <<" (tracker:"<<n_tracker_recounted_simhits<<"/muons:"<<n_muon_simhits<<")"
00554               <<", from "<<(*trpart).g4Tracks().size()<<" SimTrack:";
00555             for(TrackingParticle::g4t_iterator g4T=(*trpart).g4Track_begin(); 
00556                 g4T!=(*trpart).g4Track_end(); 
00557                 ++g4T) {
00558               if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00559                 <<" Id:"<<(*g4T).trackId()<<"/Evt:("<<(*g4T).eventId().event()<<","<<(*g4T).eventId().bunchCrossing()<<")";
00560             }
00561             if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00562               <<"\t NOT matched  to reco::Track "<<tindex
00563               <<" with quality = "<<global_quality<<" (tracker: "<<tracker_quality<<" / muon: "<<muon_quality<<")"
00564               <<"\n\t and purity = "<<global_purity<<" (tracker: "<<tracker_purity<<" / muon: "<<muon_purity<<")";
00565           }
00566         }
00567       }  // loop over TrackingParticle's
00568     }   // if(n_matching_simhits)
00569   }    // loop over reco Tracks
00570   
00571   if (!any_trackingParticle_matched) {
00572     edm::LogVerbatim("MuonAssociatorByHits")
00573       <<"\n"
00574       <<"************************************************************************************************************************"
00575       << "\n NO TrackingParticle associated to ANY input reco::Track ! \n"
00576       <<"************************************************************************************************************************"<<"\n";  
00577   } else {
00578     edm::LogVerbatim("MuonAssociatorByHits")
00579       <<"************************************************************************************************************************"<<"\n";  
00580   }
00581   
00582   delete trackertruth;
00583   outputCollection.post_insert();
00584   return outputCollection;
00585 }
00586 
00587 int MuonAssociatorByHits::LayerFromDetid(const DetId& detId ) const
00588 {
00589   int layerNumber=0;
00590   if (detId.det() != DetId::Tracker) return layerNumber;
00591 
00592   unsigned int subdetId = static_cast<unsigned int>(detId.subdetId()); 
00593   if ( subdetId == StripSubdetector::TIB) 
00594     { 
00595       TIBDetId tibid(detId.rawId()); 
00596       layerNumber = tibid.layer();
00597     }
00598   else if ( subdetId ==  StripSubdetector::TOB )
00599     { 
00600       TOBDetId tobid(detId.rawId()); 
00601       layerNumber = tobid.layer();
00602     }
00603   else if ( subdetId ==  StripSubdetector::TID) 
00604     { 
00605       TIDDetId tidid(detId.rawId());
00606       layerNumber = tidid.wheel();
00607     }
00608   else if ( subdetId ==  StripSubdetector::TEC )
00609     { 
00610       TECDetId tecid(detId.rawId()); 
00611       layerNumber = tecid.wheel(); 
00612     }
00613   else if ( subdetId ==  PixelSubdetector::PixelBarrel ) 
00614     { 
00615       PXBDetId pxbid(detId.rawId()); 
00616       layerNumber = pxbid.layer();  
00617     }
00618   else if ( subdetId ==  PixelSubdetector::PixelEndcap ) 
00619     { 
00620       PXFDetId pxfid(detId.rawId()); 
00621       layerNumber = pxfid.disk();  
00622     }
00623   else
00624     edm::LogWarning("MuonAssociatorByHits") << "Unknown Tracker subdetector: subdetId = " <<  subdetId;
00625   
00626   return layerNumber;
00627 } 
00628 
00629 template<typename iter>
00630 void MuonAssociatorByHits::getMatchedIds(std::vector<SimHitIdpr>& tracker_matchedIds,std::vector<SimHitIdpr>& muon_matchedIds,
00631                                          int& n_valid_hits, int& n_tracker_valid_hits, 
00632                                          int& n_dt_valid_hits, int& n_csc_valid_hits, int& n_rpc_valid_hits,
00633                                          int& n_matched_hits, int& n_tracker_matched_hits, 
00634                                          int& n_dt_matched_hits, int& n_csc_matched_hits, int& n_rpc_matched_hits,
00635                                          iter begin,
00636                                          iter end,
00637                                          TrackerHitAssociator* trackertruth, 
00638                                          DTHitAssociator & dttruth,
00639                                          MuonTruth & csctruth,
00640                                          RPCHitAssociator & rpctruth,
00641                                          bool printRtS) const {
00642   tracker_matchedIds.clear();
00643   muon_matchedIds.clear();
00644   
00645   n_valid_hits = 0;            // number of valid rechits (Total)
00646   n_tracker_valid_hits = 0;    //                         (Tracker)
00647   n_dt_valid_hits = 0;         //                         (DT)
00648   n_csc_valid_hits = 0;        //                         (CSC)
00649   n_rpc_valid_hits = 0;        //                         (RPC)
00650   
00651   n_matched_hits = 0;          // number of associated rechits (Total)
00652   n_tracker_matched_hits = 0;  //                              (Tracker)
00653   n_dt_matched_hits = 0;       //                              (DT)
00654   n_csc_matched_hits = 0;      //                              (CSC)
00655   n_rpc_matched_hits = 0;      //                              (RPC)
00656   
00657   std::vector<SimHitIdpr> SimTrackIds;
00658 
00659   int iloop = 0;
00660   for (iter it = begin;  it != end; it++, iloop++) {
00661     stringstream hit_index;     
00662     hit_index<<iloop;
00663 
00664     unsigned int detid = getHitPtr(it)->geographicalId().rawId();    
00665     stringstream detector_id;
00666     detector_id<<detid;
00667 
00668     string hitlog = "TrackingRecHit "+hit_index.str();
00669     string wireidlog;
00670     std::vector<string> DTSimHits;
00671     
00672     DetId::Detector det = getHitPtr(it)->geographicalId().det();
00673     int subdet = getHitPtr(it)->geographicalId().subdetId();
00674     
00675     if (getHitPtr(it)->isValid()) {
00676       n_valid_hits++;
00677       SimTrackIds.clear();        
00678       
00679       if (det == DetId::Tracker) {
00680         hitlog = hitlog+" -Tracker - detID = "+detector_id.str();
00681         
00682         n_tracker_valid_hits++;
00683         SimTrackIds = trackertruth->associateHitId(*getHitPtr(it));
00684         
00685         if(!SimTrackIds.empty()) {
00686           n_tracker_matched_hits++;
00687           n_matched_hits++;
00688           for(size_t j=0; j<SimTrackIds.size(); j++){
00689             tracker_matchedIds.push_back(SimTrackIds[j]);                       
00690           }
00691         }
00692       }
00693       else if (det == DetId::Muon) {
00694 
00695         if (subdet == MuonSubdetId::DT) {
00696           hitlog = hitlog+" -Muon DT - detID = "+detector_id.str();       
00697 
00698           n_dt_valid_hits++;
00699           SimTrackIds = dttruth.associateHitId(*getHitPtr(it));  
00700           
00701           if (!SimTrackIds.empty()) {
00702             n_dt_matched_hits++;
00703             n_matched_hits++;
00704             for(unsigned int j=0; j<SimTrackIds.size(); j++) {
00705               muon_matchedIds.push_back(SimTrackIds[j]);
00706             }
00707           }
00708           if (dumpDT) {
00709             const DTRecHit1D * dtrechit = dynamic_cast<const DTRecHit1D *>(&(*getHitPtr(it)));
00710             DTWireId wireid = dtrechit->wireId();
00711             stringstream wid; 
00712             wid<<wireid;
00713             std::vector<PSimHit> dtSimHits = dttruth.associateHit(*getHitPtr(it));
00714             
00715             stringstream ndthits;
00716             ndthits<<dtSimHits.size();
00717             wireidlog = "\t DTWireId :"+wid.str()+", "+ndthits.str()+" associated PSimHit :";
00718 
00719             for (unsigned int j=0; j<dtSimHits.size(); j++) {
00720               stringstream index;
00721               index<<j;
00722               stringstream simhit;
00723               simhit<<dtSimHits[j];
00724               string simhitlog = "\t\t PSimHit "+index.str()+": "+simhit.str();
00725               DTSimHits.push_back(simhitlog);
00726             }
00727           } 
00728         }
00729         
00730         else if (subdet == MuonSubdetId::CSC) {
00731           hitlog = hitlog+" -Muon CSC- detID = "+detector_id.str();       
00732           
00733           n_csc_valid_hits++;
00734           SimTrackIds = csctruth.associateHitId(*getHitPtr(it)); 
00735 
00736           if (!SimTrackIds.empty()) {
00737             n_csc_matched_hits++;
00738             n_matched_hits++;
00739             for(unsigned int j=0; j<SimTrackIds.size(); j++) {
00740               muon_matchedIds.push_back(SimTrackIds[j]);
00741             }
00742           } 
00743         }
00744         
00745         else if (subdet == MuonSubdetId::RPC) {
00746           hitlog = hitlog+" -Muon RPC- detID = "+detector_id.str();       
00747           
00748           n_rpc_valid_hits++;
00749           SimTrackIds = rpctruth.associateRecHit(*getHitPtr(it));  
00750           
00751           if (!SimTrackIds.empty()) {
00752             n_rpc_matched_hits++;
00753             n_matched_hits++;
00754             for(unsigned int j=0; j<SimTrackIds.size(); j++) {
00755               muon_matchedIds.push_back(SimTrackIds[j]);
00756             }
00757           } 
00758         }
00759         
00760       } else if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00761         <<"TrackingRecHit "<<iloop<<"  *** WARNING *** Unexpected Hit from Detector = "<<det;
00762       
00763       hitlog = hitlog + write_matched_simtracks(SimTrackIds);
00764 
00765       if (printRtS) edm::LogVerbatim("MuonAssociatorByHits") << hitlog;
00766       if (printRtS && dumpDT && det==DetId::Muon && subdet==MuonSubdetId::DT) {
00767         edm::LogVerbatim("MuonAssociatorByHits") <<wireidlog;
00768         for (unsigned int j=0; j<DTSimHits.size(); j++) {
00769           edm::LogVerbatim("MuonAssociatorByHits") <<DTSimHits[j];
00770         }
00771       }
00772       
00773     } else if (printRtS) edm::LogVerbatim("MuonAssociatorByHits")
00774       <<"TrackingRecHit "<<iloop<<"  *** WARNING *** Invalid Hit On DetId.rawId() : "<<detid;
00775     
00776   }//trackingRecHit loop
00777 }
00778 
00779 int MuonAssociatorByHits::getShared(std::vector<SimHitIdpr>& matchedIds, 
00780                                     std::vector<SimHitIdpr>& idcachev,
00781                                     TrackingParticleCollection::const_iterator trpart) const {
00782   int nshared = 0;
00783 
00784   for(size_t j=0; j<matchedIds.size(); j++) {
00785     if(find(idcachev.begin(), idcachev.end(),matchedIds[j]) == idcachev.end() ) {
00786       idcachev.push_back(matchedIds[j]);
00787       
00788       for (TrackingParticle::g4t_iterator simtrack = trpart->g4Track_begin(); simtrack !=  trpart->g4Track_end(); ++simtrack) {
00789         if((*simtrack).trackId() == matchedIds[j].first && trpart->eventId() == matchedIds[j].second) {
00790           
00791           int countedhits = std::count(matchedIds.begin(), matchedIds.end(), matchedIds[j]);
00792           nshared += countedhits;
00793         }       
00794       } 
00795     }
00796   }
00797   return nshared;
00798 }
00799 
00800 std::string MuonAssociatorByHits::write_matched_simtracks(const std::vector<SimHitIdpr>& SimTrackIds) const {
00801 
00802   string hitlog;
00803 
00804   if (!SimTrackIds.empty()) {
00805     hitlog = " matched to SimTrack";
00806   
00807     for(size_t j=0; j<SimTrackIds.size(); j++){
00808       stringstream trackid;  
00809       trackid<<SimTrackIds[j].first;
00810       
00811       stringstream evtid;    
00812       evtid<<SimTrackIds[j].second.event();
00813       
00814       stringstream bunchxid; 
00815       bunchxid<<SimTrackIds[j].second.bunchCrossing();
00816       
00817       hitlog = hitlog+" Id:"+trackid.str()+"/Evt:("+evtid.str()+","+bunchxid.str()+") ";
00818     }
00819   } else hitlog = "  *** UNMATCHED ***";
00820 
00821   return hitlog;
00822 }

Generated on Tue Jun 9 17:47:38 2009 for CMSSW by  doxygen 1.5.4