CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DPGAnalysis/Skims/src/WZInterestingEventSelector.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 //
00004 //
00005 //
00006 
00007 
00008 // system include files
00009 #include <memory>
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 #include <string>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 // user include files
00017 #include "FWCore/Framework/interface/Frameworkfwd.h"
00018 #include "FWCore/Framework/interface/EDFilter.h"
00019 
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/Framework/interface/MakerMacros.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 
00024 #include "FWCore/Utilities/interface/InputTag.h"
00025 
00026 #include "FWCore/Framework/interface/LuminosityBlock.h"
00027 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00028 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00029 #include "DataFormats/METReco/interface/CaloMETFwd.h"
00030 #include "DataFormats/METReco/interface/CaloMET.h"
00031 #include "DataFormats/METReco/interface/CaloMETCollection.h"
00032 #include "DataFormats/METReco/interface/PFMETFwd.h"
00033 #include "DataFormats/METReco/interface/PFMET.h"
00034 #include "DataFormats/METReco/interface/PFMETCollection.h"
00035 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
00036 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00037 
00038 #include "TLorentzVector.h"
00039 //
00040 // class declaration
00041 //
00042 
00043 using namespace reco;
00044 
00045 class WZInterestingEventSelector : public edm::EDFilter {
00046 public:
00047   struct event
00048   {
00049     long run;
00050     long event;
00051     long ls;
00052     int nEle;
00053     float maxPt;
00054     float maxPtEleEta;
00055     float maxPtElePhi;
00056     float invMass;
00057     float met;
00058     float metPhi;
00059   };
00060 
00061   explicit WZInterestingEventSelector(const edm::ParameterSet&);
00062   ~WZInterestingEventSelector();
00063   
00064 private:
00065   virtual bool filter(edm::Event&, const edm::EventSetup&);
00066   virtual void endJob(); 
00067   bool electronSelection( const GsfElectron* eleRef , math::XYZPoint bspotPosition);  
00068   // ----------member data ---------------------------
00069 
00070   //std::vector<event> interestingEvents_;  
00071 
00072   //Pt cut
00073   float ptCut_;
00074   int missHitCut_;
00075 
00076   //EB ID+ISO cuts
00077   float eb_trIsoCut_;
00078   float eb_ecalIsoCut_;
00079   float eb_hcalIsoCut_;
00080   float eb_hoeCut_;
00081   float eb_seeCut_;
00082 
00083   //EE ID+ISO cuts
00084   float ee_trIsoCut_;
00085   float ee_ecalIsoCut_;
00086   float ee_hcalIsoCut_;
00087   float ee_hoeCut_;
00088   float ee_seeCut_;
00089 
00090   //met Cut
00091   float metCut_;
00092 
00093   //invMass Cut
00094   float invMassCut_;
00095 
00096   edm::InputTag electronCollection_;
00097   edm::InputTag pfMetCollection_;
00098   edm::InputTag offlineBSCollection_;
00099 
00100 };
00101 
00102 //
00103 // constructors and destructor
00104 //
00105 WZInterestingEventSelector::WZInterestingEventSelector(const edm::ParameterSet& iConfig)
00106 {
00107   ptCut_   = iConfig.getParameter<double>("ptCut");
00108   missHitCut_ = iConfig.getParameter<int>("missHitsCut");
00109   
00110   eb_trIsoCut_   = iConfig.getParameter<double>("eb_trIsoCut");
00111   eb_ecalIsoCut_   = iConfig.getParameter<double>("eb_ecalIsoCut");
00112   eb_hcalIsoCut_   = iConfig.getParameter<double>("eb_hcalIsoCut");
00113   eb_hoeCut_   = iConfig.getParameter<double>("eb_hoeCut");
00114   eb_seeCut_   = iConfig.getParameter<double>("eb_seeCut");
00115 
00116   ee_trIsoCut_   = iConfig.getParameter<double>("ee_trIsoCut");
00117   ee_ecalIsoCut_   = iConfig.getParameter<double>("ee_ecalIsoCut");
00118   ee_hcalIsoCut_   = iConfig.getParameter<double>("ee_hcalIsoCut");
00119   ee_hoeCut_   = iConfig.getParameter<double>("ee_hoeCut");
00120   ee_seeCut_   = iConfig.getParameter<double>("ee_seeCut");
00121   
00122   metCut_ = iConfig.getParameter<double>("metCut");
00123   invMassCut_ = iConfig.getParameter<double>("invMassCut");
00124 
00125   electronCollection_ = iConfig.getUntrackedParameter<edm::InputTag>("electronCollection",edm::InputTag("gsfElectrons"));
00126   pfMetCollection_ = iConfig.getUntrackedParameter<edm::InputTag>("pfMetCollection",edm::InputTag("pfMet"));
00127   offlineBSCollection_ = iConfig.getUntrackedParameter<edm::InputTag>("offlineBSCollection",edm::InputTag("offlineBeamSpot"));
00128   
00129 }
00130 
00131 
00132 WZInterestingEventSelector::~WZInterestingEventSelector()
00133 {
00134  
00135    // do anything here that needs to be done at desctruction time
00136    // (e.g. close files, deallocate resources etc.)
00137 
00138 }
00139 
00140 
00141 //
00142 // member functions
00143 //
00144 
00145 // ------------ method called on each new Event  ------------
00146 
00147 void WZInterestingEventSelector::endJob() { 
00148 
00149 //   if (interestingEvents_.size()<1)
00150 //     return;
00151 
00152 //   std::ostringstream oss;
00153 //   for (unsigned int iEvent=0;iEvent<interestingEvents_.size();++iEvent)
00154 //     {
00155 //       oss << "==================================" << std::endl;
00156 //       oss << "Run: " << interestingEvents_[iEvent].run << " Event: " <<  interestingEvents_[iEvent].event << " LS: " <<  interestingEvents_[iEvent].ls << std::endl;
00157 //       oss << "nGoodEle: " << interestingEvents_[iEvent].nEle << " maxPt " << interestingEvents_[iEvent].maxPt <<  " maxPtEta " << interestingEvents_[iEvent].maxPtEleEta << " maxPtPhi " << interestingEvents_[iEvent].maxPtElePhi << std::endl;
00158 //       oss << "invMass " << interestingEvents_[iEvent].invMass << " met " << interestingEvents_[iEvent].met << " metPhi " << interestingEvents_[iEvent].metPhi << std::endl;
00159 //     }
00160 //   std::string mailText;
00161 //   mailText = oss.str();
00162 
00163 //   std::ofstream outputTxt;
00164 //   outputTxt.open("interestingEvents.txt");
00165 //   outputTxt << mailText;
00166 //   outputTxt.close();
00167 
00168   //Sending email
00169 //   std::ostringstream subject;
00170 //   subject << "Interesting events in Run#" << interestingEvents_[0].run; 
00171 
00172 //   std::ostringstream command;
00173 //   command << "cat interestingEvents.txt | mail -s \"" << subject.str() << "\" Paolo.Meridiani@cern.ch";
00174   
00175 //   std::string commandStr = command.str();
00176 //   char* pch = (char*)malloc( sizeof( char ) *(commandStr.length() +1) );
00177 //   string::traits_type::copy( pch, commandStr.c_str(), commandStr.length() +1 );
00178 //   int i=system(pch);
00179   
00180 }
00181 
00182 bool WZInterestingEventSelector::electronSelection( const GsfElectron* eleRef , math::XYZPoint bspotPosition )
00183 {
00184 
00185 
00186 //   if (eleRef->trackerDrivenSeed() && !eleRef->ecalDrivenSeed()) 
00187 //     return false;
00188   
00189 //   if (eleRef->ecalDrivenSeed())
00190 //     {
00191 
00192   if (eleRef->pt()<ptCut_) return false;
00193 
00194   if (eleRef->isEB())
00195     {
00196        if (eleRef->dr03TkSumPt()/eleRef->pt()>eb_trIsoCut_) return false;
00197        if (eleRef->dr03EcalRecHitSumEt()/eleRef->pt()>eb_ecalIsoCut_) return false;
00198        if (eleRef->dr03HcalTowerSumEt()/eleRef->pt()>eb_hcalIsoCut_) return false;
00199        if (eleRef->sigmaIetaIeta()>eb_seeCut_) return false;
00200        if (eleRef->hcalOverEcal()>eb_hoeCut_) return false;
00201     }
00202   else if (eleRef->isEE())
00203     {
00204       if (eleRef->dr03TkSumPt()/eleRef->pt()>ee_trIsoCut_) return false;
00205       if (eleRef->dr03EcalRecHitSumEt()/eleRef->pt()>ee_ecalIsoCut_) return false;
00206       if (eleRef->dr03HcalTowerSumEt()/eleRef->pt()>ee_hcalIsoCut_) return false;
00207       if (eleRef->sigmaIetaIeta()>ee_seeCut_) return false;
00208       if (eleRef->hcalOverEcal()>ee_hoeCut_) return false;
00209     }
00210   
00211   if (eleRef->gsfTrack()->trackerExpectedHitsInner().numberOfHits()>missHitCut_) return false;
00212   
00213   return true;
00214 }
00215 
00216 bool
00217 WZInterestingEventSelector::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) 
00218 {
00219   // using namespace edm;
00220   edm::Handle<reco::GsfElectronCollection> gsfElectrons;
00221   iEvent.getByLabel(electronCollection_,gsfElectrons);
00222 
00223 //   edm::Handle<reco::CaloMETCollection> caloMET;
00224 //   iEvent.getByLabel(edm::InputTag("met"), caloMET);  
00225 
00226   edm::Handle<reco::PFMETCollection> pfMET;
00227   iEvent.getByLabel(pfMetCollection_, pfMET);  
00228 
00229   edm::Handle<reco::BeamSpot> pBeamSpot;
00230   iEvent.getByLabel(offlineBSCollection_, pBeamSpot);
00231 
00232   const reco::BeamSpot *bspot = pBeamSpot.product();
00233   math::XYZPoint bspotPosition = bspot->position();
00234 
00235   std::vector<const GsfElectron*> goodElectrons;  
00236   float ptMax=-999.;
00237   const GsfElectron* ptMaxEle=0;
00238   for(reco::GsfElectronCollection::const_iterator myEle=gsfElectrons->begin();myEle!=gsfElectrons->end();++myEle)
00239     {
00240       //Apply a minimal isolated electron selection
00241       if (!electronSelection(&(*myEle),bspotPosition)) continue;
00242       goodElectrons.push_back(&(*myEle));
00243       if (myEle->pt() > ptMax )
00244         {
00245           ptMax =  myEle->pt();
00246           ptMaxEle = &(*myEle);
00247         }
00248     }
00249 
00250   event thisEvent;
00251   thisEvent.run=iEvent.run();
00252   thisEvent.event=iEvent.eventAuxiliary().event();
00253   thisEvent.ls=iEvent.getLuminosityBlock().luminosityBlock();
00254   thisEvent.nEle=goodElectrons.size();
00255 
00256   if (ptMaxEle)
00257     {
00258       thisEvent.maxPt=ptMax;
00259       thisEvent.maxPtEleEta=ptMaxEle->eta();
00260       thisEvent.maxPtElePhi=ptMaxEle->phi();
00261 
00262     }
00263 
00264   if (pfMET.isValid())
00265     {
00266       thisEvent.met=pfMET->begin()->et();
00267       thisEvent.metPhi=pfMET->begin()->phi();
00268     }
00269 
00270 
00271   float maxInv=-999.;
00272   TLorentzVector v1;
00273   if (ptMaxEle)
00274     v1.SetPtEtaPhiM(ptMaxEle->pt(),ptMaxEle->eta(),ptMaxEle->phi(),0);
00275   if (goodElectrons.size()>1)
00276     {
00277       for (unsigned int iEle=0; iEle<goodElectrons.size(); ++iEle)
00278         if (goodElectrons[iEle]!=ptMaxEle && (goodElectrons[iEle]->charge() * ptMaxEle->charge() == -1) )
00279           {
00280             TLorentzVector v2;
00281             v2.SetPtEtaPhiM(goodElectrons[iEle]->pt(),goodElectrons[iEle]->eta(),goodElectrons[iEle]->phi(),0.);
00282             if ( (v1+v2).M() > maxInv )
00283               maxInv = (v1+v2).M();
00284           }
00285     }
00286 
00287   if (maxInv > invMassCut_)
00288     thisEvent.invMass = maxInv;
00289 
00290   //Z filt: Retain event if more then 1 good ele and invMass above threshold (zee)
00291   if (goodElectrons.size()>1 && maxInv > invMassCut_)
00292     {
00293       //interestingEvents_.push_back(thisEvent);
00294       return true;
00295     }
00296 
00297   //W filt: Retain event also event with at least 1 good ele and some met
00298   if (goodElectrons.size()>=1 &&  (pfMET->begin()->et()>metCut_))
00299     {
00300       //interestingEvents_.push_back(thisEvent);
00301       return true;
00302     }
00303   
00304   return false;
00305 }
00306 
00307 //define this as a plug-in
00308 DEFINE_FWK_MODULE(WZInterestingEventSelector);