Belle II Software development
GRLNeuroModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <fstream>
10#include <sstream>
11#include <string>
12#include <unordered_map>
13#include <cstdlib>
14#include <iomanip>
15#include <cmath>
16#include <TFile.h>
17
18#include <framework/datastore/StoreArray.h>
19#include <framework/datastore/StoreObjPtr.h>
20#include <framework/dataobjects/EventMetaData.h>
21#include <framework/core/ModuleParam.templateDetails.h>
22#include <framework/gearbox/Unit.h>
23#include <framework/utilities/FileSystem.h>
24#include <analysis/utility/PCmsLabTransform.h>
25#include <trg/ecl/TrgEclMapping.h>
26#include <trg/ecl/dataobjects/TRGECLCluster.h>
27#include <trg/grl/dataobjects/GRLMLPData.h>
28#include <trg/grl/dataobjects/TRGGRLInfo.h>
29#include "trg/grl/dataobjects/TRGGRLUnpackerStore.h"
30#include "trg/grl/modules/trggrlneuralnet/GRLNeuroModule.h"
31
32
33using namespace Belle2;
34using namespace std;
35
36//-----------------------------------------------------------------
37// Register the Module
38//-----------------------------------------------------------------
40
41//-----------------------------------------------------------------
42// Implementation
43//-----------------------------------------------------------------
44//
46{
48 "The NeuroTrigger module of the GRL.\n"
49 "Takes CDC track and ECL cluster to prepare input data\n"
50 "for the training of a neural network.\n"
51 "Networks are trained after the event loop and saved."
52 );
54 // parameters for saving / loading
55 addParam("TrgGrlInformation", m_TrgGrlInformationName,
56 "Name of the StoreArray holding the information of tracks and clusters from cdc ecl klm.",
57 string("TRGGRLObjects"));
58 addParam("HistFileName", m_HistFileName,
59 "Name of the root file saving the output histogram.",
60 string("hist.root"));
61 addParam("SaveHist", m_saveHist,
62 "Save the output histogram to root file.",
63 false);
64 addParam("nMLP", m_parameters.nMLP,
65 "Number of expert MLPs.", 1u);
66 addParam("n_cdc_sector", m_parameters.n_cdc_sector,
67 "Number of expert CDC MLPs.", 0u);
68 addParam("n_ecl_sector", m_parameters.n_ecl_sector,
69 "Number of expert ECL MLPs.", 1u);
70 addParam("i_cdc_sector", m_parameters.i_cdc_sector,
71 "#cdc track of expert MLPs.", {0});
72 addParam("i_ecl_sector", m_parameters.i_ecl_sector,
73 "#ecl cluster of expert MLPs.", {24});
74 addParam("nHidden", m_parameters.nHidden,
75 "Number of nodes in each hidden layer for all networks "
76 "or factor to multiply with number of inputs (1 list or nMLP lists). "
77 "The number of layers is derived from the shape.",
78 {{24, 24, 24}});
79 addParam("multiplyHidden", m_parameters.multiplyHidden,
80 "If true, multiply nHidden with number of input nodes.", false);
81 addParam("outputScale", m_parameters.outputScale,
82 "Output scale for all networks (1 value list or nMLP value lists). "
83 "Output[i] of the MLP is scaled from [-1, 1] "
84 "to [outputScale[2*i], outputScale[2*i+1]]. "
85 "(units: z[cm] / theta[degree])",
86 {{ -1., 1.}});
87
88 addParam("csvThetaPhiFile", m_csvThetaPhiFile,
89 "CSV file for theta/phi correction per tcid.",
90 {FileSystem::findFile("/data/trg/grl/tcid_correct_theta_phi.csv", true)});
91
92 addParam("TRGECLClusters", m_TrgECLClusterName,
93 "Name of the StoreArray holding the information of trigger ecl clusters ",
94 string("TRGECLClusters"));
95 addParam("MVACut", m_nn_thres,
96 "Cut value applied to the MLP output",
97 {0});
98 addParam("useDB", m_useDB,
99 "Flag to use database to set config", true);
100
101 // // TRGGRLInfo must exist for this module
102 // addRequired<TRGGRLInfo>(m_TrgGrlInformationName);
103
104}
105
106
107void
109{
110
111 std::string csvPath = FileSystem::findFile(m_csvThetaPhiFile, true);
112 if (csvPath.empty()) {
113 B2ERROR("Cannot find tcid_correct_theta_phi.csv");
114 return;
115 }
116
117 std::ifstream infile(csvPath);
118 if (!infile.is_open()) {
119 B2ERROR(("Failed to open " + csvPath).c_str());
120 return;
121 }
122
123 std::string line;
124 bool isFirstLine = true;
125 while (std::getline(infile, line)) {
126 if (isFirstLine) {
127 isFirstLine = false;
128 continue;
129 }
130
131 std::stringstream ss(line);
132 std::string tc_str, theta_str, phi_str;
133 std::getline(ss, tc_str, ',');
134 std::getline(ss, theta_str, ',');
135 std::getline(ss, phi_str, ',');
136
137 if (tc_str.empty() || theta_str.empty() || phi_str.empty()) continue;
138
139 int tc = std::stoi(tc_str);
140 if (tc == 0) continue;
141 double theta = std::stod(theta_str) * TMath::RadToDeg();
142 double phi = std::stod(phi_str) * TMath::RadToDeg();
143
144 thetaMap[tc] = theta;
145 phiMap[tc] = phi;
146 }
147
148 infile.close();
149
150 TCThetaLab.clear();
151 TCPhiLab.clear();
152 TCThetaLab.reserve(576);
153 TCPhiLab.reserve(576);
154
155 for (int tc = 1; tc <= 576; ++tc) {
156 if (thetaMap.count(tc)) {
157 TCThetaLab.push_back(thetaMap[tc]);
158 TCPhiLab.push_back(phiMap[tc]);
159 } else {
160
161 TCThetaLab.push_back(0.0);
162 TCPhiLab.push_back(0.0);
163 }
164 }
165
166
167 B2INFO("Loaded TC geometry (theta/phi) from tcid_correct_theta_phi.csv");
168 B2INFO(Form("Example: TC 1 => theta = %.3f deg, phi = %.3f deg", TCThetaLab[0], TCPhiLab[0]));
169
170
171}
172
173
174void
176{
177 if (m_useDB) {
178 if (not m_db_trggrlconfig.isValid()) {
179 StoreObjPtr<EventMetaData> evtMetaData;
180 B2FATAL("No database for TRG GRL config. exp " << evtMetaData->getExperiment() << " run "
181 << evtMetaData->getRun());
182 } else {
183 m_parameters.nMLP = m_db_trggrlconfig->get_ecltaunn_nMLP();
184 m_parameters.multiplyHidden = m_db_trggrlconfig->get_ecltaunn_multiplyHidden();
185 m_parameters.nHidden = m_db_trggrlconfig->get_ecltaunn_nHidden();
186 m_parameters.n_cdc_sector = m_db_trggrlconfig->get_ecltaunn_n_cdc_sector();
187 m_parameters.n_ecl_sector = m_db_trggrlconfig->get_ecltaunn_n_ecl_sector();
188 m_parameters.i_cdc_sector = m_db_trggrlconfig->get_ecltaunn_i_cdc_sector();
189 m_parameters.i_ecl_sector = m_db_trggrlconfig->get_ecltaunn_i_ecl_sector();
190 m_nn_thres[0] = m_db_trggrlconfig->get_ecltaunn_threshold();
191 m_parameters.total_bit_bias = m_db_trggrlconfig->get_ecltaunn_total_bit_bias();
192 m_parameters.int_bit_bias = m_db_trggrlconfig->get_ecltaunn_int_bit_bias();
193 m_parameters.is_signed_bias = m_db_trggrlconfig->get_ecltaunn_is_signed_bias();
194 m_parameters.rounding_bias = m_db_trggrlconfig->get_ecltaunn_rounding_bias();
195 m_parameters.saturation_bias = m_db_trggrlconfig->get_ecltaunn_saturation_bias();
196 m_parameters.total_bit_accum = m_db_trggrlconfig->get_ecltaunn_total_bit_accum();
197 m_parameters.int_bit_accum = m_db_trggrlconfig->get_ecltaunn_int_bit_accum();
198 m_parameters.is_signed_accum = m_db_trggrlconfig->get_ecltaunn_is_signed_accum();
199 m_parameters.rounding_accum = m_db_trggrlconfig->get_ecltaunn_rounding_accum();
200 m_parameters.saturation_accum = m_db_trggrlconfig->get_ecltaunn_saturation_accum();
201 m_parameters.total_bit_weight = m_db_trggrlconfig->get_ecltaunn_total_bit_weight();
202 m_parameters.int_bit_weight = m_db_trggrlconfig->get_ecltaunn_int_bit_weight();
203 m_parameters.is_signed_weight = m_db_trggrlconfig->get_ecltaunn_is_signed_weight();
204 m_parameters.rounding_weight = m_db_trggrlconfig->get_ecltaunn_rounding_weight();
205 m_parameters.saturation_weight = m_db_trggrlconfig->get_ecltaunn_saturation_weight();
206 m_parameters.total_bit_relu = m_db_trggrlconfig->get_ecltaunn_total_bit_relu();
207 m_parameters.int_bit_relu = m_db_trggrlconfig->get_ecltaunn_int_bit_relu();
208 m_parameters.is_signed_relu = m_db_trggrlconfig->get_ecltaunn_is_signed_relu();
209 m_parameters.rounding_relu = m_db_trggrlconfig->get_ecltaunn_rounding_relu();
210 m_parameters.saturation_relu = m_db_trggrlconfig->get_ecltaunn_saturation_relu();
211 m_parameters.total_bit = m_db_trggrlconfig->get_ecltaunn_total_bit();
212 m_parameters.int_bit = m_db_trggrlconfig->get_ecltaunn_int_bit();
213 m_parameters.is_signed = m_db_trggrlconfig->get_ecltaunn_is_signed();
214 m_parameters.rounding = m_db_trggrlconfig->get_ecltaunn_rounding();
215 m_parameters.saturation = m_db_trggrlconfig->get_ecltaunn_saturation();
216 m_parameters.W_input = m_db_trggrlconfig->get_ecltaunn_W_input();
217 m_parameters.I_input = m_db_trggrlconfig->get_ecltaunn_I_input();
218
219 B2INFO("DB GRLConfig: nMLP=" << m_db_trggrlconfig->get_ecltaunn_nMLP()
220 << " weight=" << m_db_trggrlconfig->get_ecltaunn_weight().size()
221 << " bias=" << m_db_trggrlconfig->get_ecltaunn_bias().size()
222 << " total_bit_bias=" << m_db_trggrlconfig->get_ecltaunn_total_bit_bias().size());
223
224
225 m_GRLNeuro.initialize(m_parameters);
226
227 for (unsigned int isector = 0; isector < m_parameters.nMLP; isector++) {
228 if (!m_GRLNeuro.load(isector, m_db_trggrlconfig->get_ecltaunn_weight()[isector], m_db_trggrlconfig->get_ecltaunn_bias()[isector])) {
229 B2ERROR("weight of GRL ecltaunn could not be loaded correctly.");
230 }
231 }
232 }
233 }
234
235 if (m_saveHist) {
236 for (unsigned int isector = 0; isector < m_parameters.nMLP; isector++) {
237 h_target.push_back(new TH1D(("h_target_" + to_string(isector)).c_str(),
238 ("h_target_" + to_string(isector)).c_str(), 100, -40.0, 40.0));
239 }
240 }
241}
242
244{
245
246 //StoreArray<TRGECLTrg> trgArray;
248 // if (!trgInfo.isValid()) {
249 // std::cout << "[WARNING] TRGGRLObjects not found in this event!" << std::endl;
250 // return;
251 // }
252
254 int necl = eclTrgClusterArray.getEntries();
255
256 std::vector<std::tuple<float, float, float, float, int>> eclClusters; // TC id
257 eclClusters.reserve(necl);
258
259 for (int ic = 0; ic < necl; ic++) {
260 auto* eclCluster = eclTrgClusterArray[ic];
261 int TC = eclCluster->getMaxTCId();
262
263 //float energy = eclCluster->getEnergyDep() ; // data
264 float energy = eclTrgClusterArray[ic]->getEnergyDep() * 1000.0;//MC
265 float time = eclCluster->getTimeAve();
266 float theta = TCThetaLab[TC - 1 ];
267 float phi = TCPhiLab[TC - 1 ] + 180;
268 float thetaComp = theta;
269
270 eclClusters.emplace_back(energy, thetaComp, phi, time, TC);
271
272 }
273
274
275 std::sort(eclClusters.begin(), eclClusters.end(),
276 [](const std::tuple<float, float, float, float, int>& a,
277 const std::tuple<float, float, float, float, int>& b) {
278 return std::get<0>(a) > std::get<0>(b);
279 });
280 // ====quantization
281 std::vector<float> MLPinput(24, 0.0f);
282 for (size_t i = 0; i < eclClusters.size() && i < 6; i++) {
283 float energy, theta, phi, time;
284 int TC;
285 std::tie(energy, theta, phi, time, TC) = eclClusters[i];
286 MLPinput[i] = energy;
287 MLPinput[i + 6] = theta;
288 MLPinput[i + 12] = phi;
289 MLPinput[i + 18] = time;
290 }
291
292
293 float LSB_ADC = 1 / 5.25;
294 float LSB_angle = 1 / 1.40625;
295 std::for_each(MLPinput.begin() + 0, MLPinput.begin() + 6, [LSB_ADC](float & x) { x = std::ceil(x * LSB_ADC); });
296 std::for_each(MLPinput.begin() + 6, MLPinput.begin() + 12, [LSB_angle](float & x) { x = std::ceil(x * LSB_angle); });
297 std::for_each(MLPinput.begin() + 12, MLPinput.begin() + 18, [LSB_angle](float & x) { x = std::ceil(x * LSB_angle); });
298
299 for (size_t i = 0; i < eclClusters.size() && i < 6; i++) {
300 float energy, theta, phi, time;
301 int TC;
302 std::tie(energy, theta, phi, time, TC) = eclClusters[i];
303
304 }
305
306 // Run MLP
307 float target = m_GRLNeuro.runMLP(0, MLPinput);
308 if (m_saveHist) {
309 h_target[0]->Fill(target);
310 }
311 if (target > m_nn_thres[0]) {
312 trgInfo->setTauNN(true);
313 } else {
314 trgInfo->setTauNN(false);
315 }
316}
317
318
319
320void
322{
323 if (m_saveHist) {
324 TFile* file = new TFile(TString(m_HistFileName), "recreate");
325 for (unsigned int isector = 0; isector < m_parameters.nMLP; isector++) {
326 h_target[isector]->Write();
327 }
328 file->Write();
329 file->Close();
330 }
331
332}
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
std::vector< double > TCThetaLab
Polar angle of a given TRGcluster.
DBObjPtr< TRGGRLConfig > m_db_trggrlconfig
dbobject to store grl config
std::vector< double > TCPhiLab
Azimuthal angle of a given TRGcluster.
std::string m_HistFileName
Name of root file to save the histogram.
virtual void initialize() override
Initialize the module.
std::string m_TrgECLClusterName
Name of the StoreArray containing the ECL clusters.
GRLNeuro m_GRLNeuro
Instance of the NeuroTrigger.
virtual void event() override
Called once for each event.
GRLNeuro::Parameters m_parameters
Parameters for the NeuroTrigger.
GRLNeuroModule()
Constructor, for setting module description and parameters.
virtual void terminate() override
This method is called at the end of the event processing.
virtual void beginRun() override
Register run-dependent DataStore arrays.
std::string m_TrgGrlInformationName
name of TRG GRL information object
bool m_saveHist
save the output histogram
std::vector< float > m_nn_thres
cut on MVA to separate signal
std::vector< TH1D * > h_target
Histograms to save the NN classifiers.
bool m_useDB
save the ThetaPhi
Class to represent the GRL Neuro.
Definition GRLNeuro.h:34
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition StoreArray.h:216
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.