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