Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:29

0001 #include "L1Trigger/Phase2L1ParticleFlow/interface/TauNNId.h"
0002 #include "FWCore/ParameterSet/interface/FileInPath.h"
0003 #include "DataFormats/Math/interface/deltaPhi.h"
0004 #include <cmath>
0005 
0006 static constexpr unsigned int n_particles_max = 10;
0007 
0008 TauNNId::TauNNId(const std::string &iInput, const TauNNTFCache *cache, const std::string &iWeightFile, int iNParticles) {
0009   NNvectorVar_.clear();
0010   edm::FileInPath fp(iWeightFile);
0011   session_ = tensorflow::createSession(cache->graphDef);
0012   fNParticles_ = iNParticles;
0013 
0014   fPt_ = std::make_unique<float[]>(fNParticles_);
0015   fEta_ = std::make_unique<float[]>(fNParticles_);
0016   fPhi_ = std::make_unique<float[]>(fNParticles_);
0017   fId_ = std::make_unique<float[]>(fNParticles_);
0018   fInput_ = iInput;
0019 }
0020 
0021 TauNNId::~TauNNId() { tensorflow::closeSession(session_); }
0022 void TauNNId::setNNVectorVar() {
0023   NNvectorVar_.clear();
0024   for (int i0 = 0; i0 < fNParticles_; i0++) {
0025     NNvectorVar_.push_back(fPt_.get()[i0]);   //pT
0026     NNvectorVar_.push_back(fEta_.get()[i0]);  //dEta from jet axis
0027     NNvectorVar_.push_back(fPhi_.get()[i0]);  //dPhi from jet axis
0028     if (fPt_.get()[i0] == 0) {
0029       for (int i1 = 0; i1 < 5; i1++)
0030         NNvectorVar_.push_back(0);
0031       continue;
0032     }
0033     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon);         // Photon
0034     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron);       // Electron
0035     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon);           // Muon
0036     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron);  // Neutral Had
0037     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron);  // Charged Had
0038   }
0039 }
0040 float TauNNId::EvaluateNN() {
0041   tensorflow::Tensor input(tensorflow::DT_FLOAT,
0042                            {1, (unsigned int)NNvectorVar_.size()});  //was {1,35} but get size mismatch, CHECK
0043   for (unsigned int i = 0; i < NNvectorVar_.size(); i++) {
0044     input.matrix<float>()(0, i) = float(NNvectorVar_[i]);
0045   }
0046   std::vector<tensorflow::Tensor> outputs;
0047   tensorflow::run(session_, {{fInput_, input}}, {"dense_4/Sigmoid:0"}, &outputs);
0048   return outputs[0].matrix<float>()(0, 0);
0049 }  //end EvaluateNN
0050 
0051 float TauNNId::compute(const l1t::PFCandidate &iSeed, l1t::PFCandidateCollection &iParts) {
0052   for (int i0 = 0; i0 < fNParticles_; i0++) {
0053     fPt_.get()[i0] = 0;
0054     fEta_.get()[i0] = 0;
0055     fPhi_.get()[i0] = 0;
0056     fId_.get()[i0] = 0;
0057   }
0058   std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) { return (i.pt() > j.pt()); });
0059   for (unsigned int i0 = 0; i0 < iParts.size(); i0++) {
0060     if (i0 > n_particles_max || i0 >= (unsigned int)fNParticles_)
0061       break;
0062     fPt_.get()[i0] = iParts[i0].pt();
0063     fEta_.get()[i0] = iSeed.eta() - iParts[i0].eta();
0064     fPhi_.get()[i0] = deltaPhi(iSeed.phi(), iParts[i0].phi());
0065     fId_.get()[i0] = iParts[i0].id();
0066   }
0067   setNNVectorVar();
0068   return EvaluateNN();
0069 }