Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:33:00

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