Belle II Software development
KLMTriggerModule.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 <trg/klm/modules/klmtrigger/KLMTriggerModule.h>
10
11
12#include "trg/klm/modules/klmtrigger/KLMAxis.h"
13
14#include "trg/klm/modules/klmtrigger/KLM_Trig.h"
15#include <trg/klm/modules/klmtrigger/klm_trig_linear_fit.h>
16
17#include <trg/klm/modules/klmtrigger/IO_csv.h>
18
19
20#include <klm/bklm/geometry/GeometryPar.h>
21// framework - DataStore
22#include <framework/datastore/StoreArray.h>
23#include <framework/datastore/StoreObjPtr.h>
24
25// event data
26#include <framework/dataobjects/EventMetaData.h>
27
28// digits
29#include <klm/dataobjects/KLMDigit.h>
30
31#include <trg/klm/dataobjects/KLMTriggerHit.h>
32#include <trg/klm/dataobjects/KLMTriggerTrack.h>
33#include <trg/klm/dataobjects/KLMTrgSummary.h>
34#include <trg/klm/dataobjects/KLMTrgFittedTrack.h>
35
36
37
38
39
40
41#include <trg/klm/dbobjects/KLMTriggerParameters.h>
42#include <framework/database/DBObjPtr.h>
43
44
45#include <unordered_map>
46#include <algorithm>
47#include <numeric>
48#include <vector>
49#include <tuple>
50#include <iostream>
51
52//#include <trg/klm/modules/klmtrigger/geometry.h>
53
54
55using namespace std;
56using namespace Belle2;
57
58
59
60
61
62
63
64
65
66using namespace Belle2;
67using namespace Belle2::KLM_TRG_definitions;
68
69
70
71// part of unused old Trigger collection
72const std::string m_klmtrackCollectionName = "TRGKLMTracks";
73const std::string m_klmhitCollectionName = "TRGKLMHits";
74// end
75
76
77
78
79
80
81//-----------------------------------------------------------------
82// Register the Module
83//-----------------------------------------------------------------
84REG_MODULE(KLMTrigger);
85
86//-----------------------------------------------------------------
87// Implementation
88//-----------------------------------------------------------------
89
90
91vector<string> split(const string& str, const string& delim)
92{
93 vector<string> tokens;
94 size_t prev = 0, pos = 0;
95 do {
96 pos = str.find(delim, prev);
97 if (pos == string::npos) pos = str.length();
98 string token = str.substr(prev, pos - prev);
99 if (!token.empty()) tokens.push_back(token);
100 prev = pos + delim.length();
101 } while (pos < str.length() && prev < str.length());
102 return tokens;
103}
104
105std::vector<int> layer_string_list_to_integer_range(const std::string& instr)
106{
107 std::vector<int> ret;
108 auto str_spl = split(instr, ":");
109
110 int start = std::stoi(str_spl[0]);
111 int stop = std::stoi(str_spl[1]);
112 for (int i = start; i < stop ; ++i) {
113 ret.push_back(i);
114 }
115
116 return ret;
117}
118std::vector<int> layer_string_list_to_integer_list(const std::string& instr)
119{
120 std::vector<int> ret;
121 auto str_spl = split(instr, ",");
122
123 for (const auto& e : str_spl) {
124 ret.push_back(std::stoi(e));
125 }
126
127 return ret;
128}
129std::vector<int> layer_string_list_to_integer(const std::string& instr)
130{
131 if (instr.find(":") != string::npos) {
132 return layer_string_list_to_integer_range(instr);
133 }
134 if (instr.find(",") != string::npos) {
135 return layer_string_list_to_integer_list(instr);
136 }
137 std::vector<int> ret;
138 return ret;
139}
140KLMTriggerModule::KLMTriggerModule() : Module()
141{
142 setDescription("KLM trigger simulation");
143 setPropertyFlags(c_ParallelProcessingCertified);
144
145 addParam("y_cutoff", y_cutoff, "", 500);
146 addParam("intercept_cutoff", m_intercept_cutoff, "", 500);
147 addParam("CSV_Dump_Path", m_dump_Path, "", m_dump_Path);
148
149}
150
151
152
153
154
155
156
157
158
159
160
161
162
164{
165 m_event_nr = 0;
166 StoreArray<KLMDigit> klmDigits;
167 klmDigits.isRequired();
168 if (!klmDigits.isValid())
169 return;
170
173
174
175
176 StoreArray<KLMTrgFittedTrack> KLMTrgFittedTrack_("KLMTrgFittedTrack");
178
179
180
181
182
183// unused
184
185 StoreArray<KLMTriggerHit> klmTriggerHits(m_klmhitCollectionName);
186 klmTriggerHits.registerInDataStore();
187 klmTriggerHits.registerRelationTo(klmDigits);
188
189 StoreArray<KLMTriggerTrack> klmTriggerTracks(m_klmtrackCollectionName);
190 klmTriggerTracks.registerInDataStore();
191 klmTriggerTracks.registerRelationTo(klmTriggerHits);
192// end unused
193
194 if (!m_dump_Path.empty()) {
195 get_IO_csv_handle().dump_path = m_dump_Path;
196 get_IO_csv_handle().do_dump = true;
197 }
198
199}
200
202{
203 StoreObjPtr<EventMetaData> evtMetaData;
205 B2DEBUG(100, "KLMTrigger: Experiment " << evtMetaData->getExperiment() << ", run " << evtMetaData->getRun());
206 if (not KLMTriggerParameters.isValid())
207 B2FATAL("KLM trigger parameters are not available.");
208
209 try {
210 m_layerUsed = layer_string_list_to_integer(KLMTriggerParameters->getWhichLayers());
211 B2DEBUG(20, "KLMTrigger: m_layerUsed " << KLMTriggerParameters->getWhichLayers());
212 for (auto e : m_layerUsed) {
213 B2DEBUG(20, "KLMTrigger: layer " << e << " used.");
214 }
215 } catch (const std::exception& e) {
216 B2FATAL("Something went wrong when parsing the 'm_whichLayers' string"
218 << LogVar("exception", e.what()));
219 }
220 try {
221 m_klmtrg_layer_counter = std::make_shared< Belle2::klmtrg_layer_counter_t>();
222 m_klm_trig_linear_fit = std::make_shared< Belle2::klm_trig_linear_fit_t>();
223 m_klm_trig_linear_fit->set_y_cutoff(y_cutoff);
224 m_klm_trig_linear_fit->set_intercept_cutoff(m_intercept_cutoff);
225
226
227 m_klmtrg_layer_counter->set_NLayerTrigger(KLMTriggerParameters->getNLayers());
228
229 for (auto e : m_layerUsed) {
230 m_klmtrg_layer_counter->add_layersUsed(e);
231 }
232
233
235
236
237 for (size_t i = 0 ; i < KLMTriggerParameters->getGeometryDataSize() ; ++i) {
238 e.subdetector = KLMTriggerParameters->getSubdetector(i);
239 e.section = KLMTriggerParameters->getSection(i);
240 e.sector = KLMTriggerParameters->getSector(i);
241 e.layer = KLMTriggerParameters->getLayer(i);
242 e.plane = KLMTriggerParameters->getPlane(i);
243 e.slopeX = KLMTriggerParameters->getSlopeX(i);
244 e.offsetX = KLMTriggerParameters->getOffsetX(i);
245 e.slopeY = KLMTriggerParameters->getSlopeY(i);
246 e.offsetY = KLMTriggerParameters->getOffsetY(i);
247 m_klm_trig_linear_fit->add_geometry(e);
248
249 }
250
251
252
253 } catch (const std::exception& er) {
254 B2FATAL(er.what());
255 }
256
257}
258
259
261{
262
263
264}
265
266
267
268
269
270
271
272
273
274template <typename T1, typename T2>
275auto push_linear_fit_to_KLMTrgFittedTrack(T1&& linear_fited, T2& KLMTrgFittedTrack_)
276{
277 for (const auto& e : linear_fited) {
278 //if (e.slopeXY >= 1e100) { continue; }
279 auto FittedTrack = KLMTrgFittedTrack_.appendNew();
280 FittedTrack->setSlopeXY(e.slopeXY) ;
281 FittedTrack->setInterceptXY(e.interceptXY);
282 FittedTrack->setIpXY(e.ipXY) ;
283 FittedTrack->setPlane(e.plane);
284 FittedTrack->setChisqXY(e.chisqXY);
285 FittedTrack->setSubdetector(e.subdetector);
286 FittedTrack->setSection(e.section);
287 FittedTrack->setSector(e.sector);
288 FittedTrack->setNhits(e.Nhits);
289 FittedTrack->setTrack_id(e.track_id);
290
291 }
292
293}
294
295
296
297
298
299
300
302{
303
304 try {
305 ++m_event_nr;
306 StoreArray<KLMDigit> klmDigits;
307 StoreArray<KLMTrgFittedTrack> KLMTrgFittedTrack_("KLMTrgFittedTrack");
309 KLMTrgSummary.create();
310
311
312 get_IO_csv_handle().event_nr = m_event_nr;
314 auto hits = nt::algorithms::fill_vector(klmDigits.getEntries(),
315 [&](auto Index) {
316 const auto& digit = klmDigits[Index];
317
318
319 dummy.event_nr = m_event_nr;
320 dummy.subdetector = digit->getSubdetector();
321 dummy.section = digit->getSection();
322 dummy.sector = digit->getSector();
323 dummy.plane = digit->getPlane();
324 dummy.layer = digit->getLayer();
325 dummy.strip = digit->getStrip();
326 return dummy;
327
328 });
329
330 m_klmtrg_layer_counter->run(hits);
331 m_klm_trig_linear_fit->run(hits);
332
333
334 push_linear_fit_to_KLMTrgFittedTrack(m_klm_trig_linear_fit->get_result(), KLMTrgFittedTrack_);
335
336 KLMTrgSummary->setBKLM_back_to_back_flag(
337 m_klmtrg_layer_counter->get_BKLM_back_to_back_flag(KLMElementNumbers::c_BKLM)
338 );
339
340 KLMTrgSummary->setEKLM_back_to_back_flag(
341 m_klmtrg_layer_counter->get_BKLM_back_to_back_flag(KLMElementNumbers::c_EKLM)
342 );
343
344 KLMTrgSummary->setBKLM_n_trg_sectors(
345 m_klmtrg_layer_counter->get_n_sections_trig(KLMElementNumbers::c_BKLM)
346 );
347 KLMTrgSummary->setEKLM_n_trg_sectors(
348 m_klmtrg_layer_counter->get_n_sections_trig(KLMElementNumbers::c_EKLM)
349 );
350 KLMTrgSummary->setSector_mask_Backward_Barrel(
351 m_klmtrg_layer_counter->get_triggermask(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_BackwardSection)
352 );
353
354 KLMTrgSummary->setSector_mask_Forward_Barrel(
355 m_klmtrg_layer_counter->get_triggermask(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_ForwardSection)
356 );
357 KLMTrgSummary->setSector_mask_Backward_Endcap(
358 m_klmtrg_layer_counter->get_triggermask(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_BackwardSection)
359 );
360
361 KLMTrgSummary->setSector_mask_Forward_Endcap(
362 m_klmtrg_layer_counter->get_triggermask(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_ForwardSection)
363 );
364
365
366
367
368
369
370
371 KLMTrgSummary->setSector_mask_SLF_Backward_Barrel(
372 m_klm_trig_linear_fit->get_triggermask(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_BackwardSection)
373 );
374
375 KLMTrgSummary->setSector_mask_SLF_Forward_Barrel(
376 m_klm_trig_linear_fit->get_triggermask(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_ForwardSection)
377 );
378 KLMTrgSummary->setSector_mask_SLF_Backward_Endcap(
379 m_klm_trig_linear_fit->get_triggermask(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_BackwardSection)
380 );
381
382 KLMTrgSummary->setSector_mask_SLF_Forward_Endcap(
383 m_klm_trig_linear_fit->get_triggermask(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_ForwardSection)
384 );
385
386
387 KLMTrgSummary->setSector_mask_SLF_OR_Backward_Barrel(
388 m_klm_trig_linear_fit->get_triggermask_or(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_BackwardSection)
389 );
390
391 KLMTrgSummary->setSector_mask_SLF_OR_Forward_Barrel(
392 m_klm_trig_linear_fit->get_triggermask_or(KLMElementNumbers::c_BKLM, BKLMElementNumbers::c_ForwardSection)
393 );
394 KLMTrgSummary->setSector_mask_SLF_OR_Backward_Endcap(
395 m_klm_trig_linear_fit->get_triggermask_or(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_BackwardSection)
396 );
397
398 KLMTrgSummary->setSector_mask_SLF_OR_Forward_Endcap(
399 m_klm_trig_linear_fit->get_triggermask_or(KLMElementNumbers::c_EKLM, EKLMElementNumbers::c_ForwardSection)
400 );
401
402
403 } catch (const std::exception& er) {
404 B2FATAL(er.what());
405 }
406}
407
408
409
410
411
412
413
414
415
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
Store KLM TRG track information as a ROOT object.
Definition: KLMTrgSummary.h:20
virtual void initialize() override
Initialize the Module.
virtual void event() override
This method is the core of the module.
virtual void endRun() override
This method is called if the current run ends.
std::string m_dump_Path
Name of the file for debugging output.
virtual void beginRun() override
Called when entering a new run.
DBObject containing parameters used in KLMTrigger module.
int32_t getNLayers() const
Get the number of layers used in the trigger logic.
std::string getWhichLayers() const
Get the pattern of layers used in the trigger logic.
Base class for Modules.
Definition: Module.h:72
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:140
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Class to store variables with their name which were sent to the logging service.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
STL namespace.