CMS 3D CMS Logo

ParameterAssignmentLayer.cc
Go to the documentation of this file.
1 #include <cmath>
2 
5 
9 
11 
12 using namespace emtf::phase2;
13 using namespace emtf::phase2::algo;
14 
16 
17 void ParameterAssignmentLayer::apply(const bool& displaced_en, std::vector<track_t>& tracks) const {
18  std::vector<int> feature_sites = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7,
19  8, 9, 10, 11, 0, 1, 2, 3, 4, 11, 0, 1, 2, 3, 4, 11, -1, -1, -1, -1};
20 
21  for (auto& track : tracks) { // Begin loop tracks
22  // Init Parameters
23  track.pt = 0;
24  track.rels = 0;
25  track.dxy = 0;
26  track.z0 = 0;
27  track.beta = 0;
28 
29  track.pt_address = 0;
30  track.rels_address = 0;
31  track.dxy_address = 0;
32 
33  // Short-Circuit: Skip invalid tracks
34  if (track.valid == 0) {
35  continue;
36  }
37 
38  // Get Features
39  const auto& site_mask = track.site_mask;
40  const auto& features = track.features;
41 
42  // Single batch of NTrackFeatures values
43  tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, v3::kNumTrackFeatures});
44 
45  if (this->context_.config_.verbosity_ > 1) {
46  edm::LogInfo("L1TEMTFpp") << "Parameter Assignment In"
47  << " disp " << displaced_en << " zone " << track.zone << " col " << track.col << " pat "
48  << track.pattern << " qual " << track.quality << " phi " << track.phi << " theta "
49  << track.theta << " features " << std::endl;
50  }
51 
52  // Prepare input tensor
53  float* input_data = input.flat<float>().data();
54 
55  for (unsigned int i_feature = 0; i_feature < v3::kNumTrackFeatures; ++i_feature) {
56  const auto& feature = features[i_feature];
57  const auto& feature_site = feature_sites[i_feature];
58 
59  bool mask_value = false;
60 
61  // Debug Info
62  if (this->context_.config_.verbosity_ > 1 && i_feature > 0) {
63  edm::LogInfo("L1TEMTFpp") << " ";
64  }
65 
66  // Mask invalid sites
67  if (feature_site > -1) {
68  mask_value = (site_mask[feature_site] == 0);
69  }
70 
71  if (mask_value) {
72  (*input_data) = 0.;
73 
74  // Debug Info
75  if (this->context_.config_.verbosity_ > 1) {
76  edm::LogInfo("L1TEMTFpp") << "0";
77  }
78  } else {
79  (*input_data) = feature.to_float();
80 
81  // Debug Info
82  if (this->context_.config_.verbosity_ > 1) {
83  edm::LogInfo("L1TEMTFpp") << feature.to_float();
84  }
85  }
86 
87  input_data++;
88  }
89 
90  // Debug Info
91  if (this->context_.config_.verbosity_ > 1) {
92  edm::LogInfo("L1TEMTFpp") << std::endl;
93  }
94 
95  // Select TF Session
96  auto* session_ptr = context_.prompt_session_ptr_;
97 
98  if (displaced_en) {
99  session_ptr = context_.disp_session_ptr_;
100  }
101 
102  // Evaluate Prompt
103  std::vector<tensorflow::Tensor> outputs;
104 
105  tensorflow::run(session_ptr,
106  {{"inputs", input}}, // Input layer name
107  {"Identity"}, // Output layer name
108  &outputs);
109 
110  // Assign parameters
111  if (displaced_en) {
112  // Read displaced pb outputs
113  auto pt_address = outputs[0].matrix<float>()(0, 0);
114  auto rels_address = outputs[0].matrix<float>()(0, 1);
115  auto dxy_address = outputs[0].matrix<float>()(0, 2);
116 
117  track.pt_address = std::clamp<float>(pt_address, -512, 511);
118  track.rels_address = std::clamp<float>(rels_address, -512, 511);
119  track.dxy_address = std::clamp<float>(dxy_address, -512, 511);
120 
121  track.q = (track.pt_address < 0);
123  track.rels = context_.activation_lut_.lookupRels(track.rels_address);
124  track.dxy = context_.activation_lut_.lookupDxy(track.dxy_address);
125  } else {
126  // Read prompt pb outputs
127  auto pt_address = outputs[0].matrix<float>()(0, 0);
128  auto rels_address = outputs[0].matrix<float>()(0, 1);
129 
130  track.pt_address = std::clamp<float>(pt_address, -512, 511);
131  track.rels_address = std::clamp<float>(rels_address, -512, 511);
132  track.dxy_address = 0;
133 
134  track.q = (track.pt_address < 0);
136  track.rels = context_.activation_lut_.lookupRels(track.rels_address);
137  track.dxy = 0;
138  }
139 
140  // DEBUG
141  if (this->context_.config_.verbosity_ > 1) {
142  edm::LogInfo("L1TEMTFpp") << "Parameter Assignment Out"
143  << " disp " << displaced_en << " zone " << track.zone << " col " << track.col << " pat "
144  << track.pattern << " qual " << track.quality << " q " << track.q << " pt " << track.pt
145  << " rels " << track.rels << " dxy " << track.dxy << " z0 " << track.z0 << " phi "
146  << track.phi << " theta " << track.theta << " beta " << track.beta << " pt_address "
147  << track.pt_address << " rels_address " << track.rels_address << " dxy_address "
148  << track.dxy_address << " valid " << track.valid << std::endl;
149  }
150  } // End loop tracks
151 }
tensorflow::Session * disp_session_ptr_
Definition: EMTFContext.h:48
void apply(const bool &, std::vector< track_t > &) const
constexpr int kNumTrackFeatures
Definition: EMTFConstants.h:40
static std::string const input
Definition: EdmProvDump.cc:50
data::ActivationLut activation_lut_
Definition: EMTFContext.h:55
tensorflow::Session * prompt_session_ptr_
Definition: EMTFContext.h:44
const trk_pt_t & lookupPromptPt(const trk_nn_address_t &) const
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:271
const trk_pt_t & lookupDispPt(const trk_nn_address_t &) const
const trk_dxy_t & lookupDxy(const trk_nn_address_t &) const
Log< level::Info, false > LogInfo
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
const trk_rels_t & lookupRels(const trk_nn_address_t &) const
EMTFConfiguration config_
Definition: EMTFContext.h:39