Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:53:59

0001 #include <iostream>
0002 #include "L1Trigger/Phase2L1ParticleFlow/interface/taus/TauNNIdHW.h"
0003 
0004 TauNNIdHW::TauNNIdHW() { NNvectorVar_.clear(); }
0005 TauNNIdHW::~TauNNIdHW() {}
0006 
0007 void TauNNIdHW::initialize(const std::string &iInput, int iNParticles) {
0008   fNParticles_ = iNParticles;
0009   fPt_ = std::make_unique<pt_t[]>(fNParticles_);
0010   fEta_ = std::make_unique<etaphi_t[]>(fNParticles_);
0011   fPhi_ = std::make_unique<etaphi_t[]>(fNParticles_);
0012   fId_ = std::make_unique<id_t[]>(fNParticles_);
0013   fInput_ = iInput;
0014 }
0015 void TauNNIdHW::SetNNVectorVar() {
0016   NNvectorVar_.clear();
0017   for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
0018     input_t pPt = input_t(fPt_.get()[i0]);
0019     input_t pEta = input_t(fEta_.get()[i0]);
0020     input_t pPhi = input_t(fPhi_.get()[i0]);
0021 
0022     NNvectorVar_.push_back(pPt);
0023     NNvectorVar_.push_back(pEta);
0024     NNvectorVar_.push_back(pPhi);
0025     if (fPt_.get()[i0] == 0) {
0026       for (unsigned i1 = 0; i1 < 5; i1++)
0027         NNvectorVar_.push_back(0);
0028       continue;
0029     }
0030     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon);         // Photon
0031     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron);       // Electron
0032     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon);           // Muon
0033     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron);  // Neutral Had
0034     NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron);  // Charged Had
0035   }
0036 }
0037 
0038 result_t TauNNIdHW::EvaluateNN() {
0039   input_t data[N_INPUTS];
0040   for (unsigned int i = 0; i < NNvectorVar_.size(); i++) {
0041     data[i] = input_t(NNvectorVar_[i]);
0042   }
0043 
0044   layer1_t layer1_out[N_LAYER_1];
0045   layer1_t logits1[N_LAYER_1];
0046   nnet::compute_layer<input_t, layer1_t, config1>(data, logits1, w1, b1);
0047   nnet::relu<layer1_t, layer1_t, relu_config1>(logits1, layer1_out);
0048 
0049   layer2_t layer2_out[N_LAYER_2];
0050   layer2_t logits2[N_LAYER_2];
0051   nnet::compute_layer<layer1_t, layer2_t, config2>(layer1_out, logits2, w2, b2);
0052   nnet::relu<layer2_t, layer2_t, relu_config2>(logits2, layer2_out);
0053 
0054   layer3_t layer3_out[N_LAYER_3];
0055   layer3_t logits3[N_LAYER_3];
0056   nnet::compute_layer<layer2_t, layer3_t, config3>(layer2_out, logits3, w3, b3);
0057   nnet::relu<layer3_t, layer3_t, relu_config3>(logits3, layer3_out);
0058 
0059   result_t logits4[N_OUTPUTS];
0060   nnet::compute_layer<layer3_t, result_t, config4>(layer3_out, logits4, w4, b4);
0061   result_t res[N_OUTPUTS];
0062   nnet::sigmoid<result_t, result_t, sigmoid_config4>(logits4, res);
0063 
0064   return res[0];
0065 }
0066 /*
0067 void TauNNIdHW::print() { 
0068   for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
0069     input_t pPt  = input_t(fPt_.get()[i0]);
0070     input_t pEta = input_t(fEta_.get()[i0]);
0071     input_t pPhi = input_t(fPhi_.get()[i0]);
0072     input_t pId  = input_t(fId_.get()[i0]);    
0073     fprintf(file_, " %08x", pPt.to_uint());
0074     fprintf(file_, " %08x", pEta.to_uint());
0075     fprintf(file_, " %08x", pPhi.to_uint());
0076     fprintf(file_, " %08x", pId.to_uint());
0077   }
0078   fprintf(file_, "\n");
0079 }
0080 */
0081 result_t TauNNIdHW::compute(const l1t::PFCandidate &iSeed, std::vector<l1t::PFCandidate> &iParts) {
0082   for (unsigned i0 = 0; i0 < fNParticles_; i0++) {
0083     fPt_.get()[i0] = 0.;
0084     fEta_.get()[i0] = 0.;
0085     fPhi_.get()[i0] = 0.;
0086     fId_.get()[i0] = 0.;
0087   }
0088   std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) {
0089     return (pt_t(i.pt()) > pt_t(j.pt()));
0090   });
0091   for (unsigned int i0 = 0; i0 < iParts.size(); i0++) {
0092     if (i0 > fNParticles_)
0093       break;
0094     fPt_.get()[i0] = pt_t(iParts[i0].pt());
0095     fEta_.get()[i0] = etaphi_t(iSeed.eta() - iParts[i0].eta());
0096     etaphi_t lDPhi = etaphi_t(iSeed.phi()) - etaphi_t(iParts[i0].phi());
0097     etaphi_t lMPI = 3.1415;
0098     if (lDPhi > lMPI)
0099       lDPhi = lDPhi - lMPI;
0100     if (lDPhi < -lMPI)
0101       lDPhi = lDPhi + lMPI;
0102     fPhi_.get()[i0] = lDPhi;
0103     fId_.get()[i0] = id_t(iParts[i0].id());
0104   }
0105   SetNNVectorVar();
0106   return EvaluateNN();
0107 }