Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-03 02:23:39

0001 #include <string>
0002 #include <vector>
0003 
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 #include "DQMServices/Core/interface/DQMStore.h"
0009 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0010 #include "DQMOffline/Trigger/plugins/TriggerDQMBase.h"
0011 #include "CommonTools/TriggerUtils/interface/GenericTriggerEventFlag.h"
0012 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0013 #include "DataFormats/METReco/interface/PFMET.h"
0014 #include "DataFormats/METReco/interface/PFMETCollection.h"
0015 #include "DataFormats/JetReco/interface/PFJet.h"
0016 #include "DataFormats/JetReco/interface/PFJetCollection.h"
0017 #include "DataFormats/MuonReco/interface/Muon.h"
0018 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0019 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0020 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0021 #include "DataFormats/VertexReco/interface/Vertex.h"
0022 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0023 
0024 class MuonMonitor : public DQMEDAnalyzer, public TriggerDQMBase {
0025 public:
0026   typedef dqm::reco::MonitorElement MonitorElement;
0027   typedef dqm::reco::DQMStore DQMStore;
0028 
0029   MuonMonitor(const edm::ParameterSet&);
0030   ~MuonMonitor() throw() override;
0031 
0032   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0033 
0034 protected:
0035   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0036   void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
0037 
0038 private:
0039   const std::string folderName_;
0040 
0041   const bool requireValidHLTPaths_;
0042   bool hltPathsAreValid_;
0043 
0044   edm::EDGetTokenT<reco::PFMETCollection> metToken_;
0045   edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0046   edm::EDGetTokenT<reco::VertexCollection> vtxToken_;
0047   edm::EDGetTokenT<edm::View<reco::GsfElectron>> eleToken_;
0048 
0049   static constexpr double MAX_PHI = 3.2;
0050   static constexpr int N_PHI = 64;
0051   const MEbinning phi_binning_{N_PHI, -MAX_PHI, MAX_PHI};
0052 
0053   static constexpr double MAX_dxy = 2.5;
0054   static constexpr int N_dxy = 50;
0055   const MEbinning dxy_binning_{N_dxy, -MAX_dxy, MAX_dxy};
0056 
0057   static constexpr double MAX_ETA = 2.4;
0058   static constexpr int N_ETA = 68;
0059   const MEbinning eta_binning_{N_ETA, -MAX_ETA, MAX_ETA};
0060 
0061   std::vector<double> muon_variable_binning_;
0062   std::vector<double> muoneta_variable_binning_;
0063   MEbinning muon_binning_;
0064   MEbinning ls_binning_;
0065   std::vector<double> muPt_variable_binning_2D_;
0066   std::vector<double> elePt_variable_binning_2D_;
0067   std::vector<double> muEta_variable_binning_2D_;
0068   std::vector<double> eleEta_variable_binning_2D_;
0069 
0070   ObjME muonME_;
0071   ObjME muonEtaME_;
0072   ObjME muonPhiME_;
0073   ObjME muonME_variableBinning_;
0074   ObjME muonVsLS_;
0075   ObjME muonEtaPhiME_;
0076   ObjME muondxy_;
0077   ObjME muondz_;
0078   ObjME muonEtaME_variableBinning_;
0079   ObjME eleME_variableBinning_;
0080   ObjME eleEtaME_;
0081   ObjME eleEta_muEta_;
0082   ObjME elePt_muPt_;
0083 
0084   std::unique_ptr<GenericTriggerEventFlag> num_genTriggerEventFlag_;
0085   std::unique_ptr<GenericTriggerEventFlag> den_genTriggerEventFlag_;
0086 
0087   StringCutObjectSelector<reco::MET, true> metSelection_;
0088   StringCutObjectSelector<reco::Muon, true> muonSelection_;
0089   StringCutObjectSelector<reco::GsfElectron, true> eleSelection_;
0090 
0091   unsigned int nmuons_;
0092   unsigned int nelectrons_;
0093 };
0094 
0095 MuonMonitor::MuonMonitor(const edm::ParameterSet& iConfig)
0096     : folderName_(iConfig.getParameter<std::string>("FolderName")),
0097       requireValidHLTPaths_(iConfig.getParameter<bool>("requireValidHLTPaths")),
0098       hltPathsAreValid_(false),
0099       metToken_(consumes<reco::PFMETCollection>(iConfig.getParameter<edm::InputTag>("met"))),
0100       muonToken_(mayConsume<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0101       vtxToken_(mayConsume<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
0102       eleToken_(mayConsume<edm::View<reco::GsfElectron>>(iConfig.getParameter<edm::InputTag>("electrons"))),
0103       muon_variable_binning_(
0104           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muonBinning")),
0105       muoneta_variable_binning_(
0106           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muonetaBinning")),
0107       muon_binning_(getHistoPSet(
0108           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonPSet"))),
0109       ls_binning_(
0110           getHistoPSet(iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("lsPSet"))),
0111       muPt_variable_binning_2D_(
0112           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muPtBinning2D")),
0113       elePt_variable_binning_2D_(
0114           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("elePtBinning2D")),
0115       muEta_variable_binning_2D_(
0116           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muEtaBinning2D")),
0117       eleEta_variable_binning_2D_(
0118           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("eleEtaBinning2D")),
0119       num_genTriggerEventFlag_(new GenericTriggerEventFlag(
0120           iConfig.getParameter<edm::ParameterSet>("numGenericTriggerEventPSet"), consumesCollector(), *this)),
0121       den_genTriggerEventFlag_(new GenericTriggerEventFlag(
0122           iConfig.getParameter<edm::ParameterSet>("denGenericTriggerEventPSet"), consumesCollector(), *this)),
0123       metSelection_(iConfig.getParameter<std::string>("metSelection")),
0124       muonSelection_(iConfig.getParameter<std::string>("muonSelection")),
0125       eleSelection_(iConfig.getParameter<std::string>("eleSelection")),
0126       nmuons_(iConfig.getParameter<unsigned int>("nmuons")),
0127       nelectrons_(iConfig.getParameter<unsigned int>("nelectrons")) {}
0128 
0129 MuonMonitor::~MuonMonitor() throw() {
0130   if (num_genTriggerEventFlag_) {
0131     num_genTriggerEventFlag_.reset();
0132   }
0133   if (den_genTriggerEventFlag_) {
0134     den_genTriggerEventFlag_.reset();
0135   }
0136 }
0137 
0138 void MuonMonitor::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) {
0139   // Initialize the GenericTriggerEventFlag
0140   if (num_genTriggerEventFlag_ && num_genTriggerEventFlag_->on()) {
0141     num_genTriggerEventFlag_->initRun(iRun, iSetup);
0142   }
0143   if (den_genTriggerEventFlag_ && den_genTriggerEventFlag_->on()) {
0144     den_genTriggerEventFlag_->initRun(iRun, iSetup);
0145   }
0146 
0147   // check if every HLT path specified in numerator and denominator has a valid match in the HLT Menu
0148   hltPathsAreValid_ = (num_genTriggerEventFlag_ && den_genTriggerEventFlag_ && num_genTriggerEventFlag_->on() &&
0149                        den_genTriggerEventFlag_->on() && num_genTriggerEventFlag_->allHLTPathsAreValid() &&
0150                        den_genTriggerEventFlag_->allHLTPathsAreValid());
0151 
0152   // if valid HLT paths are required,
0153   // create DQM outputs only if all paths are valid
0154   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0155     return;
0156   }
0157 
0158   std::string histname, histtitle;
0159 
0160   ibooker.setCurrentFolder(folderName_);
0161 
0162   histname = "muon_pt";
0163   histtitle = "muon PT";
0164   bookME(ibooker, muonME_, histname, histtitle, muon_binning_.nbins, muon_binning_.xmin, muon_binning_.xmax);
0165   setMETitle(muonME_, "Muon pT [GeV]", "events / [GeV]");
0166 
0167   histname = "muon_pt_variable";
0168   histtitle = "muon PT";
0169   bookME(ibooker, muonME_variableBinning_, histname, histtitle, muon_variable_binning_);
0170   setMETitle(muonME_variableBinning_, "Muon pT [GeV]", "events / [GeV]");
0171 
0172   histname = "muonVsLS";
0173   histtitle = "muon pt vs LS";
0174   bookME(ibooker,
0175          muonVsLS_,
0176          histname,
0177          histtitle,
0178          ls_binning_.nbins,
0179          ls_binning_.xmin,
0180          ls_binning_.xmax,
0181          muon_binning_.xmin,
0182          muon_binning_.xmax);
0183   setMETitle(muonVsLS_, "LS", "Muon pT [GeV]");
0184 
0185   histname = "muon_phi";
0186   histtitle = "Muon phi";
0187   bookME(ibooker, muonPhiME_, histname, histtitle, phi_binning_.nbins, phi_binning_.xmin, phi_binning_.xmax);
0188   setMETitle(muonPhiME_, "Muon #phi", "events / 0.1 rad");
0189 
0190   histname = "muon_eta";
0191   histtitle = "Muon eta";
0192   bookME(ibooker, muonEtaME_, histname, histtitle, eta_binning_.nbins, eta_binning_.xmin, eta_binning_.xmax);
0193   setMETitle(muonEtaME_, "Muon #eta", "events");
0194 
0195   histname = "muon_eta_variablebinning";
0196   histtitle = "Muon eta";
0197   bookME(ibooker, muonEtaME_variableBinning_, histname, histtitle, muoneta_variable_binning_);
0198   setMETitle(muonEtaME_variableBinning_, "Muon #eta", "events");
0199 
0200   histname = "muon_dxy";
0201   histtitle = "Muon dxy";
0202   bookME(ibooker, muondxy_, histname, histtitle, dxy_binning_.nbins, dxy_binning_.xmin, dxy_binning_.xmax);
0203   setMETitle(muondxy_, "Muon #dxy", "events");
0204 
0205   histname = "muon_dz";
0206   histtitle = "Muon dz";
0207   bookME(ibooker, muondz_, histname, histtitle, dxy_binning_.nbins, dxy_binning_.xmin, dxy_binning_.xmax);
0208   setMETitle(muondz_, "Muon #dz", "events");
0209 
0210   histname = "muon_etaphi";
0211   histtitle = "Muon eta-phi";
0212   bookME(ibooker,
0213          muonEtaPhiME_,
0214          histname,
0215          histtitle,
0216          eta_binning_.nbins,
0217          eta_binning_.xmin,
0218          eta_binning_.xmax,
0219          phi_binning_.nbins,
0220          phi_binning_.xmin,
0221          phi_binning_.xmax);
0222   setMETitle(muonEtaPhiME_, "#eta", "#phi");
0223 
0224   histname = "electron_pt_variable";
0225   histtitle = "electron PT";
0226   bookME(ibooker, eleME_variableBinning_, histname, histtitle, muon_variable_binning_);
0227   setMETitle(eleME_variableBinning_, "Electron pT [GeV]", "events / [GeV]");
0228 
0229   histname = "electron_eta";
0230   histtitle = "electron eta";
0231   bookME(ibooker, eleEtaME_, histname, histtitle, eta_binning_.nbins, eta_binning_.xmin, eta_binning_.xmax);
0232   setMETitle(eleEtaME_, "Electron #eta", "events");
0233 
0234   histname = "elePt_muPt";
0235   histtitle = "electron pt vs muon pt";
0236   bookME(ibooker, elePt_muPt_, histname, histtitle, elePt_variable_binning_2D_, muPt_variable_binning_2D_);
0237   setMETitle(elePt_muPt_, "electron pt [GeV]", "muon pt [GeV]");
0238 
0239   histname = "eleEta_muEta";
0240   histtitle = "electron #eta vs muon #eta";
0241   bookME(ibooker, eleEta_muEta_, histname, histtitle, eleEta_variable_binning_2D_, muEta_variable_binning_2D_);
0242   setMETitle(eleEta_muEta_, "electron #eta", "muon #eta");
0243 }
0244 
0245 void MuonMonitor::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0246   // if valid HLT paths are required,
0247   // analyze event only if all paths are valid
0248   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0249     return;
0250   }
0251 
0252   // Filter out events if Trigger Filtering is requested
0253   if (den_genTriggerEventFlag_->on() && !den_genTriggerEventFlag_->accept(iEvent, iSetup)) {
0254     return;
0255   }
0256 
0257   edm::Handle<reco::PFMETCollection> metHandle;
0258   iEvent.getByToken(metToken_, metHandle);
0259   reco::PFMET pfmet = metHandle->front();
0260   if (!metSelection_(pfmet)) {
0261     return;
0262   }
0263 
0264   edm::Handle<reco::VertexCollection> vtxHandle;
0265   iEvent.getByToken(vtxToken_, vtxHandle);
0266 
0267   math::XYZPoint pv(0, 0, 0);
0268   for (reco::Vertex const& v : *vtxHandle) {
0269     if (not v.isFake()) {
0270       pv.SetXYZ(v.x(), v.y(), v.z());
0271       break;
0272     }
0273   }
0274 
0275   edm::Handle<reco::MuonCollection> muonHandle;
0276   iEvent.getByToken(muonToken_, muonHandle);
0277   std::vector<reco::Muon> muons;
0278   if (muonHandle->size() < nmuons_)
0279     return;
0280   for (auto const& p : *muonHandle) {
0281     if (muonSelection_(p))
0282       muons.push_back(p);
0283   }
0284   if (muons.size() < nmuons_)
0285     return;
0286 
0287   edm::Handle<edm::View<reco::GsfElectron>> eleHandle;
0288   iEvent.getByToken(eleToken_, eleHandle);
0289   std::vector<reco::GsfElectron> electrons;
0290   if (eleHandle->size() < nelectrons_)
0291     return;
0292   for (auto const& e : *eleHandle) {
0293     if (eleSelection_(e))
0294       electrons.push_back(e);
0295   }
0296   if (electrons.size() < nelectrons_)
0297     return;
0298 
0299   // filling histograms (denominator)
0300   const int ls = iEvent.id().luminosityBlock();
0301 
0302   if (!muons.empty()) {
0303     muonME_.denominator->Fill(muons[0].pt());
0304     muonME_variableBinning_.denominator->Fill(muons[0].pt());
0305     muonPhiME_.denominator->Fill(muons[0].phi());
0306     muonEtaME_.denominator->Fill(muons[0].eta());
0307     muonVsLS_.denominator->Fill(ls, muons[0].pt());
0308     muonEtaPhiME_.denominator->Fill(muons[0].eta(), muons[0].phi());
0309     muondxy_.denominator->Fill(muons[0].muonBestTrack()->dxy(pv));
0310     muondz_.denominator->Fill(muons[0].muonBestTrack()->dz(pv));
0311     if (!electrons.empty()) {
0312       eleME_variableBinning_.denominator->Fill(electrons[0].pt());
0313       eleEtaME_.denominator->Fill(electrons[0].eta());
0314       eleEta_muEta_.denominator->Fill(electrons[0].eta(), muons[0].eta());
0315       elePt_muPt_.denominator->Fill(electrons[0].pt(), muons[0].pt());
0316     }
0317   }
0318 
0319   // applying selection for numerator
0320   if (num_genTriggerEventFlag_->on() && !num_genTriggerEventFlag_->accept(iEvent, iSetup))
0321     return;
0322 
0323   // filling histograms (num_genTriggerEventFlag_)
0324   if (!muons.empty()) {
0325     muonME_.numerator->Fill(muons[0].pt());
0326     muonME_variableBinning_.numerator->Fill(muons[0].pt());
0327     muonPhiME_.numerator->Fill(muons[0].phi());
0328     muonEtaME_.numerator->Fill(muons[0].eta());
0329     muonVsLS_.numerator->Fill(ls, muons[0].pt());
0330     muonEtaPhiME_.numerator->Fill(muons[0].eta(), muons[0].phi());
0331     muondxy_.numerator->Fill(muons[0].muonBestTrack()->dxy(pv));
0332     muondz_.numerator->Fill(muons[0].muonBestTrack()->dz(pv));
0333     if (!electrons.empty()) {
0334       eleME_variableBinning_.numerator->Fill(electrons[0].pt());
0335       eleEtaME_.numerator->Fill(electrons[0].eta());
0336       eleEta_muEta_.numerator->Fill(electrons[0].eta(), muons[0].eta());
0337       elePt_muPt_.numerator->Fill(electrons[0].pt(), muons[0].pt());
0338     }
0339   }
0340 }
0341 
0342 void MuonMonitor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0343   edm::ParameterSetDescription desc;
0344   desc.add<std::string>("FolderName", "HLT/Muon");
0345   desc.add<bool>("requireValidHLTPaths", true);
0346 
0347   desc.add<edm::InputTag>("met", edm::InputTag("pfMet"));
0348   desc.add<edm::InputTag>("muons", edm::InputTag("muons"));
0349   desc.add<edm::InputTag>("vertices", edm::InputTag("offlinePrimaryVertices"));
0350   desc.add<edm::InputTag>("electrons", edm::InputTag("gedGsfElectrons"));
0351   desc.add<std::string>("metSelection", "pt > 0");
0352   desc.add<std::string>("muonSelection", "pt > 6 && eta<2.4");
0353   desc.add<std::string>("eleSelection", "pt > 0");
0354   desc.add<unsigned int>("nmuons", 0);
0355   desc.add<unsigned int>("nelectrons", 0);
0356 
0357   edm::ParameterSetDescription genericTriggerEventPSet;
0358   GenericTriggerEventFlag::fillPSetDescription(genericTriggerEventPSet);
0359   desc.add<edm::ParameterSetDescription>("numGenericTriggerEventPSet", genericTriggerEventPSet);
0360   desc.add<edm::ParameterSetDescription>("denGenericTriggerEventPSet", genericTriggerEventPSet);
0361 
0362   edm::ParameterSetDescription histoPSet;
0363   edm::ParameterSetDescription metPSet;
0364   fillHistoPSetDescription(metPSet);
0365   histoPSet.add<edm::ParameterSetDescription>("muonPSet", metPSet);
0366   std::vector<double> bins = {0.,   20.,  40.,  60.,  80.,  90.,  100., 110., 120., 130., 140., 150., 160.,
0367                               170., 180., 190., 200., 220., 240., 260., 280., 300., 350., 400., 450., 1000.};
0368   histoPSet.add<std::vector<double>>("muonBinning", bins);
0369 
0370   std::vector<double> etabins = {-3., -2.5, -2., -1.5, -1., -.5, 0., .5, 1., 1.5, 2., 2.5, 3.};
0371   histoPSet.add<std::vector<double>>("muonetaBinning", etabins);
0372 
0373   std::vector<double> bins_2D = {0., 40., 80., 100., 120., 140., 160., 180., 200., 240., 280., 350., 450., 1000.};
0374   std::vector<double> eta_bins_2D = {-3., -2., -1., 0., 1., 2., 3.};
0375   std::vector<double> phi_bins_2D = {
0376       -3.1415, -2.5132, -1.8849, -1.2566, -0.6283, 0, 0.6283, 1.2566, 1.8849, 2.5132, 3.1415};
0377   histoPSet.add<std::vector<double>>("elePtBinning2D", bins_2D);
0378   histoPSet.add<std::vector<double>>("muPtBinning2D", bins_2D);
0379   histoPSet.add<std::vector<double>>("eleEtaBinning2D", eta_bins_2D);
0380   histoPSet.add<std::vector<double>>("muEtaBinning2D", eta_bins_2D);
0381 
0382   edm::ParameterSetDescription lsPSet;
0383   fillHistoLSPSetDescription(lsPSet);
0384   histoPSet.add<edm::ParameterSetDescription>("lsPSet", lsPSet);
0385 
0386   desc.add<edm::ParameterSetDescription>("histoPSet", histoPSet);
0387 
0388   descriptions.add("muonMonitoring", desc);
0389 }
0390 
0391 DEFINE_FWK_MODULE(MuonMonitor);