Belle II Software  release-08-01-10
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 #include "trg/klm/modules/klmtrigger/group_helper.h"
11 
12 
13 
14 #include "trg/klm/modules/klmtrigger/KLMAxis.h"
15 
16 #include "trg/klm/modules/klmtrigger/KLM_Trig.h"
17 #include <trg/klm/modules/klmtrigger/klm_trig_linear_fit.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 
53 
54 
55 using namespace std;
56 using namespace Belle2;
57 using namespace Belle2::group_helper;
58 
59 
60 
61 
62 
63 
64 
65 
66 using namespace Belle2;
67 using namespace Belle2::group_helper::KLM_Coordinates_n;
68 using namespace Belle2::group_helper::KLM_Generic;
69 using namespace Belle2::group_helper;
70 
71 
72 // part of unused old Trigger collection
73 const std::string m_klmtrackCollectionName = "TRGKLMTracks";
74 const std::string m_klmhitCollectionName = "TRGKLMHits";
75 // end
76 
77 
78 
80  std::vector<Belle2::group_helper::KLM_geo_fit_t> m_data;
81 };
82 
83 
84 //-----------------------------------------------------------------
85 // Register the Module
86 //-----------------------------------------------------------------
87 REG_MODULE(KLMTrigger);
88 
89 //-----------------------------------------------------------------
90 // Implementation
91 //-----------------------------------------------------------------
92 
93 
94 vector<string> split(const string& str, const string& delim)
95 {
96  vector<string> tokens;
97  size_t prev = 0, pos = 0;
98  do {
99  pos = str.find(delim, prev);
100  if (pos == string::npos) pos = str.length();
101  string token = str.substr(prev, pos - prev);
102  if (!token.empty()) tokens.push_back(token);
103  prev = pos + delim.length();
104  } while (pos < str.length() && prev < str.length());
105  return tokens;
106 }
107 
108 std::vector<int> layer_string_list_to_integer_range(const std::string& instr)
109 {
110  std::vector<int> ret;
111  auto str_spl = split(instr, ":");
112 
113  int start = std::stoi(str_spl[0]);
114  int stop = std::stoi(str_spl[1]);
115  for (int i = start; i < stop ; ++i) {
116  ret.push_back(i);
117  }
118 
119  return ret;
120 }
121 std::vector<int> layer_string_list_to_integer_list(const std::string& instr)
122 {
123  std::vector<int> ret;
124  auto str_spl = split(instr, ",");
125 
126  for (const auto& e : str_spl) {
127  ret.push_back(std::stoi(e));
128  }
129 
130  return ret;
131 }
132 std::vector<int> layer_string_list_to_integer(const std::string& instr)
133 {
134  if (instr.find(":") != string::npos) {
135  return layer_string_list_to_integer_range(instr);
136  }
137  if (instr.find(",") != string::npos) {
138  return layer_string_list_to_integer_list(instr);
139  }
140  std::vector<int> ret;
141  return ret;
142 }
143 KLMTriggerModule::KLMTriggerModule() : Module()
144 {
145  setDescription("KLM trigger simulation");
146  setPropertyFlags(c_ParallelProcessingCertified);
147 
148  addParam("nLayerTrigger", m_nLayerTrigger, "", 8);
149 
150 // addParam("LayerUsed", m_dummy_used_layers, "List of layers used for the simulation", string("0:16"));
151 
152 
153 }
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 void KLMTriggerModule::initialize()
168 {
169  m_event_nr = 0;
170  StoreArray<KLMDigit> klmDigits;
171  klmDigits.isRequired();
172  if (!klmDigits.isValid())
173  return;
174 
176  KLMTrgSummary.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
177 
178 
179 
180  StoreArray<KLMTrgFittedTrack> KLMTrgFittedTrack_("KLMTrgFittedTrack");
181  KLMTrgFittedTrack_.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
182 
183 
184 
185 
186 
187 // unused
188 
189  StoreArray<KLMTriggerHit> klmTriggerHits(m_klmhitCollectionName);
190  klmTriggerHits.registerInDataStore();
191  klmTriggerHits.registerRelationTo(klmDigits);
192 
193  StoreArray<KLMTriggerTrack> klmTriggerTracks(m_klmtrackCollectionName);
194  klmTriggerTracks.registerInDataStore();
195  klmTriggerTracks.registerRelationTo(klmTriggerHits);
196 // end unused
197 
198 
199 
200 
201 }
202 
203 void KLMTriggerModule::beginRun()
204 {
205  StoreObjPtr<EventMetaData> evtMetaData;
207  B2DEBUG(100, "KLMTrigger: Experiment " << evtMetaData->getExperiment() << ", run " << evtMetaData->getRun());
208  if (not KLMTriggerParameters.isValid())
209  B2FATAL("KLM trigger parameters are not available.");
210  m_nLayerTrigger = KLMTriggerParameters->getNLayers();
211  try {
212  m_layerUsed = layer_string_list_to_integer(KLMTriggerParameters->getWhichLayers());
213  B2DEBUG(20, "KLMTrigger: m_layerUsed " << KLMTriggerParameters->getWhichLayers());
214  for (auto e : m_layerUsed) {
215  B2DEBUG(20, "KLMTrigger: layer " << e << " used.");
216  }
217  } catch (const std::exception& e) {
218  B2FATAL("Something went wrong when parsing the 'm_whichLayers' string"
220  << LogVar("exception", e.what()));
221  }
222  try {
223  m_geometry = std::make_shared<KLMTriggerModule::geometry_data>();
224 
225  for (size_t i = 0; i < KLMTriggerParameters->getGeometryDataSize() ; ++i) {
226  m_geometry->m_data.emplace_back(
227  Subdetector(KLMTriggerParameters->getSubdetector(i)),
228  section(KLMTriggerParameters->getSection(i)),
229  sector(KLMTriggerParameters->getSector(i)),
230  layer(KLMTriggerParameters->getLayer(i)),
231  plane(KLMTriggerParameters->getPlane(i)),
232  geofit::slopeX(KLMTriggerParameters->getSlopeX(i)),
233  geofit::offsetX(KLMTriggerParameters->getOffsetX(i)),
234  geofit::slopeY(KLMTriggerParameters->getSlopeY(i)),
235  geofit::offsetY(KLMTriggerParameters->getOffsetY(i))
236  );
237  }
238  } catch (const std::exception& er) {
239  B2FATAL(er.what());
240  }
241 
242 }
243 
244 
245 void KLMTriggerModule::endRun()
246 {
247 
248 
249 }
250 
251 
252 isectors_t to_i_sector(int KLM_type_, int section_)
253 {
254  if (KLM_type_ == KLMElementNumbers::c_BKLM && section_ == BKLMElementNumbers::c_BackwardSection) {
255  return isectors_t(Belle2::KLM_TRG_definitions::c_backward_bklm);
256  } else if (KLM_type_ == KLMElementNumbers::c_BKLM && section_ == BKLMElementNumbers::c_ForwardSection) {
257  return isectors_t(Belle2::KLM_TRG_definitions::c_forward_bklm);
258  } else if (KLM_type_ == KLMElementNumbers::c_EKLM && section_ == EKLMElementNumbers::c_BackwardSection) {
259  return isectors_t(Belle2::KLM_TRG_definitions::c_backward_eklm);
260  } else if (KLM_type_ == KLMElementNumbers::c_EKLM && section_ == EKLMElementNumbers::c_ForwardSection) {
261  return isectors_t(Belle2::KLM_TRG_definitions::c_forward_eklm);
262  }
263 
264  return isectors_t(0);
265 }
266 
267 
268 
269 template <typename T1, typename T2>
270 void fill_KLMTrgSummary(T1& KLMTrgSummary, const T2& summary)
271 {
272  KLMTrgSummary.create();
273  KLMTrgSummary->setBKLM_n_trg_sectors(BKLM_n_trg_sectors(summary));
274  KLMTrgSummary->setEKLM_n_trg_sectors(EKLM_n_trg_sectors(summary));
275 
276  KLMTrgSummary->setSector_mask_Backward_Barrel(Sector_mask_Backward_Barrel(summary));
277  KLMTrgSummary->setSector_mask_Forward_Barrel(Sector_mask_Forward_Barrel(summary));
278  KLMTrgSummary->setSector_mask_Backward_Endcap(Sector_mask_Backward_Endcap(summary));
279  KLMTrgSummary->setSector_mask_Forward_Endcap(Sector_mask_Forward_Endcap(summary));
280 
281  KLMTrgSummary->setSector_mask_OR_Backward_Barrel(Sector_mask_OR_Backward_Barrel(summary));
282  KLMTrgSummary->setSector_mask_OR_Forward_Barrel(Sector_mask_OR_Forward_Barrel(summary));
283  KLMTrgSummary->setSector_mask_OR_Backward_Endcap(Sector_mask_OR_Backward_Endcap(summary));
284  KLMTrgSummary->setSector_mask_OR_Forward_Endcap(Sector_mask_OR_Forward_Endcap(summary));
285 
286  KLMTrgSummary->setBKLM_back_to_back_flag(BKLM_back_to_back_flag(summary));
287  KLMTrgSummary->setEKLM_back_to_back_flag(EKLM_back_to_back_flag(summary));
288 }
289 
290 
291 template <typename T1, typename T2>
292 auto push_linear_fit_to_KLMTrgFittedTrack(const T1& linear_fited, T2& KLMTrgFittedTrack_)
293 {
294  for (const auto& e : linear_fited) {
295  if (slopeXY_t(e) >= 1e100) { continue; }
296  auto FittedTrack = KLMTrgFittedTrack_.appendNew();
297  FittedTrack->setSlopeXY(slopeXY_t(e)) ;
298  FittedTrack->setInterceptXY(interceptXY_t(e));
299  FittedTrack->setIpXY(ipXY_t(e)) ;
300  FittedTrack->setPlane(plane(e));
301  FittedTrack->setChisqXY(chisqXY_t(e));
302  FittedTrack->setSubdetector(Subdetector(e));
303  FittedTrack->setSection(section(e));
304  FittedTrack->setSector(sector(e));
305  FittedTrack->setNhits(Nhits_t(e));
306 
307  }
308 
309 }
310 
311 void KLMTriggerModule::event()
312 {
313 
314  try {
315  StoreArray<KLMDigit> klmDigits;
316  StoreArray<KLMTrgFittedTrack> KLMTrgFittedTrack_("KLMTrgFittedTrack");
318 
319  auto hits = fill_vector(klmDigits.getEntries(),
320  [&](auto Index) {
321  letref digit = klmDigits[Index];
322  return std::tuple(
323  event_nr(m_event_nr),
324  Subdetector(digit->getSubdetector()),
325  section(digit->getSection()),
326  to_i_sector(digit->getSubdetector(), digit->getSection()),
327  sector(digit->getSector() - 1),
328  plane(digit->getPlane() - (digit->getSubdetector() == 2)),
329  layer(digit->getLayer() - 1),
330  strip(digit->getStrip())
331  );
332  });
333 
334 
335  sort(hits);
336  drop_duplicates(hits);
337  erase_remove_if(hits, [layerUsed = this->m_layerUsed ](const auto & ele) { return !contains(layerUsed, layer(ele)); });
338 
339  auto summary = make_trg(hits, m_event_nr, m_nLayerTrigger);
340  fill_KLMTrgSummary(KLMTrgSummary, summary);
341 
342 
343  auto linear_fit = klm_trig_linear_fit(hits, m_geometry->m_data);
344  push_linear_fit_to_KLMTrgFittedTrack(linear_fit, KLMTrgFittedTrack_);
345 
346 
347  ++m_event_nr;
348  } catch (const std::exception& er) {
349  B2FATAL(er.what());
350  }
351 }
352 
353 
354 
355 
356 
357 
358 
359 
360 
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Store KLM TRG track information as a ROOT object.
Definition: KLMTrgSummary.h:20
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.