Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added esConsumes to PhysicsTools/PatUtils #35680

Merged
merged 3 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions PhysicsTools/PatUtils/interface/LeptonVertexSignificance.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@
\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;

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<reco::VertexCollection> vertexToken_;
float calculate(const reco::Track& track,
const reco::VertexCollection& vertices,
const TransientTrackBuilder& builder);
};

} // namespace pat
Expand Down
23 changes: 10 additions & 13 deletions PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<JetCorrectorParameters>(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_);
jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(*jetCorrParameters_);
} else {
jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_));
}
}

Expand All @@ -79,10 +81,6 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> {

produces<JetCollection>();
}
~ShiftedJetProducerT() override {
delete jetCorrParameters_;
delete jecUncertainty_;
}

private:
void produce(edm::Event& evt, const edm::EventSetup& es) override {
Expand All @@ -103,11 +101,9 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> {
auto shiftedJets = std::make_unique<JetCollection>();

if (!jetCorrPayloadName_.empty()) {
edm::ESHandle<JetCorrectorParametersCollection> jetCorrParameterSet;
es.get<JetCorrectionsRecord>().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<JetCorrectionUncertainty>(jetCorrParameters);
}

for (typename JetCollection::const_iterator originalJet = originalJets->begin(); originalJet != originalJets->end();
Expand Down Expand Up @@ -175,9 +171,10 @@ class ShiftedJetProducerT : public edm::stream::EDProducer<> {

edm::FileInPath jetCorrInputFileName_;
std::string jetCorrPayloadName_;
edm::ESGetToken<JetCorrectorParametersCollection, JetCorrectionsRecord> jetCorrPayloadToken_;
std::string jetCorrUncertaintyTag_;
JetCorrectorParameters* jetCorrParameters_;
JetCorrectionUncertainty* jecUncertainty_;
std::unique_ptr<JetCorrectorParameters> jetCorrParameters_;
std::unique_ptr<JetCorrectionUncertainty> jecUncertainty_;

bool addResidualJES_;
edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,9 +49,10 @@ class ShiftedPFCandidateProducerForNoPileUpPFMEt : public edm::stream::EDProduce

edm::FileInPath jetCorrInputFileName_;
std::string jetCorrPayloadName_;
edm::ESGetToken<JetCorrectorParametersCollection, JetCorrectionsRecord> jetCorrPayloadToken_;
std::string jetCorrUncertaintyTag_;
JetCorrectorParameters* jetCorrParameters_;
JetCorrectionUncertainty* jecUncertainty_;
std::unique_ptr<JetCorrectorParameters> jetCorrParameters_;
std::unique_ptr<JetCorrectionUncertainty> jecUncertainty_;

double minJetPt_;

Expand All @@ -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<JetCorrectorParameters>(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_);
jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(*jetCorrParameters_);
} else {
jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_));
}

minJetPt_ = cfg.getParameter<double>("minJetPt");
Expand All @@ -85,10 +87,6 @@ ShiftedPFCandidateProducerForNoPileUpPFMEt::ShiftedPFCandidateProducerForNoPileU
produces<reco::PFCandidateCollection>();
}

ShiftedPFCandidateProducerForNoPileUpPFMEt::~ShiftedPFCandidateProducerForNoPileUpPFMEt() {
// nothing to be done yet...
}

void ShiftedPFCandidateProducerForNoPileUpPFMEt::produce(edm::Event& evt, const edm::EventSetup& es) {
edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
evt.getByToken(srcPFCandidatesToken_, originalPFCandidates);
Expand All @@ -103,11 +101,9 @@ void ShiftedPFCandidateProducerForNoPileUpPFMEt::produce(edm::Event& evt, const
}

if (!jetCorrPayloadName_.empty()) {
edm::ESHandle<JetCorrectorParametersCollection> jetCorrParameterSet;
es.get<JetCorrectionsRecord>().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<JetCorrectionUncertainty>(jetCorrParameters);
}

auto shiftedPFCandidates = std::make_unique<reco::PFCandidateCollection>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,10 +37,9 @@
#include <string>
#include <vector>

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;
Expand All @@ -52,9 +51,10 @@ class ShiftedPFCandidateProducerForPFNoPUMEt : public edm::EDProducer {

edm::FileInPath jetCorrInputFileName_;
std::string jetCorrPayloadName_;
edm::ESGetToken<JetCorrectorParametersCollection, JetCorrectionsRecord> jetCorrPayloadToken_;
std::string jetCorrUncertaintyTag_;
JetCorrectorParameters* jetCorrParameters_;
JetCorrectionUncertainty* jecUncertainty_;
std::unique_ptr<JetCorrectorParameters> jetCorrParameters_;
std::unique_ptr<JetCorrectionUncertainty> jecUncertainty_;

bool jecValidFileName_;

Expand All @@ -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<reco::PFCandidateCollection>(cfg.getParameter<edm::InputTag>("srcPFCandidates"))),
Expand All @@ -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<JetCorrectorParameters>(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_);
jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(*jetCorrParameters_);
} else {
edm::LogInfo("ShiftedPFCandidateProducerForPFNoPUMEt")
<< "Reading JEC parameters = " << jetCorrUncertaintyTag_ << " from DB/SQLlite file." << std::endl;
jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
jetCorrPayloadToken_ = esConsumes(edm::ESInputTag("", jetCorrPayloadName_));
}

minJetPt_ = cfg.getParameter<double>("minJetPt");
Expand All @@ -98,13 +102,6 @@ ShiftedPFCandidateProducerForPFNoPUMEt::ShiftedPFCandidateProducerForPFNoPUMEt(c
produces<reco::PFCandidateCollection>();
}

ShiftedPFCandidateProducerForPFNoPUMEt::~ShiftedPFCandidateProducerForPFNoPUMEt() {
if (jecValidFileName_) {
delete jetCorrParameters_;
delete jecUncertainty_;
}
}

void ShiftedPFCandidateProducerForPFNoPUMEt::produce(edm::Event& evt, const edm::EventSetup& es) {
edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
evt.getByToken(srcPFCandidatesToken_, originalPFCandidates);
Expand All @@ -119,11 +116,9 @@ void ShiftedPFCandidateProducerForPFNoPUMEt::produce(edm::Event& evt, const edm:
}

if (!jetCorrPayloadName_.empty()) {
edm::ESHandle<JetCorrectorParametersCollection> jetCorrParameterSet;
es.get<JetCorrectionsRecord>().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<JetCorrectionUncertainty>(jetCorrParameters);
}

auto shiftedPFCandidates = std::make_unique<reco::PFCandidateCollection>();
Expand Down
43 changes: 16 additions & 27 deletions PhysicsTools/PatUtils/src/LeptonVertexSignificance.cc
Original file line number Diff line number Diff line change
@@ -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<reco::VertexCollection>(edm::InputTag("offlinePrimaryVerticesFromCTFTracks"))) {
// instantiate the transient-track builder
edm::ESHandle<TransientTrackBuilder> builder;
iSetup.get<TransientTrackRecord>().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<reco::VertexCollection> 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()) /
Expand Down