Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-05 07:05:21

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/EDFilter.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 ElectronIdMVAProducer : public edm::stream::EDFilter<> {
0025 public:
0026   explicit ElectronIdMVAProducer(const edm::ParameterSet&);
0027   ~ElectronIdMVAProducer() override;
0028 
0029 private:
0030   bool filter(edm::Event&, const edm::EventSetup&) override;
0031 
0032   // ----------member data ---------------------------
0033   bool verbose_;
0034   edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
0035   edm::EDGetTokenT<reco::GsfElectronCollection> electronToken_;
0036   edm::EDGetTokenT<double> eventrhoToken_;
0037   edm::EDGetTokenT<EcalRecHitCollection> reducedEBRecHitCollectionToken_;
0038   edm::EDGetTokenT<EcalRecHitCollection> reducedEERecHitCollectionToken_;
0039   const EcalClusterLazyTools::ESGetTokens ecalClusterToolsESGetTokens_;
0040   edm::ESGetToken<TransientTrackBuilder, TransientTrackRecord> ttrackbuilderToken_;
0041 
0042   double _Rho;
0043   std::string method_;
0044   std::vector<std::string> mvaWeightFiles_;
0045   bool Trig_;
0046   bool NoIP_;
0047 
0048   EGammaMvaEleEstimator* mvaID_;
0049 };
0050 
0051 //
0052 // constants, enums and typedefs
0053 //
0054 
0055 //
0056 // static data member definitions
0057 //
0058 
0059 //
0060 // constructors and destructor
0061 //
0062 ElectronIdMVAProducer::ElectronIdMVAProducer(const edm::ParameterSet& iConfig)
0063     : ecalClusterToolsESGetTokens_{consumesCollector()} {
0064   verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
0065   vertexToken_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexTag"));
0066   electronToken_ = consumes<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("electronTag"));
0067   eventrhoToken_ = consumes<double>(edm::InputTag("kt6PFJets", "rho"));
0068   reducedEBRecHitCollectionToken_ =
0069       consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedEBRecHitCollection"));
0070   reducedEERecHitCollectionToken_ =
0071       consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedEERecHitCollection"));
0072   method_ = iConfig.getParameter<std::string>("method");
0073   std::vector<std::string> fpMvaWeightFiles = iConfig.getParameter<std::vector<std::string> >("mvaWeightFile");
0074   Trig_ = iConfig.getParameter<bool>("Trig");
0075   NoIP_ = iConfig.getParameter<bool>("NoIP");
0076 
0077   ttrackbuilderToken_ = esConsumes(edm::ESInputTag("", "TransientTrackBuilder"));
0078 
0079   produces<edm::ValueMap<float> >("");
0080 
0081   mvaID_ = new EGammaMvaEleEstimator();
0082 
0083   EGammaMvaEleEstimator::MVAType type_;
0084   if (Trig_ && !NoIP_) {
0085     type_ = EGammaMvaEleEstimator::kTrig;
0086   }
0087 
0088   if (Trig_ && NoIP_) {
0089     type_ = EGammaMvaEleEstimator::kTrigNoIP;
0090   }
0091 
0092   if (!Trig_) {
0093     type_ = EGammaMvaEleEstimator::kNonTrig;
0094   }
0095 
0096   bool manualCat_ = true;
0097 
0098   std::string path_mvaWeightFileEleID;
0099   for (unsigned ifile = 0; ifile < fpMvaWeightFiles.size(); ++ifile) {
0100     path_mvaWeightFileEleID = edm::FileInPath(fpMvaWeightFiles[ifile].c_str()).fullPath();
0101     mvaWeightFiles_.push_back(path_mvaWeightFileEleID);
0102   }
0103 
0104   mvaID_->initialize(method_, type_, manualCat_, mvaWeightFiles_);
0105 }
0106 
0107 ElectronIdMVAProducer::~ElectronIdMVAProducer() {
0108   // do anything here that needs to be done at desctruction time
0109   // (e.g. close files, deallocate resources etc.)
0110 }
0111 
0112 //
0113 // member functions
0114 //
0115 
0116 // ------------ method called on each new Event  ------------
0117 bool ElectronIdMVAProducer::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0118   std::unique_ptr<edm::ValueMap<float> > out(new edm::ValueMap<float>());
0119 
0120   edm::Handle<reco::VertexCollection> vertexCollection;
0121   iEvent.getByToken(vertexToken_, vertexCollection);
0122 
0123   reco::Vertex dummy;
0124   const reco::Vertex* pv = &dummy;
0125   if (!vertexCollection->empty()) {
0126     pv = &*vertexCollection->begin();
0127   } else {  // create a dummy PV
0128     reco::Vertex::Error e;
0129     e(0, 0) = 0.0015 * 0.0015;
0130     e(1, 1) = 0.0015 * 0.0015;
0131     e(2, 2) = 15. * 15.;
0132     reco::Vertex::Point p(0, 0, 0);
0133     dummy = reco::Vertex(p, e, 0, 0, 0);
0134   }
0135 
0136   EcalClusterLazyTools lazyTools(iEvent,
0137                                  ecalClusterToolsESGetTokens_.get(iSetup),
0138                                  reducedEBRecHitCollectionToken_,
0139                                  reducedEERecHitCollectionToken_);
0140   auto const& thebuilder = iSetup.getData(ttrackbuilderToken_);
0141 
0142   edm::Handle<reco::GsfElectronCollection> egCollection;
0143   iEvent.getByToken(electronToken_, egCollection);
0144   const reco::GsfElectronCollection egCandidates = (*egCollection.product());
0145 
0146   _Rho = 0;
0147   edm::Handle<double> rhoPtr;
0148   iEvent.getByToken(eventrhoToken_, rhoPtr);
0149   _Rho = *rhoPtr;
0150 
0151   std::vector<float> values;
0152   values.reserve(egCollection->size());
0153 
0154   for (reco::GsfElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end();
0155        ++egIter) {
0156     double mvaVal = -999999;
0157     if (!NoIP_) {
0158       mvaVal = mvaID_->mvaValue(*egIter, *pv, thebuilder, lazyTools, verbose_);
0159     }
0160     if (NoIP_) {
0161       mvaVal = mvaID_->mvaValue(*egIter, *pv, _Rho, /*thebuilder,*/ lazyTools, verbose_);
0162     }
0163 
0164     values.push_back(mvaVal);
0165   }
0166 
0167   edm::ValueMap<float>::Filler filler(*out);
0168   filler.insert(egCollection, values.begin(), values.end());
0169   filler.fill();
0170 
0171   iEvent.put(std::move(out));
0172 
0173   return true;
0174 }
0175 
0176 //define this as a plug-in
0177 DEFINE_FWK_MODULE(ElectronIdMVAProducer);