CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/PhysicsTools/PFCandProducer/plugins/PFPileUp.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/PFCandProducer/plugins/PFPileUp.h"
00002 
00003 #include "DataFormats/ParticleFlowCandidate/interface/PileUpPFCandidate.h"
00004 #include "DataFormats/ParticleFlowCandidate/interface/PileUpPFCandidateFwd.h"
00005 #include "DataFormats/VertexReco/interface/Vertex.h"
00006 
00007 #include "FWCore/Framework/interface/ESHandle.h"
00008 
00009 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 #include "FWCore/Framework/interface/EventSetup.h"
00012 
00013 #include "DataFormats/Math/interface/deltaR.h"
00014 
00015 using namespace std;
00016 using namespace edm;
00017 using namespace reco;
00018 
00019 PFPileUp::PFPileUp(const edm::ParameterSet& iConfig) {
00020   
00021 
00022 
00023   inputTagPFCandidates_ 
00024     = iConfig.getParameter<InputTag>("PFCandidates");
00025 
00026   inputTagVertices_ 
00027     = iConfig.getParameter<InputTag>("Vertices");
00028 
00029   enable_ = iConfig.getParameter<bool>("Enable");
00030 
00031   verbose_ = 
00032     iConfig.getUntrackedParameter<bool>("verbose",false);
00033 
00034 
00035 
00036   produces<reco::PileUpPFCandidateCollection>();
00037   
00038 }
00039 
00040 
00041 
00042 PFPileUp::~PFPileUp() { }
00043 
00044 
00045 
00046 void PFPileUp::beginJob() { }
00047 
00048 
00049 void PFPileUp::produce(Event& iEvent, 
00050                           const EventSetup& iSetup) {
00051   
00052 //   LogDebug("PFPileUp")<<"START event: "<<iEvent.id().event()
00053 //                       <<" in run "<<iEvent.id().run()<<endl;
00054   
00055    
00056   // get PFCandidates
00057 
00058   auto_ptr< reco::PileUpPFCandidateCollection > 
00059     pOutput( new reco::PileUpPFCandidateCollection ); 
00060   
00061   if(enable_) {
00062 
00063     Handle<PFCandidateCollection> pfCandidates;
00064     iEvent.getByLabel( inputTagPFCandidates_, pfCandidates);
00065 
00066   
00067     // get vertices 
00068 
00069     Handle<VertexCollection> vertices;
00070     iEvent.getByLabel( inputTagVertices_, vertices);
00071   
00072     for( unsigned i=0; i<pfCandidates->size(); i++ ) {
00073     
00074       // const reco::PFCandidate& cand = (*pfCandidates)[i];
00075       PFCandidatePtr candptr(pfCandidates, i);
00076 
00077       //     PFCandidateRef pfcandref(pfCandidates,i); 
00078 
00079       VertexRef vertexref;
00080 
00081       switch( candptr->particleId() ) {
00082       case PFCandidate::h:
00083         vertexref = chargedHadronVertex( vertices, *candptr );
00084         break;
00085       default:
00086         continue;
00087       } 
00088     
00089       // no associated vertex, or primary vertex
00090       // not pile-up
00091       if( vertexref.isNull() || 
00092           vertexref.key()==0 ) continue;
00093 
00094       pOutput->push_back( PileUpPFCandidate( candptr, vertexref ) );
00095       pOutput->back().setSourceCandidatePtr( candptr );
00096     }
00097   }
00098   iEvent.put( pOutput );
00099   
00100 }
00101 
00102 
00103 
00104 VertexRef 
00105 PFPileUp::chargedHadronVertex( const Handle<VertexCollection>& vertices, const PFCandidate& pfcand ) const {
00106 
00107   
00108   reco::TrackBaseRef trackBaseRef( pfcand.trackRef() );
00109   
00110   unsigned nFoundVertex = 0;
00111   size_t  iVertex = 0;
00112   unsigned index=0;
00113   typedef reco::VertexCollection::const_iterator IV;
00114   for(IV iv=vertices->begin(); iv!=vertices->end(); ++iv, ++index) {
00115 //     cout<<(*iv).x()<<" "
00116 //      <<(*iv).y()<<" "
00117 //      <<(*iv).z()<<endl;
00118 
00119     const reco::Vertex& vtx = *iv;
00120     
00121     typedef reco::Vertex::trackRef_iterator IT;
00122     
00123     // loop on tracks in vertices
00124     for(IT iTrack=vtx.tracks_begin(); 
00125         iTrack!=vtx.tracks_end(); ++iTrack) {
00126          
00127       const reco::TrackBaseRef& baseRef = *iTrack;
00128 
00129       // one of the tracks in the vertex is the same as 
00130       // the track considered in the function
00131       if(baseRef == trackBaseRef ) {
00132         iVertex = index;
00133         nFoundVertex++;
00134       }         
00135     }
00136   } 
00137 
00138   if( nFoundVertex == 1) {
00139     return VertexRef( vertices, iVertex);
00140   }
00141   else if(nFoundVertex>1) assert(false);
00142 
00143   assert( !iVertex );
00144 
00145   // no vertex found with this track. 
00146   // keep this track
00147   return VertexRef();
00148 }
00149 
00150