CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Validation/RecoParticleFlow/plugins/GenJetClosestMatchSelectorDefinition.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_PFCandProducer_GenJetClosestMatchSelectorDefinition
00002 #define PhysicsTools_PFCandProducer_GenJetClosestMatchSelectorDefinition
00003 
00004 #include "DataFormats/JetReco/interface/GenJet.h"
00005 
00006 #include "DataFormats/Math/interface/deltaR.h"
00007 
00008 #include <iostream>
00009 
00010 struct GenJetClosestMatchSelectorDefinition {
00011 
00012 
00013   typedef reco::GenJetCollection collection;
00014   typedef edm::Handle< collection > HandleToCollection;
00015   typedef std::vector< reco::GenJet *> container;
00016   typedef container::const_iterator const_iterator;
00017 
00018   GenJetClosestMatchSelectorDefinition ( const edm::ParameterSet & cfg ) {
00019 
00020     matchTo_ = cfg.getParameter< edm::InputTag >( "MatchTo" ); 
00021   }
00022   
00023   const_iterator begin() const { return selected_.begin(); }
00024 
00025   const_iterator end() const { return selected_.end(); }
00026 
00027   void select( const HandleToCollection & hc, 
00028                const edm::Event & e,
00029                const edm::EventSetup& s) 
00030   {
00031            
00032     selected_.clear();
00033     
00034     edm::Handle< edm::View<reco::Candidate> > matchCandidates; 
00035     e.getByLabel( matchTo_, matchCandidates);
00036 
00037 
00038     unsigned key=0;
00039 
00040     //    std::cout<<"number of candidates "<<matchCandidates->size()<<std::endl;
00041 
00042     typedef edm::View<reco::Candidate>::const_iterator IC;
00043     for( IC ic = matchCandidates->begin(); 
00044          ic!= matchCandidates->end(); ++ic ) {
00045       
00046       double eta2 = ic->eta();
00047       double phi2 = ic->phi();
00048 
00049       //      std::cout<<"cand "<<eta2<<" "<<phi2<<std::endl; 
00050 
00051       
00052       // look for the closest gen jet
00053       double deltaR2Min = 9999;
00054       collection::const_iterator closest = hc->end();
00055       for( collection::const_iterator genjet = hc->begin(); 
00056            genjet != hc->end();
00057            ++genjet, ++key) {
00058         
00059         reco::GenJetRef genJetRef(hc, key);
00060       
00061         // is it matched? 
00062         
00063         double eta1 = genjet->eta();
00064         double phi1 = genjet->phi();
00065         
00066         
00067         double deltaR2 = reco::deltaR2(eta1, phi1, eta2, phi2);
00068         
00069         // std::cout<<"  genjet "<<eta1<<" "<<phi1<<" "<<deltaR2<<std::endl;
00070         
00071         // cut should be a parameter
00072         if( deltaR2<deltaR2Min ) {
00073           deltaR2Min = deltaR2;
00074           closest = genjet;
00075         }
00076       }
00077 
00078       if(deltaR2Min<0.01 ) {
00079         // std::cout<<deltaR2Min<<std::endl;
00080         selected_.push_back( new reco::GenJet(*closest) );
00081       }
00082     } // end collection iteration
00083 
00084     // std::cout<<selected_.size()<<std::endl;
00085   } // end select()
00086 
00087   size_t size() const { return selected_.size(); }
00088 
00089 private:
00090   container selected_;  
00091   edm::InputTag  matchTo_;
00092 };
00093 
00094 #endif