CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoParticleFlow/PFProducer/plugins/GsfElectronLinker.cc

Go to the documentation of this file.
00001 #include "RecoParticleFlow/PFProducer/plugins/GsfElectronLinker.h"
00002 
00003 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00004 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00005 #include "RecoParticleFlow/PFProducer/interface/GsfElectronEqual.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 
00009 GsfElectronLinker::GsfElectronLinker(const edm::ParameterSet & iConfig) {
00010   inputTagPFCandidates_ 
00011     = iConfig.getParameter<edm::InputTag>("PFCandidate");
00012   inputTagGsfElectrons_
00013     = iConfig.getParameter<edm::InputTag>("GsfElectrons");
00014   nameOutputPF_ 
00015     = iConfig.getParameter<std::string>("OutputPF");
00016   
00017   produces<reco::PFCandidateCollection>(nameOutputPF_);
00018   produces<edm::ValueMap<reco::PFCandidateRef> > (nameOutputPF_);
00019 }
00020 
00021 GsfElectronLinker::~GsfElectronLinker() {;}
00022 
00023 void GsfElectronLinker::beginRun(edm::Run& run,const edm::EventSetup & es) {;}
00024 
00025 void GsfElectronLinker::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00026   electronCandidateMap_.clear();
00027   std::auto_ptr<reco::PFCandidateCollection>
00028     pfCandidates_p(new reco::PFCandidateCollection);
00029 
00030   std::auto_ptr<edm::ValueMap<reco::PFCandidateRef> > 
00031     pfMap_p(new edm::ValueMap<reco::PFCandidateRef>());
00032   edm::ValueMap<reco::PFCandidateRef>::Filler pfMapFiller(*pfMap_p);
00033 
00034   edm::Handle<reco::PFCandidateCollection> pfCandidates;
00035   bool status=fetchCandidateCollection(pfCandidates, 
00036                                        inputTagPFCandidates_, 
00037                                        iEvent );
00038 
00039   edm::Handle<reco::GsfElectronCollection> gsfElectrons;
00040   status=fetchGsfElectronCollection(gsfElectrons,
00041                                     inputTagGsfElectrons_,
00042                                     iEvent );
00043 
00044   unsigned ncand=(status)?pfCandidates->size():0;
00045 
00046   for( unsigned i=0; i<ncand; ++i ) {
00047     edm::Ptr<reco::PFCandidate> candPtr(pfCandidates,i);
00048     reco::PFCandidate cand = (*pfCandidates)[i];    
00049     cand.setSourceCandidatePtr(candPtr);
00050     
00051     // if not an electron or not GsfTrackRef
00052     if( (cand.particleId()!=reco::PFCandidate::e) ) {
00053       pfCandidates_p->push_back(cand);
00054       continue; // Watch out ! Continue
00055     }
00056     
00057     // if it is an electron. Find the GsfElectron with the same GsfTrack
00058     const reco::GsfTrackRef & gsfTrackRef(cand.gsfTrackRef());
00059     GsfElectronEqual myEqual(gsfTrackRef);
00060     std::vector<reco::GsfElectron>::const_iterator itcheck=find_if(gsfElectrons->begin(),gsfElectrons->end(),myEqual);
00061     if(itcheck==gsfElectrons->end()) {
00062       std::ostringstream err;
00063       err << " Problem in GsfElectronLinker: no GsfElectron " << std::endl;
00064       edm::LogError("GsfElectronLinker") << err.str();
00065       continue; // Watch out ! Continue
00066     } 
00067     reco::GsfElectronRef electronRef(gsfElectrons,itcheck-gsfElectrons->begin());
00068     cand.setGsfElectronRef(electronRef);
00069     electronCandidateMap_[electronRef]=i;
00070     pfCandidates_p->push_back(cand);
00071   }  
00072 
00073   const edm::OrphanHandle<reco::PFCandidateCollection> pfCandidateRefProd = 
00074     iEvent.put(pfCandidates_p,nameOutputPF_);
00075 
00076   fillValueMap(gsfElectrons,pfCandidateRefProd,pfMapFiller);
00077   iEvent.put(pfMap_p,nameOutputPF_);
00078 }
00079 
00080 
00081 bool GsfElectronLinker::fetchCandidateCollection(edm::Handle<reco::PFCandidateCollection>& c, 
00082                                                     const edm::InputTag& tag, 
00083                                                     const edm::Event& iEvent) const {  
00084   bool found = iEvent.getByLabel(tag, c);
00085   
00086   if(!found )
00087     {
00088       std::ostringstream  err;
00089       err<<" cannot get PFCandidates: "
00090          <<tag<<std::endl;
00091       edm::LogError("GsfElectronLinker")<<err.str();
00092     }
00093   return found;
00094   
00095 }
00096 
00097 bool GsfElectronLinker::fetchGsfElectronCollection(edm::Handle<reco::GsfElectronCollection>& c, 
00098                                                 const edm::InputTag& tag, 
00099                                                 const edm::Event& iEvent) const {
00100   bool found = iEvent.getByLabel(tag, c);
00101   
00102   if(!found )
00103     {
00104       std::ostringstream  err;
00105       err<<" cannot get GsfElectrons: "
00106          <<tag<<std::endl;
00107       edm::LogError("GsfElectronLinker")<<err.str();
00108     }
00109   return found;
00110 
00111 }
00112 
00113 void GsfElectronLinker::fillValueMap(edm::Handle<reco::GsfElectronCollection>& electrons,
00114                                   const edm::OrphanHandle<reco::PFCandidateCollection> & pfHandle,
00115                                   edm::ValueMap<reco::PFCandidateRef>::Filler & filler) const {
00116   unsigned nElectrons=electrons->size();
00117   std::vector<reco::PFCandidateRef> values;
00118   for(unsigned ielec=0;ielec<nElectrons;++ielec) {
00119     reco::GsfElectronRef gsfElecRef(electrons,ielec);
00120     std::map<reco::GsfElectronRef,unsigned>::const_iterator itcheck=electronCandidateMap_.find(gsfElecRef);
00121     if(itcheck==electronCandidateMap_.end()) {
00122       values.push_back(reco::PFCandidateRef());
00123     } else {
00124       values.push_back(reco::PFCandidateRef(pfHandle,itcheck->second));
00125     }
00126   }
00127   filler.insert(electrons,values.begin(),values.end());
00128 }