Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:18

0001 
0002 #include "PhysicsTools/SelectorUtils/interface/WPlusJetsEventSelector.h"
0003 #include "DataFormats/Candidate/interface/ShallowCloneCandidate.h"
0004 
0005 #include <iostream>
0006 
0007 using namespace std;
0008 
0009 WPlusJetsEventSelector::WPlusJetsEventSelector(edm::ParameterSet const& params)
0010     : EventSelector(),
0011       muonTag_(params.getParameter<edm::InputTag>("muonSrc")),
0012       electronTag_(params.getParameter<edm::InputTag>("electronSrc")),
0013       jetTag_(params.getParameter<edm::InputTag>("jetSrc")),
0014       metTag_(params.getParameter<edm::InputTag>("metSrc")),
0015       trigTag_(params.getParameter<edm::InputTag>("trigSrc")),
0016       muTrig_(params.getParameter<std::string>("muTrig")),
0017       eleTrig_(params.getParameter<std::string>("eleTrig")),
0018       pvSelector_(params.getParameter<edm::ParameterSet>("pvSelector")),
0019       muonIdTight_(params.getParameter<edm::ParameterSet>("muonIdTight")),
0020       electronIdTight_(params.getParameter<edm::ParameterSet>("electronIdTight")),
0021       muonIdLoose_(params.getParameter<edm::ParameterSet>("muonIdLoose")),
0022       electronIdLoose_(params.getParameter<edm::ParameterSet>("electronIdLoose")),
0023       jetIdLoose_(params.getParameter<edm::ParameterSet>("jetIdLoose")),
0024       pfjetIdLoose_(params.getParameter<edm::ParameterSet>("pfjetIdLoose")),
0025       minJets_(params.getParameter<int>("minJets")),
0026       muJetDR_(params.getParameter<double>("muJetDR")),
0027       eleJetDR_(params.getParameter<double>("eleJetDR")),
0028       muPlusJets_(params.getParameter<bool>("muPlusJets")),
0029       ePlusJets_(params.getParameter<bool>("ePlusJets")),
0030       muPtMin_(params.getParameter<double>("muPtMin")),
0031       muEtaMax_(params.getParameter<double>("muEtaMax")),
0032       eleEtMin_(params.getParameter<double>("eleEtMin")),
0033       eleEtaMax_(params.getParameter<double>("eleEtaMax")),
0034       muPtMinLoose_(params.getParameter<double>("muPtMinLoose")),
0035       muEtaMaxLoose_(params.getParameter<double>("muEtaMaxLoose")),
0036       eleEtMinLoose_(params.getParameter<double>("eleEtMinLoose")),
0037       eleEtaMaxLoose_(params.getParameter<double>("eleEtaMaxLoose")),
0038       jetPtMin_(params.getParameter<double>("jetPtMin")),
0039       jetEtaMax_(params.getParameter<double>("jetEtaMax")),
0040       jetScale_(params.getParameter<double>("jetScale")),
0041       metMin_(params.getParameter<double>("metMin")) {
0042   // make the bitset
0043   push_back("Inclusive");
0044   push_back("Trigger");
0045   push_back("PV");
0046   push_back(">= 1 Lepton");
0047   push_back("== 1 Tight Lepton");
0048   push_back("== 1 Tight Lepton, Mu Veto");
0049   push_back("== 1 Lepton");
0050   push_back("MET Cut");
0051   push_back("Z Veto");
0052   push_back("Conversion Veto");
0053   push_back("Cosmic Veto");
0054   push_back(">=1 Jets");
0055   push_back(">=2 Jets");
0056   push_back(">=3 Jets");
0057   push_back(">=4 Jets");
0058   push_back(">=5 Jets");
0059 
0060   // turn (almost) everything on by default
0061   set("Inclusive");
0062   set("Trigger");
0063   set("PV");
0064   set(">= 1 Lepton");
0065   set("== 1 Tight Lepton");
0066   set("== 1 Tight Lepton, Mu Veto");
0067   set("== 1 Lepton");
0068   set("MET Cut");
0069   set("Z Veto");
0070   set("Conversion Veto");
0071   set("Cosmic Veto");
0072   set(">=1 Jets", minJets_ >= 1);
0073   set(">=2 Jets", minJets_ >= 2);
0074   set(">=3 Jets", minJets_ >= 3);
0075   set(">=4 Jets", minJets_ >= 4);
0076   set(">=5 Jets", minJets_ >= 5);
0077 
0078   inclusiveIndex_ = index_type(&bits_, std::string("Inclusive"));
0079   triggerIndex_ = index_type(&bits_, std::string("Trigger"));
0080   pvIndex_ = index_type(&bits_, std::string("PV"));
0081   lep1Index_ = index_type(&bits_, std::string(">= 1 Lepton"));
0082   lep2Index_ = index_type(&bits_, std::string("== 1 Tight Lepton"));
0083   lep3Index_ = index_type(&bits_, std::string("== 1 Tight Lepton, Mu Veto"));
0084   lep4Index_ = index_type(&bits_, std::string("== 1 Lepton"));
0085   metIndex_ = index_type(&bits_, std::string("MET Cut"));
0086   zvetoIndex_ = index_type(&bits_, std::string("Z Veto"));
0087   conversionIndex_ = index_type(&bits_, std::string("Conversion Veto"));
0088   cosmicIndex_ = index_type(&bits_, std::string("Cosmic Veto"));
0089   jet1Index_ = index_type(&bits_, std::string(">=1 Jets"));
0090   jet2Index_ = index_type(&bits_, std::string(">=2 Jets"));
0091   jet3Index_ = index_type(&bits_, std::string(">=3 Jets"));
0092   jet4Index_ = index_type(&bits_, std::string(">=4 Jets"));
0093   jet5Index_ = index_type(&bits_, std::string(">=5 Jets"));
0094 
0095   if (params.exists("cutsToIgnore"))
0096     setIgnoredCuts(params.getParameter<std::vector<std::string> >("cutsToIgnore"));
0097 
0098   retInternal_ = getBitTemplate();
0099 }
0100 
0101 bool WPlusJetsEventSelector::operator()(edm::EventBase const& event, pat::strbitset& ret) {
0102   ret.set(false);
0103 
0104   selectedJets_.clear();
0105   cleanedJets_.clear();
0106   selectedMuons_.clear();
0107   selectedElectrons_.clear();
0108   looseMuons_.clear();
0109   looseElectrons_.clear();
0110   selectedMETs_.clear();
0111 
0112   passCut(ret, inclusiveIndex_);
0113 
0114   bool passTrig = false;
0115   if (!ignoreCut(triggerIndex_)) {
0116     edm::Handle<pat::TriggerEvent> triggerEvent;
0117     event.getByLabel(trigTag_, triggerEvent);
0118 
0119     pat::TriggerEvent const* trig = &*triggerEvent;
0120 
0121     if (trig->wasRun() && trig->wasAccept()) {
0122       pat::TriggerPath const* muPath = trig->path(muTrig_);
0123 
0124       pat::TriggerPath const* elePath = trig->path(eleTrig_);
0125 
0126       if (muPlusJets_ && muPath != nullptr && muPath->wasAccept()) {
0127         passTrig = true;
0128       }
0129 
0130       if (ePlusJets_ && elePath != nullptr && elePath->wasAccept()) {
0131         passTrig = true;
0132       }
0133     }
0134   }
0135 
0136   if (ignoreCut(triggerIndex_) || passTrig) {
0137     passCut(ret, triggerIndex_);
0138 
0139     bool passPV = false;
0140 
0141     passPV = pvSelector_(event);
0142     if (ignoreCut(pvIndex_) || passPV) {
0143       passCut(ret, pvIndex_);
0144 
0145       edm::Handle<vector<pat::Electron> > electronHandle;
0146       event.getByLabel(electronTag_, electronHandle);
0147 
0148       edm::Handle<vector<pat::Muon> > muonHandle;
0149       event.getByLabel(muonTag_, muonHandle);
0150 
0151       edm::Handle<vector<pat::Jet> > jetHandle;
0152 
0153       edm::Handle<edm::OwnVector<reco::Candidate> > jetClonesHandle;
0154 
0155       edm::Handle<vector<pat::MET> > metHandle;
0156       event.getByLabel(metTag_, metHandle);
0157 
0158       int nElectrons = 0;
0159       for (std::vector<pat::Electron>::const_iterator electronBegin = electronHandle->begin(),
0160                                                       electronEnd = electronHandle->end(),
0161                                                       ielectron = electronBegin;
0162            ielectron != electronEnd;
0163            ++ielectron) {
0164         ++nElectrons;
0165         // Tight cuts
0166         if (ielectron->et() > eleEtMin_ && fabs(ielectron->eta()) < eleEtaMax_ && electronIdTight_(*ielectron) &&
0167             ielectron->electronID("eidRobustTight") > 0) {
0168           selectedElectrons_.push_back(
0169               reco::ShallowClonePtrCandidate(edm::Ptr<pat::Electron>(electronHandle, ielectron - electronBegin)));
0170         } else {
0171           // Loose cuts
0172           if (ielectron->et() > eleEtMinLoose_ && fabs(ielectron->eta()) < eleEtaMaxLoose_ &&
0173               electronIdLoose_(*ielectron)) {
0174             looseElectrons_.push_back(
0175                 reco::ShallowClonePtrCandidate(edm::Ptr<pat::Electron>(electronHandle, ielectron - electronBegin)));
0176           }
0177         }
0178       }
0179 
0180       for (std::vector<pat::Muon>::const_iterator muonBegin = muonHandle->begin(),
0181                                                   muonEnd = muonHandle->end(),
0182                                                   imuon = muonBegin;
0183            imuon != muonEnd;
0184            ++imuon) {
0185         if (!imuon->isGlobalMuon())
0186           continue;
0187 
0188         // Tight cuts
0189         bool passTight = muonIdTight_(*imuon, event) && imuon->isTrackerMuon();
0190         if (imuon->pt() > muPtMin_ && fabs(imuon->eta()) < muEtaMax_ && passTight) {
0191           selectedMuons_.push_back(reco::ShallowClonePtrCandidate(edm::Ptr<pat::Muon>(muonHandle, imuon - muonBegin)));
0192         } else {
0193           // Loose cuts
0194           if (imuon->pt() > muPtMinLoose_ && fabs(imuon->eta()) < muEtaMaxLoose_ && muonIdLoose_(*imuon, event)) {
0195             looseMuons_.push_back(reco::ShallowClonePtrCandidate(edm::Ptr<pat::Muon>(muonHandle, imuon - muonBegin)));
0196           }
0197         }
0198       }
0199 
0200       met_ = reco::ShallowClonePtrCandidate(
0201           edm::Ptr<pat::MET>(metHandle, 0), metHandle->at(0).charge(), metHandle->at(0).p4());
0202 
0203       event.getByLabel(jetTag_, jetHandle);
0204       pat::strbitset ret1 = jetIdLoose_.getBitTemplate();
0205       pat::strbitset ret2 = pfjetIdLoose_.getBitTemplate();
0206       for (std::vector<pat::Jet>::const_iterator jetBegin = jetHandle->begin(),
0207                                                  jetEnd = jetHandle->end(),
0208                                                  ijet = jetBegin;
0209            ijet != jetEnd;
0210            ++ijet) {
0211         reco::ShallowClonePtrCandidate scaledJet(reco::ShallowClonePtrCandidate(
0212             edm::Ptr<pat::Jet>(jetHandle, ijet - jetBegin), ijet->charge(), ijet->p4() * jetScale_));
0213         bool passJetID = false;
0214         if (ijet->isCaloJet() || ijet->isJPTJet())
0215           passJetID = jetIdLoose_(*ijet, ret1);
0216         else
0217           passJetID = pfjetIdLoose_(*ijet, ret2);
0218         if (scaledJet.pt() > jetPtMin_ && fabs(scaledJet.eta()) < jetEtaMax_ && passJetID) {
0219           selectedJets_.push_back(scaledJet);
0220 
0221           if (muPlusJets_) {
0222             //Remove some jets
0223             bool indeltaR = false;
0224             for (std::vector<reco::ShallowClonePtrCandidate>::const_iterator muonBegin = selectedMuons_.begin(),
0225                                                                              muonEnd = selectedMuons_.end(),
0226                                                                              imuon = muonBegin;
0227                  imuon != muonEnd;
0228                  ++imuon) {
0229               if (reco::deltaR(imuon->eta(), imuon->phi(), scaledJet.eta(), scaledJet.phi()) < muJetDR_) {
0230                 indeltaR = true;
0231               }
0232             }
0233             if (!indeltaR) {
0234               cleanedJets_.push_back(scaledJet);
0235             }  // end if jet is not within dR of a muon
0236           }    // end if mu+jets
0237           else {
0238             //Remove some jets
0239             bool indeltaR = false;
0240             for (std::vector<reco::ShallowClonePtrCandidate>::const_iterator electronBegin = selectedElectrons_.begin(),
0241                                                                              electronEnd = selectedElectrons_.end(),
0242                                                                              ielectron = electronBegin;
0243                  ielectron != electronEnd;
0244                  ++ielectron) {
0245               if (reco::deltaR(ielectron->eta(), ielectron->phi(), scaledJet.eta(), scaledJet.phi()) < eleJetDR_) {
0246                 indeltaR = true;
0247               }
0248             }
0249             if (!indeltaR) {
0250               cleanedJets_.push_back(scaledJet);
0251             }  // end if jet is not within dR of an electron
0252           }    // end if e+jets
0253         }      // end if pass id and kin cuts
0254       }        // end loop over jets
0255 
0256       int nleptons = 0;
0257       if (muPlusJets_)
0258         nleptons += selectedMuons_.size();
0259 
0260       if (ePlusJets_)
0261         nleptons += selectedElectrons_.size();
0262 
0263       if (ignoreCut(lep1Index_) || (nleptons > 0)) {
0264         passCut(ret, lep1Index_);
0265 
0266         if (ignoreCut(lep2Index_) || (nleptons == 1)) {
0267           passCut(ret, lep2Index_);
0268 
0269           bool oneMuon = (selectedMuons_.size() == 1 &&
0270                           looseMuons_.size() + selectedElectrons_.size() + looseElectrons_.size() == 0);
0271           bool oneElectron = (selectedElectrons_.size() == 1 && selectedMuons_.empty());
0272 
0273           bool oneMuonMuVeto = (selectedMuons_.size() == 1 && looseMuons_.empty());
0274 
0275           if (ignoreCut(lep3Index_) || ePlusJets_ || (muPlusJets_ && oneMuonMuVeto)) {
0276             passCut(ret, lep3Index_);
0277 
0278             if (ignoreCut(lep4Index_) || ((muPlusJets_ && oneMuon) ^ (ePlusJets_ && oneElectron))) {
0279               passCut(ret, lep4Index_);
0280 
0281               bool metCut = met_.pt() > metMin_;
0282               if (ignoreCut(metIndex_) || metCut) {
0283                 passCut(ret, metIndex_);
0284 
0285                 bool zVeto = true;
0286                 if (selectedMuons_.size() == 2) {
0287                 }
0288                 if (selectedElectrons_.size() == 2) {
0289                 }
0290                 if (ignoreCut(zvetoIndex_) || zVeto) {
0291                   passCut(ret, zvetoIndex_);
0292 
0293                   bool conversionVeto = true;
0294                   if (ignoreCut(conversionIndex_) || conversionVeto) {
0295                     passCut(ret, conversionIndex_);
0296 
0297                     bool cosmicVeto = true;
0298                     if (ignoreCut(cosmicIndex_) || cosmicVeto) {
0299                       passCut(ret, cosmicIndex_);
0300 
0301                       if (ignoreCut(jet1Index_) || static_cast<int>(cleanedJets_.size()) >= 1) {
0302                         passCut(ret, jet1Index_);
0303                       }  // end if >=1 tight jets
0304 
0305                       if (ignoreCut(jet2Index_) || static_cast<int>(cleanedJets_.size()) >= 2) {
0306                         passCut(ret, jet2Index_);
0307                       }  // end if >=2 tight jets
0308 
0309                       if (ignoreCut(jet3Index_) || static_cast<int>(cleanedJets_.size()) >= 3) {
0310                         passCut(ret, jet3Index_);
0311                       }  // end if >=3 tight jets
0312 
0313                       if (ignoreCut(jet4Index_) || static_cast<int>(cleanedJets_.size()) >= 4) {
0314                         passCut(ret, jet4Index_);
0315                       }  // end if >=4 tight jets
0316 
0317                       if (ignoreCut(jet5Index_) || static_cast<int>(cleanedJets_.size()) >= 5) {
0318                         passCut(ret, jet5Index_);
0319                       }  // end if >=5 tight jets
0320 
0321                     }  // end if cosmic veto
0322 
0323                   }  // end if conversion veto
0324 
0325                 }  // end if z veto
0326 
0327               }  // end if met cut
0328 
0329             }  // end if == 1 lepton
0330 
0331           }  // end if == 1 tight lepton with a muon veto separately
0332 
0333         }  // end if == 1 tight lepton
0334 
0335       }  // end if >= 1 lepton
0336 
0337     }  // end if PV
0338 
0339   }  // end if trigger
0340 
0341   setIgnored(ret);
0342 
0343   return (bool)ret;
0344 }