00001 // -*- C++ -*- 00002 // 00003 // Package: ElectronIdMVABased 00004 // Class: ElectronIdMVABased 00005 // 00013 // 00014 // Original Author: Zablocki Jakub 00015 // Created: Thu Feb 9 10:47:50 CST 2012 00016 // $Id: ElectronIdMVABased.cc,v 1.4 2012/02/24 14:09:02 benedet Exp $ 00017 // 00018 // 00019 00020 00021 // system include files 00022 #include <memory> 00023 00024 // user include files 00025 #include "FWCore/Framework/interface/Frameworkfwd.h" 00026 #include "FWCore/Framework/interface/EDFilter.h" 00027 00028 #include "FWCore/Framework/interface/Event.h" 00029 #include "FWCore/Framework/interface/MakerMacros.h" 00030 00031 #include "FWCore/ParameterSet/interface/ParameterSet.h" 00032 #include "RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimator.h" 00033 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h" 00034 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h" 00035 #include "DataFormats/VertexReco/interface/Vertex.h" 00036 #include "DataFormats/VertexReco/interface/VertexFwd.h" 00037 // 00038 // class declaration 00039 // 00040 00041 using namespace std; 00042 using namespace reco; 00043 class ElectronIdMVABased : public edm::EDFilter { 00044 public: 00045 explicit ElectronIdMVABased(const edm::ParameterSet&); 00046 ~ElectronIdMVABased(); 00047 00048 private: 00049 virtual bool filter(edm::Event&, const edm::EventSetup&); 00050 00051 00052 // ----------member data --------------------------- 00053 edm::InputTag vertexTag; 00054 edm::InputTag electronTag; 00055 string mvaWeightFileEleID; 00056 string path_mvaWeightFileEleID; 00057 double thresholdBarrel; 00058 double thresholdEndcap; 00059 double thresholdIsoBarrel; 00060 double thresholdIsoEndcap; 00061 00062 ElectronMVAEstimator *mvaID_; 00063 }; 00064 00065 // 00066 // constants, enums and typedefs 00067 // 00068 00069 // 00070 // static data member definitions 00071 // 00072 00073 // 00074 // constructors and destructor 00075 // 00076 ElectronIdMVABased::ElectronIdMVABased(const edm::ParameterSet& iConfig) { 00077 vertexTag = iConfig.getParameter<edm::InputTag>("vertexTag"); 00078 electronTag = iConfig.getParameter<edm::InputTag>("electronTag"); 00079 mvaWeightFileEleID = iConfig.getParameter<string>("HZZmvaWeightFile"); 00080 thresholdBarrel = iConfig.getParameter<double>("thresholdBarrel"); 00081 thresholdEndcap = iConfig.getParameter<double>("thresholdEndcap"); 00082 thresholdIsoBarrel = iConfig.getParameter<double>("thresholdIsoDR03Barrel"); 00083 thresholdIsoEndcap = iConfig.getParameter<double>("thresholdIsoDR03Endcap"); 00084 00085 produces<reco::GsfElectronCollection>(); 00086 path_mvaWeightFileEleID = edm::FileInPath ( mvaWeightFileEleID.c_str() ).fullPath(); 00087 FILE * fileEleID = fopen(path_mvaWeightFileEleID.c_str(), "r"); 00088 if (fileEleID) { 00089 fclose(fileEleID); 00090 } 00091 else { 00092 string err = "ElectronIdMVABased: cannot open weight file '"; 00093 err += path_mvaWeightFileEleID; 00094 err += "'"; 00095 throw invalid_argument( err ); 00096 } 00097 00098 mvaID_ = new ElectronMVAEstimator(path_mvaWeightFileEleID); 00099 } 00100 00101 00102 ElectronIdMVABased::~ElectronIdMVABased() 00103 { 00104 00105 delete mvaID_; 00106 // do anything here that needs to be done at desctruction time 00107 // (e.g. close files, deallocate resources etc.) 00108 00109 } 00110 00111 00112 // 00113 // member functions 00114 // 00115 00116 // ------------ method called on each new Event ------------ 00117 bool ElectronIdMVABased::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) { 00118 using namespace edm; 00119 00120 std::auto_ptr<reco::GsfElectronCollection> mvaElectrons(new reco::GsfElectronCollection); 00121 00122 Handle<reco::VertexCollection> vertexCollection; 00123 iEvent.getByLabel(vertexTag, vertexCollection); 00124 int nVtx = vertexCollection->size(); 00125 00126 Handle<reco::GsfElectronCollection> egCollection; 00127 iEvent.getByLabel(electronTag,egCollection); 00128 const reco::GsfElectronCollection egCandidates = (*egCollection.product()); 00129 for ( reco::GsfElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end(); ++egIter) { 00130 double mvaVal = mvaID_->mva( *egIter, nVtx ); 00131 double isoDr03 = egIter->dr03TkSumPt() + egIter->dr03EcalRecHitSumEt() + egIter->dr03HcalTowerSumEt(); 00132 double eleEta = fabs(egIter->eta()); 00133 if (eleEta <= 1.485 && mvaVal > thresholdBarrel && isoDr03 < thresholdIsoBarrel) { 00134 mvaElectrons->push_back( *egIter ); 00135 reco::GsfElectron::MvaOutput myMvaOutput; 00136 myMvaOutput.mva = mvaVal; 00137 mvaElectrons->back().setMvaOutput(myMvaOutput); 00138 } 00139 else if (eleEta > 1.485 && mvaVal > thresholdEndcap && isoDr03 < thresholdIsoEndcap) { 00140 mvaElectrons->push_back( *egIter ); 00141 reco::GsfElectron::MvaOutput myMvaOutput; 00142 myMvaOutput.mva = mvaVal; 00143 mvaElectrons->back().setMvaOutput(myMvaOutput); 00144 } 00145 00146 00147 } 00148 00149 00150 iEvent.put(mvaElectrons); 00151 00152 return true; 00153 } 00154 00155 //define this as a plug-in 00156 DEFINE_FWK_MODULE(ElectronIdMVABased);