-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
DeepCMVATagInfoProducer.cc
211 lines (178 loc) · 8.76 KB
/
DeepCMVATagInfoProducer.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// -*- C++ -*-
//
// Package: RecoBTag/SecondaryVertex
// Class: DeepNNTagInfoProducer
//
/**\class DeepNNTagInfoProducer DeepNNTagInfoProducer.cc RecoBTag/DeepFlavour/plugins/DeepNNTagInfoProducer.cc
*
* Description: EDProducer that produces collection of DeepNNTagInfos
*
* Implementation:
* A collection of CandIPTagInfo and CandSecondaryVertexTagInfo and a CombinedSVComputer ESHandle is taken as input and a collection of DeepNNTagInfos
* is produced as output.
*/
//
// Original Author: Mauro Verzetti (U. Rochester)
//
//
// 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/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "FWCore/Utilities/interface/ESGetToken.h"
#include "FWCore/Framework/interface/makeRefToBaseProdFrom.h"
#include "DataFormats/Common/interface/RefToBase.h"
#include "DataFormats/Common/interface/View.h"
#include "DataFormats/BTauReco/interface/CandIPTagInfo.h"
#include "DataFormats/BTauReco/interface/CandSecondaryVertexTagInfo.h"
#include "DataFormats/BTauReco/interface/ShallowTagInfo.h"
#include "DataFormats/BTauReco/interface/TaggingVariable.h"
#include "RecoBTag/SecondaryVertex/interface/CombinedSVComputer.h"
#include "RecoBTau/JetTagComputer/interface/JetTagComputer.h"
#include "RecoBTau/JetTagComputer/interface/JetTagComputerRecord.h"
#include "DataFormats/BTauReco/interface/JetTag.h"
#include <cmath>
#include <map>
using namespace reco;
//
// class declaration
//
inline bool equals(const edm::RefToBase<Jet>& j1, const edm::RefToBase<Jet>& j2) {
return j1.id() == j2.id() && j1.key() == j2.key();
}
class DeepCMVATagInfoProducer : public edm::stream::EDProducer<> {
public:
explicit DeepCMVATagInfoProducer(const edm::ParameterSet&);
~DeepCMVATagInfoProducer() override;
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
private:
void beginStream(edm::StreamID) override {}
void produce(edm::Event&, const edm::EventSetup&) override;
void endStream() override {}
// ----------member data ---------------------------
const edm::EDGetTokenT<std::vector<reco::ShallowTagInfo> > deepNNSrc_;
const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > ipInfoSrc_;
const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > muInfoSrc_;
const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > elInfoSrc_;
std::string jpComputer_, jpbComputer_, softmuComputer_, softelComputer_;
double cMVAPtThreshold_;
edm::ESGetToken<JetTagComputer, JetTagComputerRecord> jpComputerToken_;
edm::ESGetToken<JetTagComputer, JetTagComputerRecord> jpbComputerToken_;
edm::ESGetToken<JetTagComputer, JetTagComputerRecord> softmuComputerToken_;
edm::ESGetToken<JetTagComputer, JetTagComputerRecord> softelComputerToken_;
};
//
// constants, enums and typedefs
//
//
// static data member definitions
//
//
// constructors and destructor
//
DeepCMVATagInfoProducer::DeepCMVATagInfoProducer(const edm::ParameterSet& iConfig)
: deepNNSrc_(consumes<std::vector<reco::ShallowTagInfo> >(iConfig.getParameter<edm::InputTag>("deepNNTagInfos"))),
ipInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("ipInfoSrc"))),
muInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("muInfoSrc"))),
elInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("elInfoSrc"))),
jpComputer_(iConfig.getParameter<std::string>("jpComputerSrc")),
jpbComputer_(iConfig.getParameter<std::string>("jpbComputerSrc")),
softmuComputer_(iConfig.getParameter<std::string>("softmuComputerSrc")),
softelComputer_(iConfig.getParameter<std::string>("softelComputerSrc")),
cMVAPtThreshold_(iConfig.getParameter<double>("cMVAPtThreshold")),
jpComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", jpComputer_))),
jpbComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", jpbComputer_))),
softmuComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", softmuComputer_))),
softelComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", softelComputer_))) {
produces<std::vector<reco::ShallowTagInfo> >();
}
DeepCMVATagInfoProducer::~DeepCMVATagInfoProducer() {
// do anything here that needs to be done at destruction time
// (e.g. close files, deallocate resources etc.)
}
//
// member functions
//
// ------------ method called to produce the data ------------
void DeepCMVATagInfoProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
// get input TagInfos from DeepCSV
edm::Handle<std::vector<reco::ShallowTagInfo> > nnInfos;
iEvent.getByToken(deepNNSrc_, nnInfos);
// get other Taginfos
edm::Handle<edm::View<BaseTagInfo> > ipInfos;
iEvent.getByToken(ipInfoSrc_, ipInfos);
edm::Handle<edm::View<BaseTagInfo> > muInfos;
iEvent.getByToken(muInfoSrc_, muInfos);
edm::Handle<edm::View<BaseTagInfo> > elInfos;
iEvent.getByToken(elInfoSrc_, elInfos);
//get computers
edm::ESHandle<JetTagComputer> jp = iSetup.getHandle(jpComputerToken_);
const JetTagComputer* compjp = jp.product();
edm::ESHandle<JetTagComputer> jpb = iSetup.getHandle(jpbComputerToken_);
const JetTagComputer* compjpb = jpb.product();
edm::ESHandle<JetTagComputer> softmu = iSetup.getHandle(softmuComputerToken_);
const JetTagComputer* compsoftmu = softmu.product();
edm::ESHandle<JetTagComputer> softel = iSetup.getHandle(softelComputerToken_);
const JetTagComputer* compsoftel = softel.product();
// create the output collection
auto tagInfos = std::make_unique<std::vector<reco::ShallowTagInfo> >();
// loop over TagInfos, assume they are ordered in the same way, check later and throw exception if not
for (size_t idx = 0; idx < nnInfos->size(); ++idx) {
auto& nnInfo = nnInfos->at(idx);
auto& ipInfo = ipInfos->at(idx);
auto& muInfo = muInfos->at(idx);
auto& elInfo = elInfos->at(idx);
if (!equals(nnInfo.jet(), ipInfo.jet()) || !equals(nnInfo.jet(), muInfo.jet()) ||
!equals(nnInfo.jet(), elInfo.jet())) {
throw cms::Exception("ValueError")
<< "DeepNNTagInfoProducer::produce: The tagInfos taken belong to different jets!" << std::endl
<< "This could be due to: " << std::endl
<< " - You passed tagInfos computed on different jet collection" << std::endl
<< " - The assumption that the tagInfos are filled in the same order is actually wrong" << std::endl;
}
// Make vector of BaseTagInfo, needed for TagInfoHelper
std::vector<const BaseTagInfo*> ipBaseInfo;
ipBaseInfo.push_back(&ipInfo);
std::vector<const BaseTagInfo*> muBaseInfo;
muBaseInfo.push_back(&muInfo);
std::vector<const BaseTagInfo*> elBaseInfo;
elBaseInfo.push_back(&elInfo);
// Copy the DeepNN TaggingVariables + add the other discriminators
TaggingVariableList vars = nnInfo.taggingVariables();
float softmu_discr = (*compsoftmu)(JetTagComputer::TagInfoHelper(muBaseInfo));
float softel_discr = (*compsoftel)(JetTagComputer::TagInfoHelper(elBaseInfo));
float jp_discr = (*compjp)(JetTagComputer::TagInfoHelper(ipBaseInfo));
float jpb_discr = (*compjpb)(JetTagComputer::TagInfoHelper(ipBaseInfo));
//if jetPt larger than cMVAPtThreshold_ --> default these taggers for easier SF measurements
if ((nnInfo.jet().get())->pt() < cMVAPtThreshold_) {
vars.insert(reco::btau::Jet_SoftMu, !(std::isinf(softmu_discr)) ? softmu_discr : -0.2, true);
vars.insert(reco::btau::Jet_SoftEl, !(std::isinf(softel_discr)) ? softel_discr : -0.2, true);
vars.insert(reco::btau::Jet_JBP, !(std::isinf(jpb_discr)) ? jpb_discr : -0.2, true);
vars.insert(reco::btau::Jet_JP, !(std::isinf(jp_discr)) ? jp_discr : -0.2, true);
}
vars.finalize();
tagInfos->emplace_back(vars, nnInfo.jet());
// just to be sure, clear all containers
ipBaseInfo.clear();
muBaseInfo.clear();
elBaseInfo.clear();
}
// put the output in the event
iEvent.put(std::move(tagInfos));
}
// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
void DeepCMVATagInfoProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
//The following says we do not know what parameters are allowed so do no validation
// Please change this to state exactly what you do use, even if it is no parameters
edm::ParameterSetDescription desc;
desc.setUnknown();
descriptions.addDefault(desc);
}
//define this as a plug-in
DEFINE_FWK_MODULE(DeepCMVATagInfoProducer);