Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:37:24

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       for (std::vector<pat::Electron>::const_iterator electronBegin = electronHandle->begin(),
0159                                                       electronEnd = electronHandle->end(),
0160                                                       ielectron = electronBegin;
0161            ielectron != electronEnd;
0162            ++ielectron) {
0163         // Tight cuts
0164         if (ielectron->et() > eleEtMin_ && fabs(ielectron->eta()) < eleEtaMax_ && electronIdTight_(*ielectron) &&
0165             ielectron->electronID("eidRobustTight") > 0) {
0166           selectedElectrons_.push_back(
0167               reco::ShallowClonePtrCandidate(edm::Ptr<pat::Electron>(electronHandle, ielectron - electronBegin)));
0168         } else {
0169           // Loose cuts
0170           if (ielectron->et() > eleEtMinLoose_ && fabs(ielectron->eta()) < eleEtaMaxLoose_ &&
0171               electronIdLoose_(*ielectron)) {
0172             looseElectrons_.push_back(
0173                 reco::ShallowClonePtrCandidate(edm::Ptr<pat::Electron>(electronHandle, ielectron - electronBegin)));
0174           }
0175         }
0176       }
0177 
0178       for (std::vector<pat::Muon>::const_iterator muonBegin = muonHandle->begin(),
0179                                                   muonEnd = muonHandle->end(),
0180                                                   imuon = muonBegin;
0181            imuon != muonEnd;
0182            ++imuon) {
0183         if (!imuon->isGlobalMuon())
0184           continue;
0185 
0186         // Tight cuts
0187         bool passTight = muonIdTight_(*imuon, event) && imuon->isTrackerMuon();
0188         if (imuon->pt() > muPtMin_ && fabs(imuon->eta()) < muEtaMax_ && passTight) {
0189           selectedMuons_.push_back(reco::ShallowClonePtrCandidate(edm::Ptr<pat::Muon>(muonHandle, imuon - muonBegin)));
0190         } else {
0191           // Loose cuts
0192           if (imuon->pt() > muPtMinLoose_ && fabs(imuon->eta()) < muEtaMaxLoose_ && muonIdLoose_(*imuon, event)) {
0193             looseMuons_.push_back(reco::ShallowClonePtrCandidate(edm::Ptr<pat::Muon>(muonHandle, imuon - muonBegin)));
0194           }
0195         }
0196       }
0197 
0198       met_ = reco::ShallowClonePtrCandidate(
0199           edm::Ptr<pat::MET>(metHandle, 0), metHandle->at(0).charge(), metHandle->at(0).p4());
0200 
0201       event.getByLabel(jetTag_, jetHandle);
0202       pat::strbitset ret1 = jetIdLoose_.getBitTemplate();
0203       pat::strbitset ret2 = pfjetIdLoose_.getBitTemplate();
0204       for (std::vector<pat::Jet>::const_iterator jetBegin = jetHandle->begin(),
0205                                                  jetEnd = jetHandle->end(),
0206                                                  ijet = jetBegin;
0207            ijet != jetEnd;
0208            ++ijet) {
0209         reco::ShallowClonePtrCandidate scaledJet(reco::ShallowClonePtrCandidate(
0210             edm::Ptr<pat::Jet>(jetHandle, ijet - jetBegin), ijet->charge(), ijet->p4() * jetScale_));
0211         bool passJetID = false;
0212         if (ijet->isCaloJet() || ijet->isJPTJet())
0213           passJetID = jetIdLoose_(*ijet, ret1);
0214         else
0215           passJetID = pfjetIdLoose_(*ijet, ret2);
0216         if (scaledJet.pt() > jetPtMin_ && fabs(scaledJet.eta()) < jetEtaMax_ && passJetID) {
0217           selectedJets_.push_back(scaledJet);
0218 
0219           if (muPlusJets_) {
0220             //Remove some jets
0221             bool indeltaR = false;
0222             for (std::vector<reco::ShallowClonePtrCandidate>::const_iterator muonBegin = selectedMuons_.begin(),
0223                                                                              muonEnd = selectedMuons_.end(),
0224                                                                              imuon = muonBegin;
0225                  imuon != muonEnd;
0226                  ++imuon) {
0227               if (reco::deltaR(imuon->eta(), imuon->phi(), scaledJet.eta(), scaledJet.phi()) < muJetDR_) {
0228                 indeltaR = true;
0229               }
0230             }
0231             if (!indeltaR) {
0232               cleanedJets_.push_back(scaledJet);
0233             }  // end if jet is not within dR of a muon
0234           }  // end if mu+jets
0235           else {
0236             //Remove some jets
0237             bool indeltaR = false;
0238             for (std::vector<reco::ShallowClonePtrCandidate>::const_iterator electronBegin = selectedElectrons_.begin(),
0239                                                                              electronEnd = selectedElectrons_.end(),
0240                                                                              ielectron = electronBegin;
0241                  ielectron != electronEnd;
0242                  ++ielectron) {
0243               if (reco::deltaR(ielectron->eta(), ielectron->phi(), scaledJet.eta(), scaledJet.phi()) < eleJetDR_) {
0244                 indeltaR = true;
0245               }
0246             }
0247             if (!indeltaR) {
0248               cleanedJets_.push_back(scaledJet);
0249             }  // end if jet is not within dR of an electron
0250           }  // end if e+jets
0251         }  // end if pass id and kin cuts
0252       }  // end loop over jets
0253 
0254       int nleptons = 0;
0255       if (muPlusJets_)
0256         nleptons += selectedMuons_.size();
0257 
0258       if (ePlusJets_)
0259         nleptons += selectedElectrons_.size();
0260 
0261       if (ignoreCut(lep1Index_) || (nleptons > 0)) {
0262         passCut(ret, lep1Index_);
0263 
0264         if (ignoreCut(lep2Index_) || (nleptons == 1)) {
0265           passCut(ret, lep2Index_);
0266 
0267           bool oneMuon = (selectedMuons_.size() == 1 &&
0268                           looseMuons_.size() + selectedElectrons_.size() + looseElectrons_.size() == 0);
0269           bool oneElectron = (selectedElectrons_.size() == 1 && selectedMuons_.empty());
0270 
0271           bool oneMuonMuVeto = (selectedMuons_.size() == 1 && looseMuons_.empty());
0272 
0273           if (ignoreCut(lep3Index_) || ePlusJets_ || (muPlusJets_ && oneMuonMuVeto)) {
0274             passCut(ret, lep3Index_);
0275 
0276             if (ignoreCut(lep4Index_) || ((muPlusJets_ && oneMuon) ^ (ePlusJets_ && oneElectron))) {
0277               passCut(ret, lep4Index_);
0278 
0279               bool metCut = met_.pt() > metMin_;
0280               if (ignoreCut(metIndex_) || metCut) {
0281                 passCut(ret, metIndex_);
0282 
0283                 bool zVeto = true;
0284                 if (selectedMuons_.size() == 2) {
0285                 }
0286                 if (selectedElectrons_.size() == 2) {
0287                 }
0288                 if (ignoreCut(zvetoIndex_) || zVeto) {
0289                   passCut(ret, zvetoIndex_);
0290 
0291                   bool conversionVeto = true;
0292                   if (ignoreCut(conversionIndex_) || conversionVeto) {
0293                     passCut(ret, conversionIndex_);
0294 
0295                     bool cosmicVeto = true;
0296                     if (ignoreCut(cosmicIndex_) || cosmicVeto) {
0297                       passCut(ret, cosmicIndex_);
0298 
0299                       if (ignoreCut(jet1Index_) || static_cast<int>(cleanedJets_.size()) >= 1) {
0300                         passCut(ret, jet1Index_);
0301                       }  // end if >=1 tight jets
0302 
0303                       if (ignoreCut(jet2Index_) || static_cast<int>(cleanedJets_.size()) >= 2) {
0304                         passCut(ret, jet2Index_);
0305                       }  // end if >=2 tight jets
0306 
0307                       if (ignoreCut(jet3Index_) || static_cast<int>(cleanedJets_.size()) >= 3) {
0308                         passCut(ret, jet3Index_);
0309                       }  // end if >=3 tight jets
0310 
0311                       if (ignoreCut(jet4Index_) || static_cast<int>(cleanedJets_.size()) >= 4) {
0312                         passCut(ret, jet4Index_);
0313                       }  // end if >=4 tight jets
0314 
0315                       if (ignoreCut(jet5Index_) || static_cast<int>(cleanedJets_.size()) >= 5) {
0316                         passCut(ret, jet5Index_);
0317                       }  // end if >=5 tight jets
0318 
0319                     }  // end if cosmic veto
0320 
0321                   }  // end if conversion veto
0322 
0323                 }  // end if z veto
0324 
0325               }  // end if met cut
0326 
0327             }  // end if == 1 lepton
0328 
0329           }  // end if == 1 tight lepton with a muon veto separately
0330 
0331         }  // end if == 1 tight lepton
0332 
0333       }  // end if >= 1 lepton
0334 
0335     }  // end if PV
0336 
0337   }  // end if trigger
0338 
0339   setIgnored(ret);
0340 
0341   return (bool)ret;
0342 }