CMS 3D CMS Logo

ElectronIdMVAProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
7 
10 
20 //
21 // class declaration
22 //
23 
25 public:
27  ~ElectronIdMVAProducer() override;
28 
29 private:
30  bool filter(edm::Event&, const edm::EventSetup&) override;
31 
32  // ----------member data ---------------------------
33  bool verbose_;
41 
42  double _Rho;
44  std::vector<std::string> mvaWeightFiles_;
45  bool Trig_;
46  bool NoIP_;
47 
49 };
50 
51 //
52 // constants, enums and typedefs
53 //
54 
55 //
56 // static data member definitions
57 //
58 
59 //
60 // constructors and destructor
61 //
63  : ecalClusterToolsESGetTokens_{consumesCollector()} {
64  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
65  vertexToken_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexTag"));
66  electronToken_ = consumes<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("electronTag"));
67  eventrhoToken_ = consumes<double>(edm::InputTag("kt6PFJets", "rho"));
68  reducedEBRecHitCollectionToken_ =
69  consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedEBRecHitCollection"));
70  reducedEERecHitCollectionToken_ =
71  consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedEERecHitCollection"));
72  method_ = iConfig.getParameter<std::string>("method");
73  std::vector<std::string> fpMvaWeightFiles = iConfig.getParameter<std::vector<std::string> >("mvaWeightFile");
74  Trig_ = iConfig.getParameter<bool>("Trig");
75  NoIP_ = iConfig.getParameter<bool>("NoIP");
76 
77  ttrackbuilderToken_ = esConsumes(edm::ESInputTag("", "TransientTrackBuilder"));
78 
79  produces<edm::ValueMap<float> >("");
80 
81  mvaID_ = new EGammaMvaEleEstimator();
82 
84  if (Trig_ && !NoIP_) {
86  }
87 
88  if (Trig_ && NoIP_) {
90  }
91 
92  if (!Trig_) {
94  }
95 
96  bool manualCat_ = true;
97 
98  std::string path_mvaWeightFileEleID;
99  for (unsigned ifile = 0; ifile < fpMvaWeightFiles.size(); ++ifile) {
100  path_mvaWeightFileEleID = edm::FileInPath(fpMvaWeightFiles[ifile].c_str()).fullPath();
101  mvaWeightFiles_.push_back(path_mvaWeightFileEleID);
102  }
103 
104  mvaID_->initialize(method_, type_, manualCat_, mvaWeightFiles_);
105 }
106 
108  // do anything here that needs to be done at desctruction time
109  // (e.g. close files, deallocate resources etc.)
110 }
111 
112 //
113 // member functions
114 //
115 
116 // ------------ method called on each new Event ------------
118  std::unique_ptr<edm::ValueMap<float> > out(new edm::ValueMap<float>());
119 
121  iEvent.getByToken(vertexToken_, vertexCollection);
122 
124  const reco::Vertex* pv = &dummy;
125  if (!vertexCollection->empty()) {
126  pv = &*vertexCollection->begin();
127  } else { // create a dummy PV
129  e(0, 0) = 0.0015 * 0.0015;
130  e(1, 1) = 0.0015 * 0.0015;
131  e(2, 2) = 15. * 15.;
132  reco::Vertex::Point p(0, 0, 0);
133  dummy = reco::Vertex(p, e, 0, 0, 0);
134  }
135 
136  EcalClusterLazyTools lazyTools(iEvent,
140  auto const& thebuilder = iSetup.getData(ttrackbuilderToken_);
141 
143  iEvent.getByToken(electronToken_, egCollection);
144  const reco::GsfElectronCollection egCandidates = (*egCollection.product());
145 
146  _Rho = 0;
147  edm::Handle<double> rhoPtr;
148  iEvent.getByToken(eventrhoToken_, rhoPtr);
149  _Rho = *rhoPtr;
150 
151  std::vector<float> values;
152  values.reserve(egCollection->size());
153 
154  for (reco::GsfElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end();
155  ++egIter) {
156  double mvaVal = -999999;
157  if (!NoIP_) {
158  mvaVal = mvaID_->mvaValue(*egIter, *pv, thebuilder, lazyTools, verbose_);
159  }
160  if (NoIP_) {
161  mvaVal = mvaID_->mvaValue(*egIter, *pv, _Rho, /*thebuilder,*/ lazyTools, verbose_);
162  }
163 
164  values.push_back(mvaVal);
165  }
166 
168  filler.insert(egCollection, values.begin(), values.end());
169  filler.fill();
170 
171  iEvent.put(std::move(out));
172 
173  return true;
174 }
175 
176 //define this as a plug-in
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::EDGetTokenT< double > eventrhoToken_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
std::string fullPath() const
Definition: FileInPath.cc:161
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > ttrackbuilderToken_
edm::EDGetTokenT< EcalRecHitCollection > reducedEBRecHitCollectionToken_
T const * product() const
Definition: Handle.h:70
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:45
Double_t mvaValue(const reco::GsfElectron &ele, const reco::Vertex &vertex, const TransientTrackBuilder &transientTrackBuilder, EcalClusterLazyTools const &myEcalCluster, bool printDebug=kFALSE)
edm::EDGetTokenT< EcalRecHitCollection > reducedEERecHitCollectionToken_
std::vector< GsfElectron > GsfElectronCollection
collection of GsfElectron objects
bool filter(edm::Event &, const edm::EventSetup &) override
ESData get(edm::EventSetup const &eventSetup) const
int iEvent
Definition: GenABIO.cc:224
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
math::XYZPoint Point
point in the space
Definition: Vertex.h:40
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< reco::VertexCollection > vertexToken_
std::vector< std::string > mvaWeightFiles_
ElectronIdMVAProducer(const edm::ParameterSet &)
edm::EDGetTokenT< reco::GsfElectronCollection > electronToken_
const EcalClusterLazyTools::ESGetTokens ecalClusterToolsESGetTokens_
def move(src, dest)
Definition: eostools.py:511
EGammaMvaEleEstimator * mvaID_