Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:28:03

0001 #include <memory>
0002 #include <set>
0003 
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/Utilities/interface/InputTag.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 
0010 #include "DataFormats/Common/interface/Handle.h"
0011 #include "DataFormats/TrackReco/interface/Track.h"
0012 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0013 #include "DataFormats/VertexReco/interface/Vertex.h"
0014 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0015 #include "RecoVertex/VertexTools/interface/VertexDistance3D.h"
0016 #include "RecoVertex/VertexTools/interface/SharedTracks.h"
0017 #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h"
0018 #include "RecoVertex/VertexPrimitives/interface/ConvertToFromReco.h"
0019 #include "RecoVertex/VertexPrimitives/interface/VertexState.h"
0020 
0021 template <class VTX>
0022 class TemplatedVertexMerger : public edm::stream::EDProducer<> {
0023 public:
0024   typedef std::vector<VTX> Product;
0025   TemplatedVertexMerger(const edm::ParameterSet &params);
0026 
0027   void produce(edm::Event &event, const edm::EventSetup &es) override;
0028 
0029 private:
0030   bool trackFilter(const reco::TrackRef &track) const;
0031 
0032   edm::EDGetTokenT<Product> token_secondaryVertex;
0033   double maxFraction;
0034   double minSignificance;
0035 };
0036 
0037 template <class VTX>
0038 TemplatedVertexMerger<VTX>::TemplatedVertexMerger(const edm::ParameterSet &params)
0039     : maxFraction(params.getParameter<double>("maxFraction")),
0040       minSignificance(params.getParameter<double>("minSignificance")) {
0041   token_secondaryVertex = consumes<Product>(params.getParameter<edm::InputTag>("secondaryVertices"));
0042   produces<Product>();
0043 }
0044 
0045 template <class VTX>
0046 void TemplatedVertexMerger<VTX>::produce(edm::Event &event, const edm::EventSetup &es) {
0047   using namespace reco;
0048 
0049   edm::Handle<Product> secondaryVertices;
0050   event.getByToken(token_secondaryVertex, secondaryVertices);
0051 
0052   VertexDistance3D dist;
0053   auto recoVertices = std::make_unique<Product>();
0054   for (typename Product::const_iterator sv = secondaryVertices->begin(); sv != secondaryVertices->end(); ++sv) {
0055     recoVertices->push_back(*sv);
0056   }
0057   for (typename Product::iterator sv = recoVertices->begin(); sv != recoVertices->end(); ++sv) {
0058     bool shared = false;
0059     VertexState s1(RecoVertex::convertPos(sv->position()), RecoVertex::convertError(sv->error()));
0060     for (typename Product::iterator sv2 = recoVertices->begin(); sv2 != recoVertices->end(); ++sv2) {
0061       VertexState s2(RecoVertex::convertPos(sv2->position()), RecoVertex::convertError(sv2->error()));
0062       double fr = vertexTools::computeSharedTracks(*sv2, *sv);
0063       //        std::cout << sv2-recoVertices->begin() << " vs " << sv-recoVertices->begin() << " : " << fr << " "  <<  computeSharedTracks(*sv, *sv2) << " sig " << dist.distance(*sv,*sv2).significance() << std::endl;
0064       //      std::cout << (fr > maxFraction) << " && " << (dist.distance(*sv,*sv2).significance() < 2)  <<  " && " <<  (sv-sv2!=0)  << " && " <<  (fr >= computeSharedTracks(*sv2, *sv))  << std::endl;
0065       if (fr > maxFraction && dist.distance(s1, s2).significance() < minSignificance && sv - sv2 != 0 &&
0066           fr >= vertexTools::computeSharedTracks(*sv, *sv2)) {
0067         shared = true;
0068         // std::cout << "shared " << sv-recoVertices->begin() << " and "  << sv2-recoVertices->begin() << " fractions: " << fr << " , "  << computeSharedTracks(*sv2, *sv) << " sig: " <<  dist.distance(*sv,*sv2).significance() <<  std::endl;
0069       }
0070     }
0071     if (shared) {
0072       sv = recoVertices->erase(sv) - 1;
0073     }
0074     //    std::cout << "it = " <<  sv-recoVertices->begin() << " new size is: " << recoVertices->size() <<   std::endl;
0075   }
0076 
0077   event.put(std::move(recoVertices));
0078 }
0079 
0080 typedef TemplatedVertexMerger<reco::Vertex> VertexMerger;
0081 typedef TemplatedVertexMerger<reco::VertexCompositePtrCandidate> CandidateVertexMerger;
0082 
0083 DEFINE_FWK_MODULE(VertexMerger);
0084 DEFINE_FWK_MODULE(CandidateVertexMerger);