Belle II Software development
CDCTriggerUnpackerModule.h
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#ifndef CDCTRIGGERUNPACKERMODULE_H
10#define CDCTRIGGERUNPACKERMODULE_H
11
12#include <framework/core/Module.h>
13#include <rawdata/dataobjects/RawTRG.h>
14#include <framework/datastore/StoreArray.h>
15#include <framework/datastore/StoreObjPtr.h>
16#include <framework/database/DBObjPtr.h>
17
18#include <trg/cdc/dataobjects/Bitstream.h>
19#include <trg/cdc/Unpacker.h>
20#include <trg/cdc/dataobjects/CDCTriggerTrack.h>
21#include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
22#include <trg/cdc/dataobjects/CDCTriggerFinderClone.h>
23#include <trg/cdc/dataobjects/CDCTriggerMLPInput.h>
24#include <trg/cdc/dbobjects/CDCTrigger2DConfig.h>
25#include <trg/cdc/dataobjects/CDCTriggerMLP.h>
26#include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
27#include <framework/dataobjects/BinnedEventT0.h>
28
29#include <array>
30#include <bitset>
31#include <vector>
32#include <string>
33#include <iomanip>
34
35namespace Belle2 {
42 using NodeList = std::vector<std::vector<int> >;
43
45 static constexpr int mergerWidth = 256;
47 static constexpr int nAllMergers = 146;
49 static constexpr int wordWidth = 32;
51 static constexpr int nFinesse = 48;
53 using MergerBus = std::array<std::bitset<mergerWidth>, nAllMergers>;
56
58 struct SubTrigger {
60 SubTrigger(const std::string& inName,
61 unsigned inEventWidth, unsigned inOffset,
62 int inHeaderSize, const std::vector<int>& inNodeID, const std::vector<int>& inNodeID_pcie40,
63 int& inDelay, int& inCnttrg, int inDebugLevel = 0) :
64 name(inName), eventWidth(inEventWidth), offset(inOffset),
65 headerSize(inHeaderSize), iNode(inNodeID.front()),
66 iFinesse(inNodeID.back()),
67 iNode_pcie40(inNodeID_pcie40.front()), iFinesse_pcie40(inNodeID_pcie40.back()),
68 delay(inDelay), cnttrg(inCnttrg),
69 debugLevel(inDebugLevel) {};
70
72 std::string name;
74 unsigned eventWidth;
76 unsigned offset;
80 int iNode;
87
88 /* information from Belle2Link header */
90 std::string firmwareType;
92 std::string firmwareVersion;
94 int& delay;
96 int& cnttrg;
97
100
105 virtual void reserve(int, std::array<int, nFinesse>, bool) {};
106
110 virtual void unpack(int,
111 std::array<int*, nFinesse>,
112 std::array<int, nFinesse>,
113 bool) {};
114
128 virtual int getHeaders(int subDetectorId,
129 std::array<int*, 48> data32tab,
130 std::array<int, 48> nWords,
131 bool pciedata)
132 {
133
134 int iNode_i = 0;
135 int iFinesse_i = 0;
136 if (pciedata) {
137 iNode_i = iNode_pcie40;
138 iFinesse_i = iFinesse_pcie40;
139 } else {
140 iNode_i = iNode;
141 iFinesse_i = iFinesse;
142 }
143
144 if (subDetectorId != iNode_i) {
145 return 0;
146 }
147 // int nWordsize = 3075; // temporary solution to hard coded the correct event size (for 2D only?)
148 // empty data buffer
149 if (nWords[iFinesse_i] < headerSize) {
150 B2WARNING("The module " << name << " does not have enough data (" <<
151 nWords[iFinesse_i] << "). Nothing will be unpacked.");
152 return 0;
153 } else if (nWords[iFinesse_i] == headerSize) {
154 B2DEBUG(20, "The module " << name <<
155 " contains only the header. Nothing will be unpacked.");
156 return 0;
157 }
158
159 // need one more check, give a warning if the event has wrong data size
160
161 // event data block header:
162 // 0xdddd --> correct event data (for 2D only?)
163 // 0xbbbb --> dummy buffer supposed to be used for only suppressed events.
164 if (nWords[iFinesse_i] > headerSize) {
165 //dataHeader = CDCTriggerUnpacker::rawIntToAscii(data32tab.at(iFinesse)[headerSize]&0xFFFF0000 >> 16);
166 //bool dataHeader = ( (data32tab.at(iFinesse)[headerSize]&0xffff0000) == 0xdddd0000);
167 long dataHeader = (data32tab.at(iFinesse_i)[headerSize] & 0xffff0000);
168 if (dataHeader != 0xdddd0000) {
169 B2DEBUG(30, "The module " << name << " has an event data header " << std::hex << std::setfill('0') << std::setw(4) <<
170 (dataHeader >> 16) <<
171 " in this event. It will be ignore.");
172 return 0;
173 }
174 B2DEBUG(50, "subdet and head size " << std::setfill('0') << std::hex << std::setw(8) << iNode_i << ", " << std::dec << std::setw(
175 0) << nWords[iFinesse_i] <<
176 " : " << std::hex << std::setw(8) << data32tab.at(iFinesse_i)[0] << " " << data32tab.at(iFinesse_i)[1] << " " << data32tab.at(
177 iFinesse_i)[2] <<
178 " " << data32tab.at(iFinesse_i)[3] << " dataheader = " << dataHeader);
179 }
180
181 /* get event header information
182 * Ideally, these parameters should not change in the same run,
183 * so it is more efficiency to do it in beginRun().
184 * However, since they are present in all events,
185 * let's check if they really remain unchanged.
186 */
187 if (headerSize >= 2) {
188 // supposedly these two Words will stay for all the versions
189 firmwareType = CDCTriggerUnpacker::rawIntToAscii(data32tab.at(iFinesse_i)[0]);
190 firmwareVersion = CDCTriggerUnpacker::rawIntToString(data32tab.at(iFinesse_i)[1]);
191 //int cnttrg = 0; // temporary solution, this should be one as a reference for comparison
192 int l1_revoclk = -1;
193
194 if (headerSize >= 3) {
195 std::bitset<wordWidth> thirdWord(data32tab.at(iFinesse_i)[2]);
196 l1_revoclk = CDCTriggerUnpacker::subset<32, 0, 11>(thirdWord).to_ulong();
197
198 if (firmwareType == "2D ") { // temporary solcuion, the following version number check is valid only for 2D
199
200 if (firmwareVersion > "19041700") { // started since 19041705
201 // the third word is cnttrg and L1_revoclk
202 int newCnttrg = CDCTriggerUnpacker::subset<32, 12, 31>(thirdWord).to_ulong();
203 cnttrg = newCnttrg;
204 } else if (firmwareVersion > "17121900") { // upto that version, headerSize == 2?
205 // the third word is b2l delay and L1_revoclk
206 int newDelay = CDCTriggerUnpacker::subset<32, 12, 20>
207 (thirdWord).to_ulong(); // or should be <32,12,19>? bit 31-20 are for prescale?
208 if (delay > 0 && delay != newDelay) {
209 B2WARNING(" the Belle2Link delay for " << name <<
210 "has changed from " << delay << " to " << newDelay << "!");
211 }
212 delay = newDelay;
213 }
214 }
215 }
216
217 B2DEBUG(20, name << ": " << firmwareType << ", version " <<
218 firmwareVersion << ", node " << std::hex << iNode_i <<
219 ", finesse " << iFinesse_i << ", delay: " << delay <<
220 ", cnttrg: " << cnttrg << std::dec << " == " << cnttrg << ", L1_revoclk " << l1_revoclk);
221
222
223 }
224 return 1;
225 };
227 virtual ~SubTrigger() {};
228 };
229
241
242 public:
243
248
250 void initialize() override;
251
253 void terminate() override;
254
256 void beginRun() override;
257
259 void event() override;
260
262 std::vector<float> unscaleNNOutput(std::vector<float> input) const;
265 static constexpr std::array<int, 9> nMergers = {10, 10, 12, 14, 16, 18, 20, 22, 24};
266
267 private:
270
278 bool m_decodeTSHit = false;
285 int m_n2DTS = 0; //TODO whats the best def val? /**< flag to unpack 2D tracker data with 15TS*/
286
292 std::vector<int> m_delayNNOutput;
293 std::vector<int> m_delayNNSelect;
297
300
303
306
309
310
313
316
319
322
325
328
331
334
337
339 //int m_2DFinderDelay = 0;
340 // since version 19041705, the B2L delay is removed, it should a fixed number for a long period and recorded in database.
341 int m_2DFinderDelay = 45; // 0x2d: changed from 0x28 since some time in 201902-03
342
345
347 // int m_Cnttrg = 0; // not used, commented out at 2019/07/31 by ytlai
354
356 unsigned m_exp = 0;
358 unsigned m_run = 0;
359
361 std::vector<SubTrigger*> m_subTrigger;
362
363 //condition database for number of TS in 2D
368 std::vector<float> m_NNOutputScale;
377 };
378
379
381}
382
383#endif /* CDCTRIGGERUNPACKERMODULE_H */
Class to keep all parameters of an expert MLP for the neuro trigger.
Definition: CDCTriggerMLP.h:20
Unpack the trigger data recorded in B2L.
static constexpr std::array< int, 9 > nMergers
data width of a single merger unit
CDCTriggerMLP m_mlp_scale
fake object to assign the user set scaling values to
int m_debugLevel
debug level specified in the steering file
StoreArray< RawTRG > m_rawTriggers
array containing the raw trigger data object
bool m_pciedata
PCIe40 data or copper data.
NodeList m_tracker2DNodeID_pcie40
list of (PCIe40 ID, ch ID) of 2D tracker
DBObjPtr< CDCTriggerNeuroConfig > m_cdctriggerneuroconfig
current neurotrigger config from database; used for unscaling network target
std::vector< float > m_NNOutputScale
output scale for the neural network output
int m_headerSize
number of words (number of bits / 32) of the B2L header
int m_mergerDelay
Belle2Link delay of the merger reader.
bool m_sim13dt
bool value wether to simulate 13 bit drift time by using 2dcc
StoreArray< MergerBits > m_mergerBits
merger output bitstream
StoreArray< CDCTriggerMLPInput > m_NeuroInputs
decoded input vector for neural network
void initialize() override
Register input and output data.
int m_NeuroDelay
Belle2Link delay of the neurotrigger.
void event() override
convert raw data (in B2L buffer to bitstream)
std::vector< int > m_delayNNOutput
delay of the NN output values clock cycle after the NN enable bit (by quadrant)
std::vector< float > unscaleNNOutput(std::vector< float > input) const
small function to rescale the NN output from -1, 1 to output scale
NodeList m_neuroNodeID
list of (COPPER ID, HSLB ID) of neurotrigger
StoreArray< CDCTriggerUnpacker::T2DOutputBitStream > m_bits2DTo3D
bitstream of 2D output to 3D/Neuro
bool m_decodeNeuro
flag to decode neurotrigger data
void terminate() override
Delete dynamically allocated variables.
int m_2DFinderDelay
Belle2Link delay of the 2D finder.
StoreArray< CDCTriggerSegmentHit > m_TSHits
decoded track segment hit
bool m_alignFoundTime
flag to align found time in different sub-modules
bool m_unpackTracker2D
flag to unpack 2D tracker data
CDCTriggerUnpackerModule()
Constructor: Sets the description, the properties and the parameters of the module.
NodeList m_tracker2DNodeID
list of (COPPER ID, HSLB ID) of 2D tracker
StoreArray< CDCTriggerSegmentHit > m_NNInputTSHitsAll
all decoded stereo track segment hits from the neural network input
StoreArray< CDCTriggerTrack > m_2DFinderTracks
decoded 2D finder track
bool m_decodeTSHit
flag to decode track segment
StoreArray< CDCTriggerTrack > m_NeuroTracks
decoded Neuro tracks
StoreArray< CDCTriggerTrack > m_NNInput2DFinderTracks
decoded 2D finder tracks from the neural network input
StoreObjPtr< BinnedEventT0 > m_ETFTime
store object for unpacked etf event time from neuro b2link
bool m_decode2DFinderInputTS
flag to decode 2D finder input TS
NodeList m_neuroNodeID_pcie40
list of (PCIe40 ID, ch ID) of neurotrigger
StoreArray< CDCTriggerUnpacker::NNBitStream > m_bitsNN
bitstream of Neuro input and output (including intermediate results)
MergerBits m_mergerBitsPerClock
Merger bits per clock.
NodeList m_mergerNodeID
list of (COPPER ID, HSLB ID) of Merger reader (TSF)
std::vector< int > m_delayNNSelect
delay of the NN selected TS clock cycle after the NN enable bit (by quadrant)
StoreArray< CDCTriggerFinderClone > m_2DFinderClones
additional information of the 2D finder track
StoreArray< CDCTriggerUnpacker::TSFOutputBitStream > m_bitsTo2D
bitstream of TSF output to 2D tracker
StoreArray< CDCTriggerSegmentHit > m_NNInputTSHits
decoded track segment hits from the neural network input
bool m_unpackNeuro
flag to unpack neurotrigger data
std::vector< SubTrigger * > m_subTrigger
vector holding the pointers to all the dynamically allocated SubTriggers
bool m_decode2DFinderTrack
flag to decode 2D finder track
bool m_useDB
bool value for wether to use the conditions database
bool m_unpackMerger
flag to unpack merger data (recorded by Merger Reader / TSF)
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Base class for Modules.
Definition: Module.h:72
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
static constexpr int wordWidth
width of a single word in the raw int buffer
std::array< std::bitset< mergerWidth >, nAllMergers > MergerBus
Merger data bus.
static constexpr int nFinesse
Number of FINESSE in a PCIe40.
static constexpr int mergerWidth
Merger data width.
std::vector< std::vector< int > > NodeList
Node list.
static constexpr int nAllMergers
Number of Mergers.
Abstract base class for different kinds of events.
enum class SubTriggerType : unsigned char {Merger, TSF, T2D, T3D, Neuro, ETF};
int iNode_pcie40
PCIe40 id of the board.
virtual void unpack(int, std::array< int *, nFinesse >, std::array< int, nFinesse >, bool)
Unpack the Belle2Link data and fill the Bitstream.
int headerSize
Size of the B2L header in words.
unsigned offset
The starting point of the data in an event.
virtual int getHeaders(int subDetectorId, std::array< int *, 48 > data32tab, std::array< int, 48 > nWords, bool pciedata)
Get the Belle2Link header information.
int debugLevel
debug level in the steering file
virtual ~SubTrigger()
destructor
SubTrigger(const std::string &inName, unsigned inEventWidth, unsigned inOffset, int inHeaderSize, const std::vector< int > &inNodeID, const std::vector< int > &inNodeID_pcie40, int &inDelay, int &inCnttrg, int inDebugLevel=0)
constructor
std::string firmwareVersion
version of the FPGA firmware
std::string firmwareType
type of the FPGA firmware
int iFinesse
FINESSE (HSLB) id) of the board.
int & delay
Reference to the variable of its Belle2Link delay.
int iFinesse_pcie40
PCIe40 ch id of the board.
int & cnttrg
counter of trgger signal, total 32 bits, the 20 LSBs recorded in the event header
std::string name
Name of the UT3.
unsigned eventWidth
Size of an event in the Belle2Link data in 32-bit words.
int iNode
COPPER id of the board.
virtual void reserve(int, std::array< int, nFinesse >, bool)
Calculate the number of clocks in the data, reserve that much of clocks in the Bitstream(s)