CMS 3D CMS Logo

TauNNId.cc
Go to the documentation of this file.
4 #include <cmath>
5 
6 static constexpr unsigned int n_particles_max = 10;
7 
8 TauNNId::TauNNId(const std::string &iInput, const TauNNTFCache *cache, const std::string &iWeightFile, int iNParticles) {
9  NNvectorVar_.clear();
10  edm::FileInPath fp(iWeightFile);
12  fNParticles_ = iNParticles;
13 
14  fPt_ = std::make_unique<float[]>(fNParticles_);
15  fEta_ = std::make_unique<float[]>(fNParticles_);
16  fPhi_ = std::make_unique<float[]>(fNParticles_);
17  fId_ = std::make_unique<float[]>(fNParticles_);
18  fInput_ = iInput;
19 }
20 
23  NNvectorVar_.clear();
24  for (int i0 = 0; i0 < fNParticles_; i0++) {
25  NNvectorVar_.push_back(fPt_.get()[i0]); //pT
26  NNvectorVar_.push_back(fEta_.get()[i0]); //dEta from jet axis
27  NNvectorVar_.push_back(fPhi_.get()[i0]); //dPhi from jet axis
28  if (fPt_.get()[i0] == 0) {
29  for (int i1 = 0; i1 < 5; i1++)
30  NNvectorVar_.push_back(0);
31  continue;
32  }
33  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon); // Photon
34  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron); // Electron
35  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon); // Muon
36  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron); // Neutral Had
37  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron); // Charged Had
38  }
39 }
41  tensorflow::Tensor input(tensorflow::DT_FLOAT,
42  {1, (unsigned int)NNvectorVar_.size()}); //was {1,35} but get size mismatch, CHECK
43  for (unsigned int i = 0; i < NNvectorVar_.size(); i++) {
44  input.matrix<float>()(0, i) = float(NNvectorVar_[i]);
45  }
46  std::vector<tensorflow::Tensor> outputs;
47  tensorflow::run(session_, {{fInput_, input}}, {"dense_4/Sigmoid:0"}, &outputs);
48  return outputs[0].matrix<float>()(0, 0);
49 } //end EvaluateNN
50 
52  for (int i0 = 0; i0 < fNParticles_; i0++) {
53  fPt_.get()[i0] = 0;
54  fEta_.get()[i0] = 0;
55  fPhi_.get()[i0] = 0;
56  fId_.get()[i0] = 0;
57  }
58  std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) { return (i.pt() > j.pt()); });
59  for (unsigned int i0 = 0; i0 < iParts.size(); i0++) {
60  if (i0 > n_particles_max || i0 >= (unsigned int)fNParticles_)
61  break;
62  fPt_.get()[i0] = iParts[i0].pt();
63  fEta_.get()[i0] = iSeed.eta() - iParts[i0].eta();
64  fPhi_.get()[i0] = deltaPhi(iSeed.phi(), iParts[i0].phi());
65  fId_.get()[i0] = iParts[i0].id();
66  }
68  return EvaluateNN();
69 }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:85
~TauNNId()
Definition: TauNNId.cc:21
unique_ptr< float[]> fPhi_
Definition: TauNNId.h:29
static constexpr unsigned int n_particles_max
Definition: TauNNId.cc:6
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:57
int fNParticles_
Definition: TauNNId.h:26
unique_ptr< float[]> fId_
Definition: TauNNId.h:30
static std::string const input
Definition: EdmProvDump.cc:47
std::vector< float > NNvectorVar_
Definition: TauNNId.h:24
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:213
bool closeSession(Session *&session)
Definition: TensorFlow.cc:198
void setNNVectorVar()
Definition: TauNNId.cc:22
unique_ptr< float[]> fPt_
Definition: TauNNId.h:27
tensorflow::Session * session_
Definition: TauNNId.h:23
std::string fInput_
Definition: TauNNId.h:25
def cache(function)
Definition: utilities.py:3
float EvaluateNN()
Definition: TauNNId.cc:40
TauNNId(const std::string &iInput, const TauNNTFCache *cache, const std::string &iWeightFile, int iNParticles)
Definition: TauNNId.cc:8
double phi() const final
momentum azimuthal angle
float compute(const l1t::PFCandidate &iSeed, l1t::PFCandidateCollection &iParts)
Definition: TauNNId.cc:51
unique_ptr< float[]> fEta_
Definition: TauNNId.h:28
double eta() const final
momentum pseudorapidity