CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/EgammaAnalysis/ElectronTools/plugins/ElectronIdMVAProducer.cc

Go to the documentation of this file.
00001 // system include files
00002 #include <memory>
00003 
00004 // user include files
00005 #include "FWCore/Framework/interface/Frameworkfwd.h"
00006 #include "FWCore/Framework/interface/EDFilter.h"
00007 
00008 #include "FWCore/Framework/interface/Event.h"
00009 #include "FWCore/Framework/interface/MakerMacros.h"
00010 
00011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00012 #include "EgammaAnalysis/ElectronTools/interface/EGammaMvaEleEstimator.h"
00013 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00014 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00015 #include "DataFormats/PatCandidates/interface/Electron.h"
00016 #include "DataFormats/VertexReco/interface/Vertex.h"
00017 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00018 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00019 #include "TrackingTools/IPTools/interface/IPTools.h"
00020 //
00021 // class declaration
00022 //
00023 
00024 using namespace std;
00025 using namespace reco;
00026 class ElectronIdMVAProducer : public edm::EDFilter {
00027         public:
00028                 explicit ElectronIdMVAProducer(const edm::ParameterSet&);
00029                 ~ElectronIdMVAProducer();
00030 
00031         private:
00032                 virtual bool filter(edm::Event&, const edm::EventSetup&);
00033 
00034                 // ----------member data ---------------------------
00035                 bool verbose_;
00036                 edm::InputTag vertexTag_;
00037                 edm::InputTag electronTag_;
00038                 edm::InputTag reducedEBRecHitCollection_;
00039                 edm::InputTag reducedEERecHitCollection_;
00040   
00041                 double _Rho;
00042                 string method_;
00043                 vector<string> mvaWeightFiles_;
00044                 bool Trig_;
00045                 bool NoIP_;
00046  
00047                 EGammaMvaEleEstimator* mvaID_;
00048 
00049 };
00050 
00051 //
00052 // constants, enums and typedefs
00053 //
00054 
00055 //
00056 // static data member definitions
00057 //
00058 
00059 //
00060 // constructors and destructor
00061 //
00062 ElectronIdMVAProducer::ElectronIdMVAProducer(const edm::ParameterSet& iConfig) {
00063         verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
00064         vertexTag_ = iConfig.getParameter<edm::InputTag>("vertexTag");
00065         electronTag_ = iConfig.getParameter<edm::InputTag>("electronTag");
00066         reducedEBRecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedEBRecHitCollection");
00067         reducedEERecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedEERecHitCollection");
00068         method_ = iConfig.getParameter<string>("method");
00069         std::vector<string> fpMvaWeightFiles = iConfig.getParameter<std::vector<std::string> >("mvaWeightFile");
00070         Trig_ = iConfig.getParameter<bool>("Trig");
00071         NoIP_ = iConfig.getParameter<bool>("NoIP");
00072 
00073         produces<edm::ValueMap<float> >("");
00074 
00075         mvaID_ = new EGammaMvaEleEstimator();
00076  
00077         EGammaMvaEleEstimator::MVAType type_;
00078         if(Trig_ && !NoIP_){type_ = EGammaMvaEleEstimator::kTrig;}
00079          
00080         if(Trig_ && NoIP_){type_ = EGammaMvaEleEstimator::kTrigNoIP;}
00081          
00082         if(!Trig_){type_ = EGammaMvaEleEstimator::kNonTrig;}
00083 
00084         bool manualCat_ = true;
00085 
00086         string path_mvaWeightFileEleID;
00087         for(unsigned ifile=0 ; ifile < fpMvaWeightFiles.size() ; ++ifile) {
00088           path_mvaWeightFileEleID = edm::FileInPath ( fpMvaWeightFiles[ifile].c_str() ).fullPath();
00089           mvaWeightFiles_.push_back(path_mvaWeightFileEleID);
00090         }
00091         
00092         mvaID_->initialize(method_, type_, manualCat_, mvaWeightFiles_);
00093 
00094 }
00095 
00096 
00097 ElectronIdMVAProducer::~ElectronIdMVAProducer()
00098 {
00099  
00100    // do anything here that needs to be done at desctruction time
00101    // (e.g. close files, deallocate resources etc.)
00102 
00103 }
00104 
00105 
00106 //
00107 // member functions
00108 //
00109 
00110 // ------------ method called on each new Event  ------------
00111 bool ElectronIdMVAProducer::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00112         using namespace edm;
00113 
00114         std::auto_ptr<edm::ValueMap<float> > out(new edm::ValueMap<float>() );
00115 
00116         Handle<reco::VertexCollection>  vertexCollection;
00117         iEvent.getByLabel(vertexTag_, vertexCollection);
00118 
00119         Vertex dummy;
00120         const Vertex *pv = &dummy;
00121         if ( vertexCollection->size() != 0) {
00122           pv = &*vertexCollection->begin();
00123         } else { // create a dummy PV
00124           Vertex::Error e;
00125           e(0, 0) = 0.0015 * 0.0015;
00126           e(1, 1) = 0.0015 * 0.0015;
00127           e(2, 2) = 15. * 15.;
00128           Vertex::Point p(0, 0, 0);
00129           dummy = Vertex(p, e, 0, 0, 0);
00130         }
00131 
00132         EcalClusterLazyTools lazyTools(iEvent, iSetup, reducedEBRecHitCollection_, reducedEERecHitCollection_);
00133 
00134         edm::ESHandle<TransientTrackBuilder> builder;
00135         iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", builder);
00136         TransientTrackBuilder thebuilder = *(builder.product());
00137 
00138         Handle<reco::GsfElectronCollection> egCollection;
00139         iEvent.getByLabel(electronTag_,egCollection);
00140         const reco::GsfElectronCollection egCandidates = (*egCollection.product());
00141 
00142         _Rho=0;
00143         edm::Handle<double> rhoPtr;
00144         const edm::InputTag eventrho("kt6PFJets", "rho");
00145         iEvent.getByLabel(eventrho,rhoPtr);
00146         _Rho=*rhoPtr;
00147 
00148         std::vector<float> values;
00149         values.reserve(egCollection->size());
00150    
00151         for ( reco::GsfElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end(); ++egIter) {
00152 
00153           double mvaVal = -999999;
00154           if(!NoIP_){
00155           mvaVal = mvaID_->mvaValue( *egIter, *pv,thebuilder,lazyTools, verbose_);
00156           }
00157           if(NoIP_){
00158             mvaVal = mvaID_->mvaValue( *egIter, *pv, _Rho,/*thebuilder,*/lazyTools, verbose_);
00159           }
00160           
00161           values.push_back( mvaVal ); 
00162         }
00163 
00164         edm::ValueMap<float>::Filler filler(*out);
00165         filler.insert(egCollection, values.begin(), values.end() );
00166         filler.fill();
00167 
00168         iEvent.put(out);
00169 
00170         return true;
00171 }
00172 
00173 //define this as a plug-in
00174 DEFINE_FWK_MODULE(ElectronIdMVAProducer);