Belle II Software  release-05-02-19
CDCPackerModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * CDC packer module *
6  * Author: The Belle II Collaboration *
7  * Contributors: Satoru Yamada and Makoto Uchida *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 #include <cdc/modules/cdcPacker/CDCPackerModule.h>
13 #include <cdc/modules/cdcPacker/CDCChannelData.h>
14 #include <cdc/dataobjects/CDCHit.h>
15 #include <cdc/dataobjects/CDCRawHit.h>
16 #include <cdc/dbobjects/CDCChannelMap.h>
17 
18 #include <framework/logging/Logger.h>
19 #include <framework/utilities/FileSystem.h>
20 
21 #include <framework/database/DBArray.h>
22 
23 #include <iostream>
24 #include <cstring>
25 
26 using namespace std;
27 using namespace Belle2;
28 using namespace CDC;
29 
30 #define NUM_CDC_COPPER 75
31 //-----------------------------------------------------------------
32 // Register the Module
33 //-----------------------------------------------------------------
34 REG_MODULE(CDCPacker)
35 
36 //-----------------------------------------------------------------
37 // Implementation
38 //-----------------------------------------------------------------
39 
41 {
42  //Set module properties
43  setDescription("Generate RawCDC object from CDCHit");
44  setPropertyFlags(c_ParallelProcessingCertified);
45 
46  addParam("rawCDCName", m_rawCDCName, "Name of the RawCDC List name..", string(""));
47  addParam("cdcRawHitName", m_cdcRawHitName, "Name of the CDCRawHit (Suppressed mode).", string(""));
48  addParam("cdcHitName", m_cdcHitName, "Name of the CDCHit List name..", string(""));
49  addParam("fadcThreshold", m_fadcThreshold, "Threshold voltage (mV).", 10);
50  addParam("xmlMapFileName", m_xmlMapFileName, "path+name of the xml file",
51  string("/cdc/data/ch_map.dat"));
52  addParam("enableStoreRawCDC", m_enableStoreCDCRawHit, "Enable to store to the RawCDC object", true);
53  addParam("enablePrintOut", m_enablePrintOut, "Enable to print out the data to the terminal", true);
54  addParam("enableDatabase", m_enableDatabase, "Enable database to read the channel map.", true);
55 
56  m_channelMapFromDB.addCallback(this, &CDCPackerModule::loadMap);
57 }
58 
59 CDCPackerModule::~CDCPackerModule()
60 {
61 }
62 
63 void CDCPackerModule::initialize()
64 {
65 
66  B2INFO("CDCPacker: initialize() Called.");
67 
68  m_rawCDCs.registerInDataStore(m_rawCDCName);
69  StoreArray<CDCRawHit> storeCDCRawHits(m_cdcRawHitName);
70 
71  storeCDCRawHits.registerInDataStore();
72 
73  StoreArray<CDCHit> storeDigit(m_cdcHitName);
74 
75  storeDigit.registerInDataStore();
76 
77  loadMap();
78 
79  B2INFO("CDCPacker: FADC threshold: " << m_fadcThreshold);
80 
81  m_event = 0;
82 
83 }
84 
85 void CDCPackerModule::beginRun()
86 {
87  B2INFO("CDCPacker: beginRun() called.");
88 }
89 
90 int CDCPackerModule::getFEEID(int copper_id, int slot_id)
91 {
92  //
93  // The releation between COPPER ID and FEE ID depends on
94  // cable connection, which has not been finalized yet. ( 2015.6.16 )
95  //
96 
97  //#define DEFAULT
98 #ifdef DEFAULT
99  return (copper_id * 4 + slot_id);
100 #endif
101 
102  //#define PATTERN1
103 #ifdef PATTERN1
104  return (copper_id + slot_id * NUM_CDC_COPPER);
105 #endif
106 
107 #define PATTERN2
108 #ifdef PATTERN2
109  return ((copper_id / 15) * 60 + (copper_id % 15) + slot_id * 15);
110 #endif
111 
112 }
113 
114 void CDCPackerModule::event()
115 {
116 
117  // Create Data objects.
118  StoreArray<CDCRawHit> cdcRawHits(m_cdcRawHitName);
119  StoreArray<CDCHit> cdcHits(m_cdcHitName);
120  // std::vector<int> eWire_nhit(36882, 0);
121 
122  int tot_chdata_bytes[302];
123  memset(tot_chdata_bytes, 0, sizeof(int) * 302);
124 
125  const int ch_data_bytes = 8; // 8bytes ( 1hit/ch case)
126 
127  std::vector<CDCChannelData> chData;
128  chData.clear();
129 
130  for (const auto& hit : cdcHits) {
131  int eWire = (int)(hit.getID());
132  int sly = eWire / 4096;
133  int ily = (eWire % 4096) / 512;
134  int iwire = (eWire % 512);
135  short tdc = hit.getTDCCount();
136  short adc = hit.getADCCount();
137  unsigned short tot = hit.getTOT();
138  //
139  // If not prepared the map element for this cell, exit.
140  //
141  if (m_fee_board[ sly ][ ily ][ iwire] < 0 || m_fee_ch[ sly ][ ily ][ iwire] < 0) {
142  printf("Hit WireID %8d SL %3d IL %3d WI %4d BOARD %3d CH %3d\n",
143  (int)(hit.getID()), sly, ily , iwire,
144  m_fee_board[ sly ][ ily ][ iwire], m_fee_ch[ sly ][ ily ][ iwire]);
145  exit(1);
146  }
147 
148  if (hit.is2ndHit() == false) { // first hit timing for one cell.
149  // increase 8 bytes (4 bhytes).
150  tot_chdata_bytes[ m_fee_board[ sly ][ ily ][ iwire] ] += ch_data_bytes;
151  CDCChannelData chd(m_fee_board[sly][ily][iwire], m_fee_ch[sly][ily][iwire], 8, tot, adc, tdc);
152  chData.push_back(chd);
153  } else { // second hit timing
154  // Search ChData object for this cell.
155  const int boardId = m_fee_board[sly][ily][iwire];
156  const int channel = m_fee_ch[sly][ily][iwire];
157  auto fi = std::find_if(chData.begin(), chData.end(),
158  [&](CDCChannelData & ch) {
159  return (ch.getBoard() == boardId && ch.getChannel() == channel);
160  });
161  if (fi != chData.end()) {
162  tot_chdata_bytes[ m_fee_board[ sly ][ ily ][ iwire] ] += 2;
163  const size_t index = std::distance(chData.begin(), fi);
164  chData[index].setTDC2ndHit(tdc);
165  }
166  }
167 
168  }
169 
170  for (const auto& c : chData) {
171  int board = c.getBoard();
172  int ch = c.getChannel();
173  bool flag = c.is2ndHit();
174  int len = c.getDataLength();
175  if (!((len == 8 && flag == false) ||
176  (len == 10 && flag == true))) {
177  B2ERROR("inconsistent data object board : " << board << " ch " << ch);
178  }
179  }
180 
181 
182  RawCOPPERPackerInfo rawcprpacker_info;
183  rawcprpacker_info.exp_num = 0;
184  rawcprpacker_info.run_subrun_num = 1; // run number : 14bits, subrun # : 8bits
185  rawcprpacker_info.eve_num = m_event;
186  rawcprpacker_info.tt_ctime = 0x7123456;
187  rawcprpacker_info.tt_utime = 0xF1234567;
188  rawcprpacker_info.b2l_ctime = 0x7654321;
189 
190  const int packet_header_words = 3; // 12bytes
191 
192  for (int i = 0 ; i < NUM_CDC_COPPER; i++) {
193 
194  rawcprpacker_info.node_id = 0x02000000 + i + 1; // CDC detector ID + (node ID).
195  int nwords[4] = {0, 0, 0, 0};
196  int* buf[4] = {0, 0, 0, 0};
197 
198  for (int j = 0; j < 4; j++) {
199  int fee_id = getFEEID(i, j);
200  nwords[ j ] = ((tot_chdata_bytes[ fee_id ] + 3) / 4) + packet_header_words;
201 
202  buf[ j ] = new int[ nwords[ j ] ];
203 
204  // Store CDC header in buffer.
205  const char type = 0x20; // suppressed mode.
206  const char ver = 0x0; // version (always 0).
207  const short trigTime = 0x0;
208  const short dataLength = nwords[j] * 4 - packet_header_words * 4;
209  const int trigNum = m_event;
210 
211  *(buf[j] + 0) = (type << 24) | (ver << 16) | fee_id;
212  *(buf[j] + 1) = ((trigTime << 16) | dataLength);
213  *(buf[j] + 2) = trigNum;
214 
215  short* sbuf = (short*)(buf[j] + 3);
216 
217  bool halfOffset = false;
218  short reservedValue = 0xcccc;
219  for (const auto& c : chData) {
220  const int board = c.getBoard();
221  const int ch = c.getChannel();
222  const int len = c.getDataLength();
223  if (board == fee_id) {
224  // printf("board %.8x ch %.8d adc %.8x tdc %.8x\n" ,board, ch, c.getADCCount(), c.getTDCCount());
225  if (halfOffset == false) {
226  *sbuf++ = c.getTOT();
227  *sbuf++ = ((ch << 8) | len);
228  *sbuf++ = c.getTDCCount();
229  *sbuf++ = c.getADCCount();
230  if (c.is2ndHit()) {
231  reservedValue = c.getTDCCount2ndHit();
232  halfOffset = true;
233  }
234  } else {
235  *sbuf++ = ((ch << 8) | len);
236  *sbuf++ = reservedValue;
237  *sbuf++ = c.getADCCount();
238  *sbuf++ = c.getTOT();
239  if (c.is2ndHit()) {
240  *sbuf++ = c.getTDCCount2ndHit();
241  *sbuf++ = c.getTDCCount();
242  halfOffset = false;
243  } else {
244  reservedValue = c.getTDCCount();
245  }
246  }
247  }
248  }
249  if (halfOffset == true) {
250  *sbuf++ = 0xff02;
251  *sbuf = reservedValue;
252  }
253  }
254 
255  RawCDC* raw_cdc = m_rawCDCs.appendNew();
256  raw_cdc->PackDetectorBuf(buf[0], nwords[0],
257  buf[1], nwords[1],
258  buf[2], nwords[2],
259  buf[3], nwords[3],
260  rawcprpacker_info);
261 
262  for (int j = 0; j < 4; j++) {
263  if (buf[j] != NULL) delete [] buf[j];
264  }
265  }
266  m_event++;
267 
268 }
269 
270 void CDCPackerModule::endRun()
271 {
272  B2INFO("CDCPacker : End run.");
273 }
274 
275 void CDCPackerModule::terminate()
276 {
277  B2INFO("CDCPacker : Terminated.");
278 }
279 
280 
281 const WireID CDCPackerModule::getWireID(int iBoard, int iCh)
282 {
283  return m_map[iBoard][iCh];
284 }
285 
286 void CDCPackerModule::loadMap()
287 {
288 
289  // Frontend : 48 channels/board
290  // Number of board : 302
291 
292  for (int i = 0 ; i < 9; ++i) {
293  for (int j = 0 ; j < 8; ++j) {
294  for (int k = 0 ; k < 384; ++k) {
295  m_fee_board[i][j][k] = -1;
296  m_fee_ch[i][j][k] = -1;
297  }
298  }
299  }
300 
301  if (m_enableDatabase == false) {
302 
303  // Read the channel map from the local text.
304  std::string fileName = FileSystem::findFile(m_xmlMapFileName);
305  std::cout << fileName << std::endl;
306  if (fileName == "") {
307  B2ERROR("CDCPacker can't fine a filename: " << m_xmlMapFileName);
308  exit(1);
309  }
310 
311  ifstream ifs;
312  ifs.open(fileName.c_str());
313  int isl;
314  int il;
315  int iw;
316  int iBoard;
317  int iCh;
318 
319  while (!ifs.eof()) {
320 
321  ifs >> isl >> il >> iw >> iBoard >> iCh;
322  if (isl >= 9) continue; // Super layers should be from 0 t0 8.
323  const WireID wireId(isl, il, iw);
324  m_map[iBoard][iCh] = wireId;
325  m_fee_board[isl][il][iw] = iBoard;
326  m_fee_ch[isl][il][iw] = iCh;
327  }
328  } else {
329  // Read the channel map from the database.
330  for (const auto& cm : m_channelMapFromDB) {
331  const int isl = cm.getISuperLayer();
332  if (isl >= 9) continue; // Super layers should be from 0 t0 8.
333  const int il = cm.getILayer();
334  const int iw = cm.getIWire();
335  const int iBoard = cm.getBoardID();
336  const int iCh = cm.getBoardChannel();
337  const WireID wireId(isl, il, iw);
338  m_map[iBoard][iCh] = wireId;
339  m_fee_board[isl][il][iw] = iBoard;
340  m_fee_ch[isl][il][iw] = iCh;
341  }
342  }
343 }
344 
Belle2::RawCOPPERPackerInfo::tt_utime
unsigned int tt_utime
27bit clock ticks at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user...
Definition: RawCOPPERPackerInfo.h:28
Belle2::RawCOPPERPackerInfo
struct to contain header information used by RawCOPPERFormat::Packer()
Definition: RawCOPPERPackerInfo.h:12
Belle2::RawCOPPERPackerInfo::exp_num
unsigned int exp_num
Experiment number (10bit)
Definition: RawCOPPERPackerInfo.h:16
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::RawCOPPERPackerInfo::node_id
unsigned int node_id
Event Number (32bit)
Definition: RawCOPPERPackerInfo.h:22
Belle2::CDC::CDCChannelData
CDCChannelData.
Definition: CDCChannelData.h:37
Belle2::RawCOPPERPackerInfo::b2l_ctime
unsigned int b2l_ctime
32bit unitx time at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user ...
Definition: RawCOPPERPackerInfo.h:31
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::WireID::getISuperLayer
unsigned short getISuperLayer() const
Getter for Super-Layer.
Definition: WireID.h:140
Belle2::RawCOPPERPackerInfo::eve_num
unsigned int eve_num
Run # and subrun # ( 22bit )
Definition: RawCOPPERPackerInfo.h:20
Belle2::RawCOPPERPackerInfo::tt_ctime
unsigned int tt_ctime
Node ID (32bit)
Definition: RawCOPPERPackerInfo.h:25
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::RawCDC
The Raw CDC class Class for RawCOPPER class data taken by CDC Currently, this class is almost same as...
Definition: RawCDC.h:27
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RawCOPPERPackerInfo::run_subrun_num
unsigned int run_subrun_num
Experiment number (10bit)
Definition: RawCOPPERPackerInfo.h:18
Belle2::CDC::CDCPackerModule
CDCPackerModule: The CDC Raw Hits Decoder.
Definition: CDCPackerModule.h:45
Belle2::RawCOPPER::PackDetectorBuf
void PackDetectorBuf(int *detector_buf_1st, int nwords_1st, int *detector_buf_2nd, int nwords_2nd, int *detector_buf_3rd, int nwords_3rd, int *detector_buf_4th, int nwords_4th, RawCOPPERPackerInfo rawcprpacker_info)
Packer for RawCOPPER class Pack data (format ver.
Definition: RawCOPPER.cc:183
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33