15 for(
unsigned int i = 0;
i < pfcands->size(); ++
i) {
27 const size_t tau_index = 0;
34 static const std::map<unsigned, unsigned> nparticles { { 0, 60 }, { 1, 36 } };
35 return nparticles.at(graphVersion);
40 static const std::map<unsigned, unsigned> nfeatures { { 0, 47 }, { 1, 51 } };
41 return nfeatures.at(graphVersion);
50 desc.
add<
std::string>(
"graph_file",
"RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v0_quantized.pb");
51 desc.
add<
unsigned>(
"version", 0);
52 desc.
add<
bool>(
"mem_mapped",
false);
65 descriptions.
add(
"DPFTau2016v0", desc);
69 DeepTauBase(cfg, GetOutputs(), cache),
70 graphVersion(cfg.getParameter<unsigned>(
"version"))
72 const auto& shape = cache_->getGraph().node(0).attr().at(
"shape").shape();
74 if(!(graphVersion == 1 || graphVersion == 0 ))
75 throw cms::Exception(
"DPFIsolation") <<
"unknown version of the graph file.";
77 if(!(shape.dim(1).size() == getNumberOfParticles(graphVersion) && shape.dim(2).size() == GetNumberOfFeatures(graphVersion)))
78 throw cms::Exception(
"DPFIsolation") <<
"number of inputs does not match the expected inputs for the given version";
86 event.getByToken(pfcandToken_, pfcands);
89 event.getByToken(vtxToken_, vertices);
91 tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1,
92 static_cast<int>(getNumberOfParticles(graphVersion)), static_cast<int>(GetNumberOfFeatures(graphVersion))});
94 tensorflow::Tensor predictions(tensorflow::DT_FLOAT, {
static_cast<int>(taus->size()), 1});
96 std::vector<tensorflow::Tensor> outputs_;
98 float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz,
99 pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight,
100 pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV,
101 pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig, pfCandD0Err,
102 pfCandPtRelPtRel, pfCandDzDz, pfCandDVx_1, pfCandDVy_1, pfCandDVz_1, pfCandD_1;
103 float pvx = !vertices->empty() ? (*vertices)[0].x() : -1;
104 float pvy = !vertices->empty() ? (*vertices)[0].y() : -1;
105 float pvz = !vertices->empty() ? (*vertices)[0].z() : -1;
110 static constexpr float pfCandPt_max = 500.f;
111 static constexpr float pfCandPz_max = 1000.f;
112 static constexpr float pfCandPtRel_max = 1.f;
113 static constexpr float pfCandPzRel_max = 100.f;
114 static constexpr float pfCandPtRelPtRel_max = 1.f;
115 static constexpr float pfCandD0_max = 5.f;
116 static constexpr float pfCandDz_max = 5.f;
117 static constexpr float pfCandDVx_y_z_1_max = 0.05f;
118 static constexpr float pfCandD_1_max = 0.1f;
119 static constexpr float pfCandD0_z_Err_max = 1.f;
120 static constexpr float pfCandDzSig_max = 3.f;
121 static constexpr float pfCandD0Sig_max = 1.f;
122 static constexpr float pfCandDr_max = 0.5f;
123 static constexpr float pfCandEta_max = 2.75f;
124 static constexpr float pfCandDEta_max = 0.5f;
125 static constexpr float pfCandDPhi_max = 0.5f;
126 static constexpr float pfCandPixHits_max = 7.f;
127 static constexpr float pfCandHits_max = 30.f;
129 for(
size_t tau_index = 0; tau_index < taus->size(); tau_index++) {
131 bool isGoodTau =
false;
132 const float lepRecoPt = tau.
pt();
134 const float lepRecoEta = tau.
eta();
135 const float lepRecoPhi = tau.
phi();
139 isGoodTau = (tau.
tauID(
"againstElectronVLooseMVA6") && tau.
tauID(
"againstMuonLoose3") );
143 predictions.matrix<
float>()(tau_index, 0) = -1;
147 std::vector<unsigned int> signalCandidateInds;
150 signalCandidateInds.push_back(getPFCandidateIndex(pfcands,
c));
154 const unsigned n_inputs = getNumberOfParticles(graphVersion) * GetNumberOfFeatures(graphVersion);
155 for(
unsigned input_idx = 0; input_idx < n_inputs; ++input_idx)
156 tensor.flat<
float>()(input_idx) = 0;
158 unsigned int iPF = 0;
159 const unsigned max_iPF = getNumberOfParticles(graphVersion);
161 std::vector<unsigned int> sorted_inds(pfcands->size());
166 [&](
int i1,
int i2) {
return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } );
168 for(
size_t pf_index = 0; pf_index < pfcands->size() && iPF < max_iPF; pf_index++) {
172 if (p.
pt() < 0.5)
continue;
173 if (p.
fromPV() < 0)
continue;
174 if (deltaR_tau_p > 0.5)
continue;
177 pfCandPtRel = p.
pt()/lepRecoPt;
179 pfCandDr = deltaR_tau_p;
183 pfCandIsBarrel = (
std::abs(pfCandEta) < 1.4);
184 pfCandPz =
std::abs(std::sinh(pfCandEta)*pfCandPt);
185 pfCandPzRel = pfCandPz/lepRecoPz;
187 pfCandCharge = p.
charge();
188 pfCandDVx_1 = p.
vx() - pvx;
189 pfCandDVy_1 = p.
vy() - pvy;
190 pfCandDVz_1 = p.
vz() - pvz;
192 pfCandD_1 =
std::sqrt(pfCandDVx_1*pfCandDVx_1 + pfCandDVy_1*pfCandDVy_1 + pfCandDVz_1*pfCandDVz_1);
206 int psudorand = p.
pt()*1000000;
207 if (psudorand%2 == 0) disp = -1;
216 pfCandLostInnerHits = 2.;
224 pfCandFromPV = p.
fromPV();
227 float pfCandTauIndMatch_temp = 0;
229 for (
auto i : signalCandidateInds) {
230 if (
i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1;
233 pfCandTauIndMatch = pfCandTauIndMatch_temp;
234 pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel;
235 pfCandPt =
std::min(pfCandPt, pfCandPt_max);
236 pfCandPt = pfCandPt/pfCandPt_max;
238 pfCandPz =
std::min(pfCandPz, pfCandPz_max);
239 pfCandPz = pfCandPz/pfCandPz_max;
241 pfCandPtRel =
std::min(pfCandPtRel, pfCandPtRel_max);
242 pfCandPzRel =
std::min(pfCandPzRel, pfCandPzRel_max);
243 pfCandPzRel = pfCandPzRel/pfCandPzRel_max;
244 pfCandDr = pfCandDr/pfCandDr_max;
245 pfCandEta = pfCandEta/pfCandEta_max;
246 pfCandDEta = pfCandDEta/pfCandDEta_max;
247 pfCandDPhi = pfCandDPhi/pfCandDPhi_max;
248 pfCandPixHits = pfCandPixHits/pfCandPixHits_max;
249 pfCandHits = pfCandHits/pfCandHits_max;
251 pfCandPtRelPtRel =
std::min(pfCandPtRelPtRel, pfCandPtRelPtRel_max);
253 pfCandD0 = std::clamp(pfCandD0, -pfCandD0_max, pfCandD0_max);
254 pfCandD0 = pfCandD0/pfCandD0_max;
256 pfCandDz = std::clamp(pfCandDz, -pfCandDz_max, pfCandDz_max);
257 pfCandDz = pfCandDz/pfCandDz_max;
259 pfCandD0Err =
std::min(pfCandD0Err, pfCandD0_z_Err_max);
260 pfCandDzErr =
std::min(pfCandDzErr, pfCandD0_z_Err_max);
261 pfCandDzSig =
std::min(pfCandDzSig, pfCandDzSig_max);
262 pfCandDzSig = pfCandDzSig/pfCandDzSig_max;
264 pfCandD0Sig =
std::min(pfCandD0Sig, pfCandD0Sig_max);
265 pfCandD0D0 = pfCandD0*pfCandD0;
266 pfCandDzDz = pfCandDz*pfCandDz;
267 pfCandD0Dz = pfCandD0*pfCandDz;
268 pfCandD0Dphi = pfCandD0*pfCandDPhi;
270 pfCandDVx_1 = std::clamp(pfCandDVx_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
271 pfCandDVx_1 = pfCandDVx_1/pfCandDVx_y_z_1_max;
273 pfCandDVy_1 = std::clamp(pfCandDVy_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
274 pfCandDVy_1 = pfCandDVy_1/pfCandDVx_y_z_1_max;
276 pfCandDVz_1 = std::clamp(pfCandDVz_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
277 pfCandDVz_1 = pfCandDVz_1/pfCandDVx_y_z_1_max;
279 pfCandD_1 = std::clamp(pfCandD_1, -pfCandD_1_max, pfCandD_1_max);
280 pfCandD_1 = pfCandD_1/ pfCandD_1_max;
282 if (graphVersion == 0) {
283 tensor.tensor<
float,3>()( 0, 60-1-iPF, 0) = pfCandPt;
284 tensor.tensor<
float,3>()( 0, 60-1-iPF, 1) = pfCandPz;
285 tensor.tensor<
float,3>()( 0, 60-1-iPF, 2) = pfCandPtRel;
286 tensor.tensor<
float,3>()( 0, 60-1-iPF, 3) = pfCandPzRel;
287 tensor.tensor<
float,3>()( 0, 60-1-iPF, 4) = pfCandDr;
288 tensor.tensor<
float,3>()( 0, 60-1-iPF, 5) = pfCandDEta;
289 tensor.tensor<
float,3>()( 0, 60-1-iPF, 6) = pfCandDPhi;
290 tensor.tensor<
float,3>()( 0, 60-1-iPF, 7) = pfCandEta;
291 tensor.tensor<
float,3>()( 0, 60-1-iPF, 8) = pfCandDz;
292 tensor.tensor<
float,3>()( 0, 60-1-iPF, 9) = pfCandDzSig;
293 tensor.tensor<
float,3>()( 0, 60-1-iPF, 10) = pfCandD0;
294 tensor.tensor<
float,3>()( 0, 60-1-iPF, 11) = pfCandD0Sig;
295 tensor.tensor<
float,3>()( 0, 60-1-iPF, 12) = pfCandDzErr;
296 tensor.tensor<
float,3>()( 0, 60-1-iPF, 13) = pfCandD0Err;
297 tensor.tensor<
float,3>()( 0, 60-1-iPF, 14) = pfCandD0D0;
298 tensor.tensor<
float,3>()( 0, 60-1-iPF, 15) = pfCandCharge==0;
299 tensor.tensor<
float,3>()( 0, 60-1-iPF, 16) = pfCandCharge==1;
300 tensor.tensor<
float,3>()( 0, 60-1-iPF, 17) = pfCandCharge==-1;
301 tensor.tensor<
float,3>()( 0, 60-1-iPF, 18) = pfCandPdgID>22;
302 tensor.tensor<
float,3>()( 0, 60-1-iPF, 19) = pfCandPdgID==22;
303 tensor.tensor<
float,3>()( 0, 60-1-iPF, 20) = pfCandDzDz;
304 tensor.tensor<
float,3>()( 0, 60-1-iPF, 21) = pfCandD0Dz;
305 tensor.tensor<
float,3>()( 0, 60-1-iPF, 22) = pfCandD0Dphi;
306 tensor.tensor<
float,3>()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel;
307 tensor.tensor<
float,3>()( 0, 60-1-iPF, 24) = pfCandPixHits;
308 tensor.tensor<
float,3>()( 0, 60-1-iPF, 25) = pfCandHits;
309 tensor.tensor<
float,3>()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1;
310 tensor.tensor<
float,3>()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0;
311 tensor.tensor<
float,3>()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1;
312 tensor.tensor<
float,3>()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2;
313 tensor.tensor<
float,3>()( 0, 60-1-iPF, 30) = pfCandPuppiWeight;
314 tensor.tensor<
float,3>()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1);
315 tensor.tensor<
float,3>()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5);
316 tensor.tensor<
float,3>()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6);
317 tensor.tensor<
float,3>()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7);
318 tensor.tensor<
float,3>()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1);
319 tensor.tensor<
float,3>()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2);
320 tensor.tensor<
float,3>()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3);
321 tensor.tensor<
float,3>()( 0, 60-1-iPF, 38) = pfCandIsBarrel;
322 tensor.tensor<
float,3>()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk;
323 tensor.tensor<
float,3>()( 0, 60-1-iPF, 40) = pfCandPdgID==1;
324 tensor.tensor<
float,3>()( 0, 60-1-iPF, 41) = pfCandPdgID==2;
325 tensor.tensor<
float,3>()( 0, 60-1-iPF, 42) = pfCandPdgID==11;
326 tensor.tensor<
float,3>()( 0, 60-1-iPF, 43) = pfCandPdgID==13;
327 tensor.tensor<
float,3>()( 0, 60-1-iPF, 44) = pfCandPdgID==130;
328 tensor.tensor<
float,3>()( 0, 60-1-iPF, 45) = pfCandPdgID==211;
329 tensor.tensor<
float,3>()( 0, 60-1-iPF, 46) = pfCandTauIndMatch;
332 if (graphVersion == 1) {
333 tensor.tensor<
float,3>()( 0, 36-1-iPF, 0) = pfCandPt;
334 tensor.tensor<
float,3>()( 0, 36-1-iPF, 1) = pfCandPz;
335 tensor.tensor<
float,3>()( 0, 36-1-iPF, 2) = pfCandPtRel;
336 tensor.tensor<
float,3>()( 0, 36-1-iPF, 3) = pfCandPzRel;
337 tensor.tensor<
float,3>()( 0, 36-1-iPF, 4) = pfCandDr;
338 tensor.tensor<
float,3>()( 0, 36-1-iPF, 5) = pfCandDEta;
339 tensor.tensor<
float,3>()( 0, 36-1-iPF, 6) = pfCandDPhi;
340 tensor.tensor<
float,3>()( 0, 36-1-iPF, 7) = pfCandEta;
341 tensor.tensor<
float,3>()( 0, 36-1-iPF, 8) = pfCandDz;
342 tensor.tensor<
float,3>()( 0, 36-1-iPF, 9) = pfCandDzSig;
343 tensor.tensor<
float,3>()( 0, 36-1-iPF, 10) = pfCandD0;
344 tensor.tensor<
float,3>()( 0, 36-1-iPF, 11) = pfCandD0Sig;
345 tensor.tensor<
float,3>()( 0, 36-1-iPF, 12) = pfCandDzErr;
346 tensor.tensor<
float,3>()( 0, 36-1-iPF, 13) = pfCandD0Err;
347 tensor.tensor<
float,3>()( 0, 36-1-iPF, 14) = pfCandD0D0;
348 tensor.tensor<
float,3>()( 0, 36-1-iPF, 15) = pfCandCharge==0;
349 tensor.tensor<
float,3>()( 0, 36-1-iPF, 16) = pfCandCharge==1;
350 tensor.tensor<
float,3>()( 0, 36-1-iPF, 17) = pfCandCharge==-1;
351 tensor.tensor<
float,3>()( 0, 36-1-iPF, 18) = pfCandPdgID>22;
352 tensor.tensor<
float,3>()( 0, 36-1-iPF, 19) = pfCandPdgID==22;
353 tensor.tensor<
float,3>()( 0, 36-1-iPF, 20) = pfCandDVx_1;
354 tensor.tensor<
float,3>()( 0, 36-1-iPF, 21) = pfCandDVy_1;
355 tensor.tensor<
float,3>()( 0, 36-1-iPF, 22) = pfCandDVz_1;
356 tensor.tensor<
float,3>()( 0, 36-1-iPF, 23) = pfCandD_1;
357 tensor.tensor<
float,3>()( 0, 36-1-iPF, 24) = pfCandDzDz;
358 tensor.tensor<
float,3>()( 0, 36-1-iPF, 25) = pfCandD0Dz;
359 tensor.tensor<
float,3>()( 0, 36-1-iPF, 26) = pfCandD0Dphi;
360 tensor.tensor<
float,3>()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel;
361 tensor.tensor<
float,3>()( 0, 36-1-iPF, 28) = pfCandPixHits;
362 tensor.tensor<
float,3>()( 0, 36-1-iPF, 29) = pfCandHits;
363 tensor.tensor<
float,3>()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1;
364 tensor.tensor<
float,3>()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0;
365 tensor.tensor<
float,3>()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1;
366 tensor.tensor<
float,3>()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2;
367 tensor.tensor<
float,3>()( 0, 36-1-iPF, 34) = pfCandPuppiWeight;
368 tensor.tensor<
float,3>()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1);
369 tensor.tensor<
float,3>()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5);
370 tensor.tensor<
float,3>()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6);
371 tensor.tensor<
float,3>()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7);
372 tensor.tensor<
float,3>()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1);
373 tensor.tensor<
float,3>()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2);
374 tensor.tensor<
float,3>()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3);
375 tensor.tensor<
float,3>()( 0, 36-1-iPF, 42) = pfCandIsBarrel;
376 tensor.tensor<
float,3>()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk;
377 tensor.tensor<
float,3>()( 0, 36-1-iPF, 44) = pfCandPdgID==1;
378 tensor.tensor<
float,3>()( 0, 36-1-iPF, 45) = pfCandPdgID==2;
379 tensor.tensor<
float,3>()( 0, 36-1-iPF, 46) = pfCandPdgID==11;
380 tensor.tensor<
float,3>()( 0, 36-1-iPF, 47) = pfCandPdgID==13;
381 tensor.tensor<
float,3>()( 0, 36-1-iPF, 48) = pfCandPdgID==130;
382 tensor.tensor<
float,3>()( 0, 36-1-iPF, 49) = pfCandPdgID==211;
383 tensor.tensor<
float,3>()( 0, 36-1-iPF, 50) = pfCandTauIndMatch;
387 tensorflow::run(&(cache_->getSession()), { {
"input_1", tensor } }, {
"output_node0" }, {}, &outputs_);
388 predictions.matrix<
float>()(tau_index, 0) = outputs_[0].flat<
float>()(0);
float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
float dxyError() const override
uncertainty on dxy
double eta() const final
momentum pseudorapidity
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int pdgId() const override
PDG identifier.
static unsigned getNumberOfParticles(unsigned graphVersion)
#define DEFINE_FWK_MODULE(type)
const LorentzVector & p4() const override
four-momentum Lorentz vecto r
float dzError() const override
uncertainty on dz
double pt() const final
transverse momentum
float tauID(const std::string &name) const
reco::CandidatePtrVector signalCands() const
double vz() const override
z coordinate of vertex position
std::map< std::string, Output > OutputCollection
def generate(map_blobs=False, class_name=None)
int charge() const override
electric charge
double vy() const override
y coordinate of vertex position
static unsigned GetNumberOfFeatures(unsigned graphVersion)
const PVAssociationQuality pvAssociationQuality() const
double pz() const final
z coordinate of momentum vector
const PVAssoc fromPV(size_t ipv=0) const
bool trackHighPurity() const
true if the track had the highPurity quality bit
LostInnerHits lostInnerHits() const
Abs< T >::type abs(const T &t)
const LorentzVector & p4() const final
four-momentum Lorentz vector
double pt() const override
transverse momentum
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int numberOfPixelHits() const
Analysis-level tau class.
tensorflow::Tensor getPredictions(edm::Event &event, const edm::EventSetup &es, edm::Handle< TauCollection > taus) override
bool hasTrackDetails() const
Return true if a bestTrack can be extracted from this Candidate.
double deltaR(double eta1, double eta2, double phi1, double phi2)
bool isTauIDAvailable(const std::string &name) const
Returns true if a specific ID is available in this pat::Tau.
double eta() const override
momentum pseudorapidity
double phi() const override
momentum azimuthal angle
DPFIsolation(const edm::ParameterSet &cfg, const deep_tau::DeepTauCache *cache)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
static const OutputCollection & GetOutputs()
virtual float dxy() const
dxy with respect to the PV ref
double phi() const final
momentum azimuthal angle
double vx() const override
x coordinate of vertex position