Belle II Software development
trgtopTRD2TTSConverterModule.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/top/modules/trgtopTRD2TTSConverter/trgtopTRD2TTSConverterModule.h>
10
11#include <trg/top/dataobjects/TRGTOPTimeStampsSlot.h>
12#include <trg/top/dataobjects/TRGTOPTimeStamp.h>
13#include <top/dataobjects/TOPDigit.h>
14#include <top/dataobjects/TOPRawDigit.h>
15
16/* --------------- WARNING ---------------------------------------------- *
17If you have more complex parameter types in your class then simple int,
18double or std::vector of those you might need to uncomment the following
19include directive to avoid an undefined reference on compilation.
20* ---------------------------------------------------------------------- */
21// #include <framework/core/ModuleParam.templateDetails.h>
22
23#include <iostream>
24
25//using namespace std;
26using namespace Belle2;
27
28//-----------------------------------------------------------------
29// Register the Module
30//-----------------------------------------------------------------
31REG_MODULE(TRGTOPTRD2TTSConverter);
32
34{
35 return std::string("1.00");
36}
37
38//-----------------------------------------------------------------
39// Implementation
40//-----------------------------------------------------------------
41
44{
45 // Set module properties
46
47
48 std::string desc = "TRGTOPTRD2TTSConverterModule(" + version() + ")" + "converts TOPRawDigits to TOP TRG timestamps";
49 setDescription(desc);
51
52 B2DEBUG(20, "TRGTOPTRD2TTSConverter: Constructor done.");
53
54 // Parameter definitions
55
56 // Add parameters
57 addParam("inputRawDigitsName", m_inputRawDigitsName,
58 "name of TOPRawDigit store array", std::string(""));
59
60 addParam("inputConvertedDigitsName", m_inputConvertedDigitsName,
61 "name of TOPDigit store array", std::string(""));
62
63 // Add parameters
64 addParam("outputTimeStampsSlotName", m_outputTimeStampsSlotName,
65 "name of TRGTOPTimeStampsSlot store array", std::string(""));
66
67 addParam("outputTimeStampName", m_outputTimeStampName,
68 "name of TRGTOPTimeStamp store array", std::string(""));
69
70 addParam("addRelations", m_addRelations, "if true, make relations to TOPRawDigits", true);
71
72 addParam("requireMinNumberOfTimeStamps", m_requireMinNumberOfTimeStamps, "if true, require minimum number of timestamps in a slot",
73 true);
74
75 addParam("minNumberOfTimeStamps", m_minNumberOfTimeStamps, "minimum number of timestamps in a slot (when required, default:5)",
76 MIN_NUMBER_OF_TIMESTAMPS);
77
78}
79
80TRGTOPTRD2TTSConverterModule::~TRGTOPTRD2TTSConverterModule()
81{
82}
83
85{
88
89 m_TRGTOPTimeStampsSlots.registerInDataStore(m_outputTimeStampsSlotName);
90 m_TRGTOPTimeStamps.registerInDataStore(m_outputTimeStampName);
91
92 m_TRGTOPTimeStampsSlots.registerRelationTo(m_TRGTOPTimeStamps);
93 m_TRGTOPTimeStamps.registerRelationTo(m_TRGTOPTimeStampsSlots);
94
95 m_TRGTOPTimeStamps.registerRelationTo(m_convertedDigits);
96 m_convertedDigits.registerRelationTo(m_TRGTOPTimeStamps);
97}
98
99
103
105{
106
107 // clear TimeStamps - this should be done elsewhere automatically
108 // m_TRGTOPTimeStampsSlots.clear();
109 // m_TRGTOPTimeStamps.clear();
110
111 for (int slot = 0; slot < NUMBER_OF_TOP_SLOTS; slot++) m_interimTimeStamps[slot].clear();
112
113 if (m_convertedDigits.getEntries() && m_rawDigits.getEntries()) {
114
115 for (const auto& convertedDigit : m_convertedDigits) {
116
117 // note that here we use convention 0 through 15
118 int slot = convertedDigit.getModuleID() - 1;
119
120 auto relRawDigits = convertedDigit.getRelationsTo<TOPRawDigit>();
121
122 if (relRawDigits.size()) {
123
124 int revo9Counter = relRawDigits[0]->getRevo9Counter();
125
126 // latency was calibrated on 01/16/22 by taking an average between revo9 of L1 and TOPTRG decision for events where TOP and ECL agree with each other
127 int deltaRevo9 = revo9Counter - latencyL1;
128
129 // 11520 = 1280*9
130 int revo9CounterEvent = deltaRevo9 >= 0 ? deltaRevo9 : revo9CounterMax + deltaRevo9 ;
131
132 int phase = 0;
133
134 for (int i = 1; i < 9; i++) {
135 if (revo9CounterEvent < timeOfWindows[i]) break;
136 phase++;
137 }
138
139 auto window = relRawDigits[0]->getASICWindow();
140 auto sample = relRawDigits[0]->getSampleRise();
141
142 if (phase >= 0 && phase <= 8) {
143
144 // we use 0.375, because our timestamps are in units of 2ns, so we pretend that FTSW clock is 127MHz
145 // will have to divide by 2 soon
146
147 double timeStamp_ns_d = ((numberOfWindows[phase] + window) * 64 + sample) * 0.375 + 0.5 + timeCorrection;
148 int timeStamp_ns = timeStamp_ns_d;
149
150 interimTimeStamp thisTimeStamp;
151 thisTimeStamp.slot = slot + 1;
152 // TG & VS: Feb. 8, 2022, the value in FW is in 2ns units
153 thisTimeStamp.value = timeStamp_ns / 2;
154 thisTimeStamp.refDigit = &convertedDigit;
155
156 m_interimTimeStamps[slot].push_back(thisTimeStamp);
157
158 }
159 }
160 }
161
162 for (int slot = 0; slot < NUMBER_OF_TOP_SLOTS; slot++) {
163
164 int numberOfTimeStamps = m_interimTimeStamps[slot].size();
165
166 if ((m_requireMinNumberOfTimeStamps && numberOfTimeStamps >= m_minNumberOfTimeStamps) || (!m_requireMinNumberOfTimeStamps
167 && numberOfTimeStamps)) {
168
169
170 // store decision in event store
171 auto* timeStampsSlotStore = m_TRGTOPTimeStampsSlots.appendNew(slot + 1, numberOfTimeStamps);
172
173 // int i = m_TRGTOPTimeStampsSlots.getEntries() - 1;
174 // m_TRGTOPTimeStampsSlots[i]->setSlotId(slot);
175 // m_TRGTOPTimeStampsSlots[i]->setNumberOfTimeStamps(numberOfTimeStamps);
176 // or
177 // timeStampsSlotStore->setSlotId(slot);
178 // timeStampsSlotStore->setNumberOfTimeStamps(numberOfTimeStamps);
179
180 // sort timestamps in increasing order of their values (i.e. time)
181
182 sort(m_interimTimeStamps[slot].begin(), m_interimTimeStamps[slot].end(), timeOrder());
183
184 for (std::vector<interimTimeStamp>::const_iterator it = m_interimTimeStamps[slot].begin(); it != m_interimTimeStamps[slot].end();
185 ++it) {
186
187 const interimTimeStamp& thisInterimTimeStamp = *it;
188
189 int value = thisInterimTimeStamp.value;
190
191 TRGTOPTimeStamp timeStamp(value, slot + 1);
192
193 auto* timeStampStore = m_TRGTOPTimeStamps.appendNew(timeStamp);
194
195 timeStampsSlotStore->addRelationTo(timeStampStore);
196 timeStampStore->addRelationTo(timeStampsSlotStore);
197
198 if (m_addRelations) {
199 timeStampStore->addRelationTo(thisInterimTimeStamp.refDigit);
200 thisInterimTimeStamp.refDigit->addRelationTo(timeStampStore);
201 }
202 }
203 }
204 }
205 }
206}
207
211
215
216
217
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
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
Class to store unpacked raw data (hits in feature-extraction format) It provides also calculation of ...
Definition TOPRawDigit.h:24
static constexpr int latencyL1
L1 latency in FTSW clock cycles.
StoreArray< TOPRawDigit > m_rawDigits
collection of raw digits
std::string m_outputTimeStampsSlotName
name of TOPTRGTimeStampsSlot store array
bool m_requireMinNumberOfTimeStamps
switch ON/OFF min number of timestamps requirement for individual slots
virtual void initialize() override
Initialize the Module.
std::string m_outputTimeStampName
name of TOPTRGTimeStamp store array
virtual void event() override
This method is the core of the module.
static constexpr int revo9CounterMax
max number of FTSW clocks in revo9 cycle
virtual void endRun() override
This method is called if the current run ends.
virtual void terminate() override
This method is called at the end of the event processing.
TRGTOPTRD2TTSConverterModule()
Constructor: Sets the description, the properties and the parameters of the module.
std::string m_inputRawDigitsName
name of TOPRawDigit store array
int m_eventNumber
Event number (according to L1/global)
virtual void beginRun() override
Called when entering a new run.
static constexpr int timeCorrection
time correction in ns (estimated by comparing trigger readout with main readout
StoreArray< TOPDigit > m_convertedDigits
collection of raw digits
bool m_addRelations
switch ON/OFF relations to TOPRawDigits
std::string m_inputConvertedDigitsName
name of TOPRawDigit store array
std::string version() const
returns version of TRGTOPTRD2TTSConverterModule.
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.