File indexing completed on 2024-09-07 04:37:21
0001 #include <string>
0002 #include <memory>
0003
0004
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/global/EDProducer.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "DataFormats/VertexReco/interface/Vertex.h"
0012
0013 #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h"
0014 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0015 #include "DataFormats/Common/interface/RefToPtr.h"
0016 #include "DataFormats/Candidate/interface/VertexCompositeCandidate.h"
0017 #include "DataFormats/Candidate/interface/VertexCompositeCandidateFwd.h"
0018 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
0019
0020 namespace pat {
0021 class PATSecondaryVertexSlimmer : public edm::global::EDProducer<> {
0022 public:
0023 explicit PATSecondaryVertexSlimmer(const edm::ParameterSet&);
0024 ~PATSecondaryVertexSlimmer() override;
0025
0026 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0027
0028 private:
0029 const edm::EDGetTokenT<reco::VertexCompositePtrCandidateCollection> src_;
0030 const edm::EDGetTokenT<std::vector<reco::Vertex> > srcLegacy_;
0031 const edm::EDGetTokenT<reco::VertexCompositeCandidateCollection> srcV0s_;
0032 const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection> > map_;
0033 const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection> > map2_;
0034 };
0035 }
0036
0037 pat::PATSecondaryVertexSlimmer::PATSecondaryVertexSlimmer(const edm::ParameterSet& iConfig)
0038 : src_(consumes<reco::VertexCompositePtrCandidateCollection>(iConfig.getParameter<edm::InputTag>("src"))),
0039 srcLegacy_(mayConsume<std::vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("src"))),
0040 srcV0s_(mayConsume<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("src"))),
0041 map_(consumes<edm::Association<pat::PackedCandidateCollection> >(
0042 iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0043 map2_(mayConsume<edm::Association<pat::PackedCandidateCollection> >(
0044 iConfig.existsAs<edm::InputTag>("lostTracksCandidates")
0045 ? iConfig.getParameter<edm::InputTag>("lostTracksCandidates")
0046 : edm::InputTag("lostTracks"))) {
0047 produces<reco::VertexCompositePtrCandidateCollection>();
0048 }
0049
0050 pat::PATSecondaryVertexSlimmer::~PATSecondaryVertexSlimmer() {}
0051
0052 void pat::PATSecondaryVertexSlimmer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0053 auto outPtr = std::make_unique<reco::VertexCompositePtrCandidateCollection>();
0054
0055 edm::Handle<reco::VertexCompositePtrCandidateCollection> candVertices;
0056 iEvent.getByToken(src_, candVertices);
0057
0058 edm::Handle<edm::Association<pat::PackedCandidateCollection> > pf2pc;
0059 iEvent.getByToken(map_, pf2pc);
0060
0061
0062 if (candVertices.isValid()) {
0063 outPtr->reserve(candVertices->size());
0064 for (unsigned int i = 0, n = candVertices->size(); i < n; ++i) {
0065 reco::VertexCompositePtrCandidate v = (*candVertices)[i];
0066
0067 std::vector<reco::CandidatePtr> daughters = v.daughterPtrVector();
0068 v.clearDaughters();
0069
0070 for (std::vector<reco::CandidatePtr>::const_iterator it = daughters.begin(); it != daughters.end(); ++it) {
0071 if ((*pf2pc)[*it].isNonnull() && (*pf2pc)[*it]->numberOfHits() > 0)
0072 v.addDaughter(reco::CandidatePtr(edm::refToPtr((*pf2pc)[*it])));
0073 }
0074
0075 outPtr->push_back(v);
0076 }
0077 }
0078
0079 else {
0080 edm::Handle<std::vector<reco::Vertex> > vertices;
0081 iEvent.getByToken(srcLegacy_, vertices);
0082
0083 if (vertices.isValid()) {
0084 edm::Handle<edm::Association<pat::PackedCandidateCollection> > pf2pc2;
0085 iEvent.getByToken(map2_, pf2pc2);
0086
0087 outPtr->reserve(vertices->size());
0088 for (unsigned int i = 0, n = vertices->size(); i < n; ++i) {
0089 const reco::Vertex& v = (*vertices)[i];
0090 outPtr->push_back(reco::VertexCompositePtrCandidate(0, v.p4(), v.position(), v.error(), v.chi2(), v.ndof()));
0091
0092 for (reco::Vertex::trackRef_iterator it = v.tracks_begin(); it != v.tracks_end(); it++) {
0093 if (v.trackWeight(*it) > 0.5) {
0094 if ((*pf2pc)[*it].isNonnull() && (*pf2pc)[*it]->numberOfHits() > 0) {
0095 outPtr->back().addDaughter(reco::CandidatePtr(edm::refToPtr((*pf2pc)[*it])));
0096 } else {
0097 if ((*pf2pc2)[*it].isNonnull()) {
0098 outPtr->back().addDaughter(reco::CandidatePtr(edm::refToPtr((*pf2pc2)[*it])));
0099 } else {
0100 edm::LogError("PATSecondaryVertexSlimmer") << "HELPME" << std::endl;
0101 }
0102 }
0103 }
0104 }
0105 }
0106 } else {
0107
0108 edm::Handle<reco::VertexCompositeCandidateCollection> srcV0s;
0109 iEvent.getByToken(srcV0s_, srcV0s);
0110
0111 edm::Handle<edm::Association<pat::PackedCandidateCollection> > pf2pc2;
0112 iEvent.getByToken(map2_, pf2pc2);
0113
0114 outPtr->reserve(srcV0s->size());
0115 for (unsigned int i = 0, n = srcV0s->size(); i < n; ++i) {
0116 const reco::VertexCompositeCandidate& v = (*srcV0s)[i];
0117 outPtr->push_back(reco::VertexCompositePtrCandidate(
0118 0, v.p4(), v.vertex(), v.vertexCovariance(), v.vertexChi2(), v.vertexNdof()));
0119
0120 for (size_t dIdx = 0; dIdx < v.numberOfDaughters(); dIdx++) {
0121 reco::TrackRef trackRef = (dynamic_cast<const reco::RecoChargedCandidate*>(v.daughter(dIdx)))->track();
0122 if ((*pf2pc)[trackRef].isNonnull() && (*pf2pc)[trackRef]->numberOfHits() > 0) {
0123 outPtr->back().addDaughter(reco::CandidatePtr(edm::refToPtr((*pf2pc)[trackRef])));
0124 } else {
0125 if ((*pf2pc2)[trackRef].isNonnull()) {
0126 outPtr->back().addDaughter(reco::CandidatePtr(edm::refToPtr((*pf2pc2)[trackRef])));
0127 } else {
0128 edm::LogError("PATSecondaryVertexSlimmer") << "HELPME" << std::endl;
0129 }
0130 }
0131 }
0132 }
0133
0134 }
0135 }
0136
0137 iEvent.put(std::move(outPtr));
0138 }
0139
0140 using pat::PATSecondaryVertexSlimmer;
0141 #include "FWCore/Framework/interface/MakerMacros.h"
0142 DEFINE_FWK_MODULE(PATSecondaryVertexSlimmer);