Skip to content

Commit

Permalink
Modernized modules
Browse files Browse the repository at this point in the history
- made modules global
- use new Event get syntax
- added fillDescriptions
  • Loading branch information
Dr15Jones committed Aug 25, 2021
1 parent 6214d48 commit 5b1ca8e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 158 deletions.
103 changes: 44 additions & 59 deletions RecoBTag/SoftLepton/plugins/SoftLepton.cc
Expand Up @@ -22,7 +22,7 @@

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "DataFormats/Common/interface/RefToBase.h"
Expand Down Expand Up @@ -62,10 +62,10 @@
#include "TrackingTools/Records/interface/TransientTrackRecord.h"
#include "TrackingTools/IPTools/interface/IPTools.h"

class SoftLepton : public edm::stream::EDProducer<> {
class SoftLepton : public edm::global::EDProducer<> {
public:
explicit SoftLepton(const edm::ParameterSet &iConfig);
~SoftLepton() override;

static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);

struct TrackCompare {
Expand All @@ -74,14 +74,15 @@ class SoftLepton : public edm::stream::EDProducer<> {
}
};

typedef std::map<unsigned int, float> LeptonIds;
typedef std::map<edm::RefToBase<reco::Track>, LeptonIds, TrackCompare> Leptons;
using LeptonIds = std::map<unsigned int, float>;
using Leptons = std::map<edm::RefToBase<reco::Track>, LeptonIds, TrackCompare>;

// generic interface, using a TrackRefVector for lepton tracks
reco::SoftLeptonTagInfo tag(const edm::RefToBase<reco::Jet> &jet,
const reco::TrackRefVector &tracks,
const Leptons &leptons,
const reco::Vertex &primaryVertex) const;
const reco::Vertex &primaryVertex,
const TransientTrackBuilder &builder) const;

protected:
// generic interface, using a TrackRefVector for lepton tracks
Expand All @@ -95,7 +96,7 @@ class SoftLepton : public edm::stream::EDProducer<> {
static double boostedPPar(const math::XYZVector &vector, const math::XYZVector &axis);

private:
void produce(edm::Event &event, const edm::EventSetup &setup) override;
void produce(edm::StreamID, edm::Event &event, const edm::EventSetup &setup) const final;

// configuration
const edm::InputTag m_jets;
Expand All @@ -115,8 +116,8 @@ class SoftLepton : public edm::stream::EDProducer<> {

// service used to make transient tracks from tracks
const edm::ESGetToken<TransientTrackBuilder, TransientTrackRecord> token_builder;
const TransientTrackBuilder *m_transientTrackBuilder;

const edm::EDPutTokenT<reco::SoftLeptonTagInfoCollection> token_put;
// algorithm configuration
const unsigned int m_refineJetAxis;
const double m_deltaRCut;
Expand Down Expand Up @@ -163,38 +164,30 @@ const reco::Vertex SoftLepton::s_nominalBeamSpot(
// ------------ c'tor --------------------------------------------------------------------
SoftLepton::SoftLepton(const edm::ParameterSet &iConfig)
: m_jets(iConfig.getParameter<edm::InputTag>("jets")),
token_jtas(mayConsume<reco::JetTracksAssociationCollection>(iConfig.getParameter<edm::InputTag>("jets"))),
token_jets(mayConsume<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>("jets"))),
token_jtas(mayConsume<reco::JetTracksAssociationCollection>(m_jets)),
token_jets(mayConsume<edm::View<reco::Jet> >(m_jets)),
token_primaryVertex(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("primaryVertex"))),
m_leptons(iConfig.getParameter<edm::InputTag>("leptons")),
token_gsfElectrons(mayConsume<GsfElectronView>(iConfig.getParameter<edm::InputTag>("leptons"))),
token_electrons(mayConsume<ElectronView>(iConfig.getParameter<edm::InputTag>("leptons"))),
token_pfElectrons(mayConsume<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("leptons"))),
token_muons(mayConsume<MuonView>(iConfig.getParameter<edm::InputTag>("leptons"))),
token_tracks(mayConsume<edm::View<reco::Track> >(iConfig.getParameter<edm::InputTag>("leptons"))),
m_leptonCands(iConfig.exists("leptonCands") ? iConfig.getParameter<edm::InputTag>("leptonCands")
: edm::InputTag()),
token_leptonCands(mayConsume<edm::ValueMap<float> >(
iConfig.exists("leptonCands") ? iConfig.getParameter<edm::InputTag>("leptonCands") : edm::InputTag())),
m_leptonId(iConfig.exists("leptonId") ? iConfig.getParameter<edm::InputTag>("leptonId") : edm::InputTag()),
token_leptonId(mayConsume<edm::ValueMap<float> >(
iConfig.exists("leptonId") ? iConfig.getParameter<edm::InputTag>("leptonId") : edm::InputTag())),
token_gsfElectrons(mayConsume<GsfElectronView>(m_leptons)),
token_electrons(mayConsume<ElectronView>(m_leptons)),
token_pfElectrons(mayConsume<reco::PFCandidateCollection>(m_leptons)),
token_muons(mayConsume<MuonView>(m_leptons)),
token_tracks(mayConsume<edm::View<reco::Track> >(m_leptons)),
m_leptonCands(iConfig.getParameter<edm::InputTag>("leptonCands")),
token_leptonCands(mayConsume<edm::ValueMap<float> >(m_leptonCands)),
m_leptonId(iConfig.getParameter<edm::InputTag>("leptonId")),
token_leptonId(mayConsume<edm::ValueMap<float> >(m_leptonId)),
token_builder(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
m_transientTrackBuilder(nullptr),
token_put(produces()),
m_refineJetAxis(iConfig.getParameter<unsigned int>("refineJetAxis")),
m_deltaRCut(iConfig.getParameter<double>("leptonDeltaRCut")),
m_chi2Cut(iConfig.getParameter<double>("leptonChi2Cut")),
m_muonSelection((muon::SelectionType)iConfig.getParameter<unsigned int>("muonSelection")) {
produces<reco::SoftLeptonTagInfoCollection>();
}

// ------------ d'tor --------------------------------------------------------------------
SoftLepton::~SoftLepton(void) {}
m_muonSelection((muon::SelectionType)iConfig.getParameter<unsigned int>("muonSelection")) {}

// ------------ method called once per event during the event loop -----------------------
void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
void SoftLepton::produce(edm::StreamID, edm::Event &event, const edm::EventSetup &setup) const {
// grab a TransientTrack helper from the Event Setup
m_transientTrackBuilder = &setup.getData(token_builder);
auto const &transientTrackBuilder = setup.getData(token_builder);

// input objects

Expand All @@ -205,8 +198,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
do {
{
// look for a JetTracksAssociationCollection
edm::Handle<reco::JetTracksAssociationCollection> h_jtas;
event.getByToken(token_jtas, h_jtas);
edm::Handle<reco::JetTracksAssociationCollection> h_jtas = event.getHandle(token_jtas);
if (h_jtas.isValid()) {
unsigned int size = h_jtas->size();
jets.resize(size);
Expand All @@ -220,8 +212,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
}
{ // else...
// look for a View<Jet>
edm::Handle<edm::View<reco::Jet> > h_jets;
event.getByToken(token_jets, h_jets);
edm::Handle<edm::View<reco::Jet> > h_jets = event.getHandle(token_jets);
if (h_jets.isValid()) {
unsigned int size = h_jets->size();
jets.resize(size);
Expand All @@ -240,8 +231,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {

// input primary vetex
reco::Vertex vertex;
Handle<reco::VertexCollection> h_primaryVertex;
event.getByToken(token_primaryVertex, h_primaryVertex);
Handle<reco::VertexCollection> h_primaryVertex = event.getHandle(token_primaryVertex);
if (h_primaryVertex.isValid() and not h_primaryVertex->empty())
vertex = h_primaryVertex->front();
else
Expand All @@ -254,16 +244,15 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
Handle<edm::ValueMap<float> > h_leptonCands;
bool haveLeptonCands = !(m_leptonCands == edm::InputTag());
if (haveLeptonCands)
event.getByToken(token_leptonCands, h_leptonCands);
h_leptonCands = event.getHandle(token_leptonCands);

// try to access the input collection as a collection of GsfElectrons, Muons or Tracks

unsigned int leptonId = SoftLeptonProperties::Quality::leptonId;
do {
{
// look for View<GsfElectron>
Handle<GsfElectronView> h_electrons;
event.getByToken(token_gsfElectrons, h_electrons);
Handle<GsfElectronView> h_electrons = event.getHandle(token_gsfElectrons);

if (h_electrons.isValid()) {
leptonId = SoftLeptonProperties::Quality::egammaElectronId;
Expand All @@ -281,8 +270,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
{ // else
// look for View<Electron>
// FIXME: is this obsolete?
Handle<ElectronView> h_electrons;
event.getByToken(token_electrons, h_electrons);
Handle<ElectronView> h_electrons = event.getHandle(token_electrons);
if (h_electrons.isValid()) {
leptonId = SoftLeptonProperties::Quality::egammaElectronId;
for (ElectronView::const_iterator electron = h_electrons->begin(); electron != h_electrons->end(); ++electron) {
Expand All @@ -297,8 +285,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
{ // else
// look for PFElectrons
// FIXME: is this obsolete?
Handle<reco::PFCandidateCollection> h_electrons;
event.getByToken(token_pfElectrons, h_electrons);
Handle<reco::PFCandidateCollection> h_electrons = event.getHandle(token_pfElectrons);
if (h_electrons.isValid()) {
leptonId = SoftLeptonProperties::Quality::egammaElectronId;
for (reco::PFCandidateCollection::const_iterator electron = h_electrons->begin();
Expand All @@ -321,8 +308,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
}
{ // else
// look for View<Muon>
Handle<MuonView> h_muons;
event.getByToken(token_muons, h_muons);
Handle<MuonView> h_muons = event.getHandle(token_muons);
if (h_muons.isValid()) {
for (MuonView::const_iterator muon = h_muons->begin(); muon != h_muons->end(); ++muon) {
// FIXME -> turn this selection into a muonCands input?
Expand All @@ -347,8 +333,7 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
}
{ // else
// look for edm::View<Track>
Handle<edm::View<reco::Track> > h_tracks;
event.getByToken(token_tracks, h_tracks);
Handle<edm::View<reco::Track> > h_tracks = event.getHandle(token_tracks);
if (h_tracks.isValid()) {
for (unsigned int i = 0; i < h_tracks->size(); i++) {
LeptonIds &id = leptons[h_tracks->refAt(i)];
Expand All @@ -366,27 +351,27 @@ void SoftLepton::produce(edm::Event &event, const edm::EventSetup &setup) {
} while (false);

if (!(m_leptonId == edm::InputTag())) {
Handle<edm::ValueMap<float> > h_leptonId;
event.getByToken(token_leptonId, h_leptonId);
edm::ValueMap<float> const &h_leptonId = event.get(token_leptonId);

for (Leptons::iterator lepton = leptons.begin(); lepton != leptons.end(); ++lepton)
lepton->second[leptonId] = (*h_leptonId)[lepton->first];
lepton->second[leptonId] = h_leptonId[lepton->first];
}

// output collections
auto outputCollection = std::make_unique<reco::SoftLeptonTagInfoCollection>();
reco::SoftLeptonTagInfoCollection outputCollection;
for (unsigned int i = 0; i < jets.size(); ++i) {
reco::SoftLeptonTagInfo result = tag(jets[i], tracks[i], leptons, vertex);
outputCollection->push_back(result);
reco::SoftLeptonTagInfo result = tag(jets[i], tracks[i], leptons, vertex, transientTrackBuilder);
outputCollection.push_back(result);
}
event.put(std::move(outputCollection));
event.emplace(token_put, std::move(outputCollection));
}

// ---------------------------------------------------------------------------------------
reco::SoftLeptonTagInfo SoftLepton::tag(const edm::RefToBase<reco::Jet> &jet,
const reco::TrackRefVector &tracks,
const Leptons &leptons,
const reco::Vertex &primaryVertex) const {
const reco::Vertex &primaryVertex,
const TransientTrackBuilder &transientTrackBuilder) const {
reco::SoftLeptonTagInfo info;
info.setJetRef(jet);

Expand All @@ -403,7 +388,7 @@ reco::SoftLeptonTagInfo SoftLepton::tag(const edm::RefToBase<reco::Jet> &jet,

reco::SoftLeptonProperties properties;

reco::TransientTrack transientTrack = m_transientTrackBuilder->build(*lepton->first);
reco::TransientTrack transientTrack = transientTrackBuilder.build(*lepton->first);
Measurement1D ip2d = IPTools::signedTransverseImpactParameter(transientTrack, jetAxis, primaryVertex).second;
Measurement1D ip3d = IPTools::signedImpactParameter3D(transientTrack, jetAxis, primaryVertex).second;
properties.sip2dsig = ip2d.significance();
Expand Down Expand Up @@ -527,8 +512,8 @@ void SoftLepton::fillDescriptions(edm::ConfigurationDescriptions &descriptions)
desc.add<unsigned int>("muonSelection", 1);
desc.add<edm::InputTag>("leptons", edm::InputTag("muons"));
desc.add<edm::InputTag>("primaryVertex", edm::InputTag("offlinePrimaryVertices"));
desc.add<edm::InputTag>("leptonCands", edm::InputTag(""));
desc.add<edm::InputTag>("leptonId", edm::InputTag(""));
desc.add<edm::InputTag>("leptonCands", edm::InputTag());
desc.add<edm::InputTag>("leptonId", edm::InputTag());
desc.add<unsigned int>("refineJetAxis", 0);
desc.add<edm::InputTag>("jets", edm::InputTag("ak4PFJetsCHS"));
desc.add<double>("leptonDeltaRCut", 0.4);
Expand Down

0 comments on commit 5b1ca8e

Please sign in to comment.