Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-28 03:54:27

0001 #include "DQM/Physics/src/TopSingleLeptonDQM_miniAOD.h"
0002 #include "DataFormats/BTauReco/interface/JetTag.h"
0003 #include "DataFormats/JetReco/interface/CaloJet.h"
0004 #include "DataFormats/JetReco/interface/PFJet.h"
0005 #include "DataFormats/Math/interface/deltaR.h"
0006 #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
0007 #include <iostream>
0008 #include <memory>
0009 
0010 #include "FWCore/Framework/interface/ConsumesCollector.h"
0011 #include "FWCore/Framework/interface/EDConsumerBase.h"
0012 #include "FWCore/Utilities/interface/EDGetToken.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/Muon.h"
0015 #include "DataFormats/PatCandidates/interface/Electron.h"
0016 #include "DataFormats/PatCandidates/interface/Jet.h"
0017 #include "DataFormats/PatCandidates/interface/MET.h"
0018 
0019 using namespace std;
0020 
0021 namespace TopSingleLepton_miniAOD {
0022 
0023   // maximal number of leading jets
0024   // to be used for top mass estimate
0025   static const unsigned int MAXJETS = 4;
0026   // nominal mass of the W boson to
0027   // be used for the top mass estimate
0028   static const double WMASS = 80.4;
0029 
0030   MonitorEnsemble::MonitorEnsemble(const char* label, const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0031       : label_(label),
0032         elecIso_(nullptr),
0033         elecSelect_(nullptr),
0034         pvSelect_(nullptr),
0035         muonIso_(nullptr),
0036         muonSelect_(nullptr),
0037         jetIDSelect_(nullptr),
0038         jetSelect(nullptr),
0039         includeBTag_(false),
0040         lowerEdge_(-1.),
0041         upperEdge_(-1.),
0042         logged_(0) {
0043     // sources have to be given; this PSet is not optional
0044     edm::ParameterSet sources = cfg.getParameter<edm::ParameterSet>("sources");
0045     // muons_ = iC.consumes<edm::View<reco::PFCandidate> >(
0046     //     sources.getParameter<edm::InputTag>("muons"));
0047 
0048     muons_ = iC.consumes<edm::View<pat::Muon>>(sources.getParameter<edm::InputTag>("muons"));
0049 
0050     elecs_ = iC.consumes<edm::View<pat::Electron>>(sources.getParameter<edm::InputTag>("elecs"));
0051     pvs_ = iC.consumes<edm::View<reco::Vertex>>(sources.getParameter<edm::InputTag>("pvs"));
0052     jets_ = iC.consumes<edm::View<pat::Jet>>(sources.getParameter<edm::InputTag>("jets"));
0053     for (edm::InputTag const& tag : sources.getParameter<std::vector<edm::InputTag>>("mets"))
0054       mets_.push_back(iC.consumes<edm::View<pat::MET>>(tag));
0055     // electronExtras are optional; they may be omitted or
0056     // empty
0057     if (cfg.existsAs<edm::ParameterSet>("elecExtras")) {
0058       edm::ParameterSet elecExtras = cfg.getParameter<edm::ParameterSet>("elecExtras");
0059       // select is optional; in case it's not found no
0060       // selection will be applied
0061       if (elecExtras.existsAs<std::string>("select")) {
0062         elecSelect_ =
0063             std::make_unique<StringCutObjectSelector<pat::Electron>>(elecExtras.getParameter<std::string>("select"));
0064       }
0065       // isolation is optional; in case it's not found no
0066       // isolation will be applied
0067       if (elecExtras.existsAs<std::string>("isolation")) {
0068         elecIso_ =
0069             std::make_unique<StringCutObjectSelector<pat::Electron>>(elecExtras.getParameter<std::string>("isolation"));
0070       }
0071 
0072       if (elecExtras.existsAs<std::string>("rho")) {
0073         rhoTag = elecExtras.getParameter<edm::InputTag>("rho");
0074       }
0075       // electronId is optional; in case it's not found the
0076       // InputTag will remain empty
0077       if (elecExtras.existsAs<edm::ParameterSet>("electronId")) {
0078         edm::ParameterSet elecId = elecExtras.getParameter<edm::ParameterSet>("electronId");
0079         electronId_ = iC.consumes<edm::ValueMap<float>>(elecId.getParameter<edm::InputTag>("src"));
0080         eidCutValue_ = elecId.getParameter<double>("cutValue");
0081       }
0082     }
0083     // pvExtras are opetional; they may be omitted or empty
0084     if (cfg.existsAs<edm::ParameterSet>("pvExtras")) {
0085       edm::ParameterSet pvExtras = cfg.getParameter<edm::ParameterSet>("pvExtras");
0086       // select is optional; in case it's not found no
0087       // selection will be applied
0088       if (pvExtras.existsAs<std::string>("select")) {
0089         pvSelect_ =
0090             std::make_unique<StringCutObjectSelector<reco::Vertex>>(pvExtras.getParameter<std::string>("select"));
0091       }
0092     }
0093     // muonExtras are optional; they may be omitted or empty
0094     if (cfg.existsAs<edm::ParameterSet>("muonExtras")) {
0095       edm::ParameterSet muonExtras = cfg.getParameter<edm::ParameterSet>("muonExtras");
0096       // select is optional; in case it's not found no
0097       // selection will be applied
0098       if (muonExtras.existsAs<std::string>("select")) {
0099         muonSelect_ =
0100             std::make_unique<StringCutObjectSelector<pat::Muon>>(muonExtras.getParameter<std::string>("select"));
0101       }
0102       // isolation is optional; in case it's not found no
0103       // isolation will be applied
0104       if (muonExtras.existsAs<std::string>("isolation")) {
0105         muonIso_ =
0106             std::make_unique<StringCutObjectSelector<pat::Muon>>(muonExtras.getParameter<std::string>("isolation"));
0107       }
0108     }
0109 
0110     // jetExtras are optional; they may be omitted or
0111     // empty
0112     if (cfg.existsAs<edm::ParameterSet>("jetExtras")) {
0113       edm::ParameterSet jetExtras = cfg.getParameter<edm::ParameterSet>("jetExtras");
0114       // read jetID information if it exists
0115       if (jetExtras.existsAs<edm::ParameterSet>("jetID")) {
0116         edm::ParameterSet jetID = jetExtras.getParameter<edm::ParameterSet>("jetID");
0117         jetIDLabel_ = iC.consumes<reco::JetIDValueMap>(jetID.getParameter<edm::InputTag>("label"));
0118         jetIDSelect_ =
0119             std::make_unique<StringCutObjectSelector<reco::JetID>>(jetID.getParameter<std::string>("select"));
0120       }
0121       // select is optional; in case it's not found no
0122       // selection will be applied (only implemented for
0123       // CaloJets at the moment)
0124       if (jetExtras.existsAs<std::string>("select")) {
0125         jetSelect_ = jetExtras.getParameter<std::string>("select");
0126         jetSelect = std::make_unique<StringCutObjectSelector<pat::Jet>>(jetSelect_);
0127       }
0128     }
0129 
0130     // triggerExtras are optional; they may be omitted or empty
0131     if (cfg.existsAs<edm::ParameterSet>("triggerExtras")) {
0132       edm::ParameterSet triggerExtras = cfg.getParameter<edm::ParameterSet>("triggerExtras");
0133       triggerTable_ = iC.consumes<edm::TriggerResults>(triggerExtras.getParameter<edm::InputTag>("src"));
0134       triggerPaths_ = triggerExtras.getParameter<std::vector<std::string>>("paths");
0135     }
0136 
0137     // massExtras is optional; in case it's not found no mass
0138     // window cuts are applied for the same flavor monitor
0139     // histograms
0140     if (cfg.existsAs<edm::ParameterSet>("massExtras")) {
0141       edm::ParameterSet massExtras = cfg.getParameter<edm::ParameterSet>("massExtras");
0142       lowerEdge_ = massExtras.getParameter<double>("lowerEdge");
0143       upperEdge_ = massExtras.getParameter<double>("upperEdge");
0144     }
0145 
0146     // setup the verbosity level for booking histograms;
0147     // per default the verbosity level will be set to
0148     // STANDARD. This will also be the chosen level in
0149     // the case when the monitoring PSet is not found
0150     verbosity_ = STANDARD;
0151     if (cfg.existsAs<edm::ParameterSet>("monitoring")) {
0152       edm::ParameterSet monitoring = cfg.getParameter<edm::ParameterSet>("monitoring");
0153       if (monitoring.getParameter<std::string>("verbosity") == "DEBUG")
0154         verbosity_ = DEBUG;
0155       if (monitoring.getParameter<std::string>("verbosity") == "VERBOSE")
0156         verbosity_ = VERBOSE;
0157       if (monitoring.getParameter<std::string>("verbosity") == "STANDARD")
0158         verbosity_ = STANDARD;
0159     }
0160     // and don't forget to do the histogram booking
0161     directory_ = cfg.getParameter<std::string>("directory");
0162     // book(ibooker);
0163   }
0164 
0165   void MonitorEnsemble::book(DQMStore::IBooker& ibooker) {
0166     // set up the current directory path
0167     std::string current(directory_);
0168     current += label_;
0169     ibooker.setCurrentFolder(current);
0170 
0171     // determine number of bins for trigger monitoring
0172     //unsigned int nPaths = triggerPaths_.size();
0173 
0174     // --- [STANDARD] --- //
0175     // Run Number
0176     //hists_["RunNumb_"] = ibooker.book1D("RunNumber", "Run Nr.", 1.e4, 1.5e5, 3.e5);
0177     // instantaneous luminosity
0178     //hists_["InstLumi_"] = ibooker.book1D("InstLumi", "Inst. Lumi.", 100, 0., 1.e3);
0179     // number of selected primary vertices
0180     hists_["pvMult_"] = ibooker.book1D("PvMult", "N_{good pvs}", 50, 0., 50.);
0181     // pt of the leading muon
0182     hists_["muonPt_"] = ibooker.book1D("MuonPt", "pt(#mu TightId, TightIso)", 40, 0., 200.);
0183     // muon multiplicity before std isolation
0184     hists_["muonMult_"] = ibooker.book1D("MuonMult", "N_{loose}(#mu)", 10, 0., 10.);
0185     // muon multiplicity after  std isolation
0186     //hists_["muonMultIso_"] = ibooker.book1D("MuonMultIso",
0187     //    "N_{TightIso}(#mu)", 10, 0., 10.);
0188 
0189     hists_["muonMultTight_"] = ibooker.book1D("MuonMultTight", "N_{TightIso,TightId}(#mu)", 10, 0., 10.);
0190 
0191     // pt of the leading electron
0192     hists_["elecPt_"] = ibooker.book1D("ElecPt", "pt(e TightId, TightIso)", 40, 0., 200.);
0193     // electron multiplicity before std isolation
0194     //hists_["elecMult_"] = ibooker.book1D("ElecMult", "N_{looseId}(e)", 10, 0., 10.);
0195     // electron multiplicity after  std isolation
0196     //hists_["elecMultIso_"] = ibooker.book1D("ElecMultIso", "N_{Iso}(e)", 10, 0., 10.);
0197     // multiplicity of jets with pt>20 (corrected to L2+L3)
0198     hists_["jetMult_"] = ibooker.book1D("JetMult", "N_{30}(jet)", 10, 0., 10.);
0199     hists_["jetLooseMult_"] = ibooker.book1D("JetLooseMult", "N_{30,loose}(jet)", 10, 0., 10.);
0200 
0201     // trigger efficiency estimates for single lepton triggers
0202     //hists_["triggerEff_"] = ibooker.book1D("TriggerEff",
0203     //    "Eff(trigger)", nPaths, 0., nPaths);
0204     // monitored trigger occupancy for single lepton triggers
0205     //hists_["triggerMon_"] = ibooker.book1D("TriggerMon",
0206     //    "Mon(trigger)", nPaths, 0., nPaths);
0207     // MET (calo)
0208     hists_["slimmedMETs_"] = ibooker.book1D("slimmedMETs", "MET_{slimmed}", 40, 0., 200.);
0209     // W mass estimate
0210     hists_["massW_"] = ibooker.book1D("MassW", "M(W)", 60, 0., 300.);
0211     // Top mass estimate
0212     hists_["massTop_"] = ibooker.book1D("MassTop", "M(Top)", 50, 0., 500.);
0213     // b-tagged Top mass
0214     hists_["massBTop_"] = ibooker.book1D("MassBTop", "M(Top, 1 b-tag)", 50, 0., 500.);
0215     // set bin labels for trigger monitoring
0216     triggerBinLabels(std::string("trigger"), triggerPaths_);
0217 
0218     if (verbosity_ == STANDARD)
0219       return;
0220 
0221     // --- [VERBOSE] --- //
0222     // eta of the leading muon
0223     hists_["muonEta_"] = ibooker.book1D("MuonEta", "#eta(#mu TightId,TightIso)", 30, -3., 3.);
0224     // relative isolation of the candidate muon (depending on the decay channel)
0225     hists_["muonPhi_"] = ibooker.book1D("MuonPhi", "#phi(#mu TightId,TightIso)", 40, -4., 4.);
0226     hists_["muonRelIso_"] = ibooker.book1D("MuonRelIso", "Iso_{Rel}(#mu TightId) (#Delta#beta Corrected)", 50, 0., 1.);
0227 
0228     // eta of the leading electron
0229     hists_["elecEta_"] = ibooker.book1D("ElecEta", "#eta(e TightId, TightIso)", 30, -3., 3.);
0230     hists_["elecPhi_"] = ibooker.book1D("ElecPhi", "#phi(e TightId, TightIso)", 40, -4., 4.);
0231     // std isolation variable of the leading electron
0232     hists_["elecRelIso_"] = ibooker.book1D("ElecRelIso", "Iso_{Rel}(e TightId)", 50, 0., 1.);
0233 
0234     hists_["elecMultTight_"] = ibooker.book1D("ElecMultTight", "N_{TightIso,TightId}(e)", 10, 0., 10.);
0235 
0236     // multiplicity of btagged jets (for track counting high efficiency) with
0237     // pt(L2L3)>30
0238     //hists_["jetMultBEff_"] = ibooker.book1D("JetMultBEff",
0239     //    "N_{30}(TCHE)", 10, 0., 10.);
0240     // btag discriminator for track counting high efficiency for jets with
0241     // pt(L2L3)>30
0242     //hists_["jetBDiscEff_"] = ibooker.book1D("JetBDiscEff",
0243     //    "Disc_{TCHE}(jet)", 100, 0., 10.);
0244     // eta of the 1. leading jet (corrected to L2+L3)
0245     hists_["jet1Eta_"] = ibooker.book1D("Jet1Eta", "#eta_{30,loose}(jet1)", 60, -3., 3.);
0246     // pt of the 1. leading jet (corrected to L2+L3)
0247     hists_["jet1Pt_"] = ibooker.book1D("Jet1Pt", "pt_{30,loose}(jet1)", 60, 0., 300.);
0248     // eta of the 2. leading jet (corrected to L2+L3)
0249     hists_["jet2Eta_"] = ibooker.book1D("Jet2Eta", "#eta_{30,loose}(jet2)", 60, -3., 3.);
0250     // pt of the 2. leading jet (corrected to L2+L3)
0251     hists_["jet2Pt_"] = ibooker.book1D("Jet2Pt", "pt_{30,loose}(jet2)", 60, 0., 300.);
0252     // eta of the 3. leading jet (corrected to L2+L3)
0253     hists_["jet3Eta_"] = ibooker.book1D("Jet3Eta", "#eta_{30,loose}(jet3)", 60, -3., 3.);
0254     // pt of the 3. leading jet (corrected to L2+L3)
0255     hists_["jet3Pt_"] = ibooker.book1D("Jet3Pt", "pt_{30,loose}(jet3)", 60, 0., 300.);
0256     // eta of the 4. leading jet (corrected to L2+L3)
0257     hists_["jet4Eta_"] = ibooker.book1D("Jet4Eta", "#eta_{30,loose}(jet4)", 60, -3., 3.);
0258     // pt of the 4. leading jet (corrected to L2+L3)
0259     hists_["jet4Pt_"] = ibooker.book1D("Jet4Pt", "pt_{30,loose}(jet4)", 60, 0., 300.);
0260     // MET (tc)
0261     // MET (pflow)
0262     hists_["slimmedMETsPuppi_"] = ibooker.book1D("slimmedMETsPuppi", "MET_{slimmedPuppi}", 40, 0., 200.);
0263     // dz for muons (to suppress cosmis)
0264     hists_["muonDelZ_"] = ibooker.book1D("MuonDelZ", "d_{z}(#mu)", 50, -25., 25.);
0265     // dxy for muons (to suppress cosmics)
0266     hists_["muonDelXY_"] = ibooker.book2D("MuonDelXY", "d_{xy}(#mu)", 50, -0.1, 0.1, 50, -0.1, 0.1);
0267 
0268     // set axes titles for dxy for muons
0269     hists_["muonDelXY_"]->setAxisTitle("x [cm]", 1);
0270     hists_["muonDelXY_"]->setAxisTitle("y [cm]", 2);
0271 
0272     if (verbosity_ == VERBOSE)
0273       return;
0274 
0275     // --- [DEBUG] --- //
0276     // charged hadron isolation component of the candidate muon (depending on the
0277     // decay channel)
0278     hists_["muonChHadIso_"] = ibooker.book1D("MuonChHadIsoComp", "ChHad_{IsoComponent}(#mu TightId)", 50, 0., 5.);
0279     // neutral hadron isolation component of the candidate muon (depending on the
0280     // decay channel)
0281     hists_["muonNeHadIso_"] = ibooker.book1D("MuonNeHadIsoComp", "NeHad_{IsoComponent}(#mu TightId)", 50, 0., 5.);
0282     // photon isolation component of the candidate muon (depending on the decay
0283     // channel)
0284     hists_["muonPhIso_"] = ibooker.book1D("MuonPhIsoComp", "Photon_{IsoComponent}(#mu TightId)", 50, 0., 5.);
0285     // charged hadron isolation component of the candidate electron (depending on
0286     // the decay channel)
0287     hists_["elecChHadIso_"] = ibooker.book1D("ElectronChHadIsoComp", "ChHad_{IsoComponent}(e TightId)", 50, 0., 5.);
0288     // neutral hadron isolation component of the candidate electron (depending on
0289     // the decay channel)
0290     hists_["elecNeHadIso_"] = ibooker.book1D("ElectronNeHadIsoComp", "NeHad_{IsoComponent}(e TightId)", 50, 0., 5.);
0291     // photon isolation component of the candidate electron (depending on the
0292     // decay channel)
0293     hists_["elecPhIso_"] = ibooker.book1D("ElectronPhIsoComp", "Photon_{IsoComponent}(e TightId)", 50, 0., 5.);
0294     // multiplicity of btagged jets (for track counting high purity) with
0295     // pt(L2L3)>30
0296     //hists_["jetMultBPur_"] = ibooker.book1D("JetMultBPur",
0297     //    "N_{30}(TCHP)", 10, 0., 10.);
0298     // btag discriminator for track counting high purity
0299     //hists_["jetBDiscPur_"] = ibooker.book1D("JetBDiscPur",
0300     //    "Disc_{TCHP}(Jet)", 100, 0., 10.);
0301     // multiplicity of btagged jets (for simple secondary vertex) with pt(L2L3)>30
0302     //hists_["jetMultBVtx_"] = ibooker.book1D("JetMultBVtx",
0303     //    "N_{30}(SSVHE)", 10, 0., 10.);
0304     // btag discriminator for simple secondary vertex
0305     //hists_["jetBDiscVtx_"] = ibooker.book1D("JetBDiscVtx",
0306     //    "Disc_{SSVHE}(Jet)", 35, -1., 6.);
0307     // multiplicity for combined secondary vertex
0308     hists_["jetMultBPNetM_"] = ibooker.book1D("JetMultBPNetM", "N_{30}(PNetM)", 10, 0., 10.);
0309     // btag discriminator for combined secondary vertex
0310     hists_["jetBPNet_"] = ibooker.book1D("JetDiscPNet", "BJet Disc_{PNet}(JET)", 100, -1., 2.);
0311     // pt of the 1. leading jet (uncorrected)
0312     //hists_["jet1PtRaw_"] = ibooker.book1D("Jet1PtRaw", "pt_{Raw}(jet1)", 60, 0., 300.);
0313     // pt of the 2. leading jet (uncorrected)
0314     //hists_["jet2PtRaw_"] = ibooker.book1D("Jet2PtRaw", "pt_{Raw}(jet2)", 60, 0., 300.);
0315     // pt of the 3. leading jet (uncorrected)
0316     //hists_["jet3PtRaw_"] = ibooker.book1D("Jet3PtRaw", "pt_{Raw}(jet3)", 60, 0., 300.);
0317     // pt of the 4. leading jet (uncorrected)
0318     //hists_["jet4PtRaw_"] = ibooker.book1D("Jet4PtRaw", "pt_{Raw}(jet4)", 60, 0., 300.);
0319     // selected events
0320     hists_["eventLogger_"] = ibooker.book2D("EventLogger", "Logged Events", 9, 0., 9., 10, 0., 10.);
0321 
0322     // set axes titles for selected events
0323     hists_["eventLogger_"]->getTH1()->SetOption("TEXT");
0324     hists_["eventLogger_"]->setBinLabel(1, "Run", 1);
0325     hists_["eventLogger_"]->setBinLabel(2, "Block", 1);
0326     hists_["eventLogger_"]->setBinLabel(3, "Event", 1);
0327     hists_["eventLogger_"]->setBinLabel(4, "pt_{L2L3}(jet1)", 1);
0328     hists_["eventLogger_"]->setBinLabel(5, "pt_{L2L3}(jet2)", 1);
0329     hists_["eventLogger_"]->setBinLabel(6, "pt_{L2L3}(jet3)", 1);
0330     hists_["eventLogger_"]->setBinLabel(7, "pt_{L2L3}(jet4)", 1);
0331     hists_["eventLogger_"]->setBinLabel(8, "M_{W}", 1);
0332     hists_["eventLogger_"]->setBinLabel(9, "M_{Top}", 1);
0333     hists_["eventLogger_"]->setAxisTitle("logged evts", 2);
0334     return;
0335   }
0336 
0337   void MonitorEnsemble::fill(const edm::Event& event, const edm::EventSetup& setup) {
0338     // fetch trigger event if configured such
0339     edm::Handle<edm::TriggerResults> triggerTable;
0340 
0341     if (!triggerTable_.isUninitialized()) {
0342       if (!event.getByToken(triggerTable_, triggerTable))
0343         return;
0344     }
0345 
0346     /*
0347   ------------------------------------------------------------
0348 
0349   Primary Vertex Monitoring
0350 
0351   ------------------------------------------------------------
0352   */
0353     // fill monitoring plots for primary verices
0354     edm::Handle<edm::View<reco::Vertex>> pvs;
0355     if (!event.getByToken(pvs_, pvs))
0356       return;
0357     const reco::Vertex& pver = pvs->front();
0358 
0359     unsigned int pvMult = 0;
0360     if (pvs.isValid()) {
0361       for (edm::View<reco::Vertex>::const_iterator pv = pvs->begin(); pv != pvs->end(); ++pv) {
0362         bool isGood =
0363             (!(pv->isFake()) && (pv->ndof() > 4.0) && (abs(pv->z()) < 24.0) && (abs(pv->position().Rho()) < 2.0));
0364         if (!isGood)
0365           continue;
0366         pvMult++;
0367       }
0368       //std::cout<<" npv  "<<testn<<endl;
0369     }
0370 
0371     fill("pvMult_", pvMult);
0372 
0373     /*
0374   ------------------------------------------------------------
0375 
0376   Run and Inst. Luminosity information (Inst. Lumi. filled now with a dummy
0377   value=5.0)
0378 
0379   ------------------------------------------------------------
0380   */
0381 
0382     //if (!event.eventAuxiliary().run()) return;
0383 
0384     //fill("RunNumb_", event.eventAuxiliary().run());
0385 
0386     //double dummy = 5.;
0387     //fill("InstLumi_", dummy);
0388 
0389     /*
0390   ------------------------------------------------------------
0391 
0392   Electron Monitoring
0393 
0394   ------------------------------------------------------------
0395   */
0396 
0397     // fill monitoring plots for electrons
0398     edm::Handle<edm::View<pat::Electron>> elecs;
0399     if (!event.getByToken(elecs_, elecs))
0400       return;
0401 
0402     edm::Handle<double> _rhoHandle;
0403     event.getByLabel(rhoTag, _rhoHandle);
0404     //if (!event.getByToken(elecs_, elecs)) return;
0405 
0406     // check availability of electron id
0407     edm::Handle<edm::ValueMap<float>> electronId;
0408     if (!electronId_.isUninitialized()) {
0409       if (!event.getByToken(electronId_, electronId))
0410         return;
0411     }
0412 
0413     // loop electron collection
0414     unsigned int eMultIso = 0, eMult = 0;
0415     std::vector<const pat::Electron*> isoElecs;
0416 
0417     for (edm::View<pat::Electron>::const_iterator elec = elecs->begin(); elec != elecs->end(); ++elec) {
0418       if (true) {  //loose id
0419         if (!elecSelect_ || (*elecSelect_)(*elec)) {
0420           double el_ChHadIso = elec->pfIsolationVariables().sumChargedHadronPt;
0421           double el_NeHadIso = elec->pfIsolationVariables().sumNeutralHadronEt;
0422           double el_PhIso = elec->pfIsolationVariables().sumPhotonEt;
0423 
0424           double rho = _rhoHandle.isValid() ? (float)(*_rhoHandle) : 0;
0425           double absEta = abs(elec->superCluster()->eta());
0426           double eA = 0;
0427           if (absEta < 1.000)
0428             eA = 0.1703;
0429           else if (absEta < 1.479)
0430             eA = 0.1715;
0431           else if (absEta < 2.000)
0432             eA = 0.1213;
0433           else if (absEta < 2.200)
0434             eA = 0.1230;
0435           else if (absEta < 2.300)
0436             eA = 0.1635;
0437           else if (absEta < 2.400)
0438             eA = 0.1937;
0439           else if (absEta < 5.000)
0440             eA = 0.2393;
0441 
0442           double el_pfRelIso = (el_ChHadIso + max(0., el_NeHadIso + el_PhIso - rho * eA)) / elec->pt();
0443 
0444           ++eMult;
0445 
0446           if (eMult == 1) {
0447             fill("elecRelIso_", el_pfRelIso);
0448             fill("elecChHadIso_", el_ChHadIso);
0449             fill("elecNeHadIso_", el_NeHadIso);
0450             fill("elecPhIso_", el_PhIso);
0451           }
0452           //loose Iso
0453           //if(!((el_pfRelIso<0.0994 && absEta<1.479)||(el_pfRelIso<0.107 && absEta>1.479)))continue;
0454 
0455           //tight Iso
0456           if (!((el_pfRelIso < 0.0588 && absEta < 1.479) || (el_pfRelIso < 0.0571 && absEta > 1.479)))
0457             continue;
0458           ++eMultIso;
0459 
0460           if (eMultIso == 1) {
0461             // restrict to the leading electron
0462             fill("elecPt_", elec->pt());
0463             fill("elecEta_", elec->eta());
0464             fill("elecPhi_", elec->phi());
0465           }
0466         }
0467       }
0468     }
0469     //fill("elecMult_", eMult);
0470     fill("elecMultTight_", eMultIso);
0471 
0472     /*
0473   ------------------------------------------------------------
0474 
0475   Muon Monitoring
0476 
0477   ------------------------------------------------------------
0478   */
0479 
0480     // fill monitoring plots for muons
0481     unsigned int mMult = 0, mTight = 0, mTightId = 0;
0482 
0483     edm::Handle<edm::View<pat::Muon>> muons;
0484     edm::View<pat::Muon>::const_iterator muonit;
0485 
0486     if (!event.getByToken(muons_, muons))
0487       return;
0488 
0489     for (edm::View<pat::Muon>::const_iterator muon = muons->begin(); muon != muons->end(); ++muon) {
0490       // restrict to globalMuons
0491       if (muon->isGlobalMuon()) {
0492         fill("muonDelZ_", muon->innerTrack()->vz());  // CB using inner track!
0493         fill("muonDelXY_", muon->innerTrack()->vx(), muon->innerTrack()->vy());
0494 
0495         // apply preselection loose muon
0496         if (!muonSelect_ || (*muonSelect_)(*muon)) {
0497           //loose muon count
0498           ++mMult;
0499 
0500           double chHadPt = muon->pfIsolationR04().sumChargedHadronPt;
0501           double neHadEt = muon->pfIsolationR04().sumNeutralHadronEt;
0502           double phoEt = muon->pfIsolationR04().sumPhotonEt;
0503 
0504           double pfRelIso = (chHadPt + max(0., neHadEt + phoEt - 0.5 * muon->pfIsolationR04().sumPUPt)) /
0505                             muon->pt();  // CB dBeta corrected iso!
0506 
0507           if (!(muon->isGlobalMuon() && muon->isPFMuon() && muon->globalTrack()->normalizedChi2() < 10. &&
0508                 muon->globalTrack()->hitPattern().numberOfValidMuonHits() > 0 && muon->numberOfMatchedStations() > 1 &&
0509                 fabs(muon->muonBestTrack()->dxy(pver.position())) < 0.2 &&
0510                 fabs(muon->muonBestTrack()->dz(pver.position())) < 0.5 &&
0511                 muon->innerTrack()->hitPattern().numberOfValidPixelHits() > 0 &&
0512                 muon->innerTrack()->hitPattern().trackerLayersWithMeasurement() > 5))
0513             continue;
0514 
0515           if (mTightId == 0) {
0516             // restrict to leading muon
0517             fill("muonRelIso_", pfRelIso);
0518             fill("muonChHadIso_", chHadPt);
0519             fill("muonNeHadIso_", neHadEt);
0520             fill("muonPhIso_", phoEt);
0521             //fill("muonRelIso_", pfRelIso);
0522           }
0523 
0524           if (!(pfRelIso < 0.15))
0525             continue;
0526           //tight id
0527           if (mTight == 0) {
0528             // restrict to leading muon
0529 
0530             fill("muonPt_", muon->pt());
0531             fill("muonEta_", muon->eta());
0532             fill("muonPhi_", muon->phi());
0533           }
0534           mTight++;
0535           mTightId++;
0536         }
0537       }
0538     }
0539     fill("muonMult_", mMult);        //loose
0540     fill("muonMultTight_", mTight);  //tight id & iso
0541 
0542     /*
0543   ------------------------------------------------------------
0544 
0545   Jet Monitoring
0546 
0547   ------------------------------------------------------------
0548   */
0549 
0550     // loop jet collection
0551     std::vector<pat::Jet> correctedJets;
0552     std::vector<double> JetTagValues;
0553     unsigned int mult = 0, loosemult = 0, multBPNetM = 0;
0554 
0555     edm::Handle<edm::View<pat::Jet>> jets;
0556     if (!event.getByToken(jets_, jets)) {
0557       return;
0558     }
0559 
0560     for (edm::View<pat::Jet>::const_iterator jet = jets->begin(); jet != jets->end(); ++jet) {
0561       // check jetID for calo jets
0562       //unsigned int idx = jet - jets->begin();
0563 
0564       const pat::Jet& sel = *jet;
0565 
0566       if (!(*jetSelect)(sel))
0567         continue;
0568       //      if (!jetSelect(sel)) continue;
0569 
0570       // prepare jet to fill monitor histograms
0571       const pat::Jet& monitorJet = *jet;
0572 
0573       ++mult;
0574 
0575       if (monitorJet.chargedHadronEnergyFraction() > 0 && monitorJet.chargedMultiplicity() > 0 &&
0576           monitorJet.chargedEmEnergyFraction() < 0.99 && monitorJet.neutralHadronEnergyFraction() < 0.99 &&
0577           monitorJet.neutralEmEnergyFraction() < 0.99 &&
0578           (monitorJet.chargedMultiplicity() + monitorJet.neutralMultiplicity()) > 1) {
0579         correctedJets.push_back(monitorJet);
0580         ++loosemult;  // determine jet multiplicity
0581 
0582         //ParticleNet discriminator
0583 
0584         double discriminator =
0585             monitorJet.bDiscriminator("pfParticleNetFromMiniAODAK4CHSCentralDiscriminatorsJetTags:BvsAll") > 0
0586                 ? monitorJet.bDiscriminator("pfParticleNetFromMiniAODAK4CHSCentralDiscriminatorsJetTags:BvsAll")
0587                 : -1;
0588 
0589         fill("jetBPNet_", discriminator);  //hard coded discriminator and value right now.
0590         if (discriminator > 0.1919)
0591           ++multBPNetM;
0592 
0593         // Fill a vector with Jet b-tag WP for later M3+1tag calculation: PNet
0594         // tagger
0595         JetTagValues.push_back(discriminator);
0596         //    }
0597         // fill pt (raw or L2L3) for the leading four jets
0598         if (loosemult == 1) {
0599           //cout<<" jet id= "<<monitorJet.chargedHadronEnergyFraction()<<endl;
0600 
0601           fill("jet1Pt_", monitorJet.pt());
0602           //fill("jet1PtRaw_", jet->pt());
0603           fill("jet1Eta_", monitorJet.eta());
0604         };
0605         if (loosemult == 2) {
0606           fill("jet2Pt_", monitorJet.pt());
0607           //fill("jet2PtRaw_", jet->pt());
0608           fill("jet2Eta_", monitorJet.eta());
0609         }
0610         if (loosemult == 3) {
0611           fill("jet3Pt_", monitorJet.pt());
0612           //fill("jet3PtRaw_", jet->pt());
0613           fill("jet3Eta_", monitorJet.eta());
0614         }
0615         if (loosemult == 4) {
0616           fill("jet4Pt_", monitorJet.pt());
0617           //fill("jet4PtRaw_", jet->pt());
0618           fill("jet4Eta_", monitorJet.eta());
0619         }
0620       }
0621     }
0622     fill("jetMult_", mult);
0623     fill("jetLooseMult_", loosemult);
0624     fill("jetMultBPNetM_", multBPNetM);
0625 
0626     /*
0627   ------------------------------------------------------------
0628 
0629   MET Monitoring
0630 
0631   ------------------------------------------------------------
0632   */
0633 
0634     // fill monitoring histograms for met
0635     for (std::vector<edm::EDGetTokenT<edm::View<pat::MET>>>::const_iterator met_ = mets_.begin(); met_ != mets_.end();
0636          ++met_) {
0637       edm::Handle<edm::View<pat::MET>> met;
0638       if (!event.getByToken(*met_, met))
0639         continue;
0640       if (met->begin() != met->end()) {
0641         unsigned int idx = met_ - mets_.begin();
0642         if (idx == 0)
0643           fill("slimmedMETs_", met->begin()->et());
0644         if (idx == 1)
0645           fill("slimmedMETsPuppi_", met->begin()->et());
0646       }
0647     }
0648 
0649     /*
0650   ------------------------------------------------------------
0651 
0652   Event Monitoring
0653 
0654   ------------------------------------------------------------
0655   */
0656 
0657     // fill W boson and top mass estimates
0658 
0659     Calculate_miniAOD eventKinematics(MAXJETS, WMASS);
0660     double wMass = eventKinematics.massWBoson(correctedJets);
0661     double topMass = eventKinematics.massTopQuark(correctedJets);
0662     if (wMass >= 0 && topMass >= 0) {
0663       fill("massW_", wMass);
0664       fill("massTop_", topMass);
0665     }
0666 
0667     // Fill M3 with Btag (PNet Tight) requirement
0668 
0669     // if (!includeBTag_) return;
0670     if (correctedJets.size() != JetTagValues.size())
0671       return;
0672 
0673     double btopMass = eventKinematics.massBTopQuark(correctedJets, JetTagValues, 0.2435);  //hard coded PNet value
0674 
0675     if (btopMass >= 0)
0676       fill("massBTop_", btopMass);
0677 
0678     // fill plots for trigger monitoring
0679     if ((lowerEdge_ == -1. && upperEdge_ == -1.) || (lowerEdge_ < wMass && wMass < upperEdge_)) {
0680       if (!triggerTable_.isUninitialized())
0681         fill(event, *triggerTable, "trigger", triggerPaths_);
0682       if (logged_ <= hists_.find("eventLogger_")->second->getNbinsY()) {
0683         // log runnumber, lumi block, event number & some
0684         // more pysics infomation for interesting events
0685         fill("eventLogger_", 0.5, logged_ + 0.5, event.eventAuxiliary().run());
0686         fill("eventLogger_", 1.5, logged_ + 0.5, event.eventAuxiliary().luminosityBlock());
0687         fill("eventLogger_", 2.5, logged_ + 0.5, event.eventAuxiliary().event());
0688         //if (correctedJets.size() > 0)
0689         if (!correctedJets.empty())
0690           fill("eventLogger_", 3.5, logged_ + 0.5, correctedJets[0].pt());
0691         if (correctedJets.size() > 1)
0692           fill("eventLogger_", 4.5, logged_ + 0.5, correctedJets[1].pt());
0693         if (correctedJets.size() > 2)
0694           fill("eventLogger_", 5.5, logged_ + 0.5, correctedJets[2].pt());
0695         if (correctedJets.size() > 3)
0696           fill("eventLogger_", 6.5, logged_ + 0.5, correctedJets[3].pt());
0697         fill("eventLogger_", 7.5, logged_ + 0.5, wMass);
0698         fill("eventLogger_", 8.5, logged_ + 0.5, topMass);
0699         ++logged_;
0700       }
0701     }
0702   }
0703 }  // namespace TopSingleLepton_miniAOD
0704 
0705 TopSingleLeptonDQM_miniAOD::TopSingleLeptonDQM_miniAOD(const edm::ParameterSet& cfg)
0706     : vertexSelect_(nullptr),
0707       beamspot_(""),
0708       beamspotSelect_(nullptr),
0709       MuonStep(nullptr),
0710       ElectronStep(nullptr),
0711       PvStep(nullptr),
0712       METStep(nullptr) {
0713   JetSteps.clear();
0714 
0715   // configure preselection
0716   edm::ParameterSet presel = cfg.getParameter<edm::ParameterSet>("preselection");
0717   if (presel.existsAs<edm::ParameterSet>("trigger")) {
0718     edm::ParameterSet trigger = presel.getParameter<edm::ParameterSet>("trigger");
0719     triggerTable__ = consumes<edm::TriggerResults>(trigger.getParameter<edm::InputTag>("src"));
0720     triggerPaths_ = trigger.getParameter<std::vector<std::string>>("select");
0721   }
0722   if (presel.existsAs<edm::ParameterSet>("beamspot")) {
0723     edm::ParameterSet beamspot = presel.getParameter<edm::ParameterSet>("beamspot");
0724     beamspot_ = beamspot.getParameter<edm::InputTag>("src");
0725     beamspot__ = consumes<reco::BeamSpot>(beamspot.getParameter<edm::InputTag>("src"));
0726     beamspotSelect_ =
0727         std::make_unique<StringCutObjectSelector<reco::BeamSpot>>(beamspot.getParameter<std::string>("select"));
0728   }
0729 
0730   // conifgure the selection
0731   sel_ = cfg.getParameter<std::vector<edm::ParameterSet>>("selection");
0732   setup_ = cfg.getParameter<edm::ParameterSet>("setup");
0733   for (unsigned int i = 0; i < sel_.size(); ++i) {
0734     selectionOrder_.push_back(sel_.at(i).getParameter<std::string>("label"));
0735     selection_[selectionStep(selectionOrder_.back())] =
0736         std::make_pair(sel_.at(i),
0737                        std::make_unique<TopSingleLepton_miniAOD::MonitorEnsemble>(
0738                            selectionStep(selectionOrder_.back()).c_str(), setup_, consumesCollector()));
0739   }
0740   for (std::vector<std::string>::const_iterator selIt = selectionOrder_.begin(); selIt != selectionOrder_.end();
0741        ++selIt) {
0742     std::string key = selectionStep(*selIt), type = objectType(*selIt);
0743     if (selection_.find(key) != selection_.end()) {
0744       if (type == "muons") {
0745         MuonStep = std::make_unique<SelectionStep<pat::Muon>>(selection_[key].first, consumesCollector());
0746       }
0747       if (type == "elecs") {
0748         ElectronStep = std::make_unique<SelectionStep<pat::Electron>>(selection_[key].first, consumesCollector());
0749       }
0750       if (type == "pvs") {
0751         PvStep = std::make_unique<SelectionStep<reco::Vertex>>(selection_[key].first, consumesCollector());
0752       }
0753       if (type == "jets") {
0754         JetSteps.push_back(std::make_unique<SelectionStep<pat::Jet>>(selection_[key].first, consumesCollector()));
0755       }
0756 
0757       if (type == "met") {
0758         METStep = std::make_unique<SelectionStep<pat::MET>>(selection_[key].first, consumesCollector());
0759       }
0760     }
0761   }
0762 }
0763 void TopSingleLeptonDQM_miniAOD::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const&, edm::EventSetup const&) {
0764   for (auto selIt = selection_.begin(); selIt != selection_.end(); ++selIt) {
0765     selIt->second.second->book(ibooker);
0766   }
0767 }
0768 void TopSingleLeptonDQM_miniAOD::analyze(const edm::Event& event, const edm::EventSetup& setup) {
0769   if (!triggerTable__.isUninitialized()) {
0770     edm::Handle<edm::TriggerResults> triggerTable;
0771     if (!event.getByToken(triggerTable__, triggerTable))
0772       return;
0773     if (!accept(event, *triggerTable, triggerPaths_))
0774       return;
0775   }
0776   if (!beamspot__.isUninitialized()) {
0777     edm::Handle<reco::BeamSpot> beamspot;
0778     if (!event.getByToken(beamspot__, beamspot))
0779       return;
0780     if (!(*beamspotSelect_)(*beamspot))
0781       return;
0782   }
0783 
0784   unsigned int nJetSteps = -1;
0785 
0786   for (std::vector<std::string>::const_iterator selIt = selectionOrder_.begin(); selIt != selectionOrder_.end();
0787        ++selIt) {
0788     std::string key = selectionStep(*selIt), type = objectType(*selIt);
0789     if (selection_.find(key) != selection_.end()) {
0790       if (type == "empty") {
0791         selection_[key].second->fill(event, setup);
0792       }
0793       if (type == "muons" && MuonStep != nullptr) {
0794         if (MuonStep->select(event)) {
0795           selection_[key].second->fill(event, setup);
0796         } else
0797           break;
0798       }
0799 
0800       if (type == "elecs" && ElectronStep != nullptr) {
0801         if (ElectronStep->select(event)) {
0802           selection_[key].second->fill(event, setup);
0803         } else
0804           break;
0805       }
0806 
0807       if (type == "pvs" && PvStep != nullptr) {
0808         if (PvStep->selectVertex(event)) {
0809           selection_[key].second->fill(event, setup);
0810         } else
0811           break;
0812       }
0813 
0814       if (type == "jets") {
0815         nJetSteps++;
0816         if (JetSteps[nJetSteps] != nullptr) {
0817           if (JetSteps[nJetSteps]->select(event, setup)) {
0818             selection_[key].second->fill(event, setup);
0819           } else
0820             break;
0821         }
0822       }
0823 
0824       if (type == "met" && METStep != nullptr) {
0825         if (METStep->select(event)) {
0826           selection_[key].second->fill(event, setup);
0827         } else
0828           break;
0829       }
0830     }
0831   }
0832 }
0833 
0834 // Local Variables:
0835 // show-trailing-whitespace: t
0836 // truncate-lines: t
0837 // End: