Belle II Software  release-06-00-14
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/database/DBObjPtr.h>
16 
17 #include <trg/cdc/dataobjects/Bitstream.h>
18 #include <trg/cdc/Unpacker.h>
19 #include <trg/cdc/dataobjects/CDCTriggerTrack.h>
20 #include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
21 #include <trg/cdc/dataobjects/CDCTriggerFinderClone.h>
22 #include <trg/cdc/dataobjects/CDCTriggerMLPInput.h>
23 #include <trg/cdc/dbobjects/CDCTrigger2DConfig.h>
24 #include <trg/cdc/dataobjects/CDCTriggerMLP.h>
25 #include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
26 
27 #include <array>
28 #include <bitset>
29 #include <vector>
30 #include <string>
31 #include <iomanip>
32 
33 namespace Belle2 {
40  using NodeList = std::vector<std::vector<int> >;
41 
43  static constexpr int mergerWidth = 256;
45  static constexpr int nAllMergers = 146;
47  static constexpr int wordWidth = 32;
49  static constexpr int nFinesse = 4;
51  using MergerBus = std::array<std::bitset<mergerWidth>, nAllMergers>;
54 
56  struct SubTrigger {
58  SubTrigger(const std::string& inName,
59  unsigned inEventWidth, unsigned inOffset,
60  int inHeaderSize, const std::vector<int>& inNodeID,
61  int& inDelay, int& inCnttrg, int inDebugLevel = 0) :
62  name(inName), eventWidth(inEventWidth), offset(inOffset),
63  headerSize(inHeaderSize), iNode(inNodeID.front()),
64  iFinesse(inNodeID.back()), delay(inDelay),
65  cnttrg(inCnttrg),
66  debugLevel(inDebugLevel) {};
67 
69  std::string name;
71  unsigned eventWidth;
73  unsigned offset;
77  int iNode;
79  int iFinesse;
80 
81  /* information from Belle2Link header */
83  std::string firmwareType;
85  std::string firmwareVersion;
87  int& delay;
89  int& cnttrg;
90 
93 
102  virtual void reserve(int, std::array<int, nFinesse>) {};
103 
113  virtual void unpack(int,
114  std::array<int*, nFinesse>,
115  std::array<int, nFinesse>) {};
116 
128  virtual int getHeaders(int subDetectorId,
129  std::array<int*, 4> data32tab,
130  std::array<int, 4> nWords)
131  {
132  if (subDetectorId != iNode) {
133  return 0;
134  }
135  // int nWordsize = 3075; // temporary solution to hard coded the correct event size (for 2D only?)
136  // empty data buffer
137  if (nWords[iFinesse] < headerSize) {
138  B2WARNING("The module " << name << " does not have enough data (" <<
139  nWords[iFinesse] << "). Nothing will be unpacked.");
140  return 0;
141  } else if (nWords[iFinesse] == headerSize) {
142  B2DEBUG(20, "The module " << name <<
143  " contains only the header. Nothing will be unpacked.");
144  return 0;
145  }
146 
147  // need one more check, give a warning if the event has wrong data size
148 
149  // event data block header:
150  // 0xdddd --> correct event data (for 2D only?)
151  // 0xbbbb --> dummy buffer supposed to be used for only suppressed events.
152  if (nWords[iFinesse] > headerSize) {
153  //dataHeader = CDCTriggerUnpacker::rawIntToAscii(data32tab.at(iFinesse)[headerSize]&0xFFFF0000 >> 16);
154  //bool dataHeader = ( (data32tab.at(iFinesse)[headerSize]&0xffff0000) == 0xdddd0000);
155  long dataHeader = (data32tab.at(iFinesse)[headerSize] & 0xffff0000);
156  if (dataHeader != 0xdddd0000) {
157  B2DEBUG(30, "The module " << name << " has an event data header " << std::hex << std::setfill('0') << std::setw(4) <<
158  (dataHeader >> 16) <<
159  " in this event. It will be ignore.");
160  return 0;
161  }
162  B2DEBUG(50, "subdet and head size " << std::setfill('0') << std::hex << std::setw(8) << iNode << ", " << std::dec << std::setw(
163  0) << nWords[iFinesse] <<
164  " : " << std::hex << std::setw(8) << data32tab.at(iFinesse)[0] << " " << data32tab.at(iFinesse)[1] << " " << data32tab.at(
165  iFinesse)[2] <<
166  " " << data32tab.at(iFinesse)[3] << " dataheader = " << dataHeader);
167  }
168 
169  /* get event header information
170  * Ideally, these parameters should not change in the same run,
171  * so it is more efficiency to do it in beginRun().
172  * However, since they are present in all events,
173  * let's check if they really remain unchanged.
174  */
175  if (headerSize >= 2) {
176  // supposedly these two Words will stay for all the versions
177  firmwareType = CDCTriggerUnpacker::rawIntToAscii(data32tab.at(iFinesse)[0]);
178  firmwareVersion = CDCTriggerUnpacker::rawIntToString(data32tab.at(iFinesse)[1]);
179  //int cnttrg = 0; // temporary solution, this should be one as a reference for comparison
180  int l1_revoclk = -1;
181 
182  if (headerSize >= 3) {
183  std::bitset<wordWidth> thirdWord(data32tab.at(iFinesse)[2]);
184  l1_revoclk = CDCTriggerUnpacker::subset<32, 0, 11>(thirdWord).to_ulong();
185 
186  if (firmwareType == "2D ") { // temporary solcuion, the following version number check is valid only for 2D
187 
188  if (firmwareVersion > "19041700") { // started since 19041705
189  // the third word is cnttrg and L1_revoclk
190  int newCnttrg = CDCTriggerUnpacker::subset<32, 12, 31>(thirdWord).to_ulong();
191  cnttrg = newCnttrg;
192  } else if (firmwareVersion > "17121900") { // upto that version, headerSize == 2?
193  // the third word is b2l delay and L1_revoclk
194  int newDelay = CDCTriggerUnpacker::subset<32, 12, 20>
195  (thirdWord).to_ulong(); // or should be <32,12,19>? bit 31-20 are for prescale?
196  if (delay > 0 && delay != newDelay) {
197  B2WARNING(" the Belle2Link delay for " << name <<
198  "has changed from " << delay << " to " << newDelay << "!");
199  }
200  delay = newDelay;
201  }
202  }
203  }
204 
205  B2DEBUG(20, name << ": " << firmwareType << ", version " <<
206  firmwareVersion << ", node " << std::hex << iNode <<
207  ", finesse " << iFinesse << ", delay: " << delay <<
208  ", cnttrg: " << cnttrg << std::dec << " == " << cnttrg << ", L1_revoclk " << l1_revoclk);
209 
210 
211  }
212  return 1;
213  };
215  virtual ~SubTrigger() {};
216  };
217 
229 
230  public:
231 
236 
238  void initialize() override;
239 
241  void terminate() override;
242 
244  void beginRun() override;
245 
247  void event() override;
248 
250  std::vector<float> unscaleNNOutput(std::vector<float> input) const;
253  static constexpr std::array<int, 9> nMergers = {10, 10, 12, 14, 16, 18, 20, 22, 24};
254 
255  private:
258 
266  bool m_decodeTSHit = false;
272  int m_n2DTS = 0; //TODO whats the best def val? /**< flag to unpack 2D tracker data with 15TS*/
273 
278  std::vector<int> m_delayNNOutput;
279  std::vector<int> m_delayNNSelect;
283 
286 
289 
292 
295 
296 
299 
302 
305 
308 
311 
314 
316  int m_debugLevel = 0;
317 
319  int m_mergerDelay = 0;
320 
322  //int m_2DFinderDelay = 0;
323  // since version 19041705, the B2L delay is removed, it should a fixed number for a long period and recorded in database.
324  int m_2DFinderDelay = 45; // 0x2d: changed from 0x28 since some time in 201902-03
325 
327  int m_NeuroDelay = 0;
328 
330  // int m_Cnttrg = 0; // not used, commented out at 2019/07/31 by ytlai
332  int m_mergerCnttrg = 0;
336  int m_NeuroCnttrg = 0;
337 
339  unsigned m_exp = 0;
341  unsigned m_run = 0;
342 
344  std::vector<SubTrigger*> m_subTrigger;
345 
346  //condition database for number of TS in 2D
351  std::vector<float> m_NNOutputScale;
355  bool m_useDB;
357  bool m_sim13dt;
358  };
359 
360 
362 }
363 
364 #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
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)
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
bool m_decode2DFinderInputTS
flag to decode 2D finder input TS
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)
std::vector< float > unscaleNNOutput(std::vector< float > input) const
small function to rescale the NN output from -1, 1 to output scale
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
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 the copper.
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 headerSize
Size of the B2L header in words.
unsigned offset
The starting point of the data in an event.
SubTrigger(const std::string &inName, unsigned inEventWidth, unsigned inOffset, int inHeaderSize, const std::vector< int > &inNodeID, int &inDelay, int &inCnttrg, int inDebugLevel=0)
constructor
int debugLevel
debug level in the steering file
virtual ~SubTrigger()
destructor
virtual void reserve(int, std::array< int, nFinesse >)
Calculate the number of clocks in the data, reserve that much of clocks in the Bitstream(s)
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 & cnttrg
counter of trgger signal, total 32 bits, the 20 LSBs recorded in the event header
std::string name
Name of the UT3.
virtual int getHeaders(int subDetectorId, std::array< int *, 4 > data32tab, std::array< int, 4 > nWords)
Get the Belle2Link header information.
unsigned eventWidth
Size of an event in the Belle2Link data in 32-bit words.
virtual void unpack(int, std::array< int *, nFinesse >, std::array< int, nFinesse >)
Unpack the Belle2Link data and fill the Bitstream.
int iNode
COPPER id of the board.