ElectronPATIdMVAProducer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "EgammaAnalysis/ElectronTools/interface/EGammaMvaEleEstimator.h"
#include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
#include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
#include "DataFormats/PatCandidates/interface/Electron.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "TrackingTools/Records/interface/TransientTrackRecord.h"
#include "TrackingTools/IPTools/interface/IPTools.h"
//
// class declaration
//

class ElectronPATIdMVAProducer : public edm::stream::EDProducer<> {
public:
  explicit ElectronPATIdMVAProducer(const edm::ParameterSet&);
  ~ElectronPATIdMVAProducer() override;

private:
  void produce(edm::Event&, const edm::EventSetup&) override;

  // ----------member data ---------------------------
  bool verbose_;
  edm::EDGetTokenT<pat::ElectronCollection> electronToken_;
  double _Rho;
  edm::EDGetTokenT<double> eventrhoToken_;
  std::string method_;
  std::vector<std::string> mvaWeightFiles_;

  EGammaMvaEleEstimator* mvaID_;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
ElectronPATIdMVAProducer::ElectronPATIdMVAProducer(const edm::ParameterSet& iConfig) {
  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
  electronToken_ = consumes<pat::ElectronCollection>(iConfig.getParameter<edm::InputTag>("electronTag"));
  method_ = iConfig.getParameter<std::string>("method");
  std::vector<std::string> fpMvaWeightFiles = iConfig.getParameter<std::vector<std::string> >("mvaWeightFile");
  eventrhoToken_ = consumes<double>(iConfig.getParameter<edm::InputTag>("Rho"));

  produces<edm::ValueMap<float> >();

  mvaID_ = new EGammaMvaEleEstimator();

  EGammaMvaEleEstimator::MVAType type_;

  type_ = EGammaMvaEleEstimator::kTrigNoIP;

  bool manualCat_ = true;

  std::string path_mvaWeightFileEleID;
  for (unsigned ifile = 0; ifile < fpMvaWeightFiles.size(); ++ifile) {
    path_mvaWeightFileEleID = edm::FileInPath(fpMvaWeightFiles[ifile].c_str()).fullPath();
    mvaWeightFiles_.push_back(path_mvaWeightFileEleID);
  }

  mvaID_->initialize(method_, type_, manualCat_, mvaWeightFiles_);
}

ElectronPATIdMVAProducer::~ElectronPATIdMVAProducer() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called on each new Event  ------------
void ElectronPATIdMVAProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
  std::unique_ptr<edm::ValueMap<float> > out(new edm::ValueMap<float>());

  edm::Handle<pat::ElectronCollection> egCollection;
  iEvent.getByToken(electronToken_, egCollection);
  const pat::ElectronCollection egCandidates = (*egCollection.product());

  _Rho = 0;
  edm::Handle<double> rhoPtr;
  //const edm::InputTag eventrhoToken_("kt6PFJets", "rho");
  iEvent.getByToken(eventrhoToken_, rhoPtr);
  _Rho = *rhoPtr;

  std::vector<float> values;
  values.reserve(egCollection->size());

  for (pat::ElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end(); ++egIter) {
    double mvaVal = -999999;

    mvaVal = mvaID_->mvaValue(*egIter, _Rho, verbose_);

    values.push_back(mvaVal);
  }

  edm::ValueMap<float>::Filler filler(*out);
  filler.insert(egCollection, values.begin(), values.end());
  filler.fill();

  iEvent.put(std::move(out));
}

//define this as a plug-in
DEFINE_FWK_MODULE(ElectronPATIdMVAProducer);