diff --git a/PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h b/PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h index b26852ef04e45..1dd4da6bc96ef 100644 --- a/PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h +++ b/PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h @@ -14,9 +14,9 @@ \author Steven Lowette */ -#include "FWCore/Framework/interface/ConsumesCollector.h" #include "DataFormats/VertexReco/interface/Vertex.h" #include "DataFormats/VertexReco/interface/VertexFwd.h" +#include "FWCore/Utilities/interface/InputTag.h" class TransientTrackBuilder; @@ -24,28 +24,28 @@ namespace reco { class Track; } -namespace edm { - class Event; - class EventSetup; -} // namespace edm - namespace pat { class Electron; class Muon; class LeptonVertexSignificance { public: - LeptonVertexSignificance(); - LeptonVertexSignificance(const edm::EventSetup& iSetup, edm::ConsumesCollector&& iC); - ~LeptonVertexSignificance(); + LeptonVertexSignificance() = default; + ~LeptonVertexSignificance() = default; + + //NOTE: expects vertices from "offlinePrimaryVerticesFromCTFTracks" + static edm::InputTag vertexCollectionTag(); - float calculate(const Electron& anElectron, const edm::Event& iEvent); - float calculate(const Muon& aMuon, const edm::Event& iEvent); + //NOTE: expects TransientTrackBuilder to be a copy of one from record TransientTrackRecord with label "TransientTrackBuilder" + float calculate(const Electron& anElectron, + const reco::VertexCollection& vertices, + const TransientTrackBuilder& builder); + float calculate(const Muon& aMuon, const reco::VertexCollection& vertices, const TransientTrackBuilder& builder); private: - float calculate(const reco::Track& track, const edm::Event& iEvent); - TransientTrackBuilder* theTrackBuilder_; - edm::EDGetTokenT vertexToken_; + float calculate(const reco::Track& track, + const reco::VertexCollection& vertices, + const TransientTrackBuilder& builder); }; } // namespace pat diff --git a/PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h b/PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h index 3e02458e0b729..8d790acb79f80 100644 --- a/PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h +++ b/PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h @@ -55,10 +55,12 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> { if (jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT") << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n"; - jetCorrParameters_ = new JetCorrectorParameters(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); - jecUncertainty_ = new JetCorrectionUncertainty(*jetCorrParameters_); + jetCorrParameters_ = + std::make_unique(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); + jecUncertainty_ = std::make_unique(*jetCorrParameters_); } else { jetCorrPayloadName_ = cfg.getParameter("jetCorrPayloadName"); + jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_)); } } @@ -79,10 +81,6 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> { produces(); } - ~ShiftedJetProducerT() override { - delete jetCorrParameters_; - delete jecUncertainty_; - } private: void produce(edm::Event& evt, const edm::EventSetup& es) override { @@ -103,11 +101,9 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> { auto shiftedJets = std::make_unique(); if (!jetCorrPayloadName_.empty()) { - edm::ESHandle jetCorrParameterSet; - es.get().get(jetCorrPayloadName_, jetCorrParameterSet); - const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_]; - delete jecUncertainty_; - jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters); + const JetCorrectorParametersCollection& jetCorrParameterSet = es.getData(jetCorrPayloadToken_); + const JetCorrectorParameters& jetCorrParameters = (jetCorrParameterSet)[jetCorrUncertaintyTag_]; + jecUncertainty_ = std::make_unique(jetCorrParameters); } for (typename JetCollection::const_iterator originalJet = originalJets->begin(); originalJet != originalJets->end(); @@ -175,9 +171,10 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> { edm::FileInPath jetCorrInputFileName_; std::string jetCorrPayloadName_; + edm::ESGetToken jetCorrPayloadToken_; std::string jetCorrUncertaintyTag_; - JetCorrectorParameters* jetCorrParameters_; - JetCorrectionUncertainty* jecUncertainty_; + std::unique_ptr jetCorrParameters_; + std::unique_ptr jecUncertainty_; bool addResidualJES_; edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction diff --git a/PhysicsTools/PatUtils/plugins/PackedCandidatesTrackLiteModifier.cc b/PhysicsTools/PatUtils/plugins/PackedCandidatesTrackLiteModifier.cc index 46ee7d2f0ce23..8741427e6b6e7 100644 --- a/PhysicsTools/PatUtils/plugins/PackedCandidatesTrackLiteModifier.cc +++ b/PhysicsTools/PatUtils/plugins/PackedCandidatesTrackLiteModifier.cc @@ -1,4 +1,3 @@ -#include "FWCore/Framework/interface/EDProducer.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" diff --git a/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.cc b/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.cc index 510534530c9d7..b1af5da874104 100644 --- a/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.cc +++ b/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.cc @@ -40,7 +40,6 @@ class ShiftedPFCandidateProducerForNoPileUpPFMEt : public edm::stream::EDProducer<> { public: explicit ShiftedPFCandidateProducerForNoPileUpPFMEt(const edm::ParameterSet&); - ~ShiftedPFCandidateProducerForNoPileUpPFMEt() override; private: void produce(edm::Event&, const edm::EventSetup&) override; @@ -50,9 +49,10 @@ class ShiftedPFCandidateProducerForNoPileUpPFMEt : public edm::stream::EDProduce edm::FileInPath jetCorrInputFileName_; std::string jetCorrPayloadName_; + edm::ESGetToken jetCorrPayloadToken_; std::string jetCorrUncertaintyTag_; - JetCorrectorParameters* jetCorrParameters_; - JetCorrectionUncertainty* jecUncertainty_; + std::unique_ptr jetCorrParameters_; + std::unique_ptr jecUncertainty_; double minJetPt_; @@ -70,10 +70,12 @@ ShiftedPFCandidateProducerForNoPileUpPFMEt::ShiftedPFCandidateProducerForNoPileU if (jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT") << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n"; - jetCorrParameters_ = new JetCorrectorParameters(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); - jecUncertainty_ = new JetCorrectionUncertainty(*jetCorrParameters_); + jetCorrParameters_ = + std::make_unique(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); + jecUncertainty_ = std::make_unique(*jetCorrParameters_); } else { jetCorrPayloadName_ = cfg.getParameter("jetCorrPayloadName"); + jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_)); } minJetPt_ = cfg.getParameter("minJetPt"); @@ -85,10 +87,6 @@ ShiftedPFCandidateProducerForNoPileUpPFMEt::ShiftedPFCandidateProducerForNoPileU produces(); } -ShiftedPFCandidateProducerForNoPileUpPFMEt::~ShiftedPFCandidateProducerForNoPileUpPFMEt() { - // nothing to be done yet... -} - void ShiftedPFCandidateProducerForNoPileUpPFMEt::produce(edm::Event& evt, const edm::EventSetup& es) { edm::Handle originalPFCandidates; evt.getByToken(srcPFCandidatesToken_, originalPFCandidates); @@ -103,11 +101,9 @@ void ShiftedPFCandidateProducerForNoPileUpPFMEt::produce(edm::Event& evt, const } if (!jetCorrPayloadName_.empty()) { - edm::ESHandle jetCorrParameterSet; - es.get().get(jetCorrPayloadName_, jetCorrParameterSet); - const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_]; - delete jecUncertainty_; - jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters); + const JetCorrectorParametersCollection& jetCorrParameterSet = es.getData(jetCorrPayloadToken_); + const JetCorrectorParameters& jetCorrParameters = (jetCorrParameterSet)[jetCorrUncertaintyTag_]; + jecUncertainty_ = std::make_unique(jetCorrParameters); } auto shiftedPFCandidates = std::make_unique(); diff --git a/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForPFNoPUMEt.cc b/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForPFNoPUMEt.cc index 20549e1f3ccbd..83de5b66a48b6 100644 --- a/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForPFNoPUMEt.cc +++ b/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForPFNoPUMEt.cc @@ -25,7 +25,7 @@ #include "DataFormats/Math/interface/deltaR.h" #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h" -#include "FWCore/Framework/interface/EDProducer.h" +#include "FWCore/Framework/interface/stream/EDProducer.h" #include "FWCore/Framework/interface/ESHandle.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" @@ -37,10 +37,9 @@ #include #include -class ShiftedPFCandidateProducerForPFNoPUMEt : public edm::EDProducer { +class ShiftedPFCandidateProducerForPFNoPUMEt : public edm::stream::EDProducer<> { public: explicit ShiftedPFCandidateProducerForPFNoPUMEt(const edm::ParameterSet&); - ~ShiftedPFCandidateProducerForPFNoPUMEt() override; private: void produce(edm::Event&, const edm::EventSetup&) override; @@ -52,9 +51,10 @@ class ShiftedPFCandidateProducerForPFNoPUMEt : public edm::EDProducer { edm::FileInPath jetCorrInputFileName_; std::string jetCorrPayloadName_; + edm::ESGetToken jetCorrPayloadToken_; std::string jetCorrUncertaintyTag_; - JetCorrectorParameters* jetCorrParameters_; - JetCorrectionUncertainty* jecUncertainty_; + std::unique_ptr jetCorrParameters_; + std::unique_ptr jecUncertainty_; bool jecValidFileName_; @@ -65,7 +65,9 @@ class ShiftedPFCandidateProducerForPFNoPUMEt : public edm::EDProducer { double unclEnUncertainty_; }; -const double dR2Match = 0.01 * 0.01; +namespace { + constexpr double dR2Match = 0.01 * 0.01; +} ShiftedPFCandidateProducerForPFNoPUMEt::ShiftedPFCandidateProducerForPFNoPUMEt(const edm::ParameterSet& cfg) : srcPFCandidatesToken_(consumes(cfg.getParameter("srcPFCandidates"))), @@ -81,12 +83,14 @@ ShiftedPFCandidateProducerForPFNoPUMEt::ShiftedPFCandidateProducerForPFNoPUMEt(c edm::LogInfo("ShiftedPFCandidateProducerForPFNoPUMEt") << "Reading JEC parameters = " << jetCorrUncertaintyTag_ << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl; - jetCorrParameters_ = new JetCorrectorParameters(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); - jecUncertainty_ = new JetCorrectionUncertainty(*jetCorrParameters_); + jetCorrParameters_ = + std::make_unique(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_); + jecUncertainty_ = std::make_unique(*jetCorrParameters_); } else { edm::LogInfo("ShiftedPFCandidateProducerForPFNoPUMEt") << "Reading JEC parameters = " << jetCorrUncertaintyTag_ << " from DB/SQLlite file." << std::endl; jetCorrPayloadName_ = cfg.getParameter("jetCorrPayloadName"); + jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_)); } minJetPt_ = cfg.getParameter("minJetPt"); @@ -98,13 +102,6 @@ ShiftedPFCandidateProducerForPFNoPUMEt::ShiftedPFCandidateProducerForPFNoPUMEt(c produces(); } -ShiftedPFCandidateProducerForPFNoPUMEt::~ShiftedPFCandidateProducerForPFNoPUMEt() { - if (jecValidFileName_) { - delete jetCorrParameters_; - delete jecUncertainty_; - } -} - void ShiftedPFCandidateProducerForPFNoPUMEt::produce(edm::Event& evt, const edm::EventSetup& es) { edm::Handle originalPFCandidates; evt.getByToken(srcPFCandidatesToken_, originalPFCandidates); @@ -119,11 +116,9 @@ void ShiftedPFCandidateProducerForPFNoPUMEt::produce(edm::Event& evt, const edm: } if (!jetCorrPayloadName_.empty()) { - edm::ESHandle jetCorrParameterSet; - es.get().get(jetCorrPayloadName_, jetCorrParameterSet); - const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_]; - delete jecUncertainty_; - jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters); + JetCorrectorParametersCollection const& jetCorrParameterSet = es.getData(jetCorrPayloadToken_); + const JetCorrectorParameters& jetCorrParameters = (jetCorrParameterSet)[jetCorrUncertaintyTag_]; + jecUncertainty_ = std::make_unique(jetCorrParameters); } auto shiftedPFCandidates = std::make_unique(); diff --git a/PhysicsTools/PatUtils/src/LeptonVertexSignificance.cc b/PhysicsTools/PatUtils/src/LeptonVertexSignificance.cc index da240043aaa72..df03e4e571e01 100644 --- a/PhysicsTools/PatUtils/src/LeptonVertexSignificance.cc +++ b/PhysicsTools/PatUtils/src/LeptonVertexSignificance.cc @@ -1,52 +1,41 @@ #include "PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/Exception.h" -#include "FWCore/Framework/interface/ESHandle.h" -#include "DataFormats/Common/interface/Handle.h" #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h" -#include "TrackingTools/Records/interface/TransientTrackRecord.h" #include "TrackingTools/TransientTrack/interface/TransientTrack.h" #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h" #include "DataFormats/GsfTrackReco/interface/GsfTrack.h" -#include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/Framework/interface/Event.h" #include "DataFormats/PatCandidates/interface/Electron.h" #include "DataFormats/PatCandidates/interface/Muon.h" using namespace pat; -// constructor -LeptonVertexSignificance::LeptonVertexSignificance(const edm::EventSetup& iSetup, edm::ConsumesCollector&& iC) - : vertexToken_(iC.consumes(edm::InputTag("offlinePrimaryVerticesFromCTFTracks"))) { - // instantiate the transient-track builder - edm::ESHandle builder; - iSetup.get().get("TransientTrackBuilder", builder); - theTrackBuilder_ = new TransientTrackBuilder(*builder.product()); +edm::InputTag LeptonVertexSignificance::vertexCollectionTag() { + return edm::InputTag("offlinePrimaryVerticesFromCTFTracks"); } -// destructor -LeptonVertexSignificance::~LeptonVertexSignificance() { delete theTrackBuilder_; } - // calculate the TrackIsoPt for the lepton object -float LeptonVertexSignificance::calculate(const Electron& theElectron, const edm::Event& iEvent) { - return this->calculate(*theElectron.gsfTrack(), iEvent); +float LeptonVertexSignificance::calculate(const Electron& theElectron, + const reco::VertexCollection& vertices, + const TransientTrackBuilder& builder) { + return this->calculate(*theElectron.gsfTrack(), vertices, builder); } -float LeptonVertexSignificance::calculate(const Muon& theMuon, const edm::Event& iEvent) { - return this->calculate(*theMuon.track(), iEvent); +float LeptonVertexSignificance::calculate(const Muon& theMuon, + const reco::VertexCollection& vertices, + const TransientTrackBuilder& builder) { + return this->calculate(*theMuon.track(), vertices, builder); } // calculate the TrackIsoPt for the lepton's track -float LeptonVertexSignificance::calculate(const reco::Track& theTrack, const edm::Event& iEvent) { - // FIXME: think more about how to handle events without vertices - // lepton LR calculation should have nothing to do with event selection - edm::Handle vertexHandle; - iEvent.getByToken(vertexToken_, vertexHandle); - if (vertexHandle.product()->empty()) +float LeptonVertexSignificance::calculate(const reco::Track& theTrack, + const reco::VertexCollection& vertices, + const TransientTrackBuilder& builder) { + if (vertices.empty()) return 0; - reco::Vertex theVertex = vertexHandle.product()->front(); + reco::Vertex const& theVertex = vertices.front(); // calculate the track-vertex association significance - reco::TransientTrack theTrTrack = theTrackBuilder_->build(&theTrack); + reco::TransientTrack theTrTrack = builder.build(&theTrack); GlobalPoint theVertexPoint(theVertex.position().x(), theVertex.position().y(), theVertex.position().z()); FreeTrajectoryState theLeptonNearVertex = theTrTrack.trajectoryStateClosestToPoint(theVertexPoint).theState(); return fabs(theVertex.position().z() - theLeptonNearVertex.position().z()) /