Skip to content

Commit

Permalink
Merge pull request #39030 from mmusich/muscleFitConsumes_12_4_X
Browse files Browse the repository at this point in the history
[12.4.X] implement event consumes in `MuScleFit`
  • Loading branch information
cmsbuild authored Aug 12, 2022
2 parents 1d3e869 + c6428f2 commit a903d39
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
31 changes: 21 additions & 10 deletions MuonAnalysis/MomentumScaleCalibration/plugins/MuScleFit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ class MuScleFit : public edm::EDLooper, MuScleFitBase {
// Maximum number of events from root tree. It works in the same way as the maxEvents to configure a input source.
int maxEventsFromRootTree_;

edm::EDGetTokenT<edm::TriggerResults> triggerResultsToken_;
edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
edm::EDGetTokenT<std::vector<PileupSummaryInfo> > puInfoToken_;
edm::EDGetTokenT<GenEventInfoProduct> genEvtInfoToken_;

std::string triggerResultsLabel_;
std::string triggerResultsProcess_;
std::vector<std::string> triggerPath_;
Expand Down Expand Up @@ -467,13 +472,22 @@ MuScleFit::MuScleFit(const edm::ParameterSet& pset) : MuScleFitBase(pset), total

triggerResultsLabel_ = pset.getUntrackedParameter<std::string>("TriggerResultsLabel");
triggerResultsProcess_ = pset.getUntrackedParameter<std::string>("TriggerResultsProcess");

triggerResultsToken_ =
consumes<edm::TriggerResults>(edm::InputTag(triggerResultsLabel_.c_str(), "", triggerResultsProcess_.c_str()));

triggerPath_ = pset.getUntrackedParameter<std::vector<std::string> >("TriggerPath");
negateTrigger_ = pset.getUntrackedParameter<bool>("NegateTrigger", false);
saveAllToTree_ = pset.getUntrackedParameter<bool>("SaveAllToTree", false);

// input collections for PU related infos
puInfoSrc_ = pset.getUntrackedParameter<edm::InputTag>("PileUpSummaryInfo");
puInfoToken_ = consumes<std::vector<PileupSummaryInfo> >(puInfoSrc_);

vertexSrc_ = pset.getUntrackedParameter<edm::InputTag>("PrimaryVertexCollection");
vertexToken_ = consumes<reco::VertexCollection>(vertexSrc_);

genEvtInfoToken_ = consumes<GenEventInfoProduct>(edm::InputTag("generator"));

PATmuons_ = pset.getUntrackedParameter<bool>("PATmuons", false);
genParticlesName_ = pset.getUntrackedParameter<std::string>("GenParticlesName", "genParticles");
Expand Down Expand Up @@ -558,7 +572,9 @@ MuScleFit::MuScleFit(const edm::ParameterSet& pset) : MuScleFitBase(pset), total
MuScleFitUtils::massWindowHalfWidth[2][4] = 0.2;
MuScleFitUtils::massWindowHalfWidth[2][5] = 0.2;

muonSelector_ = std::make_unique<MuScleFitMuonSelector>(theMuonLabel_,
edm::ConsumesCollector iC = consumesCollector();
muonSelector_ = std::make_unique<MuScleFitMuonSelector>(iC,
theMuonLabel_,
theMuonType_,
PATmuons_,
MuScleFitUtils::resfind,
Expand Down Expand Up @@ -786,9 +802,7 @@ void MuScleFit::endOfFastLoop(const unsigned int iLoop) {
// Stuff to do during loop
// -----------------------
edm::EDLooper::Status MuScleFit::duringLoop(const edm::Event& event, const edm::EventSetup& eventSetup) {
edm::Handle<edm::TriggerResults> triggerResults;
event.getByLabel(edm::InputTag(triggerResultsLabel_.c_str(), "", triggerResultsProcess_.c_str()), triggerResults);
//event.getByLabel(InputTag(triggerResultsLabel_),triggerResults);
edm::Handle<edm::TriggerResults> triggerResults = event.getHandle(triggerResultsToken_);
bool isFired = false;

if (triggerPath_[0].empty())
Expand Down Expand Up @@ -917,8 +931,7 @@ void MuScleFit::selectMuons(const edm::Event& event) {

// Fill pile-up related informations
// --------------------------------
edm::Handle<std::vector<PileupSummaryInfo> > puInfo;
event.getByLabel(puInfoSrc_, puInfo);
edm::Handle<std::vector<PileupSummaryInfo> > puInfo = event.getHandle(puInfoToken_);
if (puInfo.isValid()) {
std::vector<PileupSummaryInfo>::const_iterator PVI;
for (PVI = puInfo->begin(); PVI != puInfo->end(); ++PVI) {
Expand All @@ -930,8 +943,7 @@ void MuScleFit::selectMuons(const edm::Event& event) {
}
}

edm::Handle<std::vector<reco::Vertex> > vertices;
event.getByLabel(vertexSrc_, vertices);
edm::Handle<std::vector<reco::Vertex> > vertices = event.getHandle(vertexToken_);
if (vertices.isValid()) {
std::vector<reco::Vertex>::const_iterator itv;
// now, count vertices
Expand All @@ -948,8 +960,7 @@ void MuScleFit::selectMuons(const edm::Event& event) {
}

// get the MC event weight
edm::Handle<GenEventInfoProduct> genEvtInfo;
event.getByLabel("generator", genEvtInfo);
edm::Handle<GenEventInfoProduct> genEvtInfo = event.getHandle(genEvtInfoToken_);
double the_genEvtweight = 1.;
if (genEvtInfo.isValid()) {
the_genEvtweight = genEvtInfo->weight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void MuScleFitMuonSelector::selectMuons(const edm::Event& event,
std::vector<GenMuonPair>& genPair,
std::vector<std::pair<lorentzVector, lorentzVector> >& simPair,
MuScleFitPlotter* plotter) {
edm::Handle<pat::CompositeCandidateCollection> collAll;
event.getByLabel("onia2MuMuPatTrkTrk", collAll);
edm::Handle<pat::CompositeCandidateCollection> collAll = event.getHandle(onia2MuMuToken_);
if (!collAll.isValid()) {
edm::LogWarning("MuScleFitUtils") << "J/psi not present in event!";
}
Expand Down Expand Up @@ -179,8 +178,7 @@ void MuScleFitMuonSelector::selectMuons(const edm::Event& event,
} else if ((muonType_ < 4 && muonType_ >= 0) || muonType_ >= 10) { // Muons (glb,sta,trk)
std::vector<reco::Track> tracks;
if (PATmuons_ == true) {
edm::Handle<pat::MuonCollection> allMuons;
event.getByLabel(muonLabel_, allMuons);
edm::Handle<pat::MuonCollection> allMuons = event.getHandle(patMuonToken_);
if (muonType_ == 0) {
// Take directly the muon
muons = fillMuonCollection(*allMuons);
Expand All @@ -192,8 +190,7 @@ void MuScleFitMuonSelector::selectMuons(const edm::Event& event,
muons = fillMuonCollection(tracks);
}
} else {
edm::Handle<reco::MuonCollection> allMuons;
event.getByLabel(muonLabel_, allMuons);
edm::Handle<reco::MuonCollection> allMuons = event.getHandle(recoMuonToken_);
if (muonType_ == 0) {
// Take directly the muon
muons = fillMuonCollection(*allMuons);
Expand All @@ -205,8 +202,7 @@ void MuScleFitMuonSelector::selectMuons(const edm::Event& event,
}
}
} else if (muonType_ == 4) { //CaloMuons
edm::Handle<reco::CaloMuonCollection> caloMuons;
event.getByLabel(muonLabel_, caloMuons);
edm::Handle<reco::CaloMuonCollection> caloMuons = event.getHandle(caloMuonToken_);
std::vector<reco::Track> tracks;
for (std::vector<reco::CaloMuon>::const_iterator muon = caloMuons->begin(); muon != caloMuons->end(); ++muon) {
tracks.push_back(*(muon->track()));
Expand All @@ -215,8 +211,7 @@ void MuScleFitMuonSelector::selectMuons(const edm::Event& event,
}

else if (muonType_ == 5) { // Inner tracker tracks
edm::Handle<reco::TrackCollection> tracks;
event.getByLabel(muonLabel_, tracks);
edm::Handle<reco::TrackCollection> tracks = event.getHandle(trackCollectionToken_);
muons = fillMuonCollection(*tracks);
}
plotter->fillRec(muons);
Expand Down Expand Up @@ -296,14 +291,11 @@ void MuScleFitMuonSelector::selectGenSimMuons(const edm::Event& event,
MuScleFitPlotter* plotter) {
// Find and store in histograms the generated and simulated resonance and muons
// ----------------------------------------------------------------------------
edm::Handle<edm::HepMCProduct> evtMC;
edm::Handle<reco::GenParticleCollection> genParticles;
edm::Handle<edm::HepMCProduct> evtMC = event.getHandle(evtMCToken_);
edm::Handle<reco::GenParticleCollection> genParticles = event.getHandle(genParticleToken_);

// Fill gen information only in the first loop
bool ifHepMC = false;

event.getByLabel(genParticlesName_, evtMC);
event.getByLabel(genParticlesName_, genParticles);
if (evtMC.isValid()) {
genPair.push_back(findGenMuFromRes(evtMC.product()));
plotter->fillGen(*evtMC, sherpa_);
Expand Down Expand Up @@ -334,9 +326,8 @@ void MuScleFitMuonSelector::selectSimulatedMuons(const edm::Event& event,
edm::Handle<edm::HepMCProduct> evtMC,
std::vector<std::pair<lorentzVector, lorentzVector> >& simPair,
MuScleFitPlotter* plotter) {
edm::Handle<edm::SimTrackContainer> simTracks;
edm::Handle<edm::SimTrackContainer> simTracks = event.getHandle(simTrackToken_);
bool simTracksFound = false;
event.getByLabel(simTracksCollectionName_, simTracks);
if (simTracks.isValid()) {
plotter->fillSim(simTracks);
if (ifHepMC) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "DataFormats/Candidate/interface/LeafCandidate.h"
#include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
#include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
Expand All @@ -28,7 +29,8 @@ typedef reco::Particle::LorentzVector lorentzVector;

class MuScleFitMuonSelector {
public:
MuScleFitMuonSelector(const edm::InputTag& muonLabel,
MuScleFitMuonSelector(edm::ConsumesCollector& iC,
const edm::InputTag& muonLabel,
const int muonType,
const bool PATmuons,
const std::vector<int>& resfind,
Expand All @@ -47,8 +49,16 @@ class MuScleFitMuonSelector {
compareToSimTracks_(compareToSimTracks),
simTracksCollectionName_(simTracksCollectionName),
sherpa_(sherpa),
debug_(debug) {}
~MuScleFitMuonSelector() {}
debug_(debug),
onia2MuMuToken_(iC.consumes<pat::CompositeCandidateCollection>(edm::InputTag("onia2MuMuPatTrkTrk"))),
trackCollectionToken_(iC.consumes<reco::TrackCollection>(muonLabel_)),
patMuonToken_(iC.consumes<pat::MuonCollection>(muonLabel_)),
recoMuonToken_(iC.consumes<reco::MuonCollection>(muonLabel_)),
caloMuonToken_(iC.consumes<reco::CaloMuonCollection>(muonLabel_)),
evtMCToken_(iC.consumes<edm::HepMCProduct>(genParticlesName_)),
genParticleToken_(iC.consumes<reco::GenParticleCollection>(genParticlesName_)),
simTrackToken_(iC.consumes<edm::SimTrackContainer>(simTracksCollectionName_)) {}
~MuScleFitMuonSelector() = default;

//Method to get the muon after FSR (status 1 muon in PYTHIA6) starting from status 3 muon which is daughter of the Z
const reco::Candidate* getStatus1Muon(const reco::Candidate* status3Muon);
Expand Down Expand Up @@ -156,6 +166,16 @@ class MuScleFitMuonSelector {
const edm::InputTag simTracksCollectionName_;
const bool sherpa_;
const bool debug_;

const edm::EDGetTokenT<pat::CompositeCandidateCollection> onia2MuMuToken_;
const edm::EDGetTokenT<reco::TrackCollection> trackCollectionToken_;
const edm::EDGetTokenT<pat::MuonCollection> patMuonToken_;
const edm::EDGetTokenT<reco::MuonCollection> recoMuonToken_;
const edm::EDGetTokenT<reco::CaloMuonCollection> caloMuonToken_;
const edm::EDGetTokenT<edm::HepMCProduct> evtMCToken_;
const edm::EDGetTokenT<reco::GenParticleCollection> genParticleToken_;
const edm::EDGetTokenT<edm::SimTrackContainer> simTrackToken_;

static const double mMu2;
static const unsigned int motherPdgIdArray[6];
};
Expand Down

0 comments on commit a903d39

Please sign in to comment.