9 #include <trg/klm/modules/klmtrigger/KLMTriggerModule.h>
10 #include "trg/klm/modules/klmtrigger/group_helper.h"
14 #include "trg/klm/modules/klmtrigger/KLMAxis.h"
16 #include "trg/klm/modules/klmtrigger/KLM_Trig.h"
17 #include <trg/klm/modules/klmtrigger/klm_trig_linear_fit.h>
20 #include <klm/bklm/geometry/GeometryPar.h>
22 #include <framework/datastore/StoreArray.h>
23 #include <framework/datastore/StoreObjPtr.h>
26 #include <framework/dataobjects/EventMetaData.h>
29 #include <klm/dataobjects/KLMDigit.h>
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>
41 #include <trg/klm/dbobjects/KLMTriggerParameters.h>
42 #include <framework/database/DBObjPtr.h>
45 #include <unordered_map>
57 using namespace Belle2::group_helper;
67 using namespace Belle2::group_helper::KLM_Coordinates_n;
68 using namespace Belle2::group_helper::KLM_Generic;
69 using namespace Belle2::group_helper;
73 const std::string m_klmtrackCollectionName =
"TRGKLMTracks";
74 const std::string m_klmhitCollectionName =
"TRGKLMHits";
80 std::vector<Belle2::group_helper::KLM_geo_fit_t> m_data;
94 vector<string> split(
const string& str,
const string& delim)
96 vector<string> tokens;
97 size_t prev = 0, pos = 0;
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());
108 std::vector<int> layer_string_list_to_integer_range(
const std::string& instr)
110 std::vector<int> ret;
111 auto str_spl = split(instr,
":");
113 int start = std::stoi(str_spl[0]);
114 int stop = std::stoi(str_spl[1]);
115 for (
int i = start; i < stop ; ++i) {
121 std::vector<int> layer_string_list_to_integer_list(
const std::string& instr)
123 std::vector<int> ret;
124 auto str_spl = split(instr,
",");
126 for (
const auto& e : str_spl) {
127 ret.push_back(std::stoi(e));
132 std::vector<int> layer_string_list_to_integer(
const std::string& instr)
134 if (instr.find(
":") != string::npos) {
135 return layer_string_list_to_integer_range(instr);
137 if (instr.find(
",") != string::npos) {
138 return layer_string_list_to_integer_list(instr);
140 std::vector<int> ret;
143 KLMTriggerModule::KLMTriggerModule() :
Module()
145 setDescription(
"KLM trigger simulation");
146 setPropertyFlags(c_ParallelProcessingCertified);
148 addParam(
"nLayerTrigger", m_nLayerTrigger,
"", 8);
167 void KLMTriggerModule::initialize()
176 KLMTrgSummary.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
203 void KLMTriggerModule::beginRun()
207 B2DEBUG(100,
"KLMTrigger: Experiment " << evtMetaData->getExperiment() <<
", run " << evtMetaData->getRun());
209 B2FATAL(
"KLM trigger parameters are not available.");
214 for (
auto e : m_layerUsed) {
215 B2DEBUG(20,
"KLMTrigger: layer " << e <<
" used.");
217 }
catch (
const std::exception& e) {
218 B2FATAL(
"Something went wrong when parsing the 'm_whichLayers' string"
220 <<
LogVar(
"exception", e.what()));
223 m_geometry = std::make_shared<KLMTriggerModule::geometry_data>();
226 m_geometry->m_data.emplace_back(
238 }
catch (
const std::exception& er) {
245 void KLMTriggerModule::endRun()
252 isectors_t to_i_sector(
int KLM_type_,
int section_)
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);
264 return isectors_t(0);
269 template <
typename T1,
typename T2>
270 void fill_KLMTrgSummary(T1&
KLMTrgSummary,
const T2& summary)
273 KLMTrgSummary->setBKLM_n_trg_sectors(BKLM_n_trg_sectors(summary));
274 KLMTrgSummary->setEKLM_n_trg_sectors(EKLM_n_trg_sectors(summary));
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));
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));
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));
291 template <
typename T1,
typename T2>
292 auto push_linear_fit_to_KLMTrgFittedTrack(
const T1& linear_fited, T2& KLMTrgFittedTrack_)
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));
311 void KLMTriggerModule::event()
319 auto hits = fill_vector(klmDigits.
getEntries(),
321 letref digit = klmDigits[Index];
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())
336 drop_duplicates(hits);
337 erase_remove_if(hits, [layerUsed = this->m_layerUsed ](
const auto & ele) {
return !contains(layerUsed, layer(ele)); });
339 auto summary = make_trg(hits, m_event_nr, m_nLayerTrigger);
343 auto linear_fit = klm_trig_linear_fit(hits, m_geometry->m_data);
344 push_linear_fit_to_KLMTrgFittedTrack(linear_fit, KLMTrgFittedTrack_);
348 }
catch (
const std::exception& er) {
Class for accessing objects in the database.
Store KLM TRG track information as a ROOT object.
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.
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.
bool isValid() const
Check wether the array was registered.
int getEntries() const
Get the number of objects in the array.
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.
Type-safe access to single objects in the data store.
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.
Abstract base class for different kinds of events.