Skip to content

Commit

Permalink
Modernize legacy EDModules in TopTools
Browse files Browse the repository at this point in the history
  • Loading branch information
makortel committed Jan 11, 2022
1 parent 0abb86e commit 91a33de
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 134 deletions.
10 changes: 5 additions & 5 deletions AnalysisDataFormats/TopObjects/interface/TtEventPartons.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class TtGenEvent;
class TtEventPartons {
public:
/// default constructor
TtEventPartons(){};
TtEventPartons() = default;
/// default destructor
virtual ~TtEventPartons(){};
virtual ~TtEventPartons() = default;

/// return vector of partons in the order defined in the corresponding enum
/// (method implemented in the derived classes)
virtual std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) = 0;
virtual std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) const = 0;

/// insert dummy index -3 for all partons that were chosen to be ignored
void expand(std::vector<int>& vec);
void expand(std::vector<int>& vec) const;

protected:
/// return pointer to an empty reco::Candidate
Expand All @@ -37,7 +37,7 @@ class TtEventPartons {
};

/// erase partons from vector if they where chosen to be ignored
void prune(std::vector<const reco::Candidate*>& vec);
void prune(std::vector<const reco::Candidate*>& vec) const;

/// flag partons that were chosen not to be used
std::vector<bool> ignorePartons_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class TtFullHadEvtPartons : public TtEventPartons {
public:
/// default constructor
TtFullHadEvtPartons(const std::vector<std::string>& partonsToIgnore = std::vector<std::string>());
/// default destructor
~TtFullHadEvtPartons() override{};

/// return vector of partons in the order defined in the corresponding enum
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) override;
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) const override;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class TtFullLepEvtPartons : public TtEventPartons {
public:
/// default constructor
TtFullLepEvtPartons(const std::vector<std::string>& partonsToIgnore = std::vector<std::string>());
/// default destructor
~TtFullLepEvtPartons() override{};

/// return vector of partons in the order defined in the corresponding enum
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) override;
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) const override;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class TtSemiLepEvtPartons : public TtEventPartons {
public:
/// default constructor
TtSemiLepEvtPartons(const std::vector<std::string>& partonsToIgnore = std::vector<std::string>());
/// default destructor
~TtSemiLepEvtPartons() override{};

/// return vector of partons in the order defined in the corresponding enum
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) override;
std::vector<const reco::Candidate*> vec(const TtGenEvent& genEvt) const override;
};

#endif
4 changes: 2 additions & 2 deletions AnalysisDataFormats/TopObjects/src/TtEventPartons.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "AnalysisDataFormats/TopObjects/interface/TtEventPartons.h"

void TtEventPartons::expand(std::vector<int>& vec) {
void TtEventPartons::expand(std::vector<int>& vec) const {
std::vector<int>::iterator vecIter = vec.begin();
for (unsigned i = 0; i < ignorePartons_.size(); i++) {
if (ignorePartons_[i]) {
Expand All @@ -10,7 +10,7 @@ void TtEventPartons::expand(std::vector<int>& vec) {
}
}

void TtEventPartons::prune(std::vector<const reco::Candidate*>& vec) {
void TtEventPartons::prune(std::vector<const reco::Candidate*>& vec) const {
unsigned int nIgnoredPartons = 0;
for (unsigned i = 0; i < ignorePartons_.size(); i++) {
if (ignorePartons_[i]) {
Expand Down
2 changes: 1 addition & 1 deletion AnalysisDataFormats/TopObjects/src/TtFullHadEvtPartons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TtFullHadEvtPartons::TtFullHadEvtPartons(const std::vector<std::string>& partons
}
}

std::vector<const reco::Candidate*> TtFullHadEvtPartons::vec(const TtGenEvent& genEvt) {
std::vector<const reco::Candidate*> TtFullHadEvtPartons::vec(const TtGenEvent& genEvt) const {
std::vector<const reco::Candidate*> vec;

if (genEvt.isFullHadronic()) {
Expand Down
2 changes: 1 addition & 1 deletion AnalysisDataFormats/TopObjects/src/TtFullLepEvtPartons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TtFullLepEvtPartons::TtFullLepEvtPartons(const std::vector<std::string>& partons
}
}

std::vector<const reco::Candidate*> TtFullLepEvtPartons::vec(const TtGenEvent& genEvt) {
std::vector<const reco::Candidate*> TtFullLepEvtPartons::vec(const TtGenEvent& genEvt) const {
std::vector<const reco::Candidate*> vec;

if (genEvt.isFullLeptonic()) {
Expand Down
2 changes: 1 addition & 1 deletion AnalysisDataFormats/TopObjects/src/TtSemiLepEvtPartons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TtSemiLepEvtPartons::TtSemiLepEvtPartons(const std::vector<std::string>& partons
}
}

std::vector<const reco::Candidate*> TtSemiLepEvtPartons::vec(const TtGenEvent& genEvt) {
std::vector<const reco::Candidate*> TtSemiLepEvtPartons::vec(const TtGenEvent& genEvt) const {
std::vector<const reco::Candidate*> vec;

if (genEvt.isSemiLeptonic()) {
Expand Down
49 changes: 22 additions & 27 deletions TopQuarkAnalysis/TopTools/plugins/MuonSelectorVertex.cc
Original file line number Diff line number Diff line change
@@ -1,55 +1,50 @@
//
//

#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "DataFormats/PatCandidates/interface/Muon.h"
#include "DataFormats/VertexReco/interface/Vertex.h"

class MuonSelectorVertex : public edm::EDProducer {
#include <vector>
#include <memory>
#include <cmath>

class MuonSelectorVertex : public edm::global::EDProducer<> {
public:
explicit MuonSelectorVertex(const edm::ParameterSet& iConfig);
~MuonSelectorVertex() override{};
void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;

private:
edm::EDGetTokenT<std::vector<pat::Muon> > muonSource_;
edm::EDGetTokenT<std::vector<reco::Vertex> > vertexSource_;
double maxDZ_;
const edm::EDGetTokenT<std::vector<pat::Muon>> muonSource_;
const edm::EDGetTokenT<std::vector<reco::Vertex>> vertexSource_;
const double maxDZ_;
};

#include <vector>
#include <memory>
#include <cmath>

MuonSelectorVertex::MuonSelectorVertex(const edm::ParameterSet& iConfig)
: muonSource_(consumes<std::vector<pat::Muon> >(iConfig.getParameter<edm::InputTag>("muonSource"))),
vertexSource_(consumes<std::vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("vertexSource"))),
: muonSource_(consumes<std::vector<pat::Muon>>(iConfig.getParameter<edm::InputTag>("muonSource"))),
vertexSource_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertexSource"))),
maxDZ_(iConfig.getParameter<double>("maxDZ")) {
produces<std::vector<pat::Muon> >();
produces<std::vector<pat::Muon>>();
}

void MuonSelectorVertex::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
edm::Handle<std::vector<pat::Muon> > muons;
iEvent.getByToken(muonSource_, muons);

edm::Handle<std::vector<reco::Vertex> > vertices;
iEvent.getByToken(vertexSource_, vertices);
void MuonSelectorVertex::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
const auto& muons = iEvent.get(muonSource_);
const auto& vertices = iEvent.get(vertexSource_);

std::vector<pat::Muon>* selectedMuons(new std::vector<pat::Muon>);
auto selectedMuons = std::make_unique<std::vector<pat::Muon>>();

if (!vertices->empty()) {
for (unsigned iMuon = 0; iMuon < muons->size(); ++iMuon) {
if (std::fabs(muons->at(iMuon).vertex().z() - vertices->at(0).z()) < maxDZ_) {
selectedMuons->push_back(muons->at(iMuon));
if (!vertices.empty()) {
for (const auto& muon : muons) {
if (std::abs(muon.vertex().z() - vertices.at(0).z()) < maxDZ_) {
selectedMuons->push_back(muon);
}
}
}

std::unique_ptr<std::vector<pat::Muon> > selectedMuonsPtr(selectedMuons);
iEvent.put(std::move(selectedMuonsPtr));
iEvent.put(std::move(selectedMuons));
}

#include "FWCore/Framework/interface/MakerMacros.h"
Expand Down
47 changes: 20 additions & 27 deletions TopQuarkAnalysis/TopTools/plugins/TtJetPartonMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

Expand Down Expand Up @@ -41,25 +41,23 @@
// ----------------------------------------------------------------------

template <typename C>
class TtJetPartonMatch : public edm::EDProducer {
class TtJetPartonMatch : public edm::global::EDProducer<> {
public:
/// default conructor
explicit TtJetPartonMatch(const edm::ParameterSet&);
/// default destructor
~TtJetPartonMatch() override;
/// write jet parton match objects into the event
void produce(edm::Event&, const edm::EventSetup&) override;
void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;

private:
/// convert string for algorithm into corresponding enumerator type
JetPartonMatching::algorithms readAlgorithm(const std::string& str);
JetPartonMatching::algorithms readAlgorithm(const std::string& str) const;

/// partons
C partons_;
/// TtGenEvent collection input
edm::EDGetTokenT<TtGenEvent> genEvt_;
/// jet collection input
edm::EDGetTokenT<edm::View<reco::Jet> > jets_;
edm::EDGetTokenT<edm::View<reco::Jet>> jets_;
/// maximal number of jets to be considered for the
/// matching
int maxNJets_;
Expand All @@ -82,9 +80,9 @@ class TtJetPartonMatch : public edm::EDProducer {

template <typename C>
TtJetPartonMatch<C>::TtJetPartonMatch(const edm::ParameterSet& cfg)
: partons_(cfg.getParameter<std::vector<std::string> >("partonsToIgnore")),
: partons_(cfg.getParameter<std::vector<std::string>>("partonsToIgnore")),
genEvt_(consumes<TtGenEvent>(edm::InputTag("genEvt"))),
jets_(consumes<edm::View<reco::Jet> >(cfg.getParameter<edm::InputTag>("jets"))),
jets_(consumes<edm::View<reco::Jet>>(cfg.getParameter<edm::InputTag>("jets"))),
maxNJets_(cfg.getParameter<int>("maxNJets")),
maxNComb_(cfg.getParameter<int>("maxNComb")),
algorithm_(readAlgorithm(cfg.getParameter<std::string>("algorithm"))),
Expand All @@ -97,43 +95,38 @@ TtJetPartonMatch<C>::TtJetPartonMatch(const edm::ParameterSet& cfg)
// * TtFullHadEvtPartons
// * TtFullLepEvtPartons
// and vectors of the corresponding quality parameters
produces<std::vector<std::vector<int> > >();
produces<std::vector<double> >("SumPt");
produces<std::vector<double> >("SumDR");
produces<std::vector<std::vector<int>>>();
produces<std::vector<double>>("SumPt");
produces<std::vector<double>>("SumDR");
produces<int>("NumberOfConsideredJets");
}

template <typename C>
TtJetPartonMatch<C>::~TtJetPartonMatch() {}

template <typename C>
void TtJetPartonMatch<C>::produce(edm::Event& evt, const edm::EventSetup& setup) {
void TtJetPartonMatch<C>::produce(edm::StreamID, edm::Event& evt, const edm::EventSetup& setup) const {
// will write
// * parton match
// * sumPt
// * sumDR
// to the event
std::unique_ptr<std::vector<std::vector<int> > > match(new std::vector<std::vector<int> >);
std::unique_ptr<std::vector<double> > sumPt(new std::vector<double>);
std::unique_ptr<std::vector<double> > sumDR(new std::vector<double>);
auto match = std::make_unique<std::vector<std::vector<int>>>();
auto sumPt = std::make_unique<std::vector<double>>();
auto sumDR = std::make_unique<std::vector<double>>();
std::unique_ptr<int> pJetsConsidered(new int);

// get TtGenEvent and jet collection from the event
edm::Handle<TtGenEvent> genEvt;
evt.getByToken(genEvt_, genEvt);
const TtGenEvent& genEvt = evt.get(genEvt_);

edm::Handle<edm::View<reco::Jet> > topJets;
evt.getByToken(jets_, topJets);
const edm::View<reco::Jet>& topJets = evt.get(jets_);

// fill vector of partons in the order of
// * TtFullLepEvtPartons
// * TtSemiLepEvtPartons
// * TtFullHadEvtPartons
std::vector<const reco::Candidate*> partons = partons_.vec(*genEvt);
std::vector<const reco::Candidate*> partons = partons_.vec(genEvt);

// prepare vector of jets
std::vector<const reco::Candidate*> jets;
for (unsigned int ij = 0; ij < topJets->size(); ++ij) {
for (unsigned int ij = 0; ij < topJets.size(); ++ij) {
// take all jets if maxNJets_ == -1; otherwise use
// maxNJets_ if maxNJets_ is big enough or use same
// number of jets as partons if maxNJets_ < number
Expand All @@ -147,7 +140,7 @@ void TtJetPartonMatch<C>::produce(edm::Event& evt, const edm::EventSetup& setup)
break;
}
}
jets.push_back((const reco::Candidate*)&(*topJets)[ij]);
jets.push_back(&topJets[ij]);
}
*pJetsConsidered = jets.size();

Expand Down Expand Up @@ -175,7 +168,7 @@ void TtJetPartonMatch<C>::produce(edm::Event& evt, const edm::EventSetup& setup)
}

template <typename C>
JetPartonMatching::algorithms TtJetPartonMatch<C>::readAlgorithm(const std::string& str) {
JetPartonMatching::algorithms TtJetPartonMatch<C>::readAlgorithm(const std::string& str) const {
if (str == "totalMinDist")
return JetPartonMatching::totalMinDist;
else if (str == "minSumDist")
Expand Down
Loading

0 comments on commit 91a33de

Please sign in to comment.