CMS 3D CMS Logo

TauNNIdHW.cc
Go to the documentation of this file.
1 #include <iostream>
3 
6 
7 void TauNNIdHW::initialize(const std::string &iInput, int iNParticles) {
8  fNParticles_ = iNParticles;
9  fPt_ = std::make_unique<pt_t[]>(fNParticles_);
10  fEta_ = std::make_unique<etaphi_t[]>(fNParticles_);
11  fPhi_ = std::make_unique<etaphi_t[]>(fNParticles_);
12  fId_ = std::make_unique<id_t[]>(fNParticles_);
13  fInput_ = iInput;
14 }
16  NNvectorVar_.clear();
17  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
18  input_t pPt = input_t(fPt_.get()[i0]);
19  input_t pEta = input_t(fEta_.get()[i0]);
20  input_t pPhi = input_t(fPhi_.get()[i0]);
21 
22  NNvectorVar_.push_back(pPt);
23  NNvectorVar_.push_back(pEta);
24  NNvectorVar_.push_back(pPhi);
25  if (fPt_.get()[i0] == 0) {
26  for (unsigned i1 = 0; i1 < 5; i1++)
27  NNvectorVar_.push_back(0);
28  continue;
29  }
30  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon); // Photon
31  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron); // Electron
32  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon); // Muon
33  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron); // Neutral Had
34  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron); // Charged Had
35  }
36 }
37 
40  for (unsigned int i = 0; i < NNvectorVar_.size(); i++) {
42  }
43 
44  layer1_t layer1_out[N_LAYER_1];
45  layer1_t logits1[N_LAYER_1];
46  nnet::compute_layer<input_t, layer1_t, config1>(data, logits1, w1, b1);
47  nnet::relu<layer1_t, layer1_t, relu_config1>(logits1, layer1_out);
48 
49  layer2_t layer2_out[N_LAYER_2];
50  layer2_t logits2[N_LAYER_2];
51  nnet::compute_layer<layer1_t, layer2_t, config2>(layer1_out, logits2, w2, b2);
52  nnet::relu<layer2_t, layer2_t, relu_config2>(logits2, layer2_out);
53 
54  layer3_t layer3_out[N_LAYER_3];
55  layer3_t logits3[N_LAYER_3];
56  nnet::compute_layer<layer2_t, layer3_t, config3>(layer2_out, logits3, w3, b3);
57  nnet::relu<layer3_t, layer3_t, relu_config3>(logits3, layer3_out);
58 
59  result_t logits4[N_OUTPUTS];
60  nnet::compute_layer<layer3_t, result_t, config4>(layer3_out, logits4, w4, b4);
62  nnet::sigmoid<result_t, result_t, sigmoid_config4>(logits4, res);
63 
64  return res[0];
65 }
66 /*
67 void TauNNIdHW::print() {
68  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
69  input_t pPt = input_t(fPt_.get()[i0]);
70  input_t pEta = input_t(fEta_.get()[i0]);
71  input_t pPhi = input_t(fPhi_.get()[i0]);
72  input_t pId = input_t(fId_.get()[i0]);
73  fprintf(file_, " %08x", pPt.to_uint());
74  fprintf(file_, " %08x", pEta.to_uint());
75  fprintf(file_, " %08x", pPhi.to_uint());
76  fprintf(file_, " %08x", pId.to_uint());
77  }
78  fprintf(file_, "\n");
79 }
80 */
81 result_t TauNNIdHW::compute(const l1t::PFCandidate &iSeed, std::vector<l1t::PFCandidate> &iParts) {
82  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
83  fPt_.get()[i0] = 0.;
84  fEta_.get()[i0] = 0.;
85  fPhi_.get()[i0] = 0.;
86  fId_.get()[i0] = 0.;
87  }
88  std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) {
89  return (pt_t(i.pt()) > pt_t(j.pt()));
90  });
91  for (unsigned int i0 = 0; i0 < iParts.size(); i0++) {
92  if (i0 > fNParticles_)
93  break;
94  fPt_.get()[i0] = pt_t(iParts[i0].pt());
95  fEta_.get()[i0] = etaphi_t(iSeed.eta() - iParts[i0].eta());
96  etaphi_t lDPhi = etaphi_t(iSeed.phi()) - etaphi_t(iParts[i0].phi());
97  etaphi_t lMPI = 3.1415;
98  if (lDPhi > lMPI)
99  lDPhi = lDPhi - lMPI;
100  if (lDPhi < -lMPI)
101  lDPhi = lDPhi + lMPI;
102  fPhi_.get()[i0] = lDPhi;
103  fId_.get()[i0] = id_t(iParts[i0].id());
104  }
105  SetNNVectorVar();
106  return EvaluateNN();
107 }
weight_default_t b3[10]
Definition: b3.h:9
weight_default_t b1[25]
Definition: b1.h:9
#define N_LAYER_1
result_t compute(const l1t::PFCandidate &iSeed, std::vector< l1t::PFCandidate > &iParts)
Definition: TauNNIdHW.cc:81
common ppss p3p6s2 common epss epspn46 common const1 w2
Definition: inclppp.h:1
weight_default_t b4[1]
Definition: b4.h:9
#define N_INPUTS
std::string fInput_
Definition: TauNNIdHW.h:154
Definition: Electron.h:6
ap_fixed< 16, 8 > input_t
common ppss p3p6s2 common epss epspn46 common const1 w4
Definition: inclppp.h:1
void SetNNVectorVar()
Definition: TauNNIdHW.cc:15
unique_ptr< id_t[]> fId_
Definition: TauNNIdHW.h:159
unique_ptr< etaphi_t[]> fPhi_
Definition: TauNNIdHW.h:158
ap_fixed< 16, 8 > layer2_t
unique_ptr< etaphi_t[]> fEta_
Definition: TauNNIdHW.h:157
TauNNIdHW()
Definition: TauNNIdHW.cc:4
~TauNNIdHW()
Definition: TauNNIdHW.cc:5
weight_default_t b2[10]
Definition: b2.h:9
unsigned fNParticles_
Definition: TauNNIdHW.h:155
std::vector< input_t > NNvectorVar_
Definition: TauNNIdHW.h:163
#define N_LAYER_3
weight_default_t w1[2000]
Definition: w1.h:9
ap_fixed< 16, 8 > layer3_t
result_t EvaluateNN()
Definition: TauNNIdHW.cc:38
#define N_LAYER_2
common ppss p3p6s2 common epss epspn46 common const1 w3
Definition: inclppp.h:1
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
#define N_OUTPUTS
ap_uint< 5 > id_t
Definition: TauNNIdHW.h:44
ap_fixed< 16, 8 > result_t
unique_ptr< pt_t[]> fPt_
Definition: TauNNIdHW.h:156
void initialize(const std::string &iName, int iNParticles)
Definition: TauNNIdHW.cc:7
ap_fixed< 10, 4 > etaphi_t
Definition: TauNNIdHW.h:28
double phi() const final
momentum azimuthal angle
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:27
ap_fixed< 16, 8 > layer1_t
double eta() const final
momentum pseudorapidity