Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-02 22:46:30

0001 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0002 #include "CommonTools/TriggerUtils/interface/GenericTriggerEventFlag.h"
0003 #include "CommonTools/CandUtils/interface/AddFourMomenta.h"
0004 
0005 #include "DataFormats/Math/interface/deltaR.h"
0006 #include "DataFormats/Common/interface/ValueMap.h"
0007 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0008 #include "DataFormats/MuonReco/interface/Muon.h"
0009 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0010 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0011 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0012 #include "DataFormats/JetReco/interface/PFJet.h"
0013 #include "DataFormats/JetReco/interface/PFJetCollection.h"
0014 #include "DataFormats/METReco/interface/PFMET.h"
0015 #include "DataFormats/METReco/interface/PFMETCollection.h"
0016 #include "DataFormats/BTauReco/interface/JetTag.h"
0017 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
0018 #include "DataFormats/Candidate/interface/CompositeCandidateFwd.h"
0019 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0020 #include "DataFormats/Common/interface/TriggerResults.h"
0021 #include "DataFormats/BTauReco/interface/ShallowTagInfo.h"
0022 #include "DataFormats/Math/interface/LorentzVector.h"
0023 
0024 #include "DQMServices/Core/interface/DQMStore.h"
0025 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0026 #include "DQMOffline/Trigger/plugins/TriggerDQMBase.h"
0027 
0028 #include "FWCore/Utilities/interface/EDGetToken.h"
0029 #include "FWCore/Framework/interface/Frameworkfwd.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/MakerMacros.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/ServiceRegistry/interface/Service.h"
0034 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0035 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0036 #include "FWCore/ParameterSet/interface/Registry.h"
0037 #include "FWCore/Utilities/interface/transform.h"
0038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0039 
0040 #include "JetMETCorrections/JetCorrector/interface/JetCorrector.h"
0041 
0042 #include <string>
0043 #include <vector>
0044 #include <memory>
0045 #include <map>
0046 
0047 class ParticleNetJetTagMonitor : public DQMEDAnalyzer, public TriggerDQMBase {
0048 public:
0049   typedef dqm::reco::MonitorElement MonitorElement;
0050   typedef dqm::reco::DQMStore DQMStore;
0051 
0052   ParticleNetJetTagMonitor(const edm::ParameterSet&);
0053   ~ParticleNetJetTagMonitor() throw() override;
0054   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0055 
0056 protected:
0057   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0058   void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
0059 
0060   struct JetRefCompare {
0061     inline bool operator()(const edm::RefToBase<reco::Jet>& j1, const edm::RefToBase<reco::Jet>& j2) const {
0062       return (j1.id() < j2.id()) || ((j1.id() == j2.id()) && (j1.key() < j2.key()));
0063     }
0064   };
0065 
0066 private:
0067   // folder for output histograms
0068   const std::string folderName_;
0069   // validity of HLT paths required for the monitoring element
0070   const bool requireValidHLTPaths_;
0071   const bool requireHLTOfflineJetMatching_;
0072   bool denHLTPathsAreValid_;
0073   bool numHLTPathsAreValid_;
0074   // input vertex collection
0075   const edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
0076   // input muon collection
0077   const edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0078   // input electron collection and IDs
0079   const edm::EDGetTokenT<reco::GsfElectronCollection> electronToken_;
0080   const edm::EDGetTokenT<edm::ValueMap<bool>> tagElectronIDToken_;
0081   const edm::EDGetTokenT<edm::ValueMap<bool>> vetoElectronIDToken_;
0082   // input jet collection
0083   const edm::EDGetTokenT<reco::PFJetCollection> jetToken_;
0084   // PNET score for offline and online jets
0085   const edm::EDGetTokenT<reco::JetTagCollection> jetPNETScoreToken_;
0086   const edm::EDGetTokenT<reco::JetTagCollection> jetPNETScoreHLTToken_;
0087   // Collection and PNET score for ak4 b-tagging and HT if needed
0088   const edm::EDGetTokenT<reco::PFJetCollection> jetForHTandBTagToken_;
0089   const edm::EDGetTokenT<reco::JetTagCollection> jetPNETScoreForHTandBTagToken_;
0090   // Jet soft drop value map
0091   const edm::EDGetTokenT<edm::ValueMap<float>> jetSoftDropMassToken_;
0092   // input MET collection
0093   const edm::EDGetTokenT<reco::PFMETCollection> metToken_;
0094   // JEC corrections
0095   const edm::EDGetTokenT<reco::JetCorrector> jecMCToken_;
0096   const edm::EDGetTokenT<reco::JetCorrector> jecDataToken_;
0097   // trigger conditions for numerator and denominator
0098   std::unique_ptr<GenericTriggerEventFlag> numGenericTriggerEvent_;
0099   std::unique_ptr<GenericTriggerEventFlag> denGenericTriggerEvent_;
0100   // Selectors for jets, electrons, muons, and lepton pairs
0101   StringCutObjectSelector<reco::PFJet, true> jetSelection_;
0102   StringCutObjectSelector<reco::PFJet, true> jetSelectionForHTandBTag_;
0103   StringCutObjectSelector<reco::Muon, true> tagMuonSelection_;
0104   StringCutObjectSelector<reco::GsfElectron, true> tagElectronSelection_;
0105   StringCutObjectSelector<reco::Muon, true> vetoMuonSelection_;
0106   StringCutObjectSelector<reco::GsfElectron, true> vetoElectronSelection_;
0107   StringCutObjectSelector<reco::Vertex, true> vertexSelection_;
0108   StringCutObjectSelector<reco::CompositeCandidate, true> dileptonSelection_;
0109   StringCutObjectSelector<reco::PFMET, true> metSelection_;
0110   // Number of objects used in the event selection
0111   const int njets_;
0112   const int nbjets_;
0113   const int ntagleptons_;
0114   const int ntagmuons_;
0115   const int ntagelectrons_;
0116   const int nvetoleptons_;
0117   const int nvetomuons_;
0118   const int nvetoelectrons_;
0119   const int nemupairs_;
0120   const unsigned int ntrigobjecttomatch_;
0121   // delta-R for cleaning and other parameters for the event selection
0122   const double lepJetDeltaRmin_;
0123   const double lepJetDeltaRminForHTandBTag_;
0124   const double hltRecoDeltaRmax_;
0125   const double maxLeptonDxyCut_;
0126   const double maxLeptonDzCut_;
0127   const double minPNETScoreCut_;
0128   const double minPNETBTagCut_;
0129   const double minSoftDropMassCut_;
0130   const double maxSoftDropMassCut_;
0131   // binning for efficiency histograms (up to two jets in the final state)
0132   std::vector<double> leptonPtBinning;
0133   std::vector<double> leptonEtaBinning;
0134   std::vector<double> diLeptonPtBinning;
0135   std::vector<double> diLeptonMassBinning;
0136   std::vector<double> HTBinning;
0137   std::vector<double> NjetBinning;
0138   std::vector<double> jet1PtBinning;
0139   std::vector<double> jet2PtBinning;
0140   std::vector<double> jet1EtaBinning;
0141   std::vector<double> jet2EtaBinning;
0142   std::vector<double> jet1PNETscoreBinning;
0143   std::vector<double> jet2PNETscoreBinning;
0144   std::vector<double> jet1PNETscoreTransBinning;
0145   std::vector<double> jet2PNETscoreTransBinning;
0146   std::vector<double> jet1PtBinning2d;
0147   std::vector<double> jet2PtBinning2d;
0148   std::vector<double> jet1EtaBinning2d;
0149   std::vector<double> jet2EtaBinning2d;
0150   std::vector<double> jet1PNETscoreBinning2d;
0151   std::vector<double> jet2PNETscoreBinning2d;
0152   std::vector<double> jet1PNETscoreTransBinning2d;
0153   std::vector<double> jet2PNETscoreTransBinning2d;
0154 
0155   // Selections imposed
0156   MonitorElement* selectionFlow = nullptr;
0157   // Efficiencies
0158   ObjME muon_pt;
0159   ObjME electron_pt;
0160   ObjME muon_eta;
0161   ObjME electron_eta;
0162   ObjME dilepton_pt;
0163   ObjME dilepton_mass;
0164   ObjME njets;
0165   ObjME nbjets;
0166   ObjME ht;
0167   ObjME jet1_pt;
0168   ObjME jet2_pt;
0169   ObjME jet1_eta;
0170   ObjME jet2_eta;
0171   ObjME jet1_pnetscore;
0172   ObjME jet2_pnetscore;
0173   ObjME jet1_pnetscore_trans;
0174   ObjME jet2_pnetscore_trans;
0175   ObjME mean_2j_pnetscore;
0176   ObjME mean_2j_pnetscore_trans;
0177 
0178   ObjME jet1_pt_eta;
0179   ObjME jet2_pt_eta;
0180   ObjME jet1_pt_pnetscore1;
0181   ObjME jet2_pt_pnetscore1;
0182   ObjME jet1_pt_pnetscore2;
0183   ObjME jet2_pt_pnetscore2;
0184   ObjME jet1_pt_pnetscore1_trans;
0185   ObjME jet2_pt_pnetscore1_trans;
0186   ObjME jet1_pt_pnetscore2_trans;
0187   ObjME jet2_pt_pnetscore2_trans;
0188   ObjME jet1_pt_mean2pnetscore;
0189   ObjME jet2_pt_mean2pnetscore;
0190   ObjME jet1_pt_mean2pnetscore_trans;
0191   ObjME jet2_pt_mean2pnetscore_trans;
0192 };
0193 
0194 ParticleNetJetTagMonitor::ParticleNetJetTagMonitor(const edm::ParameterSet& iConfig)
0195     : folderName_(iConfig.getParameter<std::string>("FolderName")),
0196       requireValidHLTPaths_(iConfig.getParameter<bool>("requireValidHLTPaths")),
0197       requireHLTOfflineJetMatching_(iConfig.getParameter<bool>("requireHLTOfflineJetMatching")),
0198       denHLTPathsAreValid_(false),
0199       numHLTPathsAreValid_(false),
0200       vertexToken_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
0201       muonToken_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0202       electronToken_(consumes<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("electrons"))),
0203       tagElectronIDToken_(consumes<edm::ValueMap<bool>>(iConfig.getParameter<edm::InputTag>("tagElectronID"))),
0204       vetoElectronIDToken_(consumes<edm::ValueMap<bool>>(iConfig.getParameter<edm::InputTag>("vetoElectronID"))),
0205       jetToken_(consumes<reco::PFJetCollection>(iConfig.getParameter<edm::InputTag>("jets"))),
0206       jetPNETScoreToken_(consumes<reco::JetTagCollection>(iConfig.getParameter<edm::InputTag>("jetPNETScore"))),
0207       jetPNETScoreHLTToken_(mayConsume<reco::JetTagCollection>(iConfig.getParameter<edm::InputTag>("jetPNETScoreHLT"))),
0208       jetForHTandBTagToken_(mayConsume<reco::PFJetCollection>(iConfig.getParameter<edm::InputTag>("jetsForHTandBTag"))),
0209       jetPNETScoreForHTandBTagToken_(
0210           mayConsume<reco::JetTagCollection>(iConfig.getParameter<edm::InputTag>("jetPNETScoreForHTandBTag"))),
0211       jetSoftDropMassToken_(mayConsume<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("jetSoftDropMass"))),
0212       metToken_(consumes<reco::PFMETCollection>(iConfig.getParameter<edm::InputTag>("met"))),
0213       jecMCToken_(mayConsume<reco::JetCorrector>(iConfig.getParameter<edm::InputTag>("jecForMC"))),
0214       jecDataToken_(mayConsume<reco::JetCorrector>(iConfig.getParameter<edm::InputTag>("jecForData"))),
0215       numGenericTriggerEvent_(new GenericTriggerEventFlag(
0216           iConfig.getParameter<edm::ParameterSet>("numGenericTriggerEvent"), consumesCollector(), *this)),
0217       denGenericTriggerEvent_(new GenericTriggerEventFlag(
0218           iConfig.getParameter<edm::ParameterSet>("denGenericTriggerEvent"), consumesCollector(), *this)),
0219       jetSelection_(iConfig.getParameter<std::string>("jetSelection")),
0220       jetSelectionForHTandBTag_(iConfig.existsAs<std::string>("jetSelectionForHTandBTag")
0221                                     ? iConfig.getParameter<std::string>("jetSelectionForHTandBTag")
0222                                     : ""),
0223       tagMuonSelection_(iConfig.getParameter<std::string>("tagMuonSelection")),
0224       tagElectronSelection_(iConfig.getParameter<std::string>("tagElectronSelection")),
0225       vetoMuonSelection_(iConfig.getParameter<std::string>("vetoMuonSelection")),
0226       vetoElectronSelection_(iConfig.getParameter<std::string>("vetoElectronSelection")),
0227       vertexSelection_(iConfig.getParameter<std::string>("vertexSelection")),
0228       dileptonSelection_(iConfig.getParameter<std::string>("dileptonSelection")),
0229       metSelection_(iConfig.getParameter<std::string>("metSelection")),
0230       njets_(iConfig.getParameter<int>("njets")),
0231       nbjets_(iConfig.getParameter<int>("nbjets")),
0232       ntagleptons_(iConfig.getParameter<int>("ntagleptons")),
0233       ntagmuons_(iConfig.getParameter<int>("ntagmuons")),
0234       ntagelectrons_(iConfig.getParameter<int>("ntagelectrons")),
0235       nvetoleptons_(iConfig.getParameter<int>("nvetoleptons")),
0236       nvetomuons_(iConfig.getParameter<int>("nvetomuons")),
0237       nvetoelectrons_(iConfig.getParameter<int>("nvetoelectrons")),
0238       nemupairs_(iConfig.getParameter<int>("nemupairs")),
0239       ntrigobjecttomatch_(iConfig.getParameter<unsigned int>("ntrigobjecttomatch")),
0240       lepJetDeltaRmin_(iConfig.getParameter<double>("lepJetDeltaRmin")),
0241       lepJetDeltaRminForHTandBTag_(iConfig.getParameter<double>("lepJetDeltaRminForHTandBTag")),
0242       hltRecoDeltaRmax_(iConfig.getParameter<double>("hltRecoDeltaRmax")),
0243       maxLeptonDxyCut_(iConfig.getParameter<double>("maxLeptonDxyCut")),
0244       maxLeptonDzCut_(iConfig.getParameter<double>("maxLeptonDzCut")),
0245       minPNETScoreCut_(iConfig.getParameter<double>("minPNETScoreCut")),
0246       minPNETBTagCut_(iConfig.getParameter<double>("minPNETBTagCut")),
0247       minSoftDropMassCut_(iConfig.getParameter<double>("minSoftDropMassCut")),
0248       maxSoftDropMassCut_(iConfig.getParameter<double>("maxSoftDropMassCut")),
0249       leptonPtBinning(iConfig.getParameter<std::vector<double>>("leptonPtBinning")),
0250       leptonEtaBinning(iConfig.getParameter<std::vector<double>>("leptonEtaBinning")),
0251       diLeptonMassBinning(iConfig.getParameter<std::vector<double>>("diLeptonMassBinning")),
0252       HTBinning(iConfig.getParameter<std::vector<double>>("HTBinning")),
0253       NjetBinning(iConfig.getParameter<std::vector<double>>("NjetBinning")),
0254       jet1PtBinning(iConfig.getParameter<std::vector<double>>("jet1PtBinning")),
0255       jet2PtBinning(iConfig.getParameter<std::vector<double>>("jet2PtBinning")),
0256       jet1EtaBinning(iConfig.getParameter<std::vector<double>>("jet1EtaBinning")),
0257       jet2EtaBinning(iConfig.getParameter<std::vector<double>>("jet2EtaBinning")),
0258       jet1PNETscoreBinning(iConfig.getParameter<std::vector<double>>("jet1PNETscoreBinning")),
0259       jet2PNETscoreBinning(iConfig.getParameter<std::vector<double>>("jet2PNETscoreBinning")),
0260       jet1PNETscoreTransBinning(iConfig.getParameter<std::vector<double>>("jet1PNETscoreTransBinning")),
0261       jet2PNETscoreTransBinning(iConfig.getParameter<std::vector<double>>("jet2PNETscoreTransBinning")),
0262       jet1PtBinning2d(iConfig.getParameter<std::vector<double>>("jet1PtBinning2d")),
0263       jet2PtBinning2d(iConfig.getParameter<std::vector<double>>("jet2PtBinning2d")),
0264       jet1EtaBinning2d(iConfig.getParameter<std::vector<double>>("jet1EtaBinning2d")),
0265       jet2EtaBinning2d(iConfig.getParameter<std::vector<double>>("jet2EtaBinning2d")),
0266       jet1PNETscoreBinning2d(iConfig.getParameter<std::vector<double>>("jet1PNETscoreBinning2d")),
0267       jet2PNETscoreBinning2d(iConfig.getParameter<std::vector<double>>("jet2PNETscoreBinning2d")),
0268       jet1PNETscoreTransBinning2d(iConfig.getParameter<std::vector<double>>("jet1PNETscoreTransBinning2d")),
0269       jet2PNETscoreTransBinning2d(iConfig.getParameter<std::vector<double>>("jet2PNETscoreTransBinning2d")) {}
0270 
0271 ParticleNetJetTagMonitor::~ParticleNetJetTagMonitor() throw() {
0272   if (numGenericTriggerEvent_)
0273     denGenericTriggerEvent_.reset();
0274   if (numGenericTriggerEvent_)
0275     denGenericTriggerEvent_.reset();
0276 }
0277 
0278 void ParticleNetJetTagMonitor::bookHistograms(DQMStore::IBooker& ibooker,
0279                                               edm::Run const& iRun,
0280                                               edm::EventSetup const& iSetup) {
0281   // Initialize the GenericTriggerEventFlag
0282   if (denGenericTriggerEvent_ and denGenericTriggerEvent_->on())
0283     denGenericTriggerEvent_->initRun(iRun, iSetup);
0284   if (numGenericTriggerEvent_ and numGenericTriggerEvent_->on())
0285     numGenericTriggerEvent_->initRun(iRun, iSetup);
0286 
0287   denHLTPathsAreValid_ =
0288       (denGenericTriggerEvent_ && denGenericTriggerEvent_->on() && denGenericTriggerEvent_->allHLTPathsAreValid());
0289   numHLTPathsAreValid_ =
0290       (numGenericTriggerEvent_ && numGenericTriggerEvent_->on() && numGenericTriggerEvent_->allHLTPathsAreValid());
0291 
0292   if (requireValidHLTPaths_ && (!denHLTPathsAreValid_))
0293     return;
0294   if (requireValidHLTPaths_ && (!numHLTPathsAreValid_))
0295     return;
0296 
0297   ibooker.setCurrentFolder(folderName_);
0298 
0299   selectionFlow = ibooker.book1D("selectionFlow", "selectionFlow", 17, 0, 17);
0300   selectionFlow->setBinLabel(1, "all");
0301   selectionFlow->setBinLabel(2, "denTrigSel");
0302   selectionFlow->setBinLabel(3, "collecttionSel");
0303   selectionFlow->setBinLabel(4, "vtxSel");
0304   selectionFlow->setBinLabel(5, "tagMuSel");
0305   selectionFlow->setBinLabel(6, "vetoMuSel");
0306   selectionFlow->setBinLabel(7, "tagEleSel");
0307   selectionFlow->setBinLabel(8, "vetoEleSel");
0308   selectionFlow->setBinLabel(9, "tagLepSel");
0309   selectionFlow->setBinLabel(10, "vetoLepSel");
0310   selectionFlow->setBinLabel(11, "diLepSel");
0311   selectionFlow->setBinLabel(12, "btagSel");
0312   selectionFlow->setBinLabel(13, "METSel");
0313   selectionFlow->setBinLabel(14, "jetSel");
0314   selectionFlow->setBinLabel(15, "pnetScoreSel");
0315   selectionFlow->setBinLabel(16, "trigMatchSel");
0316   selectionFlow->setBinLabel(17, "numTrigSel");
0317 
0318   if (!NjetBinning.empty()) {
0319     bookME(ibooker, njets, "njets", "n-jets", NjetBinning);
0320     setMETitle(njets, "number of jets", "Entries");
0321     bookME(ibooker, nbjets, "nbjets", "b-jets", NjetBinning);
0322     setMETitle(nbjets, "number of b-jets", "Entries");
0323   }
0324   if (!HTBinning.empty()) {
0325     bookME(ibooker, ht, "ht", "HT", HTBinning);
0326     setMETitle(ht, "H_{T}", "Entries");
0327   }
0328 
0329   if (!leptonPtBinning.empty()) {
0330     bookME(ibooker, muon_pt, "muon_pt", "muon p_{T}", leptonPtBinning);
0331     setMETitle(muon_pt, "p_{T}(#mu)", "Entries");
0332     bookME(ibooker, electron_pt, "electron_pt", "electron p_{T}", leptonPtBinning);
0333     setMETitle(electron_pt, "p_{T}(ele)", "Entries");
0334   }
0335   if (!leptonEtaBinning.empty()) {
0336     bookME(ibooker, muon_eta, "muon_eta", "muon #eta", leptonEtaBinning);
0337     setMETitle(muon_eta, "#eta(#mu)", "Entries");
0338     bookME(ibooker, electron_eta, "electron_eta", "electron #eta", leptonEtaBinning);
0339     setMETitle(electron_eta, "#eta(ele)", "Entries");
0340   }
0341   if (!diLeptonPtBinning.empty()) {
0342     bookME(ibooker, dilepton_pt, "dilepton_pt", "dilepton p_{T}", diLeptonPtBinning);
0343     setMETitle(dilepton_pt, "p_{T}(ll)", "Entries");
0344   }
0345   if (!diLeptonMassBinning.empty()) {
0346     bookME(ibooker, dilepton_mass, "dilepton_mass", "dilepton mass", diLeptonMassBinning);
0347     setMETitle(dilepton_mass, "m(ll)", "Entries");
0348   }
0349 
0350   if (!jet1PtBinning.empty()) {
0351     bookME(ibooker, jet1_pt, "jet1_pt", "jet1 p_{T}", jet1PtBinning);
0352     setMETitle(jet1_pt, "p_{T}(j1)", "Entries");
0353   }
0354   if (!jet2PtBinning.empty()) {
0355     bookME(ibooker, jet2_pt, "jet2_pt", "jet2 p_{T}", jet2PtBinning);
0356     setMETitle(jet2_pt, "p_{T}(j2)", "Entries");
0357   }
0358   if (!jet1EtaBinning.empty()) {
0359     bookME(ibooker, jet1_eta, "jet1_eta", "jet1 #eta", jet1EtaBinning);
0360     setMETitle(jet1_eta, "#eta(j1)", "Entries");
0361   }
0362   if (!jet2EtaBinning.empty()) {
0363     bookME(ibooker, jet2_eta, "jet2_eta", "jet2 #eta", jet2EtaBinning);
0364     setMETitle(jet2_eta, "#eta(j2)", "Entries");
0365   }
0366   if (!jet1PNETscoreBinning.empty()) {
0367     bookME(ibooker, jet1_pnetscore, "jet1_pnetscore", "jet lead PNET-score", jet1PNETscoreBinning);
0368     setMETitle(jet1_pnetscore, "Lead PNET-score", "Entries");
0369   }
0370 
0371   if (!jet2PNETscoreBinning.empty()) {
0372     bookME(ibooker, jet2_pnetscore, "jet2_pnetscore", "jet train PNET-score", jet2PNETscoreBinning);
0373     setMETitle(jet2_pnetscore, "Trail PNET-score", "Entries");
0374   }
0375   if (!jet1PNETscoreBinning.empty() and !jet2PNETscoreBinning.empty()) {
0376     bookME(ibooker, mean_2j_pnetscore, "mean_2j_pnetscore", "mean 2jet PNET-score", jet1PNETscoreBinning);
0377     setMETitle(mean_2j_pnetscore, "Mean(PNET-score)", "Entries");
0378   }
0379 
0380   if (!jet1PNETscoreTransBinning.empty()) {
0381     bookME(ibooker,
0382            jet1_pnetscore_trans,
0383            "jet1_pnetscore_trans",
0384            "jet lead PNET-score transformed",
0385            jet1PNETscoreTransBinning);
0386     setMETitle(jet1_pnetscore_trans, "Lead atanh(PNET-score)", "Entries");
0387   }
0388   if (!jet2PNETscoreTransBinning.empty()) {
0389     bookME(ibooker,
0390            jet2_pnetscore_trans,
0391            "jet2_pnetscore_trans",
0392            "jet trail PNET-score transformed",
0393            jet2PNETscoreTransBinning);
0394     setMETitle(jet2_pnetscore_trans, "Trail atanh(PNET-score)", "Entries");
0395   }
0396   if (!jet1PNETscoreTransBinning.empty() and !jet2PNETscoreTransBinning.empty()) {
0397     bookME(ibooker,
0398            mean_2j_pnetscore_trans,
0399            "mean_2j_pnetscore_trans",
0400            "mean 2jet PNET-score transformed",
0401            jet1PNETscoreTransBinning);
0402     setMETitle(mean_2j_pnetscore_trans, "atanh(Mean(PNET-score))", "Entries");
0403   }
0404 
0405   // 2D efficiencies
0406   if (!jet1PtBinning2d.empty() and !jet1EtaBinning2d.empty()) {
0407     bookME(ibooker, jet1_pt_eta, "jet1_pt_eta", "jet1 p_{T} vs #eta", jet1PtBinning2d, jet1EtaBinning2d);
0408     setMETitle(jet1_pt_eta, "p_{T}(j1)", "#eta(j1)");
0409   }
0410   if (!jet2PtBinning2d.empty() and !jet2EtaBinning2d.empty()) {
0411     bookME(ibooker, jet2_pt_eta, "jet2_pt_eta", "jet2 p_{T} vs #eta", jet2PtBinning2d, jet2EtaBinning2d);
0412     setMETitle(jet2_pt_eta, "p_{T}(j2)", "#eta(j2)");
0413   }
0414 
0415   if (!jet1PtBinning2d.empty() and !jet1PNETscoreBinning2d.empty()) {
0416     bookME(ibooker,
0417            jet1_pt_pnetscore1,
0418            "jet1_pt_pnetscore1",
0419            "jet1 p{T} vs lead PNET-score",
0420            jet1PtBinning2d,
0421            jet1PNETscoreBinning2d);
0422     setMETitle(jet1_pt_pnetscore1, "p_{T}(j1)", "Lead PNET-score");
0423   }
0424   if (!jet1PtBinning2d.empty() and !jet2PNETscoreBinning2d.empty()) {
0425     bookME(ibooker,
0426            jet1_pt_pnetscore2,
0427            "jet1_pt_pnetscore2",
0428            "jet1 p_{T} vs trail PNET-score",
0429            jet1PtBinning2d,
0430            jet2PNETscoreBinning2d);
0431     setMETitle(jet1_pt_pnetscore2, "p_{T}(j1)", "Trail PNET-score");
0432   }
0433   if (!jet1PtBinning2d.empty() and !jet1PNETscoreBinning2d.empty() and !jet2PNETscoreBinning2d.empty()) {
0434     bookME(ibooker,
0435            jet1_pt_mean2pnetscore,
0436            "jet1_pt_mean2pnetscore",
0437            "jet1 p_{T} vs mean 2jet PNET-score",
0438            jet1PtBinning2d,
0439            jet1PNETscoreBinning2d);
0440     setMETitle(jet1_pt_mean2pnetscore, "p_{T}(j1)", "Mean(PNET-score)");
0441   }
0442 
0443   if (!jet2PtBinning2d.empty() and !jet1PNETscoreBinning2d.empty()) {
0444     bookME(ibooker,
0445            jet2_pt_pnetscore1,
0446            "jet2_pt_pnetscore1",
0447            "jet2 p_{T} vs lead PNET-score",
0448            jet2PtBinning2d,
0449            jet1PNETscoreBinning2d);
0450     setMETitle(jet2_pt_pnetscore1, "p_{T}(j2)", "Lead PNET-score");
0451   }
0452   if (!jet2PtBinning2d.empty() and !jet2PNETscoreBinning2d.empty()) {
0453     bookME(ibooker,
0454            jet2_pt_pnetscore2,
0455            "jet2_pt_pnetscore2",
0456            "jet2 p_{T} vs trail PNET-score",
0457            jet2PtBinning2d,
0458            jet2PNETscoreBinning2d);
0459     setMETitle(jet2_pt_pnetscore2, "p_{T}(j2)", "Trail PNET-score");
0460   }
0461   if (!jet2PtBinning2d.empty() and !jet1PNETscoreBinning2d.empty() and !jet2PNETscoreBinning2d.empty()) {
0462     bookME(ibooker,
0463            jet2_pt_mean2pnetscore,
0464            "jet2_pt_mean2pnetscore",
0465            "jet2 p_{T} vs mean 2jet PNET-score",
0466            jet2PtBinning2d,
0467            jet1PNETscoreBinning2d);
0468     setMETitle(jet2_pt_mean2pnetscore, "p_{T}(j2)", "Mean(PNET-score)");
0469   }
0470 
0471   if (!jet1PtBinning2d.empty() and !jet1PNETscoreTransBinning2d.empty()) {
0472     bookME(ibooker,
0473            jet1_pt_pnetscore1_trans,
0474            "jet1_pt_pnetscore1_trans",
0475            "jet1 p_{T} vs lead PNET-score transformed",
0476            jet1PtBinning2d,
0477            jet1PNETscoreTransBinning2d);
0478     setMETitle(jet1_pt_pnetscore1_trans, "p_{T}(j1)", "Lead atanh(PNET-score)");
0479   }
0480   if (!jet1PtBinning2d.empty() and !jet2PNETscoreTransBinning2d.empty()) {
0481     bookME(ibooker,
0482            jet1_pt_pnetscore2_trans,
0483            "jet1_pt_pnetscore2_trans",
0484            "jet1 p_{T} vs trail PNET-score transformed",
0485            jet1PtBinning2d,
0486            jet2PNETscoreTransBinning2d);
0487     setMETitle(jet1_pt_pnetscore2_trans, "p_{T}(j1)", "Trail atanh(PNET-score)");
0488   }
0489   if (!jet1PtBinning2d.empty() and !jet1PNETscoreTransBinning2d.empty() and !jet2PNETscoreTransBinning2d.empty()) {
0490     bookME(ibooker,
0491            jet1_pt_mean2pnetscore_trans,
0492            "jet1_pt_mean2pnetscore_trans",
0493            "jet1 p_{T} vs mean 2jet PNET-score transformed",
0494            jet1PtBinning2d,
0495            jet1PNETscoreBinning2d);
0496     setMETitle(jet1_pt_mean2pnetscore_trans, "p_{T}(j1)", "atanh(Mean(PNET-score))");
0497   }
0498 
0499   if (!jet2PtBinning2d.empty() and !jet1PNETscoreTransBinning2d.empty()) {
0500     bookME(ibooker,
0501            jet2_pt_pnetscore1_trans,
0502            "jet2_pt_pnetscore1_trans",
0503            "jet2 p_{T} vs lead PNET-score transformed",
0504            jet2PtBinning2d,
0505            jet1PNETscoreTransBinning2d);
0506     setMETitle(jet2_pt_pnetscore1_trans, "p_{T}(j2)", "Lead atanh(PNET-score)");
0507   }
0508   if (!jet2PtBinning2d.empty() and !jet2PNETscoreTransBinning2d.empty()) {
0509     bookME(ibooker,
0510            jet2_pt_pnetscore2_trans,
0511            "jet2_pt_pnetscore2_trans",
0512            "jet2 p_{T} vs trail PNET-score transformed",
0513            jet2PtBinning2d,
0514            jet2PNETscoreTransBinning2d);
0515     setMETitle(jet2_pt_pnetscore2_trans, "p_{T}(j2)", "Trail atanh(PNET-score)");
0516   }
0517   if (!jet2PtBinning2d.empty() and !jet1PNETscoreTransBinning2d.empty() and !jet2PNETscoreTransBinning2d.empty()) {
0518     bookME(ibooker,
0519            jet2_pt_mean2pnetscore_trans,
0520            "jet2_pt_mean2pnetscore_trans",
0521            "jet2 p_{T} vs mean 2jet PNET-score transformed",
0522            jet2PtBinning2d,
0523            jet1PNETscoreBinning2d);
0524     setMETitle(jet2_pt_mean2pnetscore_trans, "p_{T}(j2)", "atanh(Mean(PNET-score))");
0525   }
0526 }
0527 
0528 void ParticleNetJetTagMonitor::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0529   // abort if triggers are not valid
0530   if (requireValidHLTPaths_ and (!denHLTPathsAreValid_ or !numHLTPathsAreValid_))
0531     return;
0532 
0533   int selectionFlowStatus = 0;
0534   selectionFlow->Fill(selectionFlowStatus);
0535 
0536   // Filter out events that don't pass the denominator trigger condition
0537   if (denGenericTriggerEvent_->on() and !denGenericTriggerEvent_->accept(iEvent, iSetup))
0538     return;
0539 
0540   selectionFlowStatus++;
0541   selectionFlow->Fill(selectionFlowStatus);
0542 
0543   // get all input collections
0544   edm::Handle<reco::VertexCollection> primaryVertices;
0545   iEvent.getByToken(vertexToken_, primaryVertices);
0546   if (!primaryVertices.isValid()) {
0547     edm::LogWarning("ParticleNetJetTagMonitor") << "Invalid primary vertex collection, will skip the event";
0548     return;
0549   }
0550 
0551   edm::Handle<reco::MuonCollection> muonHandle;
0552   iEvent.getByToken(muonToken_, muonHandle);
0553   if (!muonHandle.isValid()) {
0554     edm::LogWarning("ParticleNetJetTagMonitor") << "Muon collection not valid, will skip the event \n";
0555     return;
0556   }
0557 
0558   edm::Handle<reco::GsfElectronCollection> electronHandle;
0559   iEvent.getByToken(electronToken_, electronHandle);
0560   if (!electronHandle.isValid()) {
0561     edm::LogWarning("ParticleNetJetTagMonitor") << "Electron collection not valid, will skip the event \n";
0562     return;
0563   }
0564 
0565   edm::Handle<edm::ValueMap<bool>> tagEleIDHandle;
0566   iEvent.getByToken(tagElectronIDToken_, tagEleIDHandle);
0567   if (!tagEleIDHandle.isValid()) {
0568     edm::LogWarning("ParticleNetJetTagMonitor") << "Electron ID for tag not valid, will skip the event \n";
0569     return;
0570   }
0571 
0572   edm::Handle<edm::ValueMap<bool>> vetoEleIDHandle;
0573   iEvent.getByToken(vetoElectronIDToken_, vetoEleIDHandle);
0574   if (!vetoEleIDHandle.isValid()) {
0575     edm::LogWarning("ParticleNetJetTagMonitor") << "Electron ID for veto not valid, will skip the event \n";
0576     return;
0577   }
0578 
0579   edm::Handle<reco::PFJetCollection> jetHandle;
0580   iEvent.getByToken(jetToken_, jetHandle);
0581   if (!jetHandle.isValid()) {
0582     edm::LogWarning("ParticleNetJetTagMonitor") << "Jet collection not valid, will skip the event \n";
0583     return;
0584   }
0585 
0586   edm::Handle<reco::PFMETCollection> metHandle;
0587   iEvent.getByToken(metToken_, metHandle);
0588   if (!metHandle.isValid()) {
0589     edm::LogWarning("ParticleNetJetTagMonitor") << "MET collection not valid, will skip the event \n";
0590     return;
0591   }
0592 
0593   edm::Handle<reco::JetTagCollection> jetPNETScoreHandle;
0594   iEvent.getByToken(jetPNETScoreToken_, jetPNETScoreHandle);
0595   if (!jetPNETScoreHandle.isValid()) {
0596     edm::LogWarning("ParticleNetJetTagMonitor") << "Jet PNET score collection not valid, will skip event \n";
0597     return;
0598   }
0599   const reco::JetTagCollection& jetPNETScore = *(jetPNETScoreHandle.product());
0600 
0601   // Collections that are only imported when necessary and their validity determins the selection applied (different for ak4/ak8 workflows)
0602   edm::Handle<edm::ValueMap<float>> jetSoftDropMassHandle;
0603   iEvent.getByToken(jetSoftDropMassToken_, jetSoftDropMassHandle);
0604   edm::Handle<reco::PFJetCollection> jetForHTandBTagHandle;
0605   iEvent.getByToken(jetForHTandBTagToken_, jetForHTandBTagHandle);
0606   edm::Handle<reco::JetTagCollection> jetPNETScoreForHTandBTagHandle;
0607   iEvent.getByToken(jetPNETScoreForHTandBTagToken_, jetPNETScoreForHTandBTagHandle);
0608   edm::Handle<reco::JetCorrector> jecHandle;
0609   if (iEvent.isRealData())
0610     iEvent.getByToken(jecDataToken_, jecHandle);
0611   else
0612     iEvent.getByToken(jecMCToken_, jecHandle);
0613 
0614   // Start the selection part
0615   selectionFlowStatus++;
0616   selectionFlow->Fill(selectionFlowStatus);
0617 
0618   // primary vertex selection
0619   const reco::Vertex* pv = nullptr;
0620   for (auto const& v : *primaryVertices) {
0621     if (not vertexSelection_(v))
0622       continue;
0623     pv = &v;
0624     break;
0625   }
0626 
0627   if (pv == nullptr) {
0628     edm::LogWarning("ParticleNetJetTagMonitor") << "No good vertex found in the event --> skipped";
0629     return;
0630   }
0631 
0632   selectionFlowStatus++;
0633   selectionFlow->Fill(selectionFlowStatus);
0634 
0635   // Muon selection
0636   std::vector<reco::Muon> tagMuons;
0637   std::vector<reco::Muon> vetoMuons;
0638   for (auto const& m : *muonHandle) {
0639     if (tagMuonSelection_(m) and std::fabs(m.muonBestTrack()->dxy(pv->position())) <= maxLeptonDxyCut_ and
0640         std::fabs(m.muonBestTrack()->dz(pv->position())) <= maxLeptonDzCut_)
0641       tagMuons.push_back(m);
0642     if (vetoMuonSelection_(m) and std::fabs(m.muonBestTrack()->dxy(pv->position())) <= maxLeptonDxyCut_ and
0643         std::fabs(m.muonBestTrack()->dz(pv->position())) <= maxLeptonDzCut_)
0644       vetoMuons.push_back(m);
0645   }
0646 
0647   if (ntagmuons_ >= 0 and int(tagMuons.size()) != ntagmuons_)
0648     return;
0649   selectionFlowStatus++;
0650   selectionFlow->Fill(selectionFlowStatus);
0651 
0652   if (nvetomuons_ >= 0 and int(vetoMuons.size()) != nvetomuons_)
0653     return;
0654   selectionFlowStatus++;
0655   selectionFlow->Fill(selectionFlowStatus);
0656 
0657   // electron selection
0658   std::vector<reco::GsfElectron> tagElectrons;
0659   std::vector<reco::GsfElectron> vetoElectrons;
0660   for (size_t index = 0; index < electronHandle->size(); index++) {
0661     const auto e = electronHandle->at(index);
0662     if (tagElectronSelection_(e) and (*tagEleIDHandle)[reco::GsfElectronRef(electronHandle, index)] and
0663         std::fabs(e.gsfTrack()->dxy(pv->position())) <= maxLeptonDxyCut_ and
0664         std::fabs(e.gsfTrack()->dz(pv->position())) <= maxLeptonDzCut_)
0665       tagElectrons.push_back(e);
0666     if (vetoElectronSelection_(e) and (*vetoEleIDHandle)[reco::GsfElectronRef(electronHandle, index)] and
0667         std::fabs(e.gsfTrack()->dxy(pv->position())) <= maxLeptonDxyCut_ and
0668         std::fabs(e.gsfTrack()->dz(pv->position())) <= maxLeptonDzCut_)
0669       vetoElectrons.push_back(e);
0670   }
0671 
0672   if (ntagelectrons_ >= 0 and int(tagElectrons.size()) != ntagelectrons_)
0673     return;
0674   selectionFlowStatus++;
0675   selectionFlow->Fill(selectionFlowStatus);
0676 
0677   if (nvetoelectrons_ >= 0 and int(vetoElectrons.size()) != nvetoelectrons_)
0678     return;
0679   selectionFlowStatus++;
0680   selectionFlow->Fill(selectionFlowStatus);
0681 
0682   // Overall number of leptons for flavor composition
0683   if (ntagleptons_ >= 0 and int(tagElectrons.size() + tagMuons.size()) != ntagleptons_)
0684     return;
0685   selectionFlowStatus++;
0686   selectionFlow->Fill(selectionFlowStatus);
0687 
0688   if (nvetoleptons_ >= 0 and int(vetoElectrons.size() + vetoMuons.size()) != nvetoleptons_)
0689     return;
0690   selectionFlowStatus++;
0691   selectionFlow->Fill(selectionFlowStatus);
0692 
0693   // Dilepton pairs
0694   std::vector<reco::CompositeCandidate> emuPairs;
0695   for (auto const& muon : tagMuons) {
0696     for (auto const& electron : tagElectrons) {
0697       reco::CompositeCandidate emuPair("emPair");
0698       emuPair.addDaughter(*dynamic_cast<const reco::Candidate*>(&muon), "lep1");
0699       emuPair.addDaughter(*dynamic_cast<const reco::Candidate*>(&electron), "lep2");
0700       AddFourMomenta addp4;
0701       addp4.set(emuPair);
0702       if (dileptonSelection_(emuPair))
0703         emuPairs.push_back(emuPair);
0704     }
0705   }
0706 
0707   if (nemupairs_ >= 0 and int(emuPairs.size()) != nemupairs_)
0708     return;
0709   selectionFlowStatus++;
0710   selectionFlow->Fill(selectionFlowStatus);
0711 
0712   // For b-tagging requriements / content used in AK8 PNET efficiency measurement in semi-leptonic ttbar
0713   float hT = 0;
0714   std::vector<math::XYZTLorentzVector> jetsBTagged;
0715   if (jetForHTandBTagHandle.isValid()) {
0716     const reco::JetTagCollection& jetPNETScoreForHTandBTag = *(jetPNETScoreForHTandBTagHandle.product());
0717     for (auto const& j : *jetForHTandBTagHandle) {
0718       if (not jetSelectionForHTandBTag_(j))
0719         continue;
0720       float minDR_jm = 1000;
0721       for (size_t imu = 0; imu < vetoMuons.size(); imu++) {
0722         float dR = reco::deltaR(vetoMuons.at(imu).p4(), j.p4());
0723         if (dR < minDR_jm)
0724           minDR_jm = dR;
0725       }
0726       if (minDR_jm < lepJetDeltaRminForHTandBTag_)
0727         continue;
0728       float minDR_je = 1000;
0729       for (size_t iel = 0; iel < vetoElectrons.size(); iel++) {
0730         float dR = reco::deltaR(vetoElectrons.at(iel).p4(), j.p4());
0731         if (dR < minDR_je)
0732           minDR_je = dR;
0733       }
0734       if (minDR_je < lepJetDeltaRminForHTandBTag_)
0735         continue;
0736       hT += j.pt();
0737       auto const& jref = reco::JetBaseRef(reco::PFJetRef(jetForHTandBTagHandle, &j - &(*jetForHTandBTagHandle)[0]));
0738       if (jetPNETScoreForHTandBTag[jref] < minPNETBTagCut_)
0739         continue;
0740       jetsBTagged.push_back(j.p4());
0741     }
0742     if (int(jetsBTagged.size()) < nbjets_)
0743       return;
0744   }
0745   selectionFlowStatus++;
0746   selectionFlow->Fill(selectionFlowStatus);
0747 
0748   // MET selectiona
0749   reco::PFMET pfMet = metHandle->front();
0750   if (!metSelection_(pfMet))
0751     return;
0752   selectionFlowStatus++;
0753   selectionFlow->Fill(selectionFlowStatus);
0754 
0755   // Jet selection
0756   std::vector<reco::PFJet> selectedJets;
0757   std::vector<float> jetPtCorrectedValues;
0758   std::vector<float> jetPNETScoreValues;
0759 
0760   for (auto const& j : *jetHandle) {
0761     // apply or not jecs
0762     float jec = 1;
0763     if (jecHandle.isValid())
0764       jec = jecHandle->correction(j);
0765     auto jet = *(j.clone());
0766     jet.setP4(j.p4() * jec);
0767 
0768     // Basic selection
0769     if (not jetSelection_(jet))
0770       continue;
0771     // SoftDrop mass
0772     if (jetSoftDropMassHandle.isValid()) {
0773       auto const& massSD = (*jetSoftDropMassHandle)[reco::PFJetRef(jetHandle, &j - &(*jetHandle)[0])];
0774       if (massSD < minSoftDropMassCut_ or massSD > maxSoftDropMassCut_)
0775         continue;
0776     }
0777     // cleaning with leptons
0778     float minDR_jm = 1000;
0779     for (size_t imu = 0; imu < vetoMuons.size(); imu++) {
0780       float dR = reco::deltaR(vetoMuons.at(imu).p4(), jet.p4());
0781       if (dR < minDR_jm)
0782         minDR_jm = dR;
0783     }
0784     if (minDR_jm < lepJetDeltaRmin_)
0785       continue;
0786     float minDR_je = 1000;
0787     for (size_t iel = 0; iel < vetoElectrons.size(); iel++) {
0788       float dR = reco::deltaR(vetoElectrons.at(iel).p4(), jet.p4());
0789       if (dR < minDR_je)
0790         minDR_je = dR;
0791     }
0792     if (minDR_je < lepJetDeltaRmin_)
0793       continue;
0794     // Compute HT
0795     if (not jetForHTandBTagHandle.isValid())
0796       hT += jet.pt();
0797     // selected jets, pT values (post-correction), and PNET score
0798     selectedJets.push_back(jet);
0799     jetPtCorrectedValues.push_back(jet.pt());
0800     auto jref = reco::JetBaseRef(reco::PFJetRef(jetHandle, &j - &(*jetHandle)[0]));
0801     if (jetPNETScore[jref])
0802       jetPNETScoreValues.push_back(jetPNETScore[jref]);
0803     else
0804       jetPNETScoreValues.push_back(0);
0805   }
0806   if (njets_ >= 0 and int(selectedJets.size()) < njets_)
0807     return;
0808   selectionFlowStatus++;
0809   selectionFlow->Fill(selectionFlowStatus);
0810 
0811   // count number of jets over minPNETScoreCut
0812   if (std::count_if(jetPNETScoreValues.begin(), jetPNETScoreValues.end(), [&](float score) {
0813         return score > minPNETScoreCut_;
0814       }) < njets_)
0815     return;
0816   selectionFlowStatus++;
0817   selectionFlow->Fill(selectionFlowStatus);
0818 
0819   // sort descending corrected pt order only if jecHandle is valid otherwise keep the current ordering
0820   std::vector<size_t> jetPtSortedIndices(jetPtCorrectedValues.size());
0821   std::iota(jetPtSortedIndices.begin(), jetPtSortedIndices.end(), 0);
0822   if (jecHandle.isValid()) {
0823     std::sort(jetPtSortedIndices.begin(), jetPtSortedIndices.end(), [&](const size_t& i1, const size_t& i2) {
0824       return jetPtCorrectedValues[i1] > jetPtCorrectedValues[i2];
0825     });
0826   }
0827 
0828   // sort descending PNET score order
0829   std::vector<size_t> jetPNETScoreSortedIndices(jetPNETScoreValues.size());
0830   std::iota(jetPNETScoreSortedIndices.begin(), jetPNETScoreSortedIndices.end(), 0);
0831   std::sort(jetPNETScoreSortedIndices.begin(),
0832             jetPNETScoreSortedIndices.end(),
0833             [&](const size_t& i1, const size_t& i2) { return jetPNETScoreValues[i1] > jetPNETScoreValues[i2]; });
0834 
0835   // trigger object matching (for jets)
0836   if (requireHLTOfflineJetMatching_) {
0837     edm::Handle<reco::JetTagCollection> jetPNETScoreHLTHandle;
0838     iEvent.getByToken(jetPNETScoreHLTToken_, jetPNETScoreHLTHandle);
0839     if (!jetPNETScoreHLTHandle.isValid()) {
0840       edm::LogWarning("ParticleNetJetTagMonitor") << "HLT Jet tags collection not valid, will skip event \n";
0841       return;
0842     }
0843 
0844     std::vector<float> jetPNETScoreValuesHLT;
0845     std::vector<reco::JetBaseRef> jetHLTRefs;
0846     for (const auto& jtag : *jetPNETScoreHLTHandle) {
0847       jetPNETScoreValuesHLT.push_back(jtag.second);
0848       jetHLTRefs.push_back(jtag.first);
0849     }
0850 
0851     // sort in PNET score
0852     std::vector<size_t> jetPNETScoreSortedIndicesHLT(jetPNETScoreValuesHLT.size());
0853     std::iota(jetPNETScoreSortedIndicesHLT.begin(), jetPNETScoreSortedIndicesHLT.end(), 0);
0854     std::sort(
0855         jetPNETScoreSortedIndicesHLT.begin(),
0856         jetPNETScoreSortedIndicesHLT.end(),
0857         [&](const size_t& i1, const size_t& i2) { return jetPNETScoreValuesHLT[i1] > jetPNETScoreValuesHLT[i2]; });
0858 
0859     // match reco and hlt objects considering only the first ntrigobjecttomatch jets for both reco and HLT. Each of them must be matched
0860     std::vector<int> matched_obj;
0861     for (size_t jreco = 0; jreco < ntrigobjecttomatch_; jreco++) {
0862       if (jreco >= jetPNETScoreSortedIndices.size())
0863         break;
0864       float minDR = 1000;
0865       int match_index = -1;
0866       for (size_t jhlt = 0; jhlt < ntrigobjecttomatch_; jhlt++) {
0867         if (jhlt >= jetPNETScoreSortedIndicesHLT.size())
0868           break;
0869         if (std::find(matched_obj.begin(), matched_obj.end(), jhlt) != matched_obj.end())
0870           continue;
0871         float dR = reco::deltaR(selectedJets[jetPNETScoreSortedIndices.at(jreco)].p4(),
0872                                 jetHLTRefs.at(jetPNETScoreSortedIndicesHLT.at(jhlt))->p4());
0873         if (dR < hltRecoDeltaRmax_ and dR < minDR) {
0874           match_index = jhlt;
0875           minDR = dR;
0876         }
0877       }
0878       if (match_index >= 0)
0879         matched_obj.push_back(match_index);
0880     }
0881     if (matched_obj.size() != ntrigobjecttomatch_)
0882       return;
0883   }
0884   selectionFlowStatus++;
0885   selectionFlow->Fill(selectionFlowStatus);
0886 
0887   // numerator condition
0888   const bool trg_passed = (numGenericTriggerEvent_->on() and numGenericTriggerEvent_->accept(iEvent, iSetup));
0889   if (trg_passed) {
0890     selectionFlowStatus++;
0891     selectionFlow->Fill(selectionFlowStatus);
0892   }
0893 
0894   // Fill histograms for efficiency
0895   if (njets.numerator != nullptr)
0896     njets.fill(trg_passed, selectedJets.size());
0897   if (nbjets.numerator != nullptr)
0898     nbjets.fill(trg_passed, jetsBTagged.size());
0899   if (ht.numerator != nullptr)
0900     ht.fill(trg_passed, hT);
0901   if (muon_pt.numerator != nullptr)
0902     muon_pt.fill(trg_passed, (!tagMuons.empty()) ? tagMuons.front().pt() : 0);
0903   if (muon_eta.numerator != nullptr)
0904     muon_eta.fill(trg_passed, (!tagMuons.empty()) ? tagMuons.front().eta() : 0);
0905   if (electron_pt.numerator != nullptr)
0906     electron_pt.fill(trg_passed, (!tagElectrons.empty()) ? tagElectrons.front().pt() : -100);
0907   if (electron_eta.numerator != nullptr)
0908     electron_eta.fill(trg_passed, (!tagElectrons.empty()) ? tagElectrons.front().eta() : -100);
0909   if (dilepton_pt.numerator != nullptr)
0910     dilepton_pt.fill(trg_passed, (!emuPairs.empty()) ? emuPairs.front().pt() : 0);
0911   if (dilepton_mass.numerator != nullptr)
0912     dilepton_mass.fill(trg_passed, (!emuPairs.empty()) ? emuPairs.front().mass() : 0);
0913 
0914   if (jet1_pt.numerator != nullptr)
0915     jet1_pt.fill(trg_passed, (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0);
0916   if (jet2_pt.numerator != nullptr)
0917     jet2_pt.fill(trg_passed, (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0);
0918   if (jet1_eta.numerator != nullptr)
0919     jet1_eta.fill(trg_passed, (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).eta() : -100);
0920   if (jet2_eta.numerator != nullptr)
0921     jet2_eta.fill(trg_passed, (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).eta() : -100);
0922   if (jet1_pnetscore.numerator != nullptr)
0923     jet1_pnetscore.fill(trg_passed,
0924                         (!selectedJets.empty()) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) : -1);
0925   if (jet2_pnetscore.numerator != nullptr)
0926     jet2_pnetscore.fill(trg_passed,
0927                         (selectedJets.size() > 1) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1)) : -1);
0928   if (jet1_pnetscore_trans.numerator != nullptr)
0929     jet1_pnetscore_trans.fill(
0930         trg_passed, (!selectedJets.empty()) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0))) : -1);
0931   if (jet2_pnetscore_trans.numerator != nullptr)
0932     jet2_pnetscore_trans.fill(
0933         trg_passed, (selectedJets.size() > 1) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) : -1);
0934   if (mean_2j_pnetscore.numerator != nullptr)
0935     mean_2j_pnetscore.fill(trg_passed,
0936                            (selectedJets.size() > 1) ? (jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
0937                                                         jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
0938                                                            2.
0939                                                      : -1);
0940   if (mean_2j_pnetscore_trans.numerator != nullptr)
0941     mean_2j_pnetscore_trans.fill(trg_passed,
0942                                  (selectedJets.size() > 1)
0943                                      ? atanh((jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
0944                                               jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
0945                                              2.)
0946                                      : -1);
0947 
0948   if (jet1_pt_eta.numerator != nullptr)
0949     jet1_pt_eta.fill(trg_passed,
0950                      (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0951                      (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).eta() : -1);
0952   if (jet2_pt_eta.numerator != nullptr)
0953     jet2_pt_eta.fill(trg_passed,
0954                      (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
0955                      (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).eta() : -1);
0956 
0957   if (jet1_pt_pnetscore1.numerator != nullptr)
0958     jet1_pt_pnetscore1.fill(trg_passed,
0959                             (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0960                             (!selectedJets.empty()) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) : -1);
0961   if (jet2_pt_pnetscore1.numerator != nullptr)
0962     jet2_pt_pnetscore1.fill(trg_passed,
0963                             (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
0964                             (selectedJets.size() > 1) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) : -1);
0965   if (jet1_pt_pnetscore2.numerator != nullptr)
0966     jet1_pt_pnetscore2.fill(trg_passed,
0967                             (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0968                             (selectedJets.size() > 1) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1)) : -1);
0969   if (jet2_pt_pnetscore2.numerator != nullptr)
0970     jet2_pt_pnetscore2.fill(trg_passed,
0971                             (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
0972                             (selectedJets.size() > 1) ? jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1)) : -1);
0973 
0974   if (jet1_pt_pnetscore1_trans.numerator != nullptr)
0975     jet1_pt_pnetscore1_trans.fill(
0976         trg_passed,
0977         (!selectedJets.empty()) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0978         (!selectedJets.empty()) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0))) : -1);
0979   if (jet2_pt_pnetscore1_trans.numerator != nullptr)
0980     jet2_pt_pnetscore1_trans.fill(
0981         trg_passed,
0982         (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
0983         (selectedJets.size() > 1) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0))) : -1);
0984   if (jet1_pt_pnetscore2_trans.numerator != nullptr)
0985     jet1_pt_pnetscore2_trans.fill(
0986         trg_passed,
0987         (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0988         (selectedJets.size() > 1) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) : -1);
0989   if (jet2_pt_pnetscore2_trans.numerator != nullptr)
0990     jet2_pt_pnetscore2_trans.fill(
0991         trg_passed,
0992         (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
0993         (selectedJets.size() > 1) ? atanh(jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) : -1);
0994 
0995   if (jet1_pt_mean2pnetscore.numerator != nullptr)
0996     jet1_pt_mean2pnetscore.fill(trg_passed,
0997                                 (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
0998                                 (selectedJets.size() > 1) ? (jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
0999                                                              jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
1000                                                                 2
1001                                                           : -1);
1002   if (jet2_pt_mean2pnetscore.numerator != nullptr)
1003     jet2_pt_mean2pnetscore.fill(trg_passed,
1004                                 (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
1005                                 (selectedJets.size() > 1)
1006                                     ? atanh((jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
1007                                              jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
1008                                             2)
1009                                     : -1);
1010 
1011   if (jet1_pt_mean2pnetscore_trans.numerator != nullptr)
1012     jet1_pt_mean2pnetscore_trans.fill(trg_passed,
1013                                       (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(0)).pt() : 0,
1014                                       (selectedJets.size() > 1)
1015                                           ? atanh((jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
1016                                                    jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
1017                                                   2)
1018                                           : -1);
1019   if (jet2_pt_mean2pnetscore_trans.numerator != nullptr)
1020     jet2_pt_mean2pnetscore_trans.fill(trg_passed,
1021                                       (selectedJets.size() > 1) ? selectedJets.at(jetPtSortedIndices.at(1)).pt() : 0,
1022                                       (selectedJets.size() > 1)
1023                                           ? atanh((jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(0)) +
1024                                                    jetPNETScoreValues.at(jetPNETScoreSortedIndices.at(1))) /
1025                                                   2)
1026                                           : -1);
1027 }
1028 
1029 void ParticleNetJetTagMonitor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
1030   edm::ParameterSetDescription desc;
1031   desc.add<std::string>("FolderName", "HLT/Higgs");
1032   desc.add<bool>("requireValidHLTPaths", true);
1033   desc.add<bool>("requireHLTOfflineJetMatching", true);
1034   desc.add<edm::InputTag>("vertices", edm::InputTag("offlinePrimaryVertices"));
1035   desc.add<edm::InputTag>("muons", edm::InputTag("muons"));
1036   desc.add<edm::InputTag>("electrons", edm::InputTag("gedGsfElectrons"));
1037   desc.add<edm::InputTag>("tagElectronID",
1038                           edm::InputTag("egmGsfElectronIDsForDQM:cutBasedElectronID-Fall17-94X-V2-tight"));
1039   desc.add<edm::InputTag>("vetoElectronID",
1040                           edm::InputTag("egmGsfElectronIDsForDQM:cutBasedElectronID-Fall17-94X-V2-loose"));
1041   desc.add<edm::InputTag>("jets", edm::InputTag("ak4PFJetsCHS"));
1042   desc.add<edm::InputTag>("jetPNETScore", edm::InputTag("pfParticleNetAK4DiscriminatorsJetTags", "BvsAll"));
1043   desc.add<edm::InputTag>("jetPNETScoreHLT", edm::InputTag("hltParticleNetDiscriminatorsJetTags", "BvsAll"));
1044   desc.add<edm::InputTag>("jetsForHTandBTag", edm::InputTag(""));
1045   desc.add<edm::InputTag>("jetPNETScoreForHTandBTag", edm::InputTag(""));
1046   desc.add<edm::InputTag>("jetSoftDropMass", edm::InputTag(""));
1047   desc.add<edm::InputTag>("met", edm::InputTag("pfMetPuppi"));
1048   desc.add<edm::InputTag>("jecForMC", edm::InputTag("ak4PFCHSL1FastL2L3Corrector"));
1049   desc.add<edm::InputTag>("jecForData", edm::InputTag("ak4PFCHSL1FastL2L3ResidualCorrector"));
1050   desc.add<std::string>("tagMuonSelection",
1051                         "pt > 25 && abs(eta) < 2.4 && passed(CutBasedIdTight) && passed(PFIsoTight)");
1052   desc.add<std::string>("tagElectronSelection", "pt > 20 && abs(eta) < 2.5");
1053   desc.add<std::string>("vetoMuonSelection",
1054                         "pt > 10 && abs(eta) < 2.4 && passed(CutBasedIdLoose) && passed(PFIsoLoose)");
1055   desc.add<std::string>("vetoElectronSelection", "pt > 10 && abs(eta) < 2.5");
1056   desc.add<std::string>("jetSelection", "pt > 30 && abs(eta) < 2.5");
1057   desc.add<std::string>("jetSelectionForHTandBTag", "pt > 30 && abs(eta) < 2.5");
1058   desc.add<std::string>("vertexSelection", "!isFake && ndof > 4 && abs(z) <= 24 && position.Rho <= 2");
1059   desc.add<std::string>("dileptonSelection", "((mass > 20 && mass < 75) || mass > 105) && charge == 0");
1060   desc.add<std::string>("metSelection", "pt > 0");
1061   desc.add<int>("ntagleptons", 2);
1062   desc.add<int>("ntagmuons", 1);
1063   desc.add<int>("ntagelectrons", 1);
1064   desc.add<int>("nvetoleptons", 2);
1065   desc.add<int>("nvetomuons", 1);
1066   desc.add<int>("nvetoelectrons", 1);
1067   desc.add<int>("nemupairs", 1);
1068   desc.add<int>("njets", 2);
1069   desc.add<int>("nbjets", -1);
1070   desc.add<unsigned int>("ntrigobjecttomatch", 2);
1071   desc.add<double>("lepJetDeltaRmin", 0.4);
1072   desc.add<double>("lepJetDeltaRminForHTandBTag", 0.4);
1073   desc.add<double>("hltRecoDeltaRmax", 0.4);
1074   desc.add<double>("maxLeptonDxyCut", 0.1);
1075   desc.add<double>("maxLeptonDzCut", 0.2);
1076   desc.add<double>("minPNETScoreCut", 0.2);
1077   desc.add<double>("minPNETBTagCut", 0.5);
1078   desc.add<double>("minSoftDropMassCut", 50);
1079   desc.add<double>("maxSoftDropMassCut", 110);
1080   desc.add<std::vector<double>>("leptonPtBinning", {});
1081   desc.add<std::vector<double>>("leptonEtaBinning", {});
1082   desc.add<std::vector<double>>("diLeptonPtBinning", {});
1083   desc.add<std::vector<double>>("diLeptonMassBinning", {});
1084   desc.add<std::vector<double>>("HTBinning", {});
1085   desc.add<std::vector<double>>("NjetBinning", {});
1086   desc.add<std::vector<double>>("jet1PtBinning", {});
1087   desc.add<std::vector<double>>("jet2PtBinning", {});
1088   desc.add<std::vector<double>>("jet1EtaBinning", {});
1089   desc.add<std::vector<double>>("jet2EtaBinning", {});
1090   desc.add<std::vector<double>>("jet1PNETscoreBinning", {});
1091   desc.add<std::vector<double>>("jet2PNETscoreBinning", {});
1092   desc.add<std::vector<double>>("jet1PNETscoreTransBinning", {});
1093   desc.add<std::vector<double>>("jet2PNETscoreTransBinning", {});
1094   desc.add<std::vector<double>>("jet1PtBinning2d", {});
1095   desc.add<std::vector<double>>("jet2PtBinning2d", {});
1096   desc.add<std::vector<double>>("jet1EtaBinning2d", {});
1097   desc.add<std::vector<double>>("jet2EtaBinning2d", {});
1098   desc.add<std::vector<double>>("jet1PNETscoreBinning2d", {});
1099   desc.add<std::vector<double>>("jet2PNETscoreBinning2d", {});
1100   desc.add<std::vector<double>>("jet1PNETscoreTransBinning2d", {});
1101   desc.add<std::vector<double>>("jet2PNETscoreTransBinning2d", {});
1102   edm::ParameterSetDescription genericTriggerEventPSet;
1103   GenericTriggerEventFlag::fillPSetDescription(genericTriggerEventPSet);
1104   desc.add<edm::ParameterSetDescription>("numGenericTriggerEvent", genericTriggerEventPSet);
1105   desc.add<edm::ParameterSetDescription>("denGenericTriggerEvent", genericTriggerEventPSet);
1106   descriptions.add("ParticleNetJetTagMonitor", desc);
1107 }
1108 
1109 // Define this as a plug-in
1110 DEFINE_FWK_MODULE(ParticleNetJetTagMonitor);