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/* --------------- WARNING ---------------------------------------------- *
12If you have more complex parameter types in your class then simple int,
13double or std::vector of those you might need to uncomment the following
14include directive to avoid an undefined reference on compilation.
15* ---------------------------------------------------------------------- */
16// #include <framework/core/ModuleParam.templateDetails.h>
17
18#include <iostream>
19
20//using namespace std;
21using namespace Belle2;
22
23//-----------------------------------------------------------------
24// Register the Module
25//-----------------------------------------------------------------
26REG_MODULE(TRGTOPTRD2TTSConverter);
27
29{
30 return std::string("1.00");
31}
32
33//-----------------------------------------------------------------
34// Implementation
35//-----------------------------------------------------------------
36
38 : Module::Module(), m_eventNumber(0)
39{
40 // Set module properties
41
42
43 std::string desc = "TRGTOPTRD2TTSConverterModule(" + version() + ")" + "converts TOPRawDigits to TOP TRG timestamps";
44 setDescription(desc);
46
47 B2DEBUG(20, "TRGTOPTRD2TTSConverter: Constructor done.");
48
49 // Parameter definitions
50
51 // Add parameters
52 addParam("inputRawDigitsName", m_inputRawDigitsName,
53 "name of TOPRawDigit store array", std::string(""));
54
55 addParam("inputConvertedDigitsName", m_inputConvertedDigitsName,
56 "name of TOPDigit store array", std::string(""));
57
58 // Add parameters
59 addParam("outputTimeStampsSlotName", m_outputTimeStampsSlotName,
60 "name of TRGTOPTimeStampsSlot store array", std::string(""));
61
62 addParam("outputTimeStampName", m_outputTimeStampName,
63 "name of TRGTOPTimeStamp store array", std::string(""));
64
65 addParam("addRelations", m_addRelations, "if true, make relations to TOPRawDigits", true);
66
67 addParam("requireMinNumberOfTimeStamps", m_requireMinNumberOfTimeStamps, "if true, require minimum number of timestamps in a slot",
68 true);
69
70 addParam("minNumberOfTimeStamps", m_minNumberOfTimeStamps, "minimum number of timestamps in a slot (when required, default:5)",
71 MIN_NUMBER_OF_TIMESTAMPS);
72
73}
74
75TRGTOPTRD2TTSConverterModule::~TRGTOPTRD2TTSConverterModule()
76{
77}
78
80{
83
84 m_TRGTOPTimeStampsSlots.registerInDataStore(m_outputTimeStampsSlotName);
85 m_TRGTOPTimeStamps.registerInDataStore(m_outputTimeStampName);
86
87 m_TRGTOPTimeStampsSlots.registerRelationTo(m_TRGTOPTimeStamps);
88 m_TRGTOPTimeStamps.registerRelationTo(m_TRGTOPTimeStampsSlots);
89
90 m_TRGTOPTimeStamps.registerRelationTo(m_convertedDigits);
91 m_convertedDigits.registerRelationTo(m_TRGTOPTimeStamps);
92}
93
94
96{
97}
98
100{
101
102 // clear TimeStamps - this should be done elsewhere automatically
103 // m_TRGTOPTimeStampsSlots.clear();
104 // m_TRGTOPTimeStamps.clear();
105
106 for (int slot = 0; slot < NUMBER_OF_TOP_SLOTS; slot++) m_interimTimeStamps[slot].clear();
107
108 if (m_convertedDigits.getEntries() && m_rawDigits.getEntries()) {
109
110 for (const auto& convertedDigit : m_convertedDigits) {
111
112 // note that here we use convention 0 through 15
113 int slot = convertedDigit.getModuleID() - 1;
114
115 auto relRawDigits = convertedDigit.getRelationsTo<TOPRawDigit>();
116
117 if (relRawDigits.size()) {
118
119 int revo9Counter = relRawDigits[0]->getRevo9Counter();
120
121 // 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
122 int deltaRevo9 = revo9Counter - latencyL1;
123
124 // 11520 = 1280*9
125 int revo9CounterEvent = deltaRevo9 >= 0 ? deltaRevo9 : revo9CounterMax + deltaRevo9 ;
126
127 int phase = 0;
128
129 for (int i = 1; i < 9; i++) {
130 if (revo9CounterEvent < timeOfWindows[i]) break;
131 phase++;
132 }
133
134 auto window = relRawDigits[0]->getASICWindow();
135 auto sample = relRawDigits[0]->getSampleRise();
136
137 if (phase >= 0 && phase <= 8) {
138
139 // we use 0.375, because our timestamps are in units of 2ns, so we pretend that FTSW clock is 127MHz
140 // will have to divide by 2 soon
141
142 double timeStamp_ns_d = ((numberOfWindows[phase] + window) * 64 + sample) * 0.375 + 0.5 + timeCorrection;
143 int timeStamp_ns = timeStamp_ns_d;
144
145 interimTimeStamp thisTimeStamp;
146 thisTimeStamp.slot = slot + 1;
147 // TG & VS: Feb. 8, 2022, the value in FW is in 2ns units
148 thisTimeStamp.value = timeStamp_ns / 2;
149 thisTimeStamp.refDigit = &convertedDigit;
150
151 m_interimTimeStamps[slot].push_back(thisTimeStamp);
152
153 }
154 }
155 }
156
157 for (int slot = 0; slot < NUMBER_OF_TOP_SLOTS; slot++) {
158
159 int numberOfTimeStamps = m_interimTimeStamps[slot].size();
160
161 if ((m_requireMinNumberOfTimeStamps && numberOfTimeStamps >= m_minNumberOfTimeStamps) || (!m_requireMinNumberOfTimeStamps
162 && numberOfTimeStamps)) {
163
164
165 // store decision in event store
166 auto* timeStampsSlotStore = m_TRGTOPTimeStampsSlots.appendNew(slot + 1, numberOfTimeStamps);
167
168 // int i = m_TRGTOPTimeStampsSlots.getEntries() - 1;
169 // m_TRGTOPTimeStampsSlots[i]->setSlotId(slot);
170 // m_TRGTOPTimeStampsSlots[i]->setNumberOfTimeStamps(numberOfTimeStamps);
171 // or
172 // timeStampsSlotStore->setSlotId(slot);
173 // timeStampsSlotStore->setNumberOfTimeStamps(numberOfTimeStamps);
174
175 // sort timestamps in increasing order of their values (i.e. time)
176
177 sort(m_interimTimeStamps[slot].begin(), m_interimTimeStamps[slot].end(), timeOrder());
178
179 for (std::vector<interimTimeStamp>::const_iterator it = m_interimTimeStamps[slot].begin(); it != m_interimTimeStamps[slot].end();
180 ++it) {
181
182 const interimTimeStamp& thisInterimTimeStamp = *it;
183
184 int value = thisInterimTimeStamp.value;
185
186 TRGTOPTimeStamp timeStamp(value, slot + 1);
187
188 auto* timeStampStore = m_TRGTOPTimeStamps.appendNew(timeStamp);
189
190 timeStampsSlotStore->addRelationTo(timeStampStore);
191 timeStampStore->addRelationTo(timeStampsSlotStore);
192
193 if (m_addRelations) {
194 timeStampStore->addRelationTo(thisInterimTimeStamp.refDigit);
195 thisInterimTimeStamp.refDigit->addRelationTo(timeStampStore);
196 }
197 }
198 }
199 }
200 }
201}
202
204{
205}
206
208{
209}
210
211
212
Base class for Modules.
Definition: Module.h:72
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
@ 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
unsigned short getRevo9Counter() const
Returns 127 MHz clock ticks since last revo9 marker.
Definition: TOPRawDigit.h:333
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.
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
virtual void beginRun() override
Called when entering a new run.
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.
Example Detector.
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:560
#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.