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 }
15 
16 //Prepare the inputs for the Tau NN
18  NNvectorVar_.clear();
19  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
20  input_t pPt = input_t(fPt_.get()[i0]);
21  input_t pEta = input_t(fEta_.get()[i0]);
22  input_t pPhi = input_t(fPhi_.get()[i0]);
23 
24  NNvectorVar_.push_back(pPt);
25  NNvectorVar_.push_back(pEta);
26  NNvectorVar_.push_back(pPhi);
27  if (fPt_.get()[i0] == 0) {
28  for (unsigned i1 = 0; i1 < 5; i1++)
29  NNvectorVar_.push_back(0);
30  continue;
31  }
32  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon); // Photon
33  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron); // Electron
34  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon); // Muon
35  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron); // Neutral Had
36  NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron); // Charged Had
37  }
38 }
39 
40 // Main architecture of the NN here
42  input_t model_input[N_INPUT_1_1];
43  for (unsigned int i = 0; i < NNvectorVar_.size(); i++) {
44  model_input[i] = input_t(NNvectorVar_[i]);
45  }
46 
47  layer2_t layer2_out[N_LAYER_2];
48  nnet::dense<input_t, layer2_t, config2>(model_input, layer2_out, w2, b2); // Dense_1
49 
50  layer4_t layer4_out[N_LAYER_2];
51  nnet::relu<layer2_t, layer4_t, relu_config4>(layer2_out, layer4_out); // relu_1
52 
53  layer5_t layer5_out[N_LAYER_5];
54  nnet::dense<layer4_t, layer5_t, config5>(layer4_out, layer5_out, w5, b5); // Dense_2
55 
56  layer7_t layer7_out[N_LAYER_5];
57  nnet::relu<layer5_t, layer7_t, relu_config7>(layer5_out, layer7_out); // relu_2
58 
59  layer8_t layer8_out[N_LAYER_8];
60  nnet::dense<layer7_t, layer8_t, config8>(layer7_out, layer8_out, w8, b8); // Dense_3
61 
62  layer10_t layer10_out[N_LAYER_8];
63  nnet::relu<layer8_t, layer10_t, relu_config10>(layer8_out, layer10_out); // relu_3
64 
65  layer11_t layer11_out[N_LAYER_11];
66  nnet::dense<layer10_t, layer11_t, config11>(layer10_out, layer11_out, w11, b11); // Dense_4
67 
68  layer13_t layer13_out[N_LAYER_11];
69  nnet::relu<layer11_t, layer13_t, relu_config13>(layer11_out, layer13_out); // relu_4
70 
71  layer14_t layer14_out[N_LAYER_14];
72  nnet::dense<layer13_t, layer14_t, config14>(layer13_out, layer14_out, w14, b14); // Dense_5
73 
74  layer16_t layer16_out[N_LAYER_14];
75  nnet::relu<layer14_t, layer16_t, relu_config16>(layer14_out, layer16_out); // relu_5
76 
77  layer17_t layer17_out[N_LAYER_17];
78  nnet::dense<layer16_t, layer17_t, config17>(layer16_out, layer17_out, w17, b17); // Dense_6
79 
80  result_t layer19_out[N_LAYER_17];
81  nnet::sigmoid<layer17_t, result_t, sigmoid_config19>(layer17_out, layer19_out); // jetID_output
82 
83  result_t layer20_out[N_LAYER_20];
84  nnet::dense<layer16_t, result_t, config20>(layer16_out, layer20_out, w20, b20); // pT_output
85 
86  // Return both pT correction and the NN ID
87  Tau_NN_Result nn_out;
88  nn_out.nn_pt_correction = layer20_out[0];
89  nn_out.nn_id = layer19_out[0];
90 
91  return nn_out;
92 }
93 
94 /*
95 // Uncomment for debugging purposes
96 void TauNNIdHW::print() {
97  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
98  input_t pPt = input_t(fPt_.get()[i0]);
99  input_t pEta = input_t(fEta_.get()[i0]);
100  input_t pPhi = input_t(fPhi_.get()[i0]);
101  input_t pId = input_t(fId_.get()[i0]);
102  fprintf(file_, " %08x", pPt.to_uint());
103  fprintf(file_, " %08x", pEta.to_uint());
104  fprintf(file_, " %08x", pPhi.to_uint());
105  fprintf(file_, " %08x", pId.to_uint());
106  }
107  fprintf(file_, "\n");
108 }
109 */
110 
111 Tau_NN_Result TauNNIdHW::compute(const l1t::PFCandidate &iSeed, std::vector<l1t::PFCandidate> &iParts) {
112  // Initialize the input vector
113  for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
114  fPt_.get()[i0] = 0.;
115  fEta_.get()[i0] = 0.;
116  fPhi_.get()[i0] = 0.;
117  fId_.get()[i0] = 0.;
118  }
119 
120  // Sort the candidates by pT
121  std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) {
122  return (pt_t(i.pt()) > pt_t(j.pt()));
123  });
124 
125  // Compute the values w.r.t to the seeds
126  for (unsigned int i0 = 0; i0 < iParts.size(); i0++) {
127  if (i0 >= fNParticles_)
128  break;
129 
130  fPt_.get()[i0] = pt_t(iParts[i0].pt());
131  fEta_.get()[i0] = etaphi_t(iSeed.eta() - iParts[i0].eta());
132  etaphi_t lDPhi = etaphi_t(iSeed.phi()) - etaphi_t(iParts[i0].phi());
133  etaphi_t lMPI = 3.1415;
134 
135  if (lDPhi > lMPI)
136  lDPhi = lDPhi - lMPI;
137  if (lDPhi < -lMPI)
138  lDPhi = lDPhi + lMPI;
139 
140  fPhi_.get()[i0] = lDPhi;
141  fId_.get()[i0] = id_t(iParts[i0].id());
142  }
143 
144  // Set the inputs
145  SetNNVectorVar();
146 
147  // Return the N outputs with the inputs
148  return EvaluateNN();
149 }
bias5_t b5[25]
Definition: b5.h:9
bias14_t b14[10]
Definition: b14.h:9
common ppss p3p6s2 common epss epspn46 common const1 w2
Definition: inclppp.h:1
bias11_t b11[15]
Definition: b11.h:9
ap_fixed< 16, 6 > layer11_t
Definition: defines.h:48
std::string fInput_
Definition: TauNNIdHW.h:147
common ppss p3p6s2 common epss epspn46 common const1 w8
Definition: inclppp.h:1
#define N_LAYER_11
Definition: defines.h:19
bias20_t b20[1]
Definition: b20.h:9
bias8_t b8[15]
Definition: b8.h:9
common ppss p3p6s2 common epss epspn46 common const1 w11
Definition: inclppp.h:1
ap_fixed< 16, 6 > layer5_t
Definition: defines.h:36
common ppss p3p6s2 common epss epspn46 common const1 w20
Definition: inclppp.h:1
ap_ufixed< 9, 0, AP_RND_CONV, AP_SAT > layer4_t
Definition: defines.h:34
ap_fixed< 16, 6 > result_t
Definition: defines.h:64
void SetNNVectorVar()
Definition: TauNNIdHW.cc:17
unique_ptr< id_t[]> fId_
Definition: TauNNIdHW.h:152
unique_ptr< etaphi_t[]> fPhi_
Definition: TauNNIdHW.h:151
ap_fixed< 16, 6 > input_t
Definition: defines.h:27
unique_ptr< etaphi_t[]> fEta_
Definition: TauNNIdHW.h:150
TauNNIdHW()
Definition: TauNNIdHW.cc:4
~TauNNIdHW()
Definition: TauNNIdHW.cc:5
common ppss p3p6s2 common epss epspn46 common const1 w14
Definition: inclppp.h:1
#define N_LAYER_14
Definition: defines.h:21
ap_ufixed< 9, 0, AP_RND_CONV, AP_SAT > layer7_t
Definition: defines.h:40
#define N_LAYER_17
Definition: defines.h:23
ap_ufixed< 9, 0, AP_RND_CONV, AP_SAT > layer13_t
Definition: defines.h:52
unsigned fNParticles_
Definition: TauNNIdHW.h:148
bias2_t b2[25]
Definition: b2.h:9
ap_fixed< 16, 6 > layer2_t
Definition: defines.h:30
std::vector< input_t > NNvectorVar_
Definition: TauNNIdHW.h:156
ap_ufixed< 9, 0, AP_RND_CONV, AP_SAT > layer16_t
Definition: defines.h:58
ap_fixed< 16, 6 > layer14_t
Definition: defines.h:54
#define N_LAYER_5
Definition: defines.h:15
Tau_NN_Result EvaluateNN()
Definition: TauNNIdHW.cc:41
#define N_LAYER_2
Definition: defines.h:13
#define N_INPUT_1_1
Definition: defines.h:11
#define N_LAYER_20
Definition: defines.h:24
common ppss p3p6s2 common epss epspn46 common const1 w17
Definition: inclppp.h:1
ap_fixed< 16, 6 > layer8_t
Definition: defines.h:42
ap_uint< 5 > id_t
Definition: TauNNIdHW.h:36
unique_ptr< pt_t[]> fPt_
Definition: TauNNIdHW.h:149
Tau_NN_Result compute(const l1t::PFCandidate &iSeed, std::vector< l1t::PFCandidate > &iParts)
Definition: TauNNIdHW.cc:111
result_t nn_pt_correction
Definition: TauNNIdHW.h:18
bias17_t b17[1]
Definition: b17.h:9
void initialize(const std::string &iName, int iNParticles)
Definition: TauNNIdHW.cc:7
ap_fixed< 10, 4 > etaphi_t
Definition: TauNNIdHW.h:14
double phi() const final
momentum azimuthal angle
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:13
common ppss p3p6s2 common epss epspn46 common const1 w5
Definition: inclppp.h:1
#define N_LAYER_8
Definition: defines.h:17
ap_ufixed< 9, 0, AP_RND_CONV, AP_SAT > layer10_t
Definition: defines.h:46
ap_fixed< 16, 6 > layer17_t
Definition: defines.h:60
double eta() const final
momentum pseudorapidity