Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <string>
0002 #include <vector>
0003 
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.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 #include "DataFormats/Math/interface/deltaR.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/TrackReco/interface/Track.h"
0020 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0021 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0022 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0023 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0024 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
0025 #include "DQMOffline/Trigger/plugins/METDQM.h"
0026 #include "DQMOffline/Trigger/plugins/JetDQM.h"
0027 #include "DQMOffline/Trigger/plugins/HTDQM.h"
0028 #include "DQMOffline/Trigger/plugins/HMesonGammaDQM.h"
0029 
0030 class ObjMonitor : public DQMEDAnalyzer, public TriggerDQMBase {
0031 public:
0032   typedef dqm::reco::MonitorElement MonitorElement;
0033   typedef dqm::reco::DQMStore DQMStore;
0034 
0035   ObjMonitor(const edm::ParameterSet&);
0036   ~ObjMonitor() throw() override;
0037   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0038 
0039 protected:
0040   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0041   void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
0042 
0043 private:
0044   bool looseJetId(const double& abseta,
0045                   const double& NHF,
0046                   const double& NEMF,
0047                   const double& CHF,
0048                   const double& CEMF,
0049                   const unsigned& NumNeutralParticles,
0050                   const unsigned& CHM);
0051 
0052   bool tightJetId(const double& abseta,
0053                   const double& NHF,
0054                   const double& NEMF,
0055                   const double& CHF,
0056                   const double& CEMF,
0057                   const unsigned& NumNeutralParticles,
0058                   const unsigned& CHM);
0059 
0060   const std::string folderName_;
0061 
0062   const bool requireValidHLTPaths_;
0063   bool hltPathsAreValid_;
0064 
0065   edm::EDGetTokenT<reco::PFMETCollection> metToken_;
0066   edm::EDGetTokenT<reco::PFJetCollection> jetToken_;
0067   edm::EDGetTokenT<reco::GsfElectronCollection> eleToken_;
0068   edm::EDGetTokenT<reco::MuonCollection> muoToken_;
0069   edm::EDGetTokenT<reco::PhotonCollection> phoToken_;
0070   edm::EDGetTokenT<reco::TrackCollection> trkToken_;
0071 
0072   //objects to plot
0073   //add your own with corresponding switch
0074   bool do_met_;
0075   METDQM metDQM_;
0076   bool do_jet_;
0077   JetDQM jetDQM_;
0078   bool do_ht_;
0079   HTDQM htDQM_;
0080   bool do_hmg_;
0081   HMesonGammaDQM hmgDQM_;
0082 
0083   std::unique_ptr<GenericTriggerEventFlag> num_genTriggerEventFlag_;
0084   std::unique_ptr<GenericTriggerEventFlag> den_genTriggerEventFlag_;
0085 
0086   StringCutObjectSelector<reco::MET, true> metSelection_;
0087   StringCutObjectSelector<reco::PFJet, true> jetSelection_;
0088   std::string jetId_;
0089   StringCutObjectSelector<reco::PFJet, true> htjetSelection_;
0090   StringCutObjectSelector<reco::GsfElectron, true> eleSelection_;
0091   StringCutObjectSelector<reco::Muon, true> muoSelection_;
0092   StringCutObjectSelector<reco::Photon, true> phoSelection_;
0093   StringCutObjectSelector<reco::Track, true> trkSelection_;
0094 
0095   unsigned njets_;
0096   unsigned nelectrons_;
0097   unsigned nmuons_;
0098   unsigned nphotons_;
0099   unsigned nmesons_;
0100 };
0101 
0102 ObjMonitor::ObjMonitor(const edm::ParameterSet& iConfig)
0103     : folderName_(iConfig.getParameter<std::string>("FolderName")),
0104       requireValidHLTPaths_(iConfig.getParameter<bool>("requireValidHLTPaths")),
0105       hltPathsAreValid_(false),
0106       metToken_(consumes<reco::PFMETCollection>(iConfig.getParameter<edm::InputTag>("met"))),
0107       jetToken_(mayConsume<reco::PFJetCollection>(iConfig.getParameter<edm::InputTag>("jets"))),
0108       eleToken_(mayConsume<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("electrons"))),
0109       muoToken_(mayConsume<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0110       phoToken_(mayConsume<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("photons"))),
0111       trkToken_(mayConsume<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))),
0112       do_met_(iConfig.getParameter<bool>("doMETHistos")),
0113       do_jet_(iConfig.getParameter<bool>("doJetHistos")),
0114       do_ht_(iConfig.getParameter<bool>("doHTHistos")),
0115       do_hmg_(iConfig.getParameter<bool>("doHMesonGammaHistos")),
0116       num_genTriggerEventFlag_(std::make_unique<GenericTriggerEventFlag>(
0117           iConfig.getParameter<edm::ParameterSet>("numGenericTriggerEventPSet"), consumesCollector(), *this)),
0118       den_genTriggerEventFlag_(std::make_unique<GenericTriggerEventFlag>(
0119           iConfig.getParameter<edm::ParameterSet>("denGenericTriggerEventPSet"), consumesCollector(), *this)),
0120       metSelection_(iConfig.getParameter<std::string>("metSelection")),
0121       jetSelection_(iConfig.getParameter<std::string>("jetSelection")),
0122       jetId_(iConfig.getParameter<std::string>("jetId")),
0123       htjetSelection_(iConfig.getParameter<std::string>("htjetSelection")),
0124       eleSelection_(iConfig.getParameter<std::string>("eleSelection")),
0125       muoSelection_(iConfig.getParameter<std::string>("muoSelection")),
0126       phoSelection_(iConfig.getParameter<std::string>("phoSelection")),
0127       trkSelection_(iConfig.getParameter<std::string>("trkSelection")),
0128       njets_(iConfig.getParameter<int>("njets")),
0129       nelectrons_(iConfig.getParameter<int>("nelectrons")),
0130       nmuons_(iConfig.getParameter<int>("nmuons")),
0131       nphotons_(iConfig.getParameter<int>("nphotons")),
0132       nmesons_(iConfig.getParameter<int>("nmesons")) {
0133   if (do_met_) {
0134     metDQM_.initialise(iConfig);
0135   }
0136   if (do_jet_) {
0137     jetDQM_.initialise(iConfig);
0138   }
0139   if (do_ht_) {
0140     htDQM_.initialise(iConfig);
0141   }
0142   if (do_hmg_) {
0143     hmgDQM_.initialise(iConfig);
0144   }
0145 }
0146 
0147 ObjMonitor::~ObjMonitor() throw() {
0148   if (num_genTriggerEventFlag_) {
0149     num_genTriggerEventFlag_.reset();
0150   }
0151   if (den_genTriggerEventFlag_) {
0152     den_genTriggerEventFlag_.reset();
0153   }
0154 }
0155 
0156 void ObjMonitor::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) {
0157   // Initialize the GenericTriggerEventFlag
0158   if (num_genTriggerEventFlag_ && num_genTriggerEventFlag_->on())
0159     num_genTriggerEventFlag_->initRun(iRun, iSetup);
0160   if (den_genTriggerEventFlag_ && den_genTriggerEventFlag_->on())
0161     den_genTriggerEventFlag_->initRun(iRun, iSetup);
0162 
0163   // check if every HLT path specified in numerator and denominator has a valid match in the HLT Menu
0164   hltPathsAreValid_ = (num_genTriggerEventFlag_ && den_genTriggerEventFlag_ && num_genTriggerEventFlag_->on() &&
0165                        den_genTriggerEventFlag_->on() && num_genTriggerEventFlag_->allHLTPathsAreValid() &&
0166                        den_genTriggerEventFlag_->allHLTPathsAreValid());
0167 
0168   // if valid HLT paths are required,
0169   // create DQM outputs only if all paths are valid
0170   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0171     return;
0172   }
0173 
0174   std::string currentFolder = folderName_;
0175   ibooker.setCurrentFolder(currentFolder);
0176 
0177   if (do_met_)
0178     metDQM_.bookHistograms(ibooker);
0179   if (do_jet_)
0180     jetDQM_.bookHistograms(ibooker);
0181   if (do_ht_)
0182     htDQM_.bookHistograms(ibooker);
0183   if (do_hmg_)
0184     hmgDQM_.bookHistograms(ibooker);
0185 }
0186 
0187 void ObjMonitor::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0188   // if valid HLT paths are required,
0189   // analyze event only if all paths are valid
0190   if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
0191     return;
0192   }
0193 
0194   // Filter out events if Trigger Filtering is requested
0195   if (den_genTriggerEventFlag_->on() && !den_genTriggerEventFlag_->accept(iEvent, iSetup))
0196     return;
0197 
0198   edm::Handle<reco::PFMETCollection> metHandle;
0199   iEvent.getByToken(metToken_, metHandle);
0200   reco::PFMET pfmet = metHandle->front();
0201   if (!metSelection_(pfmet))
0202     return;
0203 
0204   float met = pfmet.pt();
0205   float phi = pfmet.phi();
0206 
0207   edm::Handle<reco::PFJetCollection> jetHandle;
0208   iEvent.getByToken(jetToken_, jetHandle);
0209   std::vector<reco::PFJet> jets;
0210   std::vector<reco::PFJet> htjets;
0211   if (jetHandle->size() < njets_)
0212     return;
0213   for (auto const& j : *jetHandle) {
0214     if (jetSelection_(j)) {
0215       if (jetId_ == "loose" || jetId_ == "tight") {
0216         double abseta = abs(j.eta());
0217         double NHF = j.neutralHadronEnergyFraction();
0218         double NEMF = j.neutralEmEnergyFraction();
0219         double CHF = j.chargedHadronEnergyFraction();
0220         double CEMF = j.chargedEmEnergyFraction();
0221         unsigned NumNeutralParticles = j.neutralMultiplicity();
0222         unsigned CHM = j.chargedMultiplicity();
0223         bool passId = (jetId_ == "loose" && looseJetId(abseta, NHF, NEMF, CHF, CEMF, NumNeutralParticles, CHM)) ||
0224                       (jetId_ == "tight" && tightJetId(abseta, NHF, NEMF, CHF, CEMF, NumNeutralParticles, CHM));
0225         if (passId)
0226           jets.push_back(j);
0227       } else
0228         jets.push_back(j);
0229     }
0230     if (htjetSelection_(j))
0231       htjets.push_back(j);
0232   }
0233   if (jets.size() < njets_)
0234     return;
0235 
0236   edm::Handle<reco::GsfElectronCollection> eleHandle;
0237   iEvent.getByToken(eleToken_, eleHandle);
0238   std::vector<reco::GsfElectron> electrons;
0239   if (eleHandle->size() < nelectrons_)
0240     return;
0241   for (auto const& e : *eleHandle) {
0242     if (eleSelection_(e))
0243       electrons.push_back(e);
0244   }
0245   if (electrons.size() < nelectrons_)
0246     return;
0247 
0248   edm::Handle<reco::MuonCollection> muoHandle;
0249   iEvent.getByToken(muoToken_, muoHandle);
0250   if (muoHandle->size() < nmuons_)
0251     return;
0252   std::vector<reco::Muon> muons;
0253   for (auto const& m : *muoHandle) {
0254     if (muoSelection_(m))
0255       muons.push_back(m);
0256   }
0257   if (muons.size() < nmuons_)
0258     return;
0259 
0260   edm::Handle<reco::PhotonCollection> phoHandle;
0261   iEvent.getByToken(phoToken_, phoHandle);
0262   if (phoHandle->size() < nphotons_)
0263     return;
0264   std::vector<reco::Photon> photons;
0265   for (auto const& m : *phoHandle) {
0266     if (phoSelection_(m))
0267       photons.push_back(m);
0268   }
0269   if (photons.size() < nphotons_)
0270     return;
0271 
0272   std::vector<TLorentzVector> passedMesons;
0273   if (do_hmg_) {
0274     edm::Handle<reco::TrackCollection> trkHandle;
0275     iEvent.getByToken(trkToken_, trkHandle);
0276     // find isolated mesons (phi or rho)
0277     TLorentzVector t1, t2;
0278     float hadronMassHyp[2] = {0.1396, 0.4937};  // pi or K mass
0279     float loMassLim[2] = {0.5, 0.9};            // rho or phi mass
0280     float hiMassLim[2] = {1.0, 1.11};           // rho or phi mass
0281 
0282     for (size_t i = 0; i < trkHandle->size(); ++i) {
0283       const reco::Track trk1 = trkHandle->at(i);
0284       if (!trkSelection_(trk1))
0285         continue;
0286       for (size_t j = i + 1; j < trkHandle->size(); ++j) {
0287         const reco::Track trk2 = trkHandle->at(j);
0288         if (!trkSelection_(trk2))
0289           continue;
0290         if (trk1.charge() * trk2.charge() != -1)
0291           continue;
0292 
0293         for (unsigned hyp = 0; hyp < 2; ++hyp) {
0294           t1.SetPtEtaPhiM(trk1.pt(), trk1.eta(), trk1.phi(), hadronMassHyp[hyp]);
0295           t2.SetPtEtaPhiM(trk2.pt(), trk2.eta(), trk2.phi(), hadronMassHyp[hyp]);
0296           TLorentzVector mesCand = t1 + t2;
0297 
0298           // cuts
0299           if (mesCand.M() < loMassLim[hyp] || mesCand.M() > hiMassLim[hyp])
0300             continue;  //mass
0301           if (mesCand.Pt() < 35. || fabs(mesCand.Rapidity()) > 2.1)
0302             continue;  //pT eta
0303 
0304           // isolation
0305           float absIso = 0.;
0306           for (size_t k = 0; k < trkHandle->size(); ++k) {
0307             if (k == i || k == j)
0308               continue;
0309             const reco::Track trkN = trkHandle->at(k);
0310             if (trkN.charge() == 0 || trkN.pt() < 0.5 || (trkN.dz() > 0.1) ||
0311                 deltaR(trkN.eta(), trkN.phi(), mesCand.Eta(), mesCand.Phi()) > 0.5)
0312               continue;
0313             absIso += trkN.pt();
0314           }
0315           if (absIso / mesCand.Pt() > 0.2)
0316             continue;
0317           passedMesons.push_back(mesCand);
0318         }
0319       }
0320     }
0321     if (passedMesons.size() < nmesons_)
0322       return;
0323   }
0324 
0325   bool passNumCond = num_genTriggerEventFlag_->off() || num_genTriggerEventFlag_->accept(iEvent, iSetup);
0326   int ls = iEvent.id().luminosityBlock();
0327 
0328   if (do_met_)
0329     metDQM_.fillHistograms(met, phi, ls, passNumCond);
0330   if (do_jet_)
0331     jetDQM_.fillHistograms(jets, pfmet, ls, passNumCond);
0332   if (do_ht_)
0333     htDQM_.fillHistograms(htjets, met, ls, passNumCond);
0334   if (do_hmg_)
0335     hmgDQM_.fillHistograms(photons, passedMesons, ls, passNumCond);
0336 }
0337 
0338 bool ObjMonitor::looseJetId(const double& abseta,
0339                             const double& NHF,
0340                             const double& NEMF,
0341                             const double& CHF,
0342                             const double& CEMF,
0343                             const unsigned& NumNeutralParticles,
0344                             const unsigned& CHM) {
0345   if (abseta <= 2.7) {
0346     unsigned NumConst = CHM + NumNeutralParticles;
0347 
0348     return ((NumConst > 1 && NHF < 0.99 && NEMF < 0.99) &&
0349             ((abseta <= 2.4 && CHF > 0 && CHM > 0 && CEMF < 0.99) || abseta > 2.4));
0350   } else if (abseta <= 3) {
0351     return (NumNeutralParticles > 2 && NEMF > 0.01 && NHF < 0.98);
0352   } else {
0353     return NumNeutralParticles > 10 && NEMF < 0.90;
0354   }
0355 }
0356 bool ObjMonitor::tightJetId(const double& abseta,
0357                             const double& NHF,
0358                             const double& NEMF,
0359                             const double& CHF,
0360                             const double& CEMF,
0361                             const unsigned& NumNeutralParticles,
0362                             const unsigned& CHM) {
0363   if (abseta <= 2.7) {
0364     unsigned NumConst = CHM + NumNeutralParticles;
0365     return (NumConst > 1 && NHF < 0.90 && NEMF < 0.90) &&
0366            ((abseta <= 2.4 && CHF > 0 && CHM > 0 && CEMF < 0.99) || abseta > 2.4);
0367   } else if (abseta <= 3) {
0368     return (NHF < 0.98 && NEMF > 0.01 && NumNeutralParticles > 2);
0369   } else {
0370     return (NEMF < 0.90 && NumNeutralParticles > 10);
0371   }
0372 }
0373 
0374 void ObjMonitor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0375   edm::ParameterSetDescription desc;
0376   desc.add<std::string>("FolderName", "HLT/OBJ");
0377   desc.add<bool>("requireValidHLTPaths", true);
0378 
0379   desc.add<edm::InputTag>("met", edm::InputTag("pfMet"));
0380   desc.add<edm::InputTag>("jets", edm::InputTag("ak4PFJetsCHS"));
0381   desc.add<edm::InputTag>("electrons", edm::InputTag("gedGsfElectrons"));
0382   desc.add<edm::InputTag>("muons", edm::InputTag("muons"));
0383   desc.add<edm::InputTag>("photons", edm::InputTag("gedPhotons"));
0384   desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
0385   desc.add<std::string>("metSelection", "pt > 0");
0386   desc.add<std::string>("jetSelection", "pt > 0");
0387   desc.add<std::string>("jetId", "");
0388   desc.add<std::string>("htjetSelection", "pt > 30");
0389   desc.add<std::string>("eleSelection", "pt > 0");
0390   desc.add<std::string>("muoSelection", "pt > 0");
0391   desc.add<std::string>("phoSelection", "pt > 0");
0392   desc.add<std::string>("trkSelection", "pt > 0");
0393   desc.add<int>("njets", 0);
0394   desc.add<int>("nelectrons", 0);
0395   desc.add<int>("nmuons", 0);
0396   desc.add<int>("nphotons", 0);
0397   desc.add<int>("nmesons", 0);
0398 
0399   edm::ParameterSetDescription genericTriggerEventPSet;
0400   GenericTriggerEventFlag::fillPSetDescription(genericTriggerEventPSet);
0401   desc.add<edm::ParameterSetDescription>("numGenericTriggerEventPSet", genericTriggerEventPSet);
0402   desc.add<edm::ParameterSetDescription>("denGenericTriggerEventPSet", genericTriggerEventPSet);
0403 
0404   desc.add<bool>("doMETHistos", true);
0405   edm::ParameterSetDescription histoPSet;
0406   METDQM::fillMetDescription(histoPSet);
0407   desc.add<bool>("doJetHistos", true);
0408   JetDQM::fillJetDescription(histoPSet);
0409   desc.add<bool>("doHTHistos", true);
0410   HTDQM::fillHtDescription(histoPSet);
0411   desc.add<bool>("doHMesonGammaHistos", true);
0412   HMesonGammaDQM::fillHmgDescription(histoPSet);
0413 
0414   desc.add<edm::ParameterSetDescription>("histoPSet", histoPSet);
0415 
0416   descriptions.add("objMonitoring", desc);
0417 }
0418 
0419 // Define this as a plug-in
0420 #include "FWCore/Framework/interface/MakerMacros.h"
0421 DEFINE_FWK_MODULE(ObjMonitor);