CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoVertex/AdaptiveVertexFinder/plugins/VertexMerger.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <set>
00003 
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/Framework/interface/MakerMacros.h"
00007 #include "FWCore/Utilities/interface/InputTag.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009 
00010 #include "DataFormats/Common/interface/Handle.h"
00011 #include "DataFormats/TrackReco/interface/Track.h"
00012 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00013 #include "DataFormats/VertexReco/interface/Vertex.h"
00014 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00015 #include "RecoVertex/VertexTools/interface/VertexDistance3D.h"
00016 
00017 class VertexMerger : public edm::EDProducer {
00018     public:
00019         VertexMerger(const edm::ParameterSet &params);
00020 
00021         virtual void produce(edm::Event &event, const edm::EventSetup &es);
00022 
00023     private:
00024         bool trackFilter(const reco::TrackRef &track) const;
00025 
00026 //      edm::InputTag                           primaryVertexCollection;
00027         edm::InputTag                           secondaryVertexCollection;
00028         double                                  maxFraction;
00029 };
00030 
00031 VertexMerger::VertexMerger(const edm::ParameterSet &params) :
00032 //      primaryVertexCollection(params.getParameter<edm::InputTag>("primaryVertices")),
00033         secondaryVertexCollection(params.getParameter<edm::InputTag>("secondaryVertices")),
00034         maxFraction(params.getParameter<double>("maxFraction"))
00035 {
00036         produces<reco::VertexCollection>();
00037 }
00038 
00039 static double computeSharedTracks(const reco::Vertex &pv,
00040                                         const reco::Vertex &sv)
00041 {
00042         std::set<reco::TrackRef> pvTracks;
00043         for(std::vector<reco::TrackBaseRef>::const_iterator iter = pv.tracks_begin();
00044             iter != pv.tracks_end(); iter++) {
00045                 if (pv.trackWeight(*iter) >= 0.5)
00046                         pvTracks.insert(iter->castTo<reco::TrackRef>());
00047         }
00048 
00049         unsigned int count = 0, total = 0;
00050         for(std::vector<reco::TrackBaseRef>::const_iterator iter = sv.tracks_begin();
00051             iter != sv.tracks_end(); iter++) {
00052                 if (sv.trackWeight(*iter) >= 0.5) {
00053                         total++;
00054                         count += pvTracks.count(iter->castTo<reco::TrackRef>());
00055                 }
00056         }
00057 
00058         return (double)count / (double)total;
00059 }
00060 
00061 void VertexMerger::produce(edm::Event &event, const edm::EventSetup &es)
00062 {
00063         using namespace reco;
00064 
00065         edm::Handle<VertexCollection> secondaryVertices;
00066         event.getByLabel(secondaryVertexCollection, secondaryVertices);
00067 
00068         VertexDistance3D dist;
00069         std::auto_ptr<VertexCollection> recoVertices(new VertexCollection);
00070         for(std::vector<reco::Vertex>::const_iterator sv = secondaryVertices->begin();
00071             sv != secondaryVertices->end(); ++sv) {
00072           recoVertices->push_back(*sv);
00073         }
00074        for(std::vector<reco::Vertex>::iterator sv = recoVertices->begin();
00075             sv != recoVertices->end(); ++sv) {
00076 
00077         bool shared=false;
00078        for(std::vector<reco::Vertex>::iterator sv2 = recoVertices->begin();
00079             sv2 != recoVertices->end(); ++sv2) {
00080                   double fr=computeSharedTracks(*sv, *sv2);
00081 //                std::cout << sv2-recoVertices->begin() << " vs " << sv-recoVertices->begin() << " : " << fr << " "  <<  computeSharedTracks(*sv2, *sv) << " sig " << dist.distance(*sv,*sv2).significance() << std::endl;
00082   //              std::cout << (fr > maxFraction) << " && " << (dist.distance(*sv,*sv2).significance() < 2)  <<  " && " <<  (sv-sv2!=0)  << " && " <<  (fr >= computeSharedTracks(*sv2, *sv))  << std::endl;
00083                 if (fr > maxFraction && dist.distance(*sv,*sv2).significance() < 2 && sv-sv2!=0 
00084                     && fr >= computeSharedTracks(*sv2, *sv) )
00085                   {
00086                       shared=true; 
00087                 //      std::cout << "shared " << sv-recoVertices->begin() << " and "  << sv2-recoVertices->begin() << " fractions: " << fr << " , "  << computeSharedTracks(*sv2, *sv) << " sig: " <<  dist.distance(*sv,*sv2).significance() <<  std::endl;
00088          
00089                   }
00090                  
00091 
00092         }
00093         if(shared) { sv=recoVertices->erase(sv)-1; }
00094 //         std::cout << "it = " <<  sv-recoVertices->begin() << " new size is: " << recoVertices->size() <<   std::endl;
00095        }
00096 
00097         event.put(recoVertices);
00098 }
00099 
00100 DEFINE_FWK_MODULE(VertexMerger);