Belle II Software  release-06-02-00
trggdlUnpackerModule.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 // $Id$
10 //---------------------------------------------------------------
11 // Filename : trggdlUnpackerModule.cc
12 // Section : TRG GDL
13 // Owner :
14 // Email :
15 //---------------------------------------------------------------
16 // Description : TRG GDL Unpacker Module
17 //---------------------------------------------------------------
18 // 1.00 : 2017/07/26 : First version
19 //---------------------------------------------------------------
20 
21 #include <trg/gdl/modules/trggdlUnpacker/trggdlUnpackerModule.h>
22 
23 #include <iostream>
24 
25 using namespace std;
26 using namespace Belle2;
27 using namespace GDL;
28 
30 REG_MODULE(TRGGDLUnpacker);
31 
32 string TRGGDLUnpackerModule::version() const
33 {
34  return string("1.00");
35 }
36 
37 TRGGDLUnpackerModule::TRGGDLUnpackerModule()
38  : Module::Module()
39 {
40  string desc = "TRGGDLUnpackerModule(" + version() + ")";
41  setDescription(desc);
43  addParam("trgReadoutBoardSearch", m_trgReadoutBoardSearch,
44  "Print trigger readout board included in the data.",
45  false);
46  addParam("print_dbmap", m_print_dbmap, "Print Database Bit Map", false);
47  B2DEBUG(20, "trggdlunpacker: Constructor done.");
48 }
49 
51 {
52  store.registerInDataStore();
53 
54  unpack_flag = 1;
55 
56  //check bad run or not
57  bad_flag = m_dbbadrun->getflag();
58  if (bad_flag == -1) {
59  B2INFO("bad run");
60  unpack_flag = 0;
61  }
62 
63  //load unpacker format
64  if (!m_dbunpacker) {
65  B2INFO("no database of gdl unpacker");
66  unpack_flag = 0;
67  } else {
68  nword_header = m_dbunpacker->get_nword_header();
69  n_clocks = m_dbunpacker->getnClks();
70  nBits = m_dbunpacker->getnBits();
71  n_leafs = m_dbunpacker->getnLeafs();
72  n_leafsExtra = m_dbunpacker->getnLeafsExtra();
73  conf = m_dbunpacker->getconf();
74  for (int i = 0; i < 320; i++) {
75  LeafBitMap[i] = m_dbunpacker->getLeafMap(i);
76  }
77  for (int i = 0; i < 320; i++) {
78  strcpy(LeafNames[i], m_dbunpacker->getLeafnames(i));
79  }
80  conf_map = -1;
81  evt_map = -1;
82  clk_map = -1;
83  for (int i = 0; i < 320; i++) {
84  if (strcmp(LeafNames[i], "conf") == 0)conf_map = i;
85  if (strcmp(LeafNames[i], "evt") == 0) evt_map = i;
86  if (strcmp(LeafNames[i], "clk") == 0) clk_map = i;
87  }
88 
89  BitMap = vector<vector<int>>(n_leafs, vector<int>(2, 0));
90  for (int i = 0; i < n_leafs; i++) {
91  BitMap[i][0] = m_dbunpacker->getBitMap(i, 0);
92  BitMap[i][1] = m_dbunpacker->getBitMap(i, 1);
93  }
94  BitMap_extra = vector<vector<int>>(n_leafsExtra, vector<int>(3, 0));
95  for (int i = 0; i < n_leafsExtra; i++) {
96  BitMap_extra[i][0] = m_dbunpacker->getBitMap_extra(i, 0);
97  BitMap_extra[i][1] = m_dbunpacker->getBitMap_extra(i, 1);
98  BitMap_extra[i][2] = m_dbunpacker->getBitMap_extra(i, 2);
99  }
100 
101  if (m_print_dbmap) {
102 
103  int aBitMap[320][2] = {0};
104  int aBitMap_extra[100][3] = { -1};
105  for (int i = 0; i < n_leafsExtra; i++) {
106  aBitMap_extra[i][0] = m_dbunpacker->getBitMap_extra(i, 0);
107  aBitMap_extra[i][1] = m_dbunpacker->getBitMap_extra(i, 1);
108  aBitMap_extra[i][2] = m_dbunpacker->getBitMap_extra(i, 2);
109  }
110 
111  for (int i = 0; i < 200; i++) {
112  LeafBitMap[i] = m_dbunpacker->getLeafMap(i);
113  std::cout << "LeafBitMap[" << i << "] = " << m_dbunpacker->getLeafMap(i) << std::endl;
114  strcpy(LeafNames[i], m_dbunpacker->getLeafnames(i));
115  std::cout << "LeafNames[" << i << "] = " << m_dbunpacker->getLeafnames(i) << std::endl;
116  }
117  for (int i = 0; i < n_leafs; i++) {
118  aBitMap[i][0] = m_dbunpacker->getBitMap(i, 0);
119  aBitMap[i][1] = m_dbunpacker->getBitMap(i, 1);
120  }
121  for (int i = 0; i < 320; i++) {
122  int bin = m_dbunpacker->getLeafMap(i) + 1;
123  if (0 < bin && bin <= n_leafs) {
124  std::cout << "leaf(" << i
125  << "), bin(" << bin
126  << "), LeafNames[leaf](" << LeafNames[i]
127  << "), BitMap[bin-1][0](" << aBitMap[bin - 1][0]
128  << "), BitMap[bin-1][1](" << aBitMap[bin - 1][1]
129  << ")" << std::endl;
130  }
131  }
132  // for leafsExtra
133  for (int i = 0; i < 320; i++) {
134  int bin = m_dbunpacker->getLeafMap(i) + 1;
135  int j = bin - n_leafs - 1;
136  if (n_leafs < bin && bin <= n_leafs + n_leafsExtra) {
137  std::cout << "i(" << i
138  << "), bin(" << bin
139  << "), LeafNames[leaf](" << LeafNames[i]
140  << "), BitMap_extra[j][0](buf[" << aBitMap_extra[j][0]
141  << "]), BitMap_extra[j][1](downto " << aBitMap_extra[j][1]
142  << "), BitMap_extra[j][2](" << aBitMap_extra[j][2]
143  << " bit length)" << std::endl;
144  }
145  }
146 
147  }
148 
149  }
150 
151 }
152 
154 {
155  if (unpack_flag == 1) {
156  StoreArray<RawTRG> raw_trgarray;
157  for (int i = 0; i < raw_trgarray.getEntries(); i++) {
158 
159  // Check PCIe40 data or Copper data
160  if (raw_trgarray[i]->GetMaxNumOfCh(0) == 48) { m_pciedata = true; }
161  else if (raw_trgarray[i]->GetMaxNumOfCh(0) == 4) { m_pciedata = false; }
162  else { B2FATAL("TRGGRLUnpackerModule: Invalid value of GetMaxNumOfCh from raw data: " << LogVar("Number of ch: ", raw_trgarray[i]->GetMaxNumOfCh(0))); }
163 
164  unsigned int node_id = 0;
165  unsigned int ch_id = 0;
166  if (m_pciedata) {
167  node_id = 0x10000001;
168  ch_id = 21;
169  } else {
170  node_id = 0x15000001;
171  ch_id = 0;
172  }
173 
174 
175  for (int j = 0; j < raw_trgarray[i]->GetNumEntries(); j++) {
176  if (! m_trgReadoutBoardSearch) {
177  if (raw_trgarray[i]->GetNodeID(j) == node_id) {
178  int nword = raw_trgarray[i]->GetDetectorNwords(j, ch_id);
179  if (nword > 0) {
180  fillTreeGDLDB(raw_trgarray[i]->GetDetectorBuffer(j, ch_id),
181  raw_trgarray[i]->GetEveNo(j));
182  }
183  }
184  } else if (!m_pciedata) {
185 
186  unsigned cprid = raw_trgarray[i]->GetNodeID(j);
187  if ((0x15000001 <= cprid && cprid <= 0x15000002) ||
188  (0x11000001 <= cprid && cprid <= 0x11000010)) {
189  int _exp = raw_trgarray[i]->GetExpNo(j);
190  int _run = raw_trgarray[i]->GetRunNo(j);
191  for (int hslb = 0; hslb < 2; hslb++) {
192  int nword = raw_trgarray[i]->GetDetectorNwords(j, hslb);
193  int* buf = raw_trgarray[i]->GetDetectorBuffer(j, hslb);
194  printf("0x%x%c exp(%d), run(%d), nword(%d)",
195  cprid, 'a' + hslb, _exp, _run, nword);
196  if (nword > 2) {
197  printf(", 0x%x 0x%x 0x%x",
198  buf[0], buf[1], buf[2]);
199  }
200  printf("\n");
201  }
202  }
203 
204  } else if (m_pciedata) {
205 
206  unsigned pcie40id = raw_trgarray[i]->GetNodeID(j);
207  if (pcie40id == 0x10000001) {
208  int _exp = raw_trgarray[i]->GetExpNo(j);
209  int _run = raw_trgarray[i]->GetRunNo(j);
210  for (int hslb = 0; hslb < 48; hslb++) {
211  int nword = raw_trgarray[i]->GetDetectorNwords(j, hslb);
212  int* buf = raw_trgarray[i]->GetDetectorBuffer(j, hslb);
213  printf("0x%x ch%c exp(%d), run(%d), nword(%d)",
214  pcie40id, hslb, _exp, _run, nword);
215  if (nword > 2) {
216  printf(", 0x%x 0x%x 0x%x",
217  buf[0], buf[1], buf[2]);
218  }
219  printf("\n");
220  }
221  }
222 
223  }
224 
225  }
226  }
227  }
228 }
229 
230 
231 void TRGGDLUnpackerModule::fillTreeGDLDB(int* buf, int evt)
232 {
233 
234 
236  if (storeAry.isValid()) storeAry.clear();
237  for (int clk = 0; clk < n_clocks; clk++) {
238 
239  storeAry.appendNew();
240  int ntups = storeAry.getEntries() - 1;
241  vector<int*> Bits(n_leafs + n_leafsExtra);
242 
243  for (int i = 0; i < 320; i++) {
244  if (LeafBitMap[i] != -1) {
245  Bits[LeafBitMap[i]] = &(storeAry[ntups]->m_unpacker[i]);
246  strcpy(storeAry[ntups]->m_unpackername[i], LeafNames[i]);
247  }
248  }
249 
250 
251  for (int l = 0; l < n_leafs + n_leafsExtra; l++) *Bits[l] = 0;
252 
253  if (conf_map >= 0) storeAry[ntups]->m_unpacker[conf_map] = conf;
254  if (evt_map >= 0) storeAry[ntups]->m_unpacker[evt_map] = evt;
255  if (clk_map >= 0) storeAry[ntups]->m_unpacker[clk_map] = clk;
256 
257  for (int i = 0; i < n_leafsExtra; i++) {
258  if (BitMap_extra[i][0] != -1) {
259  if (BitMap_extra[i][1] == -1) *Bits[i + n_leafs] = buf[BitMap_extra[i][0]];
260  else *Bits[i + n_leafs] = (buf[BitMap_extra[i][0]] >> BitMap_extra[i][1]) & ((
261  1 << BitMap_extra[i][2]) - 1);
262  }
263  }
264 
265 
266  for (int _wd = 0; _wd < nBits / 32; _wd++) { // 0..19
267  unsigned wd = buf[clk * (nBits / 32) + _wd + nword_header];
268  for (int bb = 0; bb < 32; bb++) { // bit by bit
269  if ((wd >> (31 - bb)) & 1) { /* MSB to LSB */
270  int bitIn640 = (nBits - 1) - _wd * 32 - bb;
271  for (int leaf = 0; // Find a leaf that covers the bit.
272  leaf < n_leafs; leaf++) {
273  int bitMaxOfTheLeaf = BitMap[leaf][0];
274  int bitWidOfTheLeaf = BitMap[leaf][1];
275  int bitMinOfTheLeaf = bitMaxOfTheLeaf - bitWidOfTheLeaf;
276  if (bitMinOfTheLeaf <= bitIn640 && bitIn640 <= bitMaxOfTheLeaf) {
277  *Bits[leaf] |= (1 << (bitIn640 - bitMinOfTheLeaf));
278  }
279  }
280  }
281  }
282  }
283  }//clk
284 }
285 
286 
bool m_pciedata
PCIe40 data or copper data.
virtual void initialize() override
Initilizes TRGGDLUnpackerModuel.
virtual void event() override
Called event by event.
bool m_trgReadoutBoardSearch
flag to select board search mode
bool m_print_dbmap
flag to dump data base map
std::string version() const
returns version of TRGGDLUnpackerModule.
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
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
void clear() override
Delete all entries in this array.
Definition: StoreArray.h:207
Class to store variables with their name which were sent to the logging service.
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.