Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:53

0001 #include <string>
0002 #include <vector>
0003 
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "DQMServices/Core/interface/DQMStore.h"
0008 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0009 #include "DQMOffline/Trigger/plugins/TriggerDQMBase.h"
0010 #include "CommonTools/TriggerUtils/interface/GenericTriggerEventFlag.h"
0011 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0012 
0013 #include "DataFormats/TrackReco/interface/Track.h"
0014 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0015 
0016 class DiDispStaMuonMonitor : public DQMEDAnalyzer, public TriggerDQMBase {
0017 public:
0018   typedef dqm::reco::MonitorElement MonitorElement;
0019   typedef dqm::reco::DQMStore DQMStore;
0020 
0021   DiDispStaMuonMonitor(const edm::ParameterSet&);
0022   ~DiDispStaMuonMonitor() throw() override;
0023   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0024 
0025 protected:
0026   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0027   void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
0028 
0029 private:
0030   const std::string folderName_;
0031 
0032   const bool requireValidHLTPaths_;
0033   bool hltPathsAreValid_;
0034 
0035   edm::EDGetTokenT<reco::TrackCollection> muonToken_;
0036 
0037   std::vector<double> muonPt_variable_binning_;
0038   MEbinning muonPt_binning_;
0039   MEbinning muonEta_binning_;
0040   MEbinning muonPhi_binning_;
0041   MEbinning muonDxy_binning_;
0042   MEbinning ls_binning_;
0043 
0044   ObjME muonPtME_;
0045   ObjME muonPtNoDxyCutME_;
0046   ObjME muonPtME_variableBinning_;
0047   ObjME muonPtVsLS_;
0048   ObjME muonEtaME_;
0049   ObjME muonPhiME_;
0050   ObjME muonDxyME_;
0051   ObjME subMuonPtME_;
0052   ObjME subMuonPtME_variableBinning_;
0053   ObjME subMuonEtaME_;
0054   ObjME subMuonPhiME_;
0055   ObjME subMuonDxyME_;
0056 
0057   std::unique_ptr<GenericTriggerEventFlag> num_genTriggerEventFlag_;
0058   std::unique_ptr<GenericTriggerEventFlag> den_genTriggerEventFlag_;
0059 
0060   StringCutObjectSelector<reco::Track, true> muonSelectionGeneral_;
0061   StringCutObjectSelector<reco::Track, true> muonSelectionPt_;
0062   StringCutObjectSelector<reco::Track, true> muonSelectionDxy_;
0063 
0064   unsigned int nmuons_;
0065 };
0066 
0067 DiDispStaMuonMonitor::DiDispStaMuonMonitor(const edm::ParameterSet& iConfig)
0068     : folderName_(iConfig.getParameter<std::string>("FolderName")),
0069       requireValidHLTPaths_(iConfig.getParameter<bool>("requireValidHLTPaths")),
0070       hltPathsAreValid_(false),
0071       muonToken_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0072       muonPt_variable_binning_(
0073           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muonPtBinning")),
0074       muonPt_binning_(getHistoPSet(
0075           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonPtPSet"))),
0076       muonEta_binning_(getHistoPSet(
0077           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonEtaPSet"))),
0078       muonPhi_binning_(getHistoPSet(
0079           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonPhiPSet"))),
0080       muonDxy_binning_(getHistoPSet(
0081           iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonDxyPSet"))),
0082       ls_binning_(
0083           getHistoPSet(iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("lsPSet"))),
0084       num_genTriggerEventFlag_(new GenericTriggerEventFlag(
0085           iConfig.getParameter<edm::ParameterSet>("numGenericTriggerEventPSet"), consumesCollector(), *this)),
0086       den_genTriggerEventFlag_(new GenericTriggerEventFlag(
0087           iConfig.getParameter<edm::ParameterSet>("denGenericTriggerEventPSet"), consumesCollector(), *this)),
0088       muonSelectionGeneral_(
0089           iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("general")),
0090       muonSelectionPt_(iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("pt")),
0091       muonSelectionDxy_(iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("dxy")),
0092       nmuons_(iConfig.getParameter<unsigned int>("nmuons")) {}
0093 
0094 DiDispStaMuonMonitor::~DiDispStaMuonMonitor() throw() {
0095   if (num_genTriggerEventFlag_) {
0096     num_genTriggerEventFlag_.reset();
0097   }
0098   if (den_genTriggerEventFlag_) {
0099     den_genTriggerEventFlag_.reset();
0100   }
0101 }
0102 
0103 void DiDispStaMuonMonitor::bookHistograms(DQMStore::IBooker& ibooker,
0104                                           edm::Run const& iRun,
0105                                           edm::EventSetup const& iSetup) {
0106   // Initialize the GenericTriggerEventFlag
0107   if (num_genTriggerEventFlag_ && num_genTriggerEventFlag_->on()) {
0108     num_genTriggerEventFlag_->initRun(iRun, iSetup);
0109   }
0110   if (den_genTriggerEventFlag_ && den_genTriggerEventFlag_->on()) {
0111     den_genTriggerEventFlag_->initRun(iRun, iSetup);
0112   }
0113 
0114   // check if every HLT path specified in numerator and denominator has a valid match in the HLT Menu
0115   hltPathsAreValid_ = (num_genTriggerEventFlag_ && den_genTriggerEventFlag_ && num_genTriggerEventFlag_->on() &&
0116                        den_genTriggerEventFlag_->on() && num_genTriggerEventFlag_->allHLTPathsAreValid() &&
0117                        den_genTriggerEventFlag_->allHLTPathsAreValid());
0118 
0119   // if valid HLT paths are required,
0120   // create DQM outputs only if all paths are valid
0121   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0122     return;
0123   }
0124 
0125   std::string histname, histtitle;
0126 
0127   std::string currentFolder = folderName_;
0128   ibooker.setCurrentFolder(currentFolder);
0129 
0130   histname = "muonPt";
0131   histtitle = "muonPt";
0132 
0133   bookME(ibooker, muonPtME_, histname, histtitle, muonPt_binning_.nbins, muonPt_binning_.xmin, muonPt_binning_.xmax);
0134   setMETitle(muonPtME_, "DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");
0135 
0136   histname = "muonPtNoDxyCut";
0137   histtitle = "muonPtNoDxyCut";
0138   bookME(ibooker,
0139          muonPtNoDxyCutME_,
0140          histname,
0141          histtitle,
0142          muonPt_binning_.nbins,
0143          muonPt_binning_.xmin,
0144          muonPt_binning_.xmax);
0145   setMETitle(muonPtNoDxyCutME_, "DisplacedStandAlone Muon p_{T} [GeV] without Dxy cut", "Events / [GeV]");
0146 
0147   histname = "muonPt_variable";
0148   histtitle = "muonPt";
0149   bookME(ibooker, muonPtME_variableBinning_, histname, histtitle, muonPt_variable_binning_);
0150   setMETitle(muonPtME_variableBinning_, "DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");
0151 
0152   histname = "muonPtVsLS";
0153   histtitle = "muonPt vs LS";
0154   bookME(ibooker,
0155          muonPtVsLS_,
0156          histname,
0157          histtitle,
0158          ls_binning_.nbins,
0159          ls_binning_.xmin,
0160          ls_binning_.xmax,
0161          muonPt_binning_.xmin,
0162          muonPt_binning_.xmax);
0163   setMETitle(muonPtVsLS_, "LS", "DisplacedStandAlone Muon p_{T} [GeV]");
0164 
0165   histname = "muonEta";
0166   histtitle = "muonEta";
0167   bookME(
0168       ibooker, muonEtaME_, histname, histtitle, muonEta_binning_.nbins, muonEta_binning_.xmin, muonEta_binning_.xmax);
0169   setMETitle(muonEtaME_, "DisplacedStandAlone Muon #eta", "Events");
0170 
0171   histname = "muonPhi";
0172   histtitle = "muonPhi";
0173   bookME(
0174       ibooker, muonPhiME_, histname, histtitle, muonPhi_binning_.nbins, muonPhi_binning_.xmin, muonPhi_binning_.xmax);
0175   setMETitle(muonPhiME_, "DisplacedStandAlone Muon #phi", "Events");
0176 
0177   histname = "muonDxy";
0178   histtitle = "muonDxy";
0179   bookME(
0180       ibooker, muonDxyME_, histname, histtitle, muonDxy_binning_.nbins, muonDxy_binning_.xmin, muonDxy_binning_.xmax);
0181   setMETitle(muonDxyME_, "DisplacedStandAlone Muon #dxy", "Events");
0182 
0183   if (nmuons_ > 1) {
0184     histname = "subMuonPt";
0185     histtitle = "subMuonPt";
0186     bookME(
0187         ibooker, subMuonPtME_, histname, histtitle, muonPt_binning_.nbins, muonPt_binning_.xmin, muonPt_binning_.xmax);
0188     setMETitle(subMuonPtME_, "Subleading DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");
0189 
0190     histname = "subMuonPt_variable";
0191     histtitle = "subMuonPt";
0192     bookME(ibooker, subMuonPtME_variableBinning_, histname, histtitle, muonPt_variable_binning_);
0193     setMETitle(subMuonPtME_variableBinning_, "Subleading DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");
0194 
0195     histname = "subMuonEta";
0196     histtitle = "subMuonEta";
0197     bookME(ibooker,
0198            subMuonEtaME_,
0199            histname,
0200            histtitle,
0201            muonEta_binning_.nbins,
0202            muonEta_binning_.xmin,
0203            muonEta_binning_.xmax);
0204     setMETitle(subMuonEtaME_, "Subleading DisplacedStandAlone Muon #eta", "Events");
0205 
0206     histname = "subMuonPhi";
0207     histtitle = "subMuonPhi";
0208     bookME(ibooker,
0209            subMuonPhiME_,
0210            histname,
0211            histtitle,
0212            muonPhi_binning_.nbins,
0213            muonPhi_binning_.xmin,
0214            muonPhi_binning_.xmax);
0215     setMETitle(subMuonPhiME_, "Subleading DisplacedStandAlone Muon #phi", "Events");
0216 
0217     histname = "subMuonDxy";
0218     histtitle = "subMuonDxy";
0219     bookME(ibooker,
0220            subMuonDxyME_,
0221            histname,
0222            histtitle,
0223            muonDxy_binning_.nbins,
0224            muonDxy_binning_.xmin,
0225            muonDxy_binning_.xmax);
0226     setMETitle(subMuonDxyME_, "Subleading DisplacedStandAlone Muon #dxy", "Events");
0227   }
0228 }
0229 
0230 void DiDispStaMuonMonitor::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0231   // if valid HLT paths are required,
0232   // analyze event only if all paths are valid
0233   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0234     return;
0235   }
0236 
0237   // Filter out events if Trigger Filtering is requested
0238   if (den_genTriggerEventFlag_->on() && !den_genTriggerEventFlag_->accept(iEvent, iSetup))
0239     return;
0240 
0241   const int ls = iEvent.id().luminosityBlock();
0242 
0243   edm::Handle<reco::TrackCollection> DSAHandle;
0244   iEvent.getByToken(muonToken_, DSAHandle);
0245   if ((unsigned int)(DSAHandle->size()) < nmuons_)
0246     return;
0247   std::vector<edm::Ptr<reco::Track>> dsaMuonPtrs_{};  // = DSAHandle->ptrs();
0248   for (size_t i(0); i != DSAHandle->size(); ++i) {
0249     dsaMuonPtrs_.emplace_back(DSAHandle, i);
0250   }
0251   std::vector<edm::Ptr<reco::Track>> muons{}, muonsCutOnPt{}, muonsCutOnDxy{}, muonsCutOnPtAndDxy{};
0252 
0253   // general selection
0254   auto selectGeneral_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionGeneral_(*m); });
0255   std::copy_if(dsaMuonPtrs_.begin(), dsaMuonPtrs_.end(), back_inserter(muons), selectGeneral_);
0256   if ((unsigned int)(muons.size()) < nmuons_)
0257     return;
0258 
0259   // sort by pt
0260   auto ptSorter_ = [](edm::Ptr<reco::Track> const& lhs, edm::Ptr<reco::Track> const& rhs) -> bool {
0261     return lhs->pt() > rhs->pt();
0262   };
0263   std::sort(muons.begin(), muons.end(), ptSorter_);
0264 
0265   // cut on pt
0266   auto selectOnPt_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionPt_(*m); });
0267   std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnPt), selectOnPt_);
0268   // cut on dxy
0269   auto selectOnDxy_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionDxy_(*m); });
0270   std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnDxy), selectOnDxy_);
0271   // cut on pt and dxy
0272   auto selectOnPtAndDxy_(
0273       [this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionPt_(*m) && muonSelectionDxy_(*m); });
0274   std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnPtAndDxy), selectOnPtAndDxy_);
0275 
0276   std::sort(muonsCutOnPt.begin(), muonsCutOnPt.end(), ptSorter_);
0277   std::sort(muonsCutOnDxy.begin(), muonsCutOnDxy.end(), ptSorter_);
0278   std::sort(muonsCutOnPtAndDxy.begin(), muonsCutOnPtAndDxy.end(), ptSorter_);
0279 
0280   // --------------------------------
0281   // filling histograms (denominator)
0282   // --------------------------------
0283   if (muonsCutOnDxy.size() >= nmuons_) {
0284     // pt has cut on dxy
0285     muonPtME_.denominator->Fill(muonsCutOnDxy[0]->pt());
0286     muonPtNoDxyCutME_.denominator->Fill(muons[0]->pt());
0287     muonPtME_variableBinning_.denominator->Fill(muonsCutOnDxy[0]->pt());
0288     muonPtVsLS_.denominator->Fill(ls, muonsCutOnDxy[0]->pt());
0289     if (nmuons_ > 1) {
0290       subMuonPtME_.denominator->Fill(muonsCutOnDxy[1]->pt());
0291       subMuonPtME_variableBinning_.denominator->Fill(muonsCutOnDxy[1]->pt());
0292     }
0293   }
0294   if (muonsCutOnPtAndDxy.size() >= nmuons_) {
0295     // eta, phi have cut on pt and dxy
0296     muonEtaME_.denominator->Fill(muonsCutOnPtAndDxy[0]->eta());
0297     muonPhiME_.denominator->Fill(muonsCutOnPtAndDxy[0]->phi());
0298     if (nmuons_ > 1) {
0299       subMuonEtaME_.denominator->Fill(muonsCutOnPtAndDxy[1]->eta());
0300       subMuonPhiME_.denominator->Fill(muonsCutOnPtAndDxy[1]->phi());
0301     }
0302   }
0303   if (muonsCutOnPt.size() >= nmuons_) {
0304     // dxy has cut on pt
0305     muonDxyME_.denominator->Fill(muonsCutOnPt[0]->dxy());
0306     if (nmuons_ > 1) {
0307       subMuonDxyME_.denominator->Fill(muonsCutOnPt[1]->dxy());
0308     }
0309   }
0310 
0311   // --------------------------------
0312   // filling histograms (numerator)
0313   // --------------------------------
0314   if (num_genTriggerEventFlag_->on() && !num_genTriggerEventFlag_->accept(iEvent, iSetup))
0315     return;
0316 
0317   if (muonsCutOnDxy.size() >= nmuons_) {
0318     // pt has cut on dxy
0319     muonPtME_.numerator->Fill(muonsCutOnDxy[0]->pt());
0320     muonPtNoDxyCutME_.numerator->Fill(muons[0]->pt());
0321     muonPtME_variableBinning_.numerator->Fill(muonsCutOnDxy[0]->pt());
0322     muonPtVsLS_.numerator->Fill(ls, muonsCutOnDxy[0]->pt());
0323     if (nmuons_ > 1) {
0324       subMuonPtME_.numerator->Fill(muonsCutOnDxy[1]->pt());
0325       subMuonPtME_variableBinning_.numerator->Fill(muonsCutOnDxy[1]->pt());
0326     }
0327   }
0328   if (muonsCutOnPtAndDxy.size() >= nmuons_) {
0329     // eta, phi have cut on pt and dxy
0330     muonEtaME_.numerator->Fill(muonsCutOnPtAndDxy[0]->eta());
0331     muonPhiME_.numerator->Fill(muonsCutOnPtAndDxy[0]->phi());
0332     if (nmuons_ > 1) {
0333       subMuonEtaME_.numerator->Fill(muonsCutOnPtAndDxy[1]->eta());
0334       subMuonPhiME_.numerator->Fill(muonsCutOnPtAndDxy[1]->phi());
0335     }
0336   }
0337   if (muonsCutOnPt.size() >= nmuons_) {
0338     // dxy has cut on pt
0339     muonDxyME_.numerator->Fill(muonsCutOnPt[0]->dxy());
0340     if (nmuons_ > 1) {
0341       subMuonDxyME_.numerator->Fill(muonsCutOnPt[1]->dxy());
0342     }
0343   }
0344 }
0345 
0346 void DiDispStaMuonMonitor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0347   edm::ParameterSetDescription desc;
0348   desc.add<std::string>("FolderName", "HLT/EXO/DiDispStaMuon");
0349   desc.add<bool>("requireValidHLTPaths", true);
0350 
0351   desc.add<edm::InputTag>("muons", edm::InputTag("displacedStandAloneMuons"));
0352   desc.add<unsigned int>("nmuons", 2);
0353 
0354   edm::ParameterSetDescription muonSelection;
0355   muonSelection.add<std::string>("general", "pt > 0");
0356   muonSelection.add<std::string>("pt", "");
0357   muonSelection.add<std::string>("dxy", "pt > 0");
0358   desc.add<edm::ParameterSetDescription>("muonSelection", muonSelection);
0359 
0360   edm::ParameterSetDescription genericTriggerEventPSet;
0361   GenericTriggerEventFlag::fillPSetDescription(genericTriggerEventPSet);
0362   desc.add<edm::ParameterSetDescription>("numGenericTriggerEventPSet", genericTriggerEventPSet);
0363   desc.add<edm::ParameterSetDescription>("denGenericTriggerEventPSet", genericTriggerEventPSet);
0364 
0365   edm::ParameterSetDescription histoPSet;
0366   edm::ParameterSetDescription muonPtPSet;
0367   edm::ParameterSetDescription muonEtaPSet;
0368   edm::ParameterSetDescription muonPhiPSet;
0369   edm::ParameterSetDescription muonDxyPSet;
0370   edm::ParameterSetDescription lsPSet;
0371   fillHistoPSetDescription(muonPtPSet);
0372   fillHistoPSetDescription(muonEtaPSet);
0373   fillHistoPSetDescription(muonPhiPSet);
0374   fillHistoPSetDescription(muonDxyPSet);
0375   fillHistoPSetDescription(lsPSet);
0376   histoPSet.add<edm::ParameterSetDescription>("muonPtPSet", muonPtPSet);
0377   histoPSet.add<edm::ParameterSetDescription>("muonEtaPSet", muonEtaPSet);
0378   histoPSet.add<edm::ParameterSetDescription>("muonPhiPSet", muonPhiPSet);
0379   histoPSet.add<edm::ParameterSetDescription>("muonDxyPSet", muonDxyPSet);
0380   histoPSet.add<edm::ParameterSetDescription>("lsPSet", lsPSet);
0381   std::vector<double> bins = {0.,   20.,  40.,  60.,  80.,  90.,  100., 110., 120., 130., 140., 150., 160.,
0382                               170., 180., 190., 200., 220., 240., 260., 280., 300., 350., 400., 450., 1000.};
0383   histoPSet.add<std::vector<double>>("muonPtBinning", bins);
0384 
0385   desc.add<edm::ParameterSetDescription>("histoPSet", histoPSet);
0386 
0387   descriptions.add("DiDispStaMuonMonitoring", desc);
0388 }
0389 
0390 DEFINE_FWK_MODULE(DiDispStaMuonMonitor);