Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:37

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Common/interface/TriggerNames.h"
0003 #include "FWCore/Utilities/interface/RegexMatch.h"
0004 #include "DataFormats/Common/interface/Handle.h"
0005 #include "DataFormats/Common/interface/TriggerResults.h"
0006 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0007 #include "DataFormats/VertexReco/interface/Vertex.h"
0008 
0009 #include <TLorentzVector.h>
0010 
0011 #include <memory>
0012 
0013 #include "DQMOffline/Lumi/plugins/ZCountingElectrons.h"
0014 
0015 //
0016 // -------------------------------------- Constructor --------------------------------------------
0017 //
0018 ZCountingElectrons::ZCountingElectrons(const edm::ParameterSet& iConfig)
0019     : triggerResultsInputTag_(iConfig.getParameter<edm::InputTag>("TriggerResults")),
0020       fPVName_token(consumes<reco::VertexCollection>(
0021           iConfig.getUntrackedParameter<std::string>("edmPVName", "offlinePrimaryVertices"))),
0022 
0023       // Electron-specific Parameters
0024       fGsfElectronName_token(consumes<edm::View<reco::GsfElectron>>(
0025           iConfig.getUntrackedParameter<std::string>("edmGsfEleName", "gedGsfElectrons"))),
0026       fSCName_token(consumes<edm::View<reco::SuperCluster>>(
0027           iConfig.getUntrackedParameter<std::string>("edmSCName", "particleFlowEGamma"))),
0028 
0029       // Electron-specific Tags
0030       fRhoToken(consumes<double>(iConfig.getParameter<edm::InputTag>("rhoname"))),
0031       fBeamspotToken(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamspotName"))),
0032       fConversionToken(consumes<reco::ConversionCollection>(iConfig.getParameter<edm::InputTag>("conversionsName"))),
0033 
0034       // Electron-specific Cuts
0035       PtCutL1_(iConfig.getUntrackedParameter<double>("PtCutL1")),
0036       PtCutL2_(iConfig.getUntrackedParameter<double>("PtCutL2")),
0037       EtaCutL1_(iConfig.getUntrackedParameter<double>("EtaCutL1")),
0038       EtaCutL2_(iConfig.getUntrackedParameter<double>("EtaCutL2")),
0039 
0040       MassBin_(iConfig.getUntrackedParameter<double>("MassBin")),
0041       MassMin_(iConfig.getUntrackedParameter<double>("MassMin")),
0042       MassMax_(iConfig.getUntrackedParameter<double>("MassMax")),
0043 
0044       LumiBin_(iConfig.getUntrackedParameter<double>("LumiBin")),
0045       LumiMin_(iConfig.getUntrackedParameter<double>("LumiMin")),
0046       LumiMax_(iConfig.getUntrackedParameter<double>("LumiMax")),
0047 
0048       PVBin_(iConfig.getUntrackedParameter<int>("PVBin")),
0049       PVMin_(iConfig.getUntrackedParameter<double>("PVMin")),
0050       PVMax_(iConfig.getUntrackedParameter<double>("PVMax")),
0051 
0052       VtxNTracksFitCut_(iConfig.getUntrackedParameter<double>("VtxNTracksFitMin")),
0053       VtxNdofCut_(iConfig.getUntrackedParameter<double>("VtxNdofMin")),
0054       VtxAbsZCut_(iConfig.getUntrackedParameter<double>("VtxAbsZMax")),
0055       VtxRhoCut_(iConfig.getUntrackedParameter<double>("VtxRhoMax")),
0056 
0057       ELE_ID_WP(iConfig.getUntrackedParameter<std::string>("ElectronIDType", "TIGHT")),
0058       EleID_(ElectronIdentifier(iConfig)) {
0059   edm::LogInfo("ZCounting") << "Constructor  ZCountingElectrons::ZCounting " << std::endl;
0060 
0061   // Trigger settings
0062   triggers = new TriggerTools();
0063   triggers->setTriggerResultsToken(consumes<edm::TriggerResults>(triggerResultsInputTag_));
0064   triggers->setTriggerEventToken(consumes<trigger::TriggerEvent>(iConfig.getParameter<edm::InputTag>("TriggerEvent")));
0065   triggers->setDRMAX(DRMAX);
0066 
0067   edm::LogVerbatim("ZCounting") << "ZCounting::ZCounting set trigger names";
0068   const std::vector<std::string> patterns_ = iConfig.getParameter<std::vector<std::string>>("ElectronTriggerNames");
0069   for (const std::string& pattern_ : patterns_) {
0070     triggers->addTriggerRecord(pattern_);
0071   }
0072 
0073   EleID_.setID(ELE_ID_WP);
0074 }
0075 
0076 //
0077 //  -------------------------------------- Destructor --------------------------------------------
0078 //
0079 ZCountingElectrons::~ZCountingElectrons() {
0080   edm::LogInfo("ZCountingElectrons") << "Destructor ZCountingElectrons::~ZCountingElectrons " << std::endl;
0081 }
0082 
0083 //
0084 // -------------------------------------- beginRun --------------------------------------------
0085 //
0086 void ZCountingElectrons::dqmBeginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0087   edm::LogInfo("ZCountingElectrons") << "ZCountingElectrons::beginRun" << std::endl;
0088 
0089   // initialize triggers
0090 
0091   edm::LogVerbatim("ZCountingElectrons") << "ZCountingElectrons::dqmBeginRun now at " << iRun.id();
0092 
0093   bool hltChanged_ = true;
0094   if (hltConfigProvider_.init(iRun, iSetup, triggerResultsInputTag_.process(), hltChanged_)) {
0095     edm::LogVerbatim("ZCountingElectrons")
0096         << "ZCountingElectrons::dqmBeginRun [TriggerObjMatchValueMapsProducer::beginRun] "
0097            "HLTConfigProvider initialized [processName() = \""
0098         << hltConfigProvider_.processName() << "\", tableName() = \"" << hltConfigProvider_.tableName()
0099         << "\", size() = " << hltConfigProvider_.size() << "]";
0100   } else {
0101     edm::LogError("ZCountingElectrons")
0102         << "ZCountingElectrons::dqmBeginRun Initialization of HLTConfigProvider failed for Run=" << iRun.id()
0103         << " (process=\"" << triggerResultsInputTag_.process() << "\") -> plugin will not produce outputs for this Run";
0104     return;
0105   }
0106 
0107   triggers->initHLTObjects(hltConfigProvider_);
0108 }
0109 //
0110 // -------------------------------------- bookHistos --------------------------------------------
0111 //
0112 void ZCountingElectrons::bookHistograms(DQMStore::IBooker& ibooker_, edm::Run const&, edm::EventSetup const&) {
0113   edm::LogInfo("ZCountingElectrons") << "ZCountingElectrons::bookHistograms" << std::endl;
0114   ibooker_.cd();
0115   ibooker_.setCurrentFolder("ZCounting/Histograms");
0116 
0117   h_npv = ibooker_.book2D(
0118       "h_npv", "Events with valid primary vertex", LumiBin_, LumiMin_, LumiMax_, PVBin_, PVMin_, PVMax_);
0119 
0120   h_npv->setAxisTitle("luminosity section", 1);
0121   h_npv->setAxisTitle("number of primary vertices", 2);
0122 
0123   // Electron histograms
0124   h_ee_mass_id_pass_central = ibooker_.book2D("h_ee_mass_id_pass_central",
0125                                               "h_ee_mass_id_pass_central",
0126                                               LumiBin_,
0127                                               LumiMin_,
0128                                               LumiMax_,
0129                                               MassBin_,
0130                                               MassMin_,
0131                                               MassMax_);
0132   h_ee_mass_id_fail_central = ibooker_.book2D("h_ee_mass_id_fail_central",
0133                                               "h_ee_mass_id_fail_central",
0134                                               LumiBin_,
0135                                               LumiMin_,
0136                                               LumiMax_,
0137                                               MassBin_,
0138                                               MassMin_,
0139                                               MassMax_);
0140   h_ee_mass_id_pass_forward = ibooker_.book2D("h_ee_mass_id_pass_forward",
0141                                               "h_ee_mass_id_pass_forward",
0142                                               LumiBin_,
0143                                               LumiMin_,
0144                                               LumiMax_,
0145                                               MassBin_,
0146                                               MassMin_,
0147                                               MassMax_);
0148   h_ee_mass_id_fail_forward = ibooker_.book2D("h_ee_mass_id_fail_forward",
0149                                               "h_ee_mass_id_fail_forward",
0150                                               LumiBin_,
0151                                               LumiMin_,
0152                                               LumiMax_,
0153                                               MassBin_,
0154                                               MassMin_,
0155                                               MassMax_);
0156 
0157   h_ee_mass_HLT_pass_central = ibooker_.book2D("h_ee_mass_HLT_pass_central",
0158                                                "h_ee_mass_HLT_pass_central",
0159                                                LumiBin_,
0160                                                LumiMin_,
0161                                                LumiMax_,
0162                                                MassBin_,
0163                                                MassMin_,
0164                                                MassMax_);
0165   h_ee_mass_HLT_fail_central = ibooker_.book2D("h_ee_mass_HLT_fail_central",
0166                                                "h_ee_mass_HLT_fail_central",
0167                                                LumiBin_,
0168                                                LumiMin_,
0169                                                LumiMax_,
0170                                                MassBin_,
0171                                                MassMin_,
0172                                                MassMax_);
0173   h_ee_mass_HLT_pass_forward = ibooker_.book2D("h_ee_mass_HLT_pass_forward",
0174                                                "h_ee_mass_HLT_pass_forward",
0175                                                LumiBin_,
0176                                                LumiMin_,
0177                                                LumiMax_,
0178                                                MassBin_,
0179                                                MassMin_,
0180                                                MassMax_);
0181   h_ee_mass_HLT_fail_forward = ibooker_.book2D("h_ee_mass_HLT_fail_forward",
0182                                                "h_ee_mass_HLT_fail_forward",
0183                                                LumiBin_,
0184                                                LumiMin_,
0185                                                LumiMax_,
0186                                                MassBin_,
0187                                                MassMin_,
0188                                                MassMax_);
0189 
0190   h_ee_yield_Z_ebeb = ibooker_.book1D("h_ee_yield_Z_ebeb", "h_ee_yield_Z_ebeb", LumiBin_, LumiMin_, LumiMax_);
0191   h_ee_yield_Z_ebee = ibooker_.book1D("h_ee_yield_Z_ebee", "h_ee_yield_Z_ebee", LumiBin_, LumiMin_, LumiMax_);
0192   h_ee_yield_Z_eeee = ibooker_.book1D("h_ee_yield_Z_eeee", "h_ee_yield_Z_eeee", LumiBin_, LumiMin_, LumiMax_);
0193 }
0194 
0195 //
0196 // -------------------------------------- Analyze --------------------------------------------
0197 //
0198 //--------------------------------------------------------------------------------------------------
0199 void ZCountingElectrons::analyze(const edm::Event& iEvent,
0200                                  const edm::EventSetup& iSetup) {  // Fill event tree on the fly
0201   edm::LogInfo("ZCountingElectrons") << "ZCountingElectrons::analyze" << std::endl;
0202 
0203   //-------------------------------
0204   //--- Vertex
0205   //-------------------------------
0206   edm::Handle<reco::VertexCollection> hVertexProduct;
0207   iEvent.getByToken(fPVName_token, hVertexProduct);
0208   if (!hVertexProduct.isValid()) {
0209     edm::LogWarning("ZCounting") << "ZCountingElectrons::analyze - no valid primary vertex product found" << std::endl;
0210     return;
0211   }
0212   // const reco::VertexCollection* pvCol = hVertexProduct.product();
0213   // const reco::Vertex* pv = &(*pvCol->begin());
0214   int nvtx = 0;
0215 
0216   for (auto const& itVtx : *hVertexProduct) {
0217     if (itVtx.isFake())
0218       continue;
0219     if (itVtx.tracksSize() < VtxNTracksFitCut_)
0220       continue;
0221     if (itVtx.ndof() < VtxNdofCut_)
0222       continue;
0223     if (fabs(itVtx.z()) > VtxAbsZCut_)
0224       continue;
0225     if (itVtx.position().Rho() > VtxRhoCut_)
0226       continue;
0227 
0228     // if (nvtx == 0) {
0229     //   pv = &itVtx;
0230     // }
0231     nvtx++;
0232   }
0233 
0234   h_npv->Fill(iEvent.luminosityBlock(), nvtx);
0235 
0236   //-------------------------------
0237   //--- Trigger
0238   //-------------------------------
0239   triggers->readEvent(iEvent);
0240 
0241   // Trigger requirement
0242   if (!triggers->pass())
0243     return;
0244 
0245   // Get Electrons
0246   edm::Handle<edm::View<reco::GsfElectron>> electrons;
0247   iEvent.getByToken(fGsfElectronName_token, electrons);
0248 
0249   // Get SuperClusters
0250   edm::Handle<edm::View<reco::SuperCluster>> superclusters;
0251   iEvent.getByToken(fSCName_token, superclusters);
0252 
0253   // Get Rho
0254   edm::Handle<double> rhoHandle;
0255   iEvent.getByToken(fRhoToken, rhoHandle);
0256   EleID_.setRho(*rhoHandle);
0257 
0258   // Get beamspot
0259   edm::Handle<reco::BeamSpot> beamspotHandle;
0260   iEvent.getByToken(fBeamspotToken, beamspotHandle);
0261 
0262   // Conversions
0263   edm::Handle<reco::ConversionCollection> conversionsHandle;
0264   iEvent.getByToken(fConversionToken, conversionsHandle);
0265 
0266   edm::Ptr<reco::GsfElectron> eleProbe;
0267   enum { eEleEle2HLT = 1, eEleEle1HLT1L1, eEleEle1HLT, eEleEleNoSel, eEleSC };  // event category enum
0268 
0269   // Loop over Tags
0270   for (size_t itag = 0; itag < electrons->size(); ++itag) {
0271     const auto el1 = electrons->ptrAt(itag);
0272     if (not EleID_.passID(el1, beamspotHandle, conversionsHandle))
0273       continue;
0274 
0275     float pt1 = el1->pt();
0276     float eta1 = el1->eta();
0277     float phi1 = el1->phi();
0278 
0279     if (!triggers->passObj(eta1, phi1))
0280       continue;
0281     TLorentzVector vTag(0., 0., 0., 0.);
0282     vTag.SetPtEtaPhiM(pt1, eta1, phi1, ELECTRON_MASS);
0283 
0284     // Tag selection: kinematic cuts, lepton selection and trigger matching
0285     double tag_pt = vTag.Pt();
0286     double tag_abseta = fabs(vTag.Eta());
0287 
0288     bool tag_is_valid_tag = ele_tag_selection(tag_pt, tag_abseta);
0289     bool tag_is_valid_probe = ele_probe_selection(tag_pt, tag_abseta);
0290 
0291     if (not(tag_is_valid_tag or tag_is_valid_probe))
0292       continue;
0293 
0294     // Loop over probes
0295     for (size_t iprobe = 0; iprobe < superclusters->size(); ++iprobe) {
0296       // Initialize probe
0297       const auto sc = superclusters->ptrAt(iprobe);
0298       if (*sc == *(el1->superCluster())) {
0299         continue;
0300       }
0301 
0302       // Find matching electron
0303       for (size_t iele = 0; iele < electrons->size(); ++iele) {
0304         if (iele == itag)
0305           continue;
0306         const auto ele = electrons->ptrAt(iele);
0307         if (*sc == *(ele->superCluster())) {
0308           eleProbe = ele;
0309           break;
0310         }
0311       }
0312 
0313       // Assign final probe 4-vector
0314       TLorentzVector vProbe(0., 0., 0., 0.);
0315       if (eleProbe.isNonnull()) {
0316         vProbe.SetPtEtaPhiM(eleProbe->pt(), eleProbe->eta(), eleProbe->phi(), ELECTRON_MASS);
0317       } else {
0318         double pt = sc->energy() * sqrt(1 - pow(tanh(sc->eta()), 2));
0319         vProbe.SetPtEtaPhiM(pt, sc->eta(), sc->phi(), ELECTRON_MASS);
0320       }
0321 
0322       // Probe Selection
0323       double probe_pt = vProbe.Pt();
0324       double probe_abseta = fabs(sc->eta());
0325       bool probe_is_valid_probe = ele_probe_selection(probe_pt, probe_abseta);
0326       if (!probe_is_valid_probe)
0327         continue;
0328 
0329       // Good Probe found!
0330 
0331       // Require good Z
0332       TLorentzVector vDilep = vTag + vProbe;
0333 
0334       if ((vDilep.M() < MassMin_) || (vDilep.M() > MassMax_))
0335         continue;
0336       if (eleProbe.isNonnull() and (eleProbe->charge() != -el1->charge()))
0337         continue;
0338 
0339       // Good Z found!
0340       long ls = iEvent.luminosityBlock();
0341       bool probe_pass_trigger = triggers->passObj(vProbe.Eta(), vProbe.Phi());
0342       bool probe_pass_id = eleProbe.isNonnull() and EleID_.passID(eleProbe, beamspotHandle, conversionsHandle);
0343 
0344       //// Fill for yields
0345       bool probe_is_forward = probe_abseta > ELE_ETA_CRACK_LOW;
0346       bool tag_is_forward = tag_abseta > ELE_ETA_CRACK_LOW;
0347 
0348       if (probe_pass_id) {
0349         if (probe_is_forward and tag_is_forward) {
0350           h_ee_yield_Z_eeee->Fill(ls);
0351         } else if (!probe_is_forward and !tag_is_forward) {
0352           h_ee_yield_Z_ebeb->Fill(ls);
0353         } else {
0354           h_ee_yield_Z_ebee->Fill(ls);
0355         }
0356       }
0357 
0358       if (!tag_is_valid_tag)
0359         continue;
0360 
0361       /// Fill for ID efficiency
0362       if (probe_pass_id) {
0363         if (probe_is_forward) {
0364           h_ee_mass_id_pass_forward->Fill(ls, vDilep.M());
0365         } else {
0366           h_ee_mass_id_pass_central->Fill(ls, vDilep.M());
0367         }
0368       } else {
0369         if (probe_is_forward) {
0370           h_ee_mass_id_fail_forward->Fill(ls, vDilep.M());
0371         } else {
0372           h_ee_mass_id_fail_central->Fill(ls, vDilep.M());
0373         }
0374       }
0375 
0376       /// Fill for HLT efficiency
0377       if (probe_pass_id and probe_pass_trigger) {
0378         if (probe_is_forward) {
0379           h_ee_mass_HLT_pass_forward->Fill(ls, vDilep.M());
0380         } else {
0381           h_ee_mass_HLT_pass_central->Fill(ls, vDilep.M());
0382         }
0383       } else if (probe_pass_id) {
0384         if (probe_is_forward) {
0385           h_ee_mass_HLT_fail_forward->Fill(ls, vDilep.M());
0386         } else {
0387           h_ee_mass_HLT_fail_central->Fill(ls, vDilep.M());
0388         }
0389       }
0390     }  // End of probe loop
0391   }    //End of tag loop
0392 }
0393 
0394 //
0395 // -------------------------------------- functions --------------------------------------------
0396 //
0397 
0398 bool ZCountingElectrons::ele_probe_selection(double pt, double abseta) {
0399   if (pt < PtCutL2_)
0400     return false;
0401   if (abseta > EtaCutL2_)
0402     return false;
0403   if ((abseta > ELE_ETA_CRACK_LOW) and (abseta < ELE_ETA_CRACK_HIGH))
0404     return false;
0405   return true;
0406 }
0407 
0408 bool ZCountingElectrons::ele_tag_selection(double pt, double abseta) {
0409   if (pt < PtCutL1_)
0410     return false;
0411   if (abseta > EtaCutL1_)
0412     return false;
0413   if ((abseta > ELE_ETA_CRACK_LOW) and (abseta < ELE_ETA_CRACK_HIGH))
0414     return false;
0415   return true;
0416 }
0417 
0418 DEFINE_FWK_MODULE(ZCountingElectrons);