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