Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:56:00

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