Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:16

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/stream/EDProducer.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "EgammaAnalysis/ElectronTools/interface/EGammaMvaEleEstimator.h"
0013 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0014 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0015 #include "DataFormats/PatCandidates/interface/Electron.h"
0016 #include "DataFormats/VertexReco/interface/Vertex.h"
0017 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0018 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0019 #include "TrackingTools/IPTools/interface/IPTools.h"
0020 //
0021 // class declaration
0022 //
0023 
0024 class ElectronPATIdMVAProducer : public edm::stream::EDProducer<> {
0025 public:
0026   explicit ElectronPATIdMVAProducer(const edm::ParameterSet&);
0027   ~ElectronPATIdMVAProducer() override;
0028 
0029 private:
0030   void produce(edm::Event&, const edm::EventSetup&) override;
0031 
0032   // ----------member data ---------------------------
0033   bool verbose_;
0034   edm::EDGetTokenT<pat::ElectronCollection> electronToken_;
0035   double _Rho;
0036   edm::EDGetTokenT<double> eventrhoToken_;
0037   std::string method_;
0038   std::vector<std::string> mvaWeightFiles_;
0039 
0040   EGammaMvaEleEstimator* mvaID_;
0041 };
0042 
0043 //
0044 // constants, enums and typedefs
0045 //
0046 
0047 //
0048 // static data member definitions
0049 //
0050 
0051 //
0052 // constructors and destructor
0053 //
0054 ElectronPATIdMVAProducer::ElectronPATIdMVAProducer(const edm::ParameterSet& iConfig) {
0055   verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
0056   electronToken_ = consumes<pat::ElectronCollection>(iConfig.getParameter<edm::InputTag>("electronTag"));
0057   method_ = iConfig.getParameter<std::string>("method");
0058   std::vector<std::string> fpMvaWeightFiles = iConfig.getParameter<std::vector<std::string> >("mvaWeightFile");
0059   eventrhoToken_ = consumes<double>(iConfig.getParameter<edm::InputTag>("Rho"));
0060 
0061   produces<edm::ValueMap<float> >();
0062 
0063   mvaID_ = new EGammaMvaEleEstimator();
0064 
0065   EGammaMvaEleEstimator::MVAType type_;
0066 
0067   type_ = EGammaMvaEleEstimator::kTrigNoIP;
0068 
0069   bool manualCat_ = true;
0070 
0071   std::string path_mvaWeightFileEleID;
0072   for (unsigned ifile = 0; ifile < fpMvaWeightFiles.size(); ++ifile) {
0073     path_mvaWeightFileEleID = edm::FileInPath(fpMvaWeightFiles[ifile].c_str()).fullPath();
0074     mvaWeightFiles_.push_back(path_mvaWeightFileEleID);
0075   }
0076 
0077   mvaID_->initialize(method_, type_, manualCat_, mvaWeightFiles_);
0078 }
0079 
0080 ElectronPATIdMVAProducer::~ElectronPATIdMVAProducer() {
0081   // do anything here that needs to be done at desctruction time
0082   // (e.g. close files, deallocate resources etc.)
0083 }
0084 
0085 //
0086 // member functions
0087 //
0088 
0089 // ------------ method called on each new Event  ------------
0090 void ElectronPATIdMVAProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0091   std::unique_ptr<edm::ValueMap<float> > out(new edm::ValueMap<float>());
0092 
0093   edm::Handle<pat::ElectronCollection> egCollection;
0094   iEvent.getByToken(electronToken_, egCollection);
0095   const pat::ElectronCollection egCandidates = (*egCollection.product());
0096 
0097   _Rho = 0;
0098   edm::Handle<double> rhoPtr;
0099   //const edm::InputTag eventrhoToken_("kt6PFJets", "rho");
0100   iEvent.getByToken(eventrhoToken_, rhoPtr);
0101   _Rho = *rhoPtr;
0102 
0103   std::vector<float> values;
0104   values.reserve(egCollection->size());
0105 
0106   for (pat::ElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end(); ++egIter) {
0107     double mvaVal = -999999;
0108 
0109     mvaVal = mvaID_->mvaValue(*egIter, _Rho, verbose_);
0110 
0111     values.push_back(mvaVal);
0112   }
0113 
0114   edm::ValueMap<float>::Filler filler(*out);
0115   filler.insert(egCollection, values.begin(), values.end());
0116   filler.fill();
0117 
0118   iEvent.put(std::move(out));
0119 }
0120 
0121 //define this as a plug-in
0122 DEFINE_FWK_MODULE(ElectronPATIdMVAProducer);